Skip to content

Commit a0710e1

Browse files
committed
Revert "[clang-format] Handle variable declarations in BreakAfterAttributes (#71755)"
This reverts commit 5c36f43 which caused a formatting error in polly.
1 parent 50a48d8 commit a0710e1

File tree

4 files changed

+28
-66
lines changed

4 files changed

+28
-66
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: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,10 +2000,6 @@ class AnnotatingParser {
20002000
(!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
20012001
Contexts.back().FirstStartOfName = &Current;
20022002
Current.setType(TT_StartOfName);
2003-
if (auto *PrevNonComment = Current.getPreviousNonComment();
2004-
PrevNonComment && PrevNonComment->is(TT_StartOfName)) {
2005-
PrevNonComment->setType(TT_Unknown);
2006-
}
20072003
} else if (Current.is(tok::semi)) {
20082004
// Reset FirstStartOfName after finding a semicolon so that a for loop
20092005
// with multiple increment statements is not confused with a for loop
@@ -3262,7 +3258,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
32623258
if (Current.is(TT_FunctionDeclarationName))
32633259
return true;
32643260

3265-
if (!Current.Tok.getIdentifierInfo() || Current.is(TT_CtorDtorDeclName))
3261+
if (!Current.Tok.getIdentifierInfo())
32663262
return false;
32673263

32683264
auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
@@ -3445,30 +3441,29 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
34453441
if (AlignArrayOfStructures)
34463442
calculateArrayInitializerColumnList(Line);
34473443

3448-
const bool IsCpp = Style.isCpp();
34493444
bool LineIsFunctionDeclaration = false;
34503445
FormatToken *ClosingParen = nullptr;
34513446
for (FormatToken *Tok = Current, *AfterLastAttribute = nullptr; Tok;
34523447
Tok = Tok->Next) {
34533448
if (Tok->Previous->EndsCppAttributeGroup)
34543449
AfterLastAttribute = Tok;
3455-
if (isFunctionDeclarationName(IsCpp, *Tok, Line, ClosingParen)) {
3456-
LineIsFunctionDeclaration = true;
3457-
Tok->setFinalizedType(TT_FunctionDeclarationName);
3458-
}
3459-
if (LineIsFunctionDeclaration ||
3460-
Tok->isOneOf(TT_CtorDtorDeclName, TT_StartOfName)) {
3461-
if (IsCpp && AfterLastAttribute &&
3450+
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3451+
IsCtorOrDtor ||
3452+
isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
3453+
if (!IsCtorOrDtor) {
3454+
LineIsFunctionDeclaration = true;
3455+
Tok->setFinalizedType(TT_FunctionDeclarationName);
3456+
}
3457+
if (AfterLastAttribute &&
34623458
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
34633459
AfterLastAttribute->MustBreakBefore = true;
3464-
if (LineIsFunctionDeclaration)
3465-
Line.ReturnTypeWrapped = true;
3460+
Line.ReturnTypeWrapped = true;
34663461
}
34673462
break;
34683463
}
34693464
}
34703465

3471-
if (IsCpp) {
3466+
if (Style.isCpp()) {
34723467
if (!LineIsFunctionDeclaration) {
34733468
// Annotate */&/&& in `operator` function calls as binary operators.
34743469
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)