hokein updated this revision to Diff 77521.
hokein marked 3 inline comments as done.
hokein added a comment.

Address comments.


https://reviews.llvm.org/D26515

Files:
  clang-move/ClangMove.cpp
  clang-move/ClangMove.h

Index: clang-move/ClangMove.h
===================================================================
--- clang-move/ClangMove.h
+++ clang-move/ClangMove.h
@@ -16,6 +16,7 @@
 #include "clang/Tooling/Tooling.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include <map>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -80,6 +81,14 @@
                    clang::CharSourceRange IncludeFilenameRange,
                    const SourceManager &SM);
 
+  std::vector<MovedDecl> &getMovedDecls() { return MovedDecls; }
+
+  std::vector<MovedDecl> &getRemovedDecls() { return RemovedDecls; }
+
+  llvm::SmallPtrSet<const NamedDecl *, 8> &getUnremovedDeclsInOldHeader() {
+    return UnremovedDeclsInOldHeader;
+  }
+
 private:
   // Make the Path absolute using the OrignalRunningDirectory if the Path is not
   // an absolute path. An empty Path will result in an empty string.
@@ -90,6 +99,9 @@
   void moveAll(SourceManager& SM, StringRef OldFile, StringRef NewFile);
 
   MoveDefinitionSpec Spec;
+  // Stores all MatchCallbacks created by this tool.
+  std::vector<std::unique_ptr<ast_matchers::MatchFinder::MatchCallback>>
+      MatchCallbacks;
   // The Key is file path, value is the replacements being applied to the file.
   std::map<std::string, tooling::Replacements> &FileToReplacements;
   // All declarations (the class decl being moved, forward decls) that need to
Index: clang-move/ClangMove.cpp
===================================================================
--- clang-move/ClangMove.cpp
+++ clang-move/ClangMove.cpp
@@ -136,6 +136,54 @@
   ClangMoveTool *const MoveTool;
 };
 
+class ClassDeclarationMatcher : public MatchFinder::MatchCallback {
+public:
+  explicit ClassDeclarationMatcher(ClangMoveTool *MoveTool)
+      : MoveTool(MoveTool) {}
+  void run(const ast_matchers::MatchFinder::MatchResult &Result) override {
+    if (const auto *CMD =
+            Result.Nodes.getNodeAs<clang::CXXMethodDecl>("class_method")) {
+      // Skip inline class methods. isInline() ast matcher doesn't ignore this
+      // case.
+      if (!CMD->isInlined()) {
+        MoveTool->getMovedDecls().emplace_back(
+            CMD, &Result.Context->getSourceManager());
+        MoveTool->getRemovedDecls().push_back(MoveTool->getMovedDecls().back());
+        // Get template class method from its method declaration as
+        // UnremovedDecls stores template class method.
+        if (const auto *FTD = CMD->getDescribedFunctionTemplate()) {
+          MoveTool->getUnremovedDeclsInOldHeader().erase(FTD);
+        } else {
+          MoveTool->getUnremovedDeclsInOldHeader().erase(CMD);
+        }
+      }
+    } else if (const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>(
+                   "class_static_var_decl")) {
+      MoveTool->getMovedDecls().emplace_back(
+          VD, &Result.Context->getSourceManager());
+      MoveTool->getRemovedDecls().push_back(MoveTool->getMovedDecls().back());
+      MoveTool->getUnremovedDeclsInOldHeader().erase(VD);
+    } else if (const auto *CD = Result.Nodes.getNodeAs<clang::CXXRecordDecl>(
+                   "moved_class")) {
+      // Get class template from its class declaration as UnremovedDecls stores
+      // class template.
+      if (const auto *TC = CD->getDescribedClassTemplate()) {
+        MoveTool->getMovedDecls().emplace_back(
+            TC, &Result.Context->getSourceManager());
+      } else {
+        MoveTool->getMovedDecls().emplace_back(
+            CD, &Result.Context->getSourceManager());
+      }
+      MoveTool->getRemovedDecls().push_back(MoveTool->getMovedDecls().back());
+      MoveTool->getUnremovedDeclsInOldHeader().erase(
+          MoveTool->getMovedDecls().back().Decl);
+    }
+  }
+
+private:
+  ClangMoveTool *MoveTool;
+};
+
 // Expand to get the end location of the line where the EndLoc of the given
 // Decl.
 SourceLocation
@@ -352,23 +400,27 @@
   //============================================================================
   // Matchers for old files, including old.h/old.cc
   //============================================================================
+  // Create a MatchCallback for class declarations.
+  MatchCallbacks.push_back(llvm::make_unique<ClassDeclarationMatcher>(this));
   // Match moved class declarations.
-  auto MovedClass = cxxRecordDecl(
-      InOldFiles, *InMovedClassNames, isDefinition(),
-      hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())));
-  Finder->addMatcher(MovedClass.bind("moved_class"), this);
+  auto MovedClass =
+      cxxRecordDecl(
+          InOldFiles, *InMovedClassNames, isDefinition(),
+          hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())))
+          .bind("moved_class");
+  Finder->addMatcher(MovedClass, MatchCallbacks.back().get());
   // Match moved class methods (static methods included) which are defined
   // outside moved class declaration.
   Finder->addMatcher(
       cxxMethodDecl(InOldFiles, ofOutermostEnclosingClass(*InMovedClassNames),
                     isDefinition())
           .bind("class_method"),
-      this);
+      MatchCallbacks.back().get());
   // Match static member variable definition of the moved class.
   Finder->addMatcher(
       varDecl(InMovedClass, InOldFiles, isDefinition(), isStaticDataMember())
           .bind("class_static_var_decl"),
-      this);
+      MatchCallbacks.back().get());
 
   //============================================================================
   // Matchers for old cc
@@ -404,35 +456,6 @@
   if (const auto *D =
           Result.Nodes.getNodeAs<clang::NamedDecl>("decls_in_header")) {
     UnremovedDeclsInOldHeader.insert(D);
-  } else if (const auto *CMD =
-          Result.Nodes.getNodeAs<clang::CXXMethodDecl>("class_method")) {
-    // Skip inline class methods. isInline() ast matcher doesn't ignore this
-    // case.
-    if (!CMD->isInlined()) {
-      MovedDecls.emplace_back(CMD, &Result.Context->getSourceManager());
-      RemovedDecls.push_back(MovedDecls.back());
-      // Get template class method from its method declaration as
-      // UnremovedDecls stores template class method.
-      if (const auto *FTD = CMD->getDescribedFunctionTemplate())
-        UnremovedDeclsInOldHeader.erase(FTD);
-      else
-        UnremovedDeclsInOldHeader.erase(CMD);
-    }
-  } else if (const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>(
-                 "class_static_var_decl")) {
-    MovedDecls.emplace_back(VD, &Result.Context->getSourceManager());
-    RemovedDecls.push_back(MovedDecls.back());
-    UnremovedDeclsInOldHeader.erase(MovedDecls.back().Decl);
-  } else if (const auto *CD =
-                 Result.Nodes.getNodeAs<clang::CXXRecordDecl>("moved_class")) {
-    // Get class template from its class declaration as UnremovedDecls stores
-    // class template.
-    if (const auto * TC = CD->getDescribedClassTemplate())
-      MovedDecls.emplace_back(TC, &Result.Context->getSourceManager());
-    else
-      MovedDecls.emplace_back(CD, &Result.Context->getSourceManager());
-    RemovedDecls.push_back(MovedDecls.back());
-    UnremovedDeclsInOldHeader.erase(MovedDecls.back().Decl);
   } else if (const auto *FWD =
                  Result.Nodes.getNodeAs<clang::CXXRecordDecl>("fwd_decl")) {
     // Skip all forwad declarations which appear after moved class declaration.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to