Skip to content

Commit 4851886

Browse files
authored
[clang-format] Handle bit-field colon of non-numeric-constant size (llvm#142110)
Fix llvm#142050
1 parent 84fd907 commit 4851886

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,9 @@ class AnnotatingParser {
14111411
assert(Prev);
14121412
if (Prev->isPointerOrReference())
14131413
Prev->setFinalizedType(TT_PointerOrReference);
1414-
} else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1414+
} else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1415+
(Prev && Prev->is(TT_StartOfName) && !Scopes.empty() &&
1416+
Scopes.back() == ST_Class)) {
14151417
Tok->setType(TT_BitFieldColon);
14161418
} else if (Contexts.size() == 1 &&
14171419
!Line.getFirstNonComment()->isOneOf(tok::kw_enum, tok::kw_case,

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4076,6 +4076,14 @@ TEST_F(TokenAnnotatorTest, EnumColonInTypedef) {
40764076
EXPECT_TOKEN(Tokens[2], tok::colon, TT_Unknown); // Not TT_InheritanceColon.
40774077
}
40784078

4079+
TEST_F(TokenAnnotatorTest, BitFieldColon) {
4080+
auto Tokens = annotate("class C {\n"
4081+
" int f : SIZE;\n"
4082+
"};");
4083+
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
4084+
EXPECT_TOKEN(Tokens[5], tok::colon, TT_BitFieldColon);
4085+
}
4086+
40794087
} // namespace
40804088
} // namespace format
40814089
} // namespace clang

0 commit comments

Comments
 (0)