From 09c8cfe219481a8fc20c6711dc5c87451f5a5ef1 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpiano@gmail.com> Date: Sat, 12 Apr 2025 15:04:29 -0700 Subject: [PATCH] [clang-format][NFC] Add isJava() and isTextProto() in FormatStyle (#135466) Also remove redundant name qualifiers format::, FormatStyle::, and LanguageKind::. --- clang/include/clang/Format/Format.h | 14 ++-- clang/lib/Format/BreakableToken.cpp | 6 +- clang/lib/Format/ContinuationIndenter.cpp | 14 ++-- clang/lib/Format/Format.cpp | 13 ++-- clang/lib/Format/FormatTokenLexer.cpp | 10 +-- clang/lib/Format/TokenAnnotator.cpp | 78 +++++++++------------ clang/lib/Format/UnwrappedLineFormatter.cpp | 8 +-- clang/lib/Format/UnwrappedLineParser.cpp | 47 +++++-------- clang/unittests/Format/CleanupTest.cpp | 4 +- clang/unittests/Format/FormatTest.cpp | 7 +- 10 files changed, 86 insertions(+), 115 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index cea5e257659d..f6ceef08b46d 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 @@ private: /// 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 bde775787699..5317c05f3a46 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 1969f4297b21..39eb706fc2fd 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 b90bd8276e1e..3802766b652d 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 014b10b206d9..5c4e1f814d9b 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 d184c23123c3..82dc403538c4 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -183,10 +183,8 @@ private: 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 @@ private: // 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 @@ private: 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 @@ private: 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 @@ private: 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 @@ private: // 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 @@ private: 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 @@ public: // 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 @@ private: } 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 @@ private: // 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 @@ private: 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 @@ private: } } 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 @@ private: } } } 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 @@ private: 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 @@ private: // 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 @@ private: 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 @@ private: 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 @@ private: 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 @@ private: // 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 @@ public: 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 @@ private: } 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 @@ private: 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()) { if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws)) return 1; if (Right.is(Keywords.kw_implements)) @@ -4504,7 +4491,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, Right.MatchingParen->is(TT_CastRParen)) { return true; } - if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java) + if (Left.is(Keywords.kw_assert) && Style.isJava()) return true; if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty && Left.is(tok::objc_property)) { @@ -4597,7 +4584,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, if (Left.is(tok::coloncolon)) return false; if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) { - if (Style.Language == FormatStyle::LK_TextProto || + if (Style.isTextProto() || (Style.Language == FormatStyle::LK_Proto && (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) { // Format empty list as `<>`. @@ -5251,7 +5238,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) { return true; // "x! as string", "x! in y" } - } else if (Style.Language == FormatStyle::LK_Java) { + } else if (Style.isJava()) { if (Left.is(TT_CaseLabelArrow) || Right.is(TT_CaseLabelArrow)) return true; if (Left.is(tok::r_square) && Right.is(tok::l_brace)) @@ -5498,7 +5485,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, }; if (Left.is(tok::greater) && Right.is(tok::greater)) { - if (Style.Language == FormatStyle::LK_TextProto || + if (Style.isTextProto() || (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) { return !Style.Cpp11BracedListStyle; } @@ -5515,7 +5502,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, Right.getPrecedence() == prec::Assignment) { return false; } - if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) && + if (Style.isJava() && Right.is(tok::coloncolon) && (Left.is(tok::identifier) || Left.is(tok::kw_this))) { return false; } @@ -5703,7 +5690,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, Style.AllowShortFunctionsOnASingleLine & FormatStyle::SFS_InlineOnly); } - } else if (Style.Language == FormatStyle::LK_Java) { + } else if (Style.isJava()) { if (Right.is(tok::plus) && Left.is(tok::string_literal) && AfterRight && AfterRight->is(tok::string_literal)) { return true; @@ -5729,8 +5716,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left)) return true; } else if (Style.BreakAdjacentStringLiterals && - (IsCpp || Style.isProto() || - Style.Language == FormatStyle::LK_TableGen)) { + (IsCpp || Style.isProto() || Style.isTableGen())) { if (Left.isStringLiteral() && Right.isStringLiteral()) return true; } @@ -5974,7 +5960,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, } // Put multiple Java annotation on a new line. - if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) && + if ((Style.isJava() || Style.isJavaScript()) && Left.is(TT_LeadingJavaAnnotation) && Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) { @@ -6086,7 +6072,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, // Keep nullable operators attached to their identifiers. if (Right.is(TT_CSharpNullable)) return false; - } else if (Style.Language == FormatStyle::LK_Java) { + } else if (Style.isJava()) { if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends, Keywords.kw_implements)) { return false; diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 40cedaaa84d0..617d46ad281d 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -112,10 +112,8 @@ private: /// For example, 'public:' labels in classes are offset by 1 or 2 /// characters to the left from their level. int getIndentOffset(const AnnotatedLine &Line) { - if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() || - Style.isCSharp()) { + if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) return 0; - } const auto &RootToken = *Line.First; if (Line.Type == LT_AccessModifier || RootToken.isAccessSpecifier(/*ColonRequired=*/false) || @@ -800,10 +798,8 @@ private: // Don't merge ObjC @ keywords and methods. // FIXME: If an option to allow short exception handling clauses on a single // line is added, change this to not return for @try and friends. - if (Style.Language != FormatStyle::LK_Java && - Line.First->isOneOf(tok::at, tok::minus, tok::plus)) { + if (!Style.isJava() && Line.First->isOneOf(tok::at, tok::minus, tok::plus)) return 0; - } // Check that the current line allows merging. This depends on whether we // are in a control flow statements as well as several style flags. diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 60f4f30623ba..5fe65cb9a47e 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -270,7 +270,7 @@ void UnwrappedLineParser::parseFile() { bool MustBeDeclaration = !Line->InPPDirective && !Style.isJavaScript(); ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, MustBeDeclaration); - if (Style.Language == FormatStyle::LK_TextProto) + if (Style.isTextProto()) parseBracedList(); else parseLevel(); @@ -284,10 +284,8 @@ void UnwrappedLineParser::parseFile() { // // endfile comment // do not have a chance to be put on a line of their own until this point. // Here we add this newline before end-of-file comments. - if (Style.Language == FormatStyle::LK_TextProto && - !CommentsBeforeNextToken.empty()) { + if (Style.isTextProto() && !CommentsBeforeNextToken.empty()) addUnwrappedLine(); - } flushComments(true); addUnwrappedLine(); } @@ -1422,8 +1420,7 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() { void UnwrappedLineParser::parseStructuralElement( const FormatToken *OpeningBrace, IfStmtKind *IfKind, FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) { - if (Style.Language == FormatStyle::LK_TableGen && - FormatTok->is(tok::pp_include)) { + if (Style.isTableGen() && FormatTok->is(tok::pp_include)) { nextToken(); if (FormatTok->is(tok::string_literal)) nextToken(); @@ -1465,12 +1462,10 @@ 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()) { + if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) nextToken(); - } else { + else parseAccessSpecifier(); - } return; } switch (FormatTok->Tok.getKind()) { @@ -1720,8 +1715,7 @@ void UnwrappedLineParser::parseStructuralElement( parseBracedList(); break; } - if (Style.Language == FormatStyle::LK_Java && - FormatTok->is(Keywords.kw_interface)) { + if (Style.isJava() && FormatTok->is(Keywords.kw_interface)) { nextToken(); break; } @@ -1841,10 +1835,8 @@ void UnwrappedLineParser::parseStructuralElement( case tok::period: nextToken(); // In Java, classes have an implicit static member "class". - if (Style.Language == FormatStyle::LK_Java && FormatTok && - FormatTok->is(tok::kw_class)) { + if (Style.isJava() && FormatTok && FormatTok->is(tok::kw_class)) nextToken(); - } if (Style.isJavaScript() && FormatTok && FormatTok->Tok.getIdentifierInfo()) { // JavaScript only has pseudo keywords, all keywords are allowed to @@ -1907,7 +1899,7 @@ void UnwrappedLineParser::parseStructuralElement( // structural element. // FIXME: Figure out cases where this is not true, and add projections // for them (the one we know is missing are lambdas). - if (Style.Language == FormatStyle::LK_Java && + if (Style.isJava() && Line->Tokens.front().Tok->is(Keywords.kw_synchronized)) { // If necessary, we could set the type to something different than // TT_FunctionLBrace. @@ -1963,7 +1955,7 @@ void UnwrappedLineParser::parseStructuralElement( tryToParseJSFunction(); break; } - if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) && + if ((Style.isJavaScript() || Style.isJava()) && FormatTok->is(Keywords.kw_interface)) { if (Style.isJavaScript()) { // In JavaScript/TypeScript, "interface" can be used as a standalone @@ -2091,7 +2083,7 @@ void UnwrappedLineParser::parseStructuralElement( parseNew(); break; case tok::kw_switch: - if (Style.Language == FormatStyle::LK_Java) + if (Style.isJava()) parseSwitch(/*IsExpr=*/true); else nextToken(); @@ -2570,7 +2562,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) { case tok::l_paren: if (parseParens(AmpAmpTokenType)) SeenEqual = true; - if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) + if (Style.isJava() && FormatTok->is(tok::l_brace)) parseChildBlock(); break; case tok::r_paren: { @@ -2675,7 +2667,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) { nextToken(); break; case tok::kw_switch: - if (Style.Language == FormatStyle::LK_Java) + if (Style.isJava()) parseSwitch(/*IsExpr=*/true); else nextToken(); @@ -3052,7 +3044,7 @@ void UnwrappedLineParser::parseTryCatch() { } } // Parse try with resource. - if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) + if (Style.isJava() && FormatTok->is(tok::l_paren)) parseParens(); keepAncestorBraces(); @@ -3081,7 +3073,7 @@ void UnwrappedLineParser::parseTryCatch() { if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, tok::kw___finally, tok::objc_catch, tok::objc_finally) || - ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) && + ((Style.isJava() || Style.isJavaScript()) && FormatTok->is(Keywords.kw_finally)))) { break; } @@ -3197,7 +3189,7 @@ void UnwrappedLineParser::parseNew() { } while (!eof()); } - if (Style.Language != FormatStyle::LK_Java) + if (!Style.isJava()) return; // In Java, we can parse everything up to the parens, which aren't optional. @@ -3359,7 +3351,7 @@ void UnwrappedLineParser::parseCaseLabel() { FormatTok->setFinalizedType(TT_CaseLabelColon); break; } - if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::arrow)) { + if (Style.isJava() && FormatTok->is(tok::arrow)) { FormatTok->setFinalizedType(TT_CaseLabelArrow); Case->setFinalizedType(TT_SwitchExpressionLabel); break; @@ -3811,7 +3803,7 @@ bool UnwrappedLineParser::parseEnum() { FormatTok->setFinalizedType(TT_EnumLBrace); FormatTok->setBlockKind(BK_Block); - if (Style.Language == FormatStyle::LK_Java) { + if (Style.isJava()) { // Java enums are different. parseJavaEnumBody(); return true; @@ -3852,8 +3844,7 @@ bool UnwrappedLineParser::parseStructLike() { // record declaration or definition can start a structural element. parseRecord(); // This does not apply to Java, JavaScript and C#. - if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() || - Style.isCSharp()) { + if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) { if (FormatTok->is(tok::semi)) nextToken(); addUnwrappedLine(); @@ -3989,7 +3980,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, tok::kw_alignas, tok::l_square) || FormatTok->isAttribute() || - ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) && + ((Style.isJava() || Style.isJavaScript()) && FormatTok->isOneOf(tok::period, tok::comma))) { if (Style.isJavaScript() && FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) { diff --git a/clang/unittests/Format/CleanupTest.cpp b/clang/unittests/Format/CleanupTest.cpp index a3801106e1ce..06ec78098ac1 100644 --- a/clang/unittests/Format/CleanupTest.cpp +++ b/clang/unittests/Format/CleanupTest.cpp @@ -423,7 +423,7 @@ TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesGoogleStyle) { tooling::Replacements Replaces = toReplacements({createInsertion("#include <list>"), createInsertion("#include \"x/x.h\"")}); - Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp); + Style = getGoogleStyle(FormatStyle::LK_Cpp); EXPECT_EQ(Expected, apply(Code, Replaces)); } @@ -460,7 +460,7 @@ TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortGoogle) { createInsertion("#include \"b.h\""), createInsertion("#include <vector>"), createInsertion("#include <list>"), createInsertion("#include \"fix.h\"")}); - Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp); + Style = getGoogleStyle(FormatStyle::LK_Cpp); EXPECT_EQ(Expected, formatAndApply(Code, Replaces)); } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index f0e67c604cc4..9dac6d6c5f5b 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -22483,8 +22483,7 @@ TEST_F(FormatTest, UnderstandsPragmas) { "#pragma comment(linker, \\\n" " \"argument\" \\\n" " \"argument\"", - getStyleWithColumns( - getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32)); + getStyleWithColumns(getChromiumStyle(FormatStyle::LK_Cpp), 32)); } TEST_F(FormatTest, UnderstandsPragmaOmpTarget) { @@ -25968,14 +25967,14 @@ TEST_F(FormatTest, GoogleDefaultStyle) { Style); } TEST_F(FormatTest, ChromiumDefaultStyle) { - FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); + FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); verifyFormat("extern \"C\" {\n" "int foo();\n" "}", Style); } TEST_F(FormatTest, MicrosoftDefaultStyle) { - FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); + FormatStyle Style = getMicrosoftStyle(FormatStyle::LK_Cpp); verifyFormat("extern \"C\"\n" "{\n" " int foo();\n"