diff options
author | Haojian Wu <hokein@google.com> | 2016-10-13 10:31:00 +0000 |
---|---|---|
committer | Haojian Wu <hokein@google.com> | 2016-10-13 10:31:00 +0000 |
commit | e77bcc7371c1d3054a99ee45eeac45ca335dd2d9 (patch) | |
tree | 8ae48d39e6d4d32ab0ba31796fb0debd25c30b79 /clang-tools-extra/clang-move/ClangMove.cpp | |
parent | 2f5ed342797d78e1544338de09186c54d027144e (diff) | |
download | bcm5719-llvm-e77bcc7371c1d3054a99ee45eeac45ca335dd2d9.tar.gz bcm5719-llvm-e77bcc7371c1d3054a99ee45eeac45ca335dd2d9.zip |
[clang-move] Better support enclosing class.
Summary:
* When moving an outermost enclosing class, all its nested classes should also
be moved together.
* Add a test for not moving nested class.
Reviewers: ioeric
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D25369
llvm-svn: 284111
Diffstat (limited to 'clang-tools-extra/clang-move/ClangMove.cpp')
-rw-r--r-- | clang-tools-extra/clang-move/ClangMove.cpp | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/clang-tools-extra/clang-move/ClangMove.cpp b/clang-tools-extra/clang-move/ClangMove.cpp index c429a185cea..a741f559ece 100644 --- a/clang-tools-extra/clang-move/ClangMove.cpp +++ b/clang-tools-extra/clang-move/ClangMove.cpp @@ -24,6 +24,32 @@ namespace clang { namespace move { namespace { +AST_MATCHER_P(Decl, hasOutermostEnclosingClass, + ast_matchers::internal::Matcher<Decl>, InnerMatcher) { + const auto* Context = Node.getDeclContext(); + if (!Context) return false; + while (const auto *NextContext = Context->getParent()) { + if (isa<NamespaceDecl>(NextContext) || + isa<TranslationUnitDecl>(NextContext)) + break; + Context = NextContext; + } + return InnerMatcher.matches(*Decl::castFromDeclContext(Context), Finder, + Builder); +} + +AST_MATCHER_P(CXXMethodDecl, ofOutermostEnclosingClass, + ast_matchers::internal::Matcher<CXXRecordDecl>, InnerMatcher) { + const CXXRecordDecl *Parent = Node.getParent(); + if (!Parent) return false; + while (const auto *NextParent = + dyn_cast<CXXRecordDecl>(Parent->getParent())) { + Parent = NextParent; + } + + return InnerMatcher.matches(*Parent, Finder, Builder); +} + // Make the Path absolute using the CurrentDir if the Path is not an absolute // path. An empty Path will result in an empty string. std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) { @@ -322,7 +348,7 @@ void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) { MakeAbsolutePath(OriginalRunningDirectory, Spec.OldCC)); auto InOldFiles = anyOf(InOldHeader, InOldCC); auto InMovedClass = - hasDeclContext(cxxRecordDecl(*InMovedClassNames)); + hasOutermostEnclosingClass(cxxRecordDecl(*InMovedClassNames)); // Match moved class declarations. auto MovedClass = cxxRecordDecl( @@ -332,11 +358,11 @@ void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) { // Match moved class methods (static methods included) which are defined // outside moved class declaration. - Finder->addMatcher(cxxMethodDecl(InOldFiles, - ofClass(*InMovedClassNames), - isDefinition()) - .bind("class_method"), - this); + Finder->addMatcher( + cxxMethodDecl(InOldFiles, ofOutermostEnclosingClass(*InMovedClassNames), + isDefinition()) + .bind("class_method"), + this); // Match static member variable definition of the moved class. Finder->addMatcher(varDecl(InMovedClass, InOldCC, isDefinition()) |