Index: clang-move/ClangMove.cpp =================================================================== --- clang-move/ClangMove.cpp +++ clang-move/ClangMove.cpp @@ -237,7 +237,7 @@ } GuardName = StringRef(GuardName).upper(); NewCode += "#ifndef " + GuardName + "\n"; - NewCode += "#define " + GuardName + "\n"; + NewCode += "#define " + GuardName + "\n\n"; } // Add #Includes. @@ -268,12 +268,16 @@ for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0; --RemainingSize, ++It) { assert(It < CurrentNamespaces.rend()); - NewCode += "} // namespace " + *It + "\n"; + NewCode += "} // namespace " + *It + "\n\n"; } + bool IsNamespaceStart = false; while (DeclIt != DeclNamespaces.end()) { NewCode += "namespace " + *DeclIt + " {\n"; + IsNamespaceStart = true; ++DeclIt; } + if (!IsNamespaceStart) + NewCode += "\n"; NewCode += getDeclarationSourceText(MovedDecl.Decl, MovedDecl.SM); CurrentNamespaces = std::move(NextNamespaces); } @@ -282,7 +286,7 @@ NewCode += "} // namespace " + NS + "\n"; if (IsHeader) - NewCode += "#endif // " + GuardName + "\n"; + NewCode += "\n#endif // " + GuardName + "\n"; return clang::tooling::Replacements( clang::tooling::Replacement(FileName, 0, 0, NewCode)); } Index: unittests/clang-move/ClangMoveTests.cpp =================================================================== --- unittests/clang-move/ClangMoveTests.cpp +++ unittests/clang-move/ClangMoveTests.cpp @@ -127,8 +127,10 @@ const char ExpectedNewHeader[] = "#ifndef NEW_FOO_H\n" "#define NEW_FOO_H\n" + "\n" "namespace a {\n" "class C1; // test\n" + "\n" "template class C2;\n" "namespace b {\n" "// This is a Foo class\n" @@ -144,6 +146,7 @@ "}; // abc\n" "} // namespace b\n" "} // namespace a\n" + "\n" "#endif // NEW_FOO_H\n"; const char ExpectedNewCC[] = "namespace a {\n" @@ -154,17 +157,21 @@ "/// comment2.\n" "int kConstInt1 = 0;\n" "} // namespace\n" + "\n" "/* comment 3*/\n" "static int kConstInt2 = 1;\n" + "\n" "/** comment4\n" "*/\n" "static int help() {\n" " int a = 0;\n" " return a;\n" "}\n" + "\n" "// comment5\n" "// comment5\n" "void Foo::f() { f1(); }\n" + "\n" "/////////////\n" "// comment //\n" "/////////////\n" @@ -312,15 +319,17 @@ TEST(ClangMove, DontMoveAll) { const char ExpectedHeader[] = "#ifndef NEW_FOO_H\n" "#define NEW_FOO_H\n" + "\n" "class A {\npublic:\n int f();\n};\n" + "\n" "#endif // NEW_FOO_H\n"; const char Code[] = "#include \"foo.h\"\nint A::f() { return 0; }"; std::vector TestHeaders = { - "typedef int Int;\nclass A {\npublic:\n int f();\n};", - "using Int=int;\nclass A {\npublic:\n int f();\n};", - "class B {};\nclass A {\npublic:\n int f();\n};", - "void f() {};\nclass A {\npublic:\n int f();\n};", - "enum Color { RED };\nclass A {\npublic:\n int f();\n};", + "typedef int Int;\nclass A {\npublic:\n int f();\n};\n", + "using Int=int;\nclass A {\npublic:\n int f();\n};\n", + "class B {};\nclass A {\npublic:\n int f();\n};\n", + "void f() {};\nclass A {\npublic:\n int f();\n};\n", + "enum Color { RED };\nclass A {\npublic:\n int f();\n};\n", }; move::ClangMoveTool::MoveDefinitionSpec Spec; Spec.Names.push_back("A"); @@ -332,7 +341,7 @@ auto Results = runClangMoveOnCode(Spec, Header.c_str(), Code); EXPECT_EQ(ExpectedHeader, Results[Spec.NewHeader]); // The expected old header should not contain class A definition. - std::string ExpectedOldHeader = Header.substr(0, Header.size() - 31); + std::string ExpectedOldHeader = Header.substr(0, Header.size() - 32); EXPECT_EQ(ExpectedOldHeader, Results[Spec.OldHeader]); } }