[clang] [clang-format] Remove special handling of comments after brace/paren (PR #71672)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 24 14:11:34 PDT 2025
https://github.com/HazardyKnusperkeks updated https://github.com/llvm/llvm-project/pull/71672
>From 8f6e5bcc2bfa7fe65c8ca37493a26861f455829b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Wed, 24 Sep 2025 23:02:39 +0200
Subject: [PATCH] [clang-format] Remove special handling of comments after
brace/paren
Fixes http://llvm.org/PR55487 (#55487)
The code did not match the documentation about Cpp11BracedListStyle.
Changed handling of comments after opening braces, which are supposedly
function call like to behave exactly like their parenthesis counter
part.
---
clang/lib/Format/ContinuationIndenter.cpp | 11 ++--
clang/lib/Format/FormatToken.h | 3 +
clang/lib/Format/TokenAnnotator.cpp | 13 +---
clang/unittests/Format/FormatTest.cpp | 18 +++---
clang/unittests/Format/FormatTestComments.cpp | 61 +++++++++++++++++--
5 files changed, 76 insertions(+), 30 deletions(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 9413c13a4137e..847ffae59c387 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -920,12 +920,15 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// align the commas with the opening paren.
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
- Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
- Previous.isNot(TT_TableGenDAGArgOpener) &&
- Previous.isNot(TT_TableGenDAGArgOpenerToBreak) &&
+ Previous.isNotOneOf(TT_ObjCMethodExpr, TT_RequiresClause,
+ TT_TableGenDAGArgOpener,
+ TT_TableGenDAGArgOpenerToBreak) &&
!(Current.MacroParent && Previous.MacroParent) &&
(Current.isNot(TT_LineComment) ||
- Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen)) &&
+ (Previous.is(BK_BracedInit) &&
+ (!Style.Cpp11BracedListStyle || !Previous.Previous ||
+ Previous.Previous->isNot(TT_StartOfName))) ||
+ Previous.is(TT_VerilogMultiLineListLParen)) &&
!IsInTemplateString(Current)) {
CurrentState.Indent = State.Column + Spaces;
CurrentState.IsAligned = true;
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index e04b0e7af10c0..6639d9d052d47 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -645,6 +645,9 @@ struct FormatToken {
return is(K1) || isOneOf(K2, Ks...);
}
template <typename T> bool isNot(T Kind) const { return !is(Kind); }
+ template <typename... Ts> bool isNotOneOf(Ts... Ks) const {
+ return !isOneOf(Ks...);
+ }
bool isIf(bool AllowConstexprMacro = true) const {
return is(tok::kw_if) || endsSequence(tok::kw_constexpr, tok::kw_if) ||
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 4bfb803ebedf7..276583b461532 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4099,16 +4099,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
for (auto *Current = First->Next; Current; Current = Current->Next) {
const FormatToken *Prev = Current->Previous;
if (Current->is(TT_LineComment)) {
- if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
- Current->SpacesRequiredBefore =
- (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
- ? 0
- : 1;
- } else if (Prev->is(TT_VerilogMultiLineListLParen)) {
- Current->SpacesRequiredBefore = 0;
- } else {
- Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
- }
+ Current->SpacesRequiredBefore = Prev->is(TT_VerilogMultiLineListLParen)
+ ? 0
+ : Style.SpacesBeforeTrailingComments;
// If we find a trailing comment, iterate backwards to determine whether
// it seems to relate to a specific parameter. If so, break before that
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 7d550143be5df..913b94f3d4b9c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -14291,20 +14291,18 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
" CDDDP83848_RBR_REGISTER};",
NoBinPacking);
- // FIXME: The alignment of these trailing comments might be bad. Then again,
- // this might be utterly useless in real code.
verifyFormat("Constructor::Constructor()\n"
- " : some_value{ //\n"
- " aaaaaaa, //\n"
- " bbbbbbb} {}");
+ " : some_value{ //\n"
+ " aaaaaaa, //\n"
+ " bbbbbbb} {}");
// In braced lists, the first comment is always assumed to belong to the
// first element. Thus, it can be moved to the next or previous line as
// appropriate.
- verifyFormat("function({// First element:\n"
- " 1,\n"
- " // Second element:\n"
- " 2});",
+ verifyFormat("function({ // First element:\n"
+ " 1,\n"
+ " // Second element:\n"
+ " 2});",
"function({\n"
" // First element:\n"
" 1,\n"
@@ -14426,7 +14424,7 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
verifyFormat("vector< int > x{ // comment 1\n"
- " 1, 2, 3, 4 };",
+ " 1, 2, 3, 4 };",
SpaceBetweenBraces);
SpaceBetweenBraces.ColumnLimit = 20;
verifyFormat("vector< int > x{\n"
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index 69026bce98705..bef4c31b2fc8e 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -1474,12 +1474,12 @@ TEST_F(FormatTestComments, CommentsInStaticInitializers) {
verifyFormat("S s = {{a, b, c}, // Group #1\n"
" {d, e, f}, // Group #2\n"
" {g, h, i}}; // Group #3");
- verifyFormat("S s = {{// Group #1\n"
- " a, b, c},\n"
- " {// Group #2\n"
- " d, e, f},\n"
- " {// Group #3\n"
- " g, h, i}};");
+ verifyFormat("S s = {{ // Group #1\n"
+ " a, b, c},\n"
+ " { // Group #2\n"
+ " d, e, f},\n"
+ " { // Group #3\n"
+ " g, h, i}};");
EXPECT_EQ("S s = {\n"
" // Some comment\n"
@@ -4699,6 +4699,55 @@ TEST_F(FormatTestComments, SplitCommentIntroducers) {
getLLVMStyleWithColumns(10)));
}
+TEST_F(FormatTestComments, LineCommentsOnStartOfFunctionCall) {
+ auto Style = getLLVMStyle();
+
+ EXPECT_TRUE(Style.Cpp11BracedListStyle);
+
+ verifyFormat("T foo( // Comment\n"
+ " arg);",
+ Style);
+
+ verifyFormat("T bar{ // Comment\n"
+ " arg};",
+ Style);
+
+ verifyFormat("T baz({ // Comment\n"
+ " arg});",
+ Style);
+
+ verifyFormat("func( // Comment\n"
+ " arg);",
+ Style);
+
+ verifyFormat("func({ // Comment\n"
+ " arg});",
+ Style);
+
+ Style.Cpp11BracedListStyle = false;
+
+ verifyFormat("T foo( // Comment\n"
+ " arg);",
+ Style);
+
+ verifyFormat("T bar{ // Comment\n"
+ " arg\n"
+ "};",
+ Style);
+
+ verifyFormat("T baz({ // Comment\n"
+ " arg });",
+ Style);
+
+ verifyFormat("func( // Comment\n"
+ " arg);",
+ Style);
+
+ verifyFormat("func({ // Comment\n"
+ " arg });",
+ Style);
+}
+
} // end namespace
} // namespace test
} // end namespace format
More information about the cfe-commits
mailing list