-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format] More consumeToken() cleanup #143063
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
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/143063.diff 1 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index da279d07b5918..7c102ca2c6a61 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1306,6 +1306,7 @@ class AnnotatingParser {
if (Tok->is(TT_TableGenMultiLineString))
return true;
auto *Prev = Tok->getPreviousNonComment();
+ auto *Next = Tok->getNextNonComment();
switch (bool IsIf = false; Tok->Tok.getKind()) {
case tok::plus:
case tok::minus:
@@ -1435,10 +1436,10 @@ class AnnotatingParser {
if (Prev->isAccessSpecifierKeyword())
Line.Type = LT_AccessModifier;
}
- } else if (canBeObjCSelectorComponent(*Prev) && Tok->Next &&
- (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
- (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
- Tok->Next->Next->is(tok::colon)))) {
+ } else if (canBeObjCSelectorComponent(*Prev) && Next &&
+ (Next->isOneOf(tok::r_paren, tok::comma) ||
+ (canBeObjCSelectorComponent(*Next) && Next->Next &&
+ Next->Next->is(tok::colon)))) {
// This handles a special macro in ObjC code where selectors including
// the colon are passed as macro arguments.
Tok->setType(TT_ObjCMethodExpr);
@@ -1476,10 +1477,8 @@ class AnnotatingParser {
case tok::kw_for:
if (Style.isJavaScript()) {
// x.for and {for: ...}
- if ((Prev && Prev->is(tok::period)) ||
- (Tok->Next && Tok->Next->is(tok::colon))) {
+ if ((Prev && Prev->is(tok::period)) || (Next && Next->is(tok::colon)))
break;
- }
// JS' for await ( ...
if (CurrentToken && CurrentToken->is(Keywords.kw_await))
next();
@@ -1690,9 +1689,9 @@ class AnnotatingParser {
CurrentToken->Previous->setType(TT_OverloadedOperator);
break;
case tok::question:
- if (Style.isJavaScript() && Tok->Next &&
- Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
- tok::r_brace, tok::r_square)) {
+ if (Style.isJavaScript() && Next &&
+ Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
+ tok::r_brace, tok::r_square)) {
// Question marks before semicolons, colons, etc. indicate optional
// types (fields, parameters), e.g.
// function(x?: string, y?) {...}
@@ -1709,8 +1708,7 @@ class AnnotatingParser {
if (Style.isCSharp()) {
// `Type?)`, `Type?>`, `Type? name;`, and `Type? name =` can only be
// nullable types.
- if (const auto *Next = Tok->getNextNonComment();
- Next && (Next->isOneOf(tok::r_paren, tok::greater) ||
+ if (Next && (Next->isOneOf(tok::r_paren, tok::greater) ||
Next->startsSequence(tok::identifier, tok::semi) ||
Next->startsSequence(tok::identifier, tok::equal))) {
Tok->setType(TT_CSharpNullable);
@@ -1723,10 +1721,8 @@ class AnnotatingParser {
// cond ? id : "B";
// cond ? cond2 ? "A" : "B" : "C";
if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
- (!Tok->Next ||
- !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
- !Tok->Next->Next ||
- !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
+ (!Next || !Next->isOneOf(tok::identifier, tok::string_literal) ||
+ !Next->Next || !Next->Next->isOneOf(tok::colon, tok::question))) {
Tok->setType(TT_CSharpNullable);
break;
}
@@ -1773,8 +1769,8 @@ class AnnotatingParser {
Keywords.kw___has_include_next)) {
parseHasInclude();
}
- if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
- Tok->Next->isNot(tok::l_paren)) {
+ if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Next &&
+ Next->isNot(tok::l_paren)) {
Tok->setType(TT_CSharpGenericTypeConstraint);
parseCSharpGenericTypeConstraint();
if (!Prev)
@@ -1785,8 +1781,7 @@ class AnnotatingParser {
if (!parseTableGenValue())
return false;
} else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
- (!Tok->Next ||
- !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
+ (!Next || !Next->isOneOf(tok::colon, tok::l_brace))) {
// The case NameValue appears.
if (!parseTableGenValue(true))
return false;
|
HazardyKnusperkeks
approved these changes
Jun 6, 2025
rorth
pushed a commit
to rorth/llvm-project
that referenced
this pull request
Jun 11, 2025
DhruvSrivastavaX
pushed a commit
to DhruvSrivastavaX/lldb-for-aix
that referenced
this pull request
Jun 12, 2025
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.
Similar to #142104