diff options
Diffstat (limited to 'clang-tools-extra/clang-move')
-rw-r--r-- | clang-tools-extra/clang-move/ClangMove.cpp | 32 | ||||
-rw-r--r-- | clang-tools-extra/clang-move/HelperDeclRefGraph.cpp | 15 |
2 files changed, 34 insertions, 13 deletions
diff --git a/clang-tools-extra/clang-move/ClangMove.cpp b/clang-tools-extra/clang-move/ClangMove.cpp index e1f20b70009..f43f278f07a 100644 --- a/clang-tools-extra/clang-move/ClangMove.cpp +++ b/clang-tools-extra/clang-move/ClangMove.cpp @@ -553,16 +553,22 @@ void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) { // Matchers for helper declarations in old.cc. auto InAnonymousNS = hasParent(namespaceDecl(isAnonymous())); - auto DefinitionInOldCC = allOf(isDefinition(), unless(InMovedClass), InOldCC); - auto IsOldCCHelperDefinition = - allOf(DefinitionInOldCC, anyOf(isStaticStorageClass(), InAnonymousNS)); + auto NotInMovedClass= allOf(unless(InMovedClass), InOldCC); + auto IsOldCCHelper = + allOf(NotInMovedClass, anyOf(isStaticStorageClass(), InAnonymousNS)); // Match helper classes separately with helper functions/variables since we // want to reuse these matchers in finding helpers usage below. - auto HelperFuncOrVar = - namedDecl(notInMacro(), anyOf(functionDecl(IsOldCCHelperDefinition), - varDecl(IsOldCCHelperDefinition))); + // + // There could be forward declarations usage for helpers, especially for + // classes and functions. We need include these forward declarations. + // + // Forward declarations for variable helpers will be excluded as these + // declarations (with "extern") are not supposed in cpp file. + auto HelperFuncOrVar = + namedDecl(notInMacro(), anyOf(functionDecl(IsOldCCHelper), + varDecl(isDefinition(), IsOldCCHelper))); auto HelperClasses = - cxxRecordDecl(notInMacro(), DefinitionInOldCC, InAnonymousNS); + cxxRecordDecl(notInMacro(), NotInMovedClass, InAnonymousNS); // Save all helper declarations in old.cc. Finder->addMatcher( namedDecl(anyOf(HelperFuncOrVar, HelperClasses)).bind("helper_decls"), @@ -650,6 +656,8 @@ void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) { Result.Nodes.getNodeAs<clang::NamedDecl>("helper_decls")) { MovedDecls.push_back(ND); HelperDeclarations.push_back(ND); + DEBUG(llvm::dbgs() << "Add helper : " + << ND->getNameAsString() << " (" << ND << ")\n"); } else if (const auto *UD = Result.Nodes.getNodeAs<clang::NamedDecl>("using_decl")) { MovedDecls.push_back(UD); @@ -703,9 +711,12 @@ void ClangMoveTool::removeDeclsInOldFiles() { // We remove the helper declarations which are not used in the old.cc after // moving the given declarations. for (const auto *D : HelperDeclarations) { - if (!UsedDecls.count(HelperDeclRGBuilder::getOutmostClassOrFunDecl(D))) { + DEBUG(llvm::dbgs() << "Check helper is used: " + << D->getNameAsString() << " (" << D << ")\n"); + if (!UsedDecls.count(HelperDeclRGBuilder::getOutmostClassOrFunDecl( + D->getCanonicalDecl()))) { DEBUG(llvm::dbgs() << "Helper removed in old.cc: " - << D->getNameAsString() << " " << D << "\n"); + << D->getNameAsString() << " (" << D << ")\n"); RemovedDecls.push_back(D); } } @@ -781,7 +792,8 @@ void ClangMoveTool::moveDeclsToNewFiles() { // given symbols being moved. for (const auto *D : NewCCDecls) { if (llvm::is_contained(HelperDeclarations, D) && - !UsedDecls.count(HelperDeclRGBuilder::getOutmostClassOrFunDecl(D))) + !UsedDecls.count(HelperDeclRGBuilder::getOutmostClassOrFunDecl( + D->getCanonicalDecl()))) continue; DEBUG(llvm::dbgs() << "Helper used in new.cc: " << D->getNameAsString() diff --git a/clang-tools-extra/clang-move/HelperDeclRefGraph.cpp b/clang-tools-extra/clang-move/HelperDeclRefGraph.cpp index 8303015171e..00ab9ea651f 100644 --- a/clang-tools-extra/clang-move/HelperDeclRefGraph.cpp +++ b/clang-tools-extra/clang-move/HelperDeclRefGraph.cpp @@ -10,8 +10,11 @@ #include "HelperDeclRefGraph.h" #include "ClangMove.h" #include "clang/AST/Decl.h" +#include "llvm/Support/Debug.h" #include <vector> +#define DEBUG_TYPE "clang-move" + namespace clang { namespace move { @@ -113,13 +116,19 @@ void HelperDeclRGBuilder::run( if (const auto *FuncRef = Result.Nodes.getNodeAs<DeclRefExpr>("func_ref")) { const auto *DC = Result.Nodes.getNodeAs<Decl>("dc"); assert(DC); - - RG->addEdge(getOutmostClassOrFunDecl(DC->getCanonicalDecl()), - getOutmostClassOrFunDecl(FuncRef->getDecl())); + DEBUG(llvm::dbgs() << "Find helper function usage: " + << FuncRef->getDecl()->getNameAsString() << " (" + << FuncRef->getDecl() << ")\n"); + RG->addEdge( + getOutmostClassOrFunDecl(DC->getCanonicalDecl()), + getOutmostClassOrFunDecl(FuncRef->getDecl()->getCanonicalDecl())); } else if (const auto *UsedClass = Result.Nodes.getNodeAs<CXXRecordDecl>("used_class")) { const auto *DC = Result.Nodes.getNodeAs<Decl>("dc"); assert(DC); + DEBUG(llvm::dbgs() << "Find helper class usage: " + << UsedClass->getNameAsString() << " (" << UsedClass + << ")\n"); RG->addEdge(getOutmostClassOrFunDecl(DC->getCanonicalDecl()), UsedClass); } } |