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 @@ -51,7 +51,9 @@ } Tok = FirstNSTok; - while (Tok && !Tok->is(tok::l_brace)) { + while (Tok && !Tok->is(tok::l_brace) && + (Tok->Tok.isAnyIdentifier() || Tok->is(tok::coloncolon) || + Tok->is(tok::kw_inline))) { name += Tok->TokenText; if (Tok->is(tok::kw_inline)) name += " "; diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2597,10 +2597,12 @@ parseParens(); } else { while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline, - tok::l_square, tok::period) || + tok::l_square, tok::period, tok::l_paren) || (Style.isCSharp() && FormatTok->is(tok::kw_union))) if (FormatTok->is(tok::l_square)) parseSquare(); + else if (FormatTok->is(tok::l_paren)) + parseParens(); else nextToken(); } 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 @@ -3738,6 +3738,36 @@ "void f() { f(); }\n" "}", LLVMWithNoNamespaceFix); + verifyFormat("#define M(x) x##x\n" + "namespace M(x) {\n" + "class A {};\n" + "void f() { f(); }\n" + "}", + LLVMWithNoNamespaceFix); + verifyFormat("#define M(x) x##x\n" + "namespace N::inline M(x) {\n" + "class A {};\n" + "void f() { f(); }\n" + "}", + LLVMWithNoNamespaceFix); + verifyFormat("#define M(x) x##x\n" + "namespace M(x)::inline N {\n" + "class A {};\n" + "void f() { f(); }\n" + "}", + LLVMWithNoNamespaceFix); + verifyFormat("#define M(x) x##x\n" + "namespace N::M(x) {\n" + "class A {};\n" + "void f() { f(); }\n" + "}", + LLVMWithNoNamespaceFix); + verifyFormat("#define M(x) x##x\n" + "namespace M::N(x) {\n" + "class A {};\n" + "void f() { f(); }\n" + "}", + LLVMWithNoNamespaceFix); verifyFormat("namespace N::inline D {\n" "class A {};\n" "void f() { f(); }\n" 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 @@ -68,6 +68,27 @@ "int i;\n" "int j;\n" "}")); + // FIXME: Expand macro for namepsace name. + EXPECT_EQ("#define M(x) x##x\n" + "namespace M(x) {\n" + "int i;\n" + "int j;\n" + "}// namespace", + fixNamespaceEndComments("#define M(x) x##x\n" + "namespace M(x) {\n" + "int i;\n" + "int j;\n" + "}")); + EXPECT_EQ("#define M(x) x##x\n" + "namespace A::M(x) {\n" + "int i;\n" + "int j;\n" + "}// namespace A::M", + fixNamespaceEndComments("#define M(x) x##x\n" + "namespace A::M(x) {\n" + "int i;\n" + "int j;\n" + "}")); EXPECT_EQ("inline namespace A {\n" "int i;\n" "int j;\n"