Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp =================================================================== --- clang/lib/Format/NamespaceEndCommentsFixer.cpp +++ clang/lib/Format/NamespaceEndCommentsFixer.cpp @@ -180,9 +180,13 @@ if (NamespaceTok->is(tok::l_brace)) { // "namespace" keyword can be on the line preceding '{', e.g. in styles // where BraceWrapping.AfterNamespace is true. - if (StartLineIndex > 0) + if (StartLineIndex > 0) { NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First; + if (AnnotatedLines[StartLineIndex - 1]->endsWith(tok::semi)) + return nullptr; + } } + return NamespaceTok->getNamespaceToken(); } Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp =================================================================== --- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -1185,6 +1185,82 @@ "}\n", Style)); } + +TEST_F(ShortNamespaceLinesTest, NamespaceAlias) { + auto Style = getLLVMStyle(); + + EXPECT_EQ("namespace n = nn;\n" + "{\n" + " int i;\n" + " int j;\n" + "}\n", + fixNamespaceEndComments("namespace n = nn;\n" + "{\n" + " int i;\n" + " int j;\n" + "}\n", + Style)); + + EXPECT_EQ("namespace n = nn; // comment\n" + "{\n" + " int i;\n" + " int j;\n" + "}\n", + fixNamespaceEndComments("namespace n = nn; // comment\n" + "{\n" + " int i;\n" + " int j;\n" + "}\n", + Style)); + + EXPECT_EQ("namespace n = nn; /* comment */\n" + "{\n" + " int i;\n" + " int j;\n" + "}\n", + fixNamespaceEndComments("namespace n = nn; /* comment */\n" + "{\n" + " int i;\n" + " int j;\n" + "}\n", + Style)); + + EXPECT_EQ( + "namespace n = nn; /* comment */ /* comment2 */\n" + "{\n" + " int i;\n" + " int j;\n" + "}\n", + fixNamespaceEndComments("namespace n = nn; /* comment */ /* comment2 */\n" + "{\n" + " int i;\n" + " int j;\n" + "}\n", + Style)); + + EXPECT_EQ("namespace n = nn; {\n" + " int i;\n" + " int j;\n" + "}\n", + fixNamespaceEndComments("namespace n = nn; {\n" + " int i;\n" + " int j;\n" + "}\n", + Style)); + EXPECT_EQ("int foo;\n" + "namespace n\n" + "{\n" + " int i;\n" + " int j;\n" + "}// namespace n\n", + fixNamespaceEndComments("int foo;\n" + "namespace n\n" + "{\n" + " int i;\n" + " int j;\n" + "}\n", + Style)); +} } // end namespace } // end namespace format } // end namespace clang