Skip to content

Commit 98281da

Browse files
authored
[clang-format] Correctly annotate */& in if condition with braced init (#109505)
Fixes #109371.
1 parent 6ae14c0 commit 98281da

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,19 +1167,26 @@ class AnnotatingParser {
11671167

11681168
ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
11691169
Contexts.back().ColonIsDictLiteral = true;
1170-
if (OpeningBrace.is(BK_BracedInit))
1170+
1171+
const auto *Prev = OpeningBrace.getPreviousNonComment();
1172+
1173+
if (OpeningBrace.is(BK_BracedInit)) {
11711174
Contexts.back().IsExpression = true;
1172-
if (Style.isJavaScript() && OpeningBrace.Previous &&
1173-
OpeningBrace.Previous->is(TT_JsTypeColon)) {
1174-
Contexts.back().IsExpression = false;
1175-
}
1176-
if (Style.isVerilog() &&
1177-
(!OpeningBrace.getPreviousNonComment() ||
1178-
OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1179-
Contexts.back().VerilogMayBeConcatenation = true;
1175+
if (Prev) {
1176+
for (auto *Tok = Prev->Previous; Tok && Tok->isPointerOrReference();
1177+
Tok = Tok->Previous) {
1178+
Tok->setFinalizedType(TT_PointerOrReference);
1179+
}
1180+
}
11801181
}
1182+
1183+
if (Style.isJavaScript() && Prev && Prev->is(TT_JsTypeColon))
1184+
Contexts.back().IsExpression = false;
1185+
11811186
if (Style.isTableGen())
11821187
Contexts.back().ColonIsDictLiteral = false;
1188+
else if (Style.isVerilog() && !(Prev && Prev->is(Keywords.kw_apostrophe)))
1189+
Contexts.back().VerilogMayBeConcatenation = true;
11831190

11841191
unsigned CommaCount = 0;
11851192
while (CurrentToken) {

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
308308
EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
309309
EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
310310

311+
Tokens = annotate("if (Foo *&foo{a})");
312+
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
313+
EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
314+
EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
315+
311316
FormatStyle Style = getLLVMStyle();
312317
Style.TypeNames.push_back("MYI");
313318
Tokens = annotate("if (MYI *p{nullptr})", Style);

0 commit comments

Comments
 (0)