Skip to content

Commit 86ef9d3

Browse files
committed
Revert "[clang-format] Handle variable declarations in BreakAfterAttributes (#71755)"
This reverts commit f7bbb58 which caused another formatting failure in polly.
1 parent 3153aa4 commit 86ef9d3

File tree

4 files changed

+29
-69
lines changed

4 files changed

+29
-69
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,8 +2049,8 @@ the configuration (without a prefix: ``Auto``).
20492049
.. _BreakAfterAttributes:
20502050

20512051
**BreakAfterAttributes** (``AttributeBreakingStyle``) :versionbadge:`clang-format 16` :ref:`<BreakAfterAttributes>`
2052-
Break after a group of C++11 attributes before a variable/function
2053-
(including constructor/destructor) declaration/definition name.
2052+
Break after a group of C++11 attributes before a function
2053+
declaration/definition name.
20542054

20552055
Possible values:
20562056

@@ -2059,10 +2059,6 @@ the configuration (without a prefix: ``Auto``).
20592059

20602060
.. code-block:: c++
20612061

2062-
[[maybe_unused]]
2063-
const int i;
2064-
[[gnu::const]] [[maybe_unused]]
2065-
int j;
20662062
[[nodiscard]]
20672063
inline int f();
20682064
[[gnu::const]] [[nodiscard]]
@@ -2073,9 +2069,6 @@ the configuration (without a prefix: ``Auto``).
20732069

20742070
.. code-block:: c++
20752071

2076-
[[maybe_unused]] const int i;
2077-
[[gnu::const]] [[maybe_unused]]
2078-
int j;
20792072
[[nodiscard]] inline int f();
20802073
[[gnu::const]] [[nodiscard]]
20812074
int g();
@@ -2085,8 +2078,6 @@ the configuration (without a prefix: ``Auto``).
20852078

20862079
.. code-block:: c++
20872080

2088-
[[maybe_unused]] const int i;
2089-
[[gnu::const]] [[maybe_unused]] int j;
20902081
[[nodiscard]] inline int f();
20912082
[[gnu::const]] [[nodiscard]] int g();
20922083

clang/include/clang/Format/Format.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,10 +1428,6 @@ struct FormatStyle {
14281428
enum AttributeBreakingStyle : int8_t {
14291429
/// Always break after attributes.
14301430
/// \code
1431-
/// [[maybe_unused]]
1432-
/// const int i;
1433-
/// [[gnu::const]] [[maybe_unused]]
1434-
/// int j;
14351431
/// [[nodiscard]]
14361432
/// inline int f();
14371433
/// [[gnu::const]] [[nodiscard]]
@@ -1440,26 +1436,21 @@ struct FormatStyle {
14401436
ABS_Always,
14411437
/// Leave the line breaking after attributes as is.
14421438
/// \code
1443-
/// [[maybe_unused]] const int i;
1444-
/// [[gnu::const]] [[maybe_unused]]
1445-
/// int j;
14461439
/// [[nodiscard]] inline int f();
14471440
/// [[gnu::const]] [[nodiscard]]
14481441
/// int g();
14491442
/// \endcode
14501443
ABS_Leave,
14511444
/// Never break after attributes.
14521445
/// \code
1453-
/// [[maybe_unused]] const int i;
1454-
/// [[gnu::const]] [[maybe_unused]] int j;
14551446
/// [[nodiscard]] inline int f();
14561447
/// [[gnu::const]] [[nodiscard]] int g();
14571448
/// \endcode
14581449
ABS_Never,
14591450
};
14601451

1461-
/// Break after a group of C++11 attributes before a variable/function
1462-
/// (including constructor/destructor) declaration/definition name.
1452+
/// Break after a group of C++11 attributes before a function
1453+
/// declaration/definition name.
14631454
/// \version 16
14641455
AttributeBreakingStyle BreakAfterAttributes;
14651456

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,10 +1997,6 @@ class AnnotatingParser {
19971997
(!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
19981998
Contexts.back().FirstStartOfName = &Current;
19991999
Current.setType(TT_StartOfName);
2000-
if (auto *PrevNonComment = Current.getPreviousNonComment();
2001-
PrevNonComment && PrevNonComment->is(TT_StartOfName)) {
2002-
PrevNonComment->setType(TT_Unknown);
2003-
}
20042000
} else if (Current.is(tok::semi)) {
20052001
// Reset FirstStartOfName after finding a semicolon so that a for loop
20062002
// with multiple increment statements is not confused with a for loop
@@ -2185,10 +2181,8 @@ class AnnotatingParser {
21852181
if (Style.isVerilog())
21862182
return false;
21872183

2188-
if (Tok.isNot(tok::identifier) || !Tok.Previous || !Tok.Next ||
2189-
Tok.Next->isPointerOrReference()) {
2184+
if (Tok.isNot(tok::identifier) || !Tok.Previous)
21902185
return false;
2191-
}
21922186

21932187
if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
21942188
Keywords.kw_as)) {
@@ -3261,7 +3255,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
32613255
if (Current.is(TT_FunctionDeclarationName))
32623256
return true;
32633257

3264-
if (!Current.Tok.getIdentifierInfo() || Current.is(TT_CtorDtorDeclName))
3258+
if (!Current.Tok.getIdentifierInfo())
32653259
return false;
32663260

32673261
auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
@@ -3444,30 +3438,29 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
34443438
if (AlignArrayOfStructures)
34453439
calculateArrayInitializerColumnList(Line);
34463440

3447-
const bool IsCpp = Style.isCpp();
34483441
bool LineIsFunctionDeclaration = false;
34493442
FormatToken *ClosingParen = nullptr;
34503443
for (FormatToken *Tok = Current, *AfterLastAttribute = nullptr; Tok;
34513444
Tok = Tok->Next) {
34523445
if (Tok->Previous->EndsCppAttributeGroup)
34533446
AfterLastAttribute = Tok;
3454-
if (isFunctionDeclarationName(IsCpp, *Tok, Line, ClosingParen)) {
3455-
LineIsFunctionDeclaration = true;
3456-
Tok->setFinalizedType(TT_FunctionDeclarationName);
3457-
}
3458-
if (LineIsFunctionDeclaration ||
3459-
Tok->isOneOf(TT_CtorDtorDeclName, TT_StartOfName)) {
3460-
if (IsCpp && AfterLastAttribute &&
3447+
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3448+
IsCtorOrDtor ||
3449+
isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
3450+
if (!IsCtorOrDtor) {
3451+
LineIsFunctionDeclaration = true;
3452+
Tok->setFinalizedType(TT_FunctionDeclarationName);
3453+
}
3454+
if (AfterLastAttribute &&
34613455
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
34623456
AfterLastAttribute->MustBreakBefore = true;
3463-
if (LineIsFunctionDeclaration)
3464-
Line.ReturnTypeWrapped = true;
3457+
Line.ReturnTypeWrapped = true;
34653458
}
34663459
break;
34673460
}
34683461
}
34693462

3470-
if (IsCpp) {
3463+
if (Style.isCpp()) {
34713464
if (!LineIsFunctionDeclaration) {
34723465
// Annotate */&/&& in `operator` function calls as binary operators.
34733466
for (const auto *Tok = Line.First; Tok; Tok = Tok->Next) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8479,25 +8479,18 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
84798479
" aaaaaaaaaaaaaaaaaaaaaaaaa));");
84808480
verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
84818481
" __attribute__((unused));");
8482-
8483-
Style = getGoogleStyle();
8484-
Style.AttributeMacros.push_back("GUARDED_BY");
8485-
verifyFormat(
8482+
verifyGoogleFormat(
84868483
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8487-
" GUARDED_BY(aaaaaaaaaaaa);",
8488-
Style);
8489-
verifyFormat(
8484+
" GUARDED_BY(aaaaaaaaaaaa);");
8485+
verifyGoogleFormat(
84908486
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8491-
" GUARDED_BY(aaaaaaaaaaaa);",
8492-
Style);
8493-
verifyFormat(
8487+
" GUARDED_BY(aaaaaaaaaaaa);");
8488+
verifyGoogleFormat(
84948489
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8495-
" aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8496-
Style);
8497-
verifyFormat(
8490+
" aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8491+
verifyGoogleFormat(
84988492
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8499-
" aaaaaaaaaaaaaaaaaaaaaaaaa;",
8500-
Style);
8493+
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
85018494
}
85028495

85038496
TEST_F(FormatTest, FunctionAnnotations) {
@@ -26199,10 +26192,9 @@ TEST_F(FormatTest, RemoveSemicolon) {
2619926192
}
2620026193

2620126194
TEST_F(FormatTest, BreakAfterAttributes) {
26202-
constexpr StringRef Code("[[maybe_unused]] const int i;\n"
26203-
"[[foo([[]])]] [[maybe_unused]]\n"
26204-
"int j;\n"
26205-
"[[nodiscard]] inline int f(int &i);\n"
26195+
FormatStyle Style = getLLVMStyle();
26196+
26197+
constexpr StringRef Code("[[nodiscard]] inline int f(int &i);\n"
2620626198
"[[foo([[]])]] [[nodiscard]]\n"
2620726199
"int g(int &i);\n"
2620826200
"[[nodiscard]]\n"
@@ -26215,14 +26207,11 @@ TEST_F(FormatTest, BreakAfterAttributes) {
2621526207
" return 1;\n"
2621626208
"}");
2621726209

26218-
FormatStyle Style = getLLVMStyle();
2621926210
EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave);
2622026211
verifyNoChange(Code, Style);
2622126212

2622226213
Style.BreakAfterAttributes = FormatStyle::ABS_Never;
26223-
verifyFormat("[[maybe_unused]] const int i;\n"
26224-
"[[foo([[]])]] [[maybe_unused]] int j;\n"
26225-
"[[nodiscard]] inline int f(int &i);\n"
26214+
verifyFormat("[[nodiscard]] inline int f(int &i);\n"
2622626215
"[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
2622726216
"[[nodiscard]] inline int f(int &i) {\n"
2622826217
" i = 1;\n"
@@ -26235,11 +26224,7 @@ TEST_F(FormatTest, BreakAfterAttributes) {
2623526224
Code, Style);
2623626225

2623726226
Style.BreakAfterAttributes = FormatStyle::ABS_Always;
26238-
verifyFormat("[[maybe_unused]]\n"
26239-
"const int i;\n"
26240-
"[[foo([[]])]] [[maybe_unused]]\n"
26241-
"int j;\n"
26242-
"[[nodiscard]]\n"
26227+
verifyFormat("[[nodiscard]]\n"
2624326228
"inline int f(int &i);\n"
2624426229
"[[foo([[]])]] [[nodiscard]]\n"
2624526230
"int g(int &i);\n"

0 commit comments

Comments
 (0)