Skip to content

Commit 4d18ce1

Browse files
authored
[clang-format] Handle C-style cast of qualified type (#108929)
Fixes #102874.
1 parent a8dd8f6 commit 4d18ce1

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,11 +2840,14 @@ class AnnotatingParser {
28402840
if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
28412841
return true;
28422842

2843-
// Look for a cast `( x ) (`.
2844-
if (AfterRParen->is(tok::l_paren) && BeforeRParen->Previous) {
2845-
if (BeforeRParen->is(tok::identifier) &&
2846-
BeforeRParen->Previous->is(tok::l_paren)) {
2847-
return true;
2843+
// Look for a cast `( x ) (`, where x may be a qualified identifier.
2844+
if (AfterRParen->is(tok::l_paren)) {
2845+
for (const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
2846+
Prev = Prev->Previous;
2847+
if (Prev->is(tok::coloncolon))
2848+
Prev = Prev->Previous;
2849+
if (Prev == LParen)
2850+
return true;
28482851
}
28492852
}
28502853

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
781781
EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_Unknown);
782782
EXPECT_TOKEN(Tokens[10], tok::minus, TT_BinaryOperator);
783783

784+
Tokens = annotate("return (::Type)(1 + 2);");
785+
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
786+
EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_CastRParen);
787+
788+
Tokens = annotate("return (Namespace::Class)(1 + 2);");
789+
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
790+
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen);
791+
784792
auto Style = getLLVMStyle();
785793
Style.TypeNames.push_back("Foo");
786794
Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);

0 commit comments

Comments
 (0)