bool RewriteUtils::getEntireDeclGroupStrAndRemove(DeclGroupRef DGR, std::string &Str) { Decl *FirstD, *LastD; if (DGR.isSingleDecl()) { FirstD = DGR.getSingleDecl(); LastD = FirstD; } else { DeclGroupRef::iterator I = DGR.begin(); FirstD = (*I); DeclGroupRef::iterator E = DGR.end(); --E; LastD = (*E); } SourceRange FirstRange = FirstD->getSourceRange(); SourceLocation StartLoc = FirstRange.getBegin(); SourceRange LastRange = LastD->getSourceRange(); SourceLocation EndLoc = getEndLocationUntil(LastRange, ';'); getStringBetweenLocs(Str, StartLoc, EndLoc); return !TheRewriter->RemoveText(SourceRange(StartLoc, EndLoc)); }
SourceLocation RewriteUtils::getDeclGroupRefEndLoc(DeclGroupRef DGR) { Decl *LastD; if (DGR.isSingleDecl()) { LastD = DGR.getSingleDecl(); } else { DeclGroupRef::iterator E = DGR.end(); --E; LastD = (*E); } #if 0 VarDecl *VD = dyn_cast<VarDecl>(LastD); TransAssert(VD && "Bad VD!"); SourceRange VarRange = VD->getSourceRange(); return getEndLocationFromBegin(VarRange); #endif // The LastD could be a RecordDecl SourceRange Range = LastD->getSourceRange(); SourceLocation EndLoc = getEndLocationFromBegin(Range); TransAssert(EndLoc.isValid() && "Invalid EndLoc!"); return EndLoc; }
// This function skips type specifiers bool RewriteUtils::getDeclGroupStrAndRemove(DeclGroupRef DGR, std::string &Str) { if (DGR.isSingleDecl()) { Decl *D = DGR.getSingleDecl(); VarDecl *VD = dyn_cast<VarDecl>(D); TransAssert(VD && "Bad VarDecl!"); // We need to get the outermost TypeLocEnd instead of the StartLoc of // a var name, because we need to handle the case below: // int *x; // int *y; // If we rely on the StartLoc of a var name, then we will make bad // transformation like: // int *x, y; SourceLocation TypeLocEnd = getVarDeclTypeLocEnd(VD); SourceRange VarRange = VD->getSourceRange(); SourceLocation LocEnd = getEndLocationUntil(VarRange, ';'); getStringBetweenLocs(Str, TypeLocEnd, LocEnd); SourceLocation StartLoc = VarRange.getBegin(); SourceLocation NewEndLoc = getLocationAfterSkiping(LocEnd, ';'); return !(TheRewriter->RemoveText(SourceRange(StartLoc, NewEndLoc))); } TransAssert(DGR.getDeclGroup().size() > 1); DeclGroupRef::iterator I = DGR.begin(); DeclGroupRef::iterator E = DGR.end(); --E; Decl *FirstD = (*I); VarDecl *FirstVD = dyn_cast<VarDecl>(FirstD); Decl *LastD = (*E); VarDecl *LastVD = dyn_cast<VarDecl>(LastD); TransAssert(FirstVD && "Bad First VarDecl!"); TransAssert(LastVD && "Bad First VarDecl!"); SourceLocation TypeLocEnd = getVarDeclTypeLocEnd(FirstVD); SourceRange LastVarRange = LastVD->getSourceRange(); SourceLocation LastEndLoc = getEndLocationUntil(LastVarRange, ';'); getStringBetweenLocs(Str, TypeLocEnd, LastEndLoc); SourceLocation StartLoc = FirstVD->getLocStart(); SourceLocation NewLastEndLoc = getLocationAfterSkiping(LastEndLoc, ';'); return !(TheRewriter->RemoveText(SourceRange(StartLoc, NewLastEndLoc))); }