Index: change-namespace/ChangeNamespace.cpp =================================================================== --- change-namespace/ChangeNamespace.cpp +++ change-namespace/ChangeNamespace.cpp @@ -286,6 +286,15 @@ return Node.isScoped(); } +bool isTemplateParameter(TypeLoc Type) { + while (!Type.isNull()) { + if (Type.getTypeLocClass() == TypeLoc::SubstTemplateTypeParm) + return true; + Type = Type.getNextTypeLoc(); + } + return false; +} + } // anonymous namespace ChangeNamespaceTool::ChangeNamespaceTool( @@ -833,6 +842,8 @@ // Types of CXXCtorInitializers do not need to be fixed. if (llvm::is_contained(BaseCtorInitializerTypeLocs, Type)) return; + if (isTemplateParameter(Type)) + return; // The declaration which this TypeLoc refers to. const auto *FromDecl = Result.Nodes.getNodeAs("from_decl"); // `hasDeclaration` gives underlying declaration, but if the type is Index: unittests/change-namespace/ChangeNamespaceTests.cpp =================================================================== --- unittests/change-namespace/ChangeNamespaceTests.cpp +++ unittests/change-namespace/ChangeNamespaceTests.cpp @@ -2006,6 +2006,52 @@ EXPECT_EQ(format(Expected), runChangeNamespaceOnCode(Code)); } +TEST_F(ChangeNamespaceTest, TypeAsTemplateParameter) { + std::string Code = "namespace na {\n" + "struct X {};\n" + "namespace nb {\n" + "template \n" + "void TempTemp(const TT& t) {\n" + " TT tmp;\n" + "}\n" + "template \n" + "void Temp(const T& t) {\n" + " T tmp = t;\n" + " TempTemp(tmp);\n" + " TempTemp(t);\n" + "}\n" + "void f() {\n" + " X x;\n" + " Temp(x);\n" + "}\n" + "} // namespace nb\n" + "} // namespace na\n"; + std::string Expected = "namespace na {\n" + "struct X {};\n" + "\n" + "} // namespace na\n" + "namespace x {\n" + "namespace y {\n" + "template \n" + "void TempTemp(const TT& t) {\n" + " TT tmp;\n" + "}\n" + "template \n" + "void Temp(const T& t) {\n" + " T tmp = t;\n" + " TempTemp(tmp);\n" + " TempTemp(t);\n" + "}\n" + "void f() {\n" + " ::na::X x;\n" + " Temp(x);\n" + "}\n" + "} // namespace y\n" + "} // namespace x\n"; + + EXPECT_EQ(format(Expected), runChangeNamespaceOnCode(Code)); +} + } // anonymous namespace } // namespace change_namespace } // namespace clang