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 @@ -121,7 +121,25 @@ // Named namespace comments must not mention anonymous namespace. if (!NamespaceName.empty() && !AnonymousInComment.empty()) return false; - return NamespaceNameInComment == NamespaceName; + if (NamespaceNameInComment == NamespaceName) + return true; + + // Has namespace comment flowed onto the next line. + // } // namespace + // // verylongnamespacenamethatdidnotfitonthepreviouscommentline + if (!(Comment->Next && Comment->Next->is(TT_LineComment))) + return false; + + static const llvm::Regex CommentPattern = llvm::Regex( + "^/[/*] *( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$", llvm::Regex::IgnoreCase); + + // Pull out just the comment text. + if (!CommentPattern.match(Comment->Next->TokenText, &Groups)) { + return false; + } + NamespaceNameInComment = Groups.size() > 2 ? Groups[2] : ""; + + return (NamespaceNameInComment == NamespaceName); } void addEndComment(const FormatToken *RBraceTok, StringRef EndCommentText, 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 @@ -16238,6 +16238,74 @@ verifyFormat("operator&&(int(&&)(), class Foo);", Style); } +TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { + // These tests are not in NamespaceFixer because that doesn't + // test its interaction with line wrapping + FormatStyle Style = getLLVMStyle(); + Style.ColumnLimit = 80; + verifyFormat("namespace {\n" + "int i;\n" + "int j;\n" + "} // namespace", + Style); + + verifyFormat("namespace AAA {\n" + "int i;\n" + "int j;\n" + "} // namespace AAA", + Style); + + EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" + "int i;\n" + "int j;\n" + "} // namespace Averyveryveryverylongnamespace", + format("namespace Averyveryveryverylongnamespace {\n" + "int i;\n" + "int j;\n" + "}", + Style)); + + EXPECT_EQ( + "namespace " + "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" + " went::mad::now {\n" + "int i;\n" + "int j;\n" + "} // namespace\n" + " // " + "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" + "went::mad::now", + format("namespace " + "would::it::save::you::a::lot::of::time::if_::i::" + "just::gave::up::and_::went::mad::now {\n" + "int i;\n" + "int j;\n" + "}", + Style)); + + // This used to duplicate the comment again and again on subsequent runs + EXPECT_EQ( + "namespace " + "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" + " went::mad::now {\n" + "int i;\n" + "int j;\n" + "} // namespace\n" + " // " + "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" + "went::mad::now", + format("namespace " + "would::it::save::you::a::lot::of::time::if_::i::" + "just::gave::up::and_::went::mad::now {\n" + "int i;\n" + "int j;\n" + "} // namespace\n" + " // " + "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" + "and_::went::mad::now", + Style)); +} + } // namespace } // namespace format } // namespace clang