aboutsummaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorPirama Arumuga Nainar <pirama@google.com>2015-07-01 12:25:36 -0700
committerPirama Arumuga Nainar <pirama@google.com>2015-07-01 12:25:36 -0700
commitb6d6993e6e6d3daf4d9876794254d20a134e37c2 (patch)
tree56c1af986f9588ac13a9f88597ce0738a574c6b9 /unittests
parente428a2cde8a5a5aba99db38a7d7f6a0b7ee823d2 (diff)
downloadclang-b6d6993e6e6d3daf4d9876794254d20a134e37c2.tar.gz
Update aosp/master clang for rebase to r239765
Change-Id: I0393bcc952590a7226af8c4b58534a8ee5fd2d99
Diffstat (limited to 'unittests')
-rw-r--r--unittests/AST/DeclPrinterTest.cpp18
-rw-r--r--unittests/AST/SourceLocationTest.cpp72
-rw-r--r--unittests/ASTMatchers/ASTMatchersTest.cpp17
-rw-r--r--unittests/Basic/SourceManagerTest.cpp5
-rw-r--r--unittests/Format/CMakeLists.txt1
-rw-r--r--unittests/Format/FormatTest.cpp1639
-rw-r--r--unittests/Format/FormatTestJS.cpp181
-rw-r--r--unittests/Format/FormatTestProto.cpp8
-rw-r--r--unittests/Format/FormatTestSelective.cpp441
-rw-r--r--unittests/Lex/LexerTest.cpp3
-rw-r--r--unittests/Lex/PPCallbacksTest.cpp3
-rw-r--r--unittests/Lex/PPConditionalDirectiveRecordTest.cpp3
-rw-r--r--unittests/Tooling/RefactoringTest.cpp25
13 files changed, 1625 insertions, 791 deletions
diff --git a/unittests/AST/DeclPrinterTest.cpp b/unittests/AST/DeclPrinterTest.cpp
index 070b4daabb..d8cb977092 100644
--- a/unittests/AST/DeclPrinterTest.cpp
+++ b/unittests/AST/DeclPrinterTest.cpp
@@ -157,6 +157,17 @@ public:
"input.cc");
}
+::testing::AssertionResult
+PrintedDeclCXX1ZMatches(StringRef Code, const DeclarationMatcher &NodeMatch,
+ StringRef ExpectedPrinted) {
+ std::vector<std::string> Args(1, "-std=c++1z");
+ return PrintedDeclMatches(Code,
+ Args,
+ NodeMatch,
+ ExpectedPrinted,
+ "input.cc");
+}
+
::testing::AssertionResult PrintedDeclObjCMatches(
StringRef Code,
const DeclarationMatcher &NodeMatch,
@@ -1264,6 +1275,13 @@ TEST(DeclPrinter, TestTemplateArgumentList15) {
// Should be: with semicolon
}
+TEST(DeclPrinter, TestStaticAssert1) {
+ ASSERT_TRUE(PrintedDeclCXX1ZMatches(
+ "static_assert(true);",
+ staticAssertDecl().bind("id"),
+ "static_assert(true)"));
+}
+
TEST(DeclPrinter, TestObjCMethod1) {
ASSERT_TRUE(PrintedDeclObjCMatches(
"__attribute__((objc_root_class)) @interface X\n"
diff --git a/unittests/AST/SourceLocationTest.cpp b/unittests/AST/SourceLocationTest.cpp
index a3fc9530ba..b0a8f85f0e 100644
--- a/unittests/AST/SourceLocationTest.cpp
+++ b/unittests/AST/SourceLocationTest.cpp
@@ -106,44 +106,44 @@ TEST(MemberExpr, ImplicitMemberRange) {
Verifier.expectRange(2, 30, 2, 30);
EXPECT_TRUE(Verifier.match("struct S { operator int() const; };\n"
"int foo(const S& s) { return s; }",
- memberExpr()));
-}
-
-class MemberExprArrowLocVerifier : public RangeVerifier<MemberExpr> {
+ memberExpr()));
+}
+
+class MemberExprArrowLocVerifier : public RangeVerifier<MemberExpr> {
protected:
SourceRange getRange(const MemberExpr &Node) override {
- return Node.getOperatorLoc();
- }
-};
-
-TEST(MemberExpr, ArrowRange) {
- MemberExprArrowLocVerifier Verifier;
- Verifier.expectRange(2, 19, 2, 19);
- EXPECT_TRUE(Verifier.match("struct S { int x; };\n"
- "void foo(S *s) { s->x = 0; }",
- memberExpr()));
-}
-
-TEST(MemberExpr, MacroArrowRange) {
- MemberExprArrowLocVerifier Verifier;
- Verifier.expectRange(1, 24, 1, 24);
- EXPECT_TRUE(Verifier.match("#define MEMBER(a, b) (a->b)\n"
- "struct S { int x; };\n"
- "void foo(S *s) { MEMBER(s, x) = 0; }",
- memberExpr()));
-}
-
-TEST(MemberExpr, ImplicitArrowRange) {
- MemberExprArrowLocVerifier Verifier;
- Verifier.expectRange(0, 0, 0, 0);
- EXPECT_TRUE(Verifier.match("struct S { int x; void Test(); };\n"
- "void S::Test() { x = 1; }",
- memberExpr()));
-}
-
-TEST(VarDecl, VMTypeFixedVarDeclRange) {
- RangeVerifier<VarDecl> Verifier;
- Verifier.expectRange(1, 1, 1, 23);
+ return Node.getOperatorLoc();
+ }
+};
+
+TEST(MemberExpr, ArrowRange) {
+ MemberExprArrowLocVerifier Verifier;
+ Verifier.expectRange(2, 19, 2, 19);
+ EXPECT_TRUE(Verifier.match("struct S { int x; };\n"
+ "void foo(S *s) { s->x = 0; }",
+ memberExpr()));
+}
+
+TEST(MemberExpr, MacroArrowRange) {
+ MemberExprArrowLocVerifier Verifier;
+ Verifier.expectRange(1, 24, 1, 24);
+ EXPECT_TRUE(Verifier.match("#define MEMBER(a, b) (a->b)\n"
+ "struct S { int x; };\n"
+ "void foo(S *s) { MEMBER(s, x) = 0; }",
+ memberExpr()));
+}
+
+TEST(MemberExpr, ImplicitArrowRange) {
+ MemberExprArrowLocVerifier Verifier;
+ Verifier.expectRange(0, 0, 0, 0);
+ EXPECT_TRUE(Verifier.match("struct S { int x; void Test(); };\n"
+ "void S::Test() { x = 1; }",
+ memberExpr()));
+}
+
+TEST(VarDecl, VMTypeFixedVarDeclRange) {
+ RangeVerifier<VarDecl> Verifier;
+ Verifier.expectRange(1, 1, 1, 23);
EXPECT_TRUE(Verifier.match("int a[(int)(void*)1234];",
varDecl(), Lang_C89));
}
diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp
index 4b0580a814..ae363e974b 100644
--- a/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -1398,6 +1398,12 @@ TEST(Callee, MatchesDeclarations) {
EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
+
+ CallMethodX = callExpr(callee(conversionDecl()));
+ EXPECT_TRUE(
+ matches("struct Y { operator int() const; }; int i = Y();", CallMethodX));
+ EXPECT_TRUE(notMatches("struct Y { operator int() const; }; Y y = Y();",
+ CallMethodX));
}
TEST(Callee, MatchesMemberExpressions) {
@@ -1589,6 +1595,13 @@ TEST(IsDeleted, MatchesDeletedFunctionDeclarations) {
functionDecl(hasName("Func"), isDeleted())));
}
+TEST(isConstexpr, MatchesConstexprDeclarations) {
+ EXPECT_TRUE(matches("constexpr int foo = 42;",
+ varDecl(hasName("foo"), isConstexpr())));
+ EXPECT_TRUE(matches("constexpr int bar();",
+ functionDecl(hasName("bar"), isConstexpr())));
+}
+
TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
@@ -2138,6 +2151,10 @@ TEST(Matcher, NullPtrLiteral) {
EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
}
+TEST(Matcher, GNUNullExpr) {
+ EXPECT_TRUE(matches("int* i = __null;", gnuNullExpr()));
+}
+
TEST(Matcher, AsmStatement) {
EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
}
diff --git a/unittests/Basic/SourceManagerTest.cpp b/unittests/Basic/SourceManagerTest.cpp
index 57b87b7604..494c27a2f1 100644
--- a/unittests/Basic/SourceManagerTest.cpp
+++ b/unittests/Basic/SourceManagerTest.cpp
@@ -61,8 +61,7 @@ class VoidModuleLoader : public ModuleLoader {
void makeModuleVisible(Module *Mod,
Module::NameVisibilityKind Visibility,
- SourceLocation ImportLoc,
- bool Complain) override { }
+ SourceLocation ImportLoc) override { }
GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
{ return nullptr; }
@@ -264,7 +263,7 @@ public:
MacroNameTok.getIdentifierInfo()->getName(),
true));
}
- void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
+ void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
SourceRange Range, const MacroArgs *Args) override {
Macros.push_back(MacroAction(MacroNameTok.getLocation(),
MacroNameTok.getIdentifierInfo()->getName(),
diff --git a/unittests/Format/CMakeLists.txt b/unittests/Format/CMakeLists.txt
index 4a7ab79418..6d48cf8713 100644
--- a/unittests/Format/CMakeLists.txt
+++ b/unittests/Format/CMakeLists.txt
@@ -7,6 +7,7 @@ add_clang_unittest(FormatTests
FormatTestJava.cpp
FormatTestJS.cpp
FormatTestProto.cpp
+ FormatTestSelective.cpp
)
target_link_libraries(FormatTests
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 9791e2a5fb..acb120962e 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -16,19 +16,31 @@
namespace clang {
namespace format {
+namespace {
-FormatStyle getGoogleStyle() {
- return getGoogleStyle(FormatStyle::LK_Cpp);
-}
+FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
class FormatTest : public ::testing::Test {
protected:
- std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
- const FormatStyle &Style) {
+ enum IncompleteCheck {
+ IC_ExpectComplete,
+ IC_ExpectIncomplete,
+ IC_DoNotCheck
+ };
+
+ std::string format(llvm::StringRef Code,
+ const FormatStyle &Style = getLLVMStyle(),
+ IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
DEBUG(llvm::errs() << "---\n");
DEBUG(llvm::errs() << Code << "\n\n");
- std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
- tooling::Replacements Replaces = reformat(Style, Code, Ranges);
+ std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
+ bool IncompleteFormat = false;
+ tooling::Replacements Replaces =
+ reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
+ if (CheckIncomplete != IC_DoNotCheck) {
+ bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
+ EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
+ }
ReplacementCount = Replaces.size();
std::string Result = applyAllReplacements(Code, Replaces);
EXPECT_NE("", Result);
@@ -36,11 +48,6 @@ protected:
return Result;
}
- std::string
- format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
- return format(Code, 0, Code.size(), Style);
- }
-
FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = ColumnLimit;
@@ -58,6 +65,12 @@ protected:
EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
}
+ void verifyIncompleteFormat(llvm::StringRef Code,
+ const FormatStyle &Style = getLLVMStyle()) {
+ EXPECT_EQ(Code.str(),
+ format(test::messUp(Code), Style, IC_ExpectIncomplete));
+ }
+
void verifyGoogleFormat(llvm::StringRef Code) {
verifyFormat(Code, getGoogleStyle());
}
@@ -70,7 +83,7 @@ protected:
/// \brief Verify that clang-format does not crash on the given input.
void verifyNoCrash(llvm::StringRef Code,
const FormatStyle &Style = getLLVMStyle()) {
- format(Code, Style);
+ format(Code, Style, IC_DoNotCheck);
}
int ReplacementCount;
@@ -135,55 +148,6 @@ TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
EXPECT_EQ(0, ReplacementCount);
}
-TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
- EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a;", format("int a; "));
- EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
- EXPECT_EQ("int a;\nint b; ",
- format("int a; \nint b; ", 0, 0, getLLVMStyle()));
-}
-
-TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
- EXPECT_EQ("int b;\nint a;",
- format("int b;\n int a;", 7, 0, getLLVMStyle()));
- EXPECT_EQ("int b;\n int a;",
- format("int b;\n int a;", 6, 0, getLLVMStyle()));
-
- EXPECT_EQ("#define A \\\n"
- " int a; \\\n"
- " int b;",
- format("#define A \\\n"
- " int a; \\\n"
- " int b;",
- 26, 0, getLLVMStyleWithColumns(12)));
- EXPECT_EQ("#define A \\\n"
- " int a; \\\n"
- " int b;",
- format("#define A \\\n"
- " int a; \\\n"
- " int b;",
- 25, 0, getLLVMStyleWithColumns(12)));
-}
-
-TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
- EXPECT_EQ("int b;\n\nint a;",
- format("int b;\n\nint a;", 8, 0, getLLVMStyle()));
- EXPECT_EQ("int b;\n\nint a;",
- format("int b;\n\nint a;", 7, 0, getLLVMStyle()));
-
- // This might not strictly be correct, but is likely good in all practical
- // cases.
- EXPECT_EQ("int b;\nint a;",
- format("int b;int a;", 7, 0, getLLVMStyle()));
-}
-
-TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
- EXPECT_EQ("int a;\n\n int b;",
- format("int a;\n \n\n int b;", 8, 0, getLLVMStyle()));
- EXPECT_EQ("int a;\n\n int b;",
- format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
-}
-
TEST_F(FormatTest, RemovesEmptyLines) {
EXPECT_EQ("class C {\n"
" int i;\n"
@@ -299,28 +263,15 @@ TEST_F(FormatTest, RemovesEmptyLines) {
"} // namespace"));
}
-TEST_F(FormatTest, ReformatsMovedLines) {
- EXPECT_EQ(
- "template <typename T> T *getFETokenInfo() const {\n"
- " return static_cast<T *>(FETokenInfo);\n"
- "}\n"
- " int a; // <- Should not be formatted",
- format(
- "template<typename T>\n"
- "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
- " int a; // <- Should not be formatted",
- 9, 5, getLLVMStyle()));
-}
-
TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
- verifyFormat("x = (a) and (b);");
- verifyFormat("x = (a) or (b);");
- verifyFormat("x = (a) bitand (b);");
- verifyFormat("x = (a) bitor (b);");
- verifyFormat("x = (a) not_eq (b);");
- verifyFormat("x = (a) and_eq (b);");
- verifyFormat("x = (a) or_eq (b);");
- verifyFormat("x = (a) xor (b);");
+ verifyFormat("x = (a) and (b);");
+ verifyFormat("x = (a) or (b);");
+ verifyFormat("x = (a) bitand (b);");
+ verifyFormat("x = (a) bitor (b);");
+ verifyFormat("x = (a) not_eq (b);");
+ verifyFormat("x = (a) and_eq (b);");
+ verifyFormat("x = (a) or_eq (b);");
+ verifyFormat("x = (a) xor (b);");
}
//===----------------------------------------------------------------------===//
@@ -360,10 +311,6 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
"}",
AllowsMergedIf);
- EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
- EXPECT_EQ("if (a) return; // comment",
- format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
-
AllowsMergedIf.ColumnLimit = 14;
verifyFormat("if (a) return;", AllowsMergedIf);
verifyFormat("if (aaaaaaaaa)\n"
@@ -419,6 +366,12 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
" f();\n"
"}",
AllowSimpleBracedStatements);
+ verifyFormat("if (true) {\n"
+ " f();\n"
+ "} else {\n"
+ " f();\n"
+ "}",
+ AllowSimpleBracedStatements);
verifyFormat("template <int> struct A2 {\n"
" struct B {};\n"
@@ -430,6 +383,12 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
" f();\n"
"}",
AllowSimpleBracedStatements);
+ verifyFormat("if (true) {\n"
+ " f();\n"
+ "} else {\n"
+ " f();\n"
+ "}",
+ AllowSimpleBracedStatements);
AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
verifyFormat("while (true) {\n"
@@ -528,6 +487,11 @@ TEST_F(FormatTest, FormatsForLoop) {
" I = FD->getDeclsInPrototypeScope().begin(),\n"
" E = FD->getDeclsInPrototypeScope().end();\n"
" I != E; ++I) {\n}");
+ verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
+ " I = Container.begin(),\n"
+ " E = Container.end();\n"
+ " I != E; ++I) {\n}",
+ getLLVMStyleWithColumns(76));
verifyFormat(
"for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
@@ -582,6 +546,18 @@ TEST_F(FormatTest, ForEachLoops) {
" BOOST_FOREACH (Item *item, itemlist) {}\n"
" UNKNOWN_FORACH(Item * item, itemlist) {}\n"
"}");
+
+ // As function-like macros.
+ verifyFormat("#define foreach(x, y)\n"
+ "#define Q_FOREACH(x, y)\n"
+ "#define BOOST_FOREACH(x, y)\n"
+ "#define UNKNOWN_FOREACH(x, y)\n");
+
+ // Not as function-like macros.
+ verifyFormat("#define foreach (x, y)\n"
+ "#define Q_FOREACH (x, y)\n"
+ "#define BOOST_FOREACH (x, y)\n"
+ "#define UNKNOWN_FOREACH (x, y)\n");
}
TEST_F(FormatTest, FormatsWhileLoop) {
@@ -677,11 +653,17 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
" switch (x) { \\\n"
" case a: \\\n"
" foo = b; \\\n"
- " }", getLLVMStyleWithColumns(20));
+ " }",
+ getLLVMStyleWithColumns(20));
verifyFormat("#define OPERATION_CASE(name) \\\n"
" case OP_name: \\\n"
" return operations::Operation##name\n",
getLLVMStyleWithColumns(40));
+ verifyFormat("switch (x) {\n"
+ "case 1:;\n"
+ "default:;\n"
+ " int i;\n"
+ "}");
verifyGoogleFormat("switch (x) {\n"
" case 1:\n"
@@ -817,6 +799,11 @@ TEST_F(FormatTest, FormatsLabels) {
"test_label:\n"
" some_other_code();\n"
"}");
+ verifyFormat("{\n"
+ " some_code();\n"
+ "test_label:;\n"
+ " int i = 0;\n"
+ "}");
}
//===----------------------------------------------------------------------===//
@@ -1022,11 +1009,10 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
" // at start\n"
"}"));
- verifyFormat(
- "#define A \\\n"
- " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
- " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
- getLLVMStyleWithColumns(60));
+ verifyFormat("#define A \\\n"
+ " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
+ " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
+ getLLVMStyleWithColumns(60));
verifyFormat(
"#define A \\\n"
" int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
@@ -1047,6 +1033,8 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
verifyNoCrash("/\\\n/");
verifyNoCrash("/\\\n* */");
+ // The 0-character somehow makes the lexer return a proper comment.
+ verifyNoCrash(StringRef("/*\\\0\n/", 6));
}
TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
@@ -1061,7 +1049,7 @@ TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
" c);",
format("SomeFunction(a,\n"
" b,\n"
- " // comment\n"
+ " // comment\n"
" c);"));
EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
" c);",
@@ -1075,65 +1063,6 @@ TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
" c); // comment"));
}
-TEST_F(FormatTest, CanFormatCommentsLocally) {
- EXPECT_EQ("int a; // comment\n"
- "int b; // comment",
- format("int a; // comment\n"
- "int b; // comment",
- 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a; // comment\n"
- " // line 2\n"
- "int b;",
- format("int a; // comment\n"
- " // line 2\n"
- "int b;",
- 28, 0, getLLVMStyle()));
- EXPECT_EQ("int aaaaaa; // comment\n"
- "int b;\n"
- "int c; // unrelated comment",
- format("int aaaaaa; // comment\n"
- "int b;\n"
- "int c; // unrelated comment",
- 31, 0, getLLVMStyle()));
-
- EXPECT_EQ("int a; // This\n"
- " // is\n"
- " // a",
- format("int a; // This\n"
- " // is\n"
- " // a",
- 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a; // This\n"
- " // is\n"
- " // a\n"
- "// This is b\n"
- "int b;",
- format("int a; // This\n"
- " // is\n"
- " // a\n"
- "// This is b\n"
- "int b;",
- 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a; // This\n"
- " // is\n"
- " // a\n"
- "\n"
- " // This is unrelated",
- format("int a; // This\n"
- " // is\n"
- " // a\n"
- "\n"
- " // This is unrelated",
- 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a;\n"
- "// This is\n"
- "// not formatted. ",
- format("int a;\n"
- "// This is\n"
- "// not formatted. ",
- 0, 0, getLLVMStyle()));
-}
-
TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
EXPECT_EQ("// comment", format("// comment "));
EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
@@ -1291,16 +1220,25 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
"// one line",
format("// A comment that doesn't fit on one line",
getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("/// A comment that\n"
+ "/// doesn't fit on\n"
+ "/// one line",
+ format("/// A comment that doesn't fit on one line",
+ getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("//! A comment that\n"
+ "//! doesn't fit on\n"
+ "//! one line",
+ format("//! A comment that doesn't fit on one line",
+ getLLVMStyleWithColumns(20)));
EXPECT_EQ("// a b c d\n"
"// e f g\n"
"// h i j k",
- format("// a b c d e f g h i j k",
- getLLVMStyleWithColumns(10)));
- EXPECT_EQ("// a b c d\n"
- "// e f g\n"
- "// h i j k",
- format("\\\n// a b c d e f g h i j k",
- getLLVMStyleWithColumns(10)));
+ format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
+ EXPECT_EQ(
+ "// a b c d\n"
+ "// e f g\n"
+ "// h i j k",
+ format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
EXPECT_EQ("if (true) // A comment that\n"
" // doesn't fit on\n"
" // one line",
@@ -1312,6 +1250,12 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
EXPECT_EQ("// Add leading\n"
"// whitespace",
format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("/// Add leading\n"
+ "/// whitespace",
+ format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("//! Add leading\n"
+ "//! whitespace",
+ format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
EXPECT_EQ("// Even if it makes the line exceed the column\n"
"// limit",
@@ -1479,18 +1423,18 @@ TEST_F(FormatTest, SplitsLongLinesInComments) {
"doesn't "
"fit on one line. */",
getLLVMStyleWithColumns(20)));
- EXPECT_EQ("/* a b c d\n"
- " * e f g\n"
- " * h i j k\n"
- " */",
- format("/* a b c d e f g h i j k */",
- getLLVMStyleWithColumns(10)));
- EXPECT_EQ("/* a b c d\n"
- " * e f g\n"
- " * h i j k\n"
- " */",
- format("\\\n/* a b c d e f g h i j k */",
- getLLVMStyleWithColumns(10)));
+ EXPECT_EQ(
+ "/* a b c d\n"
+ " * e f g\n"
+ " * h i j k\n"
+ " */",
+ format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
+ EXPECT_EQ(
+ "/* a b c d\n"
+ " * e f g\n"
+ " * h i j k\n"
+ " */",
+ format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
EXPECT_EQ("/*\n"
"This is a long\n"
"comment that doesn't\n"
@@ -1500,7 +1444,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) {
"This is a long "
"comment that doesn't "
"fit on one line. \n"
- "*/", getLLVMStyleWithColumns(20)));
+ "*/",
+ getLLVMStyleWithColumns(20)));
EXPECT_EQ("/*\n"
" * This is a long\n"
" * comment that\n"
@@ -1512,7 +1457,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) {
" comment that "
" doesn't fit on "
" one line. \n"
- " */", getLLVMStyleWithColumns(20)));
+ " */",
+ getLLVMStyleWithColumns(20)));
EXPECT_EQ("/*\n"
" * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
" * so_it_should_be_broken\n"
@@ -1546,7 +1492,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) {
" doesn't fit on one"
" line 1234567890\n"
"*/\n"
- "}", getLLVMStyleWithColumns(20)));
+ "}",
+ getLLVMStyleWithColumns(20)));
EXPECT_EQ("{\n"
" /*\n"
" * This i s\n"
@@ -1564,7 +1511,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) {
" fit on one l i"
" n e\n"
" */\n"
- "}", getLLVMStyleWithColumns(20)));
+ "}",
+ getLLVMStyleWithColumns(20)));
EXPECT_EQ("/*\n"
" * This is a long\n"
" * comment that\n"
@@ -1573,7 +1521,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) {
" */",
format(" /*\n"
" * This is a long comment that doesn't fit on one line\n"
- " */", getLLVMStyleWithColumns(20)));
+ " */",
+ getLLVMStyleWithColumns(20)));
EXPECT_EQ("{\n"
" if (something) /* This is a\n"
" long\n"
@@ -1608,6 +1557,17 @@ TEST_F(FormatTest, SplitsLongLinesInComments) {
" */",
getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("/**\n"
+ " * multiline block\n"
+ " * comment\n"
+ " *\n"
+ " */",
+ format("/**\n"
+ " * multiline block comment\n"
+ " *\n"
+ " */",
+ getLLVMStyleWithColumns(20)));
+
EXPECT_EQ("/*\n"
"\n"
"\n"
@@ -1867,7 +1827,6 @@ TEST_F(FormatTest, IgnoresIf0Contents) {
"#endif\n"
"Five\n"
"};"));
-
}
//===----------------------------------------------------------------------===//
@@ -1900,6 +1859,13 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
"signals:\n"
" void g();\n"
"};");
+
+ // Don't interpret 'signals' the wrong way.
+ verifyFormat("signals.set();");
+ verifyFormat("for (Signals signals : f()) {\n}");
+ verifyFormat("{\n"
+ " signals.set(); // This needs indentation.\n"
+ "}");
}
TEST_F(FormatTest, SeparatesLogicalBlocks) {
@@ -2254,10 +2220,24 @@ TEST_F(FormatTest, FormatsInlineASM) {
" call [edx][eax*4] // stdcall\n"
" }\n"
"}"));
+ EXPECT_EQ("_asm {\n"
+ " xor eax, eax;\n"
+ " cpuid;\n"
+ "}",
+ format("_asm {\n"
+ " xor eax, eax;\n"
+ " cpuid;\n"
+ "}"));
verifyFormat("void function() {\n"
" // comment\n"
" asm(\"\");\n"
"}");
+ EXPECT_EQ("__asm {\n"
+ "}\n"
+ "int i;",
+ format("__asm {\n"
+ "}\n"
+ "int i;"));
}
TEST_F(FormatTest, FormatTryCatch) {
@@ -2282,7 +2262,7 @@ TEST_F(FormatTest, FormatTryCatch) {
"};\n");
// Incomplete try-catch blocks.
- verifyFormat("try {} catch (");
+ verifyIncompleteFormat("try {} catch (");
}
TEST_F(FormatTest, FormatSEHTryCatch) {
@@ -2391,9 +2371,9 @@ TEST_F(FormatTest, FormatObjCTryCatch) {
TEST_F(FormatTest, StaticInitializers) {
verifyFormat("static SomeClass SC = {1, 'a'};");
- verifyFormat(
- "static SomeClass WithALoooooooooooooooooooongName = {\n"
- " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
+ verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
+ " 100000000, "
+ "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
// Here, everything other than the "}" would fit on a line.
verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
@@ -2447,9 +2427,9 @@ TEST_F(FormatTest, NestedStaticInitializers) {
" {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
" {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
" {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
- verifyFormat(
- "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
- " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
+ verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
+ " {rect.fRight - rect.fLeft, rect.fBottom - "
+ "rect.fTop}};");
verifyFormat(
"SomeArrayOfSomeType a = {\n"
@@ -2468,24 +2448,22 @@ TEST_F(FormatTest, NestedStaticInitializers) {
" {{1, 2, 3}},\n"
" {{1, 2, 3}}};");
- verifyFormat(
- "struct {\n"
- " unsigned bit;\n"
- " const char *const name;\n"
- "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
- " {kOsWin, \"Windows\"},\n"
- " {kOsLinux, \"Linux\"},\n"
- " {kOsCrOS, \"Chrome OS\"}};");
- verifyFormat(
- "struct {\n"
- " unsigned bit;\n"
- " const char *const name;\n"
- "} kBitsToOs[] = {\n"
- " {kOsMac, \"Mac\"},\n"
- " {kOsWin, \"Windows\"},\n"
- " {kOsLinux, \"Linux\"},\n"
- " {kOsCrOS, \"Chrome OS\"},\n"
- "};");
+ verifyFormat("struct {\n"
+ " unsigned bit;\n"
+ " const char *const name;\n"
+ "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
+ " {kOsWin, \"Windows\"},\n"
+ " {kOsLinux, \"Linux\"},\n"
+ " {kOsCrOS, \"Chrome OS\"}};");
+ verifyFormat("struct {\n"
+ " unsigned bit;\n"
+ " const char *const name;\n"
+ "} kBitsToOs[] = {\n"
+ " {kOsMac, \"Mac\"},\n"
+ " {kOsWin, \"Windows\"},\n"
+ " {kOsLinux, \"Linux\"},\n"
+ " {kOsCrOS, \"Chrome OS\"},\n"
+ "};");
}
TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
@@ -2497,6 +2475,14 @@ TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
verifyFormat("virtual void write(ELFWriter *writerrr,\n"
" OwningPtr<FileOutputBuffer> &buffer) = 0;");
+
+ // Do break defaulted and deleted functions.
+ verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
+ " default;",
+ getLLVMStyleWithColumns(40));
+ verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
+ " delete;",
+ getLLVMStyleWithColumns(40));
}
TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
@@ -2596,31 +2582,6 @@ TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
-TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
- EXPECT_EQ("int i;\n"
- "#define A \\\n"
- " int i; \\\n"
- " int j\n"
- "int k;",
- format("int i;\n"
- "#define A \\\n"
- " int i ; \\\n"
- " int j\n"
- "int k;",
- 8, 0, getGoogleStyle())); // 8: position of "#define".
- EXPECT_EQ("int i;\n"
- "#define A \\\n"
- " int i; \\\n"
- " int j\n"
- "int k;",
- format("int i;\n"
- "#define A \\\n"
- " int i ; \\\n"
- " int j\n"
- "int k;",
- 45, 0, getGoogleStyle())); // 45: position of "j".
-}
-
TEST_F(FormatTest, MacroDefinitionInsideStatement) {
EXPECT_EQ("int x,\n"
"#define A\n"
@@ -2679,23 +2640,24 @@ TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
}
TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
- verifyFormat("#define A :");
+ verifyIncompleteFormat("#define A :");
verifyFormat("#define SOMECASES \\\n"
" case 1: \\\n"
" case 2\n",
getLLVMStyleWithColumns(20));
verifyFormat("#define A template <typename T>");
- verifyFormat("#define STR(x) #x\n"
- "f(STR(this_is_a_string_literal{));");
+ verifyIncompleteFormat("#define STR(x) #x\n"
+ "f(STR(this_is_a_string_literal{));");
verifyFormat("#pragma omp threadprivate( \\\n"
" y)), // expected-warning",
getLLVMStyleWithColumns(28));
verifyFormat("#d, = };");
verifyFormat("#if \"a");
- verifyFormat("({\n"
- "#define b }\\\n"
- " a\n"
- "a");
+ verifyIncompleteFormat("({\n"
+ "#define b \\\n"
+ " } \\\n"
+ " a\n"
+ "a", getLLVMStyleWithColumns(15));
verifyFormat("#define A \\\n"
" { \\\n"
" {\n"
@@ -2703,7 +2665,6 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
" } \\\n"
" }",
getLLVMStyleWithColumns(15));
-
verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
@@ -2722,6 +2683,10 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
"\n"
" A() {\n}\n"
"} ;"));
+ EXPECT_EQ("MACRO\n"
+ "/*static*/ int i;",
+ format("MACRO\n"
+ " /*static*/ int i;"));
EXPECT_EQ("SOME_MACRO\n"
"namespace {\n"
"void f();\n"
@@ -2731,10 +2696,8 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
"void f( );\n"
"}"));
// Only if the identifier contains at least 5 characters.
- EXPECT_EQ("HTTP f();",
- format("HTTP\nf();"));
- EXPECT_EQ("MACRO\nf();",
- format("MACRO\nf();"));
+ EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
+ EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
// Only if everything is upper case.
EXPECT_EQ("class A : public QObject {\n"
" Q_Object A() {}\n"
@@ -2750,7 +2713,8 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
"<< SomeThing;"));
verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
- "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp));
+ "(n, buffers))\n",
+ getChromiumStyle(FormatStyle::LK_Cpp));
}
TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
@@ -2872,34 +2836,34 @@ TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
" A(X x)\n"
" try : t(0) {} catch (...) {}\n"
"};"));
- EXPECT_EQ(
- "class SomeClass {\n"
- "public:\n"
- " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
- "};",
- format("class SomeClass {\n"
- "public:\n"
- " SomeClass()\n"
- " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
- "};"));
- EXPECT_EQ(
- "class SomeClass {\n"
- "public:\n"
- " SomeClass()\n"
- " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
- "};",
- format("class SomeClass {\n"
- "public:\n"
- " SomeClass()\n"
- " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
- "};", getLLVMStyleWithColumns(40)));
+ EXPECT_EQ("class SomeClass {\n"
+ "public:\n"
+ " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
+ "};",
+ format("class SomeClass {\n"
+ "public:\n"
+ " SomeClass()\n"
+ " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
+ "};"));
+ EXPECT_EQ("class SomeClass {\n"
+ "public:\n"
+ " SomeClass()\n"
+ " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
+ "};",
+ format("class SomeClass {\n"
+ "public:\n"
+ " SomeClass()\n"
+ " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
+ "};",
+ getLLVMStyleWithColumns(40)));
}
TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
verifyFormat("#define A \\\n"
" f({ \\\n"
" g(); \\\n"
- " });", getLLVMStyleWithColumns(11));
+ " });",
+ getLLVMStyleWithColumns(11));
}
TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
@@ -2917,15 +2881,15 @@ TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
}
-TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
+TEST_F(FormatTest, EscapedNewlines) {
EXPECT_EQ(
"#define A \\\n int i; \\\n int j;",
format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
+ EXPECT_EQ(
+ "#define A\n\nint i;", format("#define A \\\n\n int i;"));
EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
-}
-
-TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
+ EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
}
TEST_F(FormatTest, DontCrashOnBlockComments) {
@@ -3018,12 +2982,11 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
getLLVMStyleWithColumns(28));
verifyFormat("#if 1\n"
"int i;");
- verifyFormat(
- "#if 1\n"
- "#endif\n"
- "#if 1\n"
- "#else\n"
- "#endif\n");
+ verifyFormat("#if 1\n"
+ "#endif\n"
+ "#if 1\n"
+ "#else\n"
+ "#endif\n");
verifyFormat("DEBUG({\n"
" return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
@@ -3032,11 +2995,11 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
"#else\n"
"#endif");
- verifyFormat("void f(\n"
- "#if A\n"
- " );\n"
- "#else\n"
- "#endif");
+ verifyIncompleteFormat("void f(\n"
+ "#if A\n"
+ " );\n"
+ "#else\n"
+ "#endif");
}
TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
@@ -3047,14 +3010,13 @@ TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
FormatStyle SingleLine = getLLVMStyle();
SingleLine.AllowShortIfStatementsOnASingleLine = true;
- verifyFormat(
- "#if 0\n"
- "#elif 1\n"
- "#endif\n"
- "void foo() {\n"
- " if (test) foo2();\n"
- "}",
- SingleLine);
+ verifyFormat("#if 0\n"
+ "#elif 1\n"
+ "#endif\n"
+ "void foo() {\n"
+ " if (test) foo2();\n"
+ "}",
+ SingleLine);
}
TEST_F(FormatTest, LayoutBlockInsideParens) {
@@ -3091,7 +3053,7 @@ TEST_F(FormatTest, LayoutBlockInsideParens) {
" int j;\n"
"});");
verifyFormat(
- "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
+ "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
" {\n"
" int i; // break\n"
" },\n"
@@ -3181,75 +3143,25 @@ TEST_F(FormatTest, LayoutNestedBlocks) {
verifyNoCrash("^{v^{a}}");
}
-TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
- EXPECT_EQ("DEBUG({\n"
- " int i;\n"
- " int j;\n"
- "});",
- format("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- 20, 1, getLLVMStyle()));
- EXPECT_EQ("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- format("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- 41, 1, getLLVMStyle()));
- EXPECT_EQ("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- format("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- 41, 1, getLLVMStyle()));
- EXPECT_EQ("DEBUG({\n"
- " int i;\n"
- " int j;\n"
- "});",
- format("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- 20, 1, getLLVMStyle()));
-
- EXPECT_EQ("Debug({\n"
- " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
- " return;\n"
- " },\n"
- " a);",
- format("Debug({\n"
- " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
- " return;\n"
- " },\n"
- " a);",
- 50, 1, getLLVMStyle()));
- EXPECT_EQ("DEBUG({\n"
- " DEBUG({\n"
- " int a;\n"
- " int b;\n"
- " }) ;\n"
- "});",
- format("DEBUG({\n"
- " DEBUG({\n"
- " int a;\n"
- " int b;\n" // Format this line only.
- " }) ;\n" // Don't touch this line.
- "});",
- 35, 0, getLLVMStyle()));
- EXPECT_EQ("DEBUG({\n"
- " int a; //\n"
- "});",
- format("DEBUG({\n"
- " int a; //\n"
- "});",
- 0, 0, getLLVMStyle()));
+TEST_F(FormatTest, FormatNestedBlocksInMacros) {
+ EXPECT_EQ("#define MACRO() \\\n"
+ " Debug(aaa, /* force line break */ \\\n"
+ " { \\\n"
+ " int i; \\\n"
+ " int j; \\\n"
+ " })",
+ format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
+ " { int i; int j; })",
+ getGoogleStyle()));
+
+ EXPECT_EQ("#define A \\\n"
+ " [] { \\\n"
+ " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
+ " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
+ " }",
+ format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
+ getGoogleStyle()));
}
TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
@@ -3344,10 +3256,9 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
"}");
// Even explicit parentheses stress the precedence enough to make the
// additional break unnecessary.
- verifyFormat(
- "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
- "}");
+ verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
+ "}");
// This cases is borderline, but with the indentation it is still readable.
verifyFormat(
"if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -3358,11 +3269,10 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
// If the LHS is a binary expression, we should still use the additional break
// as otherwise the formatting hides the operator precedence.
- verifyFormat(
- "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
- " 5) {\n"
- "}");
+ verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
+ " 5) {\n"
+ "}");
FormatStyle OnePerLine = getLLVMStyle();
OnePerLine.BinPackParameters = false;
@@ -3453,9 +3363,14 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
" // comment\n"
" + b;",
Style);
- verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
- " + cc;",
+ verifyFormat(
+ "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " + cc;",
+ Style);
+
+ verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
Style);
// Forced by comments.
@@ -3481,17 +3396,16 @@ TEST_F(FormatTest, NoOperandAlignment) {
FormatStyle Style = getLLVMStyle();
Style.AlignOperands = false;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
- verifyFormat(
- "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
- " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
- " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " > ccccccccccccccccccccccccccccccccccccccccc;",
- Style);
+ verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " > ccccccccccccccccccccccccccccccccccccccccc;",
+ Style);
verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
@@ -3703,10 +3617,9 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
// 1) break amongst arguments.
verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
" Cccccccccccccc cccccccccccccc);");
- verifyFormat(
- "template <class TemplateIt>\n"
- "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
- " TemplateIt *stop) {}");
+ verifyFormat("template <class TemplateIt>\n"
+ "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
+ " TemplateIt *stop) {}");
// 2) break after return type.
verifyFormat(
@@ -3793,10 +3706,9 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
Style);
- verifyFormat(
- "void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
- Style);
+ verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
+ Style);
}
TEST_F(FormatTest, TrailingReturnType) {
@@ -3810,6 +3722,9 @@ TEST_F(FormatTest, TrailingReturnType) {
verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
" -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
+ verifyFormat("template <typename T>\n"
+ "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
+ " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
// Not trailing return types.
verifyFormat("void f() { auto a = b->c(); }");
@@ -3903,6 +3818,23 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
verifyGoogleFormat(
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
" aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
+ verifyGoogleFormat(
+ "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaa;");
+}
+
+TEST_F(FormatTest, FunctionAnnotations) {
+ verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
+ "string OldFunction(const string &parameter) {}");
+ verifyFormat("template <typename T>\n"
+ "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
+ "string OldFunction(const string &parameter) {}");
+
+ // Not function annotations.
+ verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
+ verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
+ " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
}
TEST_F(FormatTest, BreaksDesireably) {
@@ -3941,10 +3873,9 @@ TEST_F(FormatTest, BreaksDesireably) {
verifyFormat(
"aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
- verifyFormat(
- "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+ verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
// Indent consistently independent of call expression.
verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
@@ -4085,14 +4016,13 @@ TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
}
TEST_F(FormatTest, FormatsBuilderPattern) {
- verifyFormat(
- "return llvm::StringSwitch<Reference::Kind>(name)\n"
- " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
- " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
- " .StartsWith(\".init\", ORDER_INIT)\n"
- " .StartsWith(\".fini\", ORDER_FINI)\n"
- " .StartsWith(\".hash\", ORDER_HASH)\n"
- " .Default(ORDER_TEXT);\n");
+ verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
+ " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
+ " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
+ " .StartsWith(\".init\", ORDER_INIT)\n"
+ " .StartsWith(\".fini\", ORDER_FINI)\n"
+ " .StartsWith(\".hash\", ORDER_HASH)\n"
+ " .Default(ORDER_TEXT);\n");
verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
" aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
@@ -4142,6 +4072,18 @@ TEST_F(FormatTest, FormatsBuilderPattern) {
" ->aaaaaaaaaaaaaae(0)\n"
" ->aaaaaaaaaaaaaaa();");
+ // Don't linewrap after very short segments.
+ verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
+ verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
+ verifyFormat("aaa()\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
+
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
" .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
" .has<bbbbbbbbbbbbbbbbbbbbb>();");
@@ -4268,18 +4210,15 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
" aaaaaaaaaaaaaaaaaaaaa));");
FormatStyle Style = getLLVMStyle();
Style.AlignAfterOpenBracket = false;
- verifyFormat(
- "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
- Style);
- verifyFormat(
- "SomeLongVariableName->someVeryLongFunctionName(\n"
- " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
- Style);
- verifyFormat(
- "SomeLongVariableName->someFunction(\n"
- " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
- Style);
+ verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
+ Style);
+ verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
+ " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
+ Style);
+ verifyFormat("SomeLongVariableName->someFunction(\n"
+ " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
+ Style);
verifyFormat(
"void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
" aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
@@ -4372,13 +4311,12 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" // comment\n"
" ? aaaa\n"
" : bbbb;");
- verifyFormat(
- "unsigned Indent =\n"
- " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
- " ? IndentForLevel[TheLine.Level]\n"
- " : TheLine * 2,\n"
- " TheLine.InPPDirective, PreviousEndOfLineColumn);",
- getLLVMStyleWithColumns(70));
+ verifyFormat("unsigned Indent =\n"
+ " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
+ " ? IndentForLevel[TheLine.Level]\n"
+ " : TheLine * 2,\n"
+ " TheLine.InPPDirective, PreviousEndOfLineColumn);",
+ getLLVMStyleWithColumns(70));
verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
" ? aaaaaaaaaaaaaaa\n"
" : bbbbbbbbbbbbbbb //\n"
@@ -4563,10 +4501,9 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
" *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
" *b = bbbbbbbbbbbbbbbbbbb;",
Style);
- verifyFormat(
- "aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
- " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
- Style);
+ verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
+ " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
+ Style);
}
TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
@@ -4619,44 +4556,53 @@ TEST_F(FormatTest, AlignsStringLiterals) {
" \"jkl\");");
verifyFormat("f(L\"a\"\n"
- " L\"b\")");
+ " L\"b\");");
verifyFormat("#define A(X) \\\n"
" L\"aaaaa\" #X L\"bbbbbb\" \\\n"
" L\"ccccc\"",
getLLVMStyleWithColumns(25));
+
+ verifyFormat("f(@\"a\"\n"
+ " @\"b\");");
+ verifyFormat("NSString s = @\"a\"\n"
+ " @\"b\"\n"
+ " @\"c\";");
+ verifyFormat("NSString s = @\"a\"\n"
+ " \"b\"\n"
+ " \"c\";");
}
TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
FormatStyle AfterType = getLLVMStyle();
AfterType.AlwaysBreakAfterDefinitionReturnType = true;
verifyFormat("const char *\n"
- "f(void) {\n" // Break here.
+ "f(void) {\n" // Break here.
" return \"\";\n"
"}\n"
- "const char *bar(void);\n", // No break here.
+ "const char *bar(void);\n", // No break here.
AfterType);
verifyFormat("template <class T>\n"
"T *\n"
- "f(T &c) {\n" // Break here.
+ "f(T &c) {\n" // Break here.
" return NULL;\n"
"}\n"
- "template <class T> T *f(T &c);\n", // No break here.
+ "template <class T> T *f(T &c);\n", // No break here.
AfterType);
AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
verifyFormat("const char *\n"
- "f(void)\n" // Break here.
+ "f(void)\n" // Break here.
"{\n"
" return \"\";\n"
"}\n"
- "const char *bar(void);\n", // No break here.
+ "const char *bar(void);\n", // No break here.
AfterType);
verifyFormat("template <class T>\n"
- "T *\n" // Problem here: no line break
- "f(T &c)\n" // Break here.
+ "T *\n" // Problem here: no line break
+ "f(T &c)\n" // Break here.
"{\n"
" return NULL;\n"
"}\n"
- "template <class T> T *f(T &c);\n", // No break here.
+ "template <class T> T *f(T &c);\n", // No break here.
AfterType);
}
@@ -4721,9 +4667,9 @@ TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
// Exempt ObjC strings for now.
EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
- " \"bbbb\";",
+ " @\"bbbb\";",
format("NSString *const kString = @\"aaaa\"\n"
- "\"bbbb\";",
+ "@\"bbbb\";",
Break));
Break.ColumnLimit = 0;
@@ -4749,15 +4695,12 @@ TEST_F(FormatTest, AlignsPipes) {
"aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
- verifyFormat(
- "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
- verifyFormat(
- "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
- " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
+ verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
+ " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
+ verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
+ " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
verifyFormat(
"llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
@@ -4787,6 +4730,13 @@ TEST_F(FormatTest, AlignsPipes) {
"}");
verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
+ verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaa)\n"
+ " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
+ verifyFormat("LOG_IF(aaa == //\n"
+ " bbb)\n"
+ " << a << b;");
// Breaking before the first "<<" is generally not desirable.
verifyFormat(
@@ -4891,12 +4841,11 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
" aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
"}");
- verifyFormat(
- "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
- " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
+ verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
+ " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
@@ -5098,6 +5047,9 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
verifyFormat("f<int>();");
verifyFormat("template <typename T> void f() {}");
verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
+ verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
+ "sizeof(char)>::type>;");
+ verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
// Not template parameters.
verifyFormat("return a < b && c > d;");
@@ -5113,6 +5065,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
getLLVMStyleWithColumns(60));
verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
+ verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
}
TEST_F(FormatTest, UnderstandsBinaryOperators) {
@@ -5298,6 +5251,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyIndependentOfContext("int a = *b;");
verifyIndependentOfContext("int a = *b * c;");
verifyIndependentOfContext("int a = b * *c;");
+ verifyIndependentOfContext("int a = b * (10);");
+ verifyIndependentOfContext("S << b * (10);");
verifyIndependentOfContext("return 10 * b;");
verifyIndependentOfContext("return *b * *c;");
verifyIndependentOfContext("return a & ~b;");
@@ -5330,13 +5285,16 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("auto PointerBinding = [](const char *S) {};");
verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
verifyFormat("[](const decltype(*a) &value) {}");
+ verifyFormat("#define MACRO() [](A *a) { return 1; }");
verifyIndependentOfContext("typedef void (*f)(int *a);");
verifyIndependentOfContext("int i{a * b};");
verifyIndependentOfContext("aaa && aaa->f();");
verifyIndependentOfContext("int x = ~*p;");
verifyFormat("Constructor() : a(a), area(width * height) {}");
verifyFormat("Constructor() : a(a), area(a, width * height) {}");
+ verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
verifyFormat("void f() { f(a, c * d); }");
+ verifyFormat("void f() { f(new a(), c * d); }");
verifyIndependentOfContext("InvalidRegions[*R] = 0;");
@@ -5421,8 +5379,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyIndependentOfContext("A = new SomeType *[Length]();");
verifyIndependentOfContext("T **t = new T *;");
verifyIndependentOfContext("T **t = new T *();");
- verifyGoogleFormat("A = new SomeType* [Length]();");
- verifyGoogleFormat("A = new SomeType* [Length];");
+ verifyGoogleFormat("A = new SomeType*[Length]();");
+ verifyGoogleFormat("A = new SomeType*[Length];");
verifyGoogleFormat("T** t = new T*;");
verifyGoogleFormat("T** t = new T*();");
@@ -5490,8 +5448,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("A<int **> a;", PointerMiddle);
verifyFormat("A<int *, int *> a;", PointerMiddle);
verifyFormat("A<int * []> a;", PointerMiddle);
- verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
- verifyFormat("A = new SomeType * [Length];", PointerMiddle);
+ verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
+ verifyFormat("A = new SomeType *[Length];", PointerMiddle);
verifyFormat("T ** t = new T *;", PointerMiddle);
}
@@ -5597,6 +5555,8 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("my_int a = (const my_int)-1;");
verifyFormat("my_int a = (const my_int *)-1;");
verifyFormat("my_int a = (my_int)(my_int)-1;");
+ verifyFormat("my_int a = (ns::my_int)-2;");
+ verifyFormat("case (my_int)ONE:");
// FIXME: single value wrapped with paren will be treated as cast.
verifyFormat("void f(int i = (kValue)*kMask) {}");
@@ -5689,6 +5649,8 @@ TEST_F(FormatTest, BreaksLongVariableDeclarations) {
" LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
" LoooooooooooooooooooooooooooooooooooooooongVariable({});");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
+ " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
}
TEST_F(FormatTest, BreaksLongDeclarations) {
@@ -5769,6 +5731,13 @@ TEST_F(FormatTest, BreaksLongDeclarations) {
verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
" const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+ verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaa);");
+ verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
}
TEST_F(FormatTest, FormatsArrays) {
@@ -5809,6 +5778,7 @@ TEST_F(FormatTest, HandlesIncludeDirectives) {
"#include \"string.h\"\n"
"#include <a-a>\n"
"#include < path with space >\n"
+ "#include_next <test.h>"
"#include \"abc.h\" // this is included for ABC\n"
"#include \"some long include\" // with a comment\n"
"#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
@@ -5938,16 +5908,16 @@ TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
TEST_F(FormatTest, IncorrectCodeMissingParens) {
verifyFormat("if {\n foo;\n foo();\n}");
verifyFormat("switch {\n foo;\n foo();\n}");
- verifyFormat("for {\n foo;\n foo();\n}");
+ verifyIncompleteFormat("for {\n foo;\n foo();\n}");
verifyFormat("while {\n foo;\n foo();\n}");
verifyFormat("do {\n foo;\n foo();\n} while;");
}
TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
- verifyFormat("namespace {\n"
- "class Foo { Foo (\n"
- "};\n"
- "} // comment");
+ verifyIncompleteFormat("namespace {\n"
+ "class Foo { Foo (\n"
+ "};\n"
+ "} // comment");
}
TEST_F(FormatTest, IncorrectCodeErrorDetection) {
@@ -6015,10 +5985,18 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
" aaaa,\n"
" },\n"
"};");
+ verifyFormat("class C : public D {\n"
+ " SomeClass SC{2};\n"
+ "};");
+ verifyFormat("class C : public A {\n"
+ " class D : public B {\n"
+ " void f() { int i{2}; }\n"
+ " };\n"
+ "};");
- // In combination with BinPackParameters = false.
+ // In combination with BinPackArguments = false.
FormatStyle NoBinPacking = getLLVMStyle();
- NoBinPacking.BinPackParameters = false;
+ NoBinPacking.BinPackArguments = false;
verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
" bbbbb,\n"
" ccccc,\n"
@@ -6132,9 +6110,18 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
ExtraSpaces);
verifyFormat(
"std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
- " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
- " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
+ " aaaaaaa,\n"
+ " aaaaaaaaaa,\n"
+ " aaaaa,\n"
+ " aaaaaaaaaaaaaaa,\n"
+ " aaa,\n"
+ " aaaaaaaaaa,\n"
+ " a,\n"
+ " aaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaa,\n"
+ " a};");
verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
}
@@ -6145,11 +6132,9 @@ 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};");
- verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
- " // line comment\n"
+ verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
" 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
- " 1, 22, 333, 4444, 55555,\n"
- " // line comment\n"
+ " 1, 22, 333, 4444, 55555, //\n"
" 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
" 1, 22, 333, 4444, 55555, 666666, 7777777};");
verifyFormat(
@@ -6163,6 +6148,14 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
" X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
" X86::R8, X86::R9, X86::R10, X86::R11, 0};");
+ verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
+ " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
+ " // Separating comment.\n"
+ " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
+ verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
+ " // Leading comment\n"
+ " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
+ " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
" 1, 1, 1, 1};",
getLLVMStyleWithColumns(39));
@@ -6172,6 +6165,24 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
getLLVMStyleWithColumns(43));
+ verifyFormat(
+ "static unsigned SomeValues[10][3] = {\n"
+ " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
+ " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
+ verifyFormat("static auto fields = new vector<string>{\n"
+ " \"aaaaaaaaaaaaa\",\n"
+ " \"aaaaaaaaaaaaa\",\n"
+ " \"aaaaaaaaaaaa\",\n"
+ " \"aaaaaaaaaaaaaa\",\n"
+ " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
+ " \"aaaaaaaaaaaa\",\n"
+ " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
+ "};");
+ verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
+ verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
+ " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
+ " 3, cccccccccccccccccccccc};",
+ getLLVMStyleWithColumns(60));
// Trailing commas.
verifyFormat("vector<int> x = {\n"
@@ -6186,15 +6197,21 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
" 1, 1, 1, 1,\n"
" /**/ /**/};",
getLLVMStyleWithColumns(39));
+
+ // Trailing comment in the first line.
+ verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
+ " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
+ " 111111111, 222222222, 3333333333, 444444444, //\n"
+ " 11111111, 22222222, 333333333, 44444444};");
+
+ // With nested lists, we should either format one item per line or all nested
+ // lists one on line.
+ // FIXME: For some nested lists, we can do better.
verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
" {aaaaaaaaaaaaaaaaaaa},\n"
" {aaaaaaaaaaaaaaaaaaaaa},\n"
" {aaaaaaaaaaaaaaaaa}};",
getLLVMStyleWithColumns(60));
-
- // With nested lists, we should either format one item per line or all nested
- // lists one one line.
- // FIXME: For some nested lists, we can do better.
verifyFormat(
"SomeStruct my_struct_array = {\n"
" {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
@@ -6264,7 +6281,8 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
EXPECT_EQ("class C {\n"
" A() : b(0) {}\n"
- "};", format("class C{A():b(0){}};", NoColumnLimit));
+ "};",
+ format("class C{A():b(0){}};", NoColumnLimit));
EXPECT_EQ("A()\n"
" : b(0) {\n"
"}",
@@ -6342,6 +6360,8 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
verifyFormat("class __declspec(X) Z {\n} n;");
verifyFormat("class A##B##C {\n} n;");
verifyFormat("class alignas(16) Z {\n} n;");
+ verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
+ verifyFormat("class MACROA MACRO(X) Z {\n} n;");
// Redefinition from nested context:
verifyFormat("class A::B::C {\n} n;");
@@ -6356,6 +6376,7 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
// FIXME: This is still incorrectly handled at the formatter side.
verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
+ verifyFormat("int i = SomeFunction(a<b, a> b);");
// FIXME:
// This now gets parsed incorrectly as class definition.
@@ -6483,7 +6504,7 @@ TEST_F(FormatTest, BlockComments) {
EXPECT_EQ("/*\n"
"*\n"
" * aaaaaa\n"
- "*aaaaaa\n"
+ " * aaaaaa\n"
"*/",
format("/*\n"
"*\n"
@@ -6652,10 +6673,10 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
- EXPECT_EQ(
- "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
- format(
- "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
+ EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
+ "forAllCells:(BOOL)flag;",
+ format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
+ "forAllCells:(BOOL)flag;"));
// Very long objectiveC method declaration.
verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
@@ -6673,11 +6694,26 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
" outRange8:(NSRange)out_range8\n"
" outRange9:(NSRange)out_range9;");
+ // When the function name has to be wrapped.
+ FormatStyle Style = getLLVMStyle();
+ Style.IndentWrappedFunctionNames = false;
+ verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
+ "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
+ " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
+ "}",
+ Style);
+ Style.IndentWrappedFunctionNames = true;
+ verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
+ " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
+ " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
+ "}",
+ Style);
+
verifyFormat("- (int)sum:(vector<int>)numbers;");
verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
// FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
// protocol lists (but not for template classes):
- //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
+ // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
verifyFormat("- (int (*)())foo:(int (*)())f;");
verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
@@ -6957,9 +6993,9 @@ TEST_F(FormatTest, FormatObjCMethodExpr) {
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("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.
@@ -7006,6 +7042,8 @@ TEST_F(FormatTest, FormatObjCMethodExpr) {
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];");
@@ -7090,6 +7128,15 @@ TEST_F(FormatTest, FormatObjCMethodExpr) {
" 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"
@@ -7106,6 +7153,13 @@ TEST_F(FormatTest, FormatObjCMethodExpr) {
" 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];");
@@ -7218,20 +7272,19 @@ TEST_F(FormatTest, ObjCDictLiterals) {
"}");
verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
- verifyFormat("[self setDict:@{}");
- verifyFormat("[self setDict:@{@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("NSDictionary *d = @{\n"
+ " @\"nam\" : NSUserNam(),\n"
+ " @\"dte\" : [NSDate date],\n"
+ " @\"processInfo\" : [NSProcessInfo processInfo]\n"
+ "};");
verifyFormat(
"@{\n"
" NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
@@ -7249,12 +7302,11 @@ TEST_F(FormatTest, ObjCDictLiterals) {
"};");
// 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("NSDictionary *d = {\n"
+ " @\"nam\" : NSUserNam(),\n"
+ " @\"dte\" : [NSDate date],\n"
+ " @\"processInfo\" : [NSProcessInfo processInfo]\n"
+ "};");
verifyFormat("NSMutableDictionary *dictionary =\n"
" [NSMutableDictionary dictionaryWithDictionary:@{\n"
" aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
@@ -7264,7 +7316,7 @@ TEST_F(FormatTest, ObjCDictLiterals) {
}
TEST_F(FormatTest, ObjCArrayLiterals) {
- verifyFormat("@[");
+ verifyIncompleteFormat("@[");
verifyFormat("@[]");
verifyFormat(
"NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
@@ -7307,117 +7359,9 @@ TEST_F(FormatTest, ObjCArrayLiterals) {
" index:(NSUInteger)index\n"
" nonDigitAttributes:\n"
" (NSDictionary *)noDigitAttributes;");
- verifyFormat(
- "[someFunction someLooooooooooooongParameter:\n"
- " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
-}
-
-TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
- EXPECT_EQ("{\n"
- "{\n"
- "a;\n"
- "b;\n"
- "}\n"
- "}",
- format("{\n"
- "{\n"
- "a;\n"
- " b;\n"
- "}\n"
- "}",
- 13, 2, getLLVMStyle()));
- EXPECT_EQ("{\n"
- "{\n"
- " a;\n"
- "b;\n"
- "}\n"
- "}",
- format("{\n"
- "{\n"
- " a;\n"
- "b;\n"
- "}\n"
- "}",
- 9, 2, getLLVMStyle()));
- EXPECT_EQ("{\n"
- "{\n"
- "public:\n"
- " b;\n"
- "}\n"
- "}",
- format("{\n"
- "{\n"
- "public:\n"
- " b;\n"
- "}\n"
- "}",
- 17, 2, getLLVMStyle()));
- EXPECT_EQ("{\n"
- "{\n"
- "a;\n"
- "}\n"
- "{\n"
- " b; //\n"
- "}\n"
- "}",
- format("{\n"
- "{\n"
- "a;\n"
- "}\n"
- "{\n"
- " b; //\n"
- "}\n"
- "}",
- 22, 2, getLLVMStyle()));
- EXPECT_EQ(" {\n"
- " a; //\n"
- " }",
- format(" {\n"
- "a; //\n"
- " }",
- 4, 2, getLLVMStyle()));
- EXPECT_EQ("void f() {}\n"
- "void g() {}",
- format("void f() {}\n"
- "void g() {}",
- 13, 0, getLLVMStyle()));
- EXPECT_EQ("int a; // comment\n"
- " // line 2\n"
- "int b;",
- format("int a; // comment\n"
- " // line 2\n"
- " int b;",
- 35, 0, getLLVMStyle()));
- EXPECT_EQ(" int a;\n"
- " void\n"
- " ffffff() {\n"
- " }",
- format(" int a;\n"
- "void ffffff() {}",
- 11, 0, getLLVMStyleWithColumns(11)));
-
- EXPECT_EQ(" void f() {\n"
- "#define A 1\n"
- " }",
- format(" void f() {\n"
- " #define A 1\n" // Format this line.
- " }",
- 20, 0, getLLVMStyle()));
- EXPECT_EQ(" void f() {\n"
- " int i;\n"
- "#define A \\\n"
- " int i; \\\n"
- " int j;\n"
- " int k;\n"
- " }",
- format(" void f() {\n"
- " int i;\n"
- "#define A \\\n"
- " int i; \\\n"
- " int j;\n"
- " int k;\n" // Format this line.
- " }",
- 67, 0, getLLVMStyle()));
+ verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
+ " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
+ "]];");
}
TEST_F(FormatTest, BreaksStringLiterals) {
@@ -7478,11 +7422,11 @@ TEST_F(FormatTest, BreaksStringLiterals) {
"loooooooooooooooooooong);",
getLLVMStyleWithColumns(20)));
- EXPECT_EQ("f(g(\"long string \"\n"
- " \"literal\"),\n"
- " b);",
- format("f(g(\"long string literal\"), b);",
- getLLVMStyleWithColumns(20)));
+ EXPECT_EQ(
+ "f(g(\"long string \"\n"
+ " \"literal\"),\n"
+ " b);",
+ format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
EXPECT_EQ("f(g(\"long string \"\n"
" \"literal\",\n"
" a),\n"
@@ -7511,23 +7455,20 @@ TEST_F(FormatTest, BreaksStringLiterals) {
" aaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
- EXPECT_EQ(
- "\"splitmea\"\n"
- "\"trandomp\"\n"
- "\"oint\"",
- format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
+ EXPECT_EQ("\"splitmea\"\n"
+ "\"trandomp\"\n"
+ "\"oint\"",
+ format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
- EXPECT_EQ(
- "\"split/\"\n"
- "\"pathat/\"\n"
- "\"slashes\"",
- format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
+ EXPECT_EQ("\"split/\"\n"
+ "\"pathat/\"\n"
+ "\"slashes\"",
+ format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
- EXPECT_EQ(
- "\"split/\"\n"
- "\"pathat/\"\n"
- "\"slashes\"",
- format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
+ EXPECT_EQ("\"split/\"\n"
+ "\"pathat/\"\n"
+ "\"slashes\"",
+ format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
EXPECT_EQ("\"split at \"\n"
"\"spaces/at/\"\n"
"\"slashes.at.any$\"\n"
@@ -7572,12 +7513,11 @@ TEST_F(FormatTest, BreaksStringLiterals) {
FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
AlignLeft.AlignEscapedNewlinesLeft = true;
- EXPECT_EQ(
- "#define A \\\n"
- " \"some \" \\\n"
- " \"text \" \\\n"
- " \"other\";",
- format("#define A \"some text other\";", AlignLeft));
+ EXPECT_EQ("#define A \\\n"
+ " \"some \" \\\n"
+ " \"text \" \\\n"
+ " \"other\";",
+ format("#define A \"some text other\";", AlignLeft));
}
TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
@@ -7741,9 +7681,7 @@ TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
format("#define x(_a) printf(\"foo\"_a);", Style));
}
-TEST_F(FormatTest, UnderstandsCpp1y) {
- verifyFormat("int bi{1'000'000};");
-}
+TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
@@ -7794,10 +7732,8 @@ TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
}
TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
- EXPECT_EQ("\"\\a\"",
- format("\"\\a\"", getLLVMStyleWithColumns(3)));
- EXPECT_EQ("\"\\\"",
- format("\"\\\"", getLLVMStyleWithColumns(2)));
+ EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
+ EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
EXPECT_EQ("\"test\"\n"
"\"\\n\"",
format("\"test\\n\"", getLLVMStyleWithColumns(7)));
@@ -7807,8 +7743,7 @@ TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
EXPECT_EQ("\"\\\\\\\\\"\n"
"\"\\n\"",
format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
- EXPECT_EQ("\"\\uff01\"",
- format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
+ EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
EXPECT_EQ("\"\\uff01\"\n"
"\"test\"",
format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
@@ -7914,33 +7849,6 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
"\t\t parameter2); \\\n"
"\t}",
Tab);
- EXPECT_EQ("void f() {\n"
- "\tf();\n"
- "\tg();\n"
- "}",
- format("void f() {\n"
- "\tf();\n"
- "\tg();\n"
- "}",
- 0, 0, Tab));
- EXPECT_EQ("void f() {\n"
- "\tf();\n"
- "\tg();\n"
- "}",
- format("void f() {\n"
- "\tf();\n"
- "\tg();\n"
- "}",
- 16, 0, Tab));
- EXPECT_EQ("void f() {\n"
- " \tf();\n"
- "\tg();\n"
- "}",
- format("void f() {\n"
- " \tf();\n"
- " \tg();\n"
- "}",
- 21, 0, Tab));
Tab.TabWidth = 4;
Tab.IndentWidth = 8;
@@ -8098,25 +8006,25 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
Tab));
EXPECT_EQ("/* some\n"
" comment */",
- format(" \t \t /* some\n"
- " \t \t comment */",
- Tab));
+ format(" \t \t /* some\n"
+ " \t \t comment */",
+ Tab));
EXPECT_EQ("int a; /* some\n"
" comment */",
- format(" \t \t int a; /* some\n"
- " \t \t comment */",
- Tab));
+ format(" \t \t int a; /* some\n"
+ " \t \t comment */",
+ Tab));
EXPECT_EQ("int a; /* some\n"
"comment */",
- format(" \t \t int\ta; /* some\n"
- " \t \t comment */",
- Tab));
+ format(" \t \t int\ta; /* some\n"
+ " \t \t comment */",
+ Tab));
EXPECT_EQ("f(\"\t\t\"); /* some\n"
" comment */",
- format(" \t \t f(\"\t\t\"); /* some\n"
- " \t \t comment */",
- Tab));
+ format(" \t \t f(\"\t\t\"); /* some\n"
+ " \t \t comment */",
+ Tab));
EXPECT_EQ("{\n"
" /*\n"
" * Comment\n"
@@ -8169,20 +8077,25 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
verifyFormat("while(true)\n"
- " continue;", NoSpace);
+ " continue;",
+ NoSpace);
verifyFormat("for(;;)\n"
- " continue;", NoSpace);
+ " continue;",
+ NoSpace);
verifyFormat("if(true)\n"
" f();\n"
"else if(true)\n"
- " f();", NoSpace);
+ " f();",
+ NoSpace);
verifyFormat("do {\n"
" do_something();\n"
- "} while(something());", NoSpace);
+ "} while(something());",
+ NoSpace);
verifyFormat("switch(x) {\n"
"default:\n"
" break;\n"
- "}", NoSpace);
+ "}",
+ NoSpace);
verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
verifyFormat("size_t x = sizeof(x);", NoSpace);
verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
@@ -8191,6 +8104,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("size_t x = alignof(MyType);", NoSpace);
verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
verifyFormat("int f() throw(Deprecated);", NoSpace);
+ verifyFormat("typedef void (*cb)(int);", NoSpace);
FormatStyle Space = getLLVMStyle();
Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
@@ -8235,6 +8149,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("size_t x = alignof (MyType);", Space);
verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
verifyFormat("int f () throw (Deprecated);", Space);
+ verifyFormat("typedef void (*cb) (int);", Space);
}
TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
@@ -8245,20 +8160,25 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
verifyFormat("call();", Spaces);
verifyFormat("std::function<void( int, int )> callback;", Spaces);
verifyFormat("while ( (bool)1 )\n"
- " continue;", Spaces);
+ " continue;",
+ Spaces);
verifyFormat("for ( ;; )\n"
- " continue;", Spaces);
+ " continue;",
+ Spaces);
verifyFormat("if ( true )\n"
" f();\n"
"else if ( true )\n"
- " f();", Spaces);
+ " f();",
+ Spaces);
verifyFormat("do {\n"
" do_something( (int)i );\n"
- "} while ( something() );", Spaces);
+ "} while ( something() );",
+ Spaces);
verifyFormat("switch ( x ) {\n"
"default:\n"
" break;\n"
- "}", Spaces);
+ "}",
+ Spaces);
Spaces.SpacesInParentheses = false;
Spaces.SpacesInCStyleCastParentheses = true;
@@ -8271,27 +8191,31 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
verifyFormat("#define x (( int )-1)", Spaces);
// Run the first set of tests again with:
- Spaces.SpacesInParentheses = false,
- Spaces.SpaceInEmptyParentheses = true;
+ Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
Spaces.SpacesInCStyleCastParentheses = true;
verifyFormat("call(x, y, z);", Spaces);
verifyFormat("call( );", Spaces);
verifyFormat("std::function<void(int, int)> callback;", Spaces);
verifyFormat("while (( bool )1)\n"
- " continue;", Spaces);
+ " continue;",
+ Spaces);
verifyFormat("for (;;)\n"
- " continue;", Spaces);
+ " continue;",
+ Spaces);
verifyFormat("if (true)\n"
" f( );\n"
"else if (true)\n"
- " f( );", Spaces);
+ " f( );",
+ Spaces);
verifyFormat("do {\n"
" do_something(( int )i);\n"
- "} while (something( ));", Spaces);
+ "} while (something( ));",
+ Spaces);
verifyFormat("switch (x) {\n"
"default:\n"
" break;\n"
- "}", Spaces);
+ "}",
+ Spaces);
// Run the first set of tests again with:
Spaces.SpaceAfterCStyleCast = true;
@@ -8365,6 +8289,152 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
verifyFormat("a or_eq 8;", Spaces);
}
+TEST_F(FormatTest, AlignConsecutiveAssignments) {
+ FormatStyle Alignment = getLLVMStyle();
+ Alignment.AlignConsecutiveAssignments = false;
+ verifyFormat("int a = 5;\n"
+ "int oneTwoThree = 123;",
+ Alignment);
+ verifyFormat("int a = 5;\n"
+ "int oneTwoThree = 123;",
+ Alignment);
+
+ Alignment.AlignConsecutiveAssignments = true;
+ verifyFormat("int a = 5;\n"
+ "int oneTwoThree = 123;",
+ Alignment);
+ verifyFormat("int a = method();\n"
+ "int oneTwoThree = 133;",
+ Alignment);
+ verifyFormat("a &= 5;\n"
+ "bcd *= 5;\n"
+ "ghtyf += 5;\n"
+ "dvfvdb -= 5;\n"
+ "a /= 5;\n"
+ "vdsvsv %= 5;\n"
+ "sfdbddfbdfbb ^= 5;\n"
+ "dvsdsv |= 5;\n"
+ "int dsvvdvsdvvv = 123;",
+ Alignment);
+ verifyFormat("int i = 1, j = 10;\n"
+ "something = 2000;",
+ Alignment);
+ verifyFormat("something = 2000;\n"
+ "int i = 1, j = 10;\n",
+ Alignment);
+ verifyFormat("something = 2000;\n"
+ "another = 911;\n"
+ "int i = 1, j = 10;\n"
+ "oneMore = 1;\n"
+ "i = 2;",
+ Alignment);
+ verifyFormat("int a = 5;\n"
+ "int one = 1;\n"
+ "method();\n"
+ "int oneTwoThree = 123;\n"
+ "int oneTwo = 12;",
+ Alignment);
+ verifyFormat("int oneTwoThree = 123; // comment\n"
+ "int oneTwo = 12; // comment",
+ Alignment);
+ EXPECT_EQ("int a = 5;\n"
+ "\n"
+ "int oneTwoThree = 123;",
+ format("int a = 5;\n"
+ "\n"
+ "int oneTwoThree= 123;",
+ Alignment));
+ EXPECT_EQ("int a = 5;\n"
+ "int one = 1;\n"
+ "\n"
+ "int oneTwoThree = 123;",
+ format("int a = 5;\n"
+ "int one = 1;\n"
+ "\n"
+ "int oneTwoThree = 123;",
+ Alignment));
+ EXPECT_EQ("int a = 5;\n"
+ "int one = 1;\n"
+ "\n"
+ "int oneTwoThree = 123;\n"
+ "int oneTwo = 12;",
+ format("int a = 5;\n"
+ "int one = 1;\n"
+ "\n"
+ "int oneTwoThree = 123;\n"
+ "int oneTwo = 12;",
+ Alignment));
+ Alignment.AlignEscapedNewlinesLeft = true;
+ verifyFormat("#define A \\\n"
+ " int aaaa = 12; \\\n"
+ " int b = 23; \\\n"
+ " int ccc = 234; \\\n"
+ " int dddddddddd = 2345;",
+ Alignment);
+ Alignment.AlignEscapedNewlinesLeft = false;
+ verifyFormat("#define A "
+ " \\\n"
+ " int aaaa = 12; "
+ " \\\n"
+ " int b = 23; "
+ " \\\n"
+ " int ccc = 234; "
+ " \\\n"
+ " int dddddddddd = 2345;",
+ Alignment);
+ verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
+ "k = 4, int l = 5,\n"
+ " int m = 6) {\n"
+ " int j = 10;\n"
+ " otherThing = 1;\n"
+ "}",
+ Alignment);
+ verifyFormat("void SomeFunction(int parameter = 0) {\n"
+ " int i = 1;\n"
+ " int j = 2;\n"
+ " int big = 10000;\n"
+ "}",
+ Alignment);
+ verifyFormat("class C {\n"
+ "public:\n"
+ " int i = 1;\n"
+ " virtual void f() = 0;\n"
+ "};",
+ Alignment);
+ verifyFormat("int i = 1;\n"
+ "if (SomeType t = getSomething()) {\n"
+ "}\n"
+ "int j = 2;\n"
+ "int big = 10000;",
+ Alignment);
+ verifyFormat("int j = 7;\n"
+ "for (int k = 0; k < N; ++k) {\n"
+ "}\n"
+ "int j = 2;\n"
+ "int big = 10000;\n"
+ "}",
+ Alignment);
+ Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
+ verifyFormat("int i = 1;\n"
+ "LooooooooooongType loooooooooooooooooooooongVariable\n"
+ " = someLooooooooooooooooongFunction();\n"
+ "int j = 2;",
+ Alignment);
+ Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
+ verifyFormat("int i = 1;\n"
+ "LooooooooooongType loooooooooooooooooooooongVariable =\n"
+ " someLooooooooooooooooongFunction();\n"
+ "int j = 2;",
+ Alignment);
+ // FIXME: Should align all three assignments
+ verifyFormat(
+ "int i = 1;\n"
+ "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
+ " loooooooooooooooooooooongParameterB);\n"
+ "int j = 2;",
+ Alignment);
+}
+
TEST_F(FormatTest, LinuxBraceBreaking) {
FormatStyle LinuxBraceStyle = getLLVMStyle();
LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
@@ -8794,11 +8864,16 @@ TEST_F(FormatTest, UnderstandsPragmas) {
"(including parentheses)."));
}
+TEST_F(FormatTest, UnderstandPragmaOption) {
+ verifyFormat("#pragma option -C -A");
+
+ EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
+}
+
#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<FormatStyle, 3> Styles;
@@ -8904,6 +8979,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
CHECK_PARSE_BOOL(AlignOperands);
CHECK_PARSE_BOOL(AlignTrailingComments);
+ CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
@@ -9172,11 +9248,12 @@ TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
Style.BreakBeforeTernaryOperators = true;
EXPECT_EQ(0, parseConfiguration("---\n"
- "BasedOnStyle: Google\n"
- "---\n"
- "Language: JavaScript\n"
- "IndentWidth: 76\n"
- "...\n", &Style).value());
+ "BasedOnStyle: Google\n"
+ "---\n"
+ "Language: JavaScript\n"
+ "IndentWidth: 76\n"
+ "...\n",
+ &Style).value());
EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
EXPECT_EQ(76u, Style.IndentWidth);
EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
@@ -9220,8 +9297,7 @@ TEST_F(FormatTest, CountsUTF8CharactersProperly) {
getLLVMStyleWithColumns(31));
verifyFormat("// Однажды в студёную зимнюю пору...",
getLLVMStyleWithColumns(36));
- verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
- getLLVMStyleWithColumns(32));
+ verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
verifyFormat("/* Однажды в студёную зимнюю пору... */",
getLLVMStyleWithColumns(39));
verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
@@ -9239,19 +9315,18 @@ TEST_F(FormatTest, SplitsUTF8Strings) {
EXPECT_EQ("\"aaaaaaaÄ\"\n"
"\"\xc2\x8d\";",
format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
+ EXPECT_EQ("\"Однажды, в \"\n"
+ "\"студёную \"\n"
+ "\"зимнюю \"\n"
+ "\"пору,\"",
+ format("\"Однажды, в студёную зимнюю пору,\"",
+ getLLVMStyleWithColumns(13)));
EXPECT_EQ(
- "\"Однажды, в \"\n"
- "\"студёную \"\n"
- "\"зимнюю \"\n"
- "\"пору,\"",
- format("\"Однажды, в студёную зимнюю пору,\"",
- getLLVMStyleWithColumns(13)));
- EXPECT_EQ("\"一 二 三 \"\n"
- "\"四 五六 \"\n"
- "\"七 八 九 \"\n"
- "\"十\"",
- format("\"一 二 三 四 五六 七 八 九 十\"",
- getLLVMStyleWithColumns(11)));
+ "\"一 二 三 \"\n"
+ "\"四 五六 \"\n"
+ "\"七 八 九 \"\n"
+ "\"十\"",
+ format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
EXPECT_EQ("\"一\t二 \"\n"
"\"\t三 \"\n"
"\"四 五\t六 \"\n"
@@ -9261,7 +9336,6 @@ TEST_F(FormatTest, SplitsUTF8Strings) {
getLLVMStyleWithColumns(11)));
}
-
TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
EXPECT_EQ("const char *sssss =\n"
" \"一二三四五六七八\\\n"
@@ -9444,22 +9518,20 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) {
Style);
// Wrap before binary operators.
- EXPECT_EQ(
- "void f()\n"
- "{\n"
- " if (aaaaaaaaaaaaaaaa\n"
- " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
- " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
- " return;\n"
- "}",
- format(
- "void f() {\n"
- "if (aaaaaaaaaaaaaaaa\n"
- "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
- "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
- "return;\n"
- "}",
- Style));
+ EXPECT_EQ("void f()\n"
+ "{\n"
+ " if (aaaaaaaaaaaaaaaa\n"
+ " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
+ " return;\n"
+ "}",
+ format("void f() {\n"
+ "if (aaaaaaaaaaaaaaaa\n"
+ "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
+ "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
+ "return;\n"
+ "}",
+ Style));
// Allow functions on a single line.
verifyFormat("void f() { return; }", Style);
@@ -9489,7 +9561,8 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) {
" , b(b)\n"
" , c(c)\n"
"{\n"
- "}", Style);
+ "}",
+ Style);
verifyFormat("SomeClass::Constructor()\n"
" : a(a)\n"
"{\n"
@@ -9614,9 +9687,21 @@ TEST_F(FormatTest, FormatsLambdas) {
" : Field([] { // comment\n"
" int i;\n"
" }) {}");
+ verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
+ " return some_parameter.size();\n"
+ "};");
+ verifyFormat("int i = aaaaaa ? 1 //\n"
+ " : [] {\n"
+ " return 2; //\n"
+ " }();");
+ verifyFormat("llvm::errs() << \"number of twos is \"\n"
+ " << std::count_if(v.begin(), v.end(), [](int x) {\n"
+ " return x == 2; // force break\n"
+ " });");
// Lambdas with return types.
verifyFormat("int c = []() -> int { return 2; }();\n");
+ verifyFormat("int c = []() -> int * { return 2; }();\n");
verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
@@ -9782,6 +9867,74 @@ TEST_F(FormatTest, FormatsBlocks) {
FourIndent);
}
+TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
+ FormatStyle ZeroColumn = getLLVMStyle();
+ ZeroColumn.ColumnLimit = 0;
+
+ verifyFormat("[[SessionService sharedService] "
+ "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
+ " if (window) {\n"
+ " [self windowDidLoad:window];\n"
+ " } else {\n"
+ " [self errorLoadingWindow];\n"
+ " }\n"
+ "}];",
+ ZeroColumn);
+ EXPECT_EQ("[[SessionService sharedService]\n"
+ " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
+ " if (window) {\n"
+ " [self windowDidLoad:window];\n"
+ " } else {\n"
+ " [self errorLoadingWindow];\n"
+ " }\n"
+ " }];",
+ format("[[SessionService sharedService]\n"
+ "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
+ " if (window) {\n"
+ " [self windowDidLoad:window];\n"
+ " } else {\n"
+ " [self errorLoadingWindow];\n"
+ " }\n"
+ "}];",
+ ZeroColumn));
+ verifyFormat("[myObject doSomethingWith:arg1\n"
+ " firstBlock:^(Foo *a) {\n"
+ " // ...\n"
+ " int i;\n"
+ " }\n"
+ " secondBlock:^(Bar *b) {\n"
+ " // ...\n"
+ " int i;\n"
+ " }\n"
+ " thirdBlock:^Foo(Bar *b) {\n"
+ " // ...\n"
+ " int i;\n"
+ " }];",
+ ZeroColumn);
+ verifyFormat("f(^{\n"
+ " @autoreleasepool {\n"
+ " if (a) {\n"
+ " g();\n"
+ " }\n"
+ " }\n"
+ "});",
+ ZeroColumn);
+ verifyFormat("void (^largeBlock)(void) = ^{\n"
+ " // ...\n"
+ "};",
+ ZeroColumn);
+
+ ZeroColumn.AllowShortBlocksOnASingleLine = true;
+ EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
+ format("void (^largeBlock)(void) = ^{ int i; };",
+ ZeroColumn));
+ ZeroColumn.AllowShortBlocksOnASingleLine = false;
+ EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
+ " int i;\n"
+ "};",
+ format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
+}
+
TEST_F(FormatTest, SupportsCRLF) {
EXPECT_EQ("int a;\r\n"
"int b;\r\n"
@@ -9882,12 +10035,10 @@ TEST_F(FormatTest, TripleAngleBrackets) {
verifyFormat("f<<<1, 1>>>();");
verifyFormat("f<<<1, 1, 1, s>>>();");
verifyFormat("f<<<a, b, c, d>>>();");
- EXPECT_EQ("f<<<1, 1>>>();",
- format("f <<< 1, 1 >>> ();"));
+ EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
verifyFormat("f<param><<<1, 1>>>();");
verifyFormat("f<1><<<1, 1>>>();");
- EXPECT_EQ("f<param><<<1, 1>>>();",
- format("f< param > <<< 1, 1 >>> ();"));
+ EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaa<<<\n 1, 1>>>();");
}
@@ -10032,7 +10183,9 @@ TEST_F(FormatTest, DisableRegions) {
TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
format("? ) =");
+ verifyNoCrash("#define a\\\n /**/}");
}
-} // end namespace tooling
+} // end namespace
+} // end namespace format
} // end namespace clang
diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp
index 695bad5553..7c8b3fce42 100644
--- a/unittests/Format/FormatTestJS.cpp
+++ b/unittests/Format/FormatTestJS.cpp
@@ -24,7 +24,10 @@ protected:
DEBUG(llvm::errs() << "---\n");
DEBUG(llvm::errs() << Code << "\n\n");
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
- tooling::Replacements Replaces = reformat(Style, Code, Ranges);
+ bool IncompleteFormat = false;
+ tooling::Replacements Replaces =
+ reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
+ EXPECT_FALSE(IncompleteFormat);
std::string Result = applyAllReplacements(Code, Replaces);
EXPECT_NE("", Result);
DEBUG(llvm::errs() << "\n" << Result << "\n\n");
@@ -82,6 +85,10 @@ TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
verifyFormat("var b = a.map((x) => x + 1);");
verifyFormat("return ('aaa') in bbbb;");
+
+ // ES6 spread operator.
+ verifyFormat("someFunction(...a);");
+ verifyFormat("var x = [1, ...a, 2];");
}
TEST_F(FormatTestJS, UnderstandsAmpAmp) {
@@ -98,6 +105,7 @@ TEST_F(FormatTestJS, ES6DestructuringAssignment) {
}
TEST_F(FormatTestJS, ContainerLiterals) {
+ verifyFormat("var x = {y: function(a) { return a; }};");
verifyFormat("return {\n"
" link: function() {\n"
" f(); //\n"
@@ -141,7 +149,23 @@ TEST_F(FormatTestJS, ContainerLiterals) {
// Enum style top level assignment.
verifyFormat("X = {\n a: 123\n};");
verifyFormat("X.Y = {\n a: 123\n};");
+ // But only on the top level, otherwise its a plain object literal assignment.
+ verifyFormat("function x() {\n"
+ " y = {z: 1};\n"
+ "}");
verifyFormat("x = foo && {a: 123};");
+
+ // Arrow functions in object literals.
+ verifyFormat("var x = {y: (a) => { return a; }};");
+ verifyFormat("var x = {y: (a) => a};");
+
+ // Computed keys.
+ verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};");
+ verifyFormat("var x = {\n"
+ " [a]: 1,\n"
+ " b: 2,\n"
+ " [c]: 3,\n"
+ "};");
}
TEST_F(FormatTestJS, MethodsInObjectLiterals) {
@@ -228,6 +252,41 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
" function inner2(a, b) { return a; }\n"
" inner2(a, b);\n"
"}");
+ verifyFormat("function f() {}");
+}
+
+TEST_F(FormatTestJS, ArrayLiterals) {
+ verifyFormat(
+ "var aaaaa: List<SomeThing> =\n"
+ " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
+ verifyFormat("return [\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
+ " ccccccccccccccccccccccccccc\n"
+ "];");
+ verifyFormat("var someVariable = SomeFuntion([\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
+ " ccccccccccccccccccccccccccc\n"
+ "]);");
+ verifyFormat("var someVariable = SomeFuntion([\n"
+ " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n"
+ "]);",
+ getGoogleJSStyleWithColumns(51));
+ verifyFormat("var someVariable = SomeFuntion(aaaa, [\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
+ " ccccccccccccccccccccccccccc\n"
+ "]);");
+ verifyFormat("var someVariable = SomeFuntion(aaaa,\n"
+ " [\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
+ " ccccccccccccccccccccccccccc\n"
+ " ],\n"
+ " aaaa);");
+
+ verifyFormat("someFunction([], {a: a});");
}
TEST_F(FormatTestJS, FunctionLiterals) {
@@ -276,6 +335,12 @@ TEST_F(FormatTestJS, FunctionLiterals) {
" return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
" };\n"
"}");
+ verifyFormat("someLooooooooongFunction(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
+ " // code\n"
+ " });");
verifyFormat("f({a: function() { return 1; }});",
getGoogleJSStyleWithColumns(33));
@@ -307,6 +372,14 @@ TEST_F(FormatTestJS, FunctionLiterals) {
" doSomething();\n"
" doSomething();\n"
" }, this));");
+
+ // FIXME: This is bad, we should be wrapping before "function() {".
+ verifyFormat("someFunction(function() {\n"
+ " doSomething(); // break\n"
+ "})\n"
+ " .doSomethingElse(\n"
+ " // break\n"
+ " );");
}
TEST_F(FormatTestJS, InliningFunctionLiterals) {
@@ -413,13 +486,61 @@ TEST_F(FormatTestJS, MultipleFunctionLiterals) {
" .thenCatch(function(error) { body(); });");
}
+TEST_F(FormatTestJS, ArrowFunctions) {
+ verifyFormat("var x = (a) => {\n"
+ " return a;\n"
+ "};");
+ verifyFormat("var x = (a) => {\n"
+ " function y() { return 42; }\n"
+ " return a;\n"
+ "};");
+ verifyFormat("var x = (a: type): {some: type} => {\n"
+ " return a;\n"
+ "};");
+ verifyFormat("var x = (a) => a;");
+ verifyFormat("return () => [];");
+ verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ "};");
+ verifyFormat(
+ "var a = a.aaaaaaa((a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n"
+ " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
+ verifyFormat(
+ "var a = a.aaaaaaa((a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n"
+ " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n"
+ " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
+
+ // FIXME: This is bad, we should be wrapping before "() => {".
+ verifyFormat("someFunction(() => {\n"
+ " doSomething(); // break\n"
+ "})\n"
+ " .doSomethingElse(\n"
+ " // break\n"
+ " );");
+}
+
TEST_F(FormatTestJS, ReturnStatements) {
verifyFormat("function() {\n"
" return [hello, world];\n"
"}");
}
-TEST_F(FormatTestJS, ClosureStyleComments) {
+TEST_F(FormatTestJS, AutomaticSemicolonInsertion) {
+ // The following statements must not wrap, as otherwise the program meaning
+ // would change due to automatic semicolon insertion.
+ // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
+ verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
+}
+
+TEST_F(FormatTestJS, ClosureStyleCasts) {
verifyFormat("var x = /** @type {foo} */ (bar);");
}
@@ -463,6 +584,7 @@ TEST_F(FormatTestJS, RegexLiteralClassification) {
}
TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
+ verifyFormat("var regex = /=/;");
verifyFormat("var regex = /a*/;");
verifyFormat("var regex = /a+/;");
verifyFormat("var regex = /a?/;");
@@ -529,11 +651,14 @@ TEST_F(FormatTestJS, RegexLiteralExamples) {
TEST_F(FormatTestJS, TypeAnnotations) {
verifyFormat("var x: string;");
verifyFormat("function x(): string {\n return 'x';\n}");
+ verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}");
verifyFormat("function x(y: string): string {\n return 'x';\n}");
verifyFormat("for (var y: string in x) {\n x();\n}");
verifyFormat("((a: string, b: number): string => a + b);");
verifyFormat("var x: (y: number) => string;");
verifyFormat("var x: P<string, (a: number) => string>;");
+ verifyFormat("var x = {y: function(): z { return 1; }};");
+ verifyFormat("var x = {y: function(): {a: number} { return 1; }};");
}
TEST_F(FormatTestJS, ClassDeclarations) {
@@ -545,6 +670,22 @@ TEST_F(FormatTestJS, ClassDeclarations) {
verifyFormat("class C {\n static x(): string { return 'asd'; }\n}");
verifyFormat("class C extends P implements I {}");
verifyFormat("class C extends p.P implements i.I {}");
+
+ // ':' is not a type declaration here.
+ verifyFormat("class X {\n"
+ " subs = {\n"
+ " 'b': {\n"
+ " 'c': 1,\n"
+ " },\n"
+ " };\n"
+ "}");
+}
+
+TEST_F(FormatTestJS, InterfaceDeclarations) {
+ verifyFormat("interface I {\n"
+ " x: string;\n"
+ "}\n"
+ "var y;");
}
TEST_F(FormatTestJS, MetadataAnnotations) {
@@ -587,6 +728,9 @@ TEST_F(FormatTestJS, Modules) {
verifyFormat("export function fn() {\n"
" return 'fn';\n"
"}");
+ verifyFormat("export function A() {}\n"
+ "export default function B() {}\n"
+ "export function C() {}");
verifyFormat("export const x = 12;");
verifyFormat("export default class X {}");
verifyFormat("export {X, Y} from 'some/module.js';");
@@ -602,11 +746,19 @@ TEST_F(FormatTestJS, Modules) {
verifyFormat("export default class X { y: number }");
verifyFormat("export default function() {\n return 1;\n}");
verifyFormat("export var x = 12;");
+ verifyFormat("class C {}\n"
+ "export function f() {}\n"
+ "var v;");
verifyFormat("export var x: number = 12;");
verifyFormat("export const y = {\n"
" a: 1,\n"
" b: 2\n"
"};");
+ verifyFormat("export enum Foo {\n"
+ " BAR,\n"
+ " // adsdasd\n"
+ " BAZ\n"
+ "}");
}
TEST_F(FormatTestJS, TemplateStrings) {
@@ -646,6 +798,12 @@ TEST_F(FormatTestJS, TemplateStrings) {
"var x =\n `multi\n line`;",
format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1)));
+ // Make sure template strings get a proper ColumnWidth assigned, even if they
+ // are first token in line.
+ verifyFormat(
+ "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
+ " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
+
// Two template strings.
verifyFormat("var x = `hello` == `hello`;");
@@ -658,6 +816,11 @@ TEST_F(FormatTestJS, TemplateStrings) {
"var y;",
format("var x =\n `/*a`;\n"
"var y;"));
+ // Unterminated string literals in a template string.
+ verifyFormat("var x = `'`; // comment with matching quote '\n"
+ "var y;");
+ verifyFormat("var x = `\"`; // comment with matching quote \"\n"
+ "var y;");
// Backticks in a comment - not a template string.
EXPECT_EQ("var x = 1 // `/*a`;\n"
" ;",
@@ -687,14 +850,26 @@ TEST_F(FormatTestJS, TypeArguments) {
verifyFormat("foo<Y>(a);");
verifyFormat("var x: X<Y>[];");
verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
+ verifyFormat("function f(a: List<any> = null) {}");
+ verifyFormat("function f(): List<any> {}");
}
TEST_F(FormatTestJS, OptionalTypes) {
- verifyFormat("function x(a?: b, c?, d?) {\n}");
+ verifyFormat("function x(a?: b, c?, d?) {}");
verifyFormat("class X {\n"
" y?: z;\n"
" z?;\n"
"}");
+ verifyFormat("interface X {\n"
+ " y?(): z;\n"
+ "}");
+ verifyFormat("x ? 1 : 2;");
+ verifyFormat("constructor({aa}: {\n"
+ " aa?: string,\n"
+ " aaaaaaaa?: string,\n"
+ " aaaaaaaaaaaaaaa?: boolean,\n"
+ " aaaaaa?: List<string>\n"
+ "}) {}");
}
TEST_F(FormatTestJS, IndexSignature) {
diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp
index d55fe8cf09..ac8fcbdda4 100644
--- a/unittests/Format/FormatTestProto.cpp
+++ b/unittests/Format/FormatTestProto.cpp
@@ -105,6 +105,14 @@ TEST_F(FormatTestProto, MessageFieldAttributes) {
"}];");
}
+TEST_F(FormatTestProto, DoesntWrapFileOptions) {
+ EXPECT_EQ(
+ "option java_package = "
+ "\"some.really.long.package.that.exceeds.the.column.limit\";",
+ format("option java_package = "
+ "\"some.really.long.package.that.exceeds.the.column.limit\";"));
+}
+
TEST_F(FormatTestProto, FormatsOptions) {
verifyFormat("option (MyProto.options) = {\n"
" field_a: OK\n"
diff --git a/unittests/Format/FormatTestSelective.cpp b/unittests/Format/FormatTestSelective.cpp
new file mode 100644
index 0000000000..8d2cb5a2fc
--- /dev/null
+++ b/unittests/Format/FormatTestSelective.cpp
@@ -0,0 +1,441 @@
+//===- unittest/Format/FormatTestSelective.cpp - Formatting unit tests ----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "format-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class FormatTestSelective : public ::testing::Test {
+protected:
+ std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) {
+ DEBUG(llvm::errs() << "---\n");
+ DEBUG(llvm::errs() << Code << "\n\n");
+ std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
+ bool IncompleteFormat = false;
+ tooling::Replacements Replaces =
+ reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
+ EXPECT_FALSE(IncompleteFormat) << Code << "\n\n";
+ std::string Result = applyAllReplacements(Code, Replaces);
+ EXPECT_NE("", Result);
+ DEBUG(llvm::errs() << "\n" << Result << "\n\n");
+ return Result;
+ }
+
+ FormatStyle Style = getLLVMStyle();
+};
+
+TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) {
+ EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0));
+ EXPECT_EQ("int a;", format("int a; ", 0, 0));
+ EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0));
+ EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0));
+}
+
+TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
+ EXPECT_EQ("int b;\nint a;", format("int b;\n int a;", 7, 0));
+ EXPECT_EQ("int b;\n int a;", format("int b;\n int a;", 6, 0));
+
+ Style.ColumnLimit = 12;
+ EXPECT_EQ("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ format("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ 26, 0));
+ EXPECT_EQ("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ format("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ 25, 0));
+}
+
+TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) {
+ EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0));
+ EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0));
+
+ // This might not strictly be correct, but is likely good in all practical
+ // cases.
+ EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0));
+}
+
+TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
+ EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0));
+ EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0));
+}
+
+TEST_F(FormatTestSelective, ReformatsMovedLines) {
+ EXPECT_EQ(
+ "template <typename T> T *getFETokenInfo() const {\n"
+ " return static_cast<T *>(FETokenInfo);\n"
+ "}\n"
+ " int a; // <- Should not be formatted",
+ format(
+ "template<typename T>\n"
+ "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
+ " int a; // <- Should not be formatted",
+ 9, 5));
+}
+
+TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) {
+ Style.AllowShortIfStatementsOnASingleLine = true;
+ EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
+ EXPECT_EQ("if (a) return; // comment",
+ format("if(a)\nreturn; // comment", 20, 1));
+}
+
+TEST_F(FormatTestSelective, FormatsCommentsLocally) {
+ EXPECT_EQ("int a; // comment\n"
+ "int b; // comment",
+ format("int a; // comment\n"
+ "int b; // comment",
+ 0, 0));
+ EXPECT_EQ("int a; // comment\n"
+ " // line 2\n"
+ "int b;",
+ format("int a; // comment\n"
+ " // line 2\n"
+ "int b;",
+ 28, 0));
+ EXPECT_EQ("int aaaaaa; // comment\n"
+ "int b;\n"
+ "int c; // unrelated comment",
+ format("int aaaaaa; // comment\n"
+ "int b;\n"
+ "int c; // unrelated comment",
+ 31, 0));
+
+ EXPECT_EQ("int a; // This\n"
+ " // is\n"
+ " // a",
+ format("int a; // This\n"
+ " // is\n"
+ " // a",
+ 0, 0));
+ EXPECT_EQ("int a; // This\n"
+ " // is\n"
+ " // a\n"
+ "// This is b\n"
+ "int b;",
+ format("int a; // This\n"
+ " // is\n"
+ " // a\n"
+ "// This is b\n"
+ "int b;",
+ 0, 0));
+ EXPECT_EQ("int a; // This\n"
+ " // is\n"
+ " // a\n"
+ "\n"
+ " // This is unrelated",
+ format("int a; // This\n"
+ " // is\n"
+ " // a\n"
+ "\n"
+ " // This is unrelated",
+ 0, 0));
+ EXPECT_EQ("int a;\n"
+ "// This is\n"
+ "// not formatted. ",
+ format("int a;\n"
+ "// This is\n"
+ "// not formatted. ",
+ 0, 0));
+}
+
+TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
+ EXPECT_EQ("DEBUG({\n"
+ " int i;\n"
+ " int j;\n"
+ "});",
+ format("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ 20, 1));
+ EXPECT_EQ("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ format("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ 41, 1));
+ EXPECT_EQ("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ format("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ 41, 1));
+ EXPECT_EQ("DEBUG({\n"
+ " int i;\n"
+ " int j;\n"
+ "});",
+ format("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ 20, 1));
+
+ EXPECT_EQ("Debug({\n"
+ " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
+ " return;\n"
+ " },\n"
+ " a);",
+ format("Debug({\n"
+ " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
+ " return;\n"
+ " },\n"
+ " a);",
+ 50, 1));
+ EXPECT_EQ("DEBUG({\n"
+ " DEBUG({\n"
+ " int a;\n"
+ " int b;\n"
+ " }) ;\n"
+ "});",
+ format("DEBUG({\n"
+ " DEBUG({\n"
+ " int a;\n"
+ " int b;\n" // Format this line only.
+ " }) ;\n" // Don't touch this line.
+ "});",
+ 35, 0));
+ EXPECT_EQ("DEBUG({\n"
+ " int a; //\n"
+ "});",
+ format("DEBUG({\n"
+ " int a; //\n"
+ "});",
+ 0, 0));
+ EXPECT_EQ("someFunction(\n"
+ " [] {\n"
+ " // Only with this comment.\n"
+ " int i; // invoke formatting here.\n"
+ " }, // force line break\n"
+ " aaa);",
+ format("someFunction(\n"
+ " [] {\n"
+ " // Only with this comment.\n"
+ " int i; // invoke formatting here.\n"
+ " }, // force line break\n"
+ " aaa);",
+ 63, 1));
+
+ EXPECT_EQ("int longlongname; // comment\n"
+ "int x = f({\n"
+ " int x; // comment\n"
+ " int y; // comment\n"
+ "});",
+ format("int longlongname; // comment\n"
+ "int x = f({\n"
+ " int x; // comment\n"
+ " int y; // comment\n"
+ "});",
+ 65, 0));
+ EXPECT_EQ("int s = f({\n"
+ " class X {\n"
+ " public:\n"
+ " void f();\n"
+ " };\n"
+ "});",
+ format("int s = f({\n"
+ " class X {\n"
+ " public:\n"
+ " void f();\n"
+ " };\n"
+ "});",
+ 0, 0));
+}
+
+TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
+ Style.AlignEscapedNewlinesLeft = true;
+ EXPECT_EQ("int i;\n"
+ "#define A \\\n"
+ " int i; \\\n"
+ " int j\n"
+ "int k;",
+ format("int i;\n"
+ "#define A \\\n"
+ " int i ; \\\n"
+ " int j\n"
+ "int k;",
+ 8, 0)); // 8: position of "#define".
+ EXPECT_EQ("int i;\n"
+ "#define A \\\n"
+ " int i; \\\n"
+ " int j\n"
+ "int k;",
+ format("int i;\n"
+ "#define A \\\n"
+ " int i ; \\\n"
+ " int j\n"
+ "int k;",
+ 45, 0)); // 45: position of "j".
+}
+
+TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
+ EXPECT_EQ("{\n"
+ "{\n"
+ "a;\n"
+ "b;\n"
+ "}\n"
+ "}",
+ format("{\n"
+ "{\n"
+ "a;\n"
+ " b;\n"
+ "}\n"
+ "}",
+ 13, 2));
+ EXPECT_EQ("{\n"
+ "{\n"
+ " a;\n"
+ "b;\n"
+ "}\n"
+ "}",
+ format("{\n"
+ "{\n"
+ " a;\n"
+ "b;\n"
+ "}\n"
+ "}",
+ 9, 2));
+ EXPECT_EQ("{\n"
+ "{\n"
+ "public:\n"
+ " b;\n"
+ "}\n"
+ "}",
+ format("{\n"
+ "{\n"
+ "public:\n"
+ " b;\n"
+ "}\n"
+ "}",
+ 17, 2));
+ EXPECT_EQ("{\n"
+ "{\n"
+ "a;\n"
+ "}\n"
+ "{\n"
+ " b; //\n"
+ "}\n"
+ "}",
+ format("{\n"
+ "{\n"
+ "a;\n"
+ "}\n"
+ "{\n"
+ " b; //\n"
+ "}\n"
+ "}",
+ 22, 2));
+ EXPECT_EQ(" {\n"
+ " a; //\n"
+ " }",
+ format(" {\n"
+ "a; //\n"
+ " }",
+ 4, 2));
+ EXPECT_EQ("void f() {}\n"
+ "void g() {}",
+ format("void f() {}\n"
+ "void g() {}",
+ 13, 0));
+ EXPECT_EQ("int a; // comment\n"
+ " // line 2\n"
+ "int b;",
+ format("int a; // comment\n"
+ " // line 2\n"
+ " int b;",
+ 35, 0));
+
+ EXPECT_EQ(" void f() {\n"
+ "#define A 1\n"
+ " }",
+ format(" void f() {\n"
+ " #define A 1\n" // Format this line.
+ " }",
+ 20, 0));
+ EXPECT_EQ(" void f() {\n"
+ " int i;\n"
+ "#define A \\\n"
+ " int i; \\\n"
+ " int j;\n"
+ " int k;\n"
+ " }",
+ format(" void f() {\n"
+ " int i;\n"
+ "#define A \\\n"
+ " int i; \\\n"
+ " int j;\n"
+ " int k;\n" // Format this line.
+ " }",
+ 67, 0));
+
+ Style.ColumnLimit = 11;
+ EXPECT_EQ(" int a;\n"
+ " void\n"
+ " ffffff() {\n"
+ " }",
+ format(" int a;\n"
+ "void ffffff() {}",
+ 11, 0));
+}
+
+TEST_F(FormatTestSelective, UnderstandsTabs) {
+ Style.IndentWidth = 8;
+ Style.UseTab = FormatStyle::UT_Always;
+ Style.AlignEscapedNewlinesLeft = true;
+ EXPECT_EQ("void f() {\n"
+ "\tf();\n"
+ "\tg();\n"
+ "}",
+ format("void f() {\n"
+ "\tf();\n"
+ "\tg();\n"
+ "}",
+ 0, 0));
+ EXPECT_EQ("void f() {\n"
+ "\tf();\n"
+ "\tg();\n"
+ "}",
+ format("void f() {\n"
+ "\tf();\n"
+ "\tg();\n"
+ "}",
+ 16, 0));
+ EXPECT_EQ("void f() {\n"
+ " \tf();\n"
+ "\tg();\n"
+ "}",
+ format("void f() {\n"
+ " \tf();\n"
+ " \tg();\n"
+ "}",
+ 21, 0));
+}
+
+} // end namespace
+} // end namespace format
+} // end namespace clang
diff --git a/unittests/Lex/LexerTest.cpp b/unittests/Lex/LexerTest.cpp
index 85987bf001..b5a39b303d 100644
--- a/unittests/Lex/LexerTest.cpp
+++ b/unittests/Lex/LexerTest.cpp
@@ -37,8 +37,7 @@ class VoidModuleLoader : public ModuleLoader {
void makeModuleVisible(Module *Mod,
Module::NameVisibilityKind Visibility,
- SourceLocation ImportLoc,
- bool Complain) override { }
+ SourceLocation ImportLoc) override { }
GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
{ return nullptr; }
diff --git a/unittests/Lex/PPCallbacksTest.cpp b/unittests/Lex/PPCallbacksTest.cpp
index da641a7afb..94812fc93d 100644
--- a/unittests/Lex/PPCallbacksTest.cpp
+++ b/unittests/Lex/PPCallbacksTest.cpp
@@ -42,8 +42,7 @@ class VoidModuleLoader : public ModuleLoader {
void makeModuleVisible(Module *Mod,
Module::NameVisibilityKind Visibility,
- SourceLocation ImportLoc,
- bool Complain) override { }
+ SourceLocation ImportLoc) override { }
GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
{ return nullptr; }
diff --git a/unittests/Lex/PPConditionalDirectiveRecordTest.cpp b/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
index 946cb88b98..d2e3640501 100644
--- a/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
+++ b/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
@@ -61,8 +61,7 @@ class VoidModuleLoader : public ModuleLoader {
void makeModuleVisible(Module *Mod,
Module::NameVisibilityKind Visibility,
- SourceLocation ImportLoc,
- bool Complain) override { }
+ SourceLocation ImportLoc) override { }
GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
{ return nullptr; }
diff --git a/unittests/Tooling/RefactoringTest.cpp b/unittests/Tooling/RefactoringTest.cpp
index 7e643fa66d..6c2c16b484 100644
--- a/unittests/Tooling/RefactoringTest.cpp
+++ b/unittests/Tooling/RefactoringTest.cpp
@@ -281,6 +281,7 @@ public:
protected:
clang::SourceManager *SM;
+ clang::ASTContext *Context;
private:
class FindConsumer : public clang::ASTConsumer {
@@ -303,6 +304,7 @@ private:
CreateASTConsumer(clang::CompilerInstance &compiler,
llvm::StringRef dummy) override {
Visitor->SM = &compiler.getSourceManager();
+ Visitor->Context = &compiler.getASTContext();
/// TestConsumer will be deleted by the framework calling us.
return llvm::make_unique<FindConsumer>(Visitor);
}
@@ -368,6 +370,29 @@ TEST(Replacement, TemplatedFunctionCall) {
expectReplacementAt(CallToF.Replace, "input.cc", 43, 8);
}
+class NestedNameSpecifierAVisitor
+ : public TestVisitor<NestedNameSpecifierAVisitor> {
+public:
+ bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLoc) {
+ if (NNSLoc.getNestedNameSpecifier()) {
+ if (const NamespaceDecl* NS = NNSLoc.getNestedNameSpecifier()->getAsNamespace()) {
+ if (NS->getName() == "a") {
+ Replace = Replacement(*SM, &NNSLoc, "", Context->getLangOpts());
+ }
+ }
+ }
+ return TestVisitor<NestedNameSpecifierAVisitor>::TraverseNestedNameSpecifierLoc(
+ NNSLoc);
+ }
+ Replacement Replace;
+};
+
+TEST(Replacement, ColonColon) {
+ NestedNameSpecifierAVisitor VisitNNSA;
+ EXPECT_TRUE(VisitNNSA.runOver("namespace a { void f() { ::a::f(); } }"));
+ expectReplacementAt(VisitNNSA.Replace, "input.cc", 25, 5);
+}
+
TEST(Range, overlaps) {
EXPECT_TRUE(Range(10, 10).overlapsWith(Range(0, 11)));
EXPECT_TRUE(Range(0, 11).overlapsWith(Range(10, 10)));