Skip to content

Commit 3c6a109

Browse files
authored
[clang-format][NFC] Add FormatToken::isAccessSpecifierKeyword() (#95727)
1 parent ffed34e commit 3c6a109

File tree

3 files changed

+43
-44
lines changed

3 files changed

+43
-44
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -667,12 +667,16 @@ struct FormatToken {
667667
return Tok.isObjCAtKeyword(Kind);
668668
}
669669

670+
bool isAccessSpecifierKeyword() const {
671+
return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private);
672+
}
673+
670674
bool isAccessSpecifier(bool ColonRequired = true) const {
671-
if (!isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
675+
if (!isAccessSpecifierKeyword())
672676
return false;
673677
if (!ColonRequired)
674678
return true;
675-
const auto NextNonComment = getNextNonComment();
679+
const auto *NextNonComment = getNextNonComment();
676680
return NextNonComment && NextNonComment->is(tok::colon);
677681
}
678682

@@ -1656,10 +1660,12 @@ struct AdditionalKeywords {
16561660
/// If \c AcceptIdentifierName is true, returns true not only for keywords,
16571661
// but also for IdentifierName tokens (aka pseudo-keywords), such as
16581662
// ``yield``.
1659-
bool IsJavaScriptIdentifier(const FormatToken &Tok,
1663+
bool isJavaScriptIdentifier(const FormatToken &Tok,
16601664
bool AcceptIdentifierName = true) const {
16611665
// Based on the list of JavaScript & TypeScript keywords here:
16621666
// https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L74
1667+
if (Tok.isAccessSpecifierKeyword())
1668+
return false;
16631669
switch (Tok.Tok.getKind()) {
16641670
case tok::kw_break:
16651671
case tok::kw_case:
@@ -1679,9 +1685,6 @@ struct AdditionalKeywords {
16791685
case tok::kw_import:
16801686
case tok::kw_module:
16811687
case tok::kw_new:
1682-
case tok::kw_private:
1683-
case tok::kw_protected:
1684-
case tok::kw_public:
16851688
case tok::kw_return:
16861689
case tok::kw_static:
16871690
case tok::kw_switch:
@@ -1724,6 +1727,8 @@ struct AdditionalKeywords {
17241727
/// Returns \c true if \p Tok is a C# keyword, returns
17251728
/// \c false if it is a anything else.
17261729
bool isCSharpKeyword(const FormatToken &Tok) const {
1730+
if (Tok.isAccessSpecifierKeyword())
1731+
return true;
17271732
switch (Tok.Tok.getKind()) {
17281733
case tok::kw_bool:
17291734
case tok::kw_break:
@@ -1750,9 +1755,6 @@ struct AdditionalKeywords {
17501755
case tok::kw_namespace:
17511756
case tok::kw_new:
17521757
case tok::kw_operator:
1753-
case tok::kw_private:
1754-
case tok::kw_protected:
1755-
case tok::kw_public:
17561758
case tok::kw_return:
17571759
case tok::kw_short:
17581760
case tok::kw_sizeof:

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,8 @@ class AnnotatingParser {
647647
return true;
648648

649649
// Limit this to being an access modifier that follows.
650-
if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
651-
tok::comment, tok::kw_class, tok::kw_static,
650+
if (AttrTok->isAccessSpecifierKeyword() ||
651+
AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
652652
tok::l_square, Keywords.kw_internal)) {
653653
return true;
654654
}
@@ -1419,7 +1419,7 @@ class AnnotatingParser {
14191419
Tok->setType(TT_CtorInitializerColon);
14201420
} else {
14211421
Tok->setType(TT_InheritanceColon);
1422-
if (Prev->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected))
1422+
if (Prev->isAccessSpecifierKeyword())
14231423
Line.Type = LT_AccessModifier;
14241424
}
14251425
} else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
@@ -2333,7 +2333,7 @@ class AnnotatingParser {
23332333
if (Current.Previous) {
23342334
bool IsIdentifier =
23352335
Style.isJavaScript()
2336-
? Keywords.IsJavaScriptIdentifier(
2336+
? Keywords.isJavaScriptIdentifier(
23372337
*Current.Previous, /* AcceptIdentifierName= */ true)
23382338
: Current.Previous->is(tok::identifier);
23392339
if (IsIdentifier ||
@@ -4949,11 +4949,11 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
49494949

49504950
// space between method modifier and opening parenthesis of a tuple return
49514951
// type
4952-
if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
4953-
tok::kw_virtual, tok::kw_extern, tok::kw_static,
4954-
Keywords.kw_internal, Keywords.kw_abstract,
4955-
Keywords.kw_sealed, Keywords.kw_override,
4956-
Keywords.kw_async, Keywords.kw_unsafe) &&
4952+
if ((Left.isAccessSpecifierKeyword() ||
4953+
Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
4954+
Keywords.kw_internal, Keywords.kw_abstract,
4955+
Keywords.kw_sealed, Keywords.kw_override,
4956+
Keywords.kw_async, Keywords.kw_unsafe)) &&
49574957
Right.is(tok::l_paren)) {
49584958
return true;
49594959
}
@@ -4979,7 +4979,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
49794979
}
49804980
// In tagged template literals ("html`bar baz`"), there is no space between
49814981
// the tag identifier and the template string.
4982-
if (Keywords.IsJavaScriptIdentifier(Left,
4982+
if (Keywords.isJavaScriptIdentifier(Left,
49834983
/* AcceptIdentifierName= */ false) &&
49844984
Right.is(TT_TemplateString)) {
49854985
return false;
@@ -5074,9 +5074,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
50745074
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
50755075
spaceRequiredBeforeParens(Right);
50765076
}
5077-
if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
5078-
tok::kw_protected) ||
5079-
Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
5077+
if ((Left.isAccessSpecifierKeyword() ||
5078+
Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
50805079
Keywords.kw_native)) &&
50815080
Right.is(TT_TemplateOpener)) {
50825081
return true;
@@ -5699,9 +5698,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
56995698
if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
57005699
auto *FirstNonComment = Line.getFirstNonComment();
57015700
bool AccessSpecifier =
5702-
FirstNonComment &&
5703-
FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
5704-
tok::kw_private, tok::kw_protected);
5701+
FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5702+
FirstNonComment->isAccessSpecifierKeyword());
57055703

57065704
if (Style.BraceWrapping.AfterEnum) {
57075705
if (Line.startsWith(tok::kw_enum) ||
@@ -5887,13 +5885,13 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
58875885
} else if (Style.isJavaScript()) {
58885886
const FormatToken *NonComment = Right.getPreviousNonComment();
58895887
if (NonComment &&
5890-
NonComment->isOneOf(
5891-
tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5892-
tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5893-
tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
5894-
Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
5895-
Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
5896-
Keywords.kw_await)) {
5888+
(NonComment->isAccessSpecifierKeyword() ||
5889+
NonComment->isOneOf(
5890+
tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5891+
tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5892+
tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
5893+
Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
5894+
Keywords.kw_async, Keywords.kw_await))) {
58975895
return false; // Otherwise automatic semicolon insertion would trigger.
58985896
}
58995897
if (Right.NestingLevel == 0 &&

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,15 @@ void UnwrappedLineParser::parseStructuralElement(
14581458
}
14591459

14601460
// Tokens that only make sense at the beginning of a line.
1461+
if (FormatTok->isAccessSpecifierKeyword()) {
1462+
if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() ||
1463+
Style.isCSharp()) {
1464+
nextToken();
1465+
} else {
1466+
parseAccessSpecifier();
1467+
}
1468+
return;
1469+
}
14611470
switch (FormatTok->Tok.getKind()) {
14621471
case tok::kw_asm:
14631472
nextToken();
@@ -1479,16 +1488,6 @@ void UnwrappedLineParser::parseStructuralElement(
14791488
case tok::kw_namespace:
14801489
parseNamespace();
14811490
return;
1482-
case tok::kw_public:
1483-
case tok::kw_protected:
1484-
case tok::kw_private:
1485-
if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() ||
1486-
Style.isCSharp()) {
1487-
nextToken();
1488-
} else {
1489-
parseAccessSpecifier();
1490-
}
1491-
return;
14921491
case tok::kw_if: {
14931492
if (Style.isJavaScript() && Line->MustBeDeclaration) {
14941493
// field/method declaration.
@@ -2156,8 +2155,8 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() {
21562155
bool HasSpecialAccessor = false;
21572156
bool IsTrivialPropertyAccessor = true;
21582157
while (!eof()) {
2159-
if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private,
2160-
tok::kw_protected, Keywords.kw_internal, Keywords.kw_get,
2158+
if (Tok->isAccessSpecifierKeyword() ||
2159+
Tok->isOneOf(tok::semi, Keywords.kw_internal, Keywords.kw_get,
21612160
Keywords.kw_init, Keywords.kw_set)) {
21622161
if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set))
21632162
HasSpecialAccessor = true;

0 commit comments

Comments
 (0)