[clang] [clang-format][NFC] Add FormatToken::is(tok::ObjCKeywordKind) (PR #134973)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 8 23:51:31 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/134973.diff
5 Files Affected:
- (modified) clang/lib/Format/Format.cpp (+1-1)
- (modified) clang/lib/Format/FormatToken.h (+5-9)
- (modified) clang/lib/Format/TokenAnnotator.cpp (+5-7)
- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+4-7)
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+15-28)
``````````diff
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)) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/134973
More information about the cfe-commits
mailing list