[PATCH] D153745: [clang-format] Fix bugs in annotating r_paren as C-style cast
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 26 00:53:13 PDT 2023
owenpan created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, MyDeveloperDay.
owenpan requested review of this revision.
Don't annotate r_paren as TT_CastRParen if it's in a macro definition or followed by an amp/star and a numeric_constant.
Fixes https://github.com/llvm/llvm-project/issues/59634.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D153745
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -554,6 +554,17 @@
Tokens = annotate("throw (Foo)p;");
EXPECT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_CastRParen);
+
+ Tokens = annotate("#define FOO(x) (((uint64_t)(x) * BAR) / 100)");
+ EXPECT_EQ(Tokens.size(), 21u) << Tokens;
+ EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_CastRParen);
+ EXPECT_TOKEN(Tokens[13], tok::r_paren, TT_Unknown);
+ EXPECT_TOKEN(Tokens[14], tok::star, TT_BinaryOperator);
+
+ Tokens = annotate("return (Foo) & 10;");
+ EXPECT_EQ(Tokens.size(), 8u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_Unknown);
+ EXPECT_TOKEN(Tokens[4], tok::amp, TT_BinaryOperator);
}
TEST_F(TokenAnnotatorTest, UnderstandsDynamicExceptionSpecifier) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2428,12 +2428,16 @@
// If the next token after the parenthesis is a unary operator, assume
// that this is cast, unless there are unexpected tokens inside the
// parenthesis.
- bool NextIsUnary =
- Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star);
- if (!NextIsUnary || Tok.Next->is(tok::plus) ||
+ const bool NextIsAmpOrStar = Tok.Next->isOneOf(tok::amp, tok::star);
+ if (!(Tok.Next->isUnaryOperator() || NextIsAmpOrStar) ||
+ Tok.Next->is(tok::plus) ||
!Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
return false;
}
+ if (NextIsAmpOrStar &&
+ (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
+ return false;
+ }
// Search for unexpected tokens.
for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
Prev = Prev->Previous) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153745.534450.patch
Type: text/x-patch
Size: 2038 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230626/05bf5e62/attachment.bin>
More information about the cfe-commits
mailing list