Skip to content

Commit

Permalink
[clang-format] Correctly annotate */& in if condition with braced init (
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca authored Oct 3, 2024
1 parent 6ae14c0 commit 98281da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
25 changes: 16 additions & 9 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,19 +1167,26 @@ class AnnotatingParser {

ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
Contexts.back().ColonIsDictLiteral = true;
if (OpeningBrace.is(BK_BracedInit))

const auto *Prev = OpeningBrace.getPreviousNonComment();

if (OpeningBrace.is(BK_BracedInit)) {
Contexts.back().IsExpression = true;
if (Style.isJavaScript() && OpeningBrace.Previous &&
OpeningBrace.Previous->is(TT_JsTypeColon)) {
Contexts.back().IsExpression = false;
}
if (Style.isVerilog() &&
(!OpeningBrace.getPreviousNonComment() ||
OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
Contexts.back().VerilogMayBeConcatenation = true;
if (Prev) {
for (auto *Tok = Prev->Previous; Tok && Tok->isPointerOrReference();
Tok = Tok->Previous) {
Tok->setFinalizedType(TT_PointerOrReference);
}
}
}

if (Style.isJavaScript() && Prev && Prev->is(TT_JsTypeColon))
Contexts.back().IsExpression = false;

if (Style.isTableGen())
Contexts.back().ColonIsDictLiteral = false;
else if (Style.isVerilog() && !(Prev && Prev->is(Keywords.kw_apostrophe)))
Contexts.back().VerilogMayBeConcatenation = true;

unsigned CommaCount = 0;
while (CurrentToken) {
Expand Down
5 changes: 5 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);

Tokens = annotate("if (Foo *&foo{a})");
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);

FormatStyle Style = getLLVMStyle();
Style.TypeNames.push_back("MYI");
Tokens = annotate("if (MYI *p{nullptr})", Style);
Expand Down

0 comments on commit 98281da

Please sign in to comment.