[clang] eb35e0e - [clang-format] don't break up #-style comment sections
Krasimir Georgiev via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 22 07:29:59 PDT 2022
Author: Krasimir Georgiev
Date: 2022-03-22T15:29:02+01:00
New Revision: eb35e0ecbe0ae153481f1df16d91cc6001477d21
URL: https://github.com/llvm/llvm-project/commit/eb35e0ecbe0ae153481f1df16d91cc6001477d21
DIFF: https://github.com/llvm/llvm-project/commit/eb35e0ecbe0ae153481f1df16d91cc6001477d21.diff
LOG: [clang-format] don't break up #-style comment sections
Follow-up from https://github.com/llvm/llvm-project/commit/36d13d3f8adb3d1a6bae71370afa23d11a94dc78; https://reviews.llvm.org/D121451.
Restore the old behavior in situations where we use # as comments and long strings of #'s for comment sections.
Reviewed By: MyDeveloperDay
Differential Revision: https://reviews.llvm.org/D122230
Added:
Modified:
clang/lib/Format/BreakableToken.cpp
clang/unittests/Format/FormatTestTextProto.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp
index db7361bf5f2ff..94bd9823e1f5a 100644
--- a/clang/lib/Format/BreakableToken.cpp
+++ b/clang/lib/Format/BreakableToken.cpp
@@ -785,7 +785,16 @@ BreakableLineCommentSection::BreakableLineCommentSection(
Lines[i].substr(IndentPrefix.size(), FirstCharByteSize),
Encoding) != 1)
return false;
- if (FirstCommentChar == '#')
+ // In C-like comments, add a space before #. For example this is useful
+ // to preserve the relative indentation when commenting out code with
+ // #includes.
+ //
+ // In languages using # as the comment leader such as proto, don't
+ // add a space to support patterns like:
+ // #########
+ // # section
+ // #########
+ if (FirstCommentChar == '#' && !TokenText.startswith("#"))
return false;
return FirstCommentChar == '\\' || isPunctuation(FirstCommentChar) ||
isHorizontalWhitespace(FirstCommentChar);
diff --git a/clang/unittests/Format/FormatTestTextProto.cpp b/clang/unittests/Format/FormatTestTextProto.cpp
index 44b67d275bcc3..10a43c1519d6d 100644
--- a/clang/unittests/Format/FormatTestTextProto.cpp
+++ b/clang/unittests/Format/FormatTestTextProto.cpp
@@ -408,6 +408,23 @@ TEST_F(FormatTestTextProto, UnderstandsHashComments) {
"dd: 100\n"
"#### another quadriple-hash comment\n",
Style));
+
+ // Ensure we support a common pattern for naming sections.
+ EXPECT_EQ("##############\n"
+ "# section name\n"
+ "##############",
+ format("##############\n"
+ "# section name\n"
+ "##############",
+ Style));
+
+ EXPECT_EQ("///////////////\n"
+ "// section name\n"
+ "///////////////",
+ format("///////////////\n"
+ "// section name\n"
+ "///////////////",
+ Style));
}
TEST_F(FormatTestTextProto, FormatsExtensions) {
More information about the cfe-commits
mailing list