-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format][NFC] Add FormatToken::is(tok::ObjCKeywordKind) #134973
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
This allows simplification of code that checks if a token is an Objective-C keyword. Also, delete the following in UnwrappedLineParser::parseStructuralElement(): - an else-after-break in the tok::at case - the copypasted code in the tok::objc_autoreleasepool case
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesThis allows simplification of code that checks if a token is an Objective-C keyword. Also, delete the following in UnwrappedLineParser::parseStructuralElement():
Full diff: https://github.com/llvm/llvm-project/pull/134973.diff 5 Files Affected:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 226d39f635676..eec5cc8632168 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3078,7 +3078,7 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
for (const FormatToken *FormatTok = Line->First; FormatTok;
FormatTok = FormatTok->Next) {
if ((FormatTok->Previous && FormatTok->Previous->is(tok::at) &&
- (FormatTok->Tok.getObjCKeywordID() != tok::objc_not_keyword ||
+ (FormatTok->isNot(tok::objc_not_keyword) ||
FormatTok->isOneOf(tok::numeric_constant, tok::l_square,
tok::l_brace))) ||
(FormatTok->Tok.isAnyIdentifier() &&
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index a5c2388bb143d..87e16397ad069 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -621,6 +621,9 @@ struct FormatToken {
bool MacroParent = false;
bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
+ bool is(tok::ObjCKeywordKind Kind) const {
+ return Tok.getObjCKeywordID() == Kind;
+ }
bool is(TokenType TT) const { return getType() == TT; }
bool is(const IdentifierInfo *II) const {
return II && II == Tok.getIdentifierInfo();
@@ -678,10 +681,6 @@ struct FormatToken {
return isOneOf(tok::kw___attribute, tok::kw___declspec, TT_AttributeMacro);
}
- bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
- return Tok.isObjCAtKeyword(Kind);
- }
-
bool isAccessSpecifierKeyword() const {
return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private);
}
@@ -707,11 +706,8 @@ struct FormatToken {
[[nodiscard]] bool isTypeOrIdentifier(const LangOptions &LangOpts) const;
bool isObjCAccessSpecifier() const {
- return is(tok::at) && Next &&
- (Next->isObjCAtKeyword(tok::objc_public) ||
- Next->isObjCAtKeyword(tok::objc_protected) ||
- Next->isObjCAtKeyword(tok::objc_package) ||
- Next->isObjCAtKeyword(tok::objc_private));
+ return Next && Next->isOneOf(tok::objc_public, tok::objc_protected,
+ tok::objc_package, tok::objc_private);
}
/// Returns whether \p Tok is ([{ or an opening < of a template or in
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index bd54470dcba37..a3aa69a1c17b4 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -330,8 +330,8 @@ class AnnotatingParser {
if (!Style.isVerilog()) {
if (FormatToken *MaybeSel = OpeningParen.Previous) {
// @selector( starts a selector.
- if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
- MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
+ if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
+ MaybeSel->Previous->is(tok::at)) {
StartsObjCMethodExpr = true;
}
}
@@ -4505,7 +4505,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
return true;
if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
- Left.Tok.getObjCKeywordID() == tok::objc_property) {
+ Left.is(tok::objc_property)) {
return true;
}
if (Right.is(tok::hashhash))
@@ -4899,7 +4899,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
}
return false;
}
- if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
+ if (Left.is(tok::at) && Right.isNot(tok::objc_not_keyword))
return false;
if (Right.is(TT_UnaryOperator)) {
return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
@@ -6220,9 +6220,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
if (Right.is(TT_TemplateCloser))
return Style.BreakBeforeTemplateCloser;
- if (Left.is(tok::at))
- return false;
- if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
+ if (Left.isOneOf(tok::at, tok::objc_interface))
return false;
if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
return Right.isNot(tok::l_paren);
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 62759d7945f7b..40cedaaa84d04 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -468,13 +468,10 @@ class LineJoiner {
switch (PreviousLine->First->Tok.getKind()) {
case tok::at:
// Don't merge block with left brace wrapped after ObjC special blocks.
- if (PreviousLine->First->Next) {
- tok::ObjCKeywordKind kwId =
- PreviousLine->First->Next->Tok.getObjCKeywordID();
- if (kwId == tok::objc_autoreleasepool ||
- kwId == tok::objc_synchronized) {
- return 0;
- }
+ if (PreviousLine->First->Next &&
+ PreviousLine->First->Next->isOneOf(tok::objc_autoreleasepool,
+ tok::objc_synchronized)) {
+ return 0;
}
break;
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 9641da1577ded..b49e80b0b89c8 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1719,12 +1719,13 @@ void UnwrappedLineParser::parseStructuralElement(
nextToken();
parseBracedList();
break;
- } else if (Style.Language == FormatStyle::LK_Java &&
- FormatTok->is(Keywords.kw_interface)) {
+ }
+ if (Style.Language == FormatStyle::LK_Java &&
+ FormatTok->is(Keywords.kw_interface)) {
nextToken();
break;
}
- switch (FormatTok->Tok.getObjCKeywordID()) {
+ switch (bool IsAutoRelease = false; FormatTok->Tok.getObjCKeywordID()) {
case tok::objc_public:
case tok::objc_protected:
case tok::objc_package:
@@ -1745,19 +1746,11 @@ void UnwrappedLineParser::parseStructuralElement(
addUnwrappedLine();
return;
case tok::objc_autoreleasepool:
- nextToken();
- if (FormatTok->is(tok::l_brace)) {
- if (Style.BraceWrapping.AfterControlStatement ==
- FormatStyle::BWACS_Always) {
- addUnwrappedLine();
- }
- parseBlock();
- }
- addUnwrappedLine();
- return;
+ IsAutoRelease = true;
+ [[fallthrough]];
case tok::objc_synchronized:
nextToken();
- if (FormatTok->is(tok::l_paren)) {
+ if (!IsAutoRelease && FormatTok->is(tok::l_paren)) {
// Skip synchronization object
parseParens();
}
@@ -3086,11 +3079,10 @@ void UnwrappedLineParser::parseTryCatch() {
if (FormatTok->is(tok::at))
nextToken();
if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
- tok::kw___finally) ||
+ tok::kw___finally, tok::objc_catch,
+ tok::objc_finally) ||
((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
- FormatTok->is(Keywords.kw_finally)) ||
- (FormatTok->isObjCAtKeyword(tok::objc_catch) ||
- FormatTok->isObjCAtKeyword(tok::objc_finally)))) {
+ FormatTok->is(Keywords.kw_finally)))) {
break;
}
nextToken();
@@ -4162,17 +4154,15 @@ void UnwrappedLineParser::parseObjCProtocolList() {
do {
nextToken();
// Early exit in case someone forgot a close angle.
- if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
- FormatTok->isObjCAtKeyword(tok::objc_end)) {
+ if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
return;
- }
} while (!eof() && FormatTok->isNot(tok::greater));
nextToken(); // Skip '>'.
}
void UnwrappedLineParser::parseObjCUntilAtEnd() {
do {
- if (FormatTok->isObjCAtKeyword(tok::objc_end)) {
+ if (FormatTok->is(tok::objc_end)) {
nextToken();
addUnwrappedLine();
break;
@@ -4195,8 +4185,7 @@ void UnwrappedLineParser::parseObjCUntilAtEnd() {
}
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
- assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
- FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
+ assert(FormatTok->isOneOf(tok::objc_interface, tok::objc_implementation));
nextToken();
nextToken(); // interface name
@@ -4244,10 +4233,8 @@ void UnwrappedLineParser::parseObjCLightweightGenerics() {
do {
nextToken();
// Early exit in case someone forgot a close angle.
- if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
- FormatTok->isObjCAtKeyword(tok::objc_end)) {
+ if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
break;
- }
if (FormatTok->is(tok::less)) {
++NumOpenAngles;
} else if (FormatTok->is(tok::greater)) {
@@ -4261,7 +4248,7 @@ void UnwrappedLineParser::parseObjCLightweightGenerics() {
// Returns true for the declaration/definition form of @protocol,
// false for the expression form.
bool UnwrappedLineParser::parseObjCProtocol() {
- assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
+ assert(FormatTok->is(tok::objc_protocol));
nextToken();
if (FormatTok->is(tok::l_paren)) {
|
HazardyKnusperkeks
approved these changes
Apr 9, 2025
mydeveloperday
approved these changes
Apr 9, 2025
AllinLeeYL
pushed a commit
to AllinLeeYL/llvm-project
that referenced
this pull request
Apr 10, 2025
…34973) This allows simplification of code that checks if a token is an Objective-C keyword. Also, delete the following in UnwrappedLineParser::parseStructuralElement(): - an else-after-break in the tok::at case - the copypasted code in the tok::objc_autoreleasepool case
var-const
pushed a commit
to ldionne/llvm-project
that referenced
this pull request
Apr 17, 2025
…34973) This allows simplification of code that checks if a token is an Objective-C keyword. Also, delete the following in UnwrappedLineParser::parseStructuralElement(): - an else-after-break in the tok::at case - the copypasted code in the tok::objc_autoreleasepool case
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.
This allows simplification of code that checks if a token is an Objective-C keyword.
Also, delete the following in UnwrappedLineParser::parseStructuralElement():