[clang] [clang-format][NFC] Add FormatToken::isAccessSpecifierKeyword() (PR #95727)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 16 17:51:26 PDT 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/95727
None
>From ec9b902518ef09d77e8b32777032a852d33476fd Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 16 Jun 2024 17:47:56 -0700
Subject: [PATCH] [clang-format][NFC] Add
FormatToken::isAccessSpecifierKeyword()
---
clang/lib/Format/FormatToken.h | 20 ++++++-----
clang/lib/Format/TokenAnnotator.cpp | 44 +++++++++++-------------
clang/lib/Format/UnwrappedLineParser.cpp | 23 ++++++-------
3 files changed, 43 insertions(+), 44 deletions(-)
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index e4a4f27e502b1..4ffd745bf9307 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -667,12 +667,16 @@ struct FormatToken {
return Tok.isObjCAtKeyword(Kind);
}
+ bool isAccessSpecifierKeyword() const {
+ return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private);
+ }
+
bool isAccessSpecifier(bool ColonRequired = true) const {
- if (!isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
+ if (!isAccessSpecifierKeyword())
return false;
if (!ColonRequired)
return true;
- const auto NextNonComment = getNextNonComment();
+ const auto *NextNonComment = getNextNonComment();
return NextNonComment && NextNonComment->is(tok::colon);
}
@@ -1656,10 +1660,12 @@ struct AdditionalKeywords {
/// If \c AcceptIdentifierName is true, returns true not only for keywords,
// but also for IdentifierName tokens (aka pseudo-keywords), such as
// ``yield``.
- bool IsJavaScriptIdentifier(const FormatToken &Tok,
+ bool isJavaScriptIdentifier(const FormatToken &Tok,
bool AcceptIdentifierName = true) const {
// Based on the list of JavaScript & TypeScript keywords here:
// https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L74
+ if (Tok.isAccessSpecifierKeyword())
+ return false;
switch (Tok.Tok.getKind()) {
case tok::kw_break:
case tok::kw_case:
@@ -1679,9 +1685,6 @@ struct AdditionalKeywords {
case tok::kw_import:
case tok::kw_module:
case tok::kw_new:
- case tok::kw_private:
- case tok::kw_protected:
- case tok::kw_public:
case tok::kw_return:
case tok::kw_static:
case tok::kw_switch:
@@ -1724,6 +1727,8 @@ struct AdditionalKeywords {
/// Returns \c true if \p Tok is a C# keyword, returns
/// \c false if it is a anything else.
bool isCSharpKeyword(const FormatToken &Tok) const {
+ if (Tok.isAccessSpecifierKeyword())
+ return true;
switch (Tok.Tok.getKind()) {
case tok::kw_bool:
case tok::kw_break:
@@ -1750,9 +1755,6 @@ struct AdditionalKeywords {
case tok::kw_namespace:
case tok::kw_new:
case tok::kw_operator:
- case tok::kw_private:
- case tok::kw_protected:
- case tok::kw_public:
case tok::kw_return:
case tok::kw_short:
case tok::kw_sizeof:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 1332445070314..5a256fc7ebd9f 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -647,8 +647,8 @@ class AnnotatingParser {
return true;
// Limit this to being an access modifier that follows.
- if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
- tok::comment, tok::kw_class, tok::kw_static,
+ if (AttrTok->isAccessSpecifierKeyword() ||
+ AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
tok::l_square, Keywords.kw_internal)) {
return true;
}
@@ -1419,7 +1419,7 @@ class AnnotatingParser {
Tok->setType(TT_CtorInitializerColon);
} else {
Tok->setType(TT_InheritanceColon);
- if (Prev->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected))
+ if (Prev->isAccessSpecifierKeyword())
Line.Type = LT_AccessModifier;
}
} else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
@@ -2333,7 +2333,7 @@ class AnnotatingParser {
if (Current.Previous) {
bool IsIdentifier =
Style.isJavaScript()
- ? Keywords.IsJavaScriptIdentifier(
+ ? Keywords.isJavaScriptIdentifier(
*Current.Previous, /* AcceptIdentifierName= */ true)
: Current.Previous->is(tok::identifier);
if (IsIdentifier ||
@@ -4948,11 +4948,11 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
// space between method modifier and opening parenthesis of a tuple return
// type
- if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
- tok::kw_virtual, tok::kw_extern, tok::kw_static,
- Keywords.kw_internal, Keywords.kw_abstract,
- Keywords.kw_sealed, Keywords.kw_override,
- Keywords.kw_async, Keywords.kw_unsafe) &&
+ if ((Left.isAccessSpecifierKeyword() ||
+ Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
+ Keywords.kw_internal, Keywords.kw_abstract,
+ Keywords.kw_sealed, Keywords.kw_override,
+ Keywords.kw_async, Keywords.kw_unsafe)) &&
Right.is(tok::l_paren)) {
return true;
}
@@ -4978,7 +4978,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
}
// In tagged template literals ("html`bar baz`"), there is no space between
// the tag identifier and the template string.
- if (Keywords.IsJavaScriptIdentifier(Left,
+ if (Keywords.isJavaScriptIdentifier(Left,
/* AcceptIdentifierName= */ false) &&
Right.is(TT_TemplateString)) {
return false;
@@ -5073,9 +5073,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
spaceRequiredBeforeParens(Right);
}
- if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
- tok::kw_protected) ||
- Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
+ if ((Left.isAccessSpecifierKeyword() ||
+ Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
Keywords.kw_native)) &&
Right.is(TT_TemplateOpener)) {
return true;
@@ -5698,9 +5697,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
auto *FirstNonComment = Line.getFirstNonComment();
bool AccessSpecifier =
- FirstNonComment &&
- FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
- tok::kw_private, tok::kw_protected);
+ FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
+ FirstNonComment->isAccessSpecifierKeyword());
if (Style.BraceWrapping.AfterEnum) {
if (Line.startsWith(tok::kw_enum) ||
@@ -5886,13 +5884,13 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
} else if (Style.isJavaScript()) {
const FormatToken *NonComment = Right.getPreviousNonComment();
if (NonComment &&
- NonComment->isOneOf(
- tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
- tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
- tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
- Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
- Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
- Keywords.kw_await)) {
+ (NonComment->isAccessSpecifierKeyword() ||
+ NonComment->isOneOf(
+ tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
+ tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
+ tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
+ Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
+ Keywords.kw_async, Keywords.kw_await))) {
return false; // Otherwise automatic semicolon insertion would trigger.
}
if (Right.NestingLevel == 0 &&
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 1fad02de202a3..df5bb757a99ec 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1458,6 +1458,15 @@ void UnwrappedLineParser::parseStructuralElement(
}
// Tokens that only make sense at the beginning of a line.
+ if (FormatTok->isAccessSpecifierKeyword()) {
+ if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() ||
+ Style.isCSharp()) {
+ nextToken();
+ } else {
+ parseAccessSpecifier();
+ }
+ return;
+ }
switch (FormatTok->Tok.getKind()) {
case tok::kw_asm:
nextToken();
@@ -1479,16 +1488,6 @@ void UnwrappedLineParser::parseStructuralElement(
case tok::kw_namespace:
parseNamespace();
return;
- case tok::kw_public:
- case tok::kw_protected:
- case tok::kw_private:
- if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() ||
- Style.isCSharp()) {
- nextToken();
- } else {
- parseAccessSpecifier();
- }
- return;
case tok::kw_if: {
if (Style.isJavaScript() && Line->MustBeDeclaration) {
// field/method declaration.
@@ -2156,8 +2155,8 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() {
bool HasSpecialAccessor = false;
bool IsTrivialPropertyAccessor = true;
while (!eof()) {
- if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private,
- tok::kw_protected, Keywords.kw_internal, Keywords.kw_get,
+ if (Tok->isAccessSpecifierKeyword() ||
+ Tok->isOneOf(tok::semi, Keywords.kw_internal, Keywords.kw_get,
Keywords.kw_init, Keywords.kw_set)) {
if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set))
HasSpecialAccessor = true;
More information about the cfe-commits
mailing list