Skip to content

Commit 5b81158

Browse files
committed
Revert "[clang-format] Handle attributes before case label."
This reverts commit 596fa2d.
1 parent 438ac28 commit 5b81158

File tree

3 files changed

+12
-81
lines changed

3 files changed

+12
-81
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,6 @@ bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace,
480480
unsigned StatementCount = 0;
481481
bool SwitchLabelEncountered = false;
482482
do {
483-
if (FormatTok->getType() == TT_AttributeMacro) {
484-
nextToken();
485-
continue;
486-
}
487483
tok::TokenKind kind = FormatTok->Tok.getKind();
488484
if (FormatTok->getType() == TT_MacroBlockBegin)
489485
kind = tok::l_brace;
@@ -573,8 +569,6 @@ bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace,
573569
parseCSharpAttribute();
574570
break;
575571
}
576-
if (handleCppAttributes())
577-
break;
578572
LLVM_FALLTHROUGH;
579573
default:
580574
ParseDefault();
@@ -1403,11 +1397,9 @@ void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
14031397
// e.g. "default void f() {}" in a Java interface.
14041398
break;
14051399
case tok::kw_case:
1406-
if (Style.isJavaScript() && Line->MustBeDeclaration) {
1400+
if (Style.isJavaScript() && Line->MustBeDeclaration)
14071401
// 'case: string' field declaration.
1408-
nextToken();
14091402
break;
1410-
}
14111403
parseCaseLabel();
14121404
return;
14131405
case tok::kw_try:
@@ -1828,12 +1820,6 @@ void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
18281820
case tok::kw_new:
18291821
parseNew();
18301822
break;
1831-
case tok::kw_case:
1832-
if (Style.isJavaScript() && Line->MustBeDeclaration)
1833-
// 'case: string' field declaration.
1834-
break;
1835-
parseCaseLabel();
1836-
break;
18371823
default:
18381824
nextToken();
18391825
break;
@@ -2402,24 +2388,17 @@ static void markOptionalBraces(FormatToken *LeftBrace) {
24022388
RightBrace->Optional = true;
24032389
}
24042390

2405-
void UnwrappedLineParser::handleAttributes() {
2406-
// Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
2407-
if (FormatTok->is(TT_AttributeMacro))
2408-
nextToken();
2409-
handleCppAttributes();
2410-
}
2411-
2412-
bool UnwrappedLineParser::handleCppAttributes() {
2413-
// Handle [[likely]] / [[unlikely]] attributes.
2414-
if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute()) {
2415-
parseSquare();
2416-
return true;
2417-
}
2418-
return false;
2419-
}
2420-
24212391
FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
24222392
bool KeepBraces) {
2393+
auto HandleAttributes = [this]() {
2394+
// Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
2395+
if (FormatTok->is(TT_AttributeMacro))
2396+
nextToken();
2397+
// Handle [[likely]] / [[unlikely]] attributes.
2398+
if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute())
2399+
parseSquare();
2400+
};
2401+
24232402
assert(FormatTok->is(tok::kw_if) && "'if' expected");
24242403
nextToken();
24252404
if (FormatTok->is(tok::exclaim))
@@ -2432,7 +2411,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
24322411
if (FormatTok->is(tok::l_paren))
24332412
parseParens();
24342413
}
2435-
handleAttributes();
2414+
HandleAttributes();
24362415

24372416
bool NeedsUnwrappedLine = false;
24382417
keepAncestorBraces();
@@ -2469,7 +2448,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
24692448
Kind = IfStmtKind::IfElse;
24702449
}
24712450
nextToken();
2472-
handleAttributes();
2451+
HandleAttributes();
24732452
if (FormatTok->is(tok::l_brace)) {
24742453
ElseLeftBrace = FormatTok;
24752454
CompoundStatementIndenter Indenter(this, Style, Line->Level);

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ class UnwrappedLineParser {
121121
void parseSquare(bool LambdaIntroducer = false);
122122
void keepAncestorBraces();
123123
void parseUnbracedBody(bool CheckEOF = false);
124-
void handleAttributes();
125-
bool handleCppAttributes();
126124
FormatToken *parseIfThenElse(IfStmtKind *IfKind, bool KeepBraces = false);
127125
void parseTryCatch();
128126
void parseForOrWhileLoop();

clang/unittests/Format/FormatTest.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2609,52 +2609,6 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
26092609
"}",
26102610
getLLVMStyleWithColumns(34));
26112611

2612-
verifyFormat("switch (a) {\n"
2613-
"[[likely]] case 1:\n"
2614-
" return;\n"
2615-
"}");
2616-
verifyFormat("switch (a) {\n"
2617-
"[[likely]] [[other::likely]] case 1:\n"
2618-
" return;\n"
2619-
"}");
2620-
verifyFormat("switch (x) {\n"
2621-
"case 1:\n"
2622-
" return;\n"
2623-
"[[likely]] case 2:\n"
2624-
" return;\n"
2625-
"}");
2626-
verifyFormat("switch (a) {\n"
2627-
"case 1:\n"
2628-
"[[likely]] case 2:\n"
2629-
" return;\n"
2630-
"}");
2631-
FormatStyle Attributes = getLLVMStyle();
2632-
Attributes.AttributeMacros.push_back("LIKELY");
2633-
Attributes.AttributeMacros.push_back("OTHER_LIKELY");
2634-
verifyFormat("switch (a) {\n"
2635-
"LIKELY case b:\n"
2636-
" return;\n"
2637-
"}",
2638-
Attributes);
2639-
verifyFormat("switch (a) {\n"
2640-
"LIKELY OTHER_LIKELY() case b:\n"
2641-
" return;\n"
2642-
"}",
2643-
Attributes);
2644-
verifyFormat("switch (a) {\n"
2645-
"case 1:\n"
2646-
" return;\n"
2647-
"LIKELY case 2:\n"
2648-
" return;\n"
2649-
"}",
2650-
Attributes);
2651-
verifyFormat("switch (a) {\n"
2652-
"case 1:\n"
2653-
"LIKELY case 2:\n"
2654-
" return;\n"
2655-
"}",
2656-
Attributes);
2657-
26582612
FormatStyle Style = getLLVMStyle();
26592613
Style.IndentCaseLabels = true;
26602614
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;

0 commit comments

Comments
 (0)