Skip to content

Commit 75cbb1f

Browse files
authored
[clang-format][NFC] Add FormatToken::is(tok::ObjCKeywordKind) (#134973)
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
1 parent 56b7923 commit 75cbb1f

File tree

5 files changed

+30
-52
lines changed

5 files changed

+30
-52
lines changed

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3078,7 +3078,7 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
30783078
for (const FormatToken *FormatTok = Line->First; FormatTok;
30793079
FormatTok = FormatTok->Next) {
30803080
if ((FormatTok->Previous && FormatTok->Previous->is(tok::at) &&
3081-
(FormatTok->Tok.getObjCKeywordID() != tok::objc_not_keyword ||
3081+
(FormatTok->isNot(tok::objc_not_keyword) ||
30823082
FormatTok->isOneOf(tok::numeric_constant, tok::l_square,
30833083
tok::l_brace))) ||
30843084
(FormatTok->Tok.isAnyIdentifier() &&

clang/lib/Format/FormatToken.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,9 @@ struct FormatToken {
621621
bool MacroParent = false;
622622

623623
bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
624+
bool is(tok::ObjCKeywordKind Kind) const {
625+
return Tok.getObjCKeywordID() == Kind;
626+
}
624627
bool is(TokenType TT) const { return getType() == TT; }
625628
bool is(const IdentifierInfo *II) const {
626629
return II && II == Tok.getIdentifierInfo();
@@ -678,10 +681,6 @@ struct FormatToken {
678681
return isOneOf(tok::kw___attribute, tok::kw___declspec, TT_AttributeMacro);
679682
}
680683

681-
bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
682-
return Tok.isObjCAtKeyword(Kind);
683-
}
684-
685684
bool isAccessSpecifierKeyword() const {
686685
return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private);
687686
}
@@ -707,11 +706,8 @@ struct FormatToken {
707706
[[nodiscard]] bool isTypeOrIdentifier(const LangOptions &LangOpts) const;
708707

709708
bool isObjCAccessSpecifier() const {
710-
return is(tok::at) && Next &&
711-
(Next->isObjCAtKeyword(tok::objc_public) ||
712-
Next->isObjCAtKeyword(tok::objc_protected) ||
713-
Next->isObjCAtKeyword(tok::objc_package) ||
714-
Next->isObjCAtKeyword(tok::objc_private));
709+
return Next && Next->isOneOf(tok::objc_public, tok::objc_protected,
710+
tok::objc_package, tok::objc_private);
715711
}
716712

717713
/// Returns whether \p Tok is ([{ or an opening < of a template or in

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ class AnnotatingParser {
330330
if (!Style.isVerilog()) {
331331
if (FormatToken *MaybeSel = OpeningParen.Previous) {
332332
// @selector( starts a selector.
333-
if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
334-
MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
333+
if (MaybeSel->is(tok::objc_selector) && MaybeSel->Previous &&
334+
MaybeSel->Previous->is(tok::at)) {
335335
StartsObjCMethodExpr = true;
336336
}
337337
}
@@ -4505,7 +4505,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
45054505
if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
45064506
return true;
45074507
if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
4508-
Left.Tok.getObjCKeywordID() == tok::objc_property) {
4508+
Left.is(tok::objc_property)) {
45094509
return true;
45104510
}
45114511
if (Right.is(tok::hashhash))
@@ -4899,7 +4899,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
48994899
}
49004900
return false;
49014901
}
4902-
if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4902+
if (Left.is(tok::at) && Right.isNot(tok::objc_not_keyword))
49034903
return false;
49044904
if (Right.is(TT_UnaryOperator)) {
49054905
return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
@@ -6220,9 +6220,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
62206220
if (Right.is(TT_TemplateCloser))
62216221
return Style.BreakBeforeTemplateCloser;
62226222

6223-
if (Left.is(tok::at))
6224-
return false;
6225-
if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
6223+
if (Left.isOneOf(tok::at, tok::objc_interface))
62266224
return false;
62276225
if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
62286226
return Right.isNot(tok::l_paren);

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,10 @@ class LineJoiner {
468468
switch (PreviousLine->First->Tok.getKind()) {
469469
case tok::at:
470470
// Don't merge block with left brace wrapped after ObjC special blocks.
471-
if (PreviousLine->First->Next) {
472-
tok::ObjCKeywordKind kwId =
473-
PreviousLine->First->Next->Tok.getObjCKeywordID();
474-
if (kwId == tok::objc_autoreleasepool ||
475-
kwId == tok::objc_synchronized) {
476-
return 0;
477-
}
471+
if (PreviousLine->First->Next &&
472+
PreviousLine->First->Next->isOneOf(tok::objc_autoreleasepool,
473+
tok::objc_synchronized)) {
474+
return 0;
478475
}
479476
break;
480477

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,12 +1719,13 @@ void UnwrappedLineParser::parseStructuralElement(
17191719
nextToken();
17201720
parseBracedList();
17211721
break;
1722-
} else if (Style.Language == FormatStyle::LK_Java &&
1723-
FormatTok->is(Keywords.kw_interface)) {
1722+
}
1723+
if (Style.Language == FormatStyle::LK_Java &&
1724+
FormatTok->is(Keywords.kw_interface)) {
17241725
nextToken();
17251726
break;
17261727
}
1727-
switch (FormatTok->Tok.getObjCKeywordID()) {
1728+
switch (bool IsAutoRelease = false; FormatTok->Tok.getObjCKeywordID()) {
17281729
case tok::objc_public:
17291730
case tok::objc_protected:
17301731
case tok::objc_package:
@@ -1745,19 +1746,11 @@ void UnwrappedLineParser::parseStructuralElement(
17451746
addUnwrappedLine();
17461747
return;
17471748
case tok::objc_autoreleasepool:
1748-
nextToken();
1749-
if (FormatTok->is(tok::l_brace)) {
1750-
if (Style.BraceWrapping.AfterControlStatement ==
1751-
FormatStyle::BWACS_Always) {
1752-
addUnwrappedLine();
1753-
}
1754-
parseBlock();
1755-
}
1756-
addUnwrappedLine();
1757-
return;
1749+
IsAutoRelease = true;
1750+
[[fallthrough]];
17581751
case tok::objc_synchronized:
17591752
nextToken();
1760-
if (FormatTok->is(tok::l_paren)) {
1753+
if (!IsAutoRelease && FormatTok->is(tok::l_paren)) {
17611754
// Skip synchronization object
17621755
parseParens();
17631756
}
@@ -3086,11 +3079,10 @@ void UnwrappedLineParser::parseTryCatch() {
30863079
if (FormatTok->is(tok::at))
30873080
nextToken();
30883081
if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
3089-
tok::kw___finally) ||
3082+
tok::kw___finally, tok::objc_catch,
3083+
tok::objc_finally) ||
30903084
((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
3091-
FormatTok->is(Keywords.kw_finally)) ||
3092-
(FormatTok->isObjCAtKeyword(tok::objc_catch) ||
3093-
FormatTok->isObjCAtKeyword(tok::objc_finally)))) {
3085+
FormatTok->is(Keywords.kw_finally)))) {
30943086
break;
30953087
}
30963088
nextToken();
@@ -4162,17 +4154,15 @@ void UnwrappedLineParser::parseObjCProtocolList() {
41624154
do {
41634155
nextToken();
41644156
// Early exit in case someone forgot a close angle.
4165-
if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
4166-
FormatTok->isObjCAtKeyword(tok::objc_end)) {
4157+
if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
41674158
return;
4168-
}
41694159
} while (!eof() && FormatTok->isNot(tok::greater));
41704160
nextToken(); // Skip '>'.
41714161
}
41724162

41734163
void UnwrappedLineParser::parseObjCUntilAtEnd() {
41744164
do {
4175-
if (FormatTok->isObjCAtKeyword(tok::objc_end)) {
4165+
if (FormatTok->is(tok::objc_end)) {
41764166
nextToken();
41774167
addUnwrappedLine();
41784168
break;
@@ -4195,8 +4185,7 @@ void UnwrappedLineParser::parseObjCUntilAtEnd() {
41954185
}
41964186

41974187
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
4198-
assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
4199-
FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
4188+
assert(FormatTok->isOneOf(tok::objc_interface, tok::objc_implementation));
42004189
nextToken();
42014190
nextToken(); // interface name
42024191

@@ -4244,10 +4233,8 @@ void UnwrappedLineParser::parseObjCLightweightGenerics() {
42444233
do {
42454234
nextToken();
42464235
// Early exit in case someone forgot a close angle.
4247-
if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
4248-
FormatTok->isObjCAtKeyword(tok::objc_end)) {
4236+
if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
42494237
break;
4250-
}
42514238
if (FormatTok->is(tok::less)) {
42524239
++NumOpenAngles;
42534240
} else if (FormatTok->is(tok::greater)) {
@@ -4261,7 +4248,7 @@ void UnwrappedLineParser::parseObjCLightweightGenerics() {
42614248
// Returns true for the declaration/definition form of @protocol,
42624249
// false for the expression form.
42634250
bool UnwrappedLineParser::parseObjCProtocol() {
4264-
assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
4251+
assert(FormatTok->is(tok::objc_protocol));
42654252
nextToken();
42664253

42674254
if (FormatTok->is(tok::l_paren)) {

0 commit comments

Comments
 (0)