From 67bb651ad165f56cdc7ceacd10e860b291572a58 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 19 Oct 2016 14:13:21 +0000 Subject: [PATCH] [clang-move] Move using-decl in old cc. Summary: Another fix is to move the whole anonymous namespace declaration completely instead of moving fun/var declarations only. Reviewers: ioeric Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D25762 llvm-svn: 284592 --- clang-tools-extra/clang-move/ClangMove.cpp | 41 ++++++++++++++-------- .../test/clang-move/Inputs/multiple_class_test.cpp | 12 +++++++ .../test/clang-move/move-multiple-classes.cpp | 25 ++++++++++++- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/clang-tools-extra/clang-move/ClangMove.cpp b/clang-tools-extra/clang-move/ClangMove.cpp index 268d31d..62356d2 100644 --- a/clang-tools-extra/clang-move/ClangMove.cpp +++ b/clang-tools-extra/clang-move/ClangMove.cpp @@ -351,21 +351,31 @@ void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) { .bind("class_static_var_decl"), this); - auto inAnonymousNamespace = hasParent(namespaceDecl(isAnonymous())); - // Match functions/variables definitions which are defined in anonymous - // namespace in old cc. + auto InOldCCNamedNamespace = + allOf(hasParent(namespaceDecl(unless(isAnonymous()))), InOldCC); + // Matching using decls/type alias decls which are in named namespace. Those + // in classes, functions and anonymous namespaces are covered in other + // matchers. Finder->addMatcher( - namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())), - inAnonymousNamespace) - .bind("decls_in_anonymous_ns"), + namedDecl( + anyOf(usingDecl(InOldCCNamedNamespace), + usingDirectiveDecl(InOldCC, InOldCCNamedNamespace), + typeAliasDecl(InOldCC, InOldCCNamedNamespace))) + .bind("using_decl"), this); - // Match static functions/variabale definitions in old cc. + // Match anonymous namespace decl in old cc. + Finder->addMatcher(namespaceDecl(isAnonymous(), InOldCC).bind("anonymous_ns"), + this); + + // Match static functions/variable definitions which are defined in named + // namespaces. + auto IsOldCCStaticDefinition = + allOf(isDefinition(), unless(InMovedClass), InOldCCNamedNamespace, + isStaticStorageClass()); Finder->addMatcher( - namedDecl(anyOf(functionDecl(isDefinition(), unless(InMovedClass), - isStaticStorageClass(), InOldCC), - varDecl(isDefinition(), unless(InMovedClass), - isStaticStorageClass(), InOldCC))) + namedDecl(anyOf(functionDecl(IsOldCCStaticDefinition), + varDecl(IsOldCCStaticDefinition))) .bind("static_decls"), this); @@ -398,12 +408,15 @@ void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) { // Skip all forwad declarations which appear after moved class declaration. if (RemovedDecls.empty()) MovedDecls.emplace_back(FWD, &Result.Context->getSourceManager()); - } else if (const auto *FD = Result.Nodes.getNodeAs( - "decls_in_anonymous_ns")) { - MovedDecls.emplace_back(FD, &Result.Context->getSourceManager()); + } else if (const auto *ANS = Result.Nodes.getNodeAs( + "anonymous_ns")) { + MovedDecls.emplace_back(ANS, &Result.Context->getSourceManager()); } else if (const auto *ND = Result.Nodes.getNodeAs("static_decls")) { MovedDecls.emplace_back(ND, &Result.Context->getSourceManager()); + } else if (const auto *UD = + Result.Nodes.getNodeAs("using_decl")) { + MovedDecls.emplace_back(UD, &Result.Context->getSourceManager()); } } diff --git a/clang-tools-extra/test/clang-move/Inputs/multiple_class_test.cpp b/clang-tools-extra/test/clang-move/Inputs/multiple_class_test.cpp index 3e62ea83..691cffe 100644 --- a/clang-tools-extra/test/clang-move/Inputs/multiple_class_test.cpp +++ b/clang-tools-extra/test/clang-move/Inputs/multiple_class_test.cpp @@ -6,7 +6,16 @@ int Move1::f() { } } // namespace a +namespace { +using a::Move1; +using namespace a; +static int k = 0; +} // anonymous namespace + namespace b { +using a::Move1; +using namespace a; +using T = a::Move1; int Move2::f() { return 0; } @@ -14,6 +23,8 @@ int Move2::f() { namespace c { int Move3::f() { + using a::Move1; + using namespace b; return 0; } @@ -30,6 +41,7 @@ int EnclosingMove5::Nested::f() { int EnclosingMove5::Nested::b = 1; int NoMove::f() { + static int F = 0; return 0; } } // namespace c diff --git a/clang-tools-extra/test/clang-move/move-multiple-classes.cpp b/clang-tools-extra/test/clang-move/move-multiple-classes.cpp index f33ac75..438056a 100644 --- a/clang-tools-extra/test/clang-move/move-multiple-classes.cpp +++ b/clang-tools-extra/test/clang-move/move-multiple-classes.cpp @@ -18,8 +18,19 @@ // CHECK-OLD-TEST-H: } // namespace c // CHECK-OLD-TEST-CPP: #include "{{.*}}multiple_class_test.h" +// CHECK-OLD-TEST-CPP: namespace { +// CHECK-OLD-TEST-CPP: using a::Move1; +// CHECK-OLD-TEST-CPP: using namespace a; +// CHECK-OLD-TEST-CPP: static int k = 0; +// CHECK-OLD-TEST-CPP: } // anonymous namespace +// CHECK-OLD-TEST-CPP: namespace b { +// CHECK-OLD-TEST-CPP: using a::Move1; +// CHECK-OLD-TEST-CPP: using namespace a; +// CHECK-OLD-TEST-CPP: using T = a::Move1; +// CHECK-OLD-TEST-CPP: } // namespace b // CHECK-OLD-TEST-CPP: namespace c { // CHECK-OLD-TEST-CPP: int NoMove::f() { +// CHECK-OLD-TEST-CPP: static int F = 0; // CHECK-OLD-TEST-CPP: return 0; // CHECK-OLD-TEST-CPP: } // CHECK-OLD-TEST-CPP: } // namespace c @@ -62,11 +73,23 @@ // CHECK-NEW-TEST-CPP: namespace a { // CHECK-NEW-TEST-CPP: int Move1::f() { return 0; } // CHECK-NEW-TEST-CPP: } // namespace a +// CHECK-NEW-TEST-CPP: namespace { +// CHECK-NEW-TEST-CPP: using a::Move1; +// CHECK-NEW-TEST-CPP: using namespace a; +// CHECK-NEW-TEST-CPP: static int k = 0; +// CHECK-NEW-TEST-CPP: } // anonymous namespace // CHECK-NEW-TEST-CPP: namespace b { +// CHECK-NEW-TEST-CPP: using a::Move1; +// CHECK-NEW-TEST-CPP: using namespace a; +// CHECK-NEW-TEST-CPP: using T = a::Move1; // CHECK-NEW-TEST-CPP: int Move2::f() { return 0; } // CHECK-NEW-TEST-CPP: } // namespace b // CHECK-NEW-TEST-CPP: namespace c { -// CHECK-NEW-TEST-CPP: int Move3::f() { return 0; } +// CHECK-NEW-TEST-CPP: int Move3::f() { +// CHECK-NEW-TEST-CPP: using a::Move1; +// CHECK-NEW-TEST-CPP: using namespace b; +// CHECK-NEW-TEST-CPP: return 0; +// CHECK-NEW-TEST-CPP: } // CHECK-NEW-TEST-CPP: int Move4::f() { return 0; } // CHECK-NEW-TEST-CPP: int EnclosingMove5::a = 1; // CHECK-NEW-TEST-CPP: int EnclosingMove5::Nested::f() { return 0; } -- 2.7.4