-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format[NFC] Clean up AnnotatingParser::rParenEndsCast() #96128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFull diff: https://github.com/llvm/llvm-project/pull/96128.diff 1 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 87e3e712d6993..67c8f7f5c8bdd 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2661,18 +2661,27 @@ class AnnotatingParser {
/// Determine whether ')' is ending a cast.
bool rParenEndsCast(const FormatToken &Tok) {
+ assert(Tok.is(tok::r_paren));
+
+ if (!Tok.Previous || !Tok.MatchingParen)
+ return false;
+
// C-style casts are only used in C++, C# and Java.
- if (!Style.isCSharp() && !IsCpp && Style.Language != FormatStyle::LK_Java)
+ if (!IsCpp && !Style.isCSharp() && Style.Language != FormatStyle::LK_Java)
return false;
+ const auto *LParen = Tok.MatchingParen;
+ const auto *BeforeRParen = Tok.Previous;
+ const auto *AfterRParen = Tok.Next;
+
// Empty parens aren't casts and there are no casts at the end of the line.
- if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
+ if (BeforeRParen == LParen || !AfterRParen)
return false;
- if (Tok.MatchingParen->is(TT_OverloadedOperatorLParen))
+ if (LParen->is(TT_OverloadedOperatorLParen))
return false;
- FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
+ FormatToken *LeftOfParens = LParen->getPreviousNonComment();
if (LeftOfParens) {
// If there is a closing parenthesis left of the current
// parentheses, look past it as these might be chained casts.
@@ -2728,37 +2737,38 @@ class AnnotatingParser {
}
}
- if (Tok.Next->is(tok::question) ||
- (Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(LangOpts))) {
+ if (AfterRParen->is(tok::question) ||
+ (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
return false;
}
// `foreach((A a, B b) in someList)` should not be seen as a cast.
- if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
+ if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
return false;
// Functions which end with decorations like volatile, noexcept are unlikely
// to be casts.
- if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
- tok::kw_requires, tok::kw_throw, tok::arrow,
- Keywords.kw_override, Keywords.kw_final) ||
- isCppAttribute(IsCpp, *Tok.Next)) {
+ if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
+ tok::kw_requires, tok::kw_throw, tok::arrow,
+ Keywords.kw_override, Keywords.kw_final) ||
+ isCppAttribute(IsCpp, *AfterRParen)) {
return false;
}
// As Java has no function types, a "(" after the ")" likely means that this
// is a cast.
- if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
+ if (Style.Language == FormatStyle::LK_Java && AfterRParen->is(tok::l_paren))
return true;
// If a (non-string) literal follows, this is likely a cast.
- if (Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
- (Tok.Next->Tok.isLiteral() && Tok.Next->isNot(tok::string_literal))) {
+ if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
+ (AfterRParen->Tok.isLiteral() &&
+ AfterRParen->isNot(tok::string_literal))) {
return true;
}
// Heuristically try to determine whether the parentheses contain a type.
- auto IsQualifiedPointerOrReference = [](FormatToken *T,
+ auto IsQualifiedPointerOrReference = [](const FormatToken *T,
const LangOptions &LangOpts) {
// This is used to handle cases such as x = (foo *const)&y;
assert(!T->isTypeName(LangOpts) && "Should have already been checked");
@@ -2791,12 +2801,11 @@ class AnnotatingParser {
return T && T->is(TT_PointerOrReference);
};
bool ParensAreType =
- !Tok.Previous ||
- Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
- Tok.Previous->isTypeName(LangOpts) ||
- IsQualifiedPointerOrReference(Tok.Previous, LangOpts);
+ BeforeRParen->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
+ BeforeRParen->isTypeName(LangOpts) ||
+ IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
bool ParensCouldEndDecl =
- Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
+ AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
if (ParensAreType && !ParensCouldEndDecl)
return true;
@@ -2808,7 +2817,7 @@ class AnnotatingParser {
// Certain token types inside the parentheses mean that this can't be a
// cast.
- for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
+ for (const FormatToken *Token = LParen->Next; Token != &Tok;
Token = Token->Next) {
if (Token->is(TT_BinaryOperator))
return false;
@@ -2816,41 +2825,43 @@ class AnnotatingParser {
// If the following token is an identifier or 'this', this is a cast. All
// cases where this can be something else are handled above.
- if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
+ if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
return true;
// Look for a cast `( x ) (`.
- if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
- if (Tok.Previous->is(tok::identifier) &&
- Tok.Previous->Previous->is(tok::l_paren)) {
+ if (AfterRParen->is(tok::l_paren) && BeforeRParen->Previous) {
+ if (BeforeRParen->is(tok::identifier) &&
+ BeforeRParen->Previous->is(tok::l_paren)) {
return true;
}
}
- if (!Tok.Next->Next)
+ if (!AfterRParen->Next)
return false;
// If the next token after the parenthesis is a unary operator, assume
// that this is cast, unless there are unexpected tokens inside the
// parenthesis.
- 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)) {
+ const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
+ if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
+ AfterRParen->is(tok::plus) ||
+ !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
return false;
}
+
if (NextIsAmpOrStar &&
- (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
+ (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
return false;
}
- if (Line.InPPDirective && Tok.Next->is(tok::minus))
+
+ if (Line.InPPDirective && AfterRParen->is(tok::minus))
return false;
+
// Search for unexpected tokens.
- for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
- Prev = Prev->Previous) {
+ for (auto *Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
return false;
- }
+
return true;
}
|
HazardyKnusperkeks
approved these changes
Jun 20, 2024
AlexisPerry
pushed a commit
to llvm-project-tlp/llvm-project
that referenced
this pull request
Jul 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.