[clang] [clang-format] Handle C-style cast of qualified type (PR #108929)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 16 21:34:44 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->102874.
---
Full diff: https://github.com/llvm/llvm-project/pull/108929.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+8-5)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+8)
``````````diff
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index dfa703aed0d34d..eabb6e68d75b09 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2840,11 +2840,14 @@ class AnnotatingParser {
if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
return true;
- // Look for a cast `( x ) (`.
- if (AfterRParen->is(tok::l_paren) && BeforeRParen->Previous) {
- if (BeforeRParen->is(tok::identifier) &&
- BeforeRParen->Previous->is(tok::l_paren)) {
- return true;
+ // Look for a cast `( x ) (`, where x may be a qualified identifier.
+ if (AfterRParen->is(tok::l_paren)) {
+ for (const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) {
+ Prev = Prev->Previous;
+ if (Prev->is(tok::coloncolon))
+ Prev = Prev->Previous;
+ if (Prev == LParen)
+ return true;
}
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 5c28e3a4ea5a1f..f97b47be772e62 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -781,6 +781,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_Unknown);
EXPECT_TOKEN(Tokens[10], tok::minus, TT_BinaryOperator);
+ Tokens = annotate("return (::Type)(1 + 2);");
+ ASSERT_EQ(Tokens.size(), 12u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_CastRParen);
+
+ Tokens = annotate("return (Namespace::Class)(1 + 2);");
+ ASSERT_EQ(Tokens.size(), 13u) << Tokens;
+ EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen);
+
auto Style = getLLVMStyle();
Style.TypeNames.push_back("Foo");
Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);
``````````
</details>
https://github.com/llvm/llvm-project/pull/108929
More information about the cfe-commits
mailing list