From: Chris Lattner Date: Thu, 31 Jan 2008 19:37:57 +0000 (+0000) Subject: Make rewriter::inserttext return a bool to indicate if it failed. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dcbc5b0b0722282a0fdd829359fe0d7e22adb882;p=clang Make rewriter::inserttext return a bool to indicate if it failed. Add a RewriteTest::ReplaceStmt method to factor the 'checking for rewrite failed + emitting diagnostic if so' code. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46619 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index d873a34721..36943d6bc3 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -170,6 +170,20 @@ namespace { "rewriting sub-expression within a macro (may not be correct)"); } ~RewriteTest(); + + void ReplaceStmt(Stmt *Old, Stmt *New) { + if (!Rewrite.ReplaceStmt(Old, New)) + return; + + // Replacement failed, report a warning unless disabled. + if (SilenceRewriteMacroWarning) return; + + SourceRange Range = Old->getSourceRange(); + Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag, + 0, 0, &Range, 1); + } + + // Syntactic Rewriting. void RewritePrologue(SourceLocation Loc); @@ -708,13 +722,7 @@ Stmt *RewriteTest::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { if (IV->isFreeIvar()) { Expr *Replacement = new MemberExpr(IV->getBase(), true, D, IV->getLocation()); - if (Rewrite.ReplaceStmt(IV, Replacement)) { - // replacement failed. - SourceRange Range = IV->getSourceRange(); - if (!SilenceRewriteMacroWarning) - Diags.Report(Context->getFullLoc(IV->getLocation()), RewriteFailedDiag, - 0, 0, &Range, 1); - } + ReplaceStmt(IV, Replacement); delete IV; return Replacement; } else { @@ -734,13 +742,7 @@ Stmt *RewriteTest::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { CastExpr *castExpr = new CastExpr(castT, IV->getBase(), SourceLocation()); // Don't forget the parens to enforce the proper binding. ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), castExpr); - if (Rewrite.ReplaceStmt(IV->getBase(), PE)) { - // replacement failed. - SourceRange Range = IV->getBase()->getSourceRange(); - if (!SilenceRewriteMacroWarning) - Diags.Report(Context->getFullLoc(IV->getBase()->getLocStart()), - RewriteFailedDiag, 0, 0, &Range, 1); - } + ReplaceStmt(IV->getBase(), PE); delete IV->getBase(); return PE; } @@ -1267,7 +1269,7 @@ Stmt *RewriteTest::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { return 0; } -// This can't be done with Rewrite.ReplaceStmt(S, ThrowExpr), since +// This can't be done with ReplaceStmt(S, ThrowExpr), since // the throw expression is typically a message expression that's already // been rewritten! (which implies the SourceLocation's are invalid). Stmt *RewriteTest::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { @@ -1301,13 +1303,7 @@ Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) { Expr *Replacement = new StringLiteral(StrEncoding.c_str(), StrEncoding.length(), false, StrType, SourceLocation(), SourceLocation()); - if (Rewrite.ReplaceStmt(Exp, Replacement)) { - // replacement failed. - SourceRange Range = Exp->getSourceRange(); - if (!SilenceRewriteMacroWarning) - Diags.Report(Context->getFullLoc(Exp->getAtLoc()), RewriteFailedDiag, - 0, 0, &Range, 1); - } + ReplaceStmt(Exp, Replacement); // Replace this subexpr in the parent. delete Exp; @@ -1325,13 +1321,7 @@ Stmt *RewriteTest::RewriteAtSelector(ObjCSelectorExpr *Exp) { SourceLocation())); CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, &SelExprs[0], SelExprs.size()); - if (Rewrite.ReplaceStmt(Exp, SelExp)) { - // replacement failed. - SourceRange Range = Exp->getSourceRange(); - if (!SilenceRewriteMacroWarning) - Diags.Report(Context->getFullLoc(Exp->getAtLoc()), RewriteFailedDiag, - 0, 0, &Range, 1); - } + ReplaceStmt(Exp, SelExp); delete Exp; return SelExp; } @@ -1657,13 +1647,7 @@ Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { &StrExpr[0], StrExpr.size()); // cast to NSConstantString * CastExpr *cast = new CastExpr(Exp->getType(), call, SourceLocation()); - if (Rewrite.ReplaceStmt(Exp, cast)) { - // replacement failed. - SourceRange Range = Exp->getSourceRange(); - if (!SilenceRewriteMacroWarning) - Diags.Report(Context->getFullLoc(Exp->getAtLoc()), RewriteFailedDiag, - 0, 0, &Range, 1); - } + ReplaceStmt(Exp, cast); delete Exp; return cast; #else @@ -1701,7 +1685,7 @@ Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { SourceLocation()); // cast to NSConstantString * cast = new CastExpr(Exp->getType(), Unop, SourceLocation()); - Rewrite.ReplaceStmt(Exp, cast); + ReplaceStmt(Exp, cast); delete Exp; return cast; #endif @@ -2037,13 +2021,7 @@ Stmt *RewriteTest::SynthMessageExpr(ObjCMessageExpr *Exp) { Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) { Stmt *ReplacingStmt = SynthMessageExpr(Exp); // Now do the actual rewrite. - if (Rewrite.ReplaceStmt(Exp, ReplacingStmt)) { - // replacement failed. - SourceRange Range = Exp->getSourceRange(); - if (!SilenceRewriteMacroWarning) - Diags.Report(Context->getFullLoc(Exp->getLocStart()), RewriteFailedDiag, - 0, 0, &Range, 1); - } + ReplaceStmt(Exp, ReplacingStmt); delete Exp; return ReplacingStmt; @@ -2064,13 +2042,7 @@ Stmt *RewriteTest::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, &ProtoExprs[0], ProtoExprs.size()); - if (Rewrite.ReplaceStmt(Exp, ProtoExp)) { - // replacement failed. - SourceRange Range = Exp->getSourceRange(); - if (!SilenceRewriteMacroWarning) - Diags.Report(Context->getFullLoc(Exp->getAtLoc()), RewriteFailedDiag, - 0, 0, &Range, 1); - } + ReplaceStmt(Exp, ProtoExp); delete Exp; return ProtoExp; diff --git a/Rewrite/Rewriter.cpp b/Rewrite/Rewriter.cpp index 62d0432f0a..9370145e47 100644 --- a/Rewrite/Rewriter.cpp +++ b/Rewrite/Rewriter.cpp @@ -206,12 +206,13 @@ RewriteBuffer &Rewriter::getEditBuffer(unsigned FileID) { /// InsertText - Insert the specified string at the specified location in the /// original buffer. This method is only valid on rewritable source /// locations. -void Rewriter::InsertText(SourceLocation Loc, +bool Rewriter::InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen) { - assert(isRewritable(Loc) && "Not a rewritable location!"); + if (!isRewritable(Loc)) return true; unsigned FileID; unsigned StartOffs = getLocationOffsetAndFileID(Loc, FileID); getEditBuffer(FileID).InsertText(StartOffs, StrData, StrLen); + return false; } /// RemoveText - Remove the specified text region. This method is only valid diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj index f8b31b64bc..7e2c3576e8 100644 --- a/clang.xcodeproj/project.pbxproj +++ b/clang.xcodeproj/project.pbxproj @@ -576,9 +576,9 @@ DE928B7C0C0A615100231DA4 /* CodeGenModule.h */, DE928B7E0C0A615600231DA4 /* CodeGenModule.cpp */, DEEBC3BB0C2363BC00A9FE82 /* CodeGenTypes.cpp */, + DEEBC3B90C2363B800A9FE82 /* CodeGenTypes.h */, 1ABC36930C7A4BDC006DB0AB /* CGBuiltin.cpp */, DE4264FB0C113592005A861D /* CGDecl.cpp */, - DEEBC3B90C2363B800A9FE82 /* CodeGenTypes.h */, DE4772FB0C10EAEC002239E8 /* CGExpr.cpp */, DEF2EFF20C6CDD74000C4259 /* CGExprAgg.cpp */, DE224FF70C7AA98800D370A5 /* CGExprComplex.cpp */, diff --git a/include/clang/Rewrite/Rewriter.h b/include/clang/Rewrite/Rewriter.h index 3e6dc9a9e9..857c77b16e 100644 --- a/include/clang/Rewrite/Rewriter.h +++ b/include/clang/Rewrite/Rewriter.h @@ -127,9 +127,9 @@ public: int getRangeSize(SourceRange Range) const; /// InsertText - Insert the specified string at the specified location in the - /// original buffer. This method is only valid on rewritable source - /// locations. - void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen); + /// original buffer. This method returns true (and does nothing) if the input + /// location was not rewritable, false otherwise. + bool InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen); /// RemoveText - Remove the specified text region. This method is only valid /// on a rewritable source location.