Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -1653,8 +1653,15 @@ /// (https://developers.google.com/protocol-buffers/). LK_TextProto }; - bool isCpp() const { return Language == LK_Cpp || Language == LK_ObjC; } + bool isCpp() const { return Language == LK_Cpp; } + bool isObjC() const { return Language == LK_ObjC; } + bool isCppOrObjC() const { return isCpp() || isObjC(); } bool isCSharp() const { return Language == LK_CSharp; } + bool isProto() const { return Language == LK_Proto; } + bool isTableGen() const { return Language == LK_TableGen; } + bool isTextProtoBuf() const { return Language == LK_TextProto; } + bool isJava() const { return Language == LK_Java; } + bool isJavaScript() const { return Language == LK_JavaScript; } /// Language, this format style is targeted at. LanguageKind Language; Index: clang/lib/Format/BreakableToken.cpp =================================================================== --- clang/lib/Format/BreakableToken.cpp +++ clang/lib/Format/BreakableToken.cpp @@ -46,7 +46,7 @@ static const char *const KnownTextProtoPrefixes[] = {"//", "#", "##", "###", "####"}; ArrayRef KnownPrefixes(KnownCStylePrefixes); - if (Style.Language == FormatStyle::LK_TextProto) + if (Style.isTextProtoBuf()) KnownPrefixes = KnownTextProtoPrefixes; StringRef LongestPrefix; @@ -97,8 +97,8 @@ // In JavaScript, some @tags can be followed by {, and machinery that parses // these comments will fail to understand the comment if followed by a line // break. So avoid ever breaking before a {. - else if (Style.Language == FormatStyle::LK_JavaScript && - SpaceOffset + 1 < Text.size() && Text[SpaceOffset + 1] == '{') + else if (Style.isJavaScript() && SpaceOffset + 1 < Text.size() && + Text[SpaceOffset + 1] == '{') SpaceOffset = Text.find_last_of(Blanks, SpaceOffset); else break; @@ -427,8 +427,7 @@ IndentAtLineBreak = std::max(IndentAtLineBreak, Decoration.size()); // Detect a multiline jsdoc comment and set DelimitersOnNewline in that case. - if (Style.Language == FormatStyle::LK_JavaScript || - Style.Language == FormatStyle::LK_Java) { + if (Style.isJavaScript() || Style.isJava()) { if ((Lines[0] == "*" || Lines[0].startswith("* ")) && Lines.size() > 1) { // This is a multiline jsdoc comment. DelimitersOnNewline = true; @@ -547,8 +546,7 @@ }; unsigned BreakableBlockComment::getContentIndent(unsigned LineIndex) const { - if (Style.Language != FormatStyle::LK_Java && - Style.Language != FormatStyle::LK_JavaScript) + if (!Style.isJava() && !Style.isJavaScript()) return 0; // The content at LineIndex 0 of a comment like: // /** line 0 */ @@ -761,8 +759,7 @@ Prefix[i] = "///< "; else if (Prefix[i] == "//!<") Prefix[i] = "//!< "; - else if (Prefix[i] == "#" && - Style.Language == FormatStyle::LK_TextProto) + else if (Prefix[i] == "#" && Style.isTextProtoBuf()) Prefix[i] = "# "; } Index: clang/lib/Format/ContinuationIndenter.cpp =================================================================== --- clang/lib/Format/ContinuationIndenter.cpp +++ clang/lib/Format/ContinuationIndenter.cpp @@ -123,7 +123,7 @@ if (Current.is(TT_CtorInitializerComma) && Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) return true; - if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName)) + if (Style.isProto() && Current.is(TT_SelectorName)) return true; return Previous.is(tok::comma) && !Current.isTrailingComment() && ((Previous.isNot(TT_CtorInitializerComma) || @@ -137,8 +137,8 @@ const FormatStyle &Style) { if (LessTok.isNot(tok::less)) return false; - return Style.Language == FormatStyle::LK_TextProto || - (Style.Language == FormatStyle::LK_Proto && + return Style.isTextProtoBuf() || + (Style.isProto() && (LessTok.NestingLevel > 0 || (LessTok.Previous && LessTok.Previous->is(tok::equal)))); } @@ -261,7 +261,7 @@ State.LowestLevelOnLine = 0; State.IgnoreStackForComparison = false; - if (Style.Language == FormatStyle::LK_TextProto) { + if (Style.isTextProtoBuf()) { // We need this in order to deal with the bin packing of text fields at // global scope. State.Stack.back().AvoidBinPacking = true; @@ -341,8 +341,7 @@ return true; if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) return true; - if (Style.Language == FormatStyle::LK_ObjC && - Style.ObjCBreakBeforeNestedBlockParam && + if (Style.isObjC() && Style.ObjCBreakBeforeNestedBlockParam && Current.ObjCSelectorNameParts > 1 && Current.startsSequence(TT_SelectorName, tok::colon, tok::caret)) { return true; @@ -354,7 +353,7 @@ return false; if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) || (Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) && - Style.isCpp() && + Style.isCppOrObjC() && // FIXME: This is a temporary workaround for the case where clang-format // sets BreakBeforeParameter to avoid bin packing and this creates a // completely unnecessary line break after a template type that isn't @@ -418,8 +417,7 @@ // ... // }.bind(...)); // FIXME: We should find a more generic solution to this problem. - !(State.Column <= NewLineColumn && - Style.Language == FormatStyle::LK_JavaScript) && + !(State.Column <= NewLineColumn && Style.isJavaScript()) && !(Previous.closesScopeAfterBlock() && State.Column <= NewLineColumn)) return true; @@ -658,7 +656,7 @@ State.Column > getNewLineColumn(State)) State.Stack.back().ContainsUnwrappedBuilder = true; - if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java) + if (Current.is(TT_LambdaArrow) && Style.isJava()) State.Stack.back().NoLineBreak = true; if (Current.isMemberAccess() && Previous.is(tok::r_paren) && (Previous.MatchingParen && @@ -794,9 +792,8 @@ // is common and should be formatted like a free-standing function. The same // goes for wrapping before the lambda return type arrow. if (!Current.is(TT_LambdaArrow) && - (Style.Language != FormatStyle::LK_JavaScript || - Current.NestingLevel != 0 || !PreviousNonComment || - !PreviousNonComment->is(tok::equal) || + (!Style.isJavaScript() || Current.NestingLevel != 0 || + !PreviousNonComment || !PreviousNonComment->is(tok::equal) || !Current.isOneOf(Keywords.kw_async, Keywords.kw_function))) State.Stack.back().NestedBlockIndent = State.Column; @@ -886,10 +883,11 @@ // Any break on this level means that the parent level has been broken // and we need to avoid bin packing there. bool NestedBlockSpecialCase = - (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 && + (!Style.isCppOrObjC() && Current.is(tok::r_brace) && + State.Stack.size() > 1 && State.Stack[State.Stack.size() - 2].NestedBlockInlined) || - (Style.Language == FormatStyle::LK_ObjC && Current.is(tok::r_brace) && - State.Stack.size() > 1 && !Style.ObjCBreakBeforeNestedBlockParam); + (Style.isObjC() && Current.is(tok::r_brace) && State.Stack.size() > 1 && + !Style.ObjCBreakBeforeNestedBlockParam); if (!NestedBlockSpecialCase) for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) State.Stack[i].BreakBeforeParameter = true; @@ -962,7 +960,7 @@ 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(State.Stack.back().LastSpace, State.Stack.back().Indent + Style.ContinuationIndentWidth); @@ -977,8 +975,7 @@ : State.Stack.back().Indent; if ((Current.isOneOf(tok::r_brace, tok::r_square) || (Current.is(tok::greater) && - (Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto))) && + (Style.isProto() || Style.isTextProtoBuf()))) && State.Stack.size() > 1) { if (Current.closesBlockOrBlockTypeList(Style)) return State.Stack[State.Stack.size() - 2].NestedBlockIndent; @@ -1010,8 +1007,7 @@ return State.Stack[State.Stack.size() - 2].LastSpace; if (Current.is(tok::identifier) && Current.Next && (Current.Next->is(TT_DictLiteral) || - ((Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) && + ((Style.isProto() || Style.isTextProtoBuf()) && Current.Next->isOneOf(tok::less, tok::l_brace)))) return State.Stack.back().Indent; if (NextNonComment->is(TT_ObjCStringLiteral) && @@ -1339,7 +1335,7 @@ (Style.AlignOperands != FormatStyle::OAS_DontAlign || *I < prec::Assignment) && (!Previous || Previous->isNot(tok::kw_return) || - (Style.Language != FormatStyle::LK_Java && *I > 0)) && + (!Style.isJava() && *I > 0)) && (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign || *I != prec::Comma || Current.NestingLevel == 0)) { NewParenState.Indent = @@ -1439,8 +1435,7 @@ Current.MatchingParen->Previous && Current.MatchingParen->Previous->is(tok::comma); AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) || - Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto || + Style.isProto() || Style.isTextProtoBuf() || !Style.BinPackArguments || (NextNoComment && NextNoComment->isOneOf(TT_DesignatedInitializerPeriod, @@ -1480,7 +1475,7 @@ AvoidBinPacking = (State.Stack.back().IsCSharpGenericTypeConstraint) || - (Style.Language == FormatStyle::LK_JavaScript && EndsInComma) || + (Style.isJavaScript() && EndsInComma) || (State.Line->MustBeDeclaration && !BinPackDeclaration) || (!State.Line->MustBeDeclaration && !Style.BinPackArguments) || (Style.ExperimentalAutoDetectBinPacking && @@ -1510,7 +1505,7 @@ } } - if (Style.Language == FormatStyle::LK_JavaScript && EndsInComma) + if (Style.isJavaScript() && EndsInComma) BreakBeforeParameter = true; } // Generally inherit NoLineBreak from the current scope to nested scope. @@ -1887,8 +1882,7 @@ // FIXME: String literal breaking is currently disabled for C#, Java and // JavaScript, as it requires strings to be merged using "+" which we // don't support. - if (Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp() || + if (Style.isJava() || Style.isJavaScript() || Style.isCSharp() || !Style.BreakStringLiterals || !AllowBreak) return nullptr; Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -1859,7 +1859,7 @@ analyze(TokenAnnotator &Annotator, SmallVectorImpl &AnnotatedLines, FormatTokenLexer &Tokens) override { - assert(Style.Language == FormatStyle::LK_Cpp); + assert(Style.isCpp()); IsObjC = guessIsObjC(Env.getSourceManager(), AnnotatedLines, Tokens.getKeywords()); tooling::Replacements Result; @@ -2393,12 +2393,11 @@ return Replaces; if (isLikelyXml(Code)) return Replaces; - if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript && - isMpegTS(Code)) + if (Style.isJavaScript() && isMpegTS(Code)) return Replaces; - if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript) + if (Style.isJavaScript()) return sortJavaScriptImports(Style, Code, Ranges, FileName); - if (Style.Language == FormatStyle::LanguageKind::LK_Java) + if (Style.isJava()) return sortJavaImports(Style, Code, Ranges, FileName, Replaces); sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor); return Replaces; @@ -2465,7 +2464,7 @@ tooling::Replacements fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces, const FormatStyle &Style) { - if (!Style.isCpp()) + if (!Style.isCppOrObjC()) return Replaces; tooling::Replacements HeaderInsertions; @@ -2561,7 +2560,7 @@ 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}; typedef std::function( @@ -2569,7 +2568,7 @@ AnalyzerPass; SmallVector Passes; - if (Style.Language == FormatStyle::LK_Cpp) { + if (Style.isCpp()) { if (Style.FixNamespaceComments) Passes.emplace_back([&](const Environment &Env) { return NamespaceEndCommentsFixer(Env, Expanded).process(); @@ -2581,8 +2580,7 @@ }); } - if (Style.Language == FormatStyle::LK_JavaScript && - Style.JavaScriptQuotes != FormatStyle::JSQS_Leave) + if (Style.isJavaScript() && Style.JavaScriptQuotes != FormatStyle::JSQS_Leave) Passes.emplace_back([&](const Environment &Env) { return JavaScriptRequoter(Env, Expanded).process(); }); @@ -2591,7 +2589,7 @@ return Formatter(Env, Expanded, Status).process(); }); - if (Style.Language == FormatStyle::LK_JavaScript && + if (Style.isJavaScript() && Style.InsertTrailingCommas == FormatStyle::TCS_Wrapped) Passes.emplace_back([&](const Environment &Env) { return TrailingCommaInserter(Env, Expanded).process(); @@ -2639,7 +2637,7 @@ ArrayRef Ranges, StringRef FileName) { // cleanups only apply to C++ (they mostly concern ctor commas etc.) - if (Style.Language != FormatStyle::LK_Cpp) + if (!Style.isCpp()) return tooling::Replacements(); return Cleaner(Environment(Code, FileName, Ranges), Style).process().first; } @@ -2688,7 +2686,7 @@ LangOpts.Char8 = LexingStd >= FormatStyle::LS_Cpp20; LangOpts.LineComment = 1; - bool AlternativeOperators = Style.isCpp(); + bool AlternativeOperators = Style.isCppOrObjC(); LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; LangOpts.Bool = 1; LangOpts.ObjC = 1; Index: clang/lib/Format/FormatToken.h =================================================================== --- clang/lib/Format/FormatToken.h +++ clang/lib/Format/FormatToken.h @@ -532,14 +532,13 @@ (is(tok::l_brace) && (BlockKind == BK_Block || is(TT_DictLiteral) || (!Style.Cpp11BracedListStyle && NestingLevel == 0))) || - (is(tok::less) && (Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto)); + (is(tok::less) && (Style.isProto() || Style.isTextProtoBuf())); } /// Returns whether the token is the left square bracket of a C++ /// structured binding declaration. bool isCppStructuredBinding(const FormatStyle &Style) const { - if (!Style.isCpp() || isNot(tok::l_square)) + if (!Style.isCppOrObjC() || isNot(tok::l_square)) return false; const FormatToken *T = this; do { Index: clang/lib/Format/FormatTokenLexer.cpp =================================================================== --- clang/lib/Format/FormatTokenLexer.cpp +++ clang/lib/Format/FormatTokenLexer.cpp @@ -50,11 +50,11 @@ assert(FirstInLineIndex == 0); do { Tokens.push_back(getNextToken()); - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { tryParseJSRegexLiteral(); handleTemplateStrings(); } - if (Style.Language == FormatStyle::LK_TextProto) + if (Style.isTextProtoBuf()) tryParsePythonComment(); tryMergePreviousTokens(); if (Style.isCSharp()) @@ -96,7 +96,7 @@ if (tryMergeNSStringLiteral()) return; - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { static const tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal}; static const tok::TokenKind JSNotIdentity[] = {tok::exclaimequal, tok::equal}; @@ -141,7 +141,7 @@ 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)) @@ -877,9 +877,8 @@ // 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.Language == FormatStyle::LK_JavaScript || - Style.Language == FormatStyle::LK_Java) && - FormatTok->is(tok::comment) && FormatTok->TokenText.startswith("//")) { + if ((Style.isJavaScript() || Style.isJava()) && FormatTok->is(tok::comment) && + FormatTok->TokenText.startswith("//")) { size_t BackslashPos = FormatTok->TokenText.find('\\'); while (BackslashPos != StringRef::npos) { if (BackslashPos + 1 < FormatTok->TokenText.size() && @@ -934,12 +933,12 @@ 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); FormatTok->Tok.setIdentifierInfo(nullptr); - } else if (Style.Language == FormatStyle::LK_JavaScript && + } else if (Style.isJavaScript() && FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_operator)) { FormatTok->Tok.setKind(tok::identifier); @@ -981,7 +980,7 @@ Column = FormatTok->LastLineColumnWidth; } - if (Style.isCpp()) { + if (Style.isCppOrObjC()) { auto it = Macros.find(FormatTok->Tok.getIdentifierInfo()); if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() && Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() == @@ -1010,15 +1009,12 @@ if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') { Tok.Tok.setKind(tok::string_literal); Tok.IsUnterminatedLiteral = true; - } else if (Style.Language == FormatStyle::LK_JavaScript && - Tok.TokenText == "''") { + } else if (Style.isJavaScript() && Tok.TokenText == "''") { Tok.Tok.setKind(tok::string_literal); } } - if ((Style.Language == FormatStyle::LK_JavaScript || - Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) && + if ((Style.isJavaScript() || Style.isProto() || Style.isTextProtoBuf()) && Tok.is(tok::char_constant)) { Tok.Tok.setKind(tok::string_literal); } Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -102,8 +102,7 @@ Contexts.back().InTemplateArgument = Left->Previous && Left->Previous->Tok.isNot(tok::kw_template); - if (Style.Language == FormatStyle::LK_Java && - CurrentToken->is(tok::question)) + if (Style.isJava() && CurrentToken->is(tok::question)) next(); while (CurrentToken) { @@ -115,8 +114,8 @@ // msg < item: data > // msg: < item: data > // In TT_TextProto, map does not occur. - if (Style.Language == FormatStyle::LK_TextProto || - (Style.Language == FormatStyle::LK_Proto && Left->Previous && + if (Style.isTextProtoBuf() || + (Style.isProto() && Left->Previous && Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) CurrentToken->setType(TT_DictLiteral); else @@ -124,15 +123,13 @@ next(); return true; } - if (CurrentToken->is(tok::question) && - Style.Language == FormatStyle::LK_Java) { + if (CurrentToken->is(tok::question) && Style.isJava()) { next(); continue; } if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) || (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext && - !Style.isCSharp() && Style.Language != FormatStyle::LK_Proto && - Style.Language != FormatStyle::LK_TextProto)) + !Style.isCSharp() && !Style.isProto() && !Style.isTextProtoBuf())) return false; // If a && or || is found and interpreted as a binary operator, this set // of angles is likely part of something like "a < b && c > d". If the @@ -146,7 +143,7 @@ !Line.startsWith(tok::kw_template)) return false; updateParameterCount(Left, CurrentToken); - if (Style.Language == FormatStyle::LK_Proto) { + if (Style.isProto()) { if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) { if (CurrentToken->is(tok::colon) || (CurrentToken->isOneOf(tok::l_brace, tok::less) && @@ -182,7 +179,7 @@ if (Left->is(TT_OverloadedOperatorLParen)) { Contexts.back().IsExpression = false; - } else if (Style.Language == FormatStyle::LK_JavaScript && + } else if (Style.isJavaScript() && (Line.startsWith(Keywords.kw_type, tok::identifier) || Line.startsWith(tok::kw_export, Keywords.kw_type, tok::identifier))) { @@ -197,13 +194,13 @@ Left->Previous->is(TT_BinaryOperator))) { // static_assert, if and while usually contain expressions. Contexts.back().IsExpression = true; - } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && + } else if (Style.isJavaScript() && Left->Previous && (Left->Previous->is(Keywords.kw_function) || (Left->Previous->endsSequence(tok::identifier, Keywords.kw_function)))) { // function(...) or function f(...) Contexts.back().IsExpression = false; - } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && + } else if (Style.isJavaScript() && Left->Previous && Left->Previous->is(TT_JsTypeColon)) { // let x: (SomeType); Contexts.back().IsExpression = false; @@ -417,7 +414,8 @@ } bool isCpp11AttributeSpecifier(const FormatToken &Tok) { - if (!Style.isCpp() || !Tok.startsSequence(tok::l_square, tok::l_square)) + if (!Style.isCppOrObjC() || + !Tok.startsSequence(tok::l_square, tok::l_square)) return false; // The first square bracket is part of an ObjC array literal if (Tok.Previous && Tok.Previous->is(tok::at)) { @@ -463,7 +461,7 @@ // In C++, this can happen either in array of templates (foo[10]) // or when array is a nested template type (unique_ptr[]>). bool CppArrayTemplates = - Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) && + Style.isCppOrObjC() && Parent && Parent->is(TT_TemplateCloser) && (Contexts.back().CanBeExpression || Contexts.back().IsExpression || Contexts.back().InTemplateArgument); @@ -479,7 +477,7 @@ bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style); bool StartsObjCMethodExpr = !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates && - Style.isCpp() && !IsCpp11AttributeSpecifier && + Style.isCppOrObjC() && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier && Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) && !CurrentToken->isOneOf(tok::l_brace, tok::r_square) && @@ -503,20 +501,20 @@ Left->setType(TT_InlineASMSymbolicNameLSquare); } else if (IsCpp11AttributeSpecifier) { Left->setType(TT_AttributeSquare); - } else if (Style.Language == FormatStyle::LK_JavaScript && Parent && + } else if (Style.isJavaScript() && Parent && Contexts.back().ContextKind == tok::l_brace && Parent->isOneOf(tok::l_brace, tok::comma)) { Left->setType(TT_JsComputedPropertyName); - } else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace && - Parent && Parent->isOneOf(tok::l_brace, tok::comma)) { + } else if (Style.isCppOrObjC() && + Contexts.back().ContextKind == tok::l_brace && Parent && + Parent->isOneOf(tok::l_brace, tok::comma)) { Left->setType(TT_DesignatedInitializerLSquare); } else if (IsCSharpAttributeSpecifier) { Left->setType(TT_AttributeSquare); } else if (CurrentToken->is(tok::r_square) && Parent && Parent->is(TT_TemplateCloser)) { Left->setType(TT_ArraySubscriptLSquare); - } else if (Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) { + } else if (Style.isProto() || Style.isTextProtoBuf()) { // Square braces in LK_Proto can either be message field attributes: // // optional Aaa aaa = 1 [ @@ -567,8 +565,7 @@ ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease); Contexts.back().IsExpression = true; - if (Style.Language == FormatStyle::LK_JavaScript && Parent && - Parent->is(TT_JsTypeColon)) + if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon)) Contexts.back().IsExpression = false; Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr; @@ -680,7 +677,7 @@ Contexts.back().ColonIsDictLiteral = true; if (Left->BlockKind == BK_BracedInit) Contexts.back().IsExpression = true; - if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && + if (Style.isJavaScript() && Left->Previous && Left->Previous->is(TT_JsTypeColon)) Contexts.back().IsExpression = false; @@ -699,20 +696,17 @@ if (Previous->is(TT_JsTypeOptionalQuestion)) Previous = Previous->getPreviousNonComment(); if ((CurrentToken->is(tok::colon) && - (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) || - Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) { + (!Contexts.back().ColonIsDictLiteral || !Style.isCppOrObjC())) || + Style.isProto() || Style.isTextProtoBuf()) { Left->setType(TT_DictLiteral); if (Previous->Tok.getIdentifierInfo() || Previous->is(tok::string_literal)) Previous->setType(TT_SelectorName); } - if (CurrentToken->is(tok::colon) || - Style.Language == FormatStyle::LK_JavaScript) + if (CurrentToken->is(tok::colon) || Style.isJavaScript()) Left->setType(TT_DictLiteral); } - if (CurrentToken->is(tok::comma) && - Style.Language == FormatStyle::LK_JavaScript) + if (CurrentToken->is(tok::comma) && Style.isJavaScript()) Left->setType(TT_DictLiteral); if (!consumeToken()) return false; @@ -776,7 +770,7 @@ if (!Tok->Previous) return false; // Colons from ?: are handled in parseConditional(). - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { if (Contexts.back().ColonIsForRangeExpr || // colon in for loop (Contexts.size() == 1 && // switch/case labels !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) || @@ -800,11 +794,10 @@ break; } } - if (Contexts.back().ColonIsDictLiteral || - Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) { + if (Contexts.back().ColonIsDictLiteral || Style.isProto() || + Style.isTextProtoBuf()) { Tok->setType(TT_DictLiteral); - if (Style.Language == FormatStyle::LK_TextProto) { + if (Style.isTextProtoBuf()) { if (FormatToken *Previous = Tok->getPreviousNonComment()) Previous->setType(TT_SelectorName); } @@ -867,8 +860,7 @@ case tok::amp: // | and & in declarations/type expressions represent union and // intersection types, respectively. - if (Style.Language == FormatStyle::LK_JavaScript && - !Contexts.back().IsExpression) + if (Style.isJavaScript() && !Contexts.back().IsExpression) Tok->setType(TT_JsTypeOperator); break; case tok::kw_if: @@ -883,7 +875,7 @@ } break; case tok::kw_for: - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { // x.for and {for: ...} if ((Tok->Previous && Tok->Previous->is(tok::period)) || (Tok->Next && Tok->Next->is(tok::colon))) @@ -924,7 +916,7 @@ return false; break; case tok::l_brace: - if (Style.Language == FormatStyle::LK_TextProto) { + if (Style.isTextProtoBuf()) { FormatToken *Previous = Tok->getPreviousNonComment(); if (Previous && Previous->getType() != TT_DictLiteral) Previous->setType(TT_SelectorName); @@ -940,8 +932,8 @@ // msg < item: data > // msg: < item: data > // In TT_TextProto, map does not occur. - if (Style.Language == FormatStyle::LK_TextProto || - (Style.Language == FormatStyle::LK_Proto && Tok->Previous && + if (Style.isTextProtoBuf() || + (Style.isProto() && Tok->Previous && Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) { Tok->setType(TT_DictLiteral); FormatToken *Previous = Tok->getPreviousNonComment(); @@ -964,14 +956,13 @@ return false; break; case tok::greater: - if (Style.Language != FormatStyle::LK_TextProto) + if (!Style.isTextProtoBuf()) Tok->setType(TT_BinaryOperator); if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser)) Tok->SpacesRequiredBefore = 1; break; case tok::kw_operator: - if (Style.Language == FormatStyle::LK_TextProto || - Style.Language == FormatStyle::LK_Proto) + if (Style.isTextProtoBuf() || Style.isProto()) break; while (CurrentToken && !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) { @@ -997,7 +988,7 @@ } if (Tok->isOneOf(TT_CSharpNullConditional, TT_CSharpNullCoalescing)) break; - if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next && + if (Style.isJavaScript() && Tok->Next && Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren, tok::r_brace)) { // Question marks before semicolons, colons, etc. indicate optional @@ -1010,7 +1001,7 @@ // Declarations cannot be conditional expressions, this can only be part // of a type declaration. if (Line.MustBeDeclaration && !Contexts.back().IsExpression && - Style.Language == FormatStyle::LK_JavaScript) + Style.isJavaScript()) break; if (Style.isCSharp()) { // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be @@ -1143,7 +1134,7 @@ if (!CurrentToken) return Type; - if (Style.Language == FormatStyle::LK_JavaScript && IsFirstToken) { + if (Style.isJavaScript() && IsFirstToken) { // JavaScript files can contain shebang lines of the form: // #!/usr/bin/env node // Treat these like C++ #include directives. @@ -1211,8 +1202,7 @@ // 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)) || (Info && Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next && CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier, @@ -1231,7 +1221,7 @@ // In .proto files, top-level options and package statements are very // similar to import statements and should not be line-wrapped. - if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 && + if (Style.isProto() && Line.Level == 0 && CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) { next(); if (CurrentToken && CurrentToken->is(tok::identifier)) { @@ -1245,14 +1235,13 @@ bool ImportStatement = false; // import {...} from '...'; - if (Style.Language == FormatStyle::LK_JavaScript && - CurrentToken->is(Keywords.kw_import)) + if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import)) ImportStatement = true; while (CurrentToken) { if (CurrentToken->is(tok::kw_virtual)) KeywordVirtualFound = true; - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { // export {...} from '...'; // An export followed by "from 'some string';" is a re-export from // another module identified by a URI and is treated as a @@ -1377,7 +1366,7 @@ !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) && // Type aliases use `type X = ...;` in TypeScript and can be exported // using `export type ...`. - !(Style.Language == FormatStyle::LK_JavaScript && + !(Style.isJavaScript() && (Line.startsWith(Keywords.kw_type, tok::identifier) || Line.startsWith(tok::kw_export, Keywords.kw_type, tok::identifier))) && @@ -1409,7 +1398,7 @@ } else if (Current.is(TT_TrailingReturnArrow)) { Contexts.back().IsExpression = false; } else if (Current.is(TT_LambdaArrow) || Current.is(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; @@ -1519,7 +1508,7 @@ } } - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { if (Current.is(tok::exclaim)) { if (Current.Previous && (Keywords.IsJavaScriptIdentifier( @@ -1555,8 +1544,7 @@ 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) && AutoFound && Line.MustBeDeclaration && Current.NestingLevel == 0 && @@ -1581,8 +1569,8 @@ } else if (Current.isOneOf(tok::exclaim, tok::tilde)) { Current.setType(TT_UnaryOperator); } else if (Current.is(tok::question)) { - if (Style.Language == FormatStyle::LK_JavaScript && - Line.MustBeDeclaration && !Contexts.back().IsExpression) { + if (Style.isJavaScript() && Line.MustBeDeclaration && + !Contexts.back().IsExpression) { // In JavaScript, `interface X { foo?(): bar; }` is an optional method // on the interface, not a ternary expression. Current.setType(TT_JsTypeOptionalQuestion); @@ -1591,8 +1579,7 @@ } } else if (Current.isBinaryOperator() && (!Current.Previous || Current.Previous->isNot(tok::l_square)) && - (!Current.is(tok::greater) && - Style.Language != FormatStyle::LK_TextProto)) { + (!Current.is(tok::greater) && !Style.isTextProtoBuf())) { Current.setType(TT_BinaryOperator); } else if (Current.is(tok::comment)) { if (Current.TokenText.startswith("/*")) { @@ -1625,9 +1612,8 @@ Current.setType(TT_FunctionAnnotationRParen); } } - } else if (Current.is(tok::at) && Current.Next && - Style.Language != FormatStyle::LK_JavaScript && - Style.Language != FormatStyle::LK_Java) { + } else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() && + !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()) { @@ -1647,7 +1633,7 @@ 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()); @@ -1672,9 +1658,7 @@ // 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.Language == FormatStyle::LK_JavaScript) && - 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; @@ -1703,8 +1687,7 @@ if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof, Keywords.kw_as)) return false; - if (Style.Language == FormatStyle::LK_JavaScript && - Tok.Previous->is(Keywords.kw_in)) + if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in)) return false; // Skip "const" as it does not have an influence on whether this is a name. @@ -1739,8 +1722,7 @@ /// Determine whether ')' is ending a cast. bool rParenEndsCast(const FormatToken &Tok) { // C-style casts are only used in C++, C# and Java. - if (!Style.isCSharp() && !Style.isCpp() && - Style.Language != FormatStyle::LK_Java) + if (!Style.isCSharp() && !Style.isCppOrObjC() && !Style.isJava()) return false; // Empty parens aren't casts and there are no casts at the end of the line. @@ -1789,7 +1771,7 @@ // As Java has no function types, a "(" after the ")" likely means that this // is a cast. - if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren)) + if (Style.isJava() && Tok.Next->is(tok::l_paren)) return true; // If a (non-string) literal follows, this is likely a cast. @@ -1849,7 +1831,7 @@ /// Return the type of the given token assuming it is * or &. TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression, bool InTemplateArgument) { - if (Style.Language == FormatStyle::LK_JavaScript) + if (Style.isJavaScript()) return TT_BinaryOperator; // && in C# must be a binary operator. @@ -2083,8 +2065,7 @@ return prec::Conditional; if (NextNonComment && Current->is(TT_SelectorName) && (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) || - ((Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) && + ((Style.isProto() || Style.isTextProtoBuf()) && NextNonComment->is(tok::less)))) return prec::Assignment; if (Current->is(TT_JsComputedPropertyName)) @@ -2099,19 +2080,17 @@ return 0; if (Current->is(TT_RangeBasedForLoopColon)) return prec::Comma; - if ((Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) && + if ((Style.isJava() || Style.isJavaScript()) && Current->is(Keywords.kw_instanceof)) return prec::Relational; - if (Style.Language == FormatStyle::LK_JavaScript && + if (Style.isJavaScript() && Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) return prec::Relational; if (Current->is(TT_BinaryOperator) || Current->is(tok::comma)) return Current->getPrecedence(); if (Current->isOneOf(tok::period, tok::arrow)) return PrecedenceArrowAndPeriod; - if ((Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) && + if ((Style.isJava() || Style.isJavaScript()) && Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements, Keywords.kw_throws)) return 0; @@ -2462,8 +2441,8 @@ // line should not force other line breaks (e.g. when ObjC method // expression is a part of other expression). Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl); - if (Style.Language == FormatStyle::LK_ObjC && - Current->is(TT_SelectorName) && Current->ParameterIndex > 0) { + if (Style.isObjC() && Current->is(TT_SelectorName) && + Current->ParameterIndex > 0) { if (Current->ParameterIndex == 1) Current->SplitPenalty += 5 * Current->BindingStrength; } else { @@ -2516,14 +2495,14 @@ if (Left.is(tok::semi)) return 0; - 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)) return 2; if (Left.is(tok::comma) && Left.NestingLevel == 0) return 3; - } else if (Style.Language == FormatStyle::LK_JavaScript) { + } else if (Style.isJavaScript()) { if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma)) return 100; if (Left.is(TT_JsTypeColon)) @@ -2539,7 +2518,7 @@ if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) return 1; if (Right.is(tok::l_square)) { - if (Style.Language == FormatStyle::LK_Proto) + if (Style.isProto()) return 1; if (Left.is(tok::r_square)) return 200; @@ -2552,8 +2531,7 @@ return 500; } - if (Left.is(tok::coloncolon) || - (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) + if (Left.is(tok::coloncolon) || (Right.is(tok::period) && Style.isProto())) return 500; if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || Right.is(tok::kw_operator)) { @@ -2717,7 +2695,7 @@ const FormatToken &Right) { if (Left.is(tok::kw_return) && Right.isNot(tok::semi)) 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.Tok.getObjCKeywordID() == tok::objc_property) @@ -2766,8 +2744,8 @@ if (Left.is(tok::coloncolon)) return false; if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) { - if (Style.Language == FormatStyle::LK_TextProto || - (Style.Language == FormatStyle::LK_Proto && + if (Style.isTextProtoBuf() || + (Style.isProto() && (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) { // Format empty list as `<>`. if (Left.is(tok::less) && Right.is(tok::greater)) @@ -2856,8 +2834,7 @@ const auto SpaceRequiredForArrayInitializerLSquare = [](const FormatToken &LSquareTok, const FormatStyle &Style) { return Style.SpacesInContainerLiterals || - ((Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) && + ((Style.isProto() || Style.isTextProtoBuf()) && !Style.Cpp11BracedListStyle && LSquareTok.endsSequence(tok::l_square, tok::colon, TT_SelectorName)); @@ -2894,8 +2871,7 @@ return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true; if (Left.is(TT_BlockComment)) // No whitespace in x(/*foo=*/1), except for JavaScript. - return Style.Language == FormatStyle::LK_JavaScript || - !Left.TokenText.endswith("=*/"); + return Style.isJavaScript() || !Left.TokenText.endswith("=*/"); // Space between template and attribute. // e.g. template [[nodiscard]] ... @@ -2973,14 +2949,13 @@ const FormatToken &Left = *Right.Previous; if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo()) return true; // Never ever merge two identifiers. - if (Style.isCpp()) { + if (Style.isCppOrObjC()) { if (Left.is(tok::kw_operator)) return Right.is(tok::coloncolon); if (Right.is(tok::l_brace) && Right.BlockKind == BK_BracedInit && !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) return true; - } else if (Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) { + } else if (Style.isProto() || Style.isTextProtoBuf()) { if (Right.is(tok::period) && Left.isOneOf(Keywords.kw_optional, Keywords.kw_required, Keywords.kw_repeated, Keywords.kw_extend)) @@ -3081,7 +3056,7 @@ if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when)) return Style.SpaceBeforeParens == FormatStyle::SBPO_ControlStatements || spaceRequiredBeforeParens(Right); - } else if (Style.Language == FormatStyle::LK_JavaScript) { + } else if (Style.isJavaScript()) { if (Left.is(TT_JsFatArrow)) return true; // for await ( ... @@ -3172,7 +3147,7 @@ if (Left.is(TT_JsNonNullAssertion) && 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(tok::r_square) && Right.is(tok::l_brace)) return true; if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) @@ -3260,8 +3235,7 @@ Right.isOneOf(TT_BinaryOperator, TT_SelectorName); if (Left.is(tok::greater) && Right.is(tok::greater)) { - if (Style.Language == FormatStyle::LK_TextProto || - (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) + if (Style.isTextProtoBuf() || (Style.isProto() && Left.is(TT_DictLiteral))) return !Style.Cpp11BracedListStyle; return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) && (Style.Standard < FormatStyle::LS_Cpp11 || Style.SpacesInAngles); @@ -3273,7 +3247,7 @@ if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) && 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; if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) @@ -3386,7 +3360,7 @@ return false; if (Right.is(TT_CSharpGenericTypeConstraint)) return true; - } else if (Style.Language == FormatStyle::LK_JavaScript) { + } else if (Style.isJavaScript()) { // FIXME: This might apply to other languages and token kinds. if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous && Left.Previous->is(tok::string_literal)) @@ -3436,15 +3410,12 @@ (Left.NestingLevel == 0 && Line.Level == 0 && 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) && Right.Next && Right.Next->is(tok::string_literal)) return true; - } else if (Style.Language == FormatStyle::LK_Cpp || - Style.Language == FormatStyle::LK_ObjC || - Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TableGen || - Style.Language == FormatStyle::LK_TextProto) { + } else if (Style.isCppOrObjC() || Style.isProto() || Style.isTableGen() || + Style.isTextProtoBuf()) { if (Left.isStringLiteral() && Right.isStringLiteral()) return true; } @@ -3456,15 +3427,13 @@ if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) { const FormatToken *BeforeClosingBrace = nullptr; if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || - (Style.Language == FormatStyle::LK_JavaScript && - Left.is(tok::l_paren))) && + (Style.isJavaScript() && Left.is(tok::l_paren))) && Left.BlockKind != BK_Block && Left.MatchingParen) BeforeClosingBrace = Left.MatchingParen->Previous; else if (Right.MatchingParen && (Right.MatchingParen->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || - (Style.Language == FormatStyle::LK_JavaScript && - Right.MatchingParen->is(tok::l_paren)))) + (Style.isJavaScript() && Right.MatchingParen->is(tok::l_paren)))) BeforeClosingBrace = &Left; if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) || BeforeClosingBrace->isTrailingComment())) @@ -3508,7 +3477,7 @@ if ((Right.Previous->is(tok::l_brace) || (Right.Previous->is(tok::less) && Right.Previous->Previous && Right.Previous->Previous->is(tok::equal))) && - Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) { + Right.NestingLevel == 1 && Style.isProto()) { // Don't put enums or option definitions onto single lines in protocol // buffers. return true; @@ -3546,8 +3515,7 @@ } // Put multiple Java annotation on a new line. - if ((Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) && + 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)) @@ -3583,8 +3551,7 @@ // together. // // We ensure elsewhere that extensions are always on their own line. - if ((Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) && + if ((Style.isProto() || Style.isTextProtoBuf()) && Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) { // Keep `@submessage` together in: // @submessage { key: value } @@ -3644,9 +3611,8 @@ // Deal with lambda arguments in C++ - we want consistent line breaks whether // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced // as aggressive line breaks are placed when the lambda is not the last arg. - if ((Style.Language == FormatStyle::LK_Cpp || - Style.Language == FormatStyle::LK_ObjC) && - Left.is(tok::l_paren) && Left.BlockParameterCount > 0 && + if ((Style.isCppOrObjC()) && Left.is(tok::l_paren) && + Left.BlockParameterCount > 0 && !Right.isOneOf(tok::l_paren, TT_LambdaLSquare)) { // Multiple lambdas in the same function call force line breaks. if (Left.BlockParameterCount > 1) @@ -3679,14 +3645,14 @@ // Only break after commas for generic type constraints. if (Line.First->is(TT_CSharpGenericTypeConstraint)) return Left.is(TT_CSharpGenericTypeConstraintComma); - } 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; if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends, Keywords.kw_implements)) return true; - } else if (Style.Language == FormatStyle::LK_JavaScript) { + } else if (Style.isJavaScript()) { const FormatToken *NonComment = Right.getPreviousNonComment(); if (NonComment && NonComment->isOneOf( @@ -3803,8 +3769,7 @@ !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) return false; if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) { - if (Style.Language == FormatStyle::LK_Proto || - Style.Language == FormatStyle::LK_TextProto) { + if (Style.isProto() || Style.isTextProtoBuf()) { if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral()) return false; // Prevent cases like: Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -95,8 +95,7 @@ /// For example, 'public:' labels in classes are offset by 1 or 2 /// characters to the left from their level. int getIndentOffset(const FormatToken &RootToken) { - if (Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) + if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) return 0; if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier() || @@ -529,8 +528,7 @@ // 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 @@ -1135,8 +1133,7 @@ bool FitsIntoOneLine = TheLine.Last->TotalLength + Indent <= ColumnLimit || (TheLine.Type == LT_ImportStatement && - (Style.Language != FormatStyle::LK_JavaScript || - !Style.JavaScriptWrapImports)) || + (!Style.isJavaScript() || !Style.JavaScriptWrapImports)) || (Style.isCSharp() && TheLine.InPPDirective); // don't split #regions in C# if (Style.ColumnLimit == 0) Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -299,11 +299,10 @@ void UnwrappedLineParser::parseFile() { // The top-level context in a file always has declarations, except for pre- // processor directives and JavaScript files. - bool MustBeDeclaration = - !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript; + bool MustBeDeclaration = !Line->InPPDirective && !Style.isJavaScript(); ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, MustBeDeclaration); - if (Style.Language == FormatStyle::LK_TextProto) + if (Style.isTextProtoBuf()) parseBracedList(); else parseLevel(/*HasOpeningBrace=*/false); @@ -317,8 +316,7 @@ // // 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.isTextProtoBuf() && !CommentsBeforeNextToken.empty()) addUnwrappedLine(); flushComments(true); addUnwrappedLine(); @@ -411,8 +409,7 @@ LLVM_FALLTHROUGH; } case tok::kw_case: - if (Style.Language == FormatStyle::LK_JavaScript && - Line->MustBeDeclaration) { + if (Style.isJavaScript() && Line->MustBeDeclaration) { // A 'case: string' style field declaration. parseStructuralElement(); break; @@ -461,7 +458,7 @@ switch (Tok->Tok.getKind()) { case tok::l_brace: - if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) { + if (Style.isJavaScript() && PrevTok) { if (PrevTok->isOneOf(tok::colon, tok::less)) // A ':' indicates this code is in a type, or a braced list // following a label in an object literal ({a: {b: 1}}). @@ -486,7 +483,7 @@ break; if (LBraceStack.back()->BlockKind == BK_Unknown) { bool ProbablyBracedList = false; - if (Style.Language == FormatStyle::LK_Proto) { + if (Style.isProto()) { ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); } else { // Using OriginalColumn to distinguish between ObjC methods and @@ -503,10 +500,10 @@ // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a // braced list in JS. ProbablyBracedList = - (Style.Language == FormatStyle::LK_JavaScript && + (Style.isJavaScript() && NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, Keywords.kw_as)) || - (Style.isCpp() && NextTok->is(tok::l_paren)) || + (Style.isCppOrObjC() && NextTok->is(tok::l_paren)) || NextTok->isOneOf(tok::comma, tok::period, tok::colon, tok::r_paren, tok::r_square, tok::l_brace, tok::ellipsis) || @@ -693,7 +690,7 @@ FormatTok->BlockKind = BK_Block; nextToken(); { - bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript && + bool SkipIndent = (Style.isJavaScript() && (isGoogScope(*Line) || isIIFE(*Line, Keywords))); ScopedLineState LineState(*this); ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, @@ -1021,8 +1018,7 @@ void UnwrappedLineParser::parseStructuralElement() { assert(!FormatTok->is(tok::l_brace)); - 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(); @@ -1053,39 +1049,38 @@ case tok::kw_public: case tok::kw_protected: case tok::kw_private: - if (Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) + if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) nextToken(); else parseAccessSpecifier(); return; case tok::kw_if: - if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + if (Style.isJavaScript() && Line->MustBeDeclaration) // field/method declaration. break; parseIfThenElse(); return; case tok::kw_for: case tok::kw_while: - if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + if (Style.isJavaScript() && Line->MustBeDeclaration) // field/method declaration. break; parseForOrWhileLoop(); return; case tok::kw_do: - if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + if (Style.isJavaScript() && Line->MustBeDeclaration) // field/method declaration. break; parseDoWhile(); return; case tok::kw_switch: - if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + if (Style.isJavaScript() && Line->MustBeDeclaration) // 'switch: string' field declaration. break; parseSwitch(); return; case tok::kw_default: - if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + if (Style.isJavaScript() && Line->MustBeDeclaration) // 'default: string' field declaration. break; nextToken(); @@ -1096,14 +1091,14 @@ // e.g. "default void f() {}" in a Java interface. break; case tok::kw_case: - if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + if (Style.isJavaScript() && Line->MustBeDeclaration) // 'case: string' field declaration. break; parseCaseLabel(); return; case tok::kw_try: case tok::kw___try: - if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) + if (Style.isJavaScript() && Line->MustBeDeclaration) // field/method declaration. break; parseTryCatch(); @@ -1125,11 +1120,11 @@ } break; case tok::kw_export: - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { parseJavaScriptEs6ImportExport(); return; } - if (!Style.isCpp()) + if (!Style.isCppOrObjC()) break; // Handle C++ "(inline|export) namespace". LLVM_FALLTHROUGH; @@ -1151,11 +1146,11 @@ return; } if (FormatTok->is(Keywords.kw_import)) { - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { parseJavaScriptEs6ImportExport(); return; } - if (Style.Language == FormatStyle::LK_Proto) { + if (Style.isProto()) { nextToken(); if (FormatTok->is(tok::kw_public)) nextToken(); @@ -1168,7 +1163,7 @@ return; } } - if (Style.isCpp() && + if (Style.isCppOrObjC() && FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, Keywords.kw_slots, Keywords.kw_qslots)) { nextToken(); @@ -1178,11 +1173,11 @@ return; } } - if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) { + if (Style.isCppOrObjC() && FormatTok->is(TT_StatementMacro)) { parseStatementMacro(); return; } - if (Style.isCpp() && FormatTok->is(TT_NamespaceMacro)) { + if (Style.isCppOrObjC() && FormatTok->is(TT_NamespaceMacro)) { parseNamespace(); return; } @@ -1200,8 +1195,7 @@ nextToken(); parseBracedList(); break; - } else if (Style.Language == FormatStyle::LK_Java && - FormatTok->is(Keywords.kw_interface)) { + } else if (Style.isJava() && FormatTok->is(Keywords.kw_interface)) { nextToken(); break; } @@ -1269,7 +1263,7 @@ if (!parseEnum()) break; // This only applies for C++. - if (!Style.isCpp()) { + if (!Style.isCppOrObjC()) { addUnwrappedLine(); return; } @@ -1289,8 +1283,7 @@ // record declaration or definition can start a structural element. parseRecord(); // This does not apply for Java, JavaScript and C#. - if (Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) { + if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) { if (FormatTok->is(tok::semi)) nextToken(); addUnwrappedLine(); @@ -1300,10 +1293,9 @@ 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.Language == FormatStyle::LK_JavaScript && FormatTok && + if (Style.isJavaScript() && FormatTok && FormatTok->Tok.getIdentifierInfo()) // JavaScript only has pseudo keywords, all keywords are allowed to // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 @@ -1351,8 +1343,7 @@ // element continues. break; case tok::kw_try: - if (Style.Language == FormatStyle::LK_JavaScript && - Line->MustBeDeclaration) { + if (Style.isJavaScript() && Line->MustBeDeclaration) { // field/method declaration. nextToken(); break; @@ -1379,17 +1370,15 @@ // expressions (functions that are not on their own line) must not create // a new unwrapped line, so they are special cased below. size_t TokenCount = Line->Tokens.size(); - if (Style.Language == FormatStyle::LK_JavaScript && - FormatTok->is(Keywords.kw_function) && + if (Style.isJavaScript() && FormatTok->is(Keywords.kw_function) && (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is( Keywords.kw_async)))) { tryToParseJSFunction(); break; } - if ((Style.Language == FormatStyle::LK_JavaScript || - Style.Language == FormatStyle::LK_Java) && + if ((Style.isJavaScript() || Style.isJava()) && FormatTok->is(Keywords.kw_interface)) { - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { // In JavaScript/TypeScript, "interface" can be used as a standalone // identifier, e.g. in `var interface = 1;`. If "interface" is // followed by another identifier, it is very like to be an actual @@ -1407,7 +1396,7 @@ return; } - if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) { + if (Style.isCppOrObjC() && FormatTok->is(TT_StatementMacro)) { parseStatementMacro(); return; } @@ -1418,7 +1407,7 @@ // JS doesn't have macros, and within classes colons indicate fields, not // labels. - if (Style.Language == FormatStyle::LK_JavaScript) + if (Style.isJavaScript()) break; TokenCount = Line->Tokens.size(); @@ -1468,8 +1457,7 @@ FormatTok->BlockKind = BK_BracedInit; nextToken(); parseBracedList(); - } else if (Style.Language == FormatStyle::LK_Proto && - FormatTok->Tok.is(tok::less)) { + } else if (Style.isProto() && FormatTok->Tok.is(tok::less)) { nextToken(); parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, /*ClosingBraceKind=*/tok::greater); @@ -1579,7 +1567,7 @@ } bool UnwrappedLineParser::tryToParseLambda() { - if (!Style.isCpp()) { + if (!Style.isCppOrObjC()) { nextToken(); return false; } @@ -1765,7 +1753,7 @@ } } } - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { if (FormatTok->is(Keywords.kw_function) || FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) { tryToParseJSFunction(); @@ -1810,7 +1798,7 @@ parseParens(); // JavaScript can just have free standing methods and getters/setters in // object literals. Detect them by a "{" following ")". - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { if (FormatTok->is(tok::l_brace)) parseChildBlock(); break; @@ -1824,7 +1812,7 @@ parseBracedList(); break; case tok::less: - if (Style.Language == FormatStyle::LK_Proto) { + if (Style.isProto()) { nextToken(); parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, /*ClosingBraceKind=*/tok::greater); @@ -1837,7 +1825,7 @@ // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be // used for error recovery if we have otherwise determined that this is // a braced list. - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { nextToken(); break; } @@ -1866,7 +1854,7 @@ switch (FormatTok->Tok.getKind()) { case tok::l_paren: parseParens(); - 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: @@ -1890,13 +1878,13 @@ } break; case tok::kw_class: - if (Style.Language == FormatStyle::LK_JavaScript) + if (Style.isJavaScript()) parseRecord(/*ParseAsExpr=*/true); else nextToken(); break; case tok::identifier: - if (Style.Language == FormatStyle::LK_JavaScript && + if (Style.isJavaScript() && (FormatTok->is(Keywords.kw_function) || FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function))) tryToParseJSFunction(); @@ -2022,7 +2010,7 @@ } } // Parse try with resource. - if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { + if (Style.isJava() && FormatTok->is(tok::l_paren)) { parseParens(); } if (FormatTok->is(tok::l_brace)) { @@ -2047,8 +2035,7 @@ nextToken(); if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, tok::kw___finally) || - ((Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) && + ((Style.isJava() || Style.isJavaScript()) && FormatTok->is(Keywords.kw_finally)) || (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) @@ -2125,7 +2112,7 @@ } 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. @@ -2152,8 +2139,7 @@ "'for', 'while' or foreach macro expected"); nextToken(); // JS' for await ( ... - if (Style.Language == FormatStyle::LK_JavaScript && - FormatTok->is(Keywords.kw_await)) + if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await)) nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); @@ -2271,12 +2257,11 @@ // In TypeScript, "enum" can also be used as property name, e.g. in interface // declarations. An "enum" keyword followed by a colon would be a syntax // error and thus assume it is just an identifier. - if (Style.Language == FormatStyle::LK_JavaScript && - FormatTok->isOneOf(tok::colon, tok::question)) + if (Style.isJavaScript() && FormatTok->isOneOf(tok::colon, tok::question)) return false; // In protobuf, "enum" can be used as a field name. - if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal)) + if (Style.isProto() && FormatTok->is(tok::equal)) return false; // Eat up enum class ... @@ -2294,7 +2279,7 @@ nextToken(); // If there are two identifiers in a row, this is likely an elaborate // return type. In Java, this can be "implements", etc. - if (Style.isCpp() && FormatTok->is(tok::identifier)) + if (Style.isCppOrObjC() && FormatTok->is(tok::identifier)) return false; } } @@ -2304,12 +2289,12 @@ return true; FormatTok->BlockKind = BK_Block; - if (Style.Language == FormatStyle::LK_Java) { + if (Style.isJava()) { // Java enums are different. parseJavaEnumBody(); return true; } - if (Style.Language == FormatStyle::LK_Proto) { + if (Style.isProto()) { parseBlock(/*MustBeDeclaration=*/true); return true; } @@ -2411,10 +2396,9 @@ while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, tok::kw___attribute, tok::kw___declspec, tok::kw_alignas, tok::l_square, tok::r_square) || - ((Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) && + ((Style.isJava() || Style.isJavaScript()) && FormatTok->isOneOf(tok::period, tok::comma))) { - if (Style.Language == FormatStyle::LK_JavaScript && + if (Style.isJavaScript() && FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) { // JavaScript/TypeScript supports inline object types in // extends/implements positions: @@ -2876,7 +2860,7 @@ flushComments(isOnNewLine(*FormatTok)); pushToken(FormatTok); FormatToken *Previous = FormatTok; - if (Style.Language != FormatStyle::LK_JavaScript) + if (!Style.isJavaScript()) readToken(LevelDifference); else readTokenWithJavaScriptASI();