Skip to content

Commit

Permalink
[clang-format] Handle goto labels preceded by C++11 attributes
Browse files Browse the repository at this point in the history
Fixes #64229.

Differential Revision: https://reviews.llvm.org/D156655
  • Loading branch information
owenca committed Aug 3, 2023
1 parent 56a31b8 commit 70d7ea0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
26 changes: 15 additions & 11 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,10 @@ void UnwrappedLineParser::parseStructuralElement(
return;
}

if (Style.isVerilog()) {
if (Style.isCpp()) {
while (FormatTok->is(tok::l_square) && handleCppAttributes()) {
}
} else if (Style.isVerilog()) {
if (Keywords.isVerilogStructuredProcedure(*FormatTok)) {
parseForOrWhileLoop(/*HasParens=*/false);
return;
Expand Down Expand Up @@ -1638,6 +1641,17 @@ void UnwrappedLineParser::parseStructuralElement(
parseNamespace();
return;
}
// In Verilog labels can be any expression, so we don't do them here.
if (!Style.isVerilog() && Tokens->peekNextToken()->is(tok::colon) &&
!Line->MustBeDeclaration) {
nextToken();
Line->Tokens.begin()->Tok->MustBreakBefore = true;
FormatTok->setFinalizedType(TT_GotoLabelColon);
parseLabel(!Style.IndentGotoLabels);
if (HasLabel)
*HasLabel = true;
return;
}
// In all other cases, parse the declaration.
break;
default:
Expand Down Expand Up @@ -1942,16 +1956,6 @@ void UnwrappedLineParser::parseStructuralElement(
return I != E && (++I == E);
};
if (OneTokenSoFar()) {
// In Verilog labels can be any expression, so we don't do them here.
if (!Style.isVerilog() && FormatTok->is(tok::colon) &&
!Line->MustBeDeclaration) {
Line->Tokens.begin()->Tok->MustBreakBefore = true;
FormatTok->setFinalizedType(TT_GotoLabelColon);
parseLabel(!Style.IndentGotoLabels);
if (HasLabel)
*HasLabel = true;
return;
}
// Recognize function-like macro usages without trailing semicolon as
// well as free-standing macros like Q_OBJECT.
bool FunctionLike = FormatTok->is(tok::l_paren);
Expand Down
40 changes: 39 additions & 1 deletion clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3023,6 +3023,26 @@ TEST_F(FormatTest, FormatsLabels) {
" some_other_code();\n"
"}\n"
"}");
verifyFormat("{\n"
"L0:\n"
"[[foo]] L1:\n"
"[[bar]] [[baz]] L2:\n"
" g();\n"
"}");
verifyFormat("{\n"
"[[foo]] L1: {\n"
"[[bar]] [[baz]] L2:\n"
" g();\n"
"}\n"
"}");
verifyFormat("{\n"
"[[foo]] L1:\n"
" f();\n"
" {\n"
" [[bar]] [[baz]] L2:\n"
" g();\n"
" }\n"
"}");
FormatStyle Style = getLLVMStyle();
Style.IndentGotoLabels = false;
verifyFormat("void f() {\n"
Expand All @@ -3046,12 +3066,22 @@ TEST_F(FormatTest, FormatsLabels) {
" some_code();\n"
"test_label:;\n"
" int i = 0;\n"
"}");
"}",
Style);
verifyFormat("{\n"
" some_code();\n"
"test_label: { some_other_code(); }\n"
"}",
Style);
verifyFormat("{\n"
"[[foo]] L1:\n"
" f();\n"
" {\n"
"[[bar]] [[baz]] L2:\n"
" g();\n"
" }\n"
"}",
Style);
// The opening brace may either be on the same unwrapped line as the colon or
// on a separate one. The formatter should recognize both.
Style = getLLVMStyle();
Expand All @@ -3064,6 +3094,14 @@ TEST_F(FormatTest, FormatsLabels) {
"}\n"
"}",
Style);
verifyFormat("{\n"
"[[foo]] L1:\n"
"{\n"
"[[bar]] [[baz]] L2:\n"
" g();\n"
"}\n"
"}",
Style);
}

TEST_F(FormatTest, MultiLineControlStatements) {
Expand Down

0 comments on commit 70d7ea0

Please sign in to comment.