From 311ff02fae0392bee6abe7723cdf5a69b2899a47 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 16 Oct 2007 22:36:42 +0000 Subject: [PATCH] Add a new Rewriter::getRangeSize method. Rename SourceRange::Begin()/End() to getBegin()/getEnd() for consistency with other code. Start building the rewriter towards handling @encode. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43047 91177308-0d34-0410-b5e6-96231b3b80d8 --- AST/StmtDumper.cpp | 6 +++--- Analysis/DeadStores.cpp | 2 +- Analysis/UninitializedValues.cpp | 2 +- Driver/RewriteTest.cpp | 29 ++++++++++++++++++++++++++++ Driver/TextDiagnosticPrinter.cpp | 10 +++++----- Rewrite/Rewriter.cpp | 19 ++++++++++++++++++ Sema/SemaDecl.cpp | 2 +- clang.xcodeproj/project.pbxproj | 1 - include/clang/AST/Expr.h | 2 +- include/clang/AST/ExprCXX.h | 2 +- include/clang/AST/Stmt.h | 4 ++-- include/clang/Basic/SourceLocation.h | 4 ++-- include/clang/Rewrite/Rewriter.h | 8 ++++++-- 13 files changed, 71 insertions(+), 20 deletions(-) diff --git a/AST/StmtDumper.cpp b/AST/StmtDumper.cpp index 604d473479..39d2effd5a 100644 --- a/AST/StmtDumper.cpp +++ b/AST/StmtDumper.cpp @@ -164,10 +164,10 @@ void StmtDumper::DumpSourceRange(const Stmt *Node) { SourceRange R = Node->getSourceRange(); fprintf(stderr, " <"); - DumpLocation(R.Begin()); - if (R.Begin() != R.End()) { + DumpLocation(R.getBegin()); + if (R.getBegin() != R.getEnd()) { fprintf(stderr, ", "); - DumpLocation(R.End()); + DumpLocation(R.getEnd()); } fprintf(stderr, ">"); diff --git a/Analysis/DeadStores.cpp b/Analysis/DeadStores.cpp index 1b85138029..658fe949a8 100644 --- a/Analysis/DeadStores.cpp +++ b/Analysis/DeadStores.cpp @@ -40,7 +40,7 @@ public: if (VarDecl* VD = dyn_cast(DR->getDecl())) if (VD->hasLocalStorage() && !Live(VD,AD)) { SourceRange R = B->getRHS()->getSourceRange(); - Diags.Report(DR->getSourceRange().Begin(), diag::warn_dead_store, + Diags.Report(DR->getSourceRange().getBegin(), diag::warn_dead_store, 0, 0, &R, 1); } } diff --git a/Analysis/UninitializedValues.cpp b/Analysis/UninitializedValues.cpp index a18319a62b..6f24872cd2 100644 --- a/Analysis/UninitializedValues.cpp +++ b/Analysis/UninitializedValues.cpp @@ -218,7 +218,7 @@ public: if (V(VD,AD) == Uninitialized) if (AlreadyWarned.insert(VD)) - Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val); + Diags.Report(DR->getSourceRange().getBegin(), diag::warn_uninit_val); } }; } // end anonymous namespace diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index 5b27f55190..731d6fe777 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -36,6 +36,9 @@ namespace { void HandleDeclInMainFile(Decl *D); void RewriteInclude(SourceLocation Loc); + + void RewriteFunctionBody(Stmt *S); + void RewriteAtEncode(ObjCEncodeExpr *Exp); ~RewriteTest(); }; @@ -86,10 +89,36 @@ void RewriteTest::RewriteInclude(SourceLocation Loc) { /// HandleDeclInMainFile - This is called for each top-level decl defined in the /// main file of the input. void RewriteTest::HandleDeclInMainFile(Decl *D) { + if (FunctionDecl *FD = dyn_cast(D)) + if (Stmt *Body = FD->getBody()) + RewriteFunctionBody(Body); // Nothing yet. } +void RewriteTest::RewriteFunctionBody(Stmt *S) { + // Handle specific things. + if (ObjCEncodeExpr *AtEncode = dyn_cast(S)) + return RewriteAtEncode(AtEncode); + + // Otherwise, just rewrite all children. + for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); + CI != E; ++CI) + RewriteFunctionBody(*CI); +} + +void RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) { +#if 0 + int Size = Rewrite.getRangeSize(Exp->getSourceRange()); + if (Size == -1) { + printf("BLAH!"); + } + + Rewrite.RemoveText(Exp->getEncLoc(), Size); +#endif +} + + RewriteTest::~RewriteTest() { // Get the top-level buffer that this corresponds to. std::pair MainBuf = SM->getBufferData(MainFileID); diff --git a/Driver/TextDiagnosticPrinter.cpp b/Driver/TextDiagnosticPrinter.cpp index 3a90a4f548..2a3bd0d954 100644 --- a/Driver/TextDiagnosticPrinter.cpp +++ b/Driver/TextDiagnosticPrinter.cpp @@ -54,16 +54,16 @@ void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, "Expect a correspondence between source and carat line!"); if (!R.isValid()) return; - unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.Begin()); + unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.getBegin()); if (StartLineNo > LineNo) return; // No intersection. - unsigned EndLineNo = SourceMgr.getLogicalLineNumber(R.End()); + unsigned EndLineNo = SourceMgr.getLogicalLineNumber(R.getEnd()); if (EndLineNo < LineNo) return; // No intersection. // Compute the column number of the start. unsigned StartColNo = 0; if (StartLineNo == LineNo) { - StartColNo = SourceMgr.getLogicalColumnNumber(R.Begin()); + StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin()); if (StartColNo) --StartColNo; // Zero base the col #. } @@ -75,12 +75,12 @@ void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, // Compute the column number of the end. unsigned EndColNo = CaratLine.size(); if (EndLineNo == LineNo) { - EndColNo = SourceMgr.getLogicalColumnNumber(R.End()); + EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd()); if (EndColNo) { --EndColNo; // Zero base the col #. // Add in the length of the token, so that we cover multi-char tokens. - EndColNo += GetTokenLength(R.End()); + EndColNo += GetTokenLength(R.getEnd()); } else { EndColNo = CaratLine.size(); } diff --git a/Rewrite/Rewriter.cpp b/Rewrite/Rewriter.cpp index 3c618de585..0c50b4b4e4 100644 --- a/Rewrite/Rewriter.cpp +++ b/Rewrite/Rewriter.cpp @@ -141,6 +141,25 @@ void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength, // Rewriter class //===----------------------------------------------------------------------===// +/// getRangeSize - Return the size in bytes of the specified range if they +/// are in the same file. If not, this returns -1. +int Rewriter::getRangeSize(SourceRange Range) const { + if (!isRewritable(Range.getBegin()) || + !isRewritable(Range.getEnd())) return -1; + + unsigned StartOff, StartFileID; + unsigned EndOff , EndFileID; + + StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID); + EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID); + + if (StartFileID != EndFileID) + return -1; + + return EndOff-StartOff; +} + + unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc, unsigned &FileID) const { std::pair V = SourceMgr->getDecomposedFileLoc(Loc); diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 76d1628bfc..9b2800f047 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -490,7 +490,7 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { // All of these full declarators require an identifier. If it doesn't have // one, the ParsedFreeStandingDeclSpec action should be used. if (II == 0) { - Diag(D.getDeclSpec().getSourceRange().Begin(), + Diag(D.getDeclSpec().getSourceRange().getBegin(), diag::err_declarator_need_ident, D.getDeclSpec().getSourceRange(), D.getSourceRange()); return 0; diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj index 75d938806b..b88c381faa 100644 --- a/clang.xcodeproj/project.pbxproj +++ b/clang.xcodeproj/project.pbxproj @@ -742,7 +742,6 @@ 08FB7793FE84155DC02AAC07 /* Project object */ = { isa = PBXProject; buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */; - compatibilityVersion = "Xcode 2.4"; hasScannedForEncodings = 1; mainGroup = 08FB7794FE84155DC02AAC07 /* clang */; projectDirPath = ""; diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 9a9e7c6057..10273529cd 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -703,7 +703,7 @@ public: Expr *getSubExpr() const { return Op; } virtual SourceRange getSourceRange() const { - return SourceRange(Loc, getSubExpr()->getSourceRange().End()); + return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); } static bool classof(const Stmt *T) { return T->getStmtClass() == CastExprClass; diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h index 14b1cc7d3a..fadfa1c148 100644 --- a/include/clang/AST/ExprCXX.h +++ b/include/clang/AST/ExprCXX.h @@ -60,7 +60,7 @@ namespace clang { } virtual SourceRange getSourceRange() const { - return SourceRange(Loc, getSubExpr()->getSourceRange().End()); + return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); } static bool classof(const Stmt *T) { return T->getStmtClass() == CXXCastExprClass; diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 2f3544e6e4..0aadb079a4 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -55,8 +55,8 @@ public: /// value objects created/interpreted by SourceManager. We assume AST /// clients will have a pointer to the respective SourceManager. virtual SourceRange getSourceRange() const = 0; - SourceLocation getLocStart() const { return getSourceRange().Begin(); } - SourceLocation getLocEnd() const { return getSourceRange().End(); } + SourceLocation getLocStart() const { return getSourceRange().getBegin(); } + SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } // global temp stats (until we have a per-module visitor) static void addStmtClass(const StmtClass s); diff --git a/include/clang/Basic/SourceLocation.h b/include/clang/Basic/SourceLocation.h index 1ebb0ff3f8..f5cad5cea4 100644 --- a/include/clang/Basic/SourceLocation.h +++ b/include/clang/Basic/SourceLocation.h @@ -166,8 +166,8 @@ public: SourceRange(SourceLocation loc) : B(loc), E(loc) {} SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} - SourceLocation Begin() const { return B; } - SourceLocation End() const { return E; } + SourceLocation getBegin() const { return B; } + SourceLocation getEnd() const { return E; } void setBegin(SourceLocation b) { B = b; } void setEnd(SourceLocation e) { E = e; } diff --git a/include/clang/Rewrite/Rewriter.h b/include/clang/Rewrite/Rewriter.h index 6e8c2084c6..bf4baf25af 100644 --- a/include/clang/Rewrite/Rewriter.h +++ b/include/clang/Rewrite/Rewriter.h @@ -121,6 +121,10 @@ public: static bool isRewritable(SourceLocation Loc) { return Loc.isFileID(); } + + /// getRangeSize - Return the size in bytes of the specified range if they + /// are in the same file. If not, this returns -1. + 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 @@ -128,8 +132,8 @@ public: void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen); /// RemoveText - Remove the specified text region. This method is only valid - /// on rewritable source locations. - void RemoveText(SourceLocation Start, SourceLocation End); + /// on a rewritable source location. + void RemoveText(SourceLocation Start, unsigned Length); /// ReplaceText - This method replaces a range of characters in the input /// buffer with a new string. This is effectively a combined "remove/insert" -- 2.40.0