[PATCH] D22906: Refactor NamedDeclFindingASTVisitor

Alexander Shaposhnikov via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 28 01:55:59 PDT 2016


alexshap created this revision.
alexshap added reviewers: omtcyfz, klimek, compnerd.
alexshap added a subscriber: cfe-commits.
alexshap changed the visibility of this Differential Revision from "Public (No Login Required)" to "All Users".

Address the comments on the diff D22881.
Remove SourceMgr.
Check Decl inside setResult.
All the tests are green.



https://reviews.llvm.org/D22906

Files:
  clang-rename/USRFinder.cpp

Index: clang-rename/USRFinder.cpp
===================================================================
--- clang-rename/USRFinder.cpp
+++ clang-rename/USRFinder.cpp
@@ -34,17 +34,15 @@
 public:
   // \brief Finds the NamedDecl at a point in the source.
   // \param Point the location in the source to search for the NamedDecl.
-  explicit NamedDeclFindingASTVisitor(const SourceManager &SourceMgr,
-                                      const SourceLocation Point,
+  explicit NamedDeclFindingASTVisitor(const SourceLocation Point,
                                       const ASTContext *Context)
-      : Result(nullptr), SourceMgr(SourceMgr), Point(Point), Context(Context) {}
+      : Result(nullptr), Point(Point), Context(Context) {}
 
   // \brief Finds the NamedDecl for a name in the source.
   // \param Name the fully qualified name.
-  explicit NamedDeclFindingASTVisitor(const SourceManager &SourceMgr,
-                                      const std::string &Name,
+  explicit NamedDeclFindingASTVisitor(const std::string &Name,
                                       const ASTContext *Context)
-      : Result(nullptr), SourceMgr(SourceMgr), Name(Name), Context(Context) {}
+      : Result(nullptr), Name(Name), Context(Context) {}
 
   // Declaration visitors:
 
@@ -74,12 +72,11 @@
   // Other visitors:
 
   bool VisitTypeLoc(const TypeLoc Loc) {
+    const SourceManager &SourceMgr = Context->getSourceManager();
     const auto TypeBeginLoc = Loc.getBeginLoc();
     const auto TypeEndLoc = Lexer::getLocForEndOfToken(
         TypeBeginLoc, 0, SourceMgr, Context->getLangOpts());
-    if (auto *RD = Loc.getType()->getAsCXXRecordDecl())
-      return setResult(RD, TypeBeginLoc, TypeEndLoc);
-    return true;
+    return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc, TypeEndLoc);
   }
 
   // Other:
@@ -91,9 +88,7 @@
   void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
     while (NameLoc) {
       const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace();
-      if (Decl) {
-        setResult(Decl, NameLoc.getLocalBeginLoc(), NameLoc.getLocalEndLoc());
-      }
+      setResult(Decl, NameLoc.getLocalBeginLoc(), NameLoc.getLocalEndLoc());
       NameLoc = NameLoc.getPrefix();
     }
   }
@@ -103,6 +98,8 @@
   // \returns false on success.
   bool setResult(const NamedDecl *Decl, SourceLocation Start,
                  SourceLocation End) {
+    if (!Decl)
+      return true;
     if (Name.empty()) {
       // Offset is used to find the declaration.
       if (!Start.isValid() || !Start.isFileID() || !End.isValid() ||
@@ -130,13 +127,13 @@
   // \brief Determines if the Point is within Start and End.
   bool isPointWithin(const SourceLocation Start, const SourceLocation End) {
     // FIXME: Add tests for Point == End.
+    const SourceManager &SourceMgr = Context->getSourceManager();
     return Point == Start || Point == End ||
            (SourceMgr.isBeforeInTranslationUnit(Start, Point) &&
             SourceMgr.isBeforeInTranslationUnit(Point, End));
   }
 
   const NamedDecl *Result;
-  const SourceManager &SourceMgr;
   const SourceLocation Point; // The location to find the NamedDecl.
   const std::string Name;
   const ASTContext *Context;
@@ -148,7 +145,7 @@
   const auto &SourceMgr = Context.getSourceManager();
   const auto SearchFile = SourceMgr.getFilename(Point);
 
-  NamedDeclFindingASTVisitor Visitor(SourceMgr, Point, &Context);
+  NamedDeclFindingASTVisitor Visitor(Point, &Context);
 
   // We only want to search the decls that exist in the same file as the point.
   auto Decls = Context.getTranslationUnitDecl()->decls();
@@ -171,8 +168,7 @@
 
 const NamedDecl *getNamedDeclFor(const ASTContext &Context,
                                  const std::string &Name) {
-  const auto &SourceMgr = Context.getSourceManager();
-  NamedDeclFindingASTVisitor Visitor(SourceMgr, Name, &Context);
+  NamedDeclFindingASTVisitor Visitor(Name, &Context);
   Visitor.TraverseDecl(Context.getTranslationUnitDecl());
 
   return Visitor.getNamedDecl();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22906.65887.patch
Type: text/x-patch
Size: 4058 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160728/57915a48/attachment-0001.bin>


More information about the cfe-commits mailing list