[clang] bcb1b77 - [clang-format] Add option to separate comment alignment between ... (#165033)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 7 07:12:34 PST 2025
Author: Björn Schäpers
Date: 2025-11-07T15:12:30Z
New Revision: bcb1b773f6412323c0dbe555c0ccd00b6cd1ae0e
URL: https://github.com/llvm/llvm-project/commit/bcb1b773f6412323c0dbe555c0ccd00b6cd1ae0e
DIFF: https://github.com/llvm/llvm-project/commit/bcb1b773f6412323c0dbe555c0ccd00b6cd1ae0e.diff
LOG: [clang-format] Add option to separate comment alignment between ... (#165033)
normal lines and PP directives.
Handling PP directives differently can be desired, like in #161848.
Changing the default is not an option, there are tests for exactly the
current behaviour.
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/ConfigParseTest.cpp
clang/unittests/Format/FormatTestComments.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 0b4a4849f6ccc..94d6f0d27619f 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -1673,6 +1673,16 @@ the configuration (without a prefix: ``Auto``).
int abcdef; // but this isn't
+ * ``bool AlignPPAndNotPP`` If comments following preprocessor directive should be aligned with
+ comments that don't.
+
+ .. code-block:: c++
+
+ true: false:
+ #define A // Comment vs. #define A // Comment
+ #define AB // Aligned #define AB // Aligned
+ int i; // Aligned int i; // Not aligned
+
.. _AllowAllArgumentsOnNextLine:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1b0a8d71a2cf8..3a4e1fce2511e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -653,6 +653,7 @@ clang-format
- Deprecate ``AlwaysBreak`` and ``BlockIndent`` suboptions from the
``AlignAfterOpenBracket`` option, and make ``AlignAfterOpenBracket`` a
``bool`` type.
+- Add ``AlignPPAndNotPP`` suboption to ``AlignTrailingComments``.
libclang
--------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index f246defc1fe81..b6f124f948b59 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -601,9 +601,19 @@ struct FormatStyle {
/// int abcdef; // but this isn't
/// \endcode
unsigned OverEmptyLines;
+ /// If comments following preprocessor directive should be aligned with
+ /// comments that don't.
+ /// \code
+ /// true: false:
+ /// #define A // Comment vs. #define A // Comment
+ /// #define AB // Aligned #define AB // Aligned
+ /// int i; // Aligned int i; // Not aligned
+ /// \endcode
+ bool AlignPPAndNotPP;
bool operator==(const TrailingCommentsAlignmentStyle &R) const {
- return Kind == R.Kind && OverEmptyLines == R.OverEmptyLines;
+ return Kind == R.Kind && OverEmptyLines == R.OverEmptyLines &&
+ AlignPPAndNotPP == R.AlignPPAndNotPP;
}
bool operator!=(const TrailingCommentsAlignmentStyle &R) const {
return !(*this == R);
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index dd14fcd72922f..9bbb33cb14502 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -876,27 +876,28 @@ template <> struct MappingTraits<FormatStyle::TrailingCommentsAlignmentStyle> {
FormatStyle::TrailingCommentsAlignmentStyle &Value) {
IO.enumCase(Value, "Leave",
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Leave, 0}));
+ {FormatStyle::TCAS_Leave, 0, true}));
IO.enumCase(Value, "Always",
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Always, 0}));
+ {FormatStyle::TCAS_Always, 0, true}));
IO.enumCase(Value, "Never",
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Never, 0}));
+ {FormatStyle::TCAS_Never, 0, true}));
// For backwards compatibility
IO.enumCase(Value, "true",
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Always, 0}));
+ {FormatStyle::TCAS_Always, 0, true}));
IO.enumCase(Value, "false",
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Never, 0}));
+ {FormatStyle::TCAS_Never, 0, true}));
}
static void mapping(IO &IO,
FormatStyle::TrailingCommentsAlignmentStyle &Value) {
+ IO.mapOptional("AlignPPAndNotPP", Value.AlignPPAndNotPP);
IO.mapOptional("Kind", Value.Kind);
IO.mapOptional("OverEmptyLines", Value.OverEmptyLines);
}
@@ -1651,6 +1652,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignTrailingComments = {};
LLVMStyle.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
LLVMStyle.AlignTrailingComments.OverEmptyLines = 0;
+ LLVMStyle.AlignTrailingComments.AlignPPAndNotPP = true;
LLVMStyle.AllowAllArgumentsOnNextLine = true;
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
LLVMStyle.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Never;
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 406c77cb3ae8f..fece3843c6470 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1007,9 +1007,13 @@ void WhitespaceManager::alignTrailingComments() {
return;
const int Size = Changes.size();
+ if (Size == 0)
+ return;
+
int MinColumn = 0;
int StartOfSequence = 0;
bool BreakBeforeNext = false;
+ bool IsInPP = Changes.front().Tok->Tok.is(tok::hash);
int NewLineThreshold = 1;
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Always)
NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
@@ -1018,7 +1022,19 @@ void WhitespaceManager::alignTrailingComments() {
auto &C = Changes[I];
if (C.StartOfBlockComment)
continue;
- Newlines += C.NewlinesBefore;
+ if (C.NewlinesBefore != 0) {
+ Newlines += C.NewlinesBefore;
+ const bool WasInPP = std::exchange(
+ IsInPP, C.Tok->Tok.is(tok::hash) || (IsInPP && C.IsTrailingComment) ||
+ C.ContinuesPPDirective);
+ if (IsInPP != WasInPP && !Style.AlignTrailingComments.AlignPPAndNotPP) {
+ alignTrailingComments(StartOfSequence, I, MinColumn);
+ MinColumn = 0;
+ MaxColumn = INT_MAX;
+ StartOfSequence = I;
+ Newlines = 0;
+ }
+ }
if (!C.IsTrailingComment)
continue;
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 43b21176962ea..d578fa7a1a1e8 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -589,20 +589,20 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("AlignTrailingComments: Leave", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Leave, 0}));
+ {FormatStyle::TCAS_Leave, 0, true}));
CHECK_PARSE("AlignTrailingComments: Always", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Always, 0}));
+ {FormatStyle::TCAS_Always, 0, true}));
CHECK_PARSE("AlignTrailingComments: Never", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Never, 0}));
+ {FormatStyle::TCAS_Never, 0, true}));
// For backwards compatibility
CHECK_PARSE("AlignTrailingComments: true", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Always, 0}));
+ {FormatStyle::TCAS_Always, 0, true}));
CHECK_PARSE("AlignTrailingComments: false", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
- {FormatStyle::TCAS_Never, 0}));
+ {FormatStyle::TCAS_Never, 0, true}));
CHECK_PARSE_NESTED_VALUE("Kind: Always", AlignTrailingComments, Kind,
FormatStyle::TCAS_Always);
CHECK_PARSE_NESTED_VALUE("Kind: Never", AlignTrailingComments, Kind,
@@ -611,6 +611,7 @@ TEST(ConfigParseTest, ParsesConfiguration) {
FormatStyle::TCAS_Leave);
CHECK_PARSE_NESTED_VALUE("OverEmptyLines: 1234", AlignTrailingComments,
OverEmptyLines, 1234u);
+ CHECK_PARSE_NESTED_BOOL(AlignTrailingComments, AlignPPAndNotPP);
Style.UseTab = FormatStyle::UT_ForIndentation;
CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index d7b2257605482..684d3014fa7bb 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -3378,6 +3378,66 @@ TEST_F(FormatTestComments, DontAlignOverScope) {
"int foobar; // group");
}
+TEST_F(FormatTestComments, DontAlignOverPPDirective) {
+ auto Style = getLLVMStyle();
+ Style.AlignTrailingComments.AlignPPAndNotPP = false;
+
+ verifyFormat("int i; // Aligned\n"
+ "int long; // with this\n"
+ "#define FOO // only aligned\n"
+ "#define LOOONG // with other pp directives\n"
+ "int loooong; // new alignment",
+ "int i;//Aligned\n"
+ "int long;//with this\n"
+ "#define FOO //only aligned\n"
+ "#define LOOONG //with other pp directives\n"
+ "int loooong; //new alignment",
+ Style);
+
+ verifyFormat("#define A // Comment\n"
+ "#define AB // Comment",
+ Style);
+
+ Style.ColumnLimit = 30;
+ verifyNoChange("#define A // Comment\n"
+ " // Continued\n"
+ "int i = 0; // New Stuff\n"
+ " // Continued\n"
+ "#define Func(X) \\\n"
+ " X(); \\\n"
+ " X(); // Comment\n"
+ " // Continued\n"
+ "long loong = 1; // Dont align",
+ Style);
+
+ verifyFormat("#define A // Comment that\n"
+ " // would wrap\n"
+ "#define FOO // For the\n"
+ " // alignment\n"
+ "#define B // Also\n"
+ " // aligned",
+ "#define A // Comment that would wrap\n"
+ "#define FOO // For the alignment\n"
+ "#define B // Also\n"
+ " // aligned",
+ Style);
+
+ Style.AlignTrailingComments.OverEmptyLines = 1;
+ verifyNoChange("#define A // Comment\n"
+ "\n"
+ " // Continued\n"
+ "int i = 0; // New Stuff\n"
+ "\n"
+ " // Continued\n"
+ "#define Func(X) \\\n"
+ " X(); \\\n"
+ " X(); // Comment\n"
+ "\n"
+ " // Continued\n"
+ "long loong = 1; // Dont align",
+ Style);
+}
+
TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
verifyFormat("/*\n"
" */",
More information about the cfe-commits
mailing list