diff --git a/clang-tools-extra/unittests/clang-include-fixer/IncludeFixerTest.cpp b/clang-tools-extra/unittests/clang-include-fixer/IncludeFixerTest.cpp --- a/clang-tools-extra/unittests/clang-include-fixer/IncludeFixerTest.cpp +++ b/clang-tools-extra/unittests/clang-include-fixer/IncludeFixerTest.cpp @@ -141,7 +141,7 @@ TEST(IncludeFixer, IncompleteType) { EXPECT_EQ( "#include \"foo.h\"\n#include \n" - "namespace std {\nclass string;\n}\nstd::string foo;\n", + "namespace std {\nclass string;\n} // namespace std\nstd::string foo;\n", runIncludeFixer("#include \"foo.h\"\n" "namespace std {\nclass string;\n}\nstring foo;\n")); @@ -192,9 +192,9 @@ } TEST(IncludeFixer, ScopedNamespaceSymbols) { - EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}", + EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n} // namespace a", runIncludeFixer("namespace a {\nb::bar b;\n}")); - EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n}", + EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n} // namespace A", runIncludeFixer("namespace A {\na::b::bar b;\n}")); EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nvoid func() { b::bar b; }\n} " "// namespace a", @@ -203,17 +203,17 @@ runIncludeFixer("namespace A { c::b::bar b; }\n")); // FIXME: The header should not be added here. Remove this after we support // full match. - EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n}", + EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n} // namespace A", runIncludeFixer("namespace A {\nb::bar b;\n}")); // Finds candidates for "str::StrCat". - EXPECT_EQ("#include \"strcat.h\"\nnamespace foo2 {\nstr::StrCat b;\n}", + EXPECT_EQ("#include \"strcat.h\"\nnamespace foo2 {\nstr::StrCat b;\n} // namespace foo2", runIncludeFixer("namespace foo2 {\nstr::StrCat b;\n}")); // str::StrCat2 doesn't exist. // In these two cases, StrCat2 is a nested class of class str. - EXPECT_EQ("#include \"str.h\"\nnamespace foo2 {\nstr::StrCat2 b;\n}", + EXPECT_EQ("#include \"str.h\"\nnamespace foo2 {\nstr::StrCat2 b;\n} // namespace foo2", runIncludeFixer("namespace foo2 {\nstr::StrCat2 b;\n}")); - EXPECT_EQ("#include \"str.h\"\nnamespace ns {\nstr::StrCat2 b;\n}", + EXPECT_EQ("#include \"str.h\"\nnamespace ns {\nstr::StrCat2 b;\n} // namespace ns", runIncludeFixer("namespace ns {\nstr::StrCat2 b;\n}")); } @@ -255,18 +255,18 @@ runIncludeFixer("a::b::bar b;\n")); EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", runIncludeFixer("bar b;\n")); - EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n", + EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n} // namespace a\n", runIncludeFixer("namespace a {\nb::bar b;\n}\n")); - EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n", + EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n} // namespace a\n", runIncludeFixer("namespace a {\nbar b;\n}\n")); EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nnamespace b{\nbar b;\n}\n} " "// namespace a\n", runIncludeFixer("namespace a {\nnamespace b{\nbar b;\n}\n}\n")); EXPECT_EQ("c::b::bar b;\n", runIncludeFixer("c::b::bar b;\n")); - EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar b;\n}\n", + EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar b;\n} // namespace d\n", runIncludeFixer("namespace d {\nbar b;\n}\n")); - EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar b;\n}\n", + EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar b;\n} // namespace c\n", runIncludeFixer("namespace c {\nbar b;\n}\n")); // Test common qualifiers reduction. @@ -278,24 +278,24 @@ runIncludeFixer("namespace d {\nnamespace a {\nbar b;\n}\n}\n")); // Test nested classes. - EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar::t b;\n}\n", + EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar::t b;\n} // namespace d\n", runIncludeFixer("namespace d {\nbar::t b;\n}\n")); - EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar::t b;\n}\n", + EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar::t b;\n} // namespace c\n", runIncludeFixer("namespace c {\nbar::t b;\n}\n")); - EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar::t b;\n}\n", + EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar::t b;\n} // namespace a\n", runIncludeFixer("namespace a {\nbar::t b;\n}\n")); EXPECT_EQ("#include \"color.h\"\nint test = a::b::Green;\n", runIncludeFixer("int test = Green;\n")); - EXPECT_EQ("#include \"color.h\"\nnamespace d {\nint test = a::b::Green;\n}\n", + EXPECT_EQ("#include \"color.h\"\nnamespace d {\nint test = a::b::Green;\n} // namespace d\n", runIncludeFixer("namespace d {\nint test = Green;\n}\n")); - EXPECT_EQ("#include \"color.h\"\nnamespace a {\nint test = b::Green;\n}\n", + EXPECT_EQ("#include \"color.h\"\nnamespace a {\nint test = b::Green;\n} // namespace a\n", runIncludeFixer("namespace a {\nint test = Green;\n}\n")); // Test global scope operator. EXPECT_EQ("#include \"bar.h\"\n::a::b::bar b;\n", runIncludeFixer("::a::b::bar b;\n")); - EXPECT_EQ("#include \"bar.h\"\nnamespace a {\n::a::b::bar b;\n}\n", + EXPECT_EQ("#include \"bar.h\"\nnamespace a {\n::a::b::bar b;\n} // namespace a\n", runIncludeFixer("namespace a {\n::a::b::bar b;\n}\n")); } diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -1611,6 +1611,9 @@ If ``true``, clang-format adds missing namespace end comments and fixes invalid existing ones. + Short namespaces will not be given an end comment, unless the comment + is invalid in which case it is always fixed. + .. code-block:: c++ true: false: diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1380,6 +1380,9 @@ /// If ``true``, clang-format adds missing namespace end comments and /// fixes invalid existing ones. + /// + /// Short namespaces will not be given an end comment, unless the comment + /// is invalid in which case it is always fixed. /// \code /// true: false: /// namespace a { vs. namespace a { diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp --- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp +++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp @@ -24,7 +24,7 @@ namespace { // The maximal number of unwrapped lines that a short namespace spans. // Short namespaces don't need an end comment. -static const int kShortNamespaceMaxLines = 1; +static const int kShortNamespaceMaxLines = 0; // Computes the name of a namespace given the namespace token. // Returns "" for anonymous namespace. diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -211,7 +211,7 @@ EXPECT_EQ("namespace N {\n" "\n" "int i;\n" - "}", + "} // namespace N", format("namespace N {\n" "\n" "int i;\n" @@ -220,7 +220,7 @@ EXPECT_EQ("/* something */ namespace N {\n" "\n" "int i;\n" - "}", + "} // namespace N", format("/* something */ namespace N {\n" "\n" "int i;\n" @@ -229,7 +229,7 @@ EXPECT_EQ("inline namespace N {\n" "\n" "int i;\n" - "}", + "} // namespace N", format("inline namespace N {\n" "\n" "int i;\n" @@ -238,7 +238,7 @@ EXPECT_EQ("/* something */ inline namespace N {\n" "\n" "int i;\n" - "}", + "} // namespace N", format("/* something */ inline namespace N {\n" "\n" "int i;\n" @@ -247,7 +247,7 @@ EXPECT_EQ("export namespace N {\n" "\n" "int i;\n" - "}", + "} // namespace N", format("export namespace N {\n" "\n" "int i;\n" @@ -369,7 +369,7 @@ EXPECT_EQ("namespace {\n" "int i;\n" "\n" - "}", + "} // namespace", format("namespace {\n" "int i;\n" "\n" @@ -8887,7 +8887,7 @@ format("void f ( ) { if ( a ) return }")); EXPECT_EQ("namespace N {\n" "void f()\n" - "}", + "} // namespace N", format("namespace N { void f() }")); EXPECT_EQ("namespace N {\n" "void f() {}\n" @@ -9839,7 +9839,7 @@ verifyFormat("namespace Foo\n" "{\n" "void Bar();\n" - "};", + "}; // namespace Foo", Style); } @@ -9872,7 +9872,7 @@ Style); verifyFormat("namespace Foo {\n" "void Bar();\n" - "};", + "}; // namespace Foo", Style); Style.BreakBeforeBraces = FormatStyle::BS_Custom; @@ -9913,7 +9913,7 @@ verifyFormat("namespace Foo\n" "{\n" "void Bar();\n" - "};", + "}; // namespace Foo", Style); } diff --git a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp --- a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -142,7 +142,7 @@ EXPECT_EQ("namespace A {\n" "namespace B {\n" "int i;\n" - "}\n" + "}// namespace B\n" "}// namespace A", fixNamespaceEndComments("namespace A {\n" "namespace B {\n" @@ -423,8 +423,9 @@ TEST_F(NamespaceEndCommentsFixerTest, DoesNotAddEndCommentForShortNamespace) { EXPECT_EQ("namespace {}", fixNamespaceEndComments("namespace {}")); EXPECT_EQ("namespace A {}", fixNamespaceEndComments("namespace A {}")); - EXPECT_EQ("namespace A { a }", fixNamespaceEndComments("namespace A { a }")); - EXPECT_EQ("namespace A { a };", + EXPECT_EQ("namespace A { a }// namespace A", + fixNamespaceEndComments("namespace A { a }")); + EXPECT_EQ("namespace A { a };// namespace A", fixNamespaceEndComments("namespace A { a };")); }