Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 275e5b4

Browse files
owencac-rhodes
authored andcommitted
[clang-format] Don't annotate C compound literal r_paren (llvm#180436)
Fixes llvm#180179 (cherry picked from commit 2302110)
1 parent 8530a2c commit 275e5b4

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2921,7 +2921,9 @@ class AnnotatingParser {
29212921
if (!AfterRParen->Next)
29222922
return false;
29232923

2924-
if (AfterRParen->is(tok::l_brace) &&
2924+
// A pair of parentheses before an l_brace in C starts a compound literal
2925+
// and is not a cast.
2926+
if (Style.Language != FormatStyle::LK_C && AfterRParen->is(tok::l_brace) &&
29252927
AfterRParen->getBlockKind() == BK_BracedInit) {
29262928
return true;
29272929
}

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
964964
EXPECT_TOKEN(Tokens[11], tok::amp, TT_UnaryOperator);
965965
}
966966

967+
TEST_F(TokenAnnotatorTest, CompoundLiteral) {
968+
auto Tokens =
969+
annotate("return (struct foo){};", getLLVMStyle(FormatStyle::LK_C));
970+
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
971+
// Not TT_CastRParen.
972+
EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_Unknown);
973+
}
974+
967975
TEST_F(TokenAnnotatorTest, UnderstandsDynamicExceptionSpecifier) {
968976
auto Tokens = annotate("void f() throw(int);");
969977
ASSERT_EQ(Tokens.size(), 10u) << Tokens;

0 commit comments

Comments
 (0)