From: Eric Liu Date: Wed, 7 Dec 2016 14:20:52 +0000 (+0000) Subject: [change-namespace] move template class forward-declarations and don't move fwd-decls... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=41552d6a37c8c9fb4a620b3a7164469a2cb914ac;p=platform%2Fupstream%2Fllvm.git [change-namespace] move template class forward-declarations and don't move fwd-decls in classes. Summary: Forward declarations in moved namespaces should be moved back to the old namespaces. We should also move template class forward declarations. Also fix a bug that moves forward declarations of nested classes. Reviewers: bkramer Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D27515 llvm-svn: 288908 --- diff --git a/clang-tools-extra/change-namespace/ChangeNamespace.cpp b/clang-tools-extra/change-namespace/ChangeNamespace.cpp index c0a8fe0..a18ad9b 100644 --- a/clang-tools-extra/change-namespace/ChangeNamespace.cpp +++ b/clang-tools-extra/change-namespace/ChangeNamespace.cpp @@ -303,10 +303,18 @@ void ChangeNamespaceTool::registerMatchers(ast_matchers::MatchFinder *Finder) { .bind("old_ns"), this); - // Match forward-declarations in the old namespace. + // Match class forward-declarations in the old namespace. + // Note that forward-declarations in classes are not matched. + Finder->addMatcher(cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition())), + IsInMovedNs, hasParent(namespaceDecl())) + .bind("class_fwd_decl"), + this); + + // Match template class forward-declarations in the old namespace. Finder->addMatcher( - cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition())), IsInMovedNs) - .bind("fwd_decl"), + classTemplateDecl(unless(hasDescendant(cxxRecordDecl(isDefinition()))), + IsInMovedNs, hasParent(namespaceDecl())) + .bind("template_class_fwd_decl"), this); // Match references to types that are not defined in the old namespace. @@ -401,8 +409,12 @@ void ChangeNamespaceTool::run( Result.Nodes.getNodeAs("old_ns")) { moveOldNamespace(Result, NsDecl); } else if (const auto *FwdDecl = - Result.Nodes.getNodeAs("fwd_decl")) { - moveClassForwardDeclaration(Result, FwdDecl); + Result.Nodes.getNodeAs("class_fwd_decl")) { + moveClassForwardDeclaration(Result, cast(FwdDecl)); + } else if (const auto *TemplateFwdDecl = + Result.Nodes.getNodeAs( + "template_class_fwd_decl")) { + moveClassForwardDeclaration(Result, cast(TemplateFwdDecl)); } else if (const auto *UsingWithShadow = Result.Nodes.getNodeAs("using_with_shadow")) { fixUsingShadowDecl(Result, UsingWithShadow); @@ -539,7 +551,7 @@ void ChangeNamespaceTool::moveOldNamespace( // } // x void ChangeNamespaceTool::moveClassForwardDeclaration( const ast_matchers::MatchFinder::MatchResult &Result, - const CXXRecordDecl *FwdDecl) { + const NamedDecl *FwdDecl) { SourceLocation Start = FwdDecl->getLocStart(); SourceLocation End = FwdDecl->getLocEnd(); SourceLocation AfterSemi = Lexer::findLocationAfterToken( diff --git a/clang-tools-extra/change-namespace/ChangeNamespace.h b/clang-tools-extra/change-namespace/ChangeNamespace.h index 300579d..048c32f 100644 --- a/clang-tools-extra/change-namespace/ChangeNamespace.h +++ b/clang-tools-extra/change-namespace/ChangeNamespace.h @@ -64,7 +64,7 @@ private: void moveClassForwardDeclaration( const ast_matchers::MatchFinder::MatchResult &Result, - const CXXRecordDecl *FwdDecl); + const NamedDecl *FwdDecl); void replaceQualifiedSymbolInDeclContext( const ast_matchers::MatchFinder::MatchResult &Result, diff --git a/clang-tools-extra/unittests/change-namespace/ChangeNamespaceTests.cpp b/clang-tools-extra/unittests/change-namespace/ChangeNamespaceTests.cpp index 53684be..450e4a4 100644 --- a/clang-tools-extra/unittests/change-namespace/ChangeNamespaceTests.cpp +++ b/clang-tools-extra/unittests/change-namespace/ChangeNamespaceTests.cpp @@ -278,6 +278,7 @@ TEST_F(ChangeNamespaceTest, LeaveForwardDeclarationBehind) { std::string Code = "namespace na {\n" "namespace nb {\n" "class FWD;\n" + "class FWD2;\n" "class A {\n" " FWD *fwd;\n" "};\n" @@ -286,6 +287,7 @@ TEST_F(ChangeNamespaceTest, LeaveForwardDeclarationBehind) { std::string Expected = "namespace na {\n" "namespace nb {\n" "class FWD;\n" + "class FWD2;\n" "} // namespace nb\n" "} // namespace na\n" "namespace x {\n" @@ -299,6 +301,58 @@ TEST_F(ChangeNamespaceTest, LeaveForwardDeclarationBehind) { EXPECT_EQ(format(Expected), runChangeNamespaceOnCode(Code)); } +TEST_F(ChangeNamespaceTest, TemplateClassForwardDeclaration) { + std::string Code = "namespace na {\n" + "namespace nb {\n" + "class FWD;\n" + "template class FWD_TEMP;\n" + "class A {\n" + " FWD *fwd;\n" + "};\n" + "template class TEMP {};\n" + "} // namespace nb\n" + "} // namespace na\n"; + std::string Expected = "namespace na {\n" + "namespace nb {\n" + "class FWD;\n" + "template class FWD_TEMP;\n" + "} // namespace nb\n" + "} // namespace na\n" + "namespace x {\n" + "namespace y {\n" + "\n" + "class A {\n" + " na::nb::FWD *fwd;\n" + "};\n" + "template class TEMP {};\n" + "} // namespace y\n" + "} // namespace x\n"; + EXPECT_EQ(format(Expected), runChangeNamespaceOnCode(Code)); +} + +TEST_F(ChangeNamespaceTest, DontMoveForwardDeclarationInClass) { + std::string Code = "namespace na {\n" + "namespace nb {\n" + "class A {\n" + " class FWD;\n" + " FWD *fwd;\n" + " template class FWD_TEMP;\n" + "};\n" + "} // namespace nb\n" + "} // namespace na\n"; + std::string Expected = "\n\n" + "namespace x {\n" + "namespace y {\n" + "class A {\n" + " class FWD;\n" + " FWD *fwd;\n" + " template class FWD_TEMP;\n" + "};\n" + "} // namespace y\n" + "} // namespace x\n"; + EXPECT_EQ(format(Expected), runChangeNamespaceOnCode(Code)); +} + TEST_F(ChangeNamespaceTest, MoveFunctions) { std::string Code = "namespace na {\n" "class C_A {};\n"