Skip to content

Commit d3be296

Browse files
authored
[clang-format] Correctly annotate pointer/reference in _Generic (#133673)
Fix #133663
1 parent bae3577 commit d3be296

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,10 @@ class AnnotatingParser {
14171417
}
14181418
} else if (Contexts.back().ContextType == Context::C11GenericSelection) {
14191419
Tok->setType(TT_GenericSelectionColon);
1420+
auto *Prev = Tok->getPreviousNonComment();
1421+
assert(Prev);
1422+
if (Prev->isPointerOrReference())
1423+
Prev->setFinalizedType(TT_PointerOrReference);
14201424
} else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
14211425
Tok->setType(TT_BitFieldColon);
14221426
} else if (Contexts.size() == 1 &&

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
363363
ASSERT_EQ(Tokens.size(), 20u) << Tokens;
364364
EXPECT_TOKEN(Tokens[14], tok::star, TT_PointerOrReference);
365365

366+
Tokens = annotate("#define foo(x) _Generic(x, bar *: 1, default: 0)");
367+
ASSERT_EQ(Tokens.size(), 20u) << Tokens;
368+
EXPECT_TOKEN(Tokens[11], tok::star, TT_PointerOrReference);
369+
366370
Tokens = annotate("Thingy kConfig = {\n"
367371
" 1,\n"
368372
" (uint16_t)(kScale * height_pixels),\n"

0 commit comments

Comments
 (0)