Skip to content

[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
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() &&
Expand Down
14 changes: 5 additions & 9 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand Down
12 changes: 5 additions & 7 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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) &&
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
43 changes: 15 additions & 28 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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

Expand Down Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down