summaryrefslogtreecommitdiff
path: root/unittests/Format/FormatTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'unittests/Format/FormatTest.cpp')
-rw-r--r--unittests/Format/FormatTest.cpp1075
1 files changed, 263 insertions, 812 deletions
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 8d46ba6efcfee..6f9df680eef5b 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -9,7 +9,7 @@
#include "clang/Format/Format.h"
-#include "../Tooling/RewriterTestContext.h"
+#include "../Tooling/ReplacementTest.h"
#include "FormatTestUtils.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
@@ -19,6 +19,9 @@
#define DEBUG_TYPE "format-test"
+using clang::tooling::ReplacementTest;
+using clang::tooling::toReplacements;
+
namespace clang {
namespace format {
namespace {
@@ -273,6 +276,30 @@ TEST_F(FormatTest, RemovesEmptyLines) {
"int i;\n"
"\n"
"} // namespace"));
+
+ FormatStyle Style = getLLVMStyle();
+ Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
+ Style.MaxEmptyLinesToKeep = 2;
+ Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+ Style.BraceWrapping.AfterClass = true;
+ Style.BraceWrapping.AfterFunction = true;
+ Style.KeepEmptyLinesAtTheStartOfBlocks = false;
+
+ EXPECT_EQ("class Foo\n"
+ "{\n"
+ " Foo() {}\n"
+ "\n"
+ " void funk() {}\n"
+ "};",
+ format("class Foo\n"
+ "{\n"
+ " Foo()\n"
+ " {\n"
+ " }\n"
+ "\n"
+ " void funk() {}\n"
+ "};",
+ Style));
}
TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
@@ -1110,6 +1137,12 @@ TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
format("SomeFunction(a, // comment\n"
" b,\n"
" c); // comment"));
+ EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n"
+ " aaaa), //\n"
+ " aaaa, bbbbb);",
+ format("aaaaaaaaaa(aaaa(aaaa,\n"
+ "aaaa), //\n"
+ "aaaa, bbbbb);"));
}
TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
@@ -1934,6 +1967,10 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
verifyFormat("{\n"
" signals.set(); // This needs indentation.\n"
"}");
+ verifyFormat("void f() {\n"
+ "label:\n"
+ " signals.baz();\n"
+ "}");
}
TEST_F(FormatTest, SeparatesLogicalBlocks) {
@@ -2456,42 +2493,6 @@ TEST_F(FormatTest, FormatTryCatchBraceStyles) {
Style);
}
-TEST_F(FormatTest, FormatObjCTryCatch) {
- verifyFormat("@try {\n"
- " f();\n"
- "} @catch (NSException e) {\n"
- " @throw;\n"
- "} @finally {\n"
- " exit(42);\n"
- "}");
- verifyFormat("DEBUG({\n"
- " @try {\n"
- " } @finally {\n"
- " }\n"
- "});\n");
-}
-
-TEST_F(FormatTest, FormatObjCAutoreleasepool) {
- FormatStyle Style = getLLVMStyle();
- verifyFormat("@autoreleasepool {\n"
- " f();\n"
- "}\n"
- "@autoreleasepool {\n"
- " f();\n"
- "}\n",
- Style);
- Style.BreakBeforeBraces = FormatStyle::BS_Allman;
- verifyFormat("@autoreleasepool\n"
- "{\n"
- " f();\n"
- "}\n"
- "@autoreleasepool\n"
- "{\n"
- " f();\n"
- "}\n",
- Style);
-}
-
TEST_F(FormatTest, StaticInitializers) {
verifyFormat("static SomeClass SC = {1, 'a'};");
@@ -4540,12 +4541,13 @@ TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
TEST_F(FormatTest, BreaksConditionalExpressions) {
verifyFormat(
- "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+ "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat(
- "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+ "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
" : aaaaaaaaaaaaa);");
@@ -4595,11 +4597,12 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" ? aaaa\n"
" : bbbb;");
verifyFormat("unsigned Indent =\n"
- " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
- " ? IndentForLevel[TheLine.Level]\n"
- " : TheLine * 2,\n"
+ " format(TheLine.First,\n"
+ " IndentForLevel[TheLine.Level] >= 0\n"
+ " ? IndentForLevel[TheLine.Level]\n"
+ " : TheLine * 2,\n"
" TheLine.InPPDirective, PreviousEndOfLineColumn);",
- getLLVMStyleWithColumns(70));
+ getLLVMStyleWithColumns(60));
verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
" ? aaaaaaaaaaaaaaa\n"
" : bbbbbbbbbbbbbbb //\n"
@@ -4674,13 +4677,14 @@ TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
Style.BreakBeforeTernaryOperators = false;
Style.ColumnLimit = 70;
verifyFormat(
- "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
+ "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
Style);
verifyFormat(
- "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
+ "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
Style);
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
@@ -4736,13 +4740,13 @@ TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
" b :\n"
" c);",
Style);
- verifyFormat(
- "unsigned Indent =\n"
- " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
- " IndentForLevel[TheLine.Level] :\n"
- " TheLine * 2,\n"
- " TheLine.InPPDirective, PreviousEndOfLineColumn);",
- Style);
+ verifyFormat("unsigned Indent =\n"
+ " format(TheLine.First,\n"
+ " IndentForLevel[TheLine.Level] >= 0 ?\n"
+ " IndentForLevel[TheLine.Level] :\n"
+ " TheLine * 2,\n"
+ " TheLine.InPPDirective, PreviousEndOfLineColumn);",
+ Style);
verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
" aaaaaaaaaaaaaaa :\n"
" bbbbbbbbbbbbbbb ? //\n"
@@ -4795,6 +4799,8 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
" *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
Style);
+ verifyFormat("vector<int*> a, b;", Style);
+ verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
}
TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
@@ -5086,6 +5092,9 @@ TEST_F(FormatTest, AlignsPipes) {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
verifyFormat(
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+ " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
+ verifyFormat(
"llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
" \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
" << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
@@ -5103,29 +5112,6 @@ TEST_F(FormatTest, AlignsPipes) {
"llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
- verifyFormat("return out << \"somepacket = {\\n\"\n"
- " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
- " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
- " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
- " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
- " << \"}\";");
-
- verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
- " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
- " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
- verifyFormat(
- "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
- " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
- " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
- " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
- " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
- verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
- " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
- verifyFormat(
- "void f() {\n"
- " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
- " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
- "}");
verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -5136,22 +5122,6 @@ TEST_F(FormatTest, AlignsPipes) {
" bbb)\n"
" << a << b;");
- // Breaking before the first "<<" is generally not desirable.
- verifyFormat(
- "llvm::errs()\n"
- " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
- getLLVMStyleWithColumns(70));
- verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
- " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
- " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
- " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
- getLLVMStyleWithColumns(70));
-
// But sometimes, breaking before the first "<<" is desirable.
verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
@@ -5197,6 +5167,65 @@ TEST_F(FormatTest, AlignsPipes) {
verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
}
+TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
+ verifyFormat("return out << \"somepacket = {\\n\"\n"
+ " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
+ " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
+ " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
+ " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
+ " << \"}\";");
+
+ verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
+ " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
+ " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
+ verifyFormat(
+ "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
+ " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
+ " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
+ " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
+ " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
+ verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
+ " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
+ verifyFormat(
+ "void f() {\n"
+ " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
+ " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
+ "}");
+
+ // Breaking before the first "<<" is generally not desirable.
+ verifyFormat(
+ "llvm::errs()\n"
+ " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
+ getLLVMStyleWithColumns(70));
+ verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
+ " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
+ " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
+ " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
+ getLLVMStyleWithColumns(70));
+
+ verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
+ " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
+ " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
+ verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
+ " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
+ " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
+ verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
+ " (aaaa + aaaa);",
+ getLLVMStyleWithColumns(40));
+ verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
+ " (aaaaaaa + aaaaa));",
+ getLLVMStyleWithColumns(40));
+ verifyFormat(
+ "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
+ " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
+ " bbbbbbbbbbbbbbbbbbbbbbb);");
+}
+
TEST_F(FormatTest, UnderstandsEquals) {
verifyFormat(
"aaaaaaaaaaaaaaaaa =\n"
@@ -5492,6 +5521,18 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
}
+TEST_F(FormatTest, BitshiftOperatorWidth) {
+ EXPECT_EQ("int a = 1 << 2; /* foo\n"
+ " bar */",
+ format("int a=1<<2; /* foo\n"
+ " bar */"));
+
+ EXPECT_EQ("int b = 256 >> 1; /* foo\n"
+ " bar */",
+ format("int b =256>>1 ; /* foo\n"
+ " bar */"));
+}
+
TEST_F(FormatTest, UnderstandsBinaryOperators) {
verifyFormat("COMPARE(a, ==, b);");
verifyFormat("auto s = sizeof...(Ts) - 1;");
@@ -5628,6 +5669,9 @@ TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
+ verifyFormat("template <typename T>\n"
+ "void F(T) && = delete;",
+ getGoogleStyle());
FormatStyle AlignLeft = getLLVMStyle();
AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
@@ -5780,7 +5824,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
FormatStyle Left = getLLVMStyle();
Left.PointerAlignment = FormatStyle::PAS_Left;
verifyFormat("x = *a(x) = *a(y);", Left);
- verifyFormat("for (;; * = b) {\n}", Left);
+ verifyFormat("for (;; *a = b) {\n}", Left);
verifyFormat("return *this += 1;", Left);
verifyIndependentOfContext("a = *(x + y);");
@@ -5845,11 +5889,12 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("foo<b & 1>();");
verifyFormat("decltype(*::std::declval<const T &>()) void F();");
verifyFormat(
- "template <class T, class = typename std::enable_if<\n"
- " std::is_integral<T>::value &&\n"
- " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
+ "template <class T,\n"
+ " class = typename std::enable_if<\n"
+ " std::is_integral<T>::value &&\n"
+ " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
"void F();",
- getLLVMStyleWithColumns(76));
+ getLLVMStyleWithColumns(70));
verifyFormat(
"template <class T,\n"
" class = typename ::std::enable_if<\n"
@@ -5861,6 +5906,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyIndependentOfContext("MACRO(auto *a);");
verifyIndependentOfContext("MACRO(const A *a);");
verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
+ verifyFormat("void f() { f(float{1}, a * a); }");
// FIXME: Is there a way to make this work?
// verifyIndependentOfContext("MACRO(A *a);");
@@ -6481,6 +6527,19 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
"};");
verifyFormat("#define A {a, a},");
+ // Cases where distinguising braced lists and blocks is hard.
+ verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
+ verifyFormat("void f() {\n"
+ " return; // comment\n"
+ "}\n"
+ "SomeType t;");
+ verifyFormat("void f() {\n"
+ " if (a) {\n"
+ " f();\n"
+ " }\n"
+ "}\n"
+ "SomeType t;");
+
// In combination with BinPackArguments = false.
FormatStyle NoBinPacking = getLLVMStyle();
NoBinPacking.BinPackArguments = false;
@@ -6595,7 +6654,7 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
"std::this_thread::sleep_for(\n"
" std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
ExtraSpaces);
- verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
+ verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
" aaaaaaa,\n"
" aaaaaaaaaa,\n"
" aaaaa,\n"
@@ -6733,6 +6792,26 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
" 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
" 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
" 1, 22, 333, 4444, 55555, 666666, 7777777});");
+
+ // Allow "single-column" layout even if that violates the column limit. There
+ // isn't going to be a better way.
+ verifyFormat("std::vector<int> a = {\n"
+ " aaaaaaaa,\n"
+ " aaaaaaaa,\n"
+ " aaaaaaaa,\n"
+ " aaaaaaaa,\n"
+ " aaaaaaaaaa,\n"
+ " aaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
+ getLLVMStyleWithColumns(30));
+ verifyFormat("vector<int> aaaa = {\n"
+ " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaa.aaaaaaa,\n"
+ " aaaaaa.aaaaaaa,\n"
+ " aaaaaa.aaaaaaa,\n"
+ " aaaaaa.aaaaaaa,\n"
+ "};");
}
TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
@@ -7170,6 +7249,30 @@ TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
verifyFormat("operator");
}
+TEST_F(FormatTest, SkipsDeeplyNestedLines) {
+ // This code would be painfully slow to format if we didn't skip it.
+ std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x
+ "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
+ "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
+ "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
+ "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
+ "A(1, 1)\n"
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
+ ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
+ // Deeply nested part is untouched, rest is formatted.
+ EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
+ format(std::string("int i;\n") + Code + "int j;\n",
+ getLLVMStyle(), IC_ExpectIncomplete));
+}
+
//===----------------------------------------------------------------------===//
// Objective-C tests.
//===----------------------------------------------------------------------===//
@@ -7240,684 +7343,6 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
verifyGoogleFormat("- foo:(int)foo;");
}
-TEST_F(FormatTest, FormatObjCInterface) {
- verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
- "@public\n"
- " int field1;\n"
- "@protected\n"
- " int field2;\n"
- "@private\n"
- " int field3;\n"
- "@package\n"
- " int field4;\n"
- "}\n"
- "+ (id)init;\n"
- "@end");
-
- verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
- " @public\n"
- " int field1;\n"
- " @protected\n"
- " int field2;\n"
- " @private\n"
- " int field3;\n"
- " @package\n"
- " int field4;\n"
- "}\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface /* wait for it */ Foo\n"
- "+ (id)init;\n"
- "// Look, a comment!\n"
- "- (int)answerWith:(int)i;\n"
- "@end");
-
- verifyFormat("@interface Foo\n"
- "@end\n"
- "@interface Bar\n"
- "@end");
-
- verifyFormat("@interface Foo : Bar\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
- "+ (id)init;\n"
- "@end");
-
- verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo (HackStuff)\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo ()\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
- "+ (id)init;\n"
- "@end");
-
- verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo {\n"
- " int _i;\n"
- "}\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo : Bar {\n"
- " int _i;\n"
- "}\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
- " int _i;\n"
- "}\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo (HackStuff) {\n"
- " int _i;\n"
- "}\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo () {\n"
- " int _i;\n"
- "}\n"
- "+ (id)init;\n"
- "@end");
-
- verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
- " int _i;\n"
- "}\n"
- "+ (id)init;\n"
- "@end");
-
- FormatStyle OnePerLine = getGoogleStyle();
- OnePerLine.BinPackParameters = false;
- verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
- "}",
- OnePerLine);
-}
-
-TEST_F(FormatTest, FormatObjCImplementation) {
- verifyFormat("@implementation Foo : NSObject {\n"
- "@public\n"
- " int field1;\n"
- "@protected\n"
- " int field2;\n"
- "@private\n"
- " int field3;\n"
- "@package\n"
- " int field4;\n"
- "}\n"
- "+ (id)init {\n}\n"
- "@end");
-
- verifyGoogleFormat("@implementation Foo : NSObject {\n"
- " @public\n"
- " int field1;\n"
- " @protected\n"
- " int field2;\n"
- " @private\n"
- " int field3;\n"
- " @package\n"
- " int field4;\n"
- "}\n"
- "+ (id)init {\n}\n"
- "@end");
-
- verifyFormat("@implementation Foo\n"
- "+ (id)init {\n"
- " if (true)\n"
- " return nil;\n"
- "}\n"
- "// Look, a comment!\n"
- "- (int)answerWith:(int)i {\n"
- " return i;\n"
- "}\n"
- "+ (int)answerWith:(int)i {\n"
- " return i;\n"
- "}\n"
- "@end");
-
- verifyFormat("@implementation Foo\n"
- "@end\n"
- "@implementation Bar\n"
- "@end");
-
- EXPECT_EQ("@implementation Foo : Bar\n"
- "+ (id)init {\n}\n"
- "- (void)foo {\n}\n"
- "@end",
- format("@implementation Foo : Bar\n"
- "+(id)init{}\n"
- "-(void)foo{}\n"
- "@end"));
-
- verifyFormat("@implementation Foo {\n"
- " int _i;\n"
- "}\n"
- "+ (id)init {\n}\n"
- "@end");
-
- verifyFormat("@implementation Foo : Bar {\n"
- " int _i;\n"
- "}\n"
- "+ (id)init {\n}\n"
- "@end");
-
- verifyFormat("@implementation Foo (HackStuff)\n"
- "+ (id)init {\n}\n"
- "@end");
- verifyFormat("@implementation ObjcClass\n"
- "- (void)method;\n"
- "{}\n"
- "@end");
-}
-
-TEST_F(FormatTest, FormatObjCProtocol) {
- verifyFormat("@protocol Foo\n"
- "@property(weak) id delegate;\n"
- "- (NSUInteger)numberOfThings;\n"
- "@end");
-
- verifyFormat("@protocol MyProtocol <NSObject>\n"
- "- (NSUInteger)numberOfThings;\n"
- "@end");
-
- verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
- "- (NSUInteger)numberOfThings;\n"
- "@end");
-
- verifyFormat("@protocol Foo;\n"
- "@protocol Bar;\n");
-
- verifyFormat("@protocol Foo\n"
- "@end\n"
- "@protocol Bar\n"
- "@end");
-
- verifyFormat("@protocol myProtocol\n"
- "- (void)mandatoryWithInt:(int)i;\n"
- "@optional\n"
- "- (void)optional;\n"
- "@required\n"
- "- (void)required;\n"
- "@optional\n"
- "@property(assign) int madProp;\n"
- "@end\n");
-
- verifyFormat("@property(nonatomic, assign, readonly)\n"
- " int *looooooooooooooooooooooooooooongNumber;\n"
- "@property(nonatomic, assign, readonly)\n"
- " NSString *looooooooooooooooooooooooooooongName;");
-
- verifyFormat("@implementation PR18406\n"
- "}\n"
- "@end");
-}
-
-TEST_F(FormatTest, FormatObjCMethodDeclarations) {
- verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
- " rect:(NSRect)theRect\n"
- " interval:(float)theInterval {\n"
- "}");
- verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
- " longKeyword:(NSRect)theRect\n"
- " longerKeyword:(float)theInterval\n"
- " error:(NSError **)theError {\n"
- "}");
- verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
- " longKeyword:(NSRect)theRect\n"
- " evenLongerKeyword:(float)theInterval\n"
- " error:(NSError **)theError {\n"
- "}");
- verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
- " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
- " NS_DESIGNATED_INITIALIZER;",
- getLLVMStyleWithColumns(60));
-
- // Continuation indent width should win over aligning colons if the function
- // name is long.
- FormatStyle continuationStyle = getGoogleStyle();
- continuationStyle.ColumnLimit = 40;
- continuationStyle.IndentWrappedFunctionNames = true;
- verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
- " dontAlignNamef:(NSRect)theRect {\n"
- "}",
- continuationStyle);
-
- // Make sure we don't break aligning for short parameter names.
- verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
- " aShortf:(NSRect)theRect {\n"
- "}",
- continuationStyle);
-}
-
-TEST_F(FormatTest, FormatObjCMethodExpr) {
- verifyFormat("[foo bar:baz];");
- verifyFormat("return [foo bar:baz];");
- verifyFormat("return (a)[foo bar:baz];");
- verifyFormat("f([foo bar:baz]);");
- verifyFormat("f(2, [foo bar:baz]);");
- verifyFormat("f(2, a ? b : c);");
- verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
-
- // Unary operators.
- verifyFormat("int a = +[foo bar:baz];");
- verifyFormat("int a = -[foo bar:baz];");
- verifyFormat("int a = ![foo bar:baz];");
- verifyFormat("int a = ~[foo bar:baz];");
- verifyFormat("int a = ++[foo bar:baz];");
- verifyFormat("int a = --[foo bar:baz];");
- verifyFormat("int a = sizeof [foo bar:baz];");
- verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
- verifyFormat("int a = &[foo bar:baz];");
- verifyFormat("int a = *[foo bar:baz];");
- // FIXME: Make casts work, without breaking f()[4].
- // verifyFormat("int a = (int)[foo bar:baz];");
- // verifyFormat("return (int)[foo bar:baz];");
- // verifyFormat("(void)[foo bar:baz];");
- verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
-
- // Binary operators.
- verifyFormat("[foo bar:baz], [foo bar:baz];");
- verifyFormat("[foo bar:baz] = [foo bar:baz];");
- verifyFormat("[foo bar:baz] *= [foo bar:baz];");
- verifyFormat("[foo bar:baz] /= [foo bar:baz];");
- verifyFormat("[foo bar:baz] %= [foo bar:baz];");
- verifyFormat("[foo bar:baz] += [foo bar:baz];");
- verifyFormat("[foo bar:baz] -= [foo bar:baz];");
- verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
- verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
- verifyFormat("[foo bar:baz] &= [foo bar:baz];");
- verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
- verifyFormat("[foo bar:baz] |= [foo bar:baz];");
- verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
- verifyFormat("[foo bar:baz] || [foo bar:baz];");
- verifyFormat("[foo bar:baz] && [foo bar:baz];");
- verifyFormat("[foo bar:baz] | [foo bar:baz];");
- verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
- verifyFormat("[foo bar:baz] & [foo bar:baz];");
- verifyFormat("[foo bar:baz] == [foo bar:baz];");
- verifyFormat("[foo bar:baz] != [foo bar:baz];");
- verifyFormat("[foo bar:baz] >= [foo bar:baz];");
- verifyFormat("[foo bar:baz] <= [foo bar:baz];");
- verifyFormat("[foo bar:baz] > [foo bar:baz];");
- verifyFormat("[foo bar:baz] < [foo bar:baz];");
- verifyFormat("[foo bar:baz] >> [foo bar:baz];");
- verifyFormat("[foo bar:baz] << [foo bar:baz];");
- verifyFormat("[foo bar:baz] - [foo bar:baz];");
- verifyFormat("[foo bar:baz] + [foo bar:baz];");
- verifyFormat("[foo bar:baz] * [foo bar:baz];");
- verifyFormat("[foo bar:baz] / [foo bar:baz];");
- verifyFormat("[foo bar:baz] % [foo bar:baz];");
- // Whew!
-
- verifyFormat("return in[42];");
- verifyFormat("for (auto v : in[1]) {\n}");
- verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
- verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
- verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
- verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
- verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
- verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
- "}");
- verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
- verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
- verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
-
- verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
- verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
- verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
- verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
- verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
- verifyFormat("[button setAction:@selector(zoomOut:)];");
- verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
-
- verifyFormat("arr[[self indexForFoo:a]];");
- verifyFormat("throw [self errorFor:a];");
- verifyFormat("@throw [self errorFor:a];");
-
- verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
- verifyFormat("[(id)foo bar:(id) ? baz : quux];");
- verifyFormat("4 > 4 ? (id)a : (id)baz;");
-
- // This tests that the formatter doesn't break after "backing" but before ":",
- // which would be at 80 columns.
- verifyFormat(
- "void f() {\n"
- " if ((self = [super initWithContentRect:contentRect\n"
- " styleMask:styleMask ?: otherMask\n"
- " backing:NSBackingStoreBuffered\n"
- " defer:YES]))");
-
- verifyFormat(
- "[foo checkThatBreakingAfterColonWorksOk:\n"
- " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
-
- verifyFormat("[myObj short:arg1 // Force line break\n"
- " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
- " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
- " error:arg4];");
- verifyFormat(
- "void f() {\n"
- " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
- " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
- " pos.width(), pos.height())\n"
- " styleMask:NSBorderlessWindowMask\n"
- " backing:NSBackingStoreBuffered\n"
- " defer:NO]);\n"
- "}");
- verifyFormat(
- "void f() {\n"
- " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
- " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
- " pos.width(), pos.height())\n"
- " syeMask:NSBorderlessWindowMask\n"
- " bking:NSBackingStoreBuffered\n"
- " der:NO]);\n"
- "}",
- getLLVMStyleWithColumns(70));
- verifyFormat(
- "void f() {\n"
- " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
- " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
- " pos.width(), pos.height())\n"
- " styleMask:NSBorderlessWindowMask\n"
- " backing:NSBackingStoreBuffered\n"
- " defer:NO]);\n"
- "}",
- getChromiumStyle(FormatStyle::LK_Cpp));
- verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
- " with:contentsNativeView];");
-
- verifyFormat(
- "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
- " owner:nillllll];");
-
- verifyFormat(
- "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
- " forType:kBookmarkButtonDragType];");
-
- verifyFormat("[defaultCenter addObserver:self\n"
- " selector:@selector(willEnterFullscreen)\n"
- " name:kWillEnterFullscreenNotification\n"
- " object:nil];");
- verifyFormat("[image_rep drawInRect:drawRect\n"
- " fromRect:NSZeroRect\n"
- " operation:NSCompositeCopy\n"
- " fraction:1.0\n"
- " respectFlipped:NO\n"
- " hints:nil];");
- verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
- verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
- verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
- " aaaaaaaaaaaaaaaaaaaaaa];");
- verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
- " .aaaaaaaa];", // FIXME: Indentation seems off.
- getLLVMStyleWithColumns(60));
-
- verifyFormat(
- "scoped_nsobject<NSTextField> message(\n"
- " // The frame will be fixed up when |-setMessageText:| is called.\n"
- " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
- verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
- " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
- " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
- " aaaa:bbb];");
- verifyFormat("[self param:function( //\n"
- " parameter)]");
- verifyFormat(
- "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
- " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
- " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
-
- // FIXME: This violates the column limit.
- verifyFormat(
- "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
- " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
- getLLVMStyleWithColumns(60));
-
- // Variadic parameters.
- verifyFormat(
- "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
- verifyFormat(
- "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
- verifyFormat("[self // break\n"
- " a:a\n"
- " aaa:aaa];");
- verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
- " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
-}
-
-TEST_F(FormatTest, ObjCAt) {
- verifyFormat("@autoreleasepool");
- verifyFormat("@catch");
- verifyFormat("@class");
- verifyFormat("@compatibility_alias");
- verifyFormat("@defs");
- verifyFormat("@dynamic");
- verifyFormat("@encode");
- verifyFormat("@end");
- verifyFormat("@finally");
- verifyFormat("@implementation");
- verifyFormat("@import");
- verifyFormat("@interface");
- verifyFormat("@optional");
- verifyFormat("@package");
- verifyFormat("@private");
- verifyFormat("@property");
- verifyFormat("@protected");
- verifyFormat("@protocol");
- verifyFormat("@public");
- verifyFormat("@required");
- verifyFormat("@selector");
- verifyFormat("@synchronized");
- verifyFormat("@synthesize");
- verifyFormat("@throw");
- verifyFormat("@try");
-
- EXPECT_EQ("@interface", format("@ interface"));
-
- // The precise formatting of this doesn't matter, nobody writes code like
- // this.
- verifyFormat("@ /*foo*/ interface");
-}
-
-TEST_F(FormatTest, ObjCSnippets) {
- verifyFormat("@autoreleasepool {\n"
- " foo();\n"
- "}");
- verifyFormat("@class Foo, Bar;");
- verifyFormat("@compatibility_alias AliasName ExistingClass;");
- verifyFormat("@dynamic textColor;");
- verifyFormat("char *buf1 = @encode(int *);");
- verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
- verifyFormat("char *buf1 = @encode(int **);");
- verifyFormat("Protocol *proto = @protocol(p1);");
- verifyFormat("SEL s = @selector(foo:);");
- verifyFormat("@synchronized(self) {\n"
- " f();\n"
- "}");
-
- verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
- verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
-
- verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
- verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
- verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
- verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
- getMozillaStyle());
- verifyFormat("@property BOOL editable;", getMozillaStyle());
- verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
- getWebKitStyle());
- verifyFormat("@property BOOL editable;", getWebKitStyle());
-
- verifyFormat("@import foo.bar;\n"
- "@import baz;");
-}
-
-TEST_F(FormatTest, ObjCForIn) {
- verifyFormat("- (void)test {\n"
- " for (NSString *n in arrayOfStrings) {\n"
- " foo(n);\n"
- " }\n"
- "}");
- verifyFormat("- (void)test {\n"
- " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
- " foo(n);\n"
- " }\n"
- "}");
-}
-
-TEST_F(FormatTest, ObjCLiterals) {
- verifyFormat("@\"String\"");
- verifyFormat("@1");
- verifyFormat("@+4.8");
- verifyFormat("@-4");
- verifyFormat("@1LL");
- verifyFormat("@.5");
- verifyFormat("@'c'");
- verifyFormat("@true");
-
- verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
- verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
- verifyFormat("NSNumber *favoriteColor = @(Green);");
- verifyFormat("NSString *path = @(getenv(\"PATH\"));");
-
- verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
-}
-
-TEST_F(FormatTest, ObjCDictLiterals) {
- verifyFormat("@{");
- verifyFormat("@{}");
- verifyFormat("@{@\"one\" : @1}");
- verifyFormat("return @{@\"one\" : @1;");
- verifyFormat("@{@\"one\" : @1}");
-
- verifyFormat("@{@\"one\" : @{@2 : @1}}");
- verifyFormat("@{\n"
- " @\"one\" : @{@2 : @1},\n"
- "}");
-
- verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
- verifyIncompleteFormat("[self setDict:@{}");
- verifyIncompleteFormat("[self setDict:@{@1 : @2}");
- verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
- verifyFormat(
- "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
- verifyFormat(
- "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
-
- verifyFormat("NSDictionary *d = @{\n"
- " @\"nam\" : NSUserNam(),\n"
- " @\"dte\" : [NSDate date],\n"
- " @\"processInfo\" : [NSProcessInfo processInfo]\n"
- "};");
- verifyFormat(
- "@{\n"
- " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
- "regularFont,\n"
- "};");
- verifyGoogleFormat(
- "@{\n"
- " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
- "regularFont,\n"
- "};");
- verifyFormat(
- "@{\n"
- " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
- " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
- "};");
-
- // We should try to be robust in case someone forgets the "@".
- verifyFormat("NSDictionary *d = {\n"
- " @\"nam\" : NSUserNam(),\n"
- " @\"dte\" : [NSDate date],\n"
- " @\"processInfo\" : [NSProcessInfo processInfo]\n"
- "};");
- verifyFormat("NSMutableDictionary *dictionary =\n"
- " [NSMutableDictionary dictionaryWithDictionary:@{\n"
- " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
- " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
- " cccccccccccccccc : ccccccccccccccc\n"
- " }];");
-
- // Ensure that casts before the key are kept on the same line as the key.
- verifyFormat(
- "NSDictionary *d = @{\n"
- " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
- "};");
-}
-
-TEST_F(FormatTest, ObjCArrayLiterals) {
- verifyIncompleteFormat("@[");
- verifyFormat("@[]");
- verifyFormat(
- "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
- verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
- verifyFormat("NSArray *array = @[ [foo description] ];");
-
- verifyFormat(
- "NSArray *some_variable = @[\n"
- " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- "];");
- verifyFormat(
- "NSArray *some_variable = @[\n"
- " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
- "];");
- verifyFormat("NSArray *some_variable = @[\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- "];");
- verifyFormat("NSArray *array = @[\n"
- " @\"a\",\n"
- " @\"a\",\n" // Trailing comma -> one per line.
- "];");
-
- // We should try to be robust in case someone forgets the "@".
- verifyFormat("NSArray *some_variable = [\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- " @\"aaaaaaaaaaaaaaaaa\",\n"
- "];");
- verifyFormat(
- "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
- " index:(NSUInteger)index\n"
- " nonDigitAttributes:\n"
- " (NSDictionary *)noDigitAttributes;");
- verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
- " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
- "]];");
-}
TEST_F(FormatTest, BreaksStringLiterals) {
EXPECT_EQ("\"some text \"\n"
@@ -10184,6 +9609,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(SpacesInContainerLiterals);
CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
+ CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
@@ -10920,6 +10346,7 @@ TEST_F(FormatTest, FormatsLambdas) {
verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
+ verifyFormat("int x = f(*+[] {});");
verifyFormat("void f() {\n"
" other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
"}\n");
@@ -11147,6 +10574,8 @@ TEST_F(FormatTest, FormatsBlocks) {
" }\n"
"});");
verifyFormat("Block b = ^int *(A *a, B *b) {}");
+ verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
+ "};");
FormatStyle FourIndent = getLLVMStyle();
FourIndent.ObjCBlockIndentWidth = 4;
@@ -11322,6 +10751,12 @@ TEST_F(FormatTest, SpacesInAngles) {
verifyFormat("A<A<int>>();", Spaces);
}
+TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
+ FormatStyle Style = getLLVMStyle();
+ Style.SpaceAfterTemplateKeyword = false;
+ verifyFormat("template<int> void foo();", Style);
+}
+
TEST_F(FormatTest, TripleAngleBrackets) {
verifyFormat("f<<<1, 1>>>();");
verifyFormat("f<<<1, 1, 1, s>>>();");
@@ -11332,6 +10767,8 @@ TEST_F(FormatTest, TripleAngleBrackets) {
EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaa<<<\n 1, 1>>>();");
+ verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
+ " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
}
TEST_F(FormatTest, MergeLessLessAtEnd) {
@@ -11483,9 +10920,25 @@ TEST_F(FormatTest, FormatsTableGenCode) {
verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
}
-// Since this test case uses UNIX-style file path. We disable it for MS
-// compiler.
-#if !defined(_MSC_VER) && !defined(__MINGW32__)
+TEST_F(FormatTest, ArrayOfTemplates) {
+ EXPECT_EQ("auto a = new unique_ptr<int>[10];",
+ format("auto a = new unique_ptr<int > [ 10];"));
+
+ FormatStyle Spaces = getLLVMStyle();
+ Spaces.SpacesInSquareBrackets = true;
+ EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
+ format("auto a = new unique_ptr<int > [10];", Spaces));
+}
+
+TEST_F(FormatTest, ArrayAsTemplateType) {
+ EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
+ format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
+
+ FormatStyle Spaces = getLLVMStyle();
+ Spaces.SpacesInSquareBrackets = true;
+ EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
+ format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
+}
TEST(FormatStyle, GetStyleOfFile) {
vfs::InMemoryFileSystem FS;
@@ -11495,13 +10948,13 @@ TEST(FormatStyle, GetStyleOfFile) {
llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
ASSERT_TRUE(
FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
- auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS);
+ auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
ASSERT_EQ(Style1, getLLVMStyle());
// Test 2: fallback to default.
ASSERT_TRUE(
FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
- auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS);
+ auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
ASSERT_EQ(Style2, getMozillaStyle());
// Test 3: format file in parent directory.
@@ -11510,23 +10963,10 @@ TEST(FormatStyle, GetStyleOfFile) {
llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
llvm::MemoryBuffer::getMemBuffer("int i;")));
- auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS);
+ auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
ASSERT_EQ(Style3, getGoogleStyle());
}
-#endif // _MSC_VER
-
-class ReplacementTest : public ::testing::Test {
-protected:
- tooling::Replacement createReplacement(SourceLocation Start, unsigned Length,
- llvm::StringRef ReplacementText) {
- return tooling::Replacement(Context.Sources, Start, Length,
- ReplacementText);
- }
-
- RewriterTestContext Context;
-};
-
TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
// Column limit is 20.
std::string Code = "Type *a =\n"
@@ -11541,15 +10981,15 @@ TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
" mm);\n"
"int bad = format ;";
FileID ID = Context.createInMemoryFile("format.cpp", Code);
- tooling::Replacements Replaces;
- Replaces.insert(tooling::Replacement(
- Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto "));
- Replaces.insert(tooling::Replacement(
- Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr"));
- Replaces.insert(tooling::Replacement(
- Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr"));
- Replaces.insert(tooling::Replacement(
- Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr"));
+ tooling::Replacements Replaces = toReplacements(
+ {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
+ "auto "),
+ tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
+ "nullptr"),
+ tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
+ "nullptr"),
+ tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
+ "nullptr")});
format::FormatStyle Style = format::getLLVMStyle();
Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
@@ -11576,9 +11016,9 @@ TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
" return 0;\n"
"}";
FileID ID = Context.createInMemoryFile("fix.cpp", Code);
- tooling::Replacements Replaces;
- Replaces.insert(tooling::Replacement(
- Context.Sources, Context.getLocation(ID, 1, 1), 0, "#include \"b.h\"\n"));
+ tooling::Replacements Replaces = toReplacements(
+ {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
+ "#include \"b.h\"\n")});
format::FormatStyle Style = format::getLLVMStyle();
Style.SortIncludes = true;
@@ -11590,6 +11030,17 @@ TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
EXPECT_EQ(Expected, *Result);
}
+TEST_F(FormatTest, AllignTrailingComments) {
+ EXPECT_EQ("#define MACRO(V) \\\n"
+ " V(Rt2) /* one more char */ \\\n"
+ " V(Rs) /* than here */ \\\n"
+ "/* comment 3 */\n",
+ format("#define MACRO(V)\\\n"
+ "V(Rt2) /* one more char */ \\\n"
+ "V(Rs) /* than here */ \\\n"
+ "/* comment 3 */ \\\n",
+ getLLVMStyleWithColumns(40)));
+}
} // end namespace
} // end namespace format
} // end namespace clang