[clang] [clang-format][NFC] Add isJava() and isTextProto() in FormatStyle (PR #135466)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 11 19:34:46 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Also remove redundant name qualifiers format::, FormatStyle::, and LanguageKind::.
---
Patch is 35.21 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/135466.diff
10 Files Affected:
- (modified) clang/include/clang/Format/Format.h (+7-7)
- (modified) clang/lib/Format/BreakableToken.cpp (+3-3)
- (modified) clang/lib/Format/ContinuationIndenter.cpp (+7-7)
- (modified) clang/lib/Format/Format.cpp (+6-7)
- (modified) clang/lib/Format/FormatTokenLexer.cpp (+5-5)
- (modified) clang/lib/Format/TokenAnnotator.cpp (+32-46)
- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+2-6)
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+19-28)
- (modified) clang/unittests/Format/CleanupTest.cpp (+2-2)
- (modified) clang/unittests/Format/FormatTest.cpp (+3-4)
``````````diff
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index cea5e257659d6..f6ceef08b46da 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -3383,11 +3383,11 @@ struct FormatStyle {
}
bool isCSharp() const { return Language == LK_CSharp; }
bool isJson() const { return Language == LK_Json; }
+ bool isJava() const { return Language == LK_Java; }
bool isJavaScript() const { return Language == LK_JavaScript; }
bool isVerilog() const { return Language == LK_Verilog; }
- bool isProto() const {
- return Language == LK_Proto || Language == LK_TextProto;
- }
+ bool isTextProto() const { return Language == LK_TextProto; }
+ bool isProto() const { return Language == LK_Proto || isTextProto(); }
bool isTableGen() const { return Language == LK_TableGen; }
/// The language that this format style targets.
@@ -5482,9 +5482,9 @@ struct FormatStyle {
// The memory management and ownership reminds of a birds nest: chicks
// leaving the nest take photos of the nest with them.
struct FormatStyleSet {
- typedef std::map<FormatStyle::LanguageKind, FormatStyle> MapType;
+ typedef std::map<LanguageKind, FormatStyle> MapType;
- std::optional<FormatStyle> Get(FormatStyle::LanguageKind Language) const;
+ std::optional<FormatStyle> Get(LanguageKind Language) const;
// Adds \p Style to this FormatStyleSet. Style must not have an associated
// FormatStyleSet.
@@ -5516,8 +5516,8 @@ struct FormatStyle {
/// Returns a format style complying with the LLVM coding standards:
/// http://llvm.org/docs/CodingStandards.html.
-FormatStyle getLLVMStyle(
- FormatStyle::LanguageKind Language = FormatStyle::LanguageKind::LK_Cpp);
+FormatStyle
+getLLVMStyle(FormatStyle::LanguageKind Language = FormatStyle::LK_Cpp);
/// Returns a format style complying with one of Google's style guides:
/// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp
index bde7757876990..5317c05f3a460 100644
--- a/clang/lib/Format/BreakableToken.cpp
+++ b/clang/lib/Format/BreakableToken.cpp
@@ -46,7 +46,7 @@ static StringRef getLineCommentIndentPrefix(StringRef Comment,
static constexpr StringRef KnownTextProtoPrefixes[] = {"####", "###", "##",
"//", "#"};
ArrayRef<StringRef> KnownPrefixes(KnownCStylePrefixes);
- if (Style.Language == FormatStyle::LK_TextProto)
+ if (Style.isTextProto())
KnownPrefixes = KnownTextProtoPrefixes;
assert(
@@ -576,7 +576,7 @@ BreakableBlockComment::BreakableBlockComment(
IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());
// Detect a multiline jsdoc comment and set DelimitersOnNewline in that case.
- if (Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) {
+ if (Style.isJavaScript() || Style.isJava()) {
if ((Lines[0] == "*" || Lines[0].starts_with("* ")) && Lines.size() > 1) {
// This is a multiline jsdoc comment.
DelimitersOnNewline = true;
@@ -694,7 +694,7 @@ const llvm::StringSet<>
};
unsigned BreakableBlockComment::getContentIndent(unsigned LineIndex) const {
- if (Style.Language != FormatStyle::LK_Java && !Style.isJavaScript())
+ if (!Style.isJava() && !Style.isJavaScript())
return 0;
// The content at LineIndex 0 of a comment like:
// /** line 0 */
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 1969f4297b211..39eb706fc2fd9 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -158,7 +158,7 @@ static bool opensProtoMessageField(const FormatToken &LessTok,
const FormatStyle &Style) {
if (LessTok.isNot(tok::less))
return false;
- return Style.Language == FormatStyle::LK_TextProto ||
+ return Style.isTextProto() ||
(Style.Language == FormatStyle::LK_Proto &&
(LessTok.NestingLevel > 0 ||
(LessTok.Previous && LessTok.Previous->is(tok::equal))));
@@ -281,7 +281,7 @@ LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
State.LowestLevelOnLine = 0;
State.IgnoreStackForComparison = false;
- if (Style.Language == FormatStyle::LK_TextProto) {
+ if (Style.isTextProto()) {
// We need this in order to deal with the bin packing of text fields at
// global scope.
auto &CurrentState = State.Stack.back();
@@ -924,7 +924,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
CurrentState.ContainsUnwrappedBuilder = true;
}
- if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java)
+ if (Current.is(TT_LambdaArrow) && Style.isJava())
CurrentState.NoLineBreak = true;
if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
(Previous.MatchingParen &&
@@ -1315,7 +1315,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
NextNonComment = &Current;
// Java specific bits.
- if (Style.Language == FormatStyle::LK_Java &&
+ if (Style.isJava() &&
Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends)) {
return std::max(CurrentState.LastSpace,
CurrentState.Indent + Style.ContinuationIndentWidth);
@@ -1806,7 +1806,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
(Style.AlignOperands != FormatStyle::OAS_DontAlign ||
PrecedenceLevel < prec::Assignment) &&
(!Previous || Previous->isNot(tok::kw_return) ||
- (Style.Language != FormatStyle::LK_Java && PrecedenceLevel > 0)) &&
+ (!Style.isJava() && PrecedenceLevel > 0)) &&
(Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
PrecedenceLevel > prec::Comma || Current.NestingLevel == 0) &&
(!Style.isTableGen() ||
@@ -2453,8 +2453,8 @@ ContinuationIndenter::createBreakableToken(const FormatToken &Current,
? 0
: Current.UnbreakableTailLength;
- if (Style.isVerilog() || Style.Language == FormatStyle::LK_Java ||
- Style.isJavaScript() || Style.isCSharp()) {
+ if (Style.isVerilog() || Style.isJava() || Style.isJavaScript() ||
+ Style.isCSharp()) {
BreakableStringLiteralUsingOperators::QuoteStyleType QuoteStyle;
if (Style.isJavaScript() && Text.starts_with("'") &&
Text.ends_with("'")) {
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index b90bd8276e1e2..3802766b652d7 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3591,13 +3591,12 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
return Replaces;
if (isLikelyXml(Code))
return Replaces;
- if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript &&
- isMpegTS(Code)) {
- return Replaces;
- }
- if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript)
+ if (Style.isJavaScript()) {
+ if (isMpegTS(Code))
+ return Replaces;
return sortJavaScriptImports(Style, Code, Ranges, FileName);
- if (Style.Language == FormatStyle::LanguageKind::LK_Java)
+ }
+ if (Style.isJava())
return sortJavaImports(Style, Code, Ranges, FileName, Replaces);
if (Style.isCpp())
sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor);
@@ -3777,7 +3776,7 @@ reformat(const FormatStyle &Style, StringRef Code,
return {tooling::Replacements(), 0};
if (isLikelyXml(Code))
return {tooling::Replacements(), 0};
- if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code))
+ if (Expanded.isJavaScript() && isMpegTS(Code))
return {tooling::Replacements(), 0};
// JSON only needs the formatting passing.
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index 014b10b206d90..5c4e1f814d9b7 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -89,7 +89,7 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
tryParseJSRegexLiteral();
handleTemplateStrings();
}
- if (Style.Language == FormatStyle::LK_TextProto)
+ if (Style.isTextProto())
tryParsePythonComment();
tryMergePreviousTokens();
if (Style.isCSharp()) {
@@ -207,7 +207,7 @@ void FormatTokenLexer::tryMergePreviousTokens() {
return;
}
- if (Style.Language == FormatStyle::LK_Java) {
+ if (Style.isJava()) {
static const tok::TokenKind JavaRightLogicalShiftAssign[] = {
tok::greater, tok::greater, tok::greaterequal};
if (tryMergeTokens(JavaRightLogicalShiftAssign, TT_BinaryOperator))
@@ -1239,8 +1239,8 @@ FormatToken *FormatTokenLexer::getNextToken() {
// finds comments that contain a backslash followed by a line break, truncates
// the comment token at the backslash, and resets the lexer to restart behind
// the backslash.
- if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) &&
- FormatTok->is(tok::comment) && FormatTok->TokenText.starts_with("//")) {
+ if ((Style.isJavaScript() || Style.isJava()) && FormatTok->is(tok::comment) &&
+ FormatTok->TokenText.starts_with("//")) {
size_t BackslashPos = FormatTok->TokenText.find('\\');
while (BackslashPos != StringRef::npos) {
if (BackslashPos + 1 < FormatTok->TokenText.size() &&
@@ -1302,7 +1302,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
FormatTok->Tok.setIdentifierInfo(&Info);
FormatTok->Tok.setKind(Info.getTokenID());
- if (Style.Language == FormatStyle::LK_Java &&
+ if (Style.isJava() &&
FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete,
tok::kw_operator)) {
FormatTok->Tok.setKind(tok::identifier);
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d184c23123c34..82dc403538c45 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -183,10 +183,8 @@ class AnnotatingParser {
if (BeforeLess && BeforeLess->isNot(tok::kw_template))
Contexts.back().ContextType = Context::TemplateArgument;
- if (Style.Language == FormatStyle::LK_Java &&
- CurrentToken->is(tok::question)) {
+ if (Style.isJava() && CurrentToken->is(tok::question))
next();
- }
for (bool SeenTernaryOperator = false, MaybeAngles = true; CurrentToken;) {
const bool InExpr = Contexts[Contexts.size() - 2].IsExpression;
@@ -219,7 +217,7 @@ class AnnotatingParser {
// msg < item: data >
// msg: < item: data >
// In TT_TextProto, map<key, value> does not occur.
- if (Style.Language == FormatStyle::LK_TextProto ||
+ if (Style.isTextProto() ||
(Style.Language == FormatStyle::LK_Proto && BeforeLess &&
BeforeLess->isOneOf(TT_SelectorName, TT_DictLiteral))) {
CurrentToken->setType(TT_DictLiteral);
@@ -236,8 +234,7 @@ class AnnotatingParser {
next();
continue;
}
- if (CurrentToken->is(tok::question) &&
- Style.Language == FormatStyle::LK_Java) {
+ if (CurrentToken->is(tok::question) && Style.isJava()) {
next();
continue;
}
@@ -1375,7 +1372,7 @@ class AnnotatingParser {
Tok->setType(TT_InlineASMColon);
} else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
Tok->setType(TT_DictLiteral);
- if (Style.Language == FormatStyle::LK_TextProto) {
+ if (Style.isTextProto()) {
if (FormatToken *Previous = Tok->getPreviousNonComment())
Previous->setType(TT_SelectorName);
}
@@ -1589,7 +1586,7 @@ class AnnotatingParser {
if (IsCpp) {
if (Tok->is(TT_RequiresExpressionLBrace))
Line.Type = LT_RequiresExpression;
- } else if (Style.Language == FormatStyle::LK_TextProto) {
+ } else if (Style.isTextProto()) {
FormatToken *Previous = Tok->getPreviousNonComment();
if (Previous && Previous->isNot(TT_DictLiteral))
Previous->setType(TT_SelectorName);
@@ -1606,7 +1603,7 @@ class AnnotatingParser {
// msg < item: data >
// msg: < item: data >
// In TT_TextProto, map<key, value> does not occur.
- if (Style.Language == FormatStyle::LK_TextProto ||
+ if (Style.isTextProto() ||
(Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
Tok->setType(TT_DictLiteral);
@@ -1635,7 +1632,7 @@ class AnnotatingParser {
return false;
break;
case tok::greater:
- if (Style.Language != FormatStyle::LK_TextProto && Tok->is(TT_Unknown))
+ if (!Style.isTextProto() && Tok->is(TT_Unknown))
Tok->setType(TT_BinaryOperator);
if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
Tok->SpacesRequiredBefore = 1;
@@ -1998,8 +1995,7 @@ class AnnotatingParser {
// definitions (github.com/google/protobuf) or missing "#" (either way we
// should not break the line).
IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
- if ((Style.Language == FormatStyle::LK_Java &&
- CurrentToken->is(Keywords.kw_package)) ||
+ if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
(!Style.isVerilog() && Info &&
Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
@@ -2297,7 +2293,7 @@ class AnnotatingParser {
} else if (Current.is(TT_TrailingReturnArrow)) {
Contexts.back().IsExpression = false;
} else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
- Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
+ Contexts.back().IsExpression = Style.isJava();
} else if (Current.Previous &&
Current.Previous->is(TT_CtorInitializerColon)) {
Contexts.back().IsExpression = true;
@@ -2415,7 +2411,7 @@ class AnnotatingParser {
// Line.MightBeFunctionDecl can only be true after the parentheses of a
// function declaration have been found. In this case, 'Current' is a
// trailing token of this declaration and thus cannot be a name.
- if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) &&
+ if ((Style.isJavaScript() || Style.isJava()) &&
Current.is(Keywords.kw_instanceof)) {
Current.setType(TT_BinaryOperator);
} else if (isStartOfName(Current) &&
@@ -2429,8 +2425,7 @@ class AnnotatingParser {
Contexts.back().FirstStartOfName = nullptr;
} else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
AutoFound = true;
- } else if (Current.is(tok::arrow) &&
- Style.Language == FormatStyle::LK_Java) {
+ } else if (Current.is(tok::arrow) && Style.isJava()) {
Current.setType(TT_LambdaArrow);
} else if (Current.is(tok::arrow) && Style.isVerilog()) {
// The implication operator.
@@ -2477,8 +2472,7 @@ class AnnotatingParser {
}
} else if (Current.isBinaryOperator() &&
(!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
- (Current.isNot(tok::greater) &&
- Style.Language != FormatStyle::LK_TextProto)) {
+ (Current.isNot(tok::greater) && !Style.isTextProto())) {
if (Style.isVerilog()) {
if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
!Contexts.back().VerilogAssignmentFound) {
@@ -2537,7 +2531,7 @@ class AnnotatingParser {
}
}
} else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
- Style.Language != FormatStyle::LK_Java) {
+ !Style.isJava()) {
// In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
// marks declarations and properties that need special formatting.
switch (Current.Next->Tok.getObjCKeywordID()) {
@@ -2557,7 +2551,7 @@ class AnnotatingParser {
if (PreviousNoComment &&
PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
Current.setType(TT_DesignatedInitializerPeriod);
- } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
+ } else if (Style.isJava() && Current.Previous &&
Current.Previous->isOneOf(TT_JavaAnnotation,
TT_LeadingJavaAnnotation)) {
Current.setType(Current.Previous->getType());
@@ -2584,9 +2578,7 @@ class AnnotatingParser {
// Line.MightBeFunctionDecl can only be true after the parentheses of a
// function declaration have been found.
Current.setType(TT_TrailingAnnotation);
- } else if ((Style.Language == FormatStyle::LK_Java ||
- Style.isJavaScript()) &&
- Current.Previous) {
+ } else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
if (Current.Previous->is(tok::at) &&
Current.isNot(Keywords.kw_interface)) {
const FormatToken &AtToken = *Current.Previous;
@@ -2616,10 +2608,8 @@ class AnnotatingParser {
if (!Tok.Previous || Tok.isNot(tok::identifier) || Tok.is(TT_ClassHeadName))
return false;
- if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) &&
- Tok.is(Keywords.kw_extends)) {
+ if ((Style.isJavaScript() || Style.isJava()) && Tok.is(Keywords.kw_extends))
return false;
- }
if (const auto *NextNonComment = Tok.getNextNonComment();
(!NextNonComment && !Line.InMacroBody) ||
@@ -2696,10 +2686,8 @@ class AnnotatingParser {
return true;
// type[] a in Java
- if (Style.Language == FormatStyle::LK_Java &&
- PreviousNotConst->is(tok::r_square)) {
+ if (Style.isJava() && PreviousNotConst->is(tok::r_square))
return true;
- }
// const a = in JavaScript.
return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
@@ -2734,7 +2722,7 @@ class AnnotatingParser {
return false;
// C-style casts are only used in C++, C# and Java.
- if (!IsCpp && !Style.isCSharp() && Style.Language != FormatStyle::LK_Java)
+ if (!IsCpp && !Style.isCSharp() && !Style.isJava())
return false;
const auto *LParen = Tok.MatchingParen;
@@ -2824,7 +2812,7 @@ class AnnotatingParser {
// As Java has no function types, a "(" after the ")" likely means that this
// is a cast.
- if (Style.Language == FormatStyle::LK_Java && AfterRParen->is(tok::l_paren))
+ if (Style.isJava() && AfterRParen->is(tok::l_paren))
return true;
// If a (non-string) literal follows, this is likely a cast.
@@ -3271,8 +3259,7 @@ class ExpressionParser {
Start = Current;
}
- if ((Style.isCSharp() || Style.isJavaScript() ||
- Style.Language == FormatStyle::LK_Java) &&
+ if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
Precedence == prec::Additive && Current) {
// A string can be broken without parentheses around it when it is
// already in a sequence of strings joined by `+` signs.
@@ -3376,7 +3363,7 @@ class ExpressionParser {
}
if (Current->is(TT_RangeBasedForLoopColon))
return prec::Comma;
- if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
+ if ((Style.isJava() || Style.isJavaScript()) &&
Current->is(Keywords.kw_instanceof)) {
return prec::Relational;
}
@@ -3390,7 +3377,7 @@ class ExpressionParser {
Current->isNot(TT_TrailingReturnArrow)) {
return PrecedenceArrowAndPeriod;
}
- if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
+ if ((Style.isJava() || Style.isJavaScript()) &&
Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
Keywords.kw_throws)) {
return 0;
@@ -4272,7 +4259,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
return 0;
// Language specific handling.
- if (Style.Language == FormatStyle::LK_Java) {
+ if (Style.isJava()) {
...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/135466
More information about the cfe-commits
mailing list