Index: lib/Format/FormatToken.h =================================================================== --- lib/Format/FormatToken.h +++ lib/Format/FormatToken.h @@ -26,78 +26,79 @@ namespace clang { namespace format { -#define LIST_TOKEN_TYPES \ - TYPE(ArrayInitializerLSquare) \ - TYPE(ArraySubscriptLSquare) \ - TYPE(AttributeParen) \ - TYPE(BinaryOperator) \ - TYPE(BitFieldColon) \ - TYPE(BlockComment) \ - TYPE(CastRParen) \ - TYPE(ConditionalExpr) \ - TYPE(ConflictAlternative) \ - TYPE(ConflictEnd) \ - TYPE(ConflictStart) \ - TYPE(CtorInitializerColon) \ - TYPE(CtorInitializerComma) \ - TYPE(DesignatedInitializerLSquare) \ - TYPE(DesignatedInitializerPeriod) \ - TYPE(DictLiteral) \ - TYPE(ForEachMacro) \ - TYPE(FunctionAnnotationRParen) \ - TYPE(FunctionDeclarationName) \ - TYPE(FunctionLBrace) \ - TYPE(FunctionTypeLParen) \ - TYPE(ImplicitStringLiteral) \ - TYPE(InheritanceColon) \ - TYPE(InheritanceComma) \ - TYPE(InlineASMBrace) \ - TYPE(InlineASMColon) \ - TYPE(JavaAnnotation) \ - TYPE(JsComputedPropertyName) \ - TYPE(JsExponentiation) \ - TYPE(JsExponentiationEqual) \ - TYPE(JsFatArrow) \ - TYPE(JsNonNullAssertion) \ - TYPE(JsTypeColon) \ - TYPE(JsTypeOperator) \ - TYPE(JsTypeOptionalQuestion) \ - TYPE(LambdaArrow) \ - TYPE(LambdaLSquare) \ - TYPE(LeadingJavaAnnotation) \ - TYPE(LineComment) \ - TYPE(MacroBlockBegin) \ - TYPE(MacroBlockEnd) \ - TYPE(ObjCBlockLBrace) \ - TYPE(ObjCBlockLParen) \ - TYPE(ObjCDecl) \ - TYPE(ObjCForIn) \ - TYPE(ObjCMethodExpr) \ - TYPE(ObjCMethodSpecifier) \ - TYPE(ObjCProperty) \ - TYPE(ObjCStringLiteral) \ - TYPE(OverloadedOperator) \ - TYPE(OverloadedOperatorLParen) \ - TYPE(PointerOrReference) \ - TYPE(PureVirtualSpecifier) \ - TYPE(RangeBasedForLoopColon) \ - TYPE(RegexLiteral) \ - TYPE(SelectorName) \ - TYPE(StartOfName) \ - TYPE(TemplateCloser) \ - TYPE(TemplateOpener) \ - TYPE(TemplateString) \ - TYPE(TrailingAnnotation) \ - TYPE(TrailingReturnArrow) \ - TYPE(TrailingUnaryOperator) \ - TYPE(UnaryOperator) \ +#define LIST_TOKEN_TYPES \ + TYPE(ArrayInitializerLSquare) \ + TYPE(ArraySubscriptLSquare) \ + TYPE(AttributeParen) \ + TYPE(BinaryOperator) \ + TYPE(BitFieldColon) \ + TYPE(BlockComment) \ + TYPE(CastRParen) \ + TYPE(ConditionalExpr) \ + TYPE(ConflictAlternative) \ + TYPE(ConflictEnd) \ + TYPE(ConflictStart) \ + TYPE(CtorInitializerColon) \ + TYPE(CtorInitializerComma) \ + TYPE(DesignatedInitializerLSquare) \ + TYPE(DesignatedInitializerPeriod) \ + TYPE(DictLiteral) \ + TYPE(ForEachMacro) \ + TYPE(FunctionAnnotationRParen) \ + TYPE(FunctionDeclarationName) \ + TYPE(FunctionLBrace) \ + TYPE(FunctionTypeLParen) \ + TYPE(ImplicitStringLiteral) \ + TYPE(InheritanceColon) \ + TYPE(InheritanceComma) \ + TYPE(InlineASMBrace) \ + TYPE(InlineASMColon) \ + TYPE(JavaAnnotation) \ + TYPE(JsComputedPropertyName) \ + TYPE(JsExponentiation) \ + TYPE(JsExponentiationEqual) \ + TYPE(JsFatArrow) \ + TYPE(JsNonNullAssertion) \ + TYPE(JsTypeColon) \ + TYPE(JsTypeOperator) \ + TYPE(JsTypeOptionalQuestion) \ + TYPE(LambdaArrow) \ + TYPE(LambdaLSquare) \ + TYPE(LeadingJavaAnnotation) \ + TYPE(LineComment) \ + TYPE(MacroBlockBegin) \ + TYPE(MacroBlockEnd) \ + TYPE(ObjCBlockLBrace) \ + TYPE(ObjCBlockLParen) \ + TYPE(ObjCDecl) \ + TYPE(ObjCForIn) \ + TYPE(ObjCMethodExpr) \ + TYPE(ObjCMethodSpecifier) \ + TYPE(ObjCProperty) \ + TYPE(ObjCStringLiteral) \ + TYPE(OverloadedOperator) \ + TYPE(OverloadedOperatorLParen) \ + TYPE(PointerOrReference) \ + TYPE(PureVirtualSpecifier) \ + TYPE(RangeBasedForLoopColon) \ + TYPE(RegexLiteral) \ + TYPE(SelectorName) \ + TYPE(StartOfName) \ + TYPE(StructuredBindingLSquare) \ + TYPE(TemplateCloser) \ + TYPE(TemplateOpener) \ + TYPE(TemplateString) \ + TYPE(TrailingAnnotation) \ + TYPE(TrailingReturnArrow) \ + TYPE(TrailingUnaryOperator) \ + TYPE(UnaryOperator) \ TYPE(Unknown) enum TokenType { #define TYPE(X) TT_##X, -LIST_TOKEN_TYPES + LIST_TOKEN_TYPES #undef TYPE - NUM_TOKEN_TYPES + NUM_TOKEN_TYPES }; /// \brief Determines the name of a token type. @@ -340,10 +341,11 @@ bool isSimpleTypeSpecifier() const; bool isObjCAccessSpecifier() const { - return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) || - Next->isObjCAtKeyword(tok::objc_protected) || - Next->isObjCAtKeyword(tok::objc_package) || - Next->isObjCAtKeyword(tok::objc_private)); + return is(tok::at) && Next && + (Next->isObjCAtKeyword(tok::objc_public) || + Next->isObjCAtKeyword(tok::objc_protected) || + Next->isObjCAtKeyword(tok::objc_package) || + Next->isObjCAtKeyword(tok::objc_private)); } /// \brief Returns whether \p Tok is ([{ or a template opening <. @@ -503,15 +505,13 @@ return is(K1) && Next && Next->startsSequenceInternal(Tokens...); } - template - bool startsSequenceInternal(A K1) const { + template bool startsSequenceInternal(A K1) const { if (is(tok::comment) && Next) return Next->startsSequenceInternal(K1); return is(K1); } - template - bool endsSequenceInternal(A K1) const { + template bool endsSequenceInternal(A K1) const { if (is(tok::comment) && Previous) return Previous->endsSequenceInternal(K1); return is(K1); Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -47,7 +47,7 @@ if (NonTemplateLess.count(CurrentToken->Previous)) return false; - const FormatToken& Previous = *CurrentToken->Previous; + const FormatToken &Previous = *CurrentToken->Previous; if (Previous.Previous) { if (Previous.Previous->Tok.isLiteral()) return false; @@ -152,11 +152,11 @@ // export type X = (...); Contexts.back().IsExpression = false; } else if (Left->Previous && - (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype, - tok::kw_if, tok::kw_while, tok::l_paren, - tok::comma) || - Left->Previous->endsSequence(tok::kw_constexpr, tok::kw_if) || - Left->Previous->is(TT_BinaryOperator))) { + (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype, + tok::kw_if, tok::kw_while, tok::l_paren, + tok::comma) || + Left->Previous->endsSequence(tok::kw_constexpr, tok::kw_if) || + 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 && @@ -325,8 +325,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.isCpp() && Parent && Parent->is(TT_TemplateCloser) && (Contexts.back().CanBeExpression || Contexts.back().IsExpression || Contexts.back().InTemplateArgument); @@ -342,9 +341,22 @@ getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown); bool ColonFound = false; + FormatToken *PreviousNoneOfConstVolatileReference = Parent; + while (PreviousNoneOfConstVolatileReference && + PreviousNoneOfConstVolatileReference->isOneOf( + tok::kw_const, tok::kw_volatile, tok::amp, tok::ampamp)) + PreviousNoneOfConstVolatileReference = + PreviousNoneOfConstVolatileReference->getPreviousNonComment(); + + bool CppStructuredBindings = + Style.isCpp() && PreviousNoneOfConstVolatileReference && + PreviousNoneOfConstVolatileReference->is(tok::kw_auto); + unsigned BindingIncrease = 1; if (Left->is(TT_Unknown)) { - if (StartsObjCMethodExpr) { + if (CppStructuredBindings) { + Left->Type = TT_StructuredBindingLSquare; + } else if (StartsObjCMethodExpr) { Left->Type = TT_ObjCMethodExpr; } else if (Style.Language == FormatStyle::LK_JavaScript && Parent && Contexts.back().ContextKind == tok::l_brace && @@ -605,7 +617,8 @@ break; case tok::kw_if: case tok::kw_while: - if (Tok->is(tok::kw_if) && CurrentToken && CurrentToken->is(tok::kw_constexpr)) + if (Tok->is(tok::kw_if) && CurrentToken && + CurrentToken->is(tok::kw_constexpr)) next(); if (CurrentToken && CurrentToken->is(tok::l_paren)) { next(); @@ -631,8 +644,7 @@ // marks the first l_paren as a OverloadedOperatorLParen. Here, we make // the first two parens OverloadedOperators and the second l_paren an // OverloadedOperatorLParen. - if (Tok->Previous && - Tok->Previous->is(tok::r_paren) && + if (Tok->Previous && Tok->Previous->is(tok::r_paren) && Tok->Previous->MatchingParen && Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) { Tok->Previous->Type = TT_OverloadedOperator; @@ -655,7 +667,7 @@ break; case tok::l_brace: if (Style.Language == FormatStyle::LK_TextProto) { - FormatToken *Previous =Tok->getPreviousNonComment(); + FormatToken *Previous = Tok->getPreviousNonComment(); if (Previous && Previous->Type != TT_DictLiteral) Previous->Type = TT_SelectorName; } @@ -752,8 +764,8 @@ void parseIncludeDirective() { if (CurrentToken && CurrentToken->is(tok::less)) { - next(); - while (CurrentToken) { + next(); + while (CurrentToken) { // Mark tokens up to the trailing line comments as implicit string // literals. if (CurrentToken->isNot(tok::comment) && @@ -793,9 +805,9 @@ void parseHasInclude() { if (!CurrentToken || !CurrentToken->is(tok::l_paren)) return; - next(); // '(' + next(); // '(' parseIncludeDirective(); - next(); // ')' + next(); // ')' } LineType parsePreprocessorDirective() { @@ -854,7 +866,7 @@ if (Tok->is(tok::l_paren)) parseParens(); else if (Tok->isOneOf(Keywords.kw___has_include, - Keywords.kw___has_include_next)) + Keywords.kw___has_include_next)) parseHasInclude(); } return Type; @@ -945,11 +957,12 @@ // FIXME: Closure-library specific stuff should not be hard-coded but be // configurable. return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) && - Tok.Next->Next && (Tok.Next->Next->TokenText == "module" || - Tok.Next->Next->TokenText == "provide" || - Tok.Next->Next->TokenText == "require" || - Tok.Next->Next->TokenText == "setTestOnly" || - Tok.Next->Next->TokenText == "forwardDeclare") && + Tok.Next->Next && + (Tok.Next->Next->TokenText == "module" || + Tok.Next->Next->TokenText == "provide" || + Tok.Next->Next->TokenText == "require" || + Tok.Next->Next->TokenText == "setTestOnly" || + Tok.Next->Next->TokenText == "forwardDeclare") && Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren); } @@ -1066,8 +1079,7 @@ Current.Previous->is(TT_CtorInitializerColon)) { Contexts.back().IsExpression = true; Contexts.back().InCtorInitializer = true; - } else if (Current.Previous && - Current.Previous->is(TT_InheritanceColon)) { + } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) { Contexts.back().InInheritanceList = true; } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) { for (FormatToken *Previous = Current.Previous; @@ -1125,10 +1137,10 @@ Current.NestingLevel == 0) { Current.Type = TT_TrailingReturnArrow; } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) { - Current.Type = - determineStarAmpUsage(Current, Contexts.back().CanBeExpression && - Contexts.back().IsExpression, - Contexts.back().InTemplateArgument); + Current.Type = determineStarAmpUsage(Current, + Contexts.back().CanBeExpression && + Contexts.back().IsExpression, + Contexts.back().InTemplateArgument); } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) { Current.Type = determinePlusMinusCaretUsage(Current); if (Current.is(TT_UnaryOperator) && Current.is(tok::caret)) @@ -1736,7 +1748,7 @@ static unsigned maxNestingDepth(const AnnotatedLine &Line) { unsigned Result = 0; - for (const auto* Tok = Line.First; Tok != nullptr; Tok = Tok->Next) + for (const auto *Tok = Line.First; Tok != nullptr; Tok = Tok->Next) Result = std::max(Result, Tok->NestingLevel); return Result; } @@ -1778,7 +1790,7 @@ // function declaration. static bool isFunctionDeclarationName(const FormatToken &Current, const AnnotatedLine &Line) { - auto skipOperatorName = [](const FormatToken* Next) -> const FormatToken* { + auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * { for (; Next; Next = Next->Next) { if (Next->is(TT_OverloadedOperatorLParen)) return Next; @@ -1786,8 +1798,8 @@ continue; if (Next->isOneOf(tok::kw_new, tok::kw_delete)) { // For 'new[]' and 'delete[]'. - if (Next->Next && Next->Next->is(tok::l_square) && - Next->Next->Next && Next->Next->Next->is(tok::r_square)) + if (Next->Next && Next->Next->is(tok::l_square) && Next->Next->Next && + Next->Next->Next->is(tok::r_square)) Next = Next->Next->Next; continue; } @@ -2066,7 +2078,8 @@ if (Left.is(tok::comment)) return 1000; - if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon, TT_CtorInitializerColon)) + if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon, + TT_CtorInitializerColon)) return 2; if (Right.isMemberAccess()) { @@ -2124,8 +2137,8 @@ Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) return 100; if (Left.is(tok::l_paren) && Left.Previous && - (Left.Previous->isOneOf(tok::kw_if, tok::kw_for) - || Left.Previous->endsSequence(tok::kw_constexpr, tok::kw_if))) + (Left.Previous->isOneOf(tok::kw_if, tok::kw_for) || + Left.Previous->endsSequence(tok::kw_constexpr, tok::kw_if))) return 1000; if (Left.is(tok::equal) && InFunctionDecl) return 110; @@ -2195,8 +2208,8 @@ : Style.SpacesInParentheses; if (Right.isOneOf(tok::semi, tok::comma)) return false; - if (Right.is(tok::less) && - Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList) + if (Right.is(tok::less) && Line.Type == LT_ObjCDecl && + Style.ObjCSpaceBeforeProtocolList) return true; if (Right.is(tok::less) && Left.is(tok::kw_template)) return Style.SpaceAfterTemplateKeyword; @@ -2256,17 +2269,20 @@ if (Left.is(tok::l_square)) return (Left.is(TT_ArrayInitializerLSquare) && Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) || - (Left.is(TT_ArraySubscriptLSquare) && Style.SpacesInSquareBrackets && - Right.isNot(tok::r_square)); + (Left.isOneOf(TT_ArraySubscriptLSquare, + TT_StructuredBindingLSquare) && + Style.SpacesInSquareBrackets && Right.isNot(tok::r_square)); if (Right.is(tok::r_square)) return Right.MatchingParen && ((Style.SpacesInContainerLiterals && Right.MatchingParen->is(TT_ArrayInitializerLSquare)) || (Style.SpacesInSquareBrackets && - Right.MatchingParen->is(TT_ArraySubscriptLSquare))); + Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare, + TT_StructuredBindingLSquare))); if (Right.is(tok::l_square) && !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, - TT_DesignatedInitializerLSquare) && + TT_DesignatedInitializerLSquare, + TT_StructuredBindingLSquare) && !Left.isOneOf(tok::numeric_constant, TT_DictLiteral)) return false; if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) @@ -2342,8 +2358,8 @@ if (Left.is(TT_JsFatArrow)) return true; // for await ( ... - if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && - Left.Previous && Left.Previous->is(tok::kw_for)) + if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous && + Left.Previous->is(tok::kw_for)) return true; if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) && Right.MatchingParen) { @@ -2500,7 +2516,8 @@ return (Left.is(TT_TemplateOpener) && Style.Standard == FormatStyle::LS_Cpp03) || !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square, - tok::kw___super, TT_TemplateCloser, TT_TemplateOpener)); + tok::kw___super, TT_TemplateCloser, + TT_TemplateOpener)); if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser))) return Style.SpacesInAngles; if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) || @@ -2625,9 +2642,8 @@ !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) return true; // Break only if we have multiple inheritance. - if (Style.BreakBeforeInheritanceComma && - Right.is(TT_InheritanceComma)) - return true; + if (Style.BreakBeforeInheritanceComma && Right.is(TT_InheritanceComma)) + return true; if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\"")) // Raw string literals are special wrt. line breaks. The author has made a // deliberate choice and might have aligned the contents of the string @@ -2634,10 +2650,8 @@ // literal accordingly. Thus, we try keep existing line breaks. return Right.NewlinesBefore > 0; if ((Right.Previous->is(tok::l_brace) || - (Right.Previous->is(tok::less) && - Right.Previous->Previous && - Right.Previous->Previous->is(tok::equal)) - ) && + (Right.Previous->is(tok::less) && Right.Previous->Previous && + Right.Previous->Previous->is(tok::equal))) && Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) { // Don't put enums or option definitions onto single lines in protocol // buffers. @@ -2740,8 +2754,7 @@ // list. return Left.BlockKind == BK_BracedInit || (Left.is(TT_CtorInitializerColon) && - Style.BreakConstructorInitializers == - FormatStyle::BCIS_AfterColon); + Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon); if (Left.is(tok::question) && Right.is(tok::colon)) return false; if (Right.is(TT_ConditionalExpr) || Right.is(tok::question)) @@ -2866,10 +2879,9 @@ << " T=" << getTokenTypeName(Tok->Type) << " S=" << Tok->SpacesRequiredBefore << " B=" << Tok->BlockParameterCount - << " BK=" << Tok->BlockKind - << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName() - << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind - << " FakeLParens="; + << " BK=" << Tok->BlockKind << " P=" << Tok->SplitPenalty + << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength + << " PPK=" << Tok->PackingKind << " FakeLParens="; for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) llvm::errs() << Tok->FakeLParens[i] << "/"; llvm::errs() << " FakeRParens=" << Tok->FakeRParens; Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -30,11 +30,7 @@ class FormatTest : public ::testing::Test { protected: - enum StatusCheck { - SC_ExpectComplete, - SC_ExpectIncomplete, - SC_DoNotCheck - }; + enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; std::string format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle(), @@ -280,7 +276,8 @@ format("namespace {\n" "int i;\n" "\n" - "}", LLVMWithNoNamespaceFix)); + "}", + LLVMWithNoNamespaceFix)); EXPECT_EQ("namespace {\n" "int i;\n" "}", @@ -961,20 +958,26 @@ "#endif\n" "}", Style)); - EXPECT_EQ("switch (a) {\n" "case 0:\n" - " return; // long long long long long long long long long long long long comment\n" - " // line\n" "}", + EXPECT_EQ("switch (a) {\n" + "case 0:\n" + " return; // long long long long long long long long long long " + "long long comment\n" + " // line\n" + "}", format("switch (a) {\n" - "case 0: return; // long long long long long long long long long long long long comment line\n" + "case 0: return; // long long long long long long long long " + "long long long long comment line\n" "}", Style)); EXPECT_EQ("switch (a) {\n" "case 0:\n" - " return; /* long long long long long long long long long long long long comment\n" + " return; /* long long long long long long long long long long " + "long long comment\n" " line */\n" "}", format("switch (a) {\n" - "case 0: return; /* long long long long long long long long long long long long comment line */\n" + "case 0: return; /* long long long long long long long long " + "long long long long comment line */\n" "}", Style)); verifyFormat("switch (a) {\n" @@ -1395,8 +1398,7 @@ // This code is more common than we thought; if we // layout this correctly the semicolon will go into // its own line, which is undesirable. - verifyFormat("namespace {};", - LLVMWithNoNamespaceFix); + verifyFormat("namespace {};", LLVMWithNoNamespaceFix); verifyFormat("namespace {\n" "class A {};\n" "};", @@ -1465,8 +1467,8 @@ Style.CompactNamespaces = true; verifyFormat("namespace A { namespace B {\n" - "}} // namespace A::B", - Style); + "}} // namespace A::B", + Style); EXPECT_EQ("namespace out { namespace in {\n" "}} // namespace out::in", @@ -2332,7 +2334,8 @@ "\\\n" " public: \\\n" " void baz(); \\\n" - " };", DontAlign)); + " };", + DontAlign)); } TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { @@ -2614,16 +2617,19 @@ Style.MacroBlockEnd = "^[A-Z_]+_END$"; verifyFormat("FOO_BEGIN\n" " FOO_ENTRY\n" - "FOO_END", Style); + "FOO_END", + Style); verifyFormat("FOO_BEGIN\n" " NESTED_FOO_BEGIN\n" " NESTED_FOO_ENTRY\n" " NESTED_FOO_END\n" - "FOO_END", Style); + "FOO_END", + Style); verifyFormat("FOO_BEGIN(Foo, Bar)\n" " int x;\n" " x = 1;\n" - "FOO_END(Baz)", Style); + "FOO_END(Baz)", + Style); } //===----------------------------------------------------------------------===// @@ -3101,22 +3107,22 @@ verifyFormat( "SomeClass::Constructor() :\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", - Style); + Style); verifyFormat( "SomeClass::Constructor() :\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", - Style); + Style); verifyFormat( "SomeClass::Constructor() :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", - Style); + Style); verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" " aaaaaaaaaa(aaaaaa) {}", - Style); + Style); verifyFormat("Constructor() :\n" " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" @@ -3123,17 +3129,17 @@ " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaaaa() {}", - Style); + Style); verifyFormat("Constructor() :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", - Style); + Style); verifyFormat("Constructor(int Parameter = 0) :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", - Style); + Style); verifyFormat("Constructor() :\n" " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" "}", @@ -3141,7 +3147,7 @@ verifyFormat("Constructor() :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", - Style); + Style); // Here a line could be saved by splitting the second initializer onto two // lines, but that is not desirable. @@ -3149,7 +3155,7 @@ " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaa(aaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", - Style); + Style); FormatStyle OnePerLine = Style; OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; @@ -3181,12 +3187,11 @@ " aaaaaaaaaaaaaaaaaaaaaa) {}", OnePerLine); OnePerLine.BinPackParameters = false; - verifyFormat( - "Constructor() :\n" - " aaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaa().aaa(),\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", - OnePerLine); + verifyFormat("Constructor() :\n" + " aaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaa().aaa(),\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", + OnePerLine); OnePerLine.ColumnLimit = 60; verifyFormat("Constructor() :\n" " aaaaaaaaaaaaaaaaaaaa(a),\n" @@ -3199,7 +3204,7 @@ format("Constructor() :\n" " // Comment forcing unwanted break.\n" " aaaa(aaaa) {}", - Style)); + Style)); Style.ColumnLimit = 0; verifyFormat("SomeClass::Constructor() :\n" @@ -3209,7 +3214,7 @@ " a(a) {}", Style); verifyFormat("SomeClass::Constructor() :\n" - " a(a), b(b), c(c) {}", + " a(a), b(b), c(c) {}", Style); verifyFormat("SomeClass::Constructor() :\n" " a(a) {\n" @@ -3220,19 +3225,18 @@ Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; verifyFormat("SomeClass::Constructor() :\n" - " a(a), b(b), c(c) {\n" - "}", + " a(a), b(b), c(c) {\n" + "}", Style); verifyFormat("SomeClass::Constructor() :\n" " a(a) {\n" - "}", + "}", Style); Style.ColumnLimit = 80; Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; Style.ConstructorInitializerIndentWidth = 2; - verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", - Style); + verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); verifyFormat("SomeClass::Constructor() :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", @@ -3417,11 +3421,10 @@ "typename aaaaaaaaaa::aaaaaaaaaaa\n" "aaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); - verifyGoogleFormat( - "template \n" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "aaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); + verifyGoogleFormat("template \n" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "aaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); FormatStyle Style = getLLVMStyle(); Style.PointerAlignment = FormatStyle::PAS_Left; @@ -3782,13 +3785,12 @@ verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); + verifyFormat("aaaaaaa->aaaaaaa\n" + " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); verifyFormat( "aaaaaaa->aaaaaaa\n" - " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" - " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); - verifyFormat( - "aaaaaaa->aaaaaaa\n" " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); verifyFormat( @@ -4109,11 +4111,10 @@ verifyFormat( "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" + " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat( - "aaaa(aaaaaaaaa, aaaaaaaaa,\n" - " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); - verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" " : aaaaaaaaaaaaa);"); verifyFormat( @@ -4254,12 +4255,11 @@ "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", Style); + verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" + " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", + Style); verifyFormat( - "aaaa(aaaaaaaa, aaaaaaaaaa,\n" - " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", - Style); - verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" " aaaaaaaaaaaaa);", Style); @@ -5021,22 +5021,24 @@ Style); Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; - verifyFormat( - "template struct s {};\n" - "extern s<\n" - " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" - " y;", - Style); + verifyFormat("template struct s {};\n" + "extern s<\n" + " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " + "aaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " + "aaaaaaaaaaaaaaaaaaaaaa>\n" + " y;", + Style); Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; - verifyFormat( - "template struct t {};\n" - "extern t<\n" - " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" - " y;", - Style); + verifyFormat("template struct t {};\n" + "extern t<\n" + " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " + "aaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " + "aaaaaaaaaaaaaaaaaaaaaa>\n" + " y;", + Style); } TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { @@ -5063,12 +5065,11 @@ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaa);"); - verifyFormat( - "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaa);", - getLLVMStyleWithColumns(74)); + verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaa);", + getLLVMStyleWithColumns(74)); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" @@ -5314,7 +5315,8 @@ Spaces.SpacesInCStyleCastParentheses = false; Spaces.SpacesInParentheses = true; verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); - verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); + verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", + Spaces); verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); } @@ -5517,16 +5519,15 @@ " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" "void F();", getLLVMStyleWithColumns(70)); + verifyFormat("template ::value &&\n" + " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" + " class U>\n" + "void F();", + getLLVMStyleWithColumns(70)); verifyFormat( "template ::value &&\n" - " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" - " class U>\n" - "void F();", - getLLVMStyleWithColumns(70)); - verifyFormat( - "template {} && ::std::is_array{}>::type>\n" "void F();", @@ -6187,13 +6188,14 @@ // Binpacking only if there is no trailing comma verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" " cccccccccc, dddddddddd};", - getLLVMStyleWithColumns(50)); + getLLVMStyleWithColumns(50)); verifyFormat("const Aaaaaa aaaaa = {\n" " aaaaaaaaaaa,\n" " bbbbbbbbbbb,\n" " ccccccccccc,\n" " ddddddddddd,\n" - "};", getLLVMStyleWithColumns(50)); + "};", + getLLVMStyleWithColumns(50)); // Cases where distinguising braced lists and blocks is hard. verifyFormat("vector v{12} GUARDED_BY(mutex);"); @@ -6821,7 +6823,7 @@ "{\n" "} Foo_t;", Style); - //typedef struct Bar {} Bar_t; + // typedef struct Bar {} Bar_t; } TEST_F(FormatTest, SplitEmptyUnion) { @@ -7195,7 +7197,6 @@ verifyGoogleFormat("- foo:(int)foo;"); } - TEST_F(FormatTest, BreaksStringLiterals) { EXPECT_EQ("\"some text \"\n" "\"other\";", @@ -7318,14 +7319,13 @@ // Verify that splitting the strings understands // Style::AlwaysBreakBeforeMultilineStrings. - EXPECT_EQ( - "aaaaaaaaaaaa(\n" - " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" - " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", - format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " - "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " - "aaaaaaaaaaaaaaaaaaaaaa\");", - getGoogleStyle())); + EXPECT_EQ("aaaaaaaaaaaa(\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", + format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaa\");", + getGoogleStyle())); EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", format("return \"aaaaaaaaaaaaaaaaaaaaaa " @@ -7456,10 +7456,10 @@ TEST_F(FormatTest, BreaksStringLiteralOperands) { // In a function call with two operands, the second can be broken with no line // break before it. - EXPECT_EQ("func(a, \"long long \"\n" - " \"long long\");", - format("func(a, \"long long long long\");", - getLLVMStyleWithColumns(24))); + EXPECT_EQ( + "func(a, \"long long \"\n" + " \"long long\");", + format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); // In a function call with three operands, the second must be broken with a // line break before it. EXPECT_EQ("func(a,\n" @@ -7488,22 +7488,21 @@ // break before it. EXPECT_EQ("a << \"line line \"\n" " \"line\";", - format("a << \"line line line\";", - getLLVMStyleWithColumns(20))); + format("a << \"line line line\";", getLLVMStyleWithColumns(20))); // In a chain of << with three operands, the second can be broken with no line // break before it. - EXPECT_EQ("abcde << \"line \"\n" - " \"line line\"\n" - " << c;", - format("abcde << \"line line line\" << c;", - getLLVMStyleWithColumns(20))); + EXPECT_EQ( + "abcde << \"line \"\n" + " \"line line\"\n" + " << c;", + format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); // In a chain of << with three operands, the third must be broken with a line // break before it. - EXPECT_EQ("a << b\n" - " << \"line line \"\n" - " \"line\";", - format("a << b << \"line line line\";", - getLLVMStyleWithColumns(20))); + EXPECT_EQ( + "a << b\n" + " << \"line line \"\n" + " \"line\";", + format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); // In a chain of << with three operands, the second can be broken with no line // break before it and the third must be broken with a line break before it. EXPECT_EQ("abcd << \"line line \"\n" @@ -7514,10 +7513,10 @@ getLLVMStyleWithColumns(20))); // In a chain of binary operators with two operands, the second can be broken // with no line break before it. - EXPECT_EQ("abcd + \"line line \"\n" - " \"line line\";", - format("abcd + \"line line line line\";", - getLLVMStyleWithColumns(20))); + EXPECT_EQ( + "abcd + \"line line \"\n" + " \"line line\";", + format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); // In a chain of binary operators with three operands, the second must be // broken with a line break before it. EXPECT_EQ("abcd +\n" @@ -9504,8 +9503,8 @@ #define EXPECT_ALL_STYLES_EQUAL(Styles) \ for (size_t i = 1; i < Styles.size(); ++i) \ - EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ - << " differs from Style #0" + EXPECT_EQ(Styles[0], Styles[i]) \ + << "Style #" << i << " of " << Styles.size() << " differs from Style #0" TEST_F(FormatTest, GetsPredefinedStyleByName) { SmallVector Styles; @@ -9691,8 +9690,7 @@ CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); - CHECK_PARSE("PenaltyBreakAssignment: 1234", - PenaltyBreakAssignment, 1234u); + CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", PenaltyBreakBeforeFirstCallParameter, 1234u); CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); @@ -10174,10 +10172,11 @@ "SomeLongTemplateVariableName<\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", Style); - verifyFormat( - "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", - Style); + verifyFormat("bool smaller = 1 < " + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" + " " + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", + Style); } TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { @@ -11005,24 +11004,23 @@ " int k;")); // Don't reflow comments within disabled regions. - EXPECT_EQ( - "// clang-format off\n" - "// long long long long long long line\n" - "/* clang-format on */\n" - "/* long long long\n" - " * long long long\n" - " * line */\n" - "int i;\n" - "/* clang-format off */\n" - "/* long long long long long long line */\n", - format("// clang-format off\n" - "// long long long long long long line\n" - "/* clang-format on */\n" - "/* long long long long long long line */\n" - "int i;\n" - "/* clang-format off */\n" - "/* long long long long long long line */\n", - getLLVMStyleWithColumns(20))); + EXPECT_EQ("// clang-format off\n" + "// long long long long long long line\n" + "/* clang-format on */\n" + "/* long long long\n" + " * long long long\n" + " * line */\n" + "int i;\n" + "/* clang-format off */\n" + "/* long long long long long long line */\n", + format("// clang-format off\n" + "// long long long long long long line\n" + "/* clang-format on */\n" + "/* long long long long long long line */\n" + "int i;\n" + "/* clang-format off */\n" + "/* long long long long long long line */\n", + getLLVMStyleWithColumns(20))); } TEST_F(FormatTest, DoNotCrashOnInvalidInput) { @@ -11056,9 +11054,7 @@ format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); } -TEST_F(FormatTest, NoSpaceAfterSuper) { - verifyFormat("__super::FooBar();"); -} +TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } TEST(FormatStyle, GetStyleOfFile) { vfs::InMemoryFileSystem FS; @@ -11201,7 +11197,8 @@ EXPECT_EQ("using std::cin;\n" "using std::cout;", format("using std::cout;\n" - "using std::cin;", getGoogleStyle())); + "using std::cin;", + getGoogleStyle())); } TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { @@ -11217,6 +11214,31 @@ EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); } +TEST_F(FormatTest, StructuredBindings) { + // Structured bindings is a C++17 feature. + // all modes, including C++11, C++14 and C++17 + verifyFormat("auto [a, b] = f();"); + EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); + EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); + EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); + EXPECT_EQ("auto const volatile [a, b] = f();", + format("auto const volatile[a, b] = f();")); + EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); + EXPECT_EQ("auto & [a, b, c] = f();", + format("auto &[ a , b,c ] = f();")); + EXPECT_EQ("auto && [a, b, c] = f();", + format("auto &&[ a , b,c ] = f();")); + EXPECT_EQ("auto const & [a, b] = f();", format("auto const&[a, b] = f();")); + EXPECT_EQ("auto const volatile && [a, b] = f();", + format("auto const volatile &&[a, b] = f();")); + EXPECT_EQ("auto && [a, b] = f();", format("auto &&[a, b] = f();")); + + format::FormatStyle Spaces = format::getLLVMStyle(); + Spaces.SpacesInSquareBrackets = true; + verifyFormat("auto [ a, b ] = f();", Spaces); + verifyFormat("auto && [ a, b ] = f();", Spaces); +} + } // end namespace } // end namespace format } // end namespace clang