This revision was automatically updated to reflect the committed changes. Closed by commit rGe8ea35e63f50: [clang-format] [PR44345] Long namespace closing comment is duplicated endlessly (authored by MyDeveloperDay).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D79935/new/ https://reviews.llvm.org/D79935 Files: clang/lib/Format/NamespaceEndCommentsFixer.cpp clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ 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 Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp =================================================================== --- clang/lib/Format/NamespaceEndCommentsFixer.cpp +++ 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,
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits