From 142e79b8681bae42bebf3eef97fdaa30ce707c67 Mon Sep 17 00:00:00 2001 From: mydeveloperday Date: Tue, 21 Dec 2021 14:24:12 +0000 Subject: [PATCH] [clang-format] NFC use recently added Style.isJavaScript() Improve the readability of these if(Style==FormatStyle::LK_JavsScript) clauses --- clang/lib/Format/BreakableToken.cpp | 11 ++-- clang/lib/Format/ContinuationIndenter.cpp | 24 ++++----- clang/lib/Format/Format.cpp | 5 +- clang/lib/Format/FormatTokenLexer.cpp | 17 +++--- clang/lib/Format/TokenAnnotator.cpp | 81 +++++++++++++---------------- clang/lib/Format/UnwrappedLineFormatter.cpp | 7 ++- clang/lib/Format/UnwrappedLineParser.cpp | 79 +++++++++++++--------------- 7 files changed, 97 insertions(+), 127 deletions(-) diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp index 968b35b..5d03c98 100644 --- a/clang/lib/Format/BreakableToken.cpp +++ b/clang/lib/Format/BreakableToken.cpp @@ -91,7 +91,7 @@ getCommentSplit(StringRef Text, unsigned ContentStartColumn, // 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 {. - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { StringRef::size_type SpaceOffset = Text.find_first_of(Blanks, MaxSplitBytes); if (SpaceOffset != StringRef::npos && SpaceOffset + 1 < Text.size() && @@ -127,8 +127,7 @@ getCommentSplit(StringRef Text, unsigned ContentStartColumn, } // Avoid ever breaking before a @tag or a { in JavaScript. - if (Style.Language == FormatStyle::LK_JavaScript && - SpaceOffset + 1 < Text.size() && + if (Style.isJavaScript() && SpaceOffset + 1 < Text.size() && (Text[SpaceOffset + 1] == '{' || Text[SpaceOffset + 1] == '@')) { SpaceOffset = Text.find_last_of(Blanks, SpaceOffset); continue; @@ -460,8 +459,7 @@ BreakableBlockComment::BreakableBlockComment( 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.Language == FormatStyle::LK_Java) { if ((Lines[0] == "*" || Lines[0].startswith("* ")) && Lines.size() > 1) { // This is a multiline jsdoc comment. DelimitersOnNewline = true; @@ -580,8 +578,7 @@ const llvm::StringSet<> }; unsigned BreakableBlockComment::getContentIndent(unsigned LineIndex) const { - if (Style.Language != FormatStyle::LK_Java && - Style.Language != FormatStyle::LK_JavaScript) + if (Style.Language != FormatStyle::LK_Java && !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 104df64..4225d6b 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -422,8 +422,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { // ... // }.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; @@ -493,8 +492,8 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { return true; } - if (Style.Language == FormatStyle::LK_JavaScript && - Previous.is(tok::r_paren) && Previous.is(TT_JavaAnnotation)) { + if (Style.isJavaScript() && Previous.is(tok::r_paren) && + Previous.is(TT_JavaAnnotation)) { // Break after the closing parenthesis of TypeScript decorators before // functions, getters and setters. static const llvm::StringSet<> BreakBeforeDecoratedTokens = {"get", "set", @@ -510,7 +509,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) && // Don't always break between a JavaScript `function` and the function // name. - Style.Language != FormatStyle::LK_JavaScript) || + !Style.isJavaScript()) || (Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) && !Previous.is(tok::kw_template) && State.Stack.back().BreakBeforeParameter) return true; @@ -827,9 +826,8 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, // 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; @@ -1519,7 +1517,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, 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 && @@ -1548,7 +1546,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, } } - if (Style.Language == FormatStyle::LK_JavaScript && EndsInComma) + if (Style.isJavaScript() && EndsInComma) BreakBeforeParameter = true; } // Generally inherit NoLineBreak from the current scope to nested scope. @@ -1925,9 +1923,9 @@ ContinuationIndenter::createBreakableToken(const FormatToken &Current, // FIXME: String literal breaking is currently disabled for C#, Java, Json // 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() || - Style.isJson() || !Style.BreakStringLiterals || !AllowBreak) + if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() || + Style.isCSharp() || Style.isJson() || !Style.BreakStringLiterals || + !AllowBreak) return nullptr; // Don't break string literals inside preprocessor directives (except for diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 8fb1fa4..be01daa 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3050,8 +3050,7 @@ reformat(const FormatStyle &Style, StringRef Code, }); } - 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(); }); @@ -3060,7 +3059,7 @@ reformat(const FormatStyle &Style, StringRef Code, 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(); diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 64fbd2d..7736a70 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -78,7 +78,7 @@ ArrayRef FormatTokenLexer::lex() { assert(FirstInLineIndex == 0); do { Tokens.push_back(getNextToken()); - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { tryParseJSRegexLiteral(); handleTemplateStrings(); } @@ -107,7 +107,7 @@ void FormatTokenLexer::tryMergePreviousTokens() { if (Style.isCpp() && tryTransformTryUsageForC()) return; - if (Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) { + if (Style.isJavaScript() || Style.isCSharp()) { static const tok::TokenKind NullishCoalescingOperator[] = {tok::question, tok::question}; static const tok::TokenKind NullPropagatingOperator[] = {tok::question, @@ -152,7 +152,7 @@ void FormatTokenLexer::tryMergePreviousTokens() { 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}; @@ -920,8 +920,7 @@ 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.Language == FormatStyle::LK_JavaScript || - Style.Language == FormatStyle::LK_Java) && + if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) && FormatTok->is(tok::comment) && FormatTok->TokenText.startswith("//")) { size_t BackslashPos = FormatTok->TokenText.find('\\'); while (BackslashPos != StringRef::npos) { @@ -982,7 +981,7 @@ FormatToken *FormatTokenLexer::getNextToken() { 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); @@ -1060,14 +1059,12 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) { 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 || + if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Proto || Style.Language == FormatStyle::LK_TextProto) && Tok.is(tok::char_constant)) { Tok.Tok.setKind(tok::string_literal); diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index e11ba45..0a51a53 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -242,7 +242,7 @@ private: bool OperatorCalledAsMemberFunction = Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow); Contexts.back().IsExpression = OperatorCalledAsMemberFunction; - } 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))) { @@ -256,13 +256,13 @@ private: 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; @@ -582,7 +582,7 @@ private: 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); @@ -646,8 +646,7 @@ private: 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; @@ -774,7 +773,7 @@ private: Contexts.back().ColonIsDictLiteral = true; if (Left->is(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; @@ -808,12 +807,11 @@ private: 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)) { - if (Style.Language == FormatStyle::LK_JavaScript) + if (Style.isJavaScript()) Left->setType(TT_DictLiteral); ++CommaCount; } @@ -879,7 +877,7 @@ private: 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)) || @@ -979,8 +977,7 @@ private: 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: @@ -995,7 +992,7 @@ private: } 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))) @@ -1106,7 +1103,7 @@ private: CurrentToken->Previous->setType(TT_OverloadedOperator); break; case tok::question: - 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 @@ -1119,7 +1116,7 @@ private: // 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 @@ -1252,7 +1249,7 @@ private: 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. @@ -1354,14 +1351,13 @@ public: 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 @@ -1504,7 +1500,7 @@ private: !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))) && @@ -1633,11 +1629,11 @@ private: // The token type is already known. return; - if ((Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) && + if ((Style.isJavaScript() || Style.isCSharp()) && Current.is(tok::exclaim)) { if (Current.Previous) { bool IsIdentifier = - Style.Language == FormatStyle::LK_JavaScript + Style.isJavaScript() ? Keywords.IsJavaScriptIdentifier( *Current.Previous, /* AcceptIdentifierName= */ true) : Current.Previous->is(tok::identifier); @@ -1704,8 +1700,8 @@ private: } 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); @@ -1748,8 +1744,7 @@ private: Current.setType(TT_FunctionAnnotationRParen); } } - } else if (Current.is(tok::at) && Current.Next && - Style.Language != FormatStyle::LK_JavaScript && + } else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() && Style.Language != FormatStyle::LK_Java) { // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it // marks declarations and properties that need special formatting. @@ -1796,7 +1791,7 @@ private: // function declaration have been found. Current.setType(TT_TrailingAnnotation); } else if ((Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) && + Style.isJavaScript()) && Current.Previous) { if (Current.Previous->is(tok::at) && Current.isNot(Keywords.kw_interface)) { @@ -2022,7 +2017,7 @@ private: /// 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. @@ -2266,19 +2261,17 @@ private: return 0; if (Current->is(TT_RangeBasedForLoopColon)) return prec::Comma; - if ((Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) && + if ((Style.Language == FormatStyle::LK_Java || 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.Language == FormatStyle::LK_Java || Style.isJavaScript()) && Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements, Keywords.kw_throws)) return 0; @@ -2747,7 +2740,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, 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)) @@ -3179,8 +3172,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, 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]] ... @@ -3435,7 +3427,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, Keywords.kw_async, Keywords.kw_unsafe) && Right.is(tok::l_paren)) return true; - } else if (Style.Language == FormatStyle::LK_JavaScript) { + } else if (Style.isJavaScript()) { if (Left.is(TT_FatArrow)) return true; // for await ( ... @@ -3753,7 +3745,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, Left.is(tok::r_square) && Right.is(tok::l_square)) 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)) @@ -3843,15 +3835,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, 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.isNot(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())) @@ -3970,8 +3960,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, } // Put multiple Java annotation on a new line. - if ((Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) && + if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) && Left.is(TT_LeadingJavaAnnotation) && Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) @@ -4114,7 +4103,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 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( diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index a9beaef..8a86b85 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -97,8 +97,8 @@ private: /// 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.Language == FormatStyle::LK_Java || Style.isJavaScript() || + Style.isCSharp()) return 0; if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier() || @@ -1187,8 +1187,7 @@ unsigned UnwrappedLineFormatter::format( 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) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index ccffa59..c2ae691 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -366,8 +366,7 @@ void UnwrappedLineParser::parse() { 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) @@ -478,8 +477,7 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { 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; @@ -528,7 +526,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { 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}}). @@ -581,7 +579,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { // 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)) || @@ -791,7 +789,7 @@ void UnwrappedLineParser::parseChildBlock() { FormatTok->setBlockKind(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, @@ -1222,39 +1220,39 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { 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.Language == FormatStyle::LK_Java || 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(); @@ -1265,14 +1263,14 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { // 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(); @@ -1300,7 +1298,7 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { } break; case tok::kw_export: - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { parseJavaScriptEs6ImportExport(); return; } @@ -1326,7 +1324,7 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { return; } if (FormatTok->is(Keywords.kw_import)) { - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { parseJavaScriptEs6ImportExport(); return; } @@ -1480,7 +1478,7 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { if (Style.Language == FormatStyle::LK_Java && 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 @@ -1537,8 +1535,7 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { // 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; @@ -1565,17 +1562,15 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { // 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.Language == FormatStyle::LK_Java) && 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 @@ -1611,7 +1606,7 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { // 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(); @@ -1972,7 +1967,7 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, if (Style.isCSharp() && FormatTok->is(TT_FatArrow)) if (tryToParseCSharpLambda()) continue; - if (Style.Language == FormatStyle::LK_JavaScript) { + if (Style.isJavaScript()) { if (FormatTok->is(Keywords.kw_function) || FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) { tryToParseJSFunction(); @@ -2017,7 +2012,7 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, 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; @@ -2044,7 +2039,7 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, // 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; } @@ -2103,13 +2098,13 @@ void UnwrappedLineParser::parseParens() { nextToken(); 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(); @@ -2280,8 +2275,7 @@ void UnwrappedLineParser::parseTryCatch() { nextToken(); if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, tok::kw___finally) || - ((Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) && + ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) && FormatTok->is(Keywords.kw_finally)) || (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) @@ -2404,8 +2398,7 @@ void UnwrappedLineParser::parseForOrWhileLoop() { "'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 (Style.isCpp() && FormatTok->is(tok::kw_co_await)) nextToken(); @@ -2651,8 +2644,7 @@ bool UnwrappedLineParser::parseEnum() { // 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. @@ -2724,8 +2716,8 @@ 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.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) { + if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() || + Style.isCSharp()) { if (FormatTok->is(tok::semi)) nextToken(); addUnwrappedLine(); @@ -2854,10 +2846,9 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { 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.Language == FormatStyle::LK_Java || 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: @@ -3331,7 +3322,7 @@ void UnwrappedLineParser::nextToken(int LevelDifference) { flushComments(isOnNewLine(*FormatTok)); pushToken(FormatTok); FormatToken *Previous = FormatTok; - if (Style.Language != FormatStyle::LK_JavaScript) + if (!Style.isJavaScript()) readToken(LevelDifference); else readTokenWithJavaScriptASI(); -- 2.7.4