[PATCH] D156065: [clang-format] Insert namespace comments with leading spaces
This revision was automatically updated to reflect the committed changes. Closed by commit rG2f6b2dafb783: [clang-format] Insert namespace comments with leading spaces (authored by owenpan). Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D156065/new/ https://reviews.llvm.org/D156065 Files: clang/lib/Format/NamespaceEndCommentsFixer.cpp clang/unittests/Format/FormatTestComments.cpp clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp === --- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -46,7 +46,7 @@ EXPECT_EQ("namespace {\n" "int i;\n" "int j;\n" -"}// namespace", +"} // namespace", fixNamespaceEndComments("namespace {\n" "int i;\n" "int j;\n" @@ -55,7 +55,7 @@ EXPECT_EQ("namespace {\n" "int i;\n" "int j;\n" -"}// namespace\n", +"} // namespace\n", fixNamespaceEndComments("namespace {\n" "int i;\n" "int j;\n" @@ -63,7 +63,7 @@ EXPECT_EQ("namespace A {\n" "int i;\n" "int j;\n" -"}// namespace A", +"} // namespace A", fixNamespaceEndComments("namespace A {\n" "int i;\n" "int j;\n" @@ -72,7 +72,7 @@ "namespace M(x) {\n" "int i;\n" "int j;\n" -"}// namespace M(x)", +"} // namespace M(x)", fixNamespaceEndComments("#define M(x) x##x\n" "namespace M(x) {\n" "int i;\n" @@ -82,7 +82,7 @@ "namespace A::M(x) {\n" "int i;\n" "int j;\n" -"}// namespace A::M(x)", +"} // namespace A::M(x)", fixNamespaceEndComments("#define M(x) x##x\n" "namespace A::M(x) {\n" "int i;\n" @@ -92,7 +92,7 @@ "namespace M(x)::A {\n" "int i;\n" "int j;\n" -"}// namespace M(x)::A", +"} // namespace M(x)::A", fixNamespaceEndComments("#define M(x) x##x\n" "namespace M(x)::A {\n" "int i;\n" @@ -102,7 +102,7 @@ "namespace A::inline M(x)::B {\n" "int i;\n" "int j;\n" -"}// namespace A::inline M(x)::B", +"} // namespace A::inline M(x)::B", fixNamespaceEndComments("#define M(x) x##x\n" "namespace A::inline M(x)::B {\n" "int i;\n" @@ -112,7 +112,7 @@ "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n" "int i;\n" "int j;\n" -"}// namespace A::inline M(x)::A", +"} // namespace A::inline M(x)::A", fixNamespaceEndComments( "#define M(x) x##x\n" "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n" @@ -123,7 +123,7 @@ "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n" "int i;\n" "int j;\n" - "}// namespace A", + "} // namespace A", fixNamespaceEndComments( "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n" "int i;\n" @@ -132,7 +132,7 @@ EXPECT_EQ("namespace /* comment */ [[deprecated(\"foo\")]] A {\n" "int i;\n" "int j;\n" -"}// namespace A", +"} // namespace A", fixNamespaceEndComments( "namespace /* comment */ [[deprecated(\"foo\")]] A {\n" "int i;\n" @@ -143,7 +143,7 @@ "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ M(x) {\n" "int i;\n" "int j;\n" - "}// namespace M(x)", + "} // namespace M(x)", fixNamespaceEndComments("#define M(x) x##x\n" "namespace /* comment */ " "[[deprecated(\"foo\")]] /* comment */ M(x) {\n" @@ -155,7 +155,7 @@ "A::M(x) {\n" "int i;\n" "int j;\n" -"}// namespace A::M(x)", +"} // namespace A::M(x)", fixNamespaceEndComments( "#define M(x) x##x\n" "namespace /* comment */ " @@ -168,7 +168,7 @@ "M(x) /* comment */ {\n" "int i;\n" "int j;\n" -"}// namespace M(x)", +"} // namespace
[PATCH] D156065: [clang-format] Insert namespace comments with leading spaces
owenpan updated this revision to Diff 543415. owenpan added a comment. Fixed unit tests in NamespaceEndCommentsFixerTest.cpp. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D156065/new/ https://reviews.llvm.org/D156065 Files: clang/lib/Format/NamespaceEndCommentsFixer.cpp clang/unittests/Format/FormatTestComments.cpp clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp === --- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -46,7 +46,7 @@ EXPECT_EQ("namespace {\n" "int i;\n" "int j;\n" -"}// namespace", +"} // namespace", fixNamespaceEndComments("namespace {\n" "int i;\n" "int j;\n" @@ -55,7 +55,7 @@ EXPECT_EQ("namespace {\n" "int i;\n" "int j;\n" -"}// namespace\n", +"} // namespace\n", fixNamespaceEndComments("namespace {\n" "int i;\n" "int j;\n" @@ -63,7 +63,7 @@ EXPECT_EQ("namespace A {\n" "int i;\n" "int j;\n" -"}// namespace A", +"} // namespace A", fixNamespaceEndComments("namespace A {\n" "int i;\n" "int j;\n" @@ -72,7 +72,7 @@ "namespace M(x) {\n" "int i;\n" "int j;\n" -"}// namespace M(x)", +"} // namespace M(x)", fixNamespaceEndComments("#define M(x) x##x\n" "namespace M(x) {\n" "int i;\n" @@ -82,7 +82,7 @@ "namespace A::M(x) {\n" "int i;\n" "int j;\n" -"}// namespace A::M(x)", +"} // namespace A::M(x)", fixNamespaceEndComments("#define M(x) x##x\n" "namespace A::M(x) {\n" "int i;\n" @@ -92,7 +92,7 @@ "namespace M(x)::A {\n" "int i;\n" "int j;\n" -"}// namespace M(x)::A", +"} // namespace M(x)::A", fixNamespaceEndComments("#define M(x) x##x\n" "namespace M(x)::A {\n" "int i;\n" @@ -102,7 +102,7 @@ "namespace A::inline M(x)::B {\n" "int i;\n" "int j;\n" -"}// namespace A::inline M(x)::B", +"} // namespace A::inline M(x)::B", fixNamespaceEndComments("#define M(x) x##x\n" "namespace A::inline M(x)::B {\n" "int i;\n" @@ -112,7 +112,7 @@ "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n" "int i;\n" "int j;\n" -"}// namespace A::inline M(x)::A", +"} // namespace A::inline M(x)::A", fixNamespaceEndComments( "#define M(x) x##x\n" "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n" @@ -123,7 +123,7 @@ "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n" "int i;\n" "int j;\n" - "}// namespace A", + "} // namespace A", fixNamespaceEndComments( "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n" "int i;\n" @@ -132,7 +132,7 @@ EXPECT_EQ("namespace /* comment */ [[deprecated(\"foo\")]] A {\n" "int i;\n" "int j;\n" -"}// namespace A", +"} // namespace A", fixNamespaceEndComments( "namespace /* comment */ [[deprecated(\"foo\")]] A {\n" "int i;\n" @@ -143,7 +143,7 @@ "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ M(x) {\n" "int i;\n" "int j;\n" - "}// namespace M(x)", + "} // namespace M(x)", fixNamespaceEndComments("#define M(x) x##x\n" "namespace /* comment */ " "[[deprecated(\"foo\")]] /* comment */ M(x) {\n" @@ -155,7 +155,7 @@ "A::M(x) {\n" "int i;\n" "int j;\n" -"}// namespace A::M(x)", +"} // namespace A::M(x)", fixNamespaceEndComments( "#define M(x) x##x\n" "namespace /* comment */ " @@ -168,7 +168,7 @@ "M(x) /* comment */ {\n" "int i;\n" "int j;\n" -"}// namespace M(x)", +"} // namespace M(x)", fixNamespaceEndComments( "#define M(x) x##x\n"
[PATCH] D156065: [clang-format] Insert namespace comments with leading spaces
HazardyKnusperkeks added inline comments. Comment at: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp:29 +FormatStyle S = Style; +S.SpacesBeforeTrailingComments = 0; tooling::Replacements Replaces = I'd rather fix all the tests. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D156065/new/ https://reviews.llvm.org/D156065 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D156065: [clang-format] Insert namespace comments with leading spaces
owenpan created this revision. Herald added projects: All, clang, clang-format. Herald added a subscriber: cfe-commits. Herald added reviewers: rymiel, HazardyKnusperkeks, MyDeveloperDay. owenpan requested review of this revision. Insert missing `namespace` comments with `SpacesBeforeTrailingComments` leading spaces. Fixes https://github.com/llvm/llvm-project/issues/64051. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D156065 Files: clang/lib/Format/NamespaceEndCommentsFixer.cpp clang/unittests/Format/FormatTestComments.cpp clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp === --- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -25,8 +25,10 @@ const FormatStyle = getLLVMStyle()) { LLVM_DEBUG(llvm::errs() << "---\n"); LLVM_DEBUG(llvm::errs() << Code << "\n\n"); +FormatStyle S = Style; +S.SpacesBeforeTrailingComments = 0; tooling::Replacements Replaces = -clang::format::fixNamespaceEndComments(Style, Code, Ranges, ""); +clang::format::fixNamespaceEndComments(S, Code, Ranges, ""); auto Result = applyAllReplacements(Code, Replaces); EXPECT_TRUE(static_cast(Result)); LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); Index: clang/unittests/Format/FormatTestComments.cpp === --- clang/unittests/Format/FormatTestComments.cpp +++ clang/unittests/Format/FormatTestComments.cpp @@ -3043,6 +3043,16 @@ "// comment", Style)); + verifyFormat("namespace ns {\n" + "int i;\n" + "int j;\n" + "} // namespace ns", + "namespace ns {\n" + "int i;\n" + "int j;\n" + "}", + Style); + // Allow to keep 2 empty lines Style.MaxEmptyLinesToKeep = 2; EXPECT_EQ("// do not touch\n" Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp === --- clang/lib/Format/NamespaceEndCommentsFixer.cpp +++ clang/lib/Format/NamespaceEndCommentsFixer.cpp @@ -360,8 +360,12 @@ Style.SpacesInLineCommentPrefix.Minimum); if (!hasEndComment(EndCommentPrevTok)) { bool isShort = I - StartLineIndex <= Style.ShortNamespaceLines + 1; - if (!isShort) -addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, ); + if (!isShort) { +addEndComment(EndCommentPrevTok, + std::string(Style.SpacesBeforeTrailingComments, ' ') + + EndCommentText, + SourceMgr, ); + } } else if (!validEndComment(EndCommentPrevTok, NamespaceName, NamespaceTok)) { updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, ); Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp === --- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -25,8 +25,10 @@ const FormatStyle = getLLVMStyle()) { LLVM_DEBUG(llvm::errs() << "---\n"); LLVM_DEBUG(llvm::errs() << Code << "\n\n"); +FormatStyle S = Style; +S.SpacesBeforeTrailingComments = 0; tooling::Replacements Replaces = -clang::format::fixNamespaceEndComments(Style, Code, Ranges, ""); +clang::format::fixNamespaceEndComments(S, Code, Ranges, ""); auto Result = applyAllReplacements(Code, Replaces); EXPECT_TRUE(static_cast(Result)); LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); Index: clang/unittests/Format/FormatTestComments.cpp === --- clang/unittests/Format/FormatTestComments.cpp +++ clang/unittests/Format/FormatTestComments.cpp @@ -3043,6 +3043,16 @@ "// comment", Style)); + verifyFormat("namespace ns {\n" + "int i;\n" + "int j;\n" + "} // namespace ns", + "namespace ns {\n" + "int i;\n" + "int j;\n" + "}", + Style); + // Allow to keep 2 empty lines Style.MaxEmptyLinesToKeep = 2; EXPECT_EQ("// do not touch\n" Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp === --- clang/lib/Format/NamespaceEndCommentsFixer.cpp +++ clang/lib/Format/NamespaceEndCommentsFixer.cpp @@ -360,8 +360,12 @@ Style.SpacesInLineCommentPrefix.Minimum); if (!hasEndComment(EndCommentPrevTok)) { bool isShort = I - StartLineIndex