Skip to content

Commit dae44c8

Browse files
owencatstellar
authored andcommitted
[clang-format] Don't annotate left brace of struct as FunctionLBrace
Related to a02c3af. Fixes #61700. Differential Revision: https://reviews.llvm.org/D146895 (cherry picked from commit 767aee1)
1 parent 86b0c6e commit dae44c8

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,16 +2739,17 @@ void UnwrappedLineParser::handleAttributes() {
27392739
// Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
27402740
if (FormatTok->is(TT_AttributeMacro))
27412741
nextToken();
2742-
handleCppAttributes();
2742+
if (FormatTok->is(tok::l_square))
2743+
handleCppAttributes();
27432744
}
27442745

27452746
bool UnwrappedLineParser::handleCppAttributes() {
27462747
// Handle [[likely]] / [[unlikely]] attributes.
2747-
if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute()) {
2748-
parseSquare();
2749-
return true;
2750-
}
2751-
return false;
2748+
assert(FormatTok->is(tok::l_square));
2749+
if (!tryToParseSimpleAttribute())
2750+
return false;
2751+
parseSquare();
2752+
return true;
27522753
}
27532754

27542755
/// Returns whether \c Tok begins a block.
@@ -3849,13 +3850,13 @@ void UnwrappedLineParser::parseJavaEnumBody() {
38493850
void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
38503851
const FormatToken &InitialToken = *FormatTok;
38513852
nextToken();
3852-
handleAttributes();
38533853

38543854
// The actual identifier can be a nested name specifier, and in macros
38553855
// it is often token-pasted.
3856+
// An [[attribute]] can be before the identifier.
38563857
while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
38573858
tok::kw___attribute, tok::kw___declspec,
3858-
tok::kw_alignas) ||
3859+
tok::kw_alignas, tok::l_square) ||
38593860
((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
38603861
FormatTok->isOneOf(tok::period, tok::comma))) {
38613862
if (Style.isJavaScript() &&
@@ -3869,16 +3870,15 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
38693870
continue;
38703871
}
38713872
}
3873+
if (FormatTok->is(tok::l_square) && handleCppAttributes())
3874+
continue;
38723875
bool IsNonMacroIdentifier =
38733876
FormatTok->is(tok::identifier) &&
38743877
FormatTok->TokenText != FormatTok->TokenText.upper();
38753878
nextToken();
38763879
// We can have macros in between 'class' and the class name.
3877-
if (!IsNonMacroIdentifier) {
3878-
if (FormatTok->is(tok::l_paren)) {
3879-
parseParens();
3880-
}
3881-
}
3880+
if (!IsNonMacroIdentifier && FormatTok->is(tok::l_paren))
3881+
parseParens();
38823882
}
38833883

38843884
// Note that parsing away template declarations here leads to incorrectly

clang/unittests/Format/FormatTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25212,6 +25212,11 @@ TEST_F(FormatTest, RemoveSemicolon) {
2521225212
"};",
2521325213
Style);
2521425214

25215+
verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
25216+
" int i;\n"
25217+
"};",
25218+
Style);
25219+
2521525220
verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
2521625221

2521725222
// These tests are here to show a problem that may not be easily

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
288288
auto Tokens = annotate("struct S {};");
289289
EXPECT_EQ(Tokens.size(), 6u) << Tokens;
290290
EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_StructLBrace);
291+
292+
Tokens = annotate("struct EXPORT_MACRO [[nodiscard]] C { int i; };");
293+
EXPECT_EQ(Tokens.size(), 15u) << Tokens;
294+
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
295+
296+
Tokens = annotate("struct [[deprecated]] [[nodiscard]] C { int i; };");
297+
EXPECT_EQ(Tokens.size(), 19u) << Tokens;
298+
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_StructLBrace);
291299
}
292300

293301
TEST_F(TokenAnnotatorTest, UnderstandsUnions) {

0 commit comments

Comments
 (0)