2012-12-04 02:12:45 +08:00
|
|
|
|
//===- unittest/Format/FormatTest.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.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2013-01-16 22:55:28 +08:00
|
|
|
|
#define DEBUG_TYPE "format-test"
|
|
|
|
|
|
2012-12-04 17:45:34 +08:00
|
|
|
|
#include "clang/Format/Format.h"
|
2012-12-04 02:12:45 +08:00
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2013-01-16 22:55:28 +08:00
|
|
|
|
#include "llvm/Support/Debug.h"
|
2013-01-19 16:09:44 +08:00
|
|
|
|
#include "gtest/gtest.h"
|
2013-01-16 22:55:28 +08:00
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
|
namespace clang {
|
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
|
|
class FormatTest : public ::testing::Test {
|
|
|
|
|
protected:
|
|
|
|
|
std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
|
|
|
|
|
const FormatStyle &Style) {
|
2013-01-16 22:55:28 +08:00
|
|
|
|
DEBUG(llvm::errs() << "---\n");
|
2013-10-12 05:25:45 +08:00
|
|
|
|
DEBUG(llvm::errs() << Code << "\n\n");
|
2013-05-16 18:40:07 +08:00
|
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
|
|
|
|
|
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
|
|
|
|
|
ReplacementCount = Replaces.size();
|
|
|
|
|
std::string Result = applyAllReplacements(Code, Replaces);
|
|
|
|
|
EXPECT_NE("", Result);
|
|
|
|
|
DEBUG(llvm::errs() << "\n" << Result << "\n\n");
|
|
|
|
|
return Result;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 19:59:17 +08:00
|
|
|
|
std::string
|
|
|
|
|
format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
|
2012-12-04 02:12:45 +08:00
|
|
|
|
return format(Code, 0, Code.size(), Style);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-19 03:56:56 +08:00
|
|
|
|
std::string messUp(llvm::StringRef Code) {
|
|
|
|
|
std::string MessedUp(Code.str());
|
|
|
|
|
bool InComment = false;
|
2013-01-09 23:25:02 +08:00
|
|
|
|
bool InPreprocessorDirective = false;
|
2012-12-19 03:56:56 +08:00
|
|
|
|
bool JustReplacedNewline = false;
|
|
|
|
|
for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
|
|
|
|
|
if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
|
|
|
|
|
if (JustReplacedNewline)
|
|
|
|
|
MessedUp[i - 1] = '\n';
|
|
|
|
|
InComment = true;
|
2013-01-14 23:40:57 +08:00
|
|
|
|
} else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
|
2013-02-18 19:59:17 +08:00
|
|
|
|
if (i != 0)
|
|
|
|
|
MessedUp[i - 1] = '\n';
|
2013-01-09 23:25:02 +08:00
|
|
|
|
InPreprocessorDirective = true;
|
2013-01-05 07:34:14 +08:00
|
|
|
|
} else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
|
|
|
|
|
MessedUp[i] = ' ';
|
2013-01-09 23:25:02 +08:00
|
|
|
|
MessedUp[i + 1] = ' ';
|
2012-12-19 03:56:56 +08:00
|
|
|
|
} else if (MessedUp[i] == '\n') {
|
|
|
|
|
if (InComment) {
|
|
|
|
|
InComment = false;
|
2013-01-09 23:25:02 +08:00
|
|
|
|
} else if (InPreprocessorDirective) {
|
|
|
|
|
InPreprocessorDirective = false;
|
2012-12-19 03:56:56 +08:00
|
|
|
|
} else {
|
|
|
|
|
JustReplacedNewline = true;
|
|
|
|
|
MessedUp[i] = ' ';
|
|
|
|
|
}
|
2013-01-05 07:34:14 +08:00
|
|
|
|
} else if (MessedUp[i] != ' ') {
|
|
|
|
|
JustReplacedNewline = false;
|
2012-12-19 03:56:56 +08:00
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
2013-10-12 05:25:45 +08:00
|
|
|
|
std::string WithoutWhitespace;
|
|
|
|
|
if (MessedUp[0] != ' ')
|
|
|
|
|
WithoutWhitespace.push_back(MessedUp[0]);
|
|
|
|
|
for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) {
|
|
|
|
|
if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ')
|
|
|
|
|
WithoutWhitespace.push_back(MessedUp[i]);
|
|
|
|
|
}
|
|
|
|
|
return WithoutWhitespace;
|
2012-12-19 03:56:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 02:33:23 +08:00
|
|
|
|
FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
|
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
|
|
|
|
Style.ColumnLimit = ColumnLimit;
|
|
|
|
|
return Style;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 00:24:39 +08:00
|
|
|
|
FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
|
|
|
|
|
FormatStyle Style = getGoogleStyle();
|
|
|
|
|
Style.ColumnLimit = ColumnLimit;
|
|
|
|
|
return Style;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 02:33:23 +08:00
|
|
|
|
void verifyFormat(llvm::StringRef Code,
|
|
|
|
|
const FormatStyle &Style = getLLVMStyle()) {
|
|
|
|
|
EXPECT_EQ(Code.str(), format(messUp(Code), Style));
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void verifyGoogleFormat(llvm::StringRef Code) {
|
2013-01-03 02:33:23 +08:00
|
|
|
|
verifyFormat(Code, getGoogleStyle());
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
2013-01-23 20:10:53 +08:00
|
|
|
|
|
|
|
|
|
void verifyIndependentOfContext(llvm::StringRef text) {
|
|
|
|
|
verifyFormat(text);
|
|
|
|
|
verifyFormat(llvm::Twine("void f() { " + text + " }").str());
|
|
|
|
|
}
|
2013-01-30 17:46:12 +08:00
|
|
|
|
|
|
|
|
|
int ReplacementCount;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
2013-01-09 23:25:02 +08:00
|
|
|
|
TEST_F(FormatTest, MessUp) {
|
|
|
|
|
EXPECT_EQ("1 2 3", messUp("1 2 3"));
|
|
|
|
|
EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
|
|
|
|
|
EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
|
|
|
|
|
EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
|
2013-10-12 05:25:45 +08:00
|
|
|
|
EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
|
2013-01-09 23:25:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Basic function tests.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
|
TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
|
2012-12-04 02:12:45 +08:00
|
|
|
|
EXPECT_EQ(";", format(";"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
|
|
|
|
|
EXPECT_EQ("int i;", format(" int i;"));
|
2013-09-11 20:25:57 +08:00
|
|
|
|
EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
|
2012-12-04 02:12:45 +08:00
|
|
|
|
EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
|
|
|
|
|
EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
|
|
|
|
|
EXPECT_EQ("int i;", format("int\ni;"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, FormatsNestedBlockStatements) {
|
2013-01-10 21:24:24 +08:00
|
|
|
|
EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, FormatsNestedCall) {
|
|
|
|
|
verifyFormat("Method(f1, f2(f3));");
|
|
|
|
|
verifyFormat("Method(f1(f2, f3()));");
|
2013-01-13 16:19:51 +08:00
|
|
|
|
verifyFormat("Method(f1(f2, (f3())));");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 05:08:36 +08:00
|
|
|
|
TEST_F(FormatTest, NestedNameSpecifiers) {
|
2013-01-16 15:19:28 +08:00
|
|
|
|
verifyFormat("vector< ::Type> v;");
|
2013-02-08 05:08:36 +08:00
|
|
|
|
verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
|
2013-08-28 15:07:07 +08:00
|
|
|
|
verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
|
2013-01-16 15:19:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-30 17:46:12 +08:00
|
|
|
|
TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
|
|
|
|
|
EXPECT_EQ("if (a) {\n"
|
|
|
|
|
" f();\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("if(a){f();}"));
|
2013-01-30 17:46:12 +08:00
|
|
|
|
EXPECT_EQ(4, ReplacementCount);
|
|
|
|
|
EXPECT_EQ("if (a) {\n"
|
|
|
|
|
" f();\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("if (a) {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}"));
|
2013-01-30 17:46:12 +08:00
|
|
|
|
EXPECT_EQ(0, ReplacementCount);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-14 17:58:41 +08:00
|
|
|
|
TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
|
|
|
|
|
EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
|
2013-03-02 02:11:39 +08:00
|
|
|
|
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()));
|
2013-02-14 17:58:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-04 21:43:19 +08:00
|
|
|
|
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)));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-08 04:50:00 +08:00
|
|
|
|
TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
|
|
|
|
|
EXPECT_EQ("int a;\n\n int b;",
|
|
|
|
|
format("int a;\n \n\n int b;", 7, 0, getLLVMStyle()));
|
|
|
|
|
EXPECT_EQ("int a;\n\n int b;",
|
|
|
|
|
format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-04 00:16:41 +08:00
|
|
|
|
TEST_F(FormatTest, RemovesEmptyLines) {
|
|
|
|
|
EXPECT_EQ("class C {\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("class C {\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"};"));
|
|
|
|
|
|
|
|
|
|
// Don't remove empty lines in more complex control statements.
|
|
|
|
|
EXPECT_EQ("void f() {\n"
|
|
|
|
|
" if (a) {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" } else if (b) {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("void f() {\n"
|
|
|
|
|
" if (a) {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" } else if (b) {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"}"));
|
|
|
|
|
|
|
|
|
|
// FIXME: This is slightly inconsistent.
|
|
|
|
|
EXPECT_EQ("namespace {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("namespace {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"}"));
|
|
|
|
|
EXPECT_EQ("namespace {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"} // namespace",
|
|
|
|
|
format("namespace {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"} // namespace"));
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 21:08:03 +08:00
|
|
|
|
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()));
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Tests for control statements.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
2013-05-14 18:31:09 +08:00
|
|
|
|
TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
|
2013-01-15 00:24:39 +08:00
|
|
|
|
verifyFormat("if (true)\n f();\ng();");
|
|
|
|
|
verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
|
2013-01-16 23:44:34 +08:00
|
|
|
|
|
2013-05-16 20:12:21 +08:00
|
|
|
|
FormatStyle AllowsMergedIf = getLLVMStyle();
|
2013-01-16 23:44:34 +08:00
|
|
|
|
AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
|
|
|
|
|
verifyFormat("if (a)\n"
|
|
|
|
|
" // comment\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" f();",
|
|
|
|
|
AllowsMergedIf);
|
2013-05-16 20:12:21 +08:00
|
|
|
|
verifyFormat("if (a)\n"
|
|
|
|
|
" ;",
|
|
|
|
|
AllowsMergedIf);
|
|
|
|
|
verifyFormat("if (a)\n"
|
|
|
|
|
" if (b) return;",
|
|
|
|
|
AllowsMergedIf);
|
2013-01-16 23:44:34 +08:00
|
|
|
|
|
2013-05-16 20:12:21 +08:00
|
|
|
|
verifyFormat("if (a) // Can't merge this\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" f();\n",
|
|
|
|
|
AllowsMergedIf);
|
2013-01-16 23:44:34 +08:00
|
|
|
|
verifyFormat("if (a) /* still don't merge */\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" f();",
|
|
|
|
|
AllowsMergedIf);
|
2013-05-16 20:12:21 +08:00
|
|
|
|
verifyFormat("if (a) { // Never merge this\n"
|
2013-01-16 23:44:34 +08:00
|
|
|
|
" f();\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
AllowsMergedIf);
|
2013-01-16 23:44:34 +08:00
|
|
|
|
verifyFormat("if (a) { /* Never merge this */\n"
|
|
|
|
|
" f();\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
AllowsMergedIf);
|
2013-01-16 23:44:34 +08:00
|
|
|
|
|
2013-05-14 17:30:02 +08:00
|
|
|
|
EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
|
2013-05-16 20:12:21 +08:00
|
|
|
|
EXPECT_EQ("if (a) return; // comment",
|
2013-05-14 18:31:09 +08:00
|
|
|
|
format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
|
2013-05-14 17:30:02 +08:00
|
|
|
|
|
2013-01-16 23:44:34 +08:00
|
|
|
|
AllowsMergedIf.ColumnLimit = 14;
|
|
|
|
|
verifyFormat("if (a) return;", AllowsMergedIf);
|
2013-01-15 00:02:06 +08:00
|
|
|
|
verifyFormat("if (aaaaaaaaa)\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" return;",
|
|
|
|
|
AllowsMergedIf);
|
2013-01-16 23:44:34 +08:00
|
|
|
|
|
|
|
|
|
AllowsMergedIf.ColumnLimit = 13;
|
|
|
|
|
verifyFormat("if (a)\n return;", AllowsMergedIf);
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 20:12:21 +08:00
|
|
|
|
TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
|
|
|
|
|
FormatStyle AllowsMergedLoops = getLLVMStyle();
|
|
|
|
|
AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
|
|
|
|
|
verifyFormat("while (true) continue;", AllowsMergedLoops);
|
|
|
|
|
verifyFormat("for (;;) continue;", AllowsMergedLoops);
|
|
|
|
|
verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
|
|
|
|
|
verifyFormat("while (true)\n"
|
|
|
|
|
" ;",
|
|
|
|
|
AllowsMergedLoops);
|
|
|
|
|
verifyFormat("for (;;)\n"
|
|
|
|
|
" ;",
|
|
|
|
|
AllowsMergedLoops);
|
|
|
|
|
verifyFormat("for (;;)\n"
|
|
|
|
|
" for (;;) continue;",
|
|
|
|
|
AllowsMergedLoops);
|
|
|
|
|
verifyFormat("for (;;) // Can't merge this\n"
|
|
|
|
|
" continue;",
|
|
|
|
|
AllowsMergedLoops);
|
|
|
|
|
verifyFormat("for (;;) /* still don't merge */\n"
|
|
|
|
|
" continue;",
|
|
|
|
|
AllowsMergedLoops);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
TEST_F(FormatTest, ParseIfElse) {
|
2012-12-04 02:12:45 +08:00
|
|
|
|
verifyFormat("if (true)\n"
|
|
|
|
|
" if (true)\n"
|
|
|
|
|
" if (true)\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" else\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
" else\n"
|
|
|
|
|
" h();\n"
|
|
|
|
|
"else\n"
|
|
|
|
|
" i();");
|
|
|
|
|
verifyFormat("if (true)\n"
|
|
|
|
|
" if (true)\n"
|
|
|
|
|
" if (true) {\n"
|
2013-01-15 00:24:39 +08:00
|
|
|
|
" if (true)\n"
|
|
|
|
|
" f();\n"
|
2012-12-04 02:12:45 +08:00
|
|
|
|
" } else {\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" else\n"
|
|
|
|
|
" h();\n"
|
|
|
|
|
"else {\n"
|
|
|
|
|
" i();\n"
|
|
|
|
|
"}");
|
2013-09-17 16:28:05 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" if (a) {\n"
|
|
|
|
|
" } else {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, ElseIf) {
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("if (a) {\n} else if (b) {\n}");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
verifyFormat("if (a)\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"else if (b)\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
"else\n"
|
|
|
|
|
" h();");
|
2013-10-30 22:04:10 +08:00
|
|
|
|
verifyFormat("if (a) {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"// or else ..\n"
|
|
|
|
|
"else {\n"
|
|
|
|
|
" g()\n"
|
|
|
|
|
"}");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsForLoop) {
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
|
|
|
|
|
" ++VeryVeryLongLoopVariable)\n"
|
|
|
|
|
" ;");
|
|
|
|
|
verifyFormat("for (;;)\n"
|
|
|
|
|
" f();");
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("for (;;) {\n}");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
verifyFormat("for (;;) {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}");
|
2013-05-03 22:50:50 +08:00
|
|
|
|
verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
|
2012-12-21 22:37:20 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
|
|
|
|
|
" E = UnwrappedLines.end();\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" I != E; ++I) {\n}");
|
2012-12-21 22:37:20 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" ++IIIII) {\n}");
|
2013-04-03 21:36:17 +08:00
|
|
|
|
verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
|
Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.
Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.
Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
.begin(), E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc), d, bar(e, f));
After:
for (llvm::ArrayRef<NamedDecl *>::iterator
I = FD->getDeclsInPrototypeScope().begin(),
E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc),
d, bar(e, f));
llvm-svn: 175741
2013-02-21 23:00:29 +08:00
|
|
|
|
verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
|
|
|
|
|
" I = FD->getDeclsInPrototypeScope().begin(),\n"
|
|
|
|
|
" E = FD->getDeclsInPrototypeScope().end();\n"
|
|
|
|
|
" I != E; ++I) {\n}");
|
2013-01-29 01:30:17 +08:00
|
|
|
|
|
|
|
|
|
// FIXME: Not sure whether we want extra identation in line 3 here:
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
|
2013-03-20 20:37:50 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
|
2013-01-29 01:30:17 +08:00
|
|
|
|
" ++aaaaaaaaaaa) {\n}");
|
2013-02-27 17:47:53 +08:00
|
|
|
|
verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
|
|
|
|
|
" aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
|
|
|
|
|
"}");
|
2013-04-03 21:36:17 +08:00
|
|
|
|
verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
|
|
|
|
|
" aaaaaaaaaa);\n"
|
|
|
|
|
" iter; ++iter) {\n"
|
|
|
|
|
"}");
|
2013-02-04 15:30:30 +08:00
|
|
|
|
|
2013-02-27 17:47:53 +08:00
|
|
|
|
FormatStyle NoBinPacking = getLLVMStyle();
|
|
|
|
|
NoBinPacking.BinPackParameters = false;
|
|
|
|
|
verifyFormat("for (int aaaaaaaaaaa = 1;\n"
|
|
|
|
|
" aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaa);\n"
|
|
|
|
|
" aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
|
|
|
|
|
"}",
|
|
|
|
|
NoBinPacking);
|
|
|
|
|
verifyFormat(
|
2013-02-24 05:01:55 +08:00
|
|
|
|
"for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
|
|
|
|
|
" E = UnwrappedLines.end();\n"
|
|
|
|
|
" I != E;\n"
|
2013-02-27 17:47:53 +08:00
|
|
|
|
" ++I) {\n}",
|
|
|
|
|
NoBinPacking);
|
2012-12-07 02:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-28 21:21:16 +08:00
|
|
|
|
TEST_F(FormatTest, RangeBasedForLoops) {
|
|
|
|
|
verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
|
|
|
|
|
verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
|
2013-02-26 21:18:08 +08:00
|
|
|
|
verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
|
2013-01-28 21:21:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsWhileLoop) {
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("while (true) {\n}");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
verifyFormat("while (true)\n"
|
|
|
|
|
" f();");
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("while () {\n}");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
verifyFormat("while () {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, FormatsDoWhile) {
|
|
|
|
|
verifyFormat("do {\n"
|
|
|
|
|
" do_something();\n"
|
|
|
|
|
"} while (something());");
|
|
|
|
|
verifyFormat("do\n"
|
|
|
|
|
" do_something();\n"
|
|
|
|
|
"while (something());");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsSwitchStatement) {
|
2012-12-04 02:12:45 +08:00
|
|
|
|
verifyFormat("switch (x) {\n"
|
|
|
|
|
"case 1:\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"case kFoo:\n"
|
|
|
|
|
"case ns::kBar:\n"
|
|
|
|
|
"case kBaz:\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"default:\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("switch (x) {\n"
|
|
|
|
|
"case 1: {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"}");
|
2013-01-18 13:50:57 +08:00
|
|
|
|
verifyFormat("switch (x) {\n"
|
|
|
|
|
"case 1: {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
" h();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("switch (x) {\n"
|
|
|
|
|
"case 1: {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" if (foo) {\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
" h();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("switch (x) {\n"
|
|
|
|
|
"case 1: {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
"} break;\n"
|
|
|
|
|
"}");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
verifyFormat("switch (test)\n"
|
|
|
|
|
" ;");
|
2013-03-12 20:26:55 +08:00
|
|
|
|
verifyFormat("switch (x) {\n"
|
|
|
|
|
"default: {\n"
|
|
|
|
|
" // Do nothing.\n"
|
2013-04-12 22:13:36 +08:00
|
|
|
|
"}\n"
|
2013-03-12 20:26:55 +08:00
|
|
|
|
"}");
|
2013-03-13 23:53:12 +08:00
|
|
|
|
verifyFormat("switch (x) {\n"
|
2013-04-10 01:46:55 +08:00
|
|
|
|
"// comment\n"
|
2013-03-13 23:53:12 +08:00
|
|
|
|
"// if 1, do f()\n"
|
|
|
|
|
"case 1:\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}");
|
2013-03-20 02:33:58 +08:00
|
|
|
|
verifyFormat("switch (x) {\n"
|
|
|
|
|
"case 1:\n"
|
|
|
|
|
" // Do amazing stuff\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}");
|
2013-03-20 18:23:53 +08:00
|
|
|
|
verifyFormat("#define A \\\n"
|
|
|
|
|
" switch (x) { \\\n"
|
|
|
|
|
" case a: \\\n"
|
|
|
|
|
" foo = b; \\\n"
|
|
|
|
|
" }", getLLVMStyleWithColumns(20));
|
2013-09-02 16:26:29 +08:00
|
|
|
|
verifyFormat("#define OPERATION_CASE(name) \\\n"
|
|
|
|
|
" case OP_name: \\\n"
|
|
|
|
|
" return operations::Operation##name\n",
|
|
|
|
|
getLLVMStyleWithColumns(40));
|
2013-02-18 19:59:17 +08:00
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
verifyGoogleFormat("switch (x) {\n"
|
|
|
|
|
" case 1:\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" case kFoo:\n"
|
|
|
|
|
" case ns::kBar:\n"
|
|
|
|
|
" case kBaz:\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" default:\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyGoogleFormat("switch (x) {\n"
|
|
|
|
|
" case 1: {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyGoogleFormat("switch (test)\n"
|
2013-07-25 19:31:57 +08:00
|
|
|
|
" ;");
|
|
|
|
|
|
|
|
|
|
verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
|
|
|
|
|
" case OP_name: \\\n"
|
|
|
|
|
" return operations::Operation##name\n");
|
|
|
|
|
verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
|
|
|
|
|
" // Get the correction operation class.\n"
|
|
|
|
|
" switch (OpCode) {\n"
|
|
|
|
|
" CASE(Add);\n"
|
|
|
|
|
" CASE(Subtract);\n"
|
|
|
|
|
" default:\n"
|
|
|
|
|
" return operations::Unknown;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"#undef OPERATION_CASE\n"
|
|
|
|
|
"}");
|
2013-09-06 15:54:20 +08:00
|
|
|
|
verifyFormat("DEBUG({\n"
|
|
|
|
|
" switch (x) {\n"
|
|
|
|
|
" case A:\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" // On B:\n"
|
|
|
|
|
" case B:\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"});");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 00:56:16 +08:00
|
|
|
|
TEST_F(FormatTest, CaseRanges) {
|
|
|
|
|
verifyFormat("switch (x) {\n"
|
|
|
|
|
"case 'A' ... 'Z':\n"
|
|
|
|
|
"case 1 ... 5:\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}");
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsLabels) {
|
2012-12-04 02:12:45 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" some_code();\n"
|
|
|
|
|
"test_label:\n"
|
|
|
|
|
" some_other_code();\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" some_more_code();\n"
|
|
|
|
|
" another_label:\n"
|
|
|
|
|
" some_more_code();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("some_code();\n"
|
|
|
|
|
"test_label:\n"
|
|
|
|
|
"some_other_code();");
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Tests for comments.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, UnderstandsSingleLineComments) {
|
2013-03-22 18:01:29 +08:00
|
|
|
|
verifyFormat("//* */");
|
2012-12-19 03:56:56 +08:00
|
|
|
|
verifyFormat("// line 1\n"
|
|
|
|
|
"// line 2\n"
|
2013-01-10 21:24:24 +08:00
|
|
|
|
"void f() {}\n");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
|
2012-12-19 03:56:56 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" // Doesn't do anything\n"
|
|
|
|
|
"}");
|
2013-07-11 21:48:16 +08:00
|
|
|
|
verifyFormat("SomeObject\n"
|
|
|
|
|
" // Calling someFunction on SomeObject\n"
|
|
|
|
|
" .someFunction();");
|
2013-01-18 16:44:07 +08:00
|
|
|
|
verifyFormat("void f(int i, // some comment (probably for i)\n"
|
|
|
|
|
" int j, // some comment (probably for j)\n"
|
2013-01-14 00:10:20 +08:00
|
|
|
|
" int k); // some comment (probably for k)");
|
|
|
|
|
verifyFormat("void f(int i,\n"
|
|
|
|
|
" // some comment (probably for j)\n"
|
|
|
|
|
" int j,\n"
|
|
|
|
|
" // some comment (probably for k)\n"
|
|
|
|
|
" int k);");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
|
2013-01-18 16:44:07 +08:00
|
|
|
|
verifyFormat("int i // This is a fancy variable\n"
|
|
|
|
|
" = 5; // with nicely aligned comment.");
|
|
|
|
|
|
|
|
|
|
verifyFormat("// Leading comment.\n"
|
|
|
|
|
"int a; // Trailing comment.");
|
|
|
|
|
verifyFormat("int a; // Trailing comment\n"
|
|
|
|
|
" // on 2\n"
|
|
|
|
|
" // or 3 lines.\n"
|
|
|
|
|
"int b;");
|
|
|
|
|
verifyFormat("int a; // Trailing comment\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"// Leading comment.\n"
|
|
|
|
|
"int b;");
|
|
|
|
|
verifyFormat("int a; // Comment.\n"
|
|
|
|
|
" // More details.\n"
|
|
|
|
|
"int bbbb; // Another comment.");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
|
|
|
|
|
"int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
|
|
|
|
|
"int cccccccccccccccccccccccccccccc; // comment\n"
|
|
|
|
|
"int ddd; // looooooooooooooooooooooooong comment\n"
|
|
|
|
|
"int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
|
|
|
|
|
"int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
|
|
|
|
|
"int ccccccccccccccccccc; // comment");
|
|
|
|
|
|
2013-01-18 17:19:33 +08:00
|
|
|
|
verifyFormat("#include \"a\" // comment\n"
|
|
|
|
|
"#include \"a/b/c\" // comment");
|
|
|
|
|
verifyFormat("#include <a> // comment\n"
|
|
|
|
|
"#include <a/b/c> // comment");
|
2013-10-30 21:54:53 +08:00
|
|
|
|
EXPECT_EQ("#include \"a\" // comment\n"
|
2013-05-23 18:56:37 +08:00
|
|
|
|
"#include \"a/b/c\" // comment",
|
|
|
|
|
format("#include \\\n"
|
|
|
|
|
" \"a\" // comment\n"
|
|
|
|
|
"#include \"a/b/c\" // comment"));
|
2012-12-07 02:03:27 +08:00
|
|
|
|
|
2012-12-19 03:56:56 +08:00
|
|
|
|
verifyFormat("enum E {\n"
|
|
|
|
|
" // comment\n"
|
2013-01-07 19:09:06 +08:00
|
|
|
|
" VAL_A, // comment\n"
|
2012-12-19 03:56:56 +08:00
|
|
|
|
" VAL_B\n"
|
|
|
|
|
"};");
|
2012-12-17 19:29:41 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
2013-01-07 19:09:06 +08:00
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
|
2013-01-09 17:33:39 +08:00
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
|
|
|
|
" // Comment inside a statement.\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
|
2013-02-06 23:23:09 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"bool aaaaaaaaaaaaa = // comment\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
|
2013-01-07 16:54:53 +08:00
|
|
|
|
|
2013-02-14 03:25:54 +08:00
|
|
|
|
verifyFormat("int aaaa; // aaaaa\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"int aa; // aaaaaaa",
|
|
|
|
|
getLLVMStyleWithColumns(20));
|
2013-02-14 03:25:54 +08:00
|
|
|
|
|
2013-01-22 06:49:20 +08:00
|
|
|
|
EXPECT_EQ("void f() { // This does something ..\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"int a; // This is unrelated",
|
|
|
|
|
format("void f() { // This does something ..\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"int a; // This is unrelated"));
|
2013-07-01 19:22:57 +08:00
|
|
|
|
EXPECT_EQ("class C {\n"
|
|
|
|
|
" void f() { // This does something ..\n"
|
|
|
|
|
" } // awesome..\n"
|
2013-01-22 06:49:20 +08:00
|
|
|
|
"\n"
|
2013-07-01 19:22:57 +08:00
|
|
|
|
" int a; // This is unrelated\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("class C{void f() { // This does something ..\n"
|
2013-01-22 06:49:20 +08:00
|
|
|
|
" } // awesome..\n"
|
|
|
|
|
" \n"
|
2013-07-01 19:22:57 +08:00
|
|
|
|
"int a; // This is unrelated\n"
|
|
|
|
|
"};"));
|
2013-01-22 06:49:20 +08:00
|
|
|
|
|
2013-01-07 19:09:06 +08:00
|
|
|
|
EXPECT_EQ("int i; // single line trailing comment",
|
2013-01-07 16:54:53 +08:00
|
|
|
|
format("int i;\\\n// single line trailing comment"));
|
2013-01-07 19:09:06 +08:00
|
|
|
|
|
|
|
|
|
verifyGoogleFormat("int a; // Trailing comment.");
|
2013-01-30 03:41:55 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("someFunction(anotherFunction( // Force break.\n"
|
|
|
|
|
" parameter));");
|
2013-02-05 17:34:14 +08:00
|
|
|
|
|
|
|
|
|
verifyGoogleFormat("#endif // HEADER_GUARD");
|
2013-02-07 06:04:05 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("const char *test[] = {\n"
|
|
|
|
|
" // A\n"
|
|
|
|
|
" \"aaaa\",\n"
|
|
|
|
|
" // B\n"
|
|
|
|
|
" \"aaaaa\",\n"
|
|
|
|
|
"};");
|
2013-02-11 20:36:37 +08:00
|
|
|
|
verifyGoogleFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
2013-06-08 00:02:52 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
|
2013-04-03 20:38:53 +08:00
|
|
|
|
EXPECT_EQ("D(a, {\n"
|
|
|
|
|
" // test\n"
|
|
|
|
|
" int a;\n"
|
|
|
|
|
"});",
|
|
|
|
|
format("D(a, {\n"
|
|
|
|
|
"// test\n"
|
|
|
|
|
"int a;\n"
|
|
|
|
|
"});"));
|
2013-05-23 19:42:52 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("lineWith(); // comment\n"
|
|
|
|
|
"// at start\n"
|
|
|
|
|
"otherLine();",
|
|
|
|
|
format("lineWith(); // comment\n"
|
|
|
|
|
"// at start\n"
|
|
|
|
|
"otherLine();"));
|
|
|
|
|
EXPECT_EQ("lineWith(); // comment\n"
|
|
|
|
|
" // at start\n"
|
|
|
|
|
"otherLine();",
|
|
|
|
|
format("lineWith(); // comment\n"
|
|
|
|
|
" // at start\n"
|
|
|
|
|
"otherLine();"));
|
2013-05-24 03:54:43 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("lineWith(); // comment\n"
|
|
|
|
|
"// at start\n"
|
|
|
|
|
"otherLine(); // comment",
|
|
|
|
|
format("lineWith(); // comment\n"
|
|
|
|
|
"// at start\n"
|
|
|
|
|
"otherLine(); // comment"));
|
2013-05-24 04:46:07 +08:00
|
|
|
|
EXPECT_EQ("lineWith();\n"
|
|
|
|
|
"// at start\n"
|
|
|
|
|
"otherLine(); // comment",
|
|
|
|
|
format("lineWith();\n"
|
|
|
|
|
" // at start\n"
|
|
|
|
|
"otherLine(); // comment"));
|
|
|
|
|
EXPECT_EQ("// first\n"
|
|
|
|
|
"// at start\n"
|
|
|
|
|
"otherLine(); // comment",
|
|
|
|
|
format("// first\n"
|
|
|
|
|
" // at start\n"
|
|
|
|
|
"otherLine(); // comment"));
|
|
|
|
|
EXPECT_EQ("f();\n"
|
|
|
|
|
"// first\n"
|
|
|
|
|
"// at start\n"
|
|
|
|
|
"otherLine(); // comment",
|
|
|
|
|
format("f();\n"
|
|
|
|
|
"// first\n"
|
|
|
|
|
" // at start\n"
|
|
|
|
|
"otherLine(); // comment"));
|
2012-12-17 19:29:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-23 00:25:51 +08:00
|
|
|
|
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()));
|
2013-04-24 14:33:59 +08:00
|
|
|
|
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()));
|
2013-03-23 00:25:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-02 00:45:59 +08:00
|
|
|
|
TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
|
|
|
|
|
EXPECT_EQ("// comment", format("// comment "));
|
|
|
|
|
EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
|
|
|
|
|
format("int aaaaaaa, bbbbbbb; // comment ",
|
|
|
|
|
getLLVMStyleWithColumns(33)));
|
2013-09-07 01:24:54 +08:00
|
|
|
|
EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
|
|
|
|
|
EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
|
2013-03-02 00:45:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 03:50:11 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsBlockComments) {
|
2013-06-26 08:15:19 +08:00
|
|
|
|
verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
|
2013-01-17 20:53:34 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbb);",
|
2013-04-18 01:34:05 +08:00
|
|
|
|
format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n/* Trailing comment for aa... */\n"
|
2013-01-17 20:53:34 +08:00
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbb);"));
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
|
|
|
|
|
format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
|
|
|
|
|
"/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
|
2013-06-07 00:08:57 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaa,\n"
|
2013-07-08 22:25:23 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
|
2013-06-07 00:08:57 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaa ,\n"
|
2013-07-08 22:25:23 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
|
2013-06-07 00:08:57 +08:00
|
|
|
|
"}"));
|
2013-01-30 03:41:55 +08:00
|
|
|
|
|
2013-02-27 17:47:53 +08:00
|
|
|
|
FormatStyle NoBinPacking = getLLVMStyle();
|
|
|
|
|
NoBinPacking.BinPackParameters = false;
|
|
|
|
|
verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
|
|
|
|
|
" /* parameter 2 */ aaaaaa,\n"
|
|
|
|
|
" /* parameter 3 */ aaaaaa,\n"
|
|
|
|
|
" /* parameter 4 */ aaaaaa);",
|
|
|
|
|
NoBinPacking);
|
2013-01-17 20:53:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 03:50:11 +08:00
|
|
|
|
TEST_F(FormatTest, AlignsBlockComments) {
|
2013-03-15 00:10:54 +08:00
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" * Really multi-line\n"
|
|
|
|
|
" * comment.\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
"void f() {}",
|
|
|
|
|
format(" /*\n"
|
|
|
|
|
" * Really multi-line\n"
|
|
|
|
|
" * comment.\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
" void f() {}"));
|
|
|
|
|
EXPECT_EQ("class C {\n"
|
|
|
|
|
" /*\n"
|
|
|
|
|
" * Another multi-line\n"
|
|
|
|
|
" * comment.\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
" void f() {}\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("class C {\n"
|
|
|
|
|
"/*\n"
|
|
|
|
|
" * Another multi-line\n"
|
|
|
|
|
" * comment.\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
"void f() {}\n"
|
|
|
|
|
"};"));
|
2013-03-15 21:42:02 +08:00
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" 1. This is a comment with non-trivial formatting.\n"
|
|
|
|
|
" 1.1. We have to indent/outdent all lines equally\n"
|
|
|
|
|
" 1.1.1. to keep the formatting.\n"
|
|
|
|
|
" */",
|
|
|
|
|
format(" /*\n"
|
|
|
|
|
" 1. This is a comment with non-trivial formatting.\n"
|
|
|
|
|
" 1.1. We have to indent/outdent all lines equally\n"
|
|
|
|
|
" 1.1.1. to keep the formatting.\n"
|
|
|
|
|
" */"));
|
|
|
|
|
EXPECT_EQ("/*\n"
|
2013-08-01 07:16:02 +08:00
|
|
|
|
"Don't try to outdent if there's not enough indentation.\n"
|
2013-05-27 23:23:34 +08:00
|
|
|
|
"*/",
|
2013-03-15 21:42:02 +08:00
|
|
|
|
format(" /*\n"
|
2013-08-01 07:16:02 +08:00
|
|
|
|
" Don't try to outdent if there's not enough indentation.\n"
|
2013-03-15 21:42:02 +08:00
|
|
|
|
" */"));
|
2013-05-28 16:55:01 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("int i; /* Comment with empty...\n"
|
|
|
|
|
" *\n"
|
|
|
|
|
" * line. */",
|
|
|
|
|
format("int i; /* Comment with empty...\n"
|
|
|
|
|
" *\n"
|
|
|
|
|
" * line. */"));
|
2013-03-15 00:10:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 03:50:11 +08:00
|
|
|
|
TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
|
|
|
|
|
EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
|
|
|
|
|
format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
|
2013-07-01 21:42:42 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"void ffffffffffff(\n"
|
|
|
|
|
" int aaaaaaaa, int bbbbbbbb,\n"
|
|
|
|
|
" int cccccccccccc) { /*\n"
|
|
|
|
|
" aaaaaaaaaa\n"
|
|
|
|
|
" aaaaaaaaaaaaa\n"
|
|
|
|
|
" bbbbbbbbbbbbbb\n"
|
|
|
|
|
" bbbbbbbbbb\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
|
|
|
|
|
"{ /*\n"
|
|
|
|
|
" aaaaaaaaaa aaaaaaaaaaaaa\n"
|
|
|
|
|
" bbbbbbbbbbbbbb bbbbbbbbbb\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
"}",
|
|
|
|
|
getLLVMStyleWithColumns(40)));
|
2013-06-20 03:50:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-17 07:47:22 +08:00
|
|
|
|
TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
|
|
|
|
|
EXPECT_EQ("void\n"
|
|
|
|
|
"ffffffffff(int aaaaa /* test */);",
|
|
|
|
|
format("void ffffffffff(int aaaaa /* test */);",
|
|
|
|
|
getLLVMStyleWithColumns(35)));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-27 19:52:18 +08:00
|
|
|
|
TEST_F(FormatTest, SplitsLongCxxComments) {
|
|
|
|
|
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)));
|
2013-04-18 01:34:05 +08:00
|
|
|
|
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)));
|
2013-03-27 19:52:18 +08:00
|
|
|
|
EXPECT_EQ("if (true) // A comment that\n"
|
|
|
|
|
" // doesn't fit on\n"
|
|
|
|
|
" // one line",
|
|
|
|
|
format("if (true) // A comment that doesn't fit on one line ",
|
|
|
|
|
getLLVMStyleWithColumns(30)));
|
|
|
|
|
EXPECT_EQ("// Don't_touch_leading_whitespace",
|
|
|
|
|
format("// Don't_touch_leading_whitespace",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
2013-06-12 00:01:49 +08:00
|
|
|
|
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",
|
|
|
|
|
format("//Even if it makes the line exceed the column limit",
|
|
|
|
|
getLLVMStyleWithColumns(51)));
|
|
|
|
|
EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
|
2013-03-29 02:40:55 +08:00
|
|
|
|
EXPECT_EQ("// A comment before\n"
|
|
|
|
|
"// a macro\n"
|
|
|
|
|
"// definition\n"
|
|
|
|
|
"#define a b",
|
|
|
|
|
format("// A comment before a macro definition\n"
|
|
|
|
|
"#define a b",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
2013-06-17 20:59:44 +08:00
|
|
|
|
EXPECT_EQ("void ffffff(int aaaaaaaaa, // wwww\n"
|
|
|
|
|
" int a, int bbb, // xxxxxxx\n"
|
|
|
|
|
" // yyyyyyyyy\n"
|
|
|
|
|
" int c, int d, int e) {}",
|
|
|
|
|
format("void ffffff(\n"
|
|
|
|
|
" int aaaaaaaaa, // wwww\n"
|
|
|
|
|
" int a,\n"
|
|
|
|
|
" int bbb, // xxxxxxx yyyyyyyyy\n"
|
|
|
|
|
" int c, int d, int e) {}",
|
|
|
|
|
getLLVMStyleWithColumns(40)));
|
2013-06-20 21:58:37 +08:00
|
|
|
|
EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
|
format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
2013-06-08 00:02:52 +08:00
|
|
|
|
}
|
2013-05-27 23:23:34 +08:00
|
|
|
|
|
2013-07-17 05:06:13 +08:00
|
|
|
|
TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
|
|
|
|
|
EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
|
|
|
|
|
"// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
|
|
|
|
|
"// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
|
format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
|
|
|
|
|
"// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
|
|
|
|
|
"// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
|
|
|
|
|
EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
|
|
|
|
|
" // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
|
|
|
|
|
" // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
|
|
|
|
|
format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
|
|
|
|
|
" // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
|
|
|
|
|
" // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
|
|
|
|
|
getLLVMStyleWithColumns(50)));
|
|
|
|
|
// FIXME: One day we might want to implement adjustment of leading whitespace
|
|
|
|
|
// of the consecutive lines in this kind of comment:
|
|
|
|
|
EXPECT_EQ("int\n"
|
|
|
|
|
"a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
|
|
|
|
|
" // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
|
|
|
|
|
" // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
|
|
|
|
|
format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
|
|
|
|
|
" // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
|
|
|
|
|
" // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
|
|
|
|
|
getLLVMStyleWithColumns(49)));
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-08 00:02:52 +08:00
|
|
|
|
TEST_F(FormatTest, PriorityOfCommentBreaking) {
|
2013-08-28 18:03:58 +08:00
|
|
|
|
EXPECT_EQ("if (xxx ==\n"
|
|
|
|
|
" yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
|
2013-06-08 00:02:52 +08:00
|
|
|
|
" zzz)\n"
|
|
|
|
|
" q();",
|
|
|
|
|
format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
|
|
|
|
|
" zzz) q();",
|
|
|
|
|
getLLVMStyleWithColumns(40)));
|
|
|
|
|
EXPECT_EQ("if (xxxxxxxxxx ==\n"
|
|
|
|
|
" yyy && // aaaaaa bbbbbbbb cccc\n"
|
|
|
|
|
" zzz)\n"
|
|
|
|
|
" q();",
|
|
|
|
|
format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
|
|
|
|
|
" zzz) q();",
|
|
|
|
|
getLLVMStyleWithColumns(40)));
|
|
|
|
|
EXPECT_EQ("if (xxxxxxxxxx &&\n"
|
|
|
|
|
" yyy || // aaaaaa bbbbbbbb cccc\n"
|
|
|
|
|
" zzz)\n"
|
|
|
|
|
" q();",
|
|
|
|
|
format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
|
|
|
|
|
" zzz) q();",
|
|
|
|
|
getLLVMStyleWithColumns(40)));
|
|
|
|
|
EXPECT_EQ("fffffffff(&xxx, // aaaaaaaaaaaa\n"
|
|
|
|
|
" // bbbbbbbbbbb\n"
|
|
|
|
|
" zzz);",
|
|
|
|
|
format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
|
|
|
|
|
" zzz);",
|
|
|
|
|
getLLVMStyleWithColumns(40)));
|
2013-05-27 23:23:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, MultiLineCommentsInDefines) {
|
2013-06-20 03:50:11 +08:00
|
|
|
|
EXPECT_EQ("#define A(x) /* \\\n"
|
|
|
|
|
" a comment \\\n"
|
|
|
|
|
" inside */ \\\n"
|
2013-05-27 23:23:34 +08:00
|
|
|
|
" f();",
|
|
|
|
|
format("#define A(x) /* \\\n"
|
|
|
|
|
" a comment \\\n"
|
|
|
|
|
" inside */ \\\n"
|
|
|
|
|
" f();",
|
|
|
|
|
getLLVMStyleWithColumns(17)));
|
2013-06-20 03:50:11 +08:00
|
|
|
|
EXPECT_EQ("#define A( \\\n"
|
|
|
|
|
" x) /* \\\n"
|
|
|
|
|
" a comment \\\n"
|
|
|
|
|
" inside */ \\\n"
|
2013-05-27 23:23:34 +08:00
|
|
|
|
" f();",
|
|
|
|
|
format("#define A( \\\n"
|
|
|
|
|
" x) /* \\\n"
|
|
|
|
|
" a comment \\\n"
|
|
|
|
|
" inside */ \\\n"
|
|
|
|
|
" f();",
|
|
|
|
|
getLLVMStyleWithColumns(17)));
|
2013-03-27 19:52:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-02 21:04:06 +08:00
|
|
|
|
TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
EXPECT_EQ("namespace {}\n// Test\n#define A",
|
2013-04-02 21:04:06 +08:00
|
|
|
|
format("namespace {}\n // Test\n#define A"));
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
EXPECT_EQ("namespace {}\n/* Test */\n#define A",
|
2013-04-02 21:04:06 +08:00
|
|
|
|
format("namespace {}\n /* Test */\n#define A"));
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
EXPECT_EQ("namespace {}\n/* Test */ #define A",
|
2013-04-02 21:04:06 +08:00
|
|
|
|
format("namespace {}\n /* Test */ #define A"));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-20 01:41:36 +08:00
|
|
|
|
TEST_F(FormatTest, SplitsLongLinesInComments) {
|
|
|
|
|
EXPECT_EQ("/* This is a long\n"
|
2013-03-21 20:28:10 +08:00
|
|
|
|
" * comment that\n"
|
|
|
|
|
" * doesn't\n"
|
|
|
|
|
" * fit on one line.\n"
|
|
|
|
|
" */",
|
2013-03-20 01:41:36 +08:00
|
|
|
|
format("/* "
|
|
|
|
|
"This is a long "
|
2013-03-21 20:28:10 +08:00
|
|
|
|
"comment that "
|
|
|
|
|
"doesn't "
|
2013-03-20 01:41:36 +08:00
|
|
|
|
"fit on one line. */",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
2013-04-18 01:34:05 +08:00
|
|
|
|
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)));
|
2013-03-20 01:41:36 +08:00
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
"This is a long\n"
|
|
|
|
|
"comment that doesn't\n"
|
|
|
|
|
"fit on one line.\n"
|
|
|
|
|
"*/",
|
|
|
|
|
format("/*\n"
|
|
|
|
|
"This is a long "
|
|
|
|
|
"comment that doesn't "
|
|
|
|
|
"fit on one line. \n"
|
|
|
|
|
"*/", getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" * This is a long\n"
|
|
|
|
|
" * comment that\n"
|
|
|
|
|
" * doesn't fit on\n"
|
|
|
|
|
" * one line.\n"
|
|
|
|
|
" */",
|
2013-03-21 20:28:10 +08:00
|
|
|
|
format("/* \n"
|
2013-03-20 01:41:36 +08:00
|
|
|
|
" * This is a long "
|
|
|
|
|
" comment that "
|
|
|
|
|
" doesn't fit on "
|
|
|
|
|
" one line. \n"
|
|
|
|
|
" */", getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
|
|
|
|
|
" * so_it_should_be_broken\n"
|
|
|
|
|
" * wherever_a_space_occurs\n"
|
|
|
|
|
" */",
|
|
|
|
|
format("/*\n"
|
|
|
|
|
" * This_is_a_comment_with_words_that_dont_fit_on_one_line "
|
|
|
|
|
" so_it_should_be_broken "
|
|
|
|
|
" wherever_a_space_occurs \n"
|
|
|
|
|
" */",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("/*\n"
|
2013-03-27 19:52:18 +08:00
|
|
|
|
" * This_comment_can_not_be_broken_into_lines\n"
|
2013-03-20 01:41:36 +08:00
|
|
|
|
" */",
|
|
|
|
|
format("/*\n"
|
2013-03-27 19:52:18 +08:00
|
|
|
|
" * This_comment_can_not_be_broken_into_lines\n"
|
2013-03-20 01:41:36 +08:00
|
|
|
|
" */",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
" /*\n"
|
|
|
|
|
" This is another\n"
|
|
|
|
|
" long comment that\n"
|
|
|
|
|
" doesn't fit on one\n"
|
|
|
|
|
" line 1234567890\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"/*\n"
|
|
|
|
|
"This is another "
|
|
|
|
|
" long comment that "
|
|
|
|
|
" doesn't fit on one"
|
|
|
|
|
" line 1234567890\n"
|
|
|
|
|
"*/\n"
|
|
|
|
|
"}", getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
" /*\n"
|
|
|
|
|
" * This i s\n"
|
|
|
|
|
" * another comment\n"
|
|
|
|
|
" * t hat doesn' t\n"
|
|
|
|
|
" * fit on one l i\n"
|
|
|
|
|
" * n e\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"/*\n"
|
|
|
|
|
" * This i s"
|
|
|
|
|
" another comment"
|
|
|
|
|
" t hat doesn' t"
|
|
|
|
|
" fit on one l i"
|
|
|
|
|
" n e\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
"}", getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" * This is a long\n"
|
|
|
|
|
" * comment that\n"
|
|
|
|
|
" * doesn't fit on\n"
|
|
|
|
|
" * one line\n"
|
|
|
|
|
" */",
|
|
|
|
|
format(" /*\n"
|
|
|
|
|
" * This is a long comment that doesn't fit on one line\n"
|
|
|
|
|
" */", getLLVMStyleWithColumns(20)));
|
2013-03-21 20:28:10 +08:00
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
" if (something) /* This is a\n"
|
2013-05-27 23:23:34 +08:00
|
|
|
|
" long\n"
|
|
|
|
|
" comment */\n"
|
2013-03-21 20:28:10 +08:00
|
|
|
|
" ;\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
" if (something) /* This is a long comment */\n"
|
|
|
|
|
" ;\n"
|
|
|
|
|
"}",
|
|
|
|
|
getLLVMStyleWithColumns(30)));
|
2013-06-08 00:02:52 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("/* A comment before\n"
|
|
|
|
|
" * a macro\n"
|
|
|
|
|
" * definition */\n"
|
|
|
|
|
"#define a b",
|
|
|
|
|
format("/* A comment before a macro definition */\n"
|
|
|
|
|
"#define a b",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("/* some comment\n"
|
|
|
|
|
" * a comment\n"
|
|
|
|
|
"* that we break\n"
|
|
|
|
|
" * another comment\n"
|
|
|
|
|
"* we have to break\n"
|
|
|
|
|
"* a left comment\n"
|
|
|
|
|
" */",
|
|
|
|
|
format(" /* some comment\n"
|
|
|
|
|
" * a comment that we break\n"
|
|
|
|
|
" * another comment we have to break\n"
|
|
|
|
|
"* a left comment\n"
|
|
|
|
|
" */",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" */\n",
|
|
|
|
|
format(" /* \n"
|
|
|
|
|
" \n"
|
|
|
|
|
" \n"
|
|
|
|
|
" */\n"));
|
2013-03-20 01:41:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
|
|
|
|
|
EXPECT_EQ("#define X \\\n"
|
2013-03-21 20:28:10 +08:00
|
|
|
|
" /* \\\n"
|
|
|
|
|
" Test \\\n"
|
2013-03-20 01:41:36 +08:00
|
|
|
|
" Macro comment \\\n"
|
|
|
|
|
" with a long \\\n"
|
|
|
|
|
" line \\\n"
|
2013-04-15 22:28:00 +08:00
|
|
|
|
" */ \\\n"
|
2013-03-20 01:41:36 +08:00
|
|
|
|
" A + B",
|
|
|
|
|
format("#define X \\\n"
|
|
|
|
|
" /*\n"
|
2013-03-21 20:28:10 +08:00
|
|
|
|
" Test\n"
|
2013-03-20 01:41:36 +08:00
|
|
|
|
" Macro comment with a long line\n"
|
|
|
|
|
" */ \\\n"
|
|
|
|
|
" A + B",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("#define X \\\n"
|
|
|
|
|
" /* Macro comment \\\n"
|
2013-03-21 20:28:10 +08:00
|
|
|
|
" with a long \\\n"
|
2013-04-15 22:28:00 +08:00
|
|
|
|
" line */ \\\n"
|
2013-03-21 20:28:10 +08:00
|
|
|
|
" A + B",
|
|
|
|
|
format("#define X \\\n"
|
|
|
|
|
" /* Macro comment with a long\n"
|
|
|
|
|
" line */ \\\n"
|
|
|
|
|
" A + B",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("#define X \\\n"
|
|
|
|
|
" /* Macro comment \\\n"
|
|
|
|
|
" * with a long \\\n"
|
2013-04-15 22:28:00 +08:00
|
|
|
|
" * line */ \\\n"
|
2013-03-20 01:41:36 +08:00
|
|
|
|
" A + B",
|
|
|
|
|
format("#define X \\\n"
|
|
|
|
|
" /* Macro comment with a long line */ \\\n"
|
|
|
|
|
" A + B",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-17 20:53:34 +08:00
|
|
|
|
TEST_F(FormatTest, CommentsInStaticInitializers) {
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
|
|
|
|
|
" /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa, // comment\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa };",
|
|
|
|
|
format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
|
|
|
|
|
" /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa , // comment\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa };"));
|
|
|
|
|
verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
|
|
|
|
|
" bbbbbbbbbbb, ccccccccccc };");
|
|
|
|
|
verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
|
|
|
|
|
" // comment for bb....\n"
|
|
|
|
|
" bbbbbbbbbbb, ccccccccccc };");
|
|
|
|
|
verifyGoogleFormat(
|
2013-05-23 18:15:45 +08:00
|
|
|
|
"static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
|
|
|
|
|
" bbbbbbbbbbb, ccccccccccc};");
|
|
|
|
|
verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
|
|
|
|
|
" // comment for bb....\n"
|
|
|
|
|
" bbbbbbbbbbb, ccccccccccc};");
|
2013-01-31 22:59:26 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("S s = { { a, b, c }, // Group #1\n"
|
|
|
|
|
" { d, e, f }, // Group #2\n"
|
|
|
|
|
" { g, h, i } }; // Group #3");
|
|
|
|
|
verifyFormat("S s = { { // Group #1\n"
|
|
|
|
|
" a, b, c },\n"
|
|
|
|
|
" { // Group #2\n"
|
|
|
|
|
" d, e, f },\n"
|
|
|
|
|
" { // Group #3\n"
|
|
|
|
|
" g, h, i } };");
|
2013-02-20 20:56:39 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("S s = {\n"
|
|
|
|
|
" // Some comment\n"
|
2013-02-26 21:10:34 +08:00
|
|
|
|
" a,\n"
|
2013-02-20 20:56:39 +08:00
|
|
|
|
"\n"
|
|
|
|
|
" // Comment after empty line\n"
|
|
|
|
|
" b\n"
|
2013-02-26 21:10:34 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("S s = {\n"
|
|
|
|
|
" // Some comment\n"
|
|
|
|
|
" a,\n"
|
|
|
|
|
" \n"
|
|
|
|
|
" // Comment after empty line\n"
|
|
|
|
|
" b\n"
|
|
|
|
|
"}"));
|
2013-06-13 03:04:12 +08:00
|
|
|
|
EXPECT_EQ("S s = {\n"
|
|
|
|
|
" /* Some comment */\n"
|
|
|
|
|
" a,\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" /* Comment after empty line */\n"
|
|
|
|
|
" b\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("S s = {\n"
|
|
|
|
|
" /* Some comment */\n"
|
|
|
|
|
" a,\n"
|
|
|
|
|
" \n"
|
|
|
|
|
" /* Comment after empty line */\n"
|
|
|
|
|
" b\n"
|
|
|
|
|
"}"));
|
Reduce penalty for splitting after "{" in static initializers.
This fixes llvm.org/PR15379.
Before:
const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = { 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, // comment
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, // comment
0x00, 0x00, 0x00, 0x00 // comment
};
After:
const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment
0x00, 0x00, 0x00, 0x00 // comment
};
llvm-svn: 176262
2013-02-28 23:04:12 +08:00
|
|
|
|
verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
|
|
|
|
|
" 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
|
|
|
|
|
" 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
|
|
|
|
|
" 0x00, 0x00, 0x00, 0x00 // comment\n"
|
|
|
|
|
"};");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 02:24:24 +08:00
|
|
|
|
TEST_F(FormatTest, IgnoresIf0Contents) {
|
|
|
|
|
EXPECT_EQ("#if 0\n"
|
|
|
|
|
"}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"void f() {}",
|
|
|
|
|
format("#if 0\n"
|
|
|
|
|
"}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"void f( ) { }"));
|
|
|
|
|
EXPECT_EQ("#if false\n"
|
|
|
|
|
"void f( ) { }\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"void g() {}\n",
|
|
|
|
|
format("#if false\n"
|
|
|
|
|
"void f( ) { }\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"void g( ) { }\n"));
|
|
|
|
|
EXPECT_EQ("enum E {\n"
|
|
|
|
|
" One,\n"
|
|
|
|
|
" Two,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
" Four,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" Five\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("enum E {\n"
|
|
|
|
|
" One,Two,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
" Four,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" Five};"));
|
|
|
|
|
EXPECT_EQ("enum F {\n"
|
|
|
|
|
" One,\n"
|
|
|
|
|
"#if 1\n"
|
|
|
|
|
" Two,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
" Four,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" Five\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("enum F {\n"
|
|
|
|
|
"One,\n"
|
|
|
|
|
"#if 1\n"
|
|
|
|
|
"Two,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
" Four,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"Five\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"};"));
|
|
|
|
|
EXPECT_EQ("enum G {\n"
|
|
|
|
|
" One,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"Two,\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
" Three,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" Four\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("enum G {\n"
|
|
|
|
|
"One,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"Two,\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"Four\n"
|
|
|
|
|
"};"));
|
|
|
|
|
EXPECT_EQ("enum H {\n"
|
|
|
|
|
" One,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"#ifdef Q\n"
|
|
|
|
|
"Two,\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" Four\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("enum H {\n"
|
|
|
|
|
"One,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"#ifdef Q\n"
|
|
|
|
|
"Two,\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"Four\n"
|
|
|
|
|
"};"));
|
|
|
|
|
EXPECT_EQ("enum I {\n"
|
|
|
|
|
" One,\n"
|
|
|
|
|
"#if /* test */ 0 || 1\n"
|
|
|
|
|
"Two,\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" Four\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("enum I {\n"
|
|
|
|
|
"One,\n"
|
|
|
|
|
"#if /* test */ 0 || 1\n"
|
|
|
|
|
"Two,\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"Four\n"
|
|
|
|
|
"};"));
|
|
|
|
|
EXPECT_EQ("enum J {\n"
|
|
|
|
|
" One,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"Two,\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"Four,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" Five\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("enum J {\n"
|
|
|
|
|
"One,\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"#if 0\n"
|
|
|
|
|
"Two,\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
"Three,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"Four,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"Five\n"
|
|
|
|
|
"};"));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Tests for classes, namespaces, etc.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
verifyFormat("class A {};");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
|
|
|
|
|
verifyFormat("class A {\n"
|
|
|
|
|
"public:\n"
|
2013-04-10 17:49:49 +08:00
|
|
|
|
"public: // comment\n"
|
2012-12-07 02:03:27 +08:00
|
|
|
|
"protected:\n"
|
|
|
|
|
"private:\n"
|
2013-01-10 21:24:24 +08:00
|
|
|
|
" void f() {}\n"
|
2012-12-04 02:12:45 +08:00
|
|
|
|
"};");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
verifyGoogleFormat("class A {\n"
|
|
|
|
|
" public:\n"
|
|
|
|
|
" protected:\n"
|
|
|
|
|
" private:\n"
|
2013-01-10 21:24:24 +08:00
|
|
|
|
" void f() {}\n"
|
2012-12-07 02:03:27 +08:00
|
|
|
|
"};");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 01:08:02 +08:00
|
|
|
|
TEST_F(FormatTest, SeparatesLogicalBlocks) {
|
|
|
|
|
EXPECT_EQ("class A {\n"
|
|
|
|
|
"public:\n"
|
|
|
|
|
" void f();\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"private:\n"
|
|
|
|
|
" void g() {}\n"
|
|
|
|
|
" // test\n"
|
|
|
|
|
"protected:\n"
|
|
|
|
|
" int h;\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("class A {\n"
|
|
|
|
|
"public:\n"
|
|
|
|
|
"void f();\n"
|
|
|
|
|
"private:\n"
|
|
|
|
|
"void g() {}\n"
|
|
|
|
|
"// test\n"
|
|
|
|
|
"protected:\n"
|
|
|
|
|
"int h;\n"
|
|
|
|
|
"};"));
|
2013-10-06 19:40:08 +08:00
|
|
|
|
EXPECT_EQ("class A {\n"
|
|
|
|
|
"protected:\n"
|
|
|
|
|
"public:\n"
|
|
|
|
|
" void f();\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("class A {\n"
|
|
|
|
|
"protected:\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"public:\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" void f();\n"
|
|
|
|
|
"};"));
|
2013-03-28 01:08:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-06 01:22:09 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsClasses) {
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
verifyFormat("class A : public B {};");
|
|
|
|
|
verifyFormat("class A : public ::B {};");
|
2013-02-14 16:42:54 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
" public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
|
2013-05-06 14:45:09 +08:00
|
|
|
|
verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
|
|
|
|
|
" : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
" public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
|
2013-02-14 16:42:54 +08:00
|
|
|
|
verifyFormat(
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
"class A : public B, public C, public D, public E, public F {};");
|
2013-02-14 16:42:54 +08:00
|
|
|
|
verifyFormat("class AAAAAAAAAAAA : public B,\n"
|
|
|
|
|
" public C,\n"
|
|
|
|
|
" public D,\n"
|
|
|
|
|
" public E,\n"
|
|
|
|
|
" public F,\n"
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
" public G {};");
|
2013-04-06 01:22:09 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("class\n"
|
2013-09-05 17:29:45 +08:00
|
|
|
|
" ReallyReallyLongClassName {\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"};",
|
2013-04-06 01:22:09 +08:00
|
|
|
|
getLLVMStyleWithColumns(32));
|
2013-10-09 00:24:07 +08:00
|
|
|
|
verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaa> {};");
|
|
|
|
|
verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaa> {};");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-08 02:10:23 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("class A {\n} a, b;");
|
|
|
|
|
verifyFormat("struct A {\n} a, b;");
|
|
|
|
|
verifyFormat("union A {\n} a;");
|
2013-01-08 02:10:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsEnum) {
|
2012-12-04 22:46:19 +08:00
|
|
|
|
verifyFormat("enum {\n"
|
|
|
|
|
" Zero,\n"
|
|
|
|
|
" One = 1,\n"
|
|
|
|
|
" Two = One + 1,\n"
|
|
|
|
|
" Three = (One + Two),\n"
|
|
|
|
|
" Four = (Zero && (One ^ Two)) | (One << Two),\n"
|
|
|
|
|
" Five = (One, Two, Three, Four, 5)\n"
|
|
|
|
|
"};");
|
2013-09-13 17:20:45 +08:00
|
|
|
|
verifyGoogleFormat("enum {\n"
|
|
|
|
|
" Zero,\n"
|
|
|
|
|
" One = 1,\n"
|
|
|
|
|
" Two = One + 1,\n"
|
|
|
|
|
" Three = (One + Two),\n"
|
|
|
|
|
" Four = (Zero && (One ^ Two)) | (One << Two),\n"
|
|
|
|
|
" Five = (One, Two, Three, Four, 5)\n"
|
|
|
|
|
"};");
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
verifyFormat("enum Enum {};");
|
|
|
|
|
verifyFormat("enum {};");
|
2013-09-13 17:20:45 +08:00
|
|
|
|
verifyFormat("enum X E {} d;");
|
|
|
|
|
verifyFormat("enum __attribute__((...)) E {} d;");
|
|
|
|
|
verifyFormat("enum __declspec__((...)) E {} d;");
|
2013-01-22 03:17:52 +08:00
|
|
|
|
verifyFormat("enum X f() {\n a();\n return 42;\n}");
|
2013-09-13 17:20:45 +08:00
|
|
|
|
verifyFormat("enum {\n"
|
|
|
|
|
" Bar = Foo<int, int>::value\n"
|
|
|
|
|
"};");
|
2012-12-04 22:46:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 18:10:19 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsEnumsWithErrors) {
|
|
|
|
|
verifyFormat("enum Type {\n"
|
|
|
|
|
" One = 0;\n" // These semicolons should be commas.
|
|
|
|
|
" Two = 1;\n"
|
|
|
|
|
"};");
|
|
|
|
|
verifyFormat("namespace n {\n"
|
|
|
|
|
"enum Type {\n"
|
|
|
|
|
" One,\n"
|
|
|
|
|
" Two,\n" // missing };
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"void g() {}");
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-20 20:42:50 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsEnumStruct) {
|
|
|
|
|
verifyFormat("enum struct {\n"
|
|
|
|
|
" Zero,\n"
|
|
|
|
|
" One = 1,\n"
|
|
|
|
|
" Two = One + 1,\n"
|
|
|
|
|
" Three = (One + Two),\n"
|
|
|
|
|
" Four = (Zero && (One ^ Two)) | (One << Two),\n"
|
|
|
|
|
" Five = (One, Two, Three, Four, 5)\n"
|
|
|
|
|
"};");
|
|
|
|
|
verifyFormat("enum struct Enum {};");
|
|
|
|
|
verifyFormat("enum struct {};");
|
2013-09-13 17:20:45 +08:00
|
|
|
|
verifyFormat("enum struct X E {} d;");
|
|
|
|
|
verifyFormat("enum struct __attribute__((...)) E {} d;");
|
|
|
|
|
verifyFormat("enum struct __declspec__((...)) E {} d;");
|
2013-08-20 20:42:50 +08:00
|
|
|
|
verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, FormatsEnumClass) {
|
|
|
|
|
verifyFormat("enum class {\n"
|
|
|
|
|
" Zero,\n"
|
|
|
|
|
" One = 1,\n"
|
|
|
|
|
" Two = One + 1,\n"
|
|
|
|
|
" Three = (One + Two),\n"
|
|
|
|
|
" Four = (Zero && (One ^ Two)) | (One << Two),\n"
|
|
|
|
|
" Five = (One, Two, Three, Four, 5)\n"
|
|
|
|
|
"};");
|
|
|
|
|
verifyFormat("enum class Enum {};");
|
|
|
|
|
verifyFormat("enum class {};");
|
2013-09-13 17:20:45 +08:00
|
|
|
|
verifyFormat("enum class X E {} d;");
|
|
|
|
|
verifyFormat("enum class __attribute__((...)) E {} d;");
|
|
|
|
|
verifyFormat("enum class __declspec__((...)) E {} d;");
|
2013-08-20 20:42:50 +08:00
|
|
|
|
verifyFormat("enum class X f() {\n a();\n return 42;\n}");
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-05 23:34:55 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsEnumTypes) {
|
|
|
|
|
verifyFormat("enum X : int {\n"
|
|
|
|
|
" A,\n"
|
|
|
|
|
" B\n"
|
|
|
|
|
"};");
|
2013-09-07 05:32:35 +08:00
|
|
|
|
verifyFormat("enum X : std::uint32_t {\n"
|
|
|
|
|
" A,\n"
|
|
|
|
|
" B\n"
|
|
|
|
|
"};");
|
2013-09-05 23:34:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-14 13:49:49 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsBitfields) {
|
|
|
|
|
verifyFormat("struct Bitfields {\n"
|
|
|
|
|
" unsigned sClass : 8;\n"
|
|
|
|
|
" unsigned ValueKind : 2;\n"
|
|
|
|
|
"};");
|
2013-10-10 21:36:20 +08:00
|
|
|
|
verifyFormat("struct A {\n"
|
|
|
|
|
" int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
|
|
|
|
|
"};");
|
2013-01-14 13:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsNamespaces) {
|
|
|
|
|
verifyFormat("namespace some_namespace {\n"
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
"class A {};\n"
|
2013-01-12 01:54:10 +08:00
|
|
|
|
"void f() { f(); }\n"
|
2012-12-07 02:03:27 +08:00
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("namespace {\n"
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
"class A {};\n"
|
2013-01-12 01:54:10 +08:00
|
|
|
|
"void f() { f(); }\n"
|
2012-12-07 02:03:27 +08:00
|
|
|
|
"}");
|
2012-12-31 05:27:25 +08:00
|
|
|
|
verifyFormat("inline namespace X {\n"
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
"class A {};\n"
|
2013-01-12 01:54:10 +08:00
|
|
|
|
"void f() { f(); }\n"
|
2012-12-31 05:27:25 +08:00
|
|
|
|
"}");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
verifyFormat("using namespace some_namespace;\n"
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
"class A {};\n"
|
2013-01-12 01:54:10 +08:00
|
|
|
|
"void f() { f(); }");
|
2013-02-07 00:08:09 +08:00
|
|
|
|
|
|
|
|
|
// This code is more common than we thought; if we
|
|
|
|
|
// layout this correctly the semicolon will go into
|
|
|
|
|
// its own line, which is undesireable.
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
verifyFormat("namespace {};");
|
2013-02-07 00:08:09 +08:00
|
|
|
|
verifyFormat("namespace {\n"
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
"class A {};\n"
|
2013-02-07 00:08:09 +08:00
|
|
|
|
"};");
|
2013-07-01 19:22:57 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("namespace {\n"
|
|
|
|
|
"int SomeVariable = 0; // comment\n"
|
|
|
|
|
"} // namespace");
|
|
|
|
|
EXPECT_EQ("#ifndef HEADER_GUARD\n"
|
|
|
|
|
"#define HEADER_GUARD\n"
|
|
|
|
|
"namespace my_namespace {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"} // my_namespace\n"
|
|
|
|
|
"#endif // HEADER_GUARD",
|
|
|
|
|
format("#ifndef HEADER_GUARD\n"
|
|
|
|
|
" #define HEADER_GUARD\n"
|
|
|
|
|
" namespace my_namespace {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"} // my_namespace\n"
|
|
|
|
|
"#endif // HEADER_GUARD"));
|
2013-08-01 07:16:02 +08:00
|
|
|
|
|
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
|
|
|
|
Style.NamespaceIndentation = FormatStyle::NI_All;
|
|
|
|
|
EXPECT_EQ("namespace out {\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
" namespace in {\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
" } // namespace\n"
|
|
|
|
|
"} // namespace",
|
|
|
|
|
format("namespace out {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"namespace in {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"} // namespace\n"
|
|
|
|
|
"} // namespace",
|
|
|
|
|
Style));
|
|
|
|
|
|
|
|
|
|
Style.NamespaceIndentation = FormatStyle::NI_Inner;
|
|
|
|
|
EXPECT_EQ("namespace out {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"namespace in {\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"} // namespace\n"
|
|
|
|
|
"} // namespace",
|
|
|
|
|
format("namespace out {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"namespace in {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"} // namespace\n"
|
|
|
|
|
"} // namespace",
|
|
|
|
|
Style));
|
2012-12-07 02:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 19:59:17 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
|
2013-01-21 22:32:05 +08:00
|
|
|
|
|
2013-03-14 21:45:21 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsInlineASM) {
|
|
|
|
|
verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
|
|
|
|
|
" \"cpuid\\n\\t\"\n"
|
|
|
|
|
" \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
|
2013-05-15 15:51:51 +08:00
|
|
|
|
" : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
|
2013-03-14 21:45:21 +08:00
|
|
|
|
" : \"a\"(value));");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-08 00:36:17 +08:00
|
|
|
|
TEST_F(FormatTest, FormatTryCatch) {
|
2013-01-12 01:54:10 +08:00
|
|
|
|
// FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
|
|
|
|
|
// also not create single-line-blocks.
|
2013-01-08 00:36:17 +08:00
|
|
|
|
verifyFormat("try {\n"
|
|
|
|
|
" throw a * b;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"catch (int a) {\n"
|
|
|
|
|
" // Do nothing.\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"catch (...) {\n"
|
|
|
|
|
" exit(42);\n"
|
|
|
|
|
"}");
|
|
|
|
|
|
|
|
|
|
// Function-level try statements.
|
2013-01-12 01:54:10 +08:00
|
|
|
|
verifyFormat("int f() try { return 4; }\n"
|
2013-01-08 00:36:17 +08:00
|
|
|
|
"catch (...) {\n"
|
|
|
|
|
" return 5;\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("class A {\n"
|
|
|
|
|
" int a;\n"
|
2013-01-10 21:24:24 +08:00
|
|
|
|
" A() try : a(0) {}\n"
|
2013-01-08 00:36:17 +08:00
|
|
|
|
" catch (...) {\n"
|
|
|
|
|
" throw;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"};\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, FormatObjCTryCatch) {
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@try {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"@catch (NSException e) {\n"
|
|
|
|
|
" @throw;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"@finally {\n"
|
|
|
|
|
" exit(42);\n"
|
|
|
|
|
"}");
|
2013-01-08 00:36:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-17 19:29:41 +08:00
|
|
|
|
TEST_F(FormatTest, StaticInitializers) {
|
|
|
|
|
verifyFormat("static SomeClass SC = { 1, 'a' };");
|
|
|
|
|
|
|
|
|
|
verifyFormat(
|
2013-01-07 21:08:40 +08:00
|
|
|
|
"static SomeClass WithALoooooooooooooooooooongName = {\n"
|
Fixes layout of right braces.
We now decide whether a newline should go before the closing brace
depending on whether a newline was inserted after the opening brace.
For example, we now insert a newline before '};' in:
static SomeClass WithALoooooooooooooooooooongName = {
100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"
};
... while not inserting a newline here:
static SomeClass = { a, b, c, d, e, f, g, h, i, j,
looooooooooooooooooooooooooooooooooongname,
looooooooooooooooooooooooooooooong };
Also fixes the formating of (column limit 25):
int x = {
avariable,
b(alongervariable)
};
llvm-svn: 172076
2013-01-10 23:58:26 +08:00
|
|
|
|
" 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
|
|
|
|
|
"};");
|
|
|
|
|
|
2013-05-17 17:35:01 +08:00
|
|
|
|
// Here, everything other than the "}" would fit on a line.
|
|
|
|
|
verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
|
|
|
|
|
" 100000000000000000000000\n"
|
|
|
|
|
"};");
|
2013-06-13 03:04:12 +08:00
|
|
|
|
EXPECT_EQ("S s = { a, b };", format("S s = {\n"
|
|
|
|
|
" a,\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" b\n"
|
|
|
|
|
"};"));
|
2013-05-17 17:35:01 +08:00
|
|
|
|
|
|
|
|
|
// FIXME: This would fit into the column limit if we'd fit "{ {" on the first
|
|
|
|
|
// line. However, the formatting looks a bit off and this probably doesn't
|
|
|
|
|
// happen often in practice.
|
|
|
|
|
verifyFormat("static int Variable[1] = {\n"
|
|
|
|
|
" { 1000000000000000000000000000000000000 }\n"
|
|
|
|
|
"};",
|
|
|
|
|
getLLVMStyleWithColumns(40));
|
2012-12-17 19:29:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-28 19:30:49 +08:00
|
|
|
|
TEST_F(FormatTest, DesignatedInitializers) {
|
|
|
|
|
verifyFormat("const struct A a = { .a = 1, .b = 2 };");
|
|
|
|
|
verifyFormat("const struct A a = { .aaaaaaaaaa = 1,\n"
|
|
|
|
|
" .bbbbbbbbbb = 2,\n"
|
|
|
|
|
" .cccccccccc = 3,\n"
|
|
|
|
|
" .dddddddddd = 4,\n"
|
|
|
|
|
" .eeeeeeeeee = 5 };");
|
|
|
|
|
verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
|
|
|
|
|
" .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
|
|
|
|
|
" .ccccccccccccccccccccccccccc = 3,\n"
|
|
|
|
|
" .ddddddddddddddddddddddddddd = 4,\n"
|
|
|
|
|
" .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5\n"
|
|
|
|
|
"};");
|
|
|
|
|
|
|
|
|
|
verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
|
|
|
|
|
}
|
|
|
|
|
|
Fixes formatting of nested brace initializers.
We now format this correctly:
Status::Rep Status::global_reps[3] = {
{ kGlobalRef, OK_CODE, NULL, NULL, NULL },
{ kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },
{ kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }
};
- fixed a bug where BreakBeforeClosingBrace would be set on the wrong
state
- added penalties for breaking between = and {, and between { and any
other non-{ token
llvm-svn: 172433
2013-01-15 00:41:43 +08:00
|
|
|
|
TEST_F(FormatTest, NestedStaticInitializers) {
|
|
|
|
|
verifyFormat("static A x = { { {} } };\n");
|
2013-02-18 19:59:17 +08:00
|
|
|
|
verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
|
|
|
|
|
" { init1, init2, init3, init4 } } };");
|
2013-01-16 22:59:02 +08:00
|
|
|
|
|
2013-02-18 19:59:17 +08:00
|
|
|
|
verifyFormat("somes Status::global_reps[3] = {\n"
|
|
|
|
|
" { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
|
|
|
|
|
" { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
|
|
|
|
|
" { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
|
|
|
|
|
"};");
|
2013-05-23 18:15:45 +08:00
|
|
|
|
verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
|
2013-07-17 02:22:10 +08:00
|
|
|
|
" {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
|
|
|
|
|
" {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
|
|
|
|
|
" {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
|
Fixes formatting of nested brace initializers.
We now format this correctly:
Status::Rep Status::global_reps[3] = {
{ kGlobalRef, OK_CODE, NULL, NULL, NULL },
{ kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },
{ kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }
};
- fixed a bug where BreakBeforeClosingBrace would be set on the wrong
state
- added penalties for breaking between = and {, and between { and any
other non-{ token
llvm-svn: 172433
2013-01-15 00:41:43 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
|
|
|
|
|
" { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
|
|
|
|
|
" } };");
|
|
|
|
|
|
2013-01-31 22:59:26 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n"
|
|
|
|
|
" { 111111111111111111111111111111,\n"
|
|
|
|
|
" 222222222222222222222222222222,\n"
|
|
|
|
|
" 333333333333333333333333333333 },\n"
|
|
|
|
|
" { 1, 2, 3 }, { 1, 2, 3 } } };");
|
Fix bug in formatting of nested initializers.
We can now format:
SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },
{ { 111111111111111111111111111111,
222222222222222222222222222222,
333333333333333333333333333333 } },
{ { 1, 2, 3 } }, { { 1, 2, 3 } } };
Before, we did strange things there.
llvm-svn: 174291
2013-02-04 02:07:15 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n"
|
|
|
|
|
" { { 111111111111111111111111111111,\n"
|
|
|
|
|
" 222222222222222222222222222222,\n"
|
|
|
|
|
" 333333333333333333333333333333 } },\n"
|
|
|
|
|
" { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
|
2013-05-23 18:15:45 +08:00
|
|
|
|
verifyGoogleFormat(
|
2013-07-17 02:22:10 +08:00
|
|
|
|
"SomeArrayOfSomeType a = {\n"
|
|
|
|
|
" {{1, 2, 3}}, {{1, 2, 3}},\n"
|
|
|
|
|
" {{111111111111111111111111111111, 222222222222222222222222222222,\n"
|
|
|
|
|
" 333333333333333333333333333333}},\n"
|
|
|
|
|
" {{1, 2, 3}}, {{1, 2, 3}}};");
|
2013-01-31 22:59:26 +08:00
|
|
|
|
|
2013-02-18 19:59:17 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"struct {\n"
|
|
|
|
|
" unsigned bit;\n"
|
|
|
|
|
" const char *const name;\n"
|
2013-10-24 22:14:49 +08:00
|
|
|
|
"} kBitsToOs[] = { { kOsMac, \"Mac\" },\n"
|
|
|
|
|
" { kOsWin, \"Windows\" },\n"
|
|
|
|
|
" { kOsLinux, \"Linux\" },\n"
|
|
|
|
|
" { kOsCrOS, \"Chrome OS\" } };");
|
Fixes formatting of nested brace initializers.
We now format this correctly:
Status::Rep Status::global_reps[3] = {
{ kGlobalRef, OK_CODE, NULL, NULL, NULL },
{ kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },
{ kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }
};
- fixed a bug where BreakBeforeClosingBrace would be set on the wrong
state
- added penalties for breaking between = and {, and between { and any
other non-{ token
llvm-svn: 172433
2013-01-15 00:41:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 00:30:12 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
|
|
|
|
|
verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
|
|
|
|
|
" \\\n"
|
|
|
|
|
" aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-07 18:48:50 +08:00
|
|
|
|
TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
|
2013-10-12 05:43:05 +08:00
|
|
|
|
verifyFormat("virtual void write(ELFWriter *writerrr,\n"
|
|
|
|
|
" OwningPtr<FileOutputBuffer> &buffer) = 0;");
|
2013-01-07 18:48:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-12 05:43:05 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
|
|
|
|
|
verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
|
|
|
|
|
getLLVMStyleWithColumns(40));
|
|
|
|
|
verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
|
|
|
|
|
getLLVMStyleWithColumns(40));
|
|
|
|
|
EXPECT_EQ("#define Q \\\n"
|
|
|
|
|
" \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
|
|
|
|
|
" \"aaaaaaaa.cpp\"",
|
|
|
|
|
format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
|
|
|
|
|
getLLVMStyleWithColumns(40)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, UnderstandsLinePPDirective) {
|
|
|
|
|
EXPECT_EQ("# 123 \"A string literal\"",
|
2013-01-31 23:58:48 +08:00
|
|
|
|
format(" # 123 \"A string literal\""));
|
2013-10-12 05:43:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, LayoutUnknownPPDirective) {
|
2013-01-31 23:58:48 +08:00
|
|
|
|
EXPECT_EQ("#;", format("#;"));
|
2013-01-07 18:03:37 +08:00
|
|
|
|
verifyFormat("#\n;\n;\n;");
|
2013-01-03 00:30:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
|
|
|
|
|
EXPECT_EQ("#line 42 \"test\"\n",
|
|
|
|
|
format("# \\\n line \\\n 42 \\\n \"test\"\n"));
|
2013-02-18 19:59:17 +08:00
|
|
|
|
EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
|
|
|
|
|
getLLVMStyleWithColumns(12)));
|
2013-01-03 00:30:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, EndOfFileEndsPPDirective) {
|
|
|
|
|
EXPECT_EQ("#line 42 \"test\"",
|
|
|
|
|
format("# \\\n line \\\n 42 \\\n \"test\""));
|
2013-02-18 19:59:17 +08:00
|
|
|
|
EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
|
2013-01-03 00:30:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-12 03:45:02 +08:00
|
|
|
|
TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
|
|
|
|
|
verifyFormat("#define A \\x20");
|
|
|
|
|
verifyFormat("#define A \\ x20");
|
|
|
|
|
EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
|
|
|
|
|
verifyFormat("#define A ''");
|
|
|
|
|
verifyFormat("#define A ''qqq");
|
|
|
|
|
verifyFormat("#define A `qqq");
|
|
|
|
|
verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 02:33:23 +08:00
|
|
|
|
TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
|
2013-02-28 19:05:57 +08:00
|
|
|
|
verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
|
|
|
|
|
verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
|
2013-01-03 02:33:23 +08:00
|
|
|
|
verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
|
2013-02-28 19:05:57 +08:00
|
|
|
|
// FIXME: We never break before the macro name.
|
2013-08-28 17:07:32 +08:00
|
|
|
|
verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
|
2013-01-14 23:40:57 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("#define A A\n#define A A");
|
|
|
|
|
verifyFormat("#define A(X) A\n#define A A");
|
|
|
|
|
|
2013-02-28 19:05:57 +08:00
|
|
|
|
verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
|
|
|
|
|
verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
|
2013-01-03 02:33:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
|
2013-03-29 02:40:55 +08:00
|
|
|
|
EXPECT_EQ("// somecomment\n"
|
2013-01-05 07:34:14 +08:00
|
|
|
|
"#include \"a.h\"\n"
|
2013-02-28 19:05:57 +08:00
|
|
|
|
"#define A( \\\n"
|
|
|
|
|
" A, B)\n"
|
2013-01-05 07:34:14 +08:00
|
|
|
|
"#include \"b.h\"\n"
|
2013-03-27 19:52:18 +08:00
|
|
|
|
"// somecomment\n",
|
2013-03-29 02:40:55 +08:00
|
|
|
|
format(" // somecomment\n"
|
2013-01-05 07:34:14 +08:00
|
|
|
|
" #include \"a.h\"\n"
|
|
|
|
|
"#define A(A,\\\n"
|
|
|
|
|
" B)\n"
|
|
|
|
|
" #include \"b.h\"\n"
|
2013-03-27 19:52:18 +08:00
|
|
|
|
" // somecomment\n",
|
2013-02-18 19:59:17 +08:00
|
|
|
|
getLLVMStyleWithColumns(13)));
|
2013-01-05 07:34:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 19:59:17 +08:00
|
|
|
|
TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
|
2013-01-05 07:34:14 +08:00
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
|
|
|
|
|
EXPECT_EQ("#define A \\\n"
|
|
|
|
|
" c; \\\n"
|
|
|
|
|
" e;\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"f;",
|
|
|
|
|
format("#define A c; e;\n"
|
|
|
|
|
"f;",
|
|
|
|
|
getLLVMStyleWithColumns(14)));
|
2013-01-03 02:33:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 19:59:17 +08:00
|
|
|
|
TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
|
2013-01-05 07:34:14 +08:00
|
|
|
|
|
2013-05-10 21:00:49 +08:00
|
|
|
|
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".
|
2013-01-05 07:34:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, MacroDefinitionInsideStatement) {
|
2013-01-09 23:25:02 +08:00
|
|
|
|
EXPECT_EQ("int x,\n"
|
|
|
|
|
"#define A\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" y;",
|
|
|
|
|
format("int x,\n#define A\ny;"));
|
2013-01-05 07:34:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-06 05:34:55 +08:00
|
|
|
|
TEST_F(FormatTest, HashInMacroDefinition) {
|
2013-09-10 21:41:43 +08:00
|
|
|
|
EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
|
2013-01-06 05:34:55 +08:00
|
|
|
|
verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
|
2013-08-28 17:07:32 +08:00
|
|
|
|
verifyFormat("#define A \\\n"
|
|
|
|
|
" { \\\n"
|
|
|
|
|
" f(#c); \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" }",
|
|
|
|
|
getLLVMStyleWithColumns(11));
|
2013-01-09 00:17:54 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("#define A(X) \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" void function##X()",
|
|
|
|
|
getLLVMStyleWithColumns(22));
|
2013-01-09 00:17:54 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("#define A(a, b, c) \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" void a##b##c()",
|
|
|
|
|
getLLVMStyleWithColumns(22));
|
2013-01-09 00:17:54 +08:00
|
|
|
|
|
2013-01-14 23:40:57 +08:00
|
|
|
|
verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
|
2013-01-06 05:34:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-23 22:37:36 +08:00
|
|
|
|
TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
|
|
|
|
|
verifyFormat("#define A (1)");
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-11 20:33:24 +08:00
|
|
|
|
TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
|
|
|
|
|
EXPECT_EQ("#define A b;", format("#define A \\\n"
|
|
|
|
|
" \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" b;",
|
|
|
|
|
getLLVMStyleWithColumns(25)));
|
2013-02-11 20:33:24 +08:00
|
|
|
|
EXPECT_EQ("#define A \\\n"
|
|
|
|
|
" \\\n"
|
|
|
|
|
" a; \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" b;",
|
|
|
|
|
format("#define A \\\n"
|
|
|
|
|
" \\\n"
|
|
|
|
|
" a; \\\n"
|
|
|
|
|
" b;",
|
|
|
|
|
getLLVMStyleWithColumns(11)));
|
2013-02-11 20:33:24 +08:00
|
|
|
|
EXPECT_EQ("#define A \\\n"
|
|
|
|
|
" a; \\\n"
|
|
|
|
|
" \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" b;",
|
|
|
|
|
format("#define A \\\n"
|
|
|
|
|
" a; \\\n"
|
|
|
|
|
" \\\n"
|
|
|
|
|
" b;",
|
|
|
|
|
getLLVMStyleWithColumns(11)));
|
2013-02-11 20:33:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 01:14:38 +08:00
|
|
|
|
TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
|
2013-03-18 20:50:26 +08:00
|
|
|
|
verifyFormat("#define A :");
|
2013-02-20 01:14:38 +08:00
|
|
|
|
verifyFormat("#define SOMECASES \\\n"
|
2013-03-20 18:23:53 +08:00
|
|
|
|
" case 1: \\\n"
|
2013-02-20 01:14:38 +08:00
|
|
|
|
" case 2\n",
|
|
|
|
|
getLLVMStyleWithColumns(20));
|
|
|
|
|
verifyFormat("#define A template <typename T>");
|
2013-02-20 04:05:41 +08:00
|
|
|
|
verifyFormat("#define STR(x) #x\n"
|
|
|
|
|
"f(STR(this_is_a_string_literal{));");
|
2013-08-28 17:17:37 +08:00
|
|
|
|
verifyFormat("#pragma omp threadprivate( \\\n"
|
|
|
|
|
" y)), // expected-warning",
|
|
|
|
|
getLLVMStyleWithColumns(28));
|
2013-02-20 01:14:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-29 21:16:10 +08:00
|
|
|
|
TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
|
|
|
|
|
verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
|
|
|
|
|
EXPECT_EQ("class A : public QObject {\n"
|
|
|
|
|
" Q_OBJECT\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" A() {}\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("class A : public QObject {\n"
|
|
|
|
|
" Q_OBJECT\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" A() {\n}\n"
|
|
|
|
|
"} ;"));
|
2013-05-29 22:09:17 +08:00
|
|
|
|
EXPECT_EQ("SOME_MACRO\n"
|
|
|
|
|
"namespace {\n"
|
|
|
|
|
"void f();\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("SOME_MACRO\n"
|
|
|
|
|
" namespace {\n"
|
|
|
|
|
"void f( );\n"
|
|
|
|
|
"}"));
|
2013-05-29 21:16:10 +08:00
|
|
|
|
// Only if the identifier contains at least 5 characters.
|
|
|
|
|
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"
|
|
|
|
|
"};",
|
|
|
|
|
format("class A : public QObject {\n"
|
|
|
|
|
" Q_Object\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" A() {\n}\n"
|
|
|
|
|
"} ;"));
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-09 06:16:06 +08:00
|
|
|
|
TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
|
|
|
|
|
EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
|
|
|
|
|
"INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
|
|
|
|
|
"INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
"class X {};\n"
|
2013-04-09 06:16:06 +08:00
|
|
|
|
"INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
|
|
|
|
|
"int *createScopDetectionPass() { return 0; }",
|
|
|
|
|
format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
|
|
|
|
|
" INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
|
|
|
|
|
" INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
|
|
|
|
|
" class X {};\n"
|
|
|
|
|
" INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
|
|
|
|
|
" int *createScopDetectionPass() { return 0; }"));
|
2013-04-10 00:15:19 +08:00
|
|
|
|
// FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
|
|
|
|
|
// braces, so that inner block is indented one level more.
|
|
|
|
|
EXPECT_EQ("int q() {\n"
|
|
|
|
|
" IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
|
|
|
|
|
" IPC_MESSAGE_HANDLER(xxx, qqq)\n"
|
|
|
|
|
" IPC_END_MESSAGE_MAP()\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("int q() {\n"
|
|
|
|
|
" IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
|
|
|
|
|
" IPC_MESSAGE_HANDLER(xxx, qqq)\n"
|
|
|
|
|
" IPC_END_MESSAGE_MAP()\n"
|
|
|
|
|
"}"));
|
2013-09-17 17:26:07 +08:00
|
|
|
|
|
|
|
|
|
// These must not be recognized as macros.
|
2013-04-10 00:15:19 +08:00
|
|
|
|
EXPECT_EQ("int q() {\n"
|
|
|
|
|
" f(x);\n"
|
|
|
|
|
" f(x) {}\n"
|
|
|
|
|
" f(x)->g();\n"
|
|
|
|
|
" f(x)->*g();\n"
|
|
|
|
|
" f(x).g();\n"
|
|
|
|
|
" f(x) = x;\n"
|
|
|
|
|
" f(x) += x;\n"
|
|
|
|
|
" f(x) -= x;\n"
|
|
|
|
|
" f(x) *= x;\n"
|
|
|
|
|
" f(x) /= x;\n"
|
|
|
|
|
" f(x) %= x;\n"
|
|
|
|
|
" f(x) &= x;\n"
|
|
|
|
|
" f(x) |= x;\n"
|
|
|
|
|
" f(x) ^= x;\n"
|
|
|
|
|
" f(x) >>= x;\n"
|
|
|
|
|
" f(x) <<= x;\n"
|
|
|
|
|
" f(x)[y].z();\n"
|
|
|
|
|
" LOG(INFO) << x;\n"
|
|
|
|
|
" ifstream(x) >> x;\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
format("int q() {\n"
|
|
|
|
|
" f(x)\n;\n"
|
|
|
|
|
" f(x)\n {}\n"
|
|
|
|
|
" f(x)\n->g();\n"
|
|
|
|
|
" f(x)\n->*g();\n"
|
|
|
|
|
" f(x)\n.g();\n"
|
|
|
|
|
" f(x)\n = x;\n"
|
|
|
|
|
" f(x)\n += x;\n"
|
|
|
|
|
" f(x)\n -= x;\n"
|
|
|
|
|
" f(x)\n *= x;\n"
|
|
|
|
|
" f(x)\n /= x;\n"
|
|
|
|
|
" f(x)\n %= x;\n"
|
|
|
|
|
" f(x)\n &= x;\n"
|
|
|
|
|
" f(x)\n |= x;\n"
|
|
|
|
|
" f(x)\n ^= x;\n"
|
|
|
|
|
" f(x)\n >>= x;\n"
|
|
|
|
|
" f(x)\n <<= x;\n"
|
|
|
|
|
" f(x)\n[y].z();\n"
|
|
|
|
|
" LOG(INFO)\n << x;\n"
|
|
|
|
|
" ifstream(x)\n >> x;\n"
|
|
|
|
|
"}\n"));
|
|
|
|
|
EXPECT_EQ("int q() {\n"
|
|
|
|
|
" f(x)\n"
|
|
|
|
|
" if (1) {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" f(x)\n"
|
|
|
|
|
" while (1) {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" f(x)\n"
|
|
|
|
|
" g(x);\n"
|
|
|
|
|
" f(x)\n"
|
|
|
|
|
" try {\n"
|
|
|
|
|
" q();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" catch (...) {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
format("int q() {\n"
|
|
|
|
|
"f(x)\n"
|
|
|
|
|
"if (1) {}\n"
|
|
|
|
|
"f(x)\n"
|
|
|
|
|
"while (1) {}\n"
|
|
|
|
|
"f(x)\n"
|
|
|
|
|
"g(x);\n"
|
|
|
|
|
"f(x)\n"
|
|
|
|
|
"try { q(); } catch (...) {}\n"
|
|
|
|
|
"}\n"));
|
|
|
|
|
EXPECT_EQ("class A {\n"
|
|
|
|
|
" A() : t(0) {}\n"
|
|
|
|
|
" A(X x)\n" // FIXME: function-level try blocks are broken.
|
|
|
|
|
" try : t(0) {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" catch (...) {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"};",
|
|
|
|
|
format("class A {\n"
|
|
|
|
|
" A()\n : t(0) {}\n"
|
|
|
|
|
" A(X x)\n"
|
|
|
|
|
" try : t(0) {} catch (...) {}\n"
|
|
|
|
|
"};"));
|
2013-04-09 06:16:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
|
TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
|
|
|
|
|
verifyFormat("#define A \\\n"
|
|
|
|
|
" f({ \\\n"
|
|
|
|
|
" g(); \\\n"
|
|
|
|
|
" });", getLLVMStyleWithColumns(11));
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-06 06:14:16 +08:00
|
|
|
|
TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
|
|
|
|
|
EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-06 06:56:06 +08:00
|
|
|
|
TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
|
2013-01-12 01:54:10 +08:00
|
|
|
|
verifyFormat("{\n { a #c; }\n}");
|
2013-01-06 06:56:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-07 04:07:31 +08:00
|
|
|
|
TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
|
|
|
|
|
EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
|
|
|
|
|
format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
|
|
|
|
|
EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
|
|
|
|
|
format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
|
|
|
|
|
}
|
2013-01-05 07:34:14 +08:00
|
|
|
|
|
2013-06-08 01:45:07 +08:00
|
|
|
|
TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
|
2013-01-07 21:26:07 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"#define A \\\n int i; \\\n int j;",
|
|
|
|
|
format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
|
2013-06-08 01:45:07 +08:00
|
|
|
|
EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
|
2013-01-07 15:56:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-14 19:46:10 +08:00
|
|
|
|
TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
|
|
|
|
|
EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-07 17:24:17 +08:00
|
|
|
|
TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
|
|
|
|
|
verifyFormat("#define A \\\n"
|
|
|
|
|
" int v( \\\n"
|
|
|
|
|
" a); \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" int i;",
|
|
|
|
|
getLLVMStyleWithColumns(11));
|
2013-01-07 17:24:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 00:30:12 +08:00
|
|
|
|
TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
|
2013-01-05 07:34:14 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"#define ALooooooooooooooooooooooooooooooooooooooongMacro("
|
|
|
|
|
" \\\n"
|
|
|
|
|
" aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
|
|
|
|
|
" aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
|
|
|
|
|
format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
|
|
|
|
|
"\\\n"
|
|
|
|
|
"aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
|
|
|
|
|
" \n"
|
|
|
|
|
" AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
|
|
|
|
|
" aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
|
2013-01-03 00:30:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 23:25:02 +08:00
|
|
|
|
TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
|
|
|
|
|
EXPECT_EQ("int\n"
|
|
|
|
|
"#define A\n"
|
2013-05-06 16:27:33 +08:00
|
|
|
|
" a;",
|
2013-06-22 01:25:42 +08:00
|
|
|
|
format("int\n#define A\na;", getGoogleStyle()));
|
2013-02-18 19:59:17 +08:00
|
|
|
|
verifyFormat("functionCallTo(\n"
|
|
|
|
|
" someOtherFunction(\n"
|
|
|
|
|
" withSomeParameters, whichInSequence,\n"
|
|
|
|
|
" areLongerThanALine(andAnotherCall,\n"
|
|
|
|
|
"#define A B\n"
|
|
|
|
|
" withMoreParamters,\n"
|
|
|
|
|
" whichStronglyInfluenceTheLayout),\n"
|
Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.
Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.
Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
.begin(), E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc), d, bar(e, f));
After:
for (llvm::ArrayRef<NamedDecl *>::iterator
I = FD->getDeclsInPrototypeScope().begin(),
E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc),
d, bar(e, f));
llvm-svn: 175741
2013-02-21 23:00:29 +08:00
|
|
|
|
" andMoreParameters),\n"
|
|
|
|
|
" trailing);",
|
2013-02-18 19:59:17 +08:00
|
|
|
|
getLLVMStyleWithColumns(69));
|
2013-09-17 17:52:48 +08:00
|
|
|
|
verifyFormat("Foo::Foo()\n"
|
|
|
|
|
"#ifdef BAR\n"
|
|
|
|
|
" : baz(0)\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
"}");
|
2013-10-12 05:25:45 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" if (true)\n"
|
|
|
|
|
"#ifdef A\n"
|
|
|
|
|
" f(42);\n"
|
|
|
|
|
" x();\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
" x();\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("void f(param1, param2,\n"
|
|
|
|
|
" param3,\n"
|
|
|
|
|
"#ifdef A\n"
|
|
|
|
|
" param4(param5,\n"
|
|
|
|
|
"#ifdef A1\n"
|
|
|
|
|
" param6,\n"
|
|
|
|
|
"#ifdef A2\n"
|
|
|
|
|
" param7),\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
" param8),\n"
|
|
|
|
|
" param9,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" param10,\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" param11)\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
" param12)\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" x();\n"
|
|
|
|
|
"}",
|
|
|
|
|
getLLVMStyleWithColumns(28));
|
2013-10-12 21:32:56 +08:00
|
|
|
|
verifyFormat("#if 1\n"
|
|
|
|
|
"int i;");
|
2013-10-22 16:27:19 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"#if 1\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
"#if 1\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
"#endif\n");
|
2013-10-24 23:23:11 +08:00
|
|
|
|
verifyFormat("DEBUG({\n"
|
|
|
|
|
" return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
|
|
|
|
|
"});\n"
|
|
|
|
|
"#if a\n"
|
|
|
|
|
"#else\n"
|
|
|
|
|
"#endif");
|
2013-01-09 23:25:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 16:11:15 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 19:52:21 +08:00
|
|
|
|
TEST_F(FormatTest, LayoutBlockInsideParens) {
|
2013-09-05 17:29:45 +08:00
|
|
|
|
EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
|
2013-01-10 19:52:21 +08:00
|
|
|
|
EXPECT_EQ("functionCall({\n"
|
|
|
|
|
" int i;\n"
|
2013-09-05 17:29:45 +08:00
|
|
|
|
" int j;\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"});",
|
2013-09-05 17:29:45 +08:00
|
|
|
|
format(" functionCall ( {int i;int j;} );"));
|
2013-05-31 22:56:29 +08:00
|
|
|
|
EXPECT_EQ("functionCall({\n"
|
2013-09-05 17:29:45 +08:00
|
|
|
|
" int i;\n"
|
|
|
|
|
" int j;\n"
|
|
|
|
|
" },\n"
|
2013-05-31 22:56:29 +08:00
|
|
|
|
" aaaa, bbbb, cccc);",
|
2013-09-05 17:29:45 +08:00
|
|
|
|
format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
|
|
|
|
|
EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
|
|
|
|
|
format(" functionCall (aaaa, bbbb, {int i;});"));
|
|
|
|
|
EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
" int j;\n"
|
|
|
|
|
"});",
|
|
|
|
|
format(" functionCall (aaaa, bbbb, {int i;int j;});"));
|
|
|
|
|
EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
|
|
|
|
|
format(" functionCall (aaaa, bbbb, {int i;});"));
|
2013-05-31 22:56:29 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"Aaa({\n"
|
2013-09-05 17:29:45 +08:00
|
|
|
|
" int i; // break\n"
|
|
|
|
|
" },\n"
|
2013-05-31 22:56:29 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
|
|
|
|
|
" ccccccccccccccccc));");
|
2013-09-05 17:29:45 +08:00
|
|
|
|
verifyFormat("DEBUG({\n"
|
|
|
|
|
" if (a)\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"});");
|
2013-01-10 19:52:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, LayoutBlockInsideStatement) {
|
2013-01-12 01:54:10 +08:00
|
|
|
|
EXPECT_EQ("SOME_MACRO { int i; }\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"int i;",
|
|
|
|
|
format(" SOME_MACRO {int i;} int i;"));
|
2013-01-10 19:52:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, LayoutNestedBlocks) {
|
|
|
|
|
verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
|
|
|
|
|
" struct s {\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
" };\n"
|
|
|
|
|
" s kBitsToOs[] = { { 10 } };\n"
|
|
|
|
|
" for (int i = 0; i < 10; ++i)\n"
|
|
|
|
|
" return;\n"
|
|
|
|
|
"}");
|
2013-09-06 15:54:20 +08:00
|
|
|
|
verifyFormat("call(parameter, {\n"
|
|
|
|
|
" something();\n"
|
|
|
|
|
" // Comment using all columns.\n"
|
|
|
|
|
" somethingelse();\n"
|
|
|
|
|
"});",
|
|
|
|
|
getLLVMStyleWithColumns(40));
|
|
|
|
|
EXPECT_EQ("call(parameter, {\n"
|
|
|
|
|
" something();\n"
|
|
|
|
|
" // Comment too\n"
|
|
|
|
|
" // looooooooooong.\n"
|
|
|
|
|
" somethingElse();\n"
|
|
|
|
|
"});",
|
|
|
|
|
format("call(parameter, {\n"
|
|
|
|
|
" something();\n"
|
|
|
|
|
" // Comment too looooooooooong.\n"
|
|
|
|
|
" somethingElse();\n"
|
|
|
|
|
"});",
|
|
|
|
|
getLLVMStyleWithColumns(29)));
|
2013-09-08 22:07:57 +08:00
|
|
|
|
EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
|
2013-10-21 01:28:32 +08:00
|
|
|
|
EXPECT_EQ("DEBUG({ // comment\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"});",
|
|
|
|
|
format("DEBUG({ // comment\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"});"));
|
2013-09-08 22:07:57 +08:00
|
|
|
|
EXPECT_EQ("DEBUG({\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" // comment\n"
|
|
|
|
|
" int j;\n"
|
|
|
|
|
"});",
|
|
|
|
|
format("DEBUG({\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" // comment\n"
|
|
|
|
|
" int j;\n"
|
|
|
|
|
"});"));
|
2013-01-10 19:52:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 21:24:24 +08:00
|
|
|
|
TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
|
|
|
|
|
EXPECT_EQ("{}", format("{}"));
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
verifyFormat("enum E {};");
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("enum E {}");
|
2013-01-10 21:24:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Line break tests.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2013-08-30 16:29:25 +08:00
|
|
|
|
TEST_F(FormatTest, PreventConfusingIndents) {
|
2012-12-07 02:03:27 +08:00
|
|
|
|
verifyFormat(
|
2013-01-28 15:35:34 +08:00
|
|
|
|
"SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
|
|
|
|
|
" parameter, parameter, parameter)),\n"
|
2012-12-25 00:43:00 +08:00
|
|
|
|
" SecondLongCall(parameter));");
|
2013-01-28 15:35:34 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaa);");
|
|
|
|
|
verifyFormat(
|
2013-07-12 19:19:37 +08:00
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" [aaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
|
|
|
|
|
" [aaaaaaaaaaaaaaaaaaaaaaaa]];");
|
2013-01-28 15:35:34 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaa<\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaa>;");
|
2013-01-28 17:35:24 +08:00
|
|
|
|
verifyFormat("int a = bbbb && ccc && fffff(\n"
|
|
|
|
|
"#define A Just forcing a new line\n"
|
|
|
|
|
" ddd);");
|
2013-01-28 15:35:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-08 22:12:04 +08:00
|
|
|
|
TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"bool aaaaaaa =\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
|
|
|
|
|
" bbbbbbbb();");
|
|
|
|
|
verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
|
|
|
|
|
" ccccccccc == ddddddddddd;");
|
|
|
|
|
|
|
|
|
|
verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
|
|
|
|
|
" aaaaaa) &&\n"
|
|
|
|
|
" bbbbbb && cccccc;");
|
2013-05-28 15:42:44 +08:00
|
|
|
|
verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
|
|
|
|
|
" aaaaaa) >>\n"
|
|
|
|
|
" bbbbbb;");
|
2013-05-08 22:12:04 +08:00
|
|
|
|
verifyFormat("Whitespaces.addUntouchableComment(\n"
|
|
|
|
|
" SourceMgr.getSpellingColumnNumber(\n"
|
|
|
|
|
" TheLine.Last->FormatTok.Tok.getLocation()) -\n"
|
|
|
|
|
" 1);");
|
2013-05-15 04:39:56 +08:00
|
|
|
|
|
2013-06-03 16:42:05 +08:00
|
|
|
|
verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" cccccc) {\n}");
|
|
|
|
|
|
2013-07-03 18:34:47 +08:00
|
|
|
|
// If the LHS of a comparison is not a binary expression itself, the
|
|
|
|
|
// additional linebreak confuses many people.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
|
|
|
|
|
"}");
|
2013-09-06 16:08:14 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
|
|
|
|
|
"}");
|
2013-07-03 18:34:47 +08:00
|
|
|
|
// Even explicit parentheses stress the precedence enough to make the
|
|
|
|
|
// additional break unnecessary.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
|
|
|
|
|
"}");
|
|
|
|
|
// This cases is borderline, but with the indentation it is still readable.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
|
|
|
|
|
"}",
|
|
|
|
|
getLLVMStyleWithColumns(75));
|
|
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
|
"}");
|
|
|
|
|
|
2013-05-15 04:39:56 +08:00
|
|
|
|
FormatStyle OnePerLine = getLLVMStyle();
|
|
|
|
|
OnePerLine.BinPackParameters = false;
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
|
|
|
|
|
OnePerLine);
|
2013-05-08 22:12:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-09 04:33:42 +08:00
|
|
|
|
TEST_F(FormatTest, ExpressionIndentation) {
|
|
|
|
|
verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
|
|
|
|
|
" ccccccccccccccccccccccccccccccccccccccccc;");
|
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
|
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
|
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
|
2013-06-06 17:11:58 +08:00
|
|
|
|
verifyFormat("if () {\n"
|
|
|
|
|
"} else if (aaaaa && bbbbb > // break\n"
|
|
|
|
|
" ccccc) {\n"
|
|
|
|
|
"}");
|
2013-06-17 21:19:53 +08:00
|
|
|
|
|
|
|
|
|
// Presence of a trailing comment used to change indentation of b.
|
|
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" b;\n"
|
|
|
|
|
"return aaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" b; //",
|
|
|
|
|
getLLVMStyleWithColumns(30));
|
2013-04-09 04:33:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-27 00:56:36 +08:00
|
|
|
|
TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
|
|
|
|
|
// Not sure what the best system is here. Like this, the LHS can be found
|
|
|
|
|
// immediately above an operator (everything with the same or a higher
|
|
|
|
|
// indent). The RHS is aligned right of the operator and so compasses
|
|
|
|
|
// everything until something with the same indent as the operator is found.
|
|
|
|
|
// FIXME: Is this a good system?
|
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
|
|
|
|
Style.BreakBeforeBinaryOperators = true;
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
|
|
|
|
|
" + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
|
|
|
|
|
" && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" > ccccccccccccccccccccccccccccccccccccccccc;",
|
|
|
|
|
Style);
|
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
|
|
|
|
|
Style);
|
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
|
|
|
|
|
Style);
|
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
|
|
|
|
|
Style);
|
|
|
|
|
verifyFormat("if () {\n"
|
|
|
|
|
"} else if (aaaaa && bbbbb // break\n"
|
|
|
|
|
" > ccccc) {\n"
|
|
|
|
|
"}",
|
|
|
|
|
Style);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-19 05:05:13 +08:00
|
|
|
|
TEST_F(FormatTest, ConstructorInitializers) {
|
2013-01-10 21:24:24 +08:00
|
|
|
|
verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
|
2013-01-11 19:37:55 +08:00
|
|
|
|
verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
|
|
|
|
|
getLLVMStyleWithColumns(45));
|
2013-02-22 05:33:55 +08:00
|
|
|
|
verifyFormat("Constructor()\n"
|
|
|
|
|
" : Inttializer(FitsOnTheLine) {}",
|
2013-01-11 19:37:55 +08:00
|
|
|
|
getLLVMStyleWithColumns(44));
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("Constructor()\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" : Inttializer(FitsOnTheLine) {}",
|
2013-01-22 00:42:44 +08:00
|
|
|
|
getLLVMStyleWithColumns(43));
|
2012-12-19 05:05:13 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"SomeClass::Constructor()\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
|
2012-12-19 05:05:13 +08:00
|
|
|
|
|
2013-01-11 19:37:55 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"SomeClass::Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
|
2012-12-19 05:05:13 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"SomeClass::Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
|
2012-12-19 05:05:13 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaa() {}");
|
2012-12-19 05:05:13 +08:00
|
|
|
|
|
2013-01-28 21:31:35 +08:00
|
|
|
|
verifyFormat("Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
|
2013-01-28 21:31:35 +08:00
|
|
|
|
|
2013-02-25 23:59:54 +08:00
|
|
|
|
verifyFormat("Constructor(int Parameter = 0)\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
|
2013-09-07 05:46:41 +08:00
|
|
|
|
verifyFormat("Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
|
|
|
|
|
"}",
|
|
|
|
|
getLLVMStyleWithColumns(60));
|
2013-02-25 23:59:54 +08:00
|
|
|
|
|
2012-12-19 05:05:13 +08:00
|
|
|
|
// Here a line could be saved by splitting the second initializer onto two
|
|
|
|
|
// lines, but that is not desireable.
|
2013-02-27 17:47:53 +08:00
|
|
|
|
verifyFormat("Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaa(aaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
|
2012-12-19 05:05:13 +08:00
|
|
|
|
|
2013-02-16 00:49:44 +08:00
|
|
|
|
FormatStyle OnePerLine = getLLVMStyle();
|
|
|
|
|
OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
|
|
|
|
|
verifyFormat("SomeClass::Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
|
2013-02-18 19:59:17 +08:00
|
|
|
|
OnePerLine);
|
2013-02-16 00:49:44 +08:00
|
|
|
|
verifyFormat("SomeClass::Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
|
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
|
2013-02-18 19:59:17 +08:00
|
|
|
|
OnePerLine);
|
2013-02-16 00:49:44 +08:00
|
|
|
|
verifyFormat("MyClass::MyClass(int var)\n"
|
|
|
|
|
" : some_var_(var), // 4 space indent\n"
|
|
|
|
|
" some_other_var_(var + 1) { // lined up\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
OnePerLine);
|
2013-02-28 17:39:12 +08:00
|
|
|
|
verifyFormat("Constructor()\n"
|
|
|
|
|
" : aaaaa(aaaaaa),\n"
|
|
|
|
|
" aaaaa(aaaaaa),\n"
|
|
|
|
|
" aaaaa(aaaaaa),\n"
|
|
|
|
|
" aaaaa(aaaaaa),\n"
|
|
|
|
|
" aaaaa(aaaaaa) {}",
|
|
|
|
|
OnePerLine);
|
2013-04-22 15:59:53 +08:00
|
|
|
|
verifyFormat("Constructor()\n"
|
|
|
|
|
" : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaa) {}",
|
|
|
|
|
OnePerLine);
|
2013-04-25 21:31:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, MemoizationTests) {
|
|
|
|
|
// This breaks if the memoization lookup does not take \c Indent and
|
|
|
|
|
// \c LastSpace into account.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"extern CFRunLoopTimerRef\n"
|
|
|
|
|
"CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
|
|
|
|
|
" CFTimeInterval interval, CFOptionFlags flags,\n"
|
|
|
|
|
" CFIndex order, CFRunLoopTimerCallBack callout,\n"
|
2013-05-06 16:27:33 +08:00
|
|
|
|
" CFRunLoopTimerContext *context) {}");
|
2013-04-25 21:31:51 +08:00
|
|
|
|
|
|
|
|
|
// Deep nesting somewhat works around our memoization.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
|
|
|
|
|
" aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
|
|
|
|
|
" aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
|
|
|
|
|
" aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
|
|
|
|
|
" aaaaa())))))))))))))))))))))))))))))))))))))));",
|
|
|
|
|
getLLVMStyleWithColumns(65));
|
2013-05-13 17:19:24 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa(\n"
|
|
|
|
|
" aaaaa,\n"
|
|
|
|
|
" aaaaa))))))))))));",
|
|
|
|
|
getLLVMStyleWithColumns(65));
|
2013-05-22 13:27:42 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" a)",
|
|
|
|
|
getLLVMStyleWithColumns(65));
|
2013-01-12 15:36:22 +08:00
|
|
|
|
|
|
|
|
|
// This test takes VERY long when memoization is broken.
|
2013-04-25 21:31:51 +08:00
|
|
|
|
FormatStyle OnePerLine = getLLVMStyle();
|
|
|
|
|
OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
|
2013-02-27 17:47:53 +08:00
|
|
|
|
OnePerLine.BinPackParameters = false;
|
2013-01-16 22:59:02 +08:00
|
|
|
|
std::string input = "Constructor()\n"
|
2013-01-25 23:43:32 +08:00
|
|
|
|
" : aaaa(a,\n";
|
2013-01-16 22:59:02 +08:00
|
|
|
|
for (unsigned i = 0, e = 80; i != e; ++i) {
|
|
|
|
|
input += " a,\n";
|
|
|
|
|
}
|
2013-02-22 05:33:55 +08:00
|
|
|
|
input += " a) {}";
|
2013-02-27 17:47:53 +08:00
|
|
|
|
verifyFormat(input, OnePerLine);
|
2012-12-19 05:05:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksAsHighAsPossible) {
|
|
|
|
|
verifyFormat(
|
2013-03-13 00:28:18 +08:00
|
|
|
|
"void f() {\n"
|
|
|
|
|
" if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
|
|
|
|
|
" (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}");
|
2013-02-26 21:59:14 +08:00
|
|
|
|
verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
|
2013-04-09 04:33:42 +08:00
|
|
|
|
" Intervals[i - 1].getRange().getLast()) {\n}");
|
2012-12-07 02:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-11 22:29:13 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksFunctionDeclarations) {
|
|
|
|
|
// Principially, we break function declarations in a certain order:
|
|
|
|
|
// 1) break amongst arguments.
|
|
|
|
|
verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccccccc);");
|
2013-08-19 18:16:18 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"template <class TemplateIt>\n"
|
|
|
|
|
"SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
|
|
|
|
|
" TemplateIt *stop) {}");
|
2013-04-11 22:29:13 +08:00
|
|
|
|
|
|
|
|
|
// 2) break after return type.
|
2013-05-06 16:27:33 +08:00
|
|
|
|
verifyFormat(
|
2013-06-22 01:25:42 +08:00
|
|
|
|
"Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
|
|
|
|
|
getGoogleStyle());
|
2013-04-11 22:29:13 +08:00
|
|
|
|
|
|
|
|
|
// 3) break after (.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
|
2013-06-22 01:25:42 +08:00
|
|
|
|
" Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
|
|
|
|
|
getGoogleStyle());
|
2013-04-11 22:29:13 +08:00
|
|
|
|
|
|
|
|
|
// 4) break before after nested name specifiers.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
2013-05-06 16:27:33 +08:00
|
|
|
|
" SomeClasssssssssssssssssssssssssssssssssssssss::\n"
|
2013-06-22 01:25:42 +08:00
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
|
|
|
|
|
getGoogleStyle());
|
2013-04-11 22:29:13 +08:00
|
|
|
|
|
|
|
|
|
// However, there are exceptions, if a sufficient amount of lines can be
|
|
|
|
|
// saved.
|
|
|
|
|
// FIXME: The precise cut-offs wrt. the number of saved lines might need some
|
|
|
|
|
// more adjusting.
|
|
|
|
|
verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc);");
|
|
|
|
|
verifyFormat(
|
2013-06-22 01:25:42 +08:00
|
|
|
|
"Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
2013-05-06 16:27:33 +08:00
|
|
|
|
" bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
|
2013-06-22 01:25:42 +08:00
|
|
|
|
" Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
|
|
|
|
|
getGoogleStyle());
|
2013-04-11 22:29:13 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc);");
|
|
|
|
|
verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
|
|
|
|
|
" Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
|
2013-04-16 06:36:37 +08:00
|
|
|
|
|
|
|
|
|
// Break after multi-line parameters.
|
|
|
|
|
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" bbbb bbbb);");
|
2013-07-09 15:43:55 +08:00
|
|
|
|
|
|
|
|
|
// Treat overloaded operators like other functions.
|
|
|
|
|
verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
|
|
|
|
|
"operator>(const SomeLoooooooooooooooooooooooooogType &other);");
|
2013-08-28 15:27:35 +08:00
|
|
|
|
verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
|
|
|
|
|
"operator>>(const SomeLooooooooooooooooooooooooogType &other);");
|
2013-07-09 15:43:55 +08:00
|
|
|
|
verifyGoogleFormat(
|
|
|
|
|
"SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
|
|
|
|
|
" const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
|
2013-04-11 22:29:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-09 22:36:48 +08:00
|
|
|
|
TEST_F(FormatTest, TrailingReturnType) {
|
|
|
|
|
verifyFormat("auto foo() -> int;\n");
|
|
|
|
|
verifyFormat("struct S {\n"
|
|
|
|
|
" auto bar() const -> int;\n"
|
|
|
|
|
"};");
|
|
|
|
|
verifyFormat("template <size_t Order, typename T>\n"
|
|
|
|
|
"auto load_img(const std::string &filename)\n"
|
|
|
|
|
" -> alias::tensor<Order, T, mem::tag::cpu> {}");
|
2013-07-11 22:33:06 +08:00
|
|
|
|
|
|
|
|
|
// Not trailing return types.
|
|
|
|
|
verifyFormat("void f() { auto a = b->c(); }");
|
2013-07-09 22:36:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 17:16:31 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
|
2013-10-19 00:34:40 +08:00
|
|
|
|
// Avoid breaking before trailing 'const' or other trailing annotations, if
|
|
|
|
|
// they are not function-like.
|
2013-05-22 16:28:26 +08:00
|
|
|
|
FormatStyle Style = getGoogleStyle();
|
|
|
|
|
Style.ColumnLimit = 47;
|
|
|
|
|
verifyFormat("void\n"
|
|
|
|
|
"someLongFunction(int someLongParameter) const {\n}",
|
|
|
|
|
getLLVMStyleWithColumns(47));
|
|
|
|
|
verifyFormat("LoooooongReturnType\n"
|
|
|
|
|
"someLoooooooongFunction() const {}",
|
|
|
|
|
getLLVMStyleWithColumns(47));
|
|
|
|
|
verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
|
|
|
|
|
" const {}",
|
|
|
|
|
Style);
|
2013-10-19 00:34:40 +08:00
|
|
|
|
verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
|
|
|
|
|
verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
|
|
|
|
|
verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
|
|
|
|
|
|
|
|
|
|
// Unless this would lead to the first parameter being broken.
|
|
|
|
|
verifyFormat("void someLongFunction(int someLongParameter)\n"
|
|
|
|
|
" const {}",
|
|
|
|
|
getLLVMStyleWithColumns(46));
|
|
|
|
|
verifyFormat("void someLongFunction(int someLongParameter)\n"
|
|
|
|
|
" const {}",
|
|
|
|
|
Style);
|
|
|
|
|
verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" LONG_AND_UGLY_ANNOTATION;");
|
2013-07-12 05:02:56 +08:00
|
|
|
|
|
|
|
|
|
// Breaking before function-like trailing annotations is fine to keep them
|
|
|
|
|
// close to their arguments.
|
2013-05-21 17:16:31 +08:00
|
|
|
|
verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
|
|
|
|
|
verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
|
|
|
|
|
" LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
|
|
|
|
|
verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
|
|
|
|
|
" LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
|
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"void aaaaaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaa));");
|
|
|
|
|
verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" __attribute__((unused));");
|
|
|
|
|
verifyFormat(
|
2013-06-22 01:25:42 +08:00
|
|
|
|
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" GUARDED_BY(aaaaaaaaaaaa);",
|
|
|
|
|
getGoogleStyle());
|
2013-07-11 20:34:23 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" GUARDED_BY(aaaaaaaaaaaa);",
|
|
|
|
|
getGoogleStyle());
|
2013-05-21 17:16:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksDesireably) {
|
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
|
2013-01-24 04:41:06 +08:00
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
|
|
|
|
|
"}");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
|
2012-12-04 18:50:12 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
|
|
|
|
|
" aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
|
2012-12-05 22:57:28 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
|
|
|
|
|
2013-01-02 22:40:02 +08:00
|
|
|
|
verifyFormat(
|
2013-03-13 00:28:18 +08:00
|
|
|
|
"void f() {\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
|
|
|
|
|
"}");
|
2013-01-25 23:43:32 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
|
2013-03-20 21:53:11 +08:00
|
|
|
|
verifyFormat(
|
2013-10-25 22:29:37 +08:00
|
|
|
|
"aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-01-02 22:40:02 +08:00
|
|
|
|
|
2013-08-30 16:29:25 +08:00
|
|
|
|
// Indent consistently indenpendent of call expression.
|
|
|
|
|
verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
|
|
|
|
|
" dddddddddddddddddddddddddddddd));\n"
|
|
|
|
|
"aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
|
|
|
|
|
" dddddddddddddddddddddddddddddd));");
|
|
|
|
|
|
2012-12-05 22:57:28 +08:00
|
|
|
|
// This test case breaks on an incorrect memoization, i.e. an optimization not
|
|
|
|
|
// taking into account the StopAt value.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
2012-12-24 21:43:52 +08:00
|
|
|
|
" aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
2012-12-25 00:43:00 +08:00
|
|
|
|
|
2013-01-07 21:26:07 +08:00
|
|
|
|
verifyFormat("{\n {\n {\n"
|
|
|
|
|
" Annotation.SpaceRequiredBefore =\n"
|
|
|
|
|
" Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
|
|
|
|
|
" Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
|
|
|
|
|
" }\n }\n}");
|
2013-06-17 20:59:44 +08:00
|
|
|
|
|
|
|
|
|
// Break on an outer level if there was a break on an inner level.
|
|
|
|
|
EXPECT_EQ("f(g(h(a, // comment\n"
|
|
|
|
|
" b, c),\n"
|
|
|
|
|
" d, e),\n"
|
|
|
|
|
" x, y);",
|
|
|
|
|
format("f(g(h(a, // comment\n"
|
|
|
|
|
" b, c), d, e), x, y);"));
|
2013-07-08 22:25:23 +08:00
|
|
|
|
|
|
|
|
|
// Prefer breaking similar line breaks.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"const int kTrackingOptions = NSTrackingMouseMoved |\n"
|
|
|
|
|
" NSTrackingMouseEnteredAndExited |\n"
|
|
|
|
|
" NSTrackingActiveAlways;");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-16 22:59:02 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
|
2013-04-03 21:36:17 +08:00
|
|
|
|
FormatStyle NoBinPacking = getGoogleStyle();
|
2013-02-27 17:47:53 +08:00
|
|
|
|
NoBinPacking.BinPackParameters = false;
|
|
|
|
|
verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
|
|
|
|
|
NoBinPacking);
|
|
|
|
|
verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
|
|
|
|
|
NoBinPacking);
|
|
|
|
|
verifyFormat(
|
2013-01-16 22:59:02 +08:00
|
|
|
|
"aaaaaaaa(aaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
|
|
|
|
|
" aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
2013-02-27 17:47:53 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
|
|
|
|
|
NoBinPacking);
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaa();",
|
|
|
|
|
NoBinPacking);
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);",
|
|
|
|
|
NoBinPacking);
|
2013-02-18 19:59:17 +08:00
|
|
|
|
|
2013-02-27 17:47:53 +08:00
|
|
|
|
verifyFormat(
|
2013-01-23 18:08:28 +08:00
|
|
|
|
"aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaa,\n"
|
2013-02-27 17:47:53 +08:00
|
|
|
|
" aaaaaaaaaaaa);",
|
|
|
|
|
NoBinPacking);
|
|
|
|
|
verifyFormat(
|
2013-01-16 22:59:02 +08:00
|
|
|
|
"somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
|
|
|
|
|
" ddddddddddddddddddddddddddddd),\n"
|
2013-02-27 17:47:53 +08:00
|
|
|
|
" test);",
|
|
|
|
|
NoBinPacking);
|
|
|
|
|
|
|
|
|
|
verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
|
|
|
|
|
NoBinPacking);
|
|
|
|
|
verifyFormat("a(\"a\"\n"
|
|
|
|
|
" \"a\",\n"
|
|
|
|
|
" a);");
|
|
|
|
|
|
|
|
|
|
NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
|
2013-01-30 00:03:49 +08:00
|
|
|
|
verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
|
2013-01-23 18:08:28 +08:00
|
|
|
|
" aaaaaaaaa,\n"
|
2013-01-30 00:03:49 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
|
2013-02-27 17:47:53 +08:00
|
|
|
|
NoBinPacking);
|
2013-02-14 22:26:07 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"void f() {\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" .aaaaaaa();\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
2013-02-27 17:47:53 +08:00
|
|
|
|
NoBinPacking);
|
2013-05-22 16:55:55 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"template <class SomeType, class SomeOtherType>\n"
|
|
|
|
|
"SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
|
|
|
|
|
NoBinPacking);
|
2013-01-16 22:59:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-10 22:02:49 +08:00
|
|
|
|
TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
|
|
|
|
|
FormatStyle Style = getLLVMStyleWithColumns(15);
|
|
|
|
|
Style.ExperimentalAutoDetectBinPacking = true;
|
|
|
|
|
EXPECT_EQ("aaa(aaaa,\n"
|
|
|
|
|
" aaaa,\n"
|
|
|
|
|
" aaaa);\n"
|
|
|
|
|
"aaa(aaaa,\n"
|
|
|
|
|
" aaaa,\n"
|
|
|
|
|
" aaaa);",
|
|
|
|
|
format("aaa(aaaa,\n" // one-per-line
|
|
|
|
|
" aaaa,\n"
|
|
|
|
|
" aaaa );\n"
|
|
|
|
|
"aaa(aaaa, aaaa, aaaa);", // inconclusive
|
|
|
|
|
Style));
|
|
|
|
|
EXPECT_EQ("aaa(aaaa, aaaa,\n"
|
|
|
|
|
" aaaa);\n"
|
|
|
|
|
"aaa(aaaa, aaaa,\n"
|
|
|
|
|
" aaaa);",
|
|
|
|
|
format("aaa(aaaa, aaaa,\n" // bin-packed
|
|
|
|
|
" aaaa );\n"
|
|
|
|
|
"aaa(aaaa, aaaa, aaaa);", // inconclusive
|
|
|
|
|
Style));
|
|
|
|
|
}
|
|
|
|
|
|
Reduce penalty for splitting between ")" and ".".
').' is likely part of a builder pattern statement.
This is based upon a patch developed by Nico Weber. Thank you!
Before:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name).StartsWith(
".eh_frame_hdr", ORDER_EH_FRAMEHDR).StartsWith(
".eh_frame", ORDER_EH_FRAME).StartsWith(".init", ORDER_INIT).StartsWith(
".fini", ORDER_FINI).StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
After:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
Probably not ideal, but makes many cases much more readable.
The changes to overriding-ftemplate-comments.cpp don't seem better or
worse. We should address those soon.
llvm-svn: 172804
2013-01-18 18:56:38 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsBuilderPattern) {
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"return llvm::StringSwitch<Reference::Kind>(name)\n"
|
clang-format: Don't indent builders relative to "return".
While this looks kind of nice, it wastes horizontal space and does not
seem to be common in the LLVM codebase.
Before:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
After:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
llvm-svn: 189657
2013-08-30 15:27:13 +08:00
|
|
|
|
" .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");
|
2013-05-15 17:35:08 +08:00
|
|
|
|
|
2013-02-16 04:33:06 +08:00
|
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
|
2013-04-09 04:33:42 +08:00
|
|
|
|
" aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
|
2013-02-18 21:24:21 +08:00
|
|
|
|
verifyFormat(
|
2013-08-07 13:34:02 +08:00
|
|
|
|
"aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
|
2013-02-18 21:24:21 +08:00
|
|
|
|
" ->aaaaaaaa(aaaaaaaaaaaaaaa);");
|
2013-02-18 21:52:06 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
|
2013-03-02 00:48:32 +08:00
|
|
|
|
" aaaaaaaaaaaaaa);");
|
2013-02-18 21:52:06 +08:00
|
|
|
|
verifyFormat(
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
|
|
|
|
|
" aaaaaa->aaaaaaaaaaaa()\n"
|
|
|
|
|
" ->aaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" ->aaaaaaaaaaaaaaaaa();");
|
2013-10-25 22:29:37 +08:00
|
|
|
|
verifyGoogleFormat(
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
|
"someobj->Add((new util::filetools::Handler(dir))\n"
|
|
|
|
|
" ->OnEvent1(NewPermanentCallback(\n"
|
|
|
|
|
" this, &HandlerHolderClass::EventHandlerCBA))\n"
|
|
|
|
|
" ->OnEvent2(NewPermanentCallback(\n"
|
|
|
|
|
" this, &HandlerHolderClass::EventHandlerCBB))\n"
|
|
|
|
|
" ->OnEvent3(NewPermanentCallback(\n"
|
|
|
|
|
" this, &HandlerHolderClass::EventHandlerCBC))\n"
|
|
|
|
|
" ->OnEvent5(NewPermanentCallback(\n"
|
|
|
|
|
" this, &HandlerHolderClass::EventHandlerCBD))\n"
|
|
|
|
|
" ->OnEvent6(NewPermanentCallback(\n"
|
|
|
|
|
" this, &HandlerHolderClass::EventHandlerCBE)));");
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
After:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaa();");
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaa();");
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaa();");
|
2013-08-30 15:12:40 +08:00
|
|
|
|
verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" ->aaaaaaaaaaaaaae(0)\n"
|
|
|
|
|
" ->aaaaaaaaaaaaaaa();");
|
2013-09-06 16:54:24 +08:00
|
|
|
|
|
2013-10-18 23:23:06 +08:00
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
|
|
|
|
|
" .has<bbbbbbbbbbbbbbbbbbbbb>();");
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
|
|
|
|
|
|
2013-09-06 16:54:24 +08:00
|
|
|
|
// Prefer not to break after empty parentheses.
|
|
|
|
|
verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
|
|
|
|
|
" First->LastNewlineOffset);");
|
Reduce penalty for splitting between ")" and ".".
').' is likely part of a builder pattern statement.
This is based upon a patch developed by Nico Weber. Thank you!
Before:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name).StartsWith(
".eh_frame_hdr", ORDER_EH_FRAMEHDR).StartsWith(
".eh_frame", ORDER_EH_FRAME).StartsWith(".init", ORDER_INIT).StartsWith(
".fini", ORDER_FINI).StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
After:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
Probably not ideal, but makes many cases much more readable.
The changes to overriding-ftemplate-comments.cpp don't seem better or
worse. We should address those soon.
llvm-svn: 172804
2013-01-18 18:56:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-24 08:13:23 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
|
2013-01-07 21:26:07 +08:00
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" ccccccccccccccccccccccccc) {\n}");
|
2013-01-07 21:26:07 +08:00
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" ccccccccccccccccccccccccc) {\n}");
|
2012-12-24 08:13:23 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" ccccccccccccccccccccccccc) {\n}");
|
2013-02-08 23:28:42 +08:00
|
|
|
|
verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
|
|
|
|
|
" bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
|
|
|
|
|
" cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
|
|
|
|
|
" dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
|
|
|
|
|
verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
|
|
|
|
|
" aaaaaaaaaaaaaaa != aa) {\n}");
|
2012-12-24 08:13:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-23 20:27:43 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksAfterAssignments) {
|
2013-01-07 21:08:40 +08:00
|
|
|
|
verifyFormat(
|
2013-01-23 20:27:43 +08:00
|
|
|
|
"unsigned Cost =\n"
|
|
|
|
|
" TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
|
|
|
|
|
" SI->getPointerAddressSpaceee());\n");
|
2013-01-07 21:08:40 +08:00
|
|
|
|
verifyFormat(
|
2013-01-23 23:55:19 +08:00
|
|
|
|
"CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
|
|
|
|
|
" Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
|
2013-01-24 00:58:21 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
|
2013-05-27 20:45:09 +08:00
|
|
|
|
verifyFormat("unsigned OriginalStartColumn =\n"
|
|
|
|
|
" SourceMgr.getSpellingColumnNumber(\n"
|
|
|
|
|
" Current.FormatTok.getStartOfNonWhitespace()) -\n"
|
|
|
|
|
" 1;");
|
2013-01-07 21:08:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-24 21:43:52 +08:00
|
|
|
|
TEST_F(FormatTest, AlignsAfterAssignments) {
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
2013-01-07 21:08:40 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
2012-12-24 21:43:52 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
2013-01-07 21:08:40 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
2012-12-24 21:43:52 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
2013-01-07 21:08:40 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
2012-12-24 21:43:52 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
2013-01-07 21:08:40 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-07-08 22:25:23 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaa;");
|
2012-12-24 21:43:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, AlignsAfterReturn) {
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-04-02 22:33:13 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
|
2013-04-09 04:33:42 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaa();");
|
2013-04-02 22:33:13 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
|
2013-04-09 04:33:42 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaa());");
|
|
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
|
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
|
2013-09-30 16:29:03 +08:00
|
|
|
|
verifyFormat("return\n"
|
|
|
|
|
" // true if code is one of a or b.\n"
|
|
|
|
|
" code == a || code == b;");
|
2012-12-24 21:43:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 15:06:56 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksConditionalExpressions) {
|
|
|
|
|
verifyFormat(
|
2013-07-08 22:25:23 +08:00
|
|
|
|
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-01-28 20:45:14 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
|
|
|
|
|
" : aaaaaaaaaaaaa);");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
2013-03-20 21:53:11 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
2013-01-28 20:45:14 +08:00
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaa);");
|
2013-08-02 06:05:00 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaa);");
|
2013-01-28 20:45:14 +08:00
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
|
|
|
|
verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-08-02 06:05:00 +08:00
|
|
|
|
verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-02-15 19:07:25 +08:00
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
|
2013-04-02 22:33:13 +08:00
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaa;");
|
2013-02-24 05:01:55 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" ? aaaaaaaaaaaaaaa\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaa;");
|
|
|
|
|
verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
|
2013-04-09 04:33:42 +08:00
|
|
|
|
" aaaaaaaaa\n"
|
2013-04-08 18:45:44 +08:00
|
|
|
|
" ? b\n"
|
|
|
|
|
" : c);");
|
2013-02-09 00:49:27 +08:00
|
|
|
|
verifyFormat(
|
2013-02-15 19:07:25 +08:00
|
|
|
|
"unsigned Indent =\n"
|
|
|
|
|
" format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
|
|
|
|
|
" ? IndentForLevel[TheLine.Level]\n"
|
Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.
Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.
Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
.begin(), E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc), d, bar(e, f));
After:
for (llvm::ArrayRef<NamedDecl *>::iterator
I = FD->getDeclsInPrototypeScope().begin(),
E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc),
d, bar(e, f));
llvm-svn: 175741
2013-02-21 23:00:29 +08:00
|
|
|
|
" : TheLine * 2,\n"
|
|
|
|
|
" TheLine.InPPDirective, PreviousEndOfLineColumn);",
|
2013-02-18 19:59:17 +08:00
|
|
|
|
getLLVMStyleWithColumns(70));
|
2013-05-31 22:56:12 +08:00
|
|
|
|
verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
|
|
|
|
|
" ? aaaaaaaaaaaaaaa\n"
|
|
|
|
|
" : bbbbbbbbbbbbbbb //\n"
|
|
|
|
|
" ? ccccccccccccccc\n"
|
|
|
|
|
" : ddddddddddddddd;");
|
|
|
|
|
verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
|
|
|
|
|
" ? aaaaaaaaaaaaaaa\n"
|
|
|
|
|
" : (bbbbbbbbbbbbbbb //\n"
|
|
|
|
|
" ? ccccccccccccccc\n"
|
|
|
|
|
" : ddddddddddddddd);");
|
2013-02-15 19:07:25 +08:00
|
|
|
|
|
2013-02-27 17:47:53 +08:00
|
|
|
|
FormatStyle NoBinPacking = getLLVMStyle();
|
|
|
|
|
NoBinPacking.BinPackParameters = false;
|
|
|
|
|
verifyFormat(
|
2013-02-24 05:01:55 +08:00
|
|
|
|
"void f() {\n"
|
|
|
|
|
" g(aaa,\n"
|
|
|
|
|
" aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" ? aaaaaaaaaaaaaaa\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaa);\n"
|
2013-02-27 17:47:53 +08:00
|
|
|
|
"}",
|
|
|
|
|
NoBinPacking);
|
2013-08-02 06:05:00 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"void f() {\n"
|
|
|
|
|
" g(aaa,\n"
|
|
|
|
|
" aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" ?: aaaaaaaaaaaaaaa);\n"
|
|
|
|
|
"}",
|
|
|
|
|
NoBinPacking);
|
2013-01-09 15:06:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-29 19:21:01 +08:00
|
|
|
|
TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
|
|
|
|
|
verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
|
|
|
|
|
" aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
|
|
|
|
|
verifyFormat("bool a = true, b = false;");
|
|
|
|
|
|
|
|
|
|
verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
2013-04-03 21:36:17 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
|
2013-01-29 19:21:01 +08:00
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
|
2013-04-02 22:33:13 +08:00
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
|
2013-01-29 19:21:01 +08:00
|
|
|
|
verifyFormat(
|
Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.
Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.
Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
.begin(), E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc), d, bar(e, f));
After:
for (llvm::ArrayRef<NamedDecl *>::iterator
I = FD->getDeclsInPrototypeScope().begin(),
E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc),
d, bar(e, f));
llvm-svn: 175741
2013-02-21 23:00:29 +08:00
|
|
|
|
"bool aaaaaaaaaaaaaaaaaaaaa =\n"
|
2013-04-03 21:36:17 +08:00
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
|
Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.
Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.
Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
.begin(), E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc), d, bar(e, f));
After:
for (llvm::ArrayRef<NamedDecl *>::iterator
I = FD->getDeclsInPrototypeScope().begin(),
E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc),
d, bar(e, f));
llvm-svn: 175741
2013-02-21 23:00:29 +08:00
|
|
|
|
" d = e && f;");
|
Improve formatting of multi-variable DeclStmts.
This fixed llvm.org/PR15670
Before:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc,
d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc,
*d = ddddddddddddddddddd;
After:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc, d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc, *d = ddddddddddddddddddd;
llvm-svn: 178860
2013-04-05 17:38:50 +08:00
|
|
|
|
verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
|
|
|
|
|
" c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
|
|
|
|
|
verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
|
|
|
|
|
" *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
|
|
|
|
|
verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
|
|
|
|
|
" ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
|
|
|
|
|
// FIXME: If multiple variables are defined, the "*" needs to move to the new
|
|
|
|
|
// line. Also fix indent for breaking after the type, this looks bad.
|
2013-06-22 01:25:42 +08:00
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
|
2013-05-06 16:27:33 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
|
2013-06-22 01:25:42 +08:00
|
|
|
|
" *b = bbbbbbbbbbbbbbbbbbb;",
|
|
|
|
|
getGoogleStyle());
|
Improve formatting of multi-variable DeclStmts.
This fixed llvm.org/PR15670
Before:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc,
d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc,
*d = ddddddddddddddddddd;
After:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc, d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc, *d = ddddddddddddddddddd;
llvm-svn: 178860
2013-04-05 17:38:50 +08:00
|
|
|
|
|
|
|
|
|
// Not ideal, but pointer-with-type does not allow much here.
|
|
|
|
|
verifyGoogleFormat(
|
|
|
|
|
"aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
|
|
|
|
|
" *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
|
2013-01-29 19:21:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-12 09:28:06 +08:00
|
|
|
|
TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
|
|
|
|
|
verifyFormat("arr[foo ? bar : baz];");
|
|
|
|
|
verifyFormat("f()[foo ? bar : baz];");
|
|
|
|
|
verifyFormat("(a + b)[foo ? bar : baz];");
|
|
|
|
|
verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
|
TEST_F(FormatTest, AlignsStringLiterals) {
|
|
|
|
|
verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
|
|
|
|
|
" \"short literal\");");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"looooooooooooooooooooooooongFunction(\n"
|
|
|
|
|
" \"short literal\"\n"
|
|
|
|
|
" \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
|
2013-02-18 19:59:17 +08:00
|
|
|
|
verifyFormat("someFunction(\"Always break between multi-line\"\n"
|
|
|
|
|
" \" string literals\",\n"
|
|
|
|
|
" and, other, parameters);");
|
2013-02-20 23:25:48 +08:00
|
|
|
|
EXPECT_EQ("fun + \"1243\" /* comment */\n"
|
|
|
|
|
" \"5678\";",
|
|
|
|
|
format("fun + \"1243\" /* comment */\n"
|
|
|
|
|
" \"5678\";",
|
|
|
|
|
getLLVMStyleWithColumns(28)));
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
|
|
|
|
|
" \"aaaaaaaaaaaaaaaaaaaaa\"\n"
|
|
|
|
|
" \"aaaaaaaaaaaaaaaa\";",
|
|
|
|
|
format("aaaaaa ="
|
|
|
|
|
"\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaa\" "
|
|
|
|
|
"\"aaaaaaaaaaaaaaaa\";"));
|
2013-02-20 23:32:58 +08:00
|
|
|
|
verifyFormat("a = a + \"a\"\n"
|
|
|
|
|
" \"a\"\n"
|
|
|
|
|
" \"a\";");
|
2013-05-16 12:26:02 +08:00
|
|
|
|
verifyFormat("f(\"a\", \"b\"\n"
|
|
|
|
|
" \"c\");");
|
2013-02-23 15:46:38 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"#define LL_FORMAT \"ll\"\n"
|
|
|
|
|
"printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
|
|
|
|
|
" \"d, ddddddddd: %\" LL_FORMAT \"d\");");
|
2013-05-14 04:50:15 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("#define A(X) \\\n"
|
|
|
|
|
" \"aaaaa\" #X \"bbbbbb\" \\\n"
|
|
|
|
|
" \"ccccc\"",
|
|
|
|
|
getLLVMStyleWithColumns(23));
|
|
|
|
|
verifyFormat("#define A \"def\"\n"
|
|
|
|
|
"f(\"abc\" A \"ghi\"\n"
|
|
|
|
|
" \"jkl\");");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 20:02:44 +08:00
|
|
|
|
TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
|
|
|
|
|
FormatStyle NoBreak = getLLVMStyle();
|
|
|
|
|
NoBreak.AlwaysBreakBeforeMultilineStrings = false;
|
|
|
|
|
FormatStyle Break = getLLVMStyle();
|
|
|
|
|
Break.AlwaysBreakBeforeMultilineStrings = true;
|
2013-07-17 23:38:19 +08:00
|
|
|
|
verifyFormat("aaaa = \"bbbb\"\n"
|
|
|
|
|
" \"cccc\";",
|
|
|
|
|
NoBreak);
|
|
|
|
|
verifyFormat("aaaa =\n"
|
|
|
|
|
" \"bbbb\"\n"
|
|
|
|
|
" \"cccc\";",
|
|
|
|
|
Break);
|
|
|
|
|
verifyFormat("aaaa(\"bbbb\"\n"
|
|
|
|
|
" \"cccc\");",
|
|
|
|
|
NoBreak);
|
|
|
|
|
verifyFormat("aaaa(\n"
|
|
|
|
|
" \"bbbb\"\n"
|
|
|
|
|
" \"cccc\");",
|
|
|
|
|
Break);
|
|
|
|
|
verifyFormat("aaaa(qqq, \"bbbb\"\n"
|
|
|
|
|
" \"cccc\");",
|
|
|
|
|
NoBreak);
|
|
|
|
|
verifyFormat("aaaa(qqq,\n"
|
|
|
|
|
" \"bbbb\"\n"
|
|
|
|
|
" \"cccc\");",
|
|
|
|
|
Break);
|
|
|
|
|
|
|
|
|
|
// Don't break if there is no column gain.
|
|
|
|
|
verifyFormat("f(\"aaaa\"\n"
|
|
|
|
|
" \"bbbb\");",
|
|
|
|
|
Break);
|
|
|
|
|
|
|
|
|
|
// Treat literals with escaped newlines like multi-line string literals.
|
2013-07-17 05:06:13 +08:00
|
|
|
|
EXPECT_EQ("x = \"a\\\n"
|
|
|
|
|
"b\\\n"
|
|
|
|
|
"c\";",
|
|
|
|
|
format("x = \"a\\\n"
|
|
|
|
|
"b\\\n"
|
|
|
|
|
"c\";",
|
|
|
|
|
NoBreak));
|
|
|
|
|
EXPECT_EQ("x =\n"
|
|
|
|
|
" \"a\\\n"
|
|
|
|
|
"b\\\n"
|
|
|
|
|
"c\";",
|
|
|
|
|
format("x = \"a\\\n"
|
|
|
|
|
"b\\\n"
|
|
|
|
|
"c\";",
|
|
|
|
|
Break));
|
2013-07-04 20:02:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
TEST_F(FormatTest, AlignsPipes) {
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaa;");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
|
|
|
|
|
" \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
|
|
|
|
|
" << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
|
2013-02-04 15:34:48 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("return out << \"somepacket = {\\n\"\n"
|
2013-10-12 05:25:45 +08:00
|
|
|
|
" << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
|
|
|
|
|
" << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
|
|
|
|
|
" << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
|
|
|
|
|
" << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
|
2013-02-04 15:34:48 +08:00
|
|
|
|
" << \"}\";");
|
2013-02-07 05:04:05 +08:00
|
|
|
|
|
2013-07-15 22:12:30 +08:00
|
|
|
|
verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
|
2013-02-07 05:04:05 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
|
|
|
|
|
" << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
|
|
|
|
|
" << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
|
|
|
|
|
" << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
|
2013-03-14 22:00:17 +08:00
|
|
|
|
verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
|
|
|
|
|
" << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
|
2013-07-15 22:33:14 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"void f() {\n"
|
|
|
|
|
" llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
|
|
|
|
|
" << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
|
|
|
|
|
"}");
|
2013-04-02 22:33:13 +08:00
|
|
|
|
|
2013-07-12 04:41:21 +08:00
|
|
|
|
// Breaking before the first "<<" is generally not desirable.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"llvm::errs()\n"
|
|
|
|
|
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
|
|
|
|
|
getLLVMStyleWithColumns(70));
|
|
|
|
|
verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << \"aaaaaaaaaaaaaaaaaaa: \"\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << \"aaaaaaaaaaaaaaaaaaa: \"\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
|
|
|
|
|
getLLVMStyleWithColumns(70));
|
|
|
|
|
|
2013-08-12 20:58:05 +08:00
|
|
|
|
// But sometimes, breaking before the first "<<" is desirable.
|
2013-07-12 23:14:05 +08:00
|
|
|
|
verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
|
2013-08-12 20:58:05 +08:00
|
|
|
|
verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
|
|
|
|
|
" << BEF << IsTemplate << Description << E->getType();");
|
2013-07-12 23:14:05 +08:00
|
|
|
|
|
2013-04-02 22:33:13 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
|
2013-09-29 20:02:57 +08:00
|
|
|
|
|
|
|
|
|
// Incomplete string literal.
|
|
|
|
|
EXPECT_EQ("llvm::errs() << \"\n"
|
|
|
|
|
" << a;",
|
|
|
|
|
format("llvm::errs() << \"\n<<a;"));
|
2012-12-07 02:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsEquals) {
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaa =\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"if (a) {\n"
|
2013-01-10 21:24:24 +08:00
|
|
|
|
" f();\n"
|
2012-12-04 02:12:45 +08:00
|
|
|
|
"} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
|
|
|
|
|
"}");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
2013-02-18 19:59:17 +08:00
|
|
|
|
verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
|
|
|
|
" 100000000 + 10000000) {\n}");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-17 22:34:14 +08:00
|
|
|
|
TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
|
2012-12-19 05:05:13 +08:00
|
|
|
|
verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
|
|
|
|
|
" .looooooooooooooooooooooooooooooooooooooongFunction();");
|
2012-12-17 22:34:14 +08:00
|
|
|
|
|
2012-12-19 05:05:13 +08:00
|
|
|
|
verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
|
|
|
|
|
" ->looooooooooooooooooooooooooooooooooooooongFunction();");
|
2012-12-17 22:34:14 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
|
|
|
|
|
" Parameter2);");
|
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"ShortObject->shortFunction(\n"
|
|
|
|
|
" LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
|
|
|
|
|
" LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
|
|
|
|
|
|
|
|
|
|
verifyFormat("loooooooooooooongFunction(\n"
|
|
|
|
|
" LoooooooooooooongObject->looooooooooooooooongFunction());");
|
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"function(LoooooooooooooooooooooooooooooooooooongObject\n"
|
|
|
|
|
" ->loooooooooooooooooooooooooooooooooooooooongFunction());");
|
|
|
|
|
|
2013-02-14 22:26:07 +08:00
|
|
|
|
verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
|
|
|
|
|
" .WillRepeatedly(Return(SomeValue));");
|
|
|
|
|
verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
|
|
|
|
|
" .insert(ccccccccccccccccccccccc);");
|
2013-05-27 19:50:16 +08:00
|
|
|
|
verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaa);");
|
2013-08-13 14:50:04 +08:00
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());");
|
2013-03-15 22:57:30 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
|
2013-07-08 22:25:23 +08:00
|
|
|
|
" .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
|
2013-04-02 22:33:13 +08:00
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
|
|
|
|
|
"}");
|
2013-02-14 22:26:07 +08:00
|
|
|
|
|
2013-01-07 15:13:20 +08:00
|
|
|
|
// Here, it is not necessary to wrap at "." or "->".
|
2012-12-19 05:05:13 +08:00
|
|
|
|
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
" aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
|
2013-01-07 15:13:20 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaa->aaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
|
2013-02-14 23:01:34 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
|
2013-03-13 23:37:48 +08:00
|
|
|
|
verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
|
|
|
|
|
" aaaaaaaaa()->aaaaaa()->aaaaa());");
|
|
|
|
|
verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
|
|
|
|
|
" aaaaaaaaa()->aaaaaa()->aaaaa());");
|
2013-02-27 17:47:53 +08:00
|
|
|
|
|
2013-03-15 22:57:30 +08:00
|
|
|
|
// FIXME: Should we break before .a()?
|
|
|
|
|
verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
|
|
|
|
|
|
2013-02-27 17:47:53 +08:00
|
|
|
|
FormatStyle NoBinPacking = getLLVMStyle();
|
|
|
|
|
NoBinPacking.BinPackParameters = false;
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
|
|
|
|
|
NoBinPacking);
|
2013-07-09 19:57:27 +08:00
|
|
|
|
|
|
|
|
|
// If there is a subsequent call, change to hanging indentation.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
|
2012-12-17 22:34:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-02 23:08:56 +08:00
|
|
|
|
TEST_F(FormatTest, WrapsTemplateDeclarations) {
|
|
|
|
|
verifyFormat("template <typename T>\n"
|
|
|
|
|
"virtual void loooooooooooongFunction(int Param1, int Param2);");
|
2013-09-27 15:49:08 +08:00
|
|
|
|
verifyFormat("template <typename T>\n"
|
|
|
|
|
"// T should be one of {A, B}.\n"
|
|
|
|
|
"virtual void loooooooooooongFunction(int Param1, int Param2);");
|
2013-02-08 23:28:42 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"template <typename T>\n"
|
|
|
|
|
"using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
|
2013-02-18 19:59:17 +08:00
|
|
|
|
verifyFormat("template <typename T>\n"
|
|
|
|
|
"void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
|
|
|
|
|
" int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
|
2013-01-02 23:08:56 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"template <typename T>\n"
|
|
|
|
|
"void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
|
|
|
|
|
" int Paaaaaaaaaaaaaaaaaaaaram2);");
|
2013-01-03 02:30:06 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"template <typename T>\n"
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-01-09 17:50:48 +08:00
|
|
|
|
verifyFormat("template <typename T>\n"
|
|
|
|
|
"void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
2013-04-03 21:36:17 +08:00
|
|
|
|
" int aaaaaaaaaaaaaaaaaaaaaa);");
|
2013-01-09 18:40:23 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"template <typename T1, typename T2 = char, typename T3 = char,\n"
|
|
|
|
|
" typename T4 = char>\n"
|
|
|
|
|
"void f();");
|
2013-01-25 23:43:32 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-02-04 15:21:18 +08:00
|
|
|
|
|
2013-02-18 19:59:17 +08:00
|
|
|
|
verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
|
|
|
|
|
" a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
|
2013-05-29 20:07:31 +08:00
|
|
|
|
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
|
verifyFormat("template <typename T> class C {};");
|
2013-05-29 20:07:31 +08:00
|
|
|
|
verifyFormat("template <typename T> void f();");
|
|
|
|
|
verifyFormat("template <typename T> void f() {}");
|
Improve detection for preventing certain kind of formatting patterns.
This is a better implementation of r183097. The main purpose is to
prevent certain constructs to be formatted "like a block of text".
Before:
aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
After:
aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa =
new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(
bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =
(*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
llvm-svn: 185687
2013-07-05 17:14:35 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
|
|
|
|
|
" new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbb);",
|
|
|
|
|
getLLVMStyleWithColumns(72));
|
2013-09-14 16:13:22 +08:00
|
|
|
|
|
|
|
|
|
FormatStyle AlwaysBreak = getLLVMStyle();
|
|
|
|
|
AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
|
|
|
|
|
verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
|
|
|
|
|
verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
|
|
|
|
|
verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
|
|
|
|
|
verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
|
|
|
|
|
" ccccccccccccccccccccccccccccccccccccccccccccccc);");
|
|
|
|
|
verifyFormat("template <template <typename> class Fooooooo,\n"
|
|
|
|
|
" template <typename> class Baaaaaaar>\n"
|
|
|
|
|
"struct C {};",
|
|
|
|
|
AlwaysBreak);
|
2013-10-09 23:06:17 +08:00
|
|
|
|
verifyFormat("template <typename T> // T can be A, B or C.\n"
|
|
|
|
|
"struct C {};",
|
|
|
|
|
AlwaysBreak);
|
2013-01-02 23:08:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-25 18:57:27 +08:00
|
|
|
|
TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
|
|
|
|
|
|
2013-05-08 18:00:18 +08:00
|
|
|
|
// FIXME: Should we have the extra indent after the second break?
|
2013-01-25 18:57:27 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
|
2013-05-08 18:00:18 +08:00
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
|
2013-01-25 18:57:27 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
|
2013-01-28 15:43:15 +08:00
|
|
|
|
" cccccccccccccccccccccccccccccccccccccccccccccc());");
|
2013-01-25 18:57:27 +08:00
|
|
|
|
|
|
|
|
|
// Breaking at nested name specifiers is generally not desirable.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-01-28 20:45:14 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaa);",
|
|
|
|
|
getLLVMStyleWithColumns(74));
|
2013-04-02 22:33:13 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
|
2013-01-25 18:57:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsTemplateParameters) {
|
|
|
|
|
verifyFormat("A<int> a;");
|
|
|
|
|
verifyFormat("A<A<A<int> > > a;");
|
|
|
|
|
verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
|
|
|
|
|
verifyFormat("bool x = a < 1 || 2 > a;");
|
|
|
|
|
verifyFormat("bool x = 5 < f<int>();");
|
|
|
|
|
verifyFormat("bool x = f<int>() > 5;");
|
|
|
|
|
verifyFormat("bool x = 5 < a<int>::x;");
|
|
|
|
|
verifyFormat("bool x = a < 4 ? a > 2 : false;");
|
|
|
|
|
verifyFormat("bool x = f() ? a < 2 : a > 2;");
|
|
|
|
|
|
|
|
|
|
verifyGoogleFormat("A<A<int>> a;");
|
|
|
|
|
verifyGoogleFormat("A<A<A<int>>> a;");
|
|
|
|
|
verifyGoogleFormat("A<A<A<A<int>>>> a;");
|
2013-02-06 22:22:40 +08:00
|
|
|
|
verifyGoogleFormat("A<A<int> > a;");
|
|
|
|
|
verifyGoogleFormat("A<A<A<int> > > a;");
|
|
|
|
|
verifyGoogleFormat("A<A<A<A<int> > > > a;");
|
2013-10-12 13:16:06 +08:00
|
|
|
|
verifyGoogleFormat("A<::A<int>> a;");
|
|
|
|
|
verifyGoogleFormat("A<::A> a;");
|
|
|
|
|
verifyGoogleFormat("A< ::A> a;");
|
|
|
|
|
verifyGoogleFormat("A< ::A<int> > a;");
|
2013-02-06 22:22:40 +08:00
|
|
|
|
EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
|
|
|
|
|
EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
|
2013-10-12 13:16:06 +08:00
|
|
|
|
EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
|
|
|
|
|
EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("test >> a >> b;");
|
|
|
|
|
verifyFormat("test << a >> b;");
|
|
|
|
|
|
|
|
|
|
verifyFormat("f<int>();");
|
2013-01-10 21:24:24 +08:00
|
|
|
|
verifyFormat("template <typename T> void f() {}");
|
2013-06-02 02:56:00 +08:00
|
|
|
|
|
|
|
|
|
// Not template parameters.
|
|
|
|
|
verifyFormat("return a < b && c > d;");
|
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" while (a < b && c > d) {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}");
|
2013-07-31 06:37:19 +08:00
|
|
|
|
verifyFormat("template <typename... Types>\n"
|
|
|
|
|
"typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
|
2013-09-17 16:15:46 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
|
|
|
|
|
getLLVMStyleWithColumns(60));
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-28 17:21:10 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsBinaryOperators) {
|
|
|
|
|
verifyFormat("COMPARE(a, ==, b);");
|
2013-03-21 00:41:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, UnderstandsPointersToMembers) {
|
|
|
|
|
verifyFormat("int A::*x;");
|
2013-05-08 22:58:20 +08:00
|
|
|
|
verifyFormat("int (S::*func)(void *);");
|
2013-05-28 16:33:00 +08:00
|
|
|
|
verifyFormat("void f() { int (S::*func)(void *); }");
|
2013-05-08 23:06:58 +08:00
|
|
|
|
verifyFormat("typedef bool *(Class::*Member)() const;");
|
2013-05-08 22:58:20 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" (a->*f)();\n"
|
|
|
|
|
" a->*x;\n"
|
|
|
|
|
" (a.*f)();\n"
|
|
|
|
|
" ((*a).*f)();\n"
|
|
|
|
|
" a.*x;\n"
|
|
|
|
|
"}");
|
2013-07-18 22:46:07 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
|
|
|
|
|
" aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
|
|
|
|
|
"}");
|
2013-05-08 23:06:58 +08:00
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
|
|
|
|
Style.PointerBindsToType = true;
|
|
|
|
|
verifyFormat("typedef bool* (Class::*Member)() const;", Style);
|
2013-02-28 17:21:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-21 17:41:31 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsUnaryOperators) {
|
2012-12-04 02:12:45 +08:00
|
|
|
|
verifyFormat("int a = -2;");
|
2012-12-04 21:02:32 +08:00
|
|
|
|
verifyFormat("f(-1, -2, -3);");
|
|
|
|
|
verifyFormat("a[-1] = 5;");
|
|
|
|
|
verifyFormat("int a = 5 + -2;");
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("if (i == -1) {\n}");
|
|
|
|
|
verifyFormat("if (i != -1) {\n}");
|
|
|
|
|
verifyFormat("if (i > -1) {\n}");
|
|
|
|
|
verifyFormat("if (i < -1) {\n}");
|
2012-12-06 21:16:39 +08:00
|
|
|
|
verifyFormat("++(a->f());");
|
|
|
|
|
verifyFormat("--(a->f());");
|
2013-01-14 20:18:19 +08:00
|
|
|
|
verifyFormat("(a->f())++;");
|
|
|
|
|
verifyFormat("a[42]++;");
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("if (!(a->f())) {\n}");
|
2012-12-21 17:41:31 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("a-- > b;");
|
|
|
|
|
verifyFormat("b ? -a : c;");
|
|
|
|
|
verifyFormat("n * sizeof char16;");
|
2013-06-28 20:51:24 +08:00
|
|
|
|
verifyFormat("n * alignof char16;", getGoogleStyle());
|
2012-12-21 17:41:31 +08:00
|
|
|
|
verifyFormat("sizeof(char);");
|
2013-06-28 20:51:24 +08:00
|
|
|
|
verifyFormat("alignof(char);", getGoogleStyle());
|
2013-01-02 23:26:16 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("return -1;");
|
|
|
|
|
verifyFormat("switch (a) {\n"
|
|
|
|
|
"case -1:\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}");
|
2013-03-22 18:44:43 +08:00
|
|
|
|
verifyFormat("#define X -1");
|
|
|
|
|
verifyFormat("#define X -kConstant");
|
2013-01-12 13:41:23 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
|
|
|
|
|
verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
|
2013-01-15 22:27:39 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("int a = /* confusing comment */ -1;");
|
|
|
|
|
// FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
|
|
|
|
|
verifyFormat("int a = i /* confusing comment */++;");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-21 16:39:01 +08:00
|
|
|
|
TEST_F(FormatTest, IndentsRelativeToUnaryOperators) {
|
|
|
|
|
verifyFormat("if (!aaaaaaaaaa( // break\n"
|
|
|
|
|
" aaaaa)) {\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
|
|
|
|
|
" aaaaa));");
|
2013-08-23 23:14:03 +08:00
|
|
|
|
|
|
|
|
|
// Only indent relative to unary operators if the expression is nested.
|
|
|
|
|
verifyFormat("*aaa = aaaaaaa( // break\n"
|
|
|
|
|
" bbbbbb);");
|
2013-08-21 16:39:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-26 16:10:17 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsOverloadedOperators) {
|
2012-12-24 18:56:04 +08:00
|
|
|
|
verifyFormat("bool operator<();");
|
|
|
|
|
verifyFormat("bool operator>();");
|
|
|
|
|
verifyFormat("bool operator=();");
|
|
|
|
|
verifyFormat("bool operator==();");
|
|
|
|
|
verifyFormat("bool operator!=();");
|
|
|
|
|
verifyFormat("int operator+();");
|
|
|
|
|
verifyFormat("int operator++();");
|
|
|
|
|
verifyFormat("bool operator();");
|
|
|
|
|
verifyFormat("bool operator()();");
|
|
|
|
|
verifyFormat("bool operator[]();");
|
|
|
|
|
verifyFormat("operator bool();");
|
2013-02-11 16:01:18 +08:00
|
|
|
|
verifyFormat("operator int();");
|
|
|
|
|
verifyFormat("operator void *();");
|
2012-12-24 18:56:04 +08:00
|
|
|
|
verifyFormat("operator SomeType<int>();");
|
2013-02-11 16:01:18 +08:00
|
|
|
|
verifyFormat("operator SomeType<int, int>();");
|
|
|
|
|
verifyFormat("operator SomeType<SomeType<int> >();");
|
2012-12-24 18:56:04 +08:00
|
|
|
|
verifyFormat("void *operator new(std::size_t size);");
|
|
|
|
|
verifyFormat("void *operator new[](std::size_t size);");
|
|
|
|
|
verifyFormat("void operator delete(void *ptr);");
|
|
|
|
|
verifyFormat("void operator delete[](void *ptr);");
|
2013-05-10 15:59:58 +08:00
|
|
|
|
verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
|
|
|
|
|
"AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
|
2013-02-11 16:01:18 +08:00
|
|
|
|
|
2013-02-16 03:24:08 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"ostream &operator<<(ostream &OutputStream,\n"
|
|
|
|
|
" SomeReallyLongType WithSomeReallyLongValue);");
|
2013-04-06 01:21:59 +08:00
|
|
|
|
verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
|
|
|
|
|
" const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
|
|
|
|
|
" return left.group < right.group;\n"
|
|
|
|
|
"}");
|
2013-04-11 16:48:20 +08:00
|
|
|
|
verifyFormat("SomeType &operator=(const SomeType &S);");
|
2013-02-16 03:24:08 +08:00
|
|
|
|
|
2013-02-11 16:01:18 +08:00
|
|
|
|
verifyGoogleFormat("operator void*();");
|
|
|
|
|
verifyGoogleFormat("operator SomeType<SomeType<int>>();");
|
2013-10-29 20:24:23 +08:00
|
|
|
|
verifyGoogleFormat("operator ::A();");
|
2013-09-02 17:20:39 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("using A::operator+;");
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-12 00:09:04 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsNewAndDelete) {
|
2013-02-23 16:07:18 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" A *a = new A;\n"
|
|
|
|
|
" A *a = new (placement) A;\n"
|
|
|
|
|
" delete a;\n"
|
|
|
|
|
" delete (A *)a;\n"
|
|
|
|
|
"}");
|
2013-01-12 00:09:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-02 16:57:10 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
|
2013-01-10 21:24:24 +08:00
|
|
|
|
verifyFormat("int *f(int *a) {}");
|
|
|
|
|
verifyFormat("int main(int argc, char **argv) {}");
|
2013-02-18 20:44:35 +08:00
|
|
|
|
verifyFormat("Test::Test(int b) : a(b * b) {}");
|
2013-01-23 20:10:53 +08:00
|
|
|
|
verifyIndependentOfContext("f(a, *a);");
|
2013-03-02 01:13:29 +08:00
|
|
|
|
verifyFormat("void g() { f(*a); }");
|
2013-01-23 20:10:53 +08:00
|
|
|
|
verifyIndependentOfContext("int a = b * 10;");
|
|
|
|
|
verifyIndependentOfContext("int a = 10 * b;");
|
|
|
|
|
verifyIndependentOfContext("int a = b * c;");
|
|
|
|
|
verifyIndependentOfContext("int a += b * c;");
|
|
|
|
|
verifyIndependentOfContext("int a -= b * c;");
|
|
|
|
|
verifyIndependentOfContext("int a *= b * c;");
|
|
|
|
|
verifyIndependentOfContext("int a /= b * c;");
|
|
|
|
|
verifyIndependentOfContext("int a = *b;");
|
|
|
|
|
verifyIndependentOfContext("int a = *b * c;");
|
|
|
|
|
verifyIndependentOfContext("int a = b * *c;");
|
|
|
|
|
verifyIndependentOfContext("return 10 * b;");
|
|
|
|
|
verifyIndependentOfContext("return *b * *c;");
|
|
|
|
|
verifyIndependentOfContext("return a & ~b;");
|
|
|
|
|
verifyIndependentOfContext("f(b ? *c : *d);");
|
|
|
|
|
verifyIndependentOfContext("int a = b ? *c : *d;");
|
|
|
|
|
verifyIndependentOfContext("*b = a;");
|
|
|
|
|
verifyIndependentOfContext("a * ~b;");
|
|
|
|
|
verifyIndependentOfContext("a * !b;");
|
|
|
|
|
verifyIndependentOfContext("a * +b;");
|
|
|
|
|
verifyIndependentOfContext("a * -b;");
|
|
|
|
|
verifyIndependentOfContext("a * ++b;");
|
|
|
|
|
verifyIndependentOfContext("a * --b;");
|
|
|
|
|
verifyIndependentOfContext("a[4] * b;");
|
2013-02-27 19:43:50 +08:00
|
|
|
|
verifyIndependentOfContext("a[a * a] = 1;");
|
2013-01-23 20:10:53 +08:00
|
|
|
|
verifyIndependentOfContext("f() * b;");
|
|
|
|
|
verifyIndependentOfContext("a * [self dostuff];");
|
2013-04-02 01:13:26 +08:00
|
|
|
|
verifyIndependentOfContext("int x = a * (a + b);");
|
2013-01-23 20:10:53 +08:00
|
|
|
|
verifyIndependentOfContext("(a *)(a + b);");
|
|
|
|
|
verifyIndependentOfContext("int *pa = (int *)&a;");
|
Formatter: Correctly format stars in `sizeof(int**)` and similar places.
This redoes how '*' and '&' are classified as pointer / reference markers when
followed by ')', '>', or ','.
Previously, determineStarAmpUsage() marked a single '*' and '&' followed by
')', '>', or ',' as pointer or reference marker. Now, all '*'s and '&'s
preceding ')', '>', or ',' are marked as pointer / reference markers. Fixes
PR14884.
Since only the last '*' in 'int ***' was marked as pointer before (the rest
were unary operators, which don't reach spaceRequiredBetween()),
spaceRequiredBetween() now had to be thought about handing multiple '*'s in
sequence.
Before:
return sizeof(int * *);
Type **A = static_cast<Type * *>(P);
Now:
return sizeof(int**);
Type **A = static_cast<Type **>(P);
While here, also make all methods of AnnotatingParser except parseLine()
private.
Review URL: http://llvm-reviews.chandlerc.com/D384
llvm-svn: 174975
2013-02-13 00:17:07 +08:00
|
|
|
|
verifyIndependentOfContext("return sizeof(int **);");
|
|
|
|
|
verifyIndependentOfContext("return sizeof(int ******);");
|
|
|
|
|
verifyIndependentOfContext("return (int **&)a;");
|
2013-05-28 23:27:10 +08:00
|
|
|
|
verifyIndependentOfContext("f((*PointerToArray)[10]);");
|
2013-03-02 01:13:29 +08:00
|
|
|
|
verifyFormat("void f(Type (*parameter)[10]) {}");
|
Formatter: Correctly format stars in `sizeof(int**)` and similar places.
This redoes how '*' and '&' are classified as pointer / reference markers when
followed by ')', '>', or ','.
Previously, determineStarAmpUsage() marked a single '*' and '&' followed by
')', '>', or ',' as pointer or reference marker. Now, all '*'s and '&'s
preceding ')', '>', or ',' are marked as pointer / reference markers. Fixes
PR14884.
Since only the last '*' in 'int ***' was marked as pointer before (the rest
were unary operators, which don't reach spaceRequiredBetween()),
spaceRequiredBetween() now had to be thought about handing multiple '*'s in
sequence.
Before:
return sizeof(int * *);
Type **A = static_cast<Type * *>(P);
Now:
return sizeof(int**);
Type **A = static_cast<Type **>(P);
While here, also make all methods of AnnotatingParser except parseLine()
private.
Review URL: http://llvm-reviews.chandlerc.com/D384
llvm-svn: 174975
2013-02-13 00:17:07 +08:00
|
|
|
|
verifyGoogleFormat("return sizeof(int**);");
|
|
|
|
|
verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
|
|
|
|
|
verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
|
2013-09-05 17:29:45 +08:00
|
|
|
|
verifyFormat("auto a = [](int **&, int ***) {};");
|
2013-10-21 02:15:30 +08:00
|
|
|
|
verifyFormat("auto PointerBinding = [](const char *S) {};");
|
2013-09-10 18:26:38 +08:00
|
|
|
|
verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
|
2013-01-23 20:10:53 +08:00
|
|
|
|
|
|
|
|
|
verifyIndependentOfContext("InvalidRegions[*R] = 0;");
|
|
|
|
|
|
|
|
|
|
verifyIndependentOfContext("A<int *> a;");
|
|
|
|
|
verifyIndependentOfContext("A<int **> a;");
|
|
|
|
|
verifyIndependentOfContext("A<int *, int *> a;");
|
2013-02-06 18:05:46 +08:00
|
|
|
|
verifyIndependentOfContext(
|
|
|
|
|
"const char *const p = reinterpret_cast<const char *const>(q);");
|
2013-01-23 20:10:53 +08:00
|
|
|
|
verifyIndependentOfContext("A<int **, int **> a;");
|
2013-02-06 18:57:42 +08:00
|
|
|
|
verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
|
2013-03-14 18:50:25 +08:00
|
|
|
|
verifyFormat("for (char **a = b; *a; ++a) {\n}");
|
2013-05-03 22:41:24 +08:00
|
|
|
|
verifyFormat("for (; a && b;) {\n}");
|
2012-12-11 02:59:13 +08:00
|
|
|
|
|
2013-01-09 04:03:18 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
|
|
|
|
|
|
2013-01-10 21:24:24 +08:00
|
|
|
|
verifyGoogleFormat("int main(int argc, char** argv) {}");
|
2012-12-11 02:59:13 +08:00
|
|
|
|
verifyGoogleFormat("A<int*> a;");
|
|
|
|
|
verifyGoogleFormat("A<int**> a;");
|
|
|
|
|
verifyGoogleFormat("A<int*, int*> a;");
|
|
|
|
|
verifyGoogleFormat("A<int**, int**> a;");
|
2013-01-02 16:57:10 +08:00
|
|
|
|
verifyGoogleFormat("f(b ? *c : *d);");
|
|
|
|
|
verifyGoogleFormat("int a = b ? *c : *d;");
|
2013-01-17 00:04:06 +08:00
|
|
|
|
verifyGoogleFormat("Type* t = **x;");
|
|
|
|
|
verifyGoogleFormat("Type* t = *++*x;");
|
|
|
|
|
verifyGoogleFormat("*++*x;");
|
|
|
|
|
verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
|
|
|
|
|
verifyGoogleFormat("Type* t = x++ * y;");
|
2013-02-06 18:05:46 +08:00
|
|
|
|
verifyGoogleFormat(
|
|
|
|
|
"const char* const p = reinterpret_cast<const char* const>(q);");
|
2013-01-14 18:58:01 +08:00
|
|
|
|
|
2013-01-23 20:10:53 +08:00
|
|
|
|
verifyIndependentOfContext("a = *(x + y);");
|
|
|
|
|
verifyIndependentOfContext("a = &(x + y);");
|
|
|
|
|
verifyIndependentOfContext("*(x + y).call();");
|
|
|
|
|
verifyIndependentOfContext("&(x + y)->call();");
|
2013-03-02 01:13:29 +08:00
|
|
|
|
verifyFormat("void f() { &(*I).first; }");
|
2013-01-15 22:27:39 +08:00
|
|
|
|
|
2013-01-23 20:10:53 +08:00
|
|
|
|
verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
|
2013-01-15 22:27:39 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"int *MyValues = {\n"
|
|
|
|
|
" *A, // Operator detection might be confused by the '{'\n"
|
|
|
|
|
" *BB // Operator detection might be confused by previous comment\n"
|
|
|
|
|
"};");
|
2013-01-18 01:17:19 +08:00
|
|
|
|
|
2013-01-23 20:10:53 +08:00
|
|
|
|
verifyIndependentOfContext("if (int *a = &b)");
|
|
|
|
|
verifyIndependentOfContext("if (int &a = *b)");
|
|
|
|
|
verifyIndependentOfContext("if (a & b[i])");
|
|
|
|
|
verifyIndependentOfContext("if (a::b::c::d & b[i])");
|
|
|
|
|
verifyIndependentOfContext("if (*b[i])");
|
|
|
|
|
verifyIndependentOfContext("if (int *a = (&b))");
|
|
|
|
|
verifyIndependentOfContext("while (int *a = &b)");
|
2013-09-22 01:31:51 +08:00
|
|
|
|
verifyIndependentOfContext("size = sizeof *a;");
|
2013-01-23 20:58:14 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" for (const int &v : Values) {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}");
|
2013-02-18 20:44:35 +08:00
|
|
|
|
verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
|
|
|
|
|
verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
|
2013-09-12 04:37:10 +08:00
|
|
|
|
verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
|
2013-01-22 19:46:26 +08:00
|
|
|
|
|
2013-05-13 15:14:40 +08:00
|
|
|
|
verifyFormat("#define MACRO \\\n"
|
|
|
|
|
" int *i = a * b; \\\n"
|
|
|
|
|
" void f(a *b);",
|
|
|
|
|
getLLVMStyleWithColumns(19));
|
|
|
|
|
|
2013-03-13 15:49:51 +08:00
|
|
|
|
verifyIndependentOfContext("A = new SomeType *[Length];");
|
2013-01-23 20:10:53 +08:00
|
|
|
|
verifyIndependentOfContext("A = new SomeType *[Length]();");
|
2013-07-05 21:30:40 +08:00
|
|
|
|
verifyIndependentOfContext("T **t = new T *;");
|
|
|
|
|
verifyIndependentOfContext("T **t = new T *();");
|
2013-01-22 19:46:26 +08:00
|
|
|
|
verifyGoogleFormat("A = new SomeType* [Length]();");
|
2013-03-13 15:49:51 +08:00
|
|
|
|
verifyGoogleFormat("A = new SomeType* [Length];");
|
2013-07-05 21:30:40 +08:00
|
|
|
|
verifyGoogleFormat("T** t = new T*;");
|
|
|
|
|
verifyGoogleFormat("T** t = new T*();");
|
2013-05-13 15:14:40 +08:00
|
|
|
|
|
2013-05-07 22:17:18 +08:00
|
|
|
|
FormatStyle PointerLeft = getLLVMStyle();
|
|
|
|
|
PointerLeft.PointerBindsToType = true;
|
|
|
|
|
verifyFormat("delete *x;", PointerLeft);
|
2013-03-02 01:13:29 +08:00
|
|
|
|
}
|
2013-02-06 22:22:40 +08:00
|
|
|
|
|
2013-07-18 04:25:02 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsAttributes) {
|
|
|
|
|
verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-06 14:35:44 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsEllipsis) {
|
|
|
|
|
verifyFormat("int printf(const char *fmt, ...);");
|
|
|
|
|
verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
|
2013-07-01 17:47:25 +08:00
|
|
|
|
verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
|
|
|
|
|
|
|
|
|
|
FormatStyle PointersLeft = getLLVMStyle();
|
|
|
|
|
PointersLeft.PointerBindsToType = true;
|
|
|
|
|
verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
|
2013-05-06 14:35:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-02 01:13:29 +08:00
|
|
|
|
TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
|
2013-02-06 22:22:40 +08:00
|
|
|
|
EXPECT_EQ("int *a;\n"
|
|
|
|
|
"int *a;\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"int *a;",
|
|
|
|
|
format("int *a;\n"
|
|
|
|
|
"int* a;\n"
|
|
|
|
|
"int *a;",
|
|
|
|
|
getGoogleStyle()));
|
2013-02-06 22:22:40 +08:00
|
|
|
|
EXPECT_EQ("int* a;\n"
|
|
|
|
|
"int* a;\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"int* a;",
|
|
|
|
|
format("int* a;\n"
|
|
|
|
|
"int* a;\n"
|
|
|
|
|
"int *a;",
|
|
|
|
|
getGoogleStyle()));
|
2013-02-06 22:22:40 +08:00
|
|
|
|
EXPECT_EQ("int *a;\n"
|
|
|
|
|
"int *a;\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"int *a;",
|
|
|
|
|
format("int *a;\n"
|
|
|
|
|
"int * a;\n"
|
|
|
|
|
"int * a;",
|
|
|
|
|
getGoogleStyle()));
|
2013-01-23 22:08:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 00:28:18 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsRvalueReferences) {
|
|
|
|
|
verifyFormat("int f(int &&a) {}");
|
|
|
|
|
verifyFormat("int f(int a, char &&b) {}");
|
|
|
|
|
verifyFormat("void f() { int &&a = b; }");
|
|
|
|
|
verifyGoogleFormat("int f(int a, char&& b) {}");
|
|
|
|
|
verifyGoogleFormat("void f() { int&& a = b; }");
|
|
|
|
|
|
2013-05-28 00:36:33 +08:00
|
|
|
|
verifyIndependentOfContext("A<int &&> a;");
|
|
|
|
|
verifyIndependentOfContext("A<int &&, int &&> a;");
|
|
|
|
|
verifyGoogleFormat("A<int&&> a;");
|
|
|
|
|
verifyGoogleFormat("A<int&&, int&&> a;");
|
2013-08-02 01:58:23 +08:00
|
|
|
|
|
|
|
|
|
// Not rvalue references:
|
|
|
|
|
verifyFormat("template <bool B, bool C> class A {\n"
|
|
|
|
|
" static_assert(B && C, \"Something is wrong\");\n"
|
|
|
|
|
"};");
|
2013-08-13 17:09:09 +08:00
|
|
|
|
verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
|
|
|
|
|
verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
|
2013-03-13 00:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-23 22:08:21 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
|
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" x[aaaaaaaaa -\n"
|
2013-04-09 04:33:42 +08:00
|
|
|
|
" b] = 23;\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
getLLVMStyleWithColumns(15));
|
2012-12-04 02:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-13 16:01:36 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsCasts) {
|
|
|
|
|
verifyFormat("Type *A = static_cast<Type *>(P);");
|
|
|
|
|
verifyFormat("Type *A = (Type *)P;");
|
|
|
|
|
verifyFormat("Type *A = (vector<Type *, int *>)P;");
|
|
|
|
|
verifyFormat("int a = (int)(2.0f);");
|
2013-06-01 00:14:28 +08:00
|
|
|
|
verifyFormat("int a = (int)2.0f;");
|
|
|
|
|
verifyFormat("x[(int32)y];");
|
|
|
|
|
verifyFormat("x = (int32)y;");
|
|
|
|
|
verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
|
|
|
|
|
verifyFormat("int a = (int)*b;");
|
|
|
|
|
verifyFormat("int a = (int)2.0f;");
|
|
|
|
|
verifyFormat("int a = (int)~0;");
|
|
|
|
|
verifyFormat("int a = (int)++a;");
|
|
|
|
|
verifyFormat("int a = (int)sizeof(int);");
|
|
|
|
|
verifyFormat("int a = (int)+2;");
|
|
|
|
|
verifyFormat("my_int a = (my_int)2.0f;");
|
|
|
|
|
verifyFormat("my_int a = (my_int)sizeof(int);");
|
2013-06-06 16:20:20 +08:00
|
|
|
|
verifyFormat("return (my_int)aaa;");
|
2013-07-15 23:04:42 +08:00
|
|
|
|
verifyFormat("#define x ((int)-1)");
|
|
|
|
|
verifyFormat("#define p(q) ((int *)&q)");
|
2013-06-01 00:14:28 +08:00
|
|
|
|
|
|
|
|
|
// FIXME: Without type knowledge, this can still fall apart miserably.
|
|
|
|
|
verifyFormat("void f() { my_int a = (my_int) * b; }");
|
2013-06-08 00:02:52 +08:00
|
|
|
|
verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }");
|
2013-06-01 00:14:28 +08:00
|
|
|
|
verifyFormat("my_int a = (my_int) ~0;");
|
|
|
|
|
verifyFormat("my_int a = (my_int)++ a;");
|
|
|
|
|
verifyFormat("my_int a = (my_int) + 2;");
|
2013-01-13 16:01:36 +08:00
|
|
|
|
|
2013-07-18 22:46:07 +08:00
|
|
|
|
// Don't break after a cast's
|
|
|
|
|
verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
|
|
|
|
" (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbb);");
|
|
|
|
|
|
2013-01-13 16:01:36 +08:00
|
|
|
|
// These are not casts.
|
|
|
|
|
verifyFormat("void f(int *) {}");
|
2013-02-13 12:32:57 +08:00
|
|
|
|
verifyFormat("f(foo)->b;");
|
|
|
|
|
verifyFormat("f(foo).b;");
|
|
|
|
|
verifyFormat("f(foo)(b);");
|
|
|
|
|
verifyFormat("f(foo)[b];");
|
2013-09-05 17:29:45 +08:00
|
|
|
|
verifyFormat("[](foo) { return 4; }(bar);");
|
2013-02-13 12:32:57 +08:00
|
|
|
|
verifyFormat("(*funptr)(foo)[4];");
|
|
|
|
|
verifyFormat("funptrs[4](foo)[4];");
|
2013-01-13 16:01:36 +08:00
|
|
|
|
verifyFormat("void f(int *);");
|
|
|
|
|
verifyFormat("void f(int *) = 0;");
|
|
|
|
|
verifyFormat("void f(SmallVector<int>) {}");
|
|
|
|
|
verifyFormat("void f(SmallVector<int>);");
|
|
|
|
|
verifyFormat("void f(SmallVector<int>) = 0;");
|
2013-02-10 02:02:07 +08:00
|
|
|
|
verifyFormat("void f(int i = (kValue) * kMask) {}");
|
|
|
|
|
verifyFormat("void f(int i = (kA * kB) & kMask) {}");
|
2013-02-13 12:32:57 +08:00
|
|
|
|
verifyFormat("int a = sizeof(int) * b;");
|
2013-06-28 20:51:24 +08:00
|
|
|
|
verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
|
2013-06-06 16:20:20 +08:00
|
|
|
|
verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
|
|
|
|
|
verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
|
2013-07-08 22:58:01 +08:00
|
|
|
|
verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
|
2013-02-27 17:47:53 +08:00
|
|
|
|
|
2013-02-23 16:07:18 +08:00
|
|
|
|
// These are not casts, but at some point were confused with casts.
|
|
|
|
|
verifyFormat("virtual void foo(int *) override;");
|
|
|
|
|
verifyFormat("virtual void foo(char &) const;");
|
|
|
|
|
verifyFormat("virtual void foo(int *a, char *) const;");
|
2013-03-14 01:13:53 +08:00
|
|
|
|
verifyFormat("int a = sizeof(int *) + b;");
|
2013-06-28 20:51:24 +08:00
|
|
|
|
verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
|
2013-07-05 15:58:34 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
|
2013-07-12 19:19:37 +08:00
|
|
|
|
// FIXME: The indentation here is not ideal.
|
Improve detection for preventing certain kind of formatting patterns.
This is a better implementation of r183097. The main purpose is to
prevent certain constructs to be formatted "like a block of text".
Before:
aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
After:
aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa =
new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(
bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =
(*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
llvm-svn: 185687
2013-07-05 17:14:35 +08:00
|
|
|
|
verifyFormat(
|
2013-07-12 19:19:37 +08:00
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
|
|
|
|
|
" [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
|
2013-01-13 16:01:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 21:08:12 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsFunctionTypes) {
|
|
|
|
|
verifyFormat("A<bool()> a;");
|
|
|
|
|
verifyFormat("A<SomeType()> a;");
|
2013-05-28 16:33:00 +08:00
|
|
|
|
verifyFormat("A<void (*)(int, std::string)> a;");
|
2013-03-20 17:53:18 +08:00
|
|
|
|
verifyFormat("A<void *(int)>;");
|
2013-04-02 01:13:26 +08:00
|
|
|
|
verifyFormat("void *(*a)(int *, SomeType *);");
|
2013-03-02 01:13:29 +08:00
|
|
|
|
verifyFormat("int (*func)(void *);");
|
2013-05-28 16:33:00 +08:00
|
|
|
|
verifyFormat("void f() { int (*func)(void *); }");
|
2013-08-12 20:16:34 +08:00
|
|
|
|
verifyFormat("template <class CallbackClass>\n"
|
|
|
|
|
"using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
|
2013-03-20 17:53:18 +08:00
|
|
|
|
|
2013-04-02 01:13:26 +08:00
|
|
|
|
verifyGoogleFormat("A<void*(int*, SomeType*)>;");
|
|
|
|
|
verifyGoogleFormat("void* (*a)(int);");
|
2013-08-12 20:16:34 +08:00
|
|
|
|
verifyGoogleFormat(
|
|
|
|
|
"template <class CallbackClass>\n"
|
|
|
|
|
"using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
|
2013-05-14 16:34:47 +08:00
|
|
|
|
|
2013-05-15 15:51:51 +08:00
|
|
|
|
// Other constructs can look somewhat like function types:
|
2013-05-14 16:34:47 +08:00
|
|
|
|
verifyFormat("A<sizeof(*x)> a;");
|
2013-05-15 15:51:51 +08:00
|
|
|
|
verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
|
2013-07-16 19:37:21 +08:00
|
|
|
|
verifyFormat("some_var = function(*some_pointer_var)[0];");
|
|
|
|
|
verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
|
2013-01-10 21:08:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.
Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
2013-02-25 02:54:32 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksLongDeclarations) {
|
2013-05-06 16:27:33 +08:00
|
|
|
|
verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
|
2013-06-22 01:25:42 +08:00
|
|
|
|
" AnotherNameForTheLongType;",
|
|
|
|
|
getGoogleStyle());
|
2013-05-06 16:27:33 +08:00
|
|
|
|
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
|
2013-06-22 01:25:42 +08:00
|
|
|
|
" LoooooooooooooooooooooooooooooooooooooooongVariable;",
|
|
|
|
|
getGoogleStyle());
|
2013-07-02 17:47:29 +08:00
|
|
|
|
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
|
|
|
|
|
" LoooooooooooooooooooooooooooooooooooooooongVariable;",
|
|
|
|
|
getGoogleStyle());
|
2013-05-06 16:27:33 +08:00
|
|
|
|
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
|
2013-06-22 01:25:42 +08:00
|
|
|
|
" LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
|
|
|
|
|
getGoogleStyle());
|
2013-05-06 16:27:33 +08:00
|
|
|
|
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
|
|
|
|
|
"LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
|
2013-07-02 17:47:29 +08:00
|
|
|
|
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
|
|
|
|
|
"LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
|
2013-05-06 16:27:33 +08:00
|
|
|
|
|
|
|
|
|
// FIXME: Without the comment, this breaks after "(".
|
2013-06-22 01:25:42 +08:00
|
|
|
|
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
|
|
|
|
|
" (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
|
|
|
|
|
getGoogleStyle());
|
2013-05-06 16:27:33 +08:00
|
|
|
|
|
2013-01-28 23:16:31 +08:00
|
|
|
|
verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" int LoooooooooooooooooooongParam2) {}");
|
2013-01-02 16:44:14 +08:00
|
|
|
|
verifyFormat(
|
2013-04-11 22:29:13 +08:00
|
|
|
|
"TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
|
|
|
|
|
" SourceLocation L, IdentifierIn *II,\n"
|
|
|
|
|
" Type *T) {}");
|
Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.
Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
2013-02-25 02:54:32 +08:00
|
|
|
|
verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
|
|
|
|
|
"ReallyReallyLongFunctionName(\n"
|
|
|
|
|
" const std::string &SomeParameter,\n"
|
|
|
|
|
" const SomeType<string, SomeOtherTemplateParameter> &\n"
|
|
|
|
|
" ReallyReallyLongParameterName,\n"
|
|
|
|
|
" const SomeType<string, SomeOtherTemplateParameter> &\n"
|
|
|
|
|
" AnotherLongParameterName) {}");
|
2013-05-15 17:35:08 +08:00
|
|
|
|
verifyFormat("template <typename A>\n"
|
|
|
|
|
"SomeLoooooooooooooooooooooongType<\n"
|
|
|
|
|
" typename some_namespace::SomeOtherType<A>::Type>\n"
|
|
|
|
|
"Function() {}");
|
Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.
Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
2013-02-25 02:54:32 +08:00
|
|
|
|
|
2013-08-19 18:16:18 +08:00
|
|
|
|
verifyGoogleFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaa;");
|
2013-01-28 23:40:20 +08:00
|
|
|
|
verifyGoogleFormat(
|
2013-02-27 17:47:53 +08:00
|
|
|
|
"TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
|
|
|
|
|
" SourceLocation L) {}");
|
2013-02-14 04:33:44 +08:00
|
|
|
|
verifyGoogleFormat(
|
|
|
|
|
"some_namespace::LongReturnType\n"
|
|
|
|
|
"long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
" int first_long_parameter, int second_parameter) {}");
|
2013-02-14 04:33:44 +08:00
|
|
|
|
|
|
|
|
|
verifyGoogleFormat("template <typename T>\n"
|
|
|
|
|
"aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
|
2013-02-22 05:33:55 +08:00
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
|
2013-02-28 18:06:05 +08:00
|
|
|
|
verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" int aaaaaaaaaaaaaaaaaaaaaaa);");
|
2013-01-02 16:44:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-12 19:19:37 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsArrays) {
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
|
|
|
|
|
" [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
|
|
|
|
|
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
" [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
|
|
|
|
|
" [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"llvm::outs() << \"aaaaaaaaaaaa: \"\n"
|
|
|
|
|
" << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
|
|
|
|
|
" [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-06 17:56:08 +08:00
|
|
|
|
TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
|
|
|
|
|
verifyFormat("(a)->b();");
|
|
|
|
|
verifyFormat("--a;");
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-04 21:02:32 +08:00
|
|
|
|
TEST_F(FormatTest, HandlesIncludeDirectives) {
|
2013-01-14 23:52:06 +08:00
|
|
|
|
verifyFormat("#include <string>\n"
|
|
|
|
|
"#include <a/b/c.h>\n"
|
|
|
|
|
"#include \"a/b/string\"\n"
|
|
|
|
|
"#include \"string.h\"\n"
|
|
|
|
|
"#include \"string.h\"\n"
|
2013-01-15 23:50:27 +08:00
|
|
|
|
"#include <a-a>\n"
|
2013-02-23 15:46:38 +08:00
|
|
|
|
"#include < path with space >\n"
|
2013-05-06 18:24:51 +08:00
|
|
|
|
"#include \"abc.h\" // this is included for ABC\n"
|
2013-05-16 20:59:13 +08:00
|
|
|
|
"#include \"some long include\" // with a comment\n"
|
2013-02-23 15:46:38 +08:00
|
|
|
|
"#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
|
|
|
|
|
getLLVMStyleWithColumns(35));
|
2013-10-30 21:54:53 +08:00
|
|
|
|
EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
|
|
|
|
|
EXPECT_EQ("#include <a>", format("#include<a>"));
|
2013-01-13 16:12:18 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("#import <string>");
|
|
|
|
|
verifyFormat("#import <a/b/c.h>");
|
|
|
|
|
verifyFormat("#import \"a/b/string\"");
|
|
|
|
|
verifyFormat("#import \"string.h\"");
|
|
|
|
|
verifyFormat("#import \"string.h\"");
|
2013-08-28 16:24:04 +08:00
|
|
|
|
verifyFormat("#if __has_include(<strstream>)\n"
|
|
|
|
|
"#include <strstream>\n"
|
|
|
|
|
"#endif");
|
2012-12-04 21:02:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Error recovery tests.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2013-02-14 17:19:04 +08:00
|
|
|
|
TEST_F(FormatTest, IncompleteParameterLists) {
|
2013-02-27 17:47:53 +08:00
|
|
|
|
FormatStyle NoBinPacking = getLLVMStyle();
|
|
|
|
|
NoBinPacking.BinPackParameters = false;
|
|
|
|
|
verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
|
|
|
|
|
" double *min_x,\n"
|
|
|
|
|
" double *max_x,\n"
|
|
|
|
|
" double *min_y,\n"
|
|
|
|
|
" double *max_y,\n"
|
|
|
|
|
" double *min_z,\n"
|
|
|
|
|
" double *max_z, ) {}",
|
|
|
|
|
NoBinPacking);
|
2013-02-14 17:19:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 17:26:47 +08:00
|
|
|
|
TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
|
2013-01-16 19:45:16 +08:00
|
|
|
|
verifyFormat("void f() { return; }\n42");
|
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" if (0)\n"
|
|
|
|
|
" return;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"42");
|
2013-01-16 19:43:46 +08:00
|
|
|
|
verifyFormat("void f() { return }\n42");
|
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" if (0)\n"
|
|
|
|
|
" return\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"42");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
|
|
|
|
|
EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
|
|
|
|
|
EXPECT_EQ("void f() {\n"
|
|
|
|
|
" if (a)\n"
|
|
|
|
|
" return\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("void f ( ) { if ( a ) return }"));
|
2013-05-15 22:09:55 +08:00
|
|
|
|
EXPECT_EQ("namespace N {\n"
|
|
|
|
|
"void f()\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("namespace N { void f() }"));
|
2013-01-16 19:43:46 +08:00
|
|
|
|
EXPECT_EQ("namespace N {\n"
|
|
|
|
|
"void f() {}\n"
|
|
|
|
|
"void g()\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("namespace N { void f( ) { } void g( ) }"));
|
2013-01-10 17:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 18:16:05 +08:00
|
|
|
|
TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
|
|
|
|
|
verifyFormat("int aaaaaaaa =\n"
|
2013-03-27 19:52:18 +08:00
|
|
|
|
" // Overlylongcomment\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" b;",
|
|
|
|
|
getLLVMStyleWithColumns(20));
|
2013-01-09 18:16:05 +08:00
|
|
|
|
verifyFormat("function(\n"
|
|
|
|
|
" ShortArgument,\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" LoooooooooooongArgument);\n",
|
|
|
|
|
getLLVMStyleWithColumns(20));
|
2013-01-09 18:16:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-11 00:34:48 +08:00
|
|
|
|
TEST_F(FormatTest, IncorrectAccessSpecifier) {
|
|
|
|
|
verifyFormat("public:");
|
|
|
|
|
verifyFormat("class A {\n"
|
|
|
|
|
"public\n"
|
2013-01-10 21:24:24 +08:00
|
|
|
|
" void f() {}\n"
|
2012-12-11 00:34:48 +08:00
|
|
|
|
"};");
|
|
|
|
|
verifyFormat("public\n"
|
|
|
|
|
"int qwerty;");
|
|
|
|
|
verifyFormat("public\n"
|
2013-01-10 21:24:24 +08:00
|
|
|
|
"B {}");
|
2012-12-11 00:34:48 +08:00
|
|
|
|
verifyFormat("public\n"
|
2013-01-10 21:24:24 +08:00
|
|
|
|
"{}");
|
2012-12-11 00:34:48 +08:00
|
|
|
|
verifyFormat("public\n"
|
2013-01-12 01:54:10 +08:00
|
|
|
|
"B { int x; }");
|
2012-12-11 00:34:48 +08:00
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
2013-03-20 23:58:10 +08:00
|
|
|
|
TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
|
|
|
|
|
verifyFormat("{");
|
|
|
|
|
verifyFormat("#})");
|
|
|
|
|
}
|
2012-12-04 23:40:36 +08:00
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("do {\n}");
|
|
|
|
|
verifyFormat("do {\n}\n"
|
2012-12-04 23:40:36 +08:00
|
|
|
|
"f();");
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("do {\n}\n"
|
2012-12-04 23:40:36 +08:00
|
|
|
|
"wheeee(fun);");
|
|
|
|
|
verifyFormat("do {\n"
|
|
|
|
|
" f();\n"
|
2013-01-08 02:10:23 +08:00
|
|
|
|
"}");
|
2012-12-04 23:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-12 03:23:05 +08:00
|
|
|
|
TEST_F(FormatTest, IncorrectCodeMissingParens) {
|
2013-01-12 02:28:36 +08:00
|
|
|
|
verifyFormat("if {\n foo;\n foo();\n}");
|
2013-01-12 03:23:05 +08:00
|
|
|
|
verifyFormat("switch {\n foo;\n foo();\n}");
|
|
|
|
|
verifyFormat("for {\n foo;\n foo();\n}");
|
|
|
|
|
verifyFormat("while {\n foo;\n foo();\n}");
|
|
|
|
|
verifyFormat("do {\n foo;\n foo();\n} while;");
|
2013-01-12 02:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-05 02:52:56 +08:00
|
|
|
|
TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
|
|
|
|
|
verifyFormat("namespace {\n"
|
2013-10-12 05:25:45 +08:00
|
|
|
|
"class Foo { Foo (\n"
|
2013-05-31 22:56:29 +08:00
|
|
|
|
"};\n"
|
|
|
|
|
"} // comment");
|
2013-01-05 02:52:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-05 01:27:50 +08:00
|
|
|
|
TEST_F(FormatTest, IncorrectCodeErrorDetection) {
|
2013-10-19 01:20:57 +08:00
|
|
|
|
EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
|
2013-01-10 21:24:24 +08:00
|
|
|
|
EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
|
|
|
|
|
EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
|
2013-10-19 01:20:57 +08:00
|
|
|
|
EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
|
2012-12-05 01:27:50 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("{\n"
|
2013-10-19 01:20:57 +08:00
|
|
|
|
" {\n"
|
|
|
|
|
" breakme(\n"
|
|
|
|
|
" qwe);\n"
|
|
|
|
|
" }\n",
|
2013-02-18 19:59:17 +08:00
|
|
|
|
format("{\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" breakme(qwe);\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
getLLVMStyleWithColumns(10)));
|
2012-12-05 01:27:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 22:36:46 +08:00
|
|
|
|
TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
|
2013-02-18 19:59:17 +08:00
|
|
|
|
verifyFormat("int x = {\n"
|
|
|
|
|
" avariable,\n"
|
|
|
|
|
" b(alongervariable)\n"
|
|
|
|
|
"};",
|
|
|
|
|
getLLVMStyleWithColumns(25));
|
2013-01-10 22:36:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-21 18:07:49 +08:00
|
|
|
|
TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
|
|
|
|
|
verifyFormat("return (a)(b) { 1, 2, 3 };");
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
|
TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
|
2013-05-23 18:15:45 +08:00
|
|
|
|
verifyFormat("vector<int> x{ 1, 2, 3, 4 };");
|
|
|
|
|
verifyFormat("vector<T> x{ {}, {}, {}, {} };");
|
2013-05-23 17:41:43 +08:00
|
|
|
|
verifyFormat("f({ 1, 2 });");
|
2013-05-23 18:15:45 +08:00
|
|
|
|
verifyFormat("auto v = Foo{ 1 };");
|
2013-05-23 17:41:43 +08:00
|
|
|
|
verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });");
|
2013-05-24 02:29:16 +08:00
|
|
|
|
verifyFormat("Class::Class : member{ 1, 2, 3 } {}");
|
2013-05-24 05:35:49 +08:00
|
|
|
|
verifyFormat("new vector<int>{ 1, 2, 3 };");
|
|
|
|
|
verifyFormat("new int[3]{ 1, 2, 3 };");
|
2013-05-24 02:29:16 +08:00
|
|
|
|
verifyFormat("return { arg1, arg2 };");
|
2013-05-29 02:50:02 +08:00
|
|
|
|
verifyFormat("return { arg1, SomeType{ parameter } };");
|
2013-05-24 02:29:16 +08:00
|
|
|
|
verifyFormat("new T{ arg1, arg2 };");
|
2013-09-13 18:55:31 +08:00
|
|
|
|
verifyFormat("f(MyMap[{ composite, key }]);");
|
2013-05-24 02:29:16 +08:00
|
|
|
|
verifyFormat("class Class {\n"
|
|
|
|
|
" T member = { arg1, arg2 };\n"
|
|
|
|
|
"};");
|
2013-05-31 18:09:55 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
|
|
|
|
|
" : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
|
|
|
|
|
" bbbbbbbbbbbbbbbbbbbb, bbbbb };");
|
2013-07-01 17:15:46 +08:00
|
|
|
|
verifyFormat("DoSomethingWithVector({} /* No data */);");
|
2013-07-02 00:43:38 +08:00
|
|
|
|
verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });");
|
2013-07-09 17:06:29 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"someFunction(OtherParam, BracedList{\n"
|
2013-07-09 19:57:27 +08:00
|
|
|
|
" // comment 1 (Forcing interesting break)\n"
|
2013-07-09 17:06:29 +08:00
|
|
|
|
" param1, param2,\n"
|
|
|
|
|
" // comment 2\n"
|
|
|
|
|
" param3, param4\n"
|
|
|
|
|
" });");
|
2013-08-28 15:50:37 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"std::this_thread::sleep_for(\n"
|
|
|
|
|
" std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);");
|
2013-05-23 18:15:45 +08:00
|
|
|
|
|
|
|
|
|
FormatStyle NoSpaces = getLLVMStyle();
|
2013-07-17 02:22:10 +08:00
|
|
|
|
NoSpaces.Cpp11BracedListStyle = true;
|
2013-05-23 18:15:45 +08:00
|
|
|
|
verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces);
|
|
|
|
|
verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces);
|
|
|
|
|
verifyFormat("f({1, 2});", NoSpaces);
|
2013-05-24 02:05:18 +08:00
|
|
|
|
verifyFormat("auto v = Foo{-1};", NoSpaces);
|
2013-05-23 18:15:45 +08:00
|
|
|
|
verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces);
|
2013-05-24 02:29:16 +08:00
|
|
|
|
verifyFormat("Class::Class : member{1, 2, 3} {}", NoSpaces);
|
2013-05-24 05:35:49 +08:00
|
|
|
|
verifyFormat("new vector<int>{1, 2, 3};", NoSpaces);
|
|
|
|
|
verifyFormat("new int[3]{1, 2, 3};", NoSpaces);
|
2013-05-24 02:29:16 +08:00
|
|
|
|
verifyFormat("return {arg1, arg2};", NoSpaces);
|
2013-05-29 02:50:02 +08:00
|
|
|
|
verifyFormat("return {arg1, SomeType{parameter}};", NoSpaces);
|
2013-05-24 02:29:16 +08:00
|
|
|
|
verifyFormat("new T{arg1, arg2};", NoSpaces);
|
2013-09-13 18:55:31 +08:00
|
|
|
|
verifyFormat("f(MyMap[{composite, key}]);", NoSpaces);
|
2013-05-24 02:29:16 +08:00
|
|
|
|
verifyFormat("class Class {\n"
|
|
|
|
|
" T member = {arg1, arg2};\n"
|
|
|
|
|
"};",
|
|
|
|
|
NoSpaces);
|
2013-05-23 17:41:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-25 22:29:37 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
|
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, 666666, 7777777,\n"
|
|
|
|
|
" 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"
|
|
|
|
|
" 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
|
|
|
|
|
" 1, 22, 333, 4444, 55555,\n"
|
|
|
|
|
" // line comment\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"
|
|
|
|
|
" 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
|
|
|
|
|
" 1, 22, 333, 4444, 55555, 666666, // comment\n"
|
|
|
|
|
" 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
|
|
|
|
|
" 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
|
|
|
|
|
" 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
|
|
|
|
|
" 7777777 };");
|
|
|
|
|
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\n"
|
|
|
|
|
"};");
|
|
|
|
|
verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
|
|
|
|
|
" 1, 1, 1, 1 };",
|
|
|
|
|
getLLVMStyleWithColumns(39));
|
|
|
|
|
verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
|
|
|
|
|
" 1, 1, 1, 1 };",
|
|
|
|
|
getLLVMStyleWithColumns(38));
|
|
|
|
|
verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
|
|
|
|
|
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1\n"
|
|
|
|
|
"};",
|
|
|
|
|
getLLVMStyleWithColumns(40));
|
2013-08-23 18:05:49 +08:00
|
|
|
|
|
|
|
|
|
// Trailing commas.
|
|
|
|
|
verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
|
|
|
|
|
" 1, 1, 1, 1, };",
|
|
|
|
|
getLLVMStyleWithColumns(39));
|
2013-10-25 22:29:37 +08:00
|
|
|
|
verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
|
|
|
|
|
" 1, 1, 1, 1, //\n"
|
2013-08-23 18:05:49 +08:00
|
|
|
|
"};",
|
|
|
|
|
getLLVMStyleWithColumns(39));
|
2013-08-26 16:10:17 +08:00
|
|
|
|
verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
|
|
|
|
|
" 1, 1, 1, 1,\n"
|
|
|
|
|
" /**/ /**/ };",
|
|
|
|
|
getLLVMStyleWithColumns(39));
|
2013-10-24 22:14:49 +08:00
|
|
|
|
verifyFormat("return { { aaaaaaaaaaaaaaaaaaaaa },\n"
|
|
|
|
|
" { aaaaaaaaaaaaaaaaaaa },\n"
|
|
|
|
|
" { aaaaaaaaaaaaaaaaaaaaa },\n"
|
|
|
|
|
" { aaaaaaaaaaaaaaaaa } };",
|
2013-08-27 16:43:47 +08:00
|
|
|
|
getLLVMStyleWithColumns(60));
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-12 01:54:10 +08:00
|
|
|
|
TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
|
|
|
|
|
verifyFormat("void f() { return 42; }");
|
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" // Comment\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("{\n"
|
|
|
|
|
"#error {\n"
|
|
|
|
|
" int a;\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("{\n"
|
|
|
|
|
" int a;\n"
|
|
|
|
|
"#error {\n"
|
|
|
|
|
"}");
|
2013-05-16 18:17:39 +08:00
|
|
|
|
verifyFormat("void f() {} // comment");
|
|
|
|
|
verifyFormat("void f() { int a; } // comment");
|
2013-05-17 00:54:34 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
"} // comment",
|
|
|
|
|
getLLVMStyleWithColumns(15));
|
2013-01-22 00:42:44 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
|
|
|
|
|
verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
|
|
|
|
|
|
|
|
|
|
verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
|
|
|
|
|
verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
|
2013-01-12 01:54:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 21:38:33 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
|
|
|
|
|
// Elaborate type variable declarations.
|
2013-01-12 02:13:04 +08:00
|
|
|
|
verifyFormat("struct foo a = { bar };\nint n;");
|
2013-01-15 21:38:33 +08:00
|
|
|
|
verifyFormat("class foo a = { bar };\nint n;");
|
|
|
|
|
verifyFormat("union foo a = { bar };\nint n;");
|
|
|
|
|
|
|
|
|
|
// Elaborate types inside function definitions.
|
|
|
|
|
verifyFormat("struct foo f() {}\nint n;");
|
|
|
|
|
verifyFormat("class foo f() {}\nint n;");
|
|
|
|
|
verifyFormat("union foo f() {}\nint n;");
|
|
|
|
|
|
|
|
|
|
// Templates.
|
|
|
|
|
verifyFormat("template <class X> void f() {}\nint n;");
|
|
|
|
|
verifyFormat("template <struct X> void f() {}\nint n;");
|
|
|
|
|
verifyFormat("template <union X> void f() {}\nint n;");
|
|
|
|
|
|
|
|
|
|
// Actual definitions...
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("struct {\n} n;");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"template <template <class T, class Y>, class Z> class X {\n} n;");
|
2013-01-15 21:38:33 +08:00
|
|
|
|
verifyFormat("union Z {\n int n;\n} x;");
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("class MACRO Z {\n} n;");
|
|
|
|
|
verifyFormat("class MACRO(X) Z {\n} n;");
|
|
|
|
|
verifyFormat("class __attribute__(X) Z {\n} n;");
|
|
|
|
|
verifyFormat("class __declspec(X) Z {\n} n;");
|
2013-02-06 23:57:54 +08:00
|
|
|
|
verifyFormat("class A##B##C {\n} n;");
|
2013-10-07 17:15:41 +08:00
|
|
|
|
verifyFormat("class alignas(16) Z {\n} n;");
|
2013-01-15 21:38:33 +08:00
|
|
|
|
|
2013-01-21 18:17:14 +08:00
|
|
|
|
// Redefinition from nested context:
|
2013-01-22 00:42:44 +08:00
|
|
|
|
verifyFormat("class A::B::C {\n} n;");
|
2013-01-21 18:17:14 +08:00
|
|
|
|
|
2013-01-21 21:58:54 +08:00
|
|
|
|
// Template definitions.
|
2013-05-15 21:46:48 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"template <typename F>\n"
|
|
|
|
|
"Matcher(const Matcher<F> &Other,\n"
|
|
|
|
|
" typename enable_if_c<is_base_of<F, T>::value &&\n"
|
|
|
|
|
" !is_same<F, T>::value>::type * = 0)\n"
|
|
|
|
|
" : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
|
|
|
|
|
|
2013-01-21 21:58:54 +08:00
|
|
|
|
// FIXME: This is still incorrectly handled at the formatter side.
|
2013-07-31 06:37:19 +08:00
|
|
|
|
verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
|
2013-01-21 21:58:54 +08:00
|
|
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
|
// This now gets parsed incorrectly as class definition.
|
2013-01-22 00:42:44 +08:00
|
|
|
|
// verifyFormat("class A<int> f() {\n}\nint n;");
|
2013-01-21 21:58:54 +08:00
|
|
|
|
|
2013-01-15 21:38:33 +08:00
|
|
|
|
// Elaborate types where incorrectly parsing the structural element would
|
|
|
|
|
// break the indent.
|
|
|
|
|
verifyFormat("if (true)\n"
|
|
|
|
|
" class X x;\n"
|
|
|
|
|
"else\n"
|
|
|
|
|
" f();\n");
|
2013-03-20 23:12:38 +08:00
|
|
|
|
|
|
|
|
|
// This is simply incomplete. Formatting is not important, but must not crash.
|
2013-08-13 18:58:30 +08:00
|
|
|
|
verifyFormat("class A:");
|
2013-01-12 02:13:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 23:50:27 +08:00
|
|
|
|
TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
|
2013-10-12 05:25:45 +08:00
|
|
|
|
EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
|
|
|
|
|
format("#error Leave all white!!!!! space* alone!\n"));
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"#warning Leave all white!!!!! space* alone!\n",
|
|
|
|
|
format("#warning Leave all white!!!!! space* alone!\n"));
|
2013-01-15 23:50:27 +08:00
|
|
|
|
EXPECT_EQ("#error 1", format(" # error 1"));
|
|
|
|
|
EXPECT_EQ("#warning 1", format(" # warning 1"));
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 21:54:04 +08:00
|
|
|
|
TEST_F(FormatTest, FormatHashIfExpressions) {
|
|
|
|
|
// FIXME: Come up with a better indentation for #elif.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
|
|
|
|
|
" defined(BBBBBBBB)\n"
|
|
|
|
|
"#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
|
|
|
|
|
" defined(BBBBBBBB)\n"
|
|
|
|
|
"#endif",
|
|
|
|
|
getLLVMStyleWithColumns(65));
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-18 22:04:34 +08:00
|
|
|
|
TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
|
|
|
|
|
FormatStyle AllowsMergedIf = getGoogleStyle();
|
|
|
|
|
AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
|
|
|
|
|
verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
|
|
|
|
|
verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
|
2013-01-18 22:46:43 +08:00
|
|
|
|
verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
|
|
|
|
|
EXPECT_EQ("if (true) return 42;",
|
|
|
|
|
format("if (true)\nreturn 42;", AllowsMergedIf));
|
|
|
|
|
FormatStyle ShortMergedIf = AllowsMergedIf;
|
|
|
|
|
ShortMergedIf.ColumnLimit = 25;
|
2013-04-25 16:56:26 +08:00
|
|
|
|
verifyFormat("#define A \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" if (true) return 42;",
|
|
|
|
|
ShortMergedIf);
|
2013-04-25 16:56:26 +08:00
|
|
|
|
verifyFormat("#define A \\\n"
|
|
|
|
|
" f(); \\\n"
|
2013-01-18 22:46:43 +08:00
|
|
|
|
" if (true)\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"#define B",
|
|
|
|
|
ShortMergedIf);
|
2013-04-25 16:56:26 +08:00
|
|
|
|
verifyFormat("#define A \\\n"
|
|
|
|
|
" f(); \\\n"
|
2013-01-18 22:46:43 +08:00
|
|
|
|
" if (true)\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"g();",
|
|
|
|
|
ShortMergedIf);
|
2013-01-21 22:16:56 +08:00
|
|
|
|
verifyFormat("{\n"
|
|
|
|
|
"#ifdef A\n"
|
|
|
|
|
" // Comment\n"
|
|
|
|
|
" if (true) continue;\n"
|
|
|
|
|
"#endif\n"
|
|
|
|
|
" // Comment\n"
|
2013-10-12 05:25:45 +08:00
|
|
|
|
" if (true) continue;\n"
|
|
|
|
|
"}",
|
2013-02-18 19:59:17 +08:00
|
|
|
|
ShortMergedIf);
|
2013-01-18 22:04:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-23 00:31:55 +08:00
|
|
|
|
TEST_F(FormatTest, BlockCommentsInControlLoops) {
|
|
|
|
|
verifyFormat("if (0) /* a comment in a strange place */ {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("if (0) /* a comment in a strange place */ {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"} /* another comment */ else /* comment #3 */ {\n"
|
|
|
|
|
" g();\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("while (0) /* a comment in a strange place */ {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("for (;;) /* a comment in a strange place */ {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("do /* a comment in a strange place */ {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"} /* another comment */ while (0);");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, BlockComments) {
|
|
|
|
|
EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
|
|
|
|
|
format("/* *//* */ /* */\n/* *//* */ /* */"));
|
2013-02-18 19:59:17 +08:00
|
|
|
|
EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
|
2013-08-28 17:07:32 +08:00
|
|
|
|
EXPECT_EQ("#define A /*123*/ \\\n"
|
2013-01-23 00:31:55 +08:00
|
|
|
|
" b\n"
|
|
|
|
|
"/* */\n"
|
|
|
|
|
"someCall(\n"
|
|
|
|
|
" parameter);",
|
2013-03-21 20:28:10 +08:00
|
|
|
|
format("#define A /*123*/ b\n"
|
2013-01-23 00:31:55 +08:00
|
|
|
|
"/* */\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"someCall(parameter);",
|
|
|
|
|
getLLVMStyleWithColumns(15)));
|
2013-01-23 00:31:55 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("#define A\n"
|
|
|
|
|
"/* */ someCall(\n"
|
|
|
|
|
" parameter);",
|
|
|
|
|
format("#define A\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"/* */someCall(parameter);",
|
|
|
|
|
getLLVMStyleWithColumns(15)));
|
2013-05-30 14:40:07 +08:00
|
|
|
|
EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
|
2013-05-30 23:20:29 +08:00
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
"*\n"
|
|
|
|
|
" * aaaaaa\n"
|
2013-07-08 22:12:07 +08:00
|
|
|
|
"*aaaaaa\n"
|
2013-05-30 23:20:29 +08:00
|
|
|
|
"*/",
|
|
|
|
|
format("/*\n"
|
|
|
|
|
"*\n"
|
|
|
|
|
" * aaaaaa aaaaaa\n"
|
|
|
|
|
"*/",
|
|
|
|
|
getLLVMStyleWithColumns(10)));
|
2013-05-31 01:27:48 +08:00
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
"**\n"
|
|
|
|
|
"* aaaaaa\n"
|
2013-07-08 22:12:07 +08:00
|
|
|
|
"*aaaaaa\n"
|
2013-05-31 01:27:48 +08:00
|
|
|
|
"*/",
|
|
|
|
|
format("/*\n"
|
|
|
|
|
"**\n"
|
|
|
|
|
"* aaaaaa aaaaaa\n"
|
|
|
|
|
"*/",
|
|
|
|
|
getLLVMStyleWithColumns(10)));
|
2013-02-04 15:32:14 +08:00
|
|
|
|
|
2013-02-27 17:47:53 +08:00
|
|
|
|
FormatStyle NoBinPacking = getLLVMStyle();
|
|
|
|
|
NoBinPacking.BinPackParameters = false;
|
2013-02-04 15:32:14 +08:00
|
|
|
|
EXPECT_EQ("someFunction(1, /* comment 1 */\n"
|
|
|
|
|
" 2, /* comment 2 */\n"
|
|
|
|
|
" 3, /* comment 3 */\n"
|
2013-02-04 16:34:57 +08:00
|
|
|
|
" aaaa,\n"
|
|
|
|
|
" bbbb);",
|
2013-02-04 15:32:14 +08:00
|
|
|
|
format("someFunction (1, /* comment 1 */\n"
|
|
|
|
|
" 2, /* comment 2 */ \n"
|
|
|
|
|
" 3, /* comment 3 */\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"aaaa, bbbb );",
|
2013-02-27 17:47:53 +08:00
|
|
|
|
NoBinPacking));
|
2013-02-06 23:23:09 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"bool aaaaaaaaaaaaa = /* trailing comment */\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
|
|
|
|
|
format(
|
|
|
|
|
"bool aaaaaaaaaaaaa = /* trailing comment */\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
|
2013-02-07 04:07:35 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
|
|
|
|
|
"int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
|
|
|
|
|
"int cccccccccccccccccccccccccccccc; /* comment */\n",
|
|
|
|
|
format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
|
|
|
|
|
"int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
|
|
|
|
|
"int cccccccccccccccccccccccccccccc; /* comment */\n"));
|
2013-07-01 17:34:09 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("void f(int * /* unused */) {}");
|
2013-07-08 22:12:07 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" **\n"
|
|
|
|
|
" */",
|
|
|
|
|
format("/*\n"
|
|
|
|
|
" **\n"
|
|
|
|
|
" */"));
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" *q\n"
|
|
|
|
|
" */",
|
|
|
|
|
format("/*\n"
|
|
|
|
|
" *q\n"
|
|
|
|
|
" */"));
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" * q\n"
|
|
|
|
|
" */",
|
|
|
|
|
format("/*\n"
|
|
|
|
|
" * q\n"
|
|
|
|
|
" */"));
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" **/",
|
|
|
|
|
format("/*\n"
|
|
|
|
|
" **/"));
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" ***/",
|
|
|
|
|
format("/*\n"
|
|
|
|
|
" ***/"));
|
2013-01-23 00:31:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 00:40:56 +08:00
|
|
|
|
TEST_F(FormatTest, BlockCommentsInMacros) {
|
|
|
|
|
EXPECT_EQ("#define A \\\n"
|
|
|
|
|
" { \\\n"
|
|
|
|
|
" /* one line */ \\\n"
|
|
|
|
|
" someCall();",
|
|
|
|
|
format("#define A { \\\n"
|
|
|
|
|
" /* one line */ \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" someCall();",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
2013-02-07 00:40:56 +08:00
|
|
|
|
EXPECT_EQ("#define A \\\n"
|
|
|
|
|
" { \\\n"
|
|
|
|
|
" /* previous */ \\\n"
|
|
|
|
|
" /* one line */ \\\n"
|
|
|
|
|
" someCall();",
|
|
|
|
|
format("#define A { \\\n"
|
|
|
|
|
" /* previous */ \\\n"
|
|
|
|
|
" /* one line */ \\\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" someCall();",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
2013-02-07 00:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 06:06:18 +08:00
|
|
|
|
TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
|
|
|
|
|
EXPECT_EQ("a = {\n"
|
|
|
|
|
" 1111 /* */\n"
|
|
|
|
|
"};",
|
2013-06-13 03:04:12 +08:00
|
|
|
|
format("a = {1111 /* */\n"
|
2013-05-30 06:06:18 +08:00
|
|
|
|
"};",
|
|
|
|
|
getLLVMStyleWithColumns(15)));
|
|
|
|
|
EXPECT_EQ("a = {\n"
|
|
|
|
|
" 1111 /* */\n"
|
|
|
|
|
"};",
|
2013-06-13 03:04:12 +08:00
|
|
|
|
format("a = {1111 /* */\n"
|
2013-05-30 06:06:18 +08:00
|
|
|
|
"};",
|
|
|
|
|
getLLVMStyleWithColumns(15)));
|
|
|
|
|
|
|
|
|
|
// FIXME: The formatting is still wrong here.
|
|
|
|
|
EXPECT_EQ("a = {\n"
|
|
|
|
|
" 1111 /* a\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
"};",
|
2013-06-13 03:04:12 +08:00
|
|
|
|
format("a = {1111 /* a */\n"
|
2013-05-30 06:06:18 +08:00
|
|
|
|
"};",
|
|
|
|
|
getLLVMStyleWithColumns(15)));
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 00:40:56 +08:00
|
|
|
|
TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
|
|
|
|
|
// FIXME: This is not what we want...
|
|
|
|
|
verifyFormat("{\n"
|
|
|
|
|
"// a"
|
|
|
|
|
"// b");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-23 18:09:28 +08:00
|
|
|
|
TEST_F(FormatTest, FormatStarDependingOnContext) {
|
2013-01-23 17:32:48 +08:00
|
|
|
|
verifyFormat("void f(int *a);");
|
|
|
|
|
verifyFormat("void f() { f(fint * b); }");
|
2013-01-23 19:03:04 +08:00
|
|
|
|
verifyFormat("class A {\n void f(int *a);\n};");
|
|
|
|
|
verifyFormat("class A {\n int *a;\n};");
|
|
|
|
|
verifyFormat("namespace a {\n"
|
|
|
|
|
"namespace b {\n"
|
|
|
|
|
"class A {\n"
|
|
|
|
|
" void f() {}\n"
|
|
|
|
|
" int *a;\n"
|
|
|
|
|
"};\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"}");
|
2013-01-23 17:32:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-23 18:09:28 +08:00
|
|
|
|
TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
|
|
|
|
|
verifyFormat("while");
|
|
|
|
|
verifyFormat("operator");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 01:56:31 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Objective-C tests.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2012-12-21 03:54:13 +08:00
|
|
|
|
TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
|
|
|
|
|
verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
|
|
|
|
|
EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
|
|
|
|
|
format("-(NSUInteger)indexOfObject:(id)anObject;"));
|
2013-01-07 21:26:07 +08:00
|
|
|
|
EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
|
2012-12-21 03:54:13 +08:00
|
|
|
|
EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
|
|
|
|
|
EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
|
|
|
|
|
format("-(NSInteger)Method3:(id)anObject;"));
|
|
|
|
|
EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
|
|
|
|
|
format("-(NSInteger)Method4:(id)anObject;"));
|
|
|
|
|
EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
|
|
|
|
|
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;"));
|
2013-01-07 21:26:07 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
|
2013-02-18 19:59:17 +08:00
|
|
|
|
format(
|
|
|
|
|
"- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
|
2012-12-22 06:51:18 +08:00
|
|
|
|
|
|
|
|
|
// Very long objectiveC method declaration.
|
2013-02-05 18:07:47 +08:00
|
|
|
|
verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
|
|
|
|
|
" inRange:(NSRange)range\n"
|
|
|
|
|
" outRange:(NSRange)out_range\n"
|
|
|
|
|
" outRange1:(NSRange)out_range1\n"
|
|
|
|
|
" outRange2:(NSRange)out_range2\n"
|
|
|
|
|
" outRange3:(NSRange)out_range3\n"
|
|
|
|
|
" outRange4:(NSRange)out_range4\n"
|
|
|
|
|
" outRange5:(NSRange)out_range5\n"
|
|
|
|
|
" outRange6:(NSRange)out_range6\n"
|
|
|
|
|
" outRange7:(NSRange)out_range7\n"
|
|
|
|
|
" outRange8:(NSRange)out_range8\n"
|
|
|
|
|
" outRange9:(NSRange)out_range9;");
|
2013-01-11 04:18:33 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("- (int)sum:(vector<int>)numbers;");
|
2013-01-17 14:14:50 +08:00
|
|
|
|
verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
|
2013-01-11 04:18:33 +08:00
|
|
|
|
// 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;");
|
2013-01-11 07:11:41 +08:00
|
|
|
|
|
2013-05-28 16:33:00 +08:00
|
|
|
|
verifyFormat("- (int (*)())foo:(int (*)())f;");
|
|
|
|
|
verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
|
2013-01-11 07:11:41 +08:00
|
|
|
|
|
|
|
|
|
// If there's no return type (very rare in practice!), LLVM and Google style
|
|
|
|
|
// agree.
|
2013-03-23 00:55:40 +08:00
|
|
|
|
verifyFormat("- foo;");
|
2013-01-11 07:11:41 +08:00
|
|
|
|
verifyFormat("- foo:(int)f;");
|
|
|
|
|
verifyGoogleFormat("- foo:(int)foo;");
|
2013-01-07 21:26:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 16:36:49 +08:00
|
|
|
|
TEST_F(FormatTest, FormatObjCBlocks) {
|
2013-01-10 21:08:12 +08:00
|
|
|
|
verifyFormat("int (^Block)(int, int);");
|
|
|
|
|
verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
|
2013-01-09 16:36:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 04:25:35 +08:00
|
|
|
|
TEST_F(FormatTest, FormatObjCInterface) {
|
2013-01-11 04:12:55 +08:00
|
|
|
|
verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"@public\n"
|
|
|
|
|
" int field1;\n"
|
|
|
|
|
"@protected\n"
|
|
|
|
|
" int field2;\n"
|
|
|
|
|
"@private\n"
|
|
|
|
|
" int field3;\n"
|
|
|
|
|
"@package\n"
|
|
|
|
|
" int field4;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
|
|
|
|
verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
|
|
|
|
|
" @public\n"
|
|
|
|
|
" int field1;\n"
|
|
|
|
|
" @protected\n"
|
|
|
|
|
" int field2;\n"
|
|
|
|
|
" @private\n"
|
|
|
|
|
" int field3;\n"
|
|
|
|
|
" @package\n"
|
|
|
|
|
" int field4;\n"
|
|
|
|
|
"}\n"
|
2013-01-17 14:14:50 +08:00
|
|
|
|
"+ (id)init;\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-23 00:53:59 +08:00
|
|
|
|
verifyFormat("@interface /* wait for it */ Foo\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"// Look, a comment!\n"
|
|
|
|
|
"- (int)answerWith:(int)i;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
|
|
|
|
verifyFormat("@interface Foo\n"
|
2013-01-10 05:42:32 +08:00
|
|
|
|
"@end\n"
|
|
|
|
|
"@interface Bar\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
|
|
|
|
verifyFormat("@interface Foo : Bar\n"
|
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-23 00:53:59 +08:00
|
|
|
|
verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-11 04:12:55 +08:00
|
|
|
|
verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
|
2013-01-17 14:14:50 +08:00
|
|
|
|
"+ (id)init;\n"
|
2013-01-11 04:12:55 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-11 03:19:14 +08:00
|
|
|
|
verifyFormat("@interface Foo (HackStuff)\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-11 03:19:14 +08:00
|
|
|
|
verifyFormat("@interface Foo ()\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-11 04:12:55 +08:00
|
|
|
|
verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-11 04:12:55 +08:00
|
|
|
|
verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
|
2013-01-17 14:14:50 +08:00
|
|
|
|
"+ (id)init;\n"
|
2013-01-11 04:12:55 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@interface Foo {\n"
|
|
|
|
|
" int _i;\n"
|
|
|
|
|
"}\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@interface Foo : Bar {\n"
|
|
|
|
|
" int _i;\n"
|
|
|
|
|
"}\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
|
|
|
|
|
" int _i;\n"
|
|
|
|
|
"}\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@interface Foo (HackStuff) {\n"
|
|
|
|
|
" int _i;\n"
|
|
|
|
|
"}\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@interface Foo () {\n"
|
|
|
|
|
" int _i;\n"
|
|
|
|
|
"}\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
|
|
|
|
|
" int _i;\n"
|
|
|
|
|
"}\n"
|
2013-01-10 04:25:35 +08:00
|
|
|
|
"+ (id)init;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 07:25:37 +08:00
|
|
|
|
TEST_F(FormatTest, FormatObjCImplementation) {
|
|
|
|
|
verifyFormat("@implementation Foo : NSObject {\n"
|
|
|
|
|
"@public\n"
|
|
|
|
|
" int field1;\n"
|
|
|
|
|
"@protected\n"
|
|
|
|
|
" int field2;\n"
|
|
|
|
|
"@private\n"
|
|
|
|
|
" int field3;\n"
|
|
|
|
|
"@package\n"
|
|
|
|
|
" int field4;\n"
|
|
|
|
|
"}\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
"+ (id)init {\n}\n"
|
2013-01-10 07:25:37 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
|
|
|
|
verifyGoogleFormat("@implementation Foo : NSObject {\n"
|
|
|
|
|
" @public\n"
|
|
|
|
|
" int field1;\n"
|
|
|
|
|
" @protected\n"
|
|
|
|
|
" int field2;\n"
|
|
|
|
|
" @private\n"
|
|
|
|
|
" int field3;\n"
|
|
|
|
|
" @package\n"
|
|
|
|
|
" int field4;\n"
|
|
|
|
|
"}\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
"+ (id)init {\n}\n"
|
2013-01-10 07:25:37 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
|
|
|
|
verifyFormat("@implementation Foo\n"
|
|
|
|
|
"+ (id)init {\n"
|
2013-01-15 00:24:39 +08:00
|
|
|
|
" if (true)\n"
|
|
|
|
|
" return nil;\n"
|
2013-01-10 07:25:37 +08:00
|
|
|
|
"}\n"
|
|
|
|
|
"// Look, a comment!\n"
|
2013-01-12 03:17:44 +08:00
|
|
|
|
"- (int)answerWith:(int)i {\n"
|
|
|
|
|
" return i;\n"
|
|
|
|
|
"}\n"
|
2013-01-12 05:14:08 +08:00
|
|
|
|
"+ (int)answerWith:(int)i {\n"
|
|
|
|
|
" return i;\n"
|
|
|
|
|
"}\n"
|
2013-01-10 07:25:37 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
|
|
|
|
verifyFormat("@implementation Foo\n"
|
|
|
|
|
"@end\n"
|
|
|
|
|
"@implementation Bar\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
|
|
|
|
verifyFormat("@implementation Foo : Bar\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
"+ (id)init {\n}\n"
|
|
|
|
|
"- (void)foo {\n}\n"
|
2013-01-10 07:25:37 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@implementation Foo {\n"
|
|
|
|
|
" int _i;\n"
|
|
|
|
|
"}\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
"+ (id)init {\n}\n"
|
2013-01-10 07:25:37 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@implementation Foo : Bar {\n"
|
|
|
|
|
" int _i;\n"
|
|
|
|
|
"}\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
"+ (id)init {\n}\n"
|
2013-01-10 07:25:37 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-11 03:19:14 +08:00
|
|
|
|
verifyFormat("@implementation Foo (HackStuff)\n"
|
2013-01-22 00:42:44 +08:00
|
|
|
|
"+ (id)init {\n}\n"
|
2013-01-10 07:25:37 +08:00
|
|
|
|
"@end");
|
2013-08-28 16:04:23 +08:00
|
|
|
|
verifyFormat("@implementation ObjcClass\n"
|
|
|
|
|
"- (void)method;\n"
|
|
|
|
|
"{}\n"
|
|
|
|
|
"@end");
|
2013-01-10 07:25:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 05:15:03 +08:00
|
|
|
|
TEST_F(FormatTest, FormatObjCProtocol) {
|
|
|
|
|
verifyFormat("@protocol Foo\n"
|
|
|
|
|
"@property(weak) id delegate;\n"
|
|
|
|
|
"- (NSUInteger)numberOfThings;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-11 04:12:55 +08:00
|
|
|
|
verifyFormat("@protocol MyProtocol <NSObject>\n"
|
2013-01-10 05:15:03 +08:00
|
|
|
|
"- (NSUInteger)numberOfThings;\n"
|
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-11 04:12:55 +08:00
|
|
|
|
verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
|
2013-01-17 14:14:50 +08:00
|
|
|
|
"- (NSUInteger)numberOfThings;\n"
|
2013-01-11 04:12:55 +08:00
|
|
|
|
"@end");
|
|
|
|
|
|
2013-01-10 05:15:03 +08:00
|
|
|
|
verifyFormat("@protocol Foo;\n"
|
|
|
|
|
"@protocol Bar;\n");
|
2013-01-10 05:42:32 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("@protocol Foo\n"
|
|
|
|
|
"@end\n"
|
|
|
|
|
"@protocol Bar\n"
|
|
|
|
|
"@end");
|
2013-01-10 08:25:19 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("@protocol myProtocol\n"
|
|
|
|
|
"- (void)mandatoryWithInt:(int)i;\n"
|
|
|
|
|
"@optional\n"
|
|
|
|
|
"- (void)optional;\n"
|
|
|
|
|
"@required\n"
|
|
|
|
|
"- (void)required;\n"
|
2013-01-10 08:42:07 +08:00
|
|
|
|
"@optional\n"
|
|
|
|
|
"@property(assign) int madProp;\n"
|
2013-01-10 08:25:19 +08:00
|
|
|
|
"@end\n");
|
2013-08-01 21:46:58 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("@property(nonatomic, assign, readonly)\n"
|
|
|
|
|
" int *looooooooooooooooooooooooooooongNumber;\n"
|
|
|
|
|
"@property(nonatomic, assign, readonly)\n"
|
|
|
|
|
" NSString *looooooooooooooooooooooooooooongName;");
|
2013-01-10 05:15:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 18:07:47 +08:00
|
|
|
|
TEST_F(FormatTest, FormatObjCMethodDeclarations) {
|
|
|
|
|
verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
|
|
|
|
|
" rect:(NSRect)theRect\n"
|
|
|
|
|
" interval:(float)theInterval {\n"
|
|
|
|
|
"}");
|
|
|
|
|
verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
|
|
|
|
|
" longKeyword:(NSRect)theRect\n"
|
|
|
|
|
" evenLongerKeyword:(float)theInterval\n"
|
|
|
|
|
" error:(NSError **)theError {\n"
|
|
|
|
|
"}");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-12 14:18:40 +08:00
|
|
|
|
TEST_F(FormatTest, FormatObjCMethodExpr) {
|
|
|
|
|
verifyFormat("[foo bar:baz];");
|
|
|
|
|
verifyFormat("return [foo bar:baz];");
|
|
|
|
|
verifyFormat("f([foo bar:baz]);");
|
|
|
|
|
verifyFormat("f(2, [foo bar:baz]);");
|
|
|
|
|
verifyFormat("f(2, a ? b : c);");
|
|
|
|
|
verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
|
|
|
|
|
|
2013-02-06 00:21:00 +08:00
|
|
|
|
// Unary operators.
|
|
|
|
|
verifyFormat("int a = +[foo bar:baz];");
|
|
|
|
|
verifyFormat("int a = -[foo bar:baz];");
|
|
|
|
|
verifyFormat("int a = ![foo bar:baz];");
|
|
|
|
|
verifyFormat("int a = ~[foo bar:baz];");
|
|
|
|
|
verifyFormat("int a = ++[foo bar:baz];");
|
|
|
|
|
verifyFormat("int a = --[foo bar:baz];");
|
|
|
|
|
verifyFormat("int a = sizeof [foo bar:baz];");
|
2013-06-28 20:51:24 +08:00
|
|
|
|
verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
|
2013-02-06 14:20:11 +08:00
|
|
|
|
verifyFormat("int a = &[foo bar:baz];");
|
|
|
|
|
verifyFormat("int a = *[foo bar:baz];");
|
2013-02-06 00:21:00 +08:00
|
|
|
|
// FIXME: Make casts work, without breaking f()[4].
|
2013-02-13 11:48:27 +08:00
|
|
|
|
//verifyFormat("int a = (int)[foo bar:baz];");
|
2013-02-13 12:40:03 +08:00
|
|
|
|
//verifyFormat("return (int)[foo bar:baz];");
|
|
|
|
|
//verifyFormat("(void)[foo bar:baz];");
|
2013-02-13 11:48:27 +08:00
|
|
|
|
verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
|
2013-02-06 00:21:00 +08:00
|
|
|
|
|
|
|
|
|
// Binary operators.
|
2013-01-12 14:18:40 +08:00
|
|
|
|
verifyFormat("[foo bar:baz], [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] = [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] *= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] /= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] %= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] += [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] -= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] &= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] |= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] || [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] && [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] | [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] & [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] == [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] != [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] >= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] <= [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] > [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] < [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] >> [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] << [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] - [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] + [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] * [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] / [foo bar:baz];");
|
|
|
|
|
verifyFormat("[foo bar:baz] % [foo bar:baz];");
|
|
|
|
|
// Whew!
|
|
|
|
|
|
2013-02-11 23:32:15 +08:00
|
|
|
|
verifyFormat("return in[42];");
|
|
|
|
|
verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
|
|
|
|
|
"}");
|
|
|
|
|
|
2013-01-12 14:18:40 +08:00
|
|
|
|
verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
|
|
|
|
|
verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
|
|
|
|
|
verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
|
|
|
|
|
verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
|
|
|
|
|
verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
|
2013-01-13 07:41:33 +08:00
|
|
|
|
verifyFormat("[button setAction:@selector(zoomOut:)];");
|
2013-01-13 07:48:49 +08:00
|
|
|
|
verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
|
2013-02-05 18:07:47 +08:00
|
|
|
|
|
2013-01-12 14:18:40 +08:00
|
|
|
|
verifyFormat("arr[[self indexForFoo:a]];");
|
|
|
|
|
verifyFormat("throw [self errorFor:a];");
|
|
|
|
|
verifyFormat("@throw [self errorFor:a];");
|
|
|
|
|
|
2013-06-25 08:55:57 +08:00
|
|
|
|
verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
|
|
|
|
|
verifyFormat("[(id)foo bar:(id) ? baz : quux];");
|
|
|
|
|
verifyFormat("4 > 4 ? (id)a : (id)baz;");
|
|
|
|
|
|
2013-01-13 06:48:47 +08:00
|
|
|
|
// This tests that the formatter doesn't break after "backing" but before ":",
|
|
|
|
|
// which would be at 80 columns.
|
2013-01-12 14:18:40 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"void f() {\n"
|
2013-02-05 18:07:47 +08:00
|
|
|
|
" if ((self = [super initWithContentRect:contentRect\n"
|
2013-08-02 07:13:03 +08:00
|
|
|
|
" styleMask:styleMask ?: otherMask\n"
|
2013-02-05 18:07:47 +08:00
|
|
|
|
" backing:NSBackingStoreBuffered\n"
|
|
|
|
|
" defer:YES]))");
|
|
|
|
|
|
2013-02-06 18:05:46 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"[foo checkThatBreakingAfterColonWorksOk:\n"
|
2013-08-02 07:13:03 +08:00
|
|
|
|
" [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
|
2013-02-05 18:07:47 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("[myObj short:arg1 // Force line break\n"
|
2013-08-02 07:13:03 +08:00
|
|
|
|
" longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
|
|
|
|
|
" evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
|
2013-02-05 18:07:47 +08:00
|
|
|
|
" error:arg4];");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"void f() {\n"
|
|
|
|
|
" popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
|
|
|
|
|
" initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
|
|
|
|
|
" pos.width(), pos.height())\n"
|
|
|
|
|
" styleMask:NSBorderlessWindowMask\n"
|
|
|
|
|
" backing:NSBackingStoreBuffered\n"
|
|
|
|
|
" defer:NO]);\n"
|
|
|
|
|
"}");
|
2013-02-06 18:05:46 +08:00
|
|
|
|
verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
|
|
|
|
|
" with:contentsNativeView];");
|
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
|
|
|
|
|
" owner:nillllll];");
|
|
|
|
|
|
|
|
|
|
verifyFormat(
|
2013-02-08 16:22:00 +08:00
|
|
|
|
"[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
|
2013-02-06 18:05:46 +08:00
|
|
|
|
" forType:kBookmarkButtonDragType];");
|
|
|
|
|
|
|
|
|
|
verifyFormat("[defaultCenter addObserver:self\n"
|
|
|
|
|
" selector:@selector(willEnterFullscreen)\n"
|
|
|
|
|
" name:kWillEnterFullscreenNotification\n"
|
|
|
|
|
" object:nil];");
|
2013-02-07 00:00:26 +08:00
|
|
|
|
verifyFormat("[image_rep drawInRect:drawRect\n"
|
|
|
|
|
" fromRect:NSZeroRect\n"
|
|
|
|
|
" operation:NSCompositeCopy\n"
|
|
|
|
|
" fraction:1.0\n"
|
|
|
|
|
" respectFlipped:NO\n"
|
|
|
|
|
" hints:nil];");
|
2013-02-07 00:54:35 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"scoped_nsobject<NSTextField> message(\n"
|
|
|
|
|
" // The frame will be fixed up when |-setMessageText:| is called.\n"
|
|
|
|
|
" [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
|
2013-08-02 07:13:03 +08:00
|
|
|
|
verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
|
|
|
|
|
" aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
|
|
|
|
|
" aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
|
|
|
|
|
" aaaa:bbb];");
|
2013-10-27 01:00:22 +08:00
|
|
|
|
|
|
|
|
|
// Variadic parameters.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
|
2013-01-12 14:18:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-07 23:56:25 +08:00
|
|
|
|
TEST_F(FormatTest, ObjCAt) {
|
2013-01-08 00:07:07 +08:00
|
|
|
|
verifyFormat("@autoreleasepool");
|
2013-01-08 00:14:28 +08:00
|
|
|
|
verifyFormat("@catch");
|
|
|
|
|
verifyFormat("@class");
|
2013-01-08 00:07:07 +08:00
|
|
|
|
verifyFormat("@compatibility_alias");
|
|
|
|
|
verifyFormat("@defs");
|
2013-01-07 23:17:23 +08:00
|
|
|
|
verifyFormat("@dynamic");
|
2013-01-08 00:07:07 +08:00
|
|
|
|
verifyFormat("@encode");
|
|
|
|
|
verifyFormat("@end");
|
|
|
|
|
verifyFormat("@finally");
|
|
|
|
|
verifyFormat("@implementation");
|
|
|
|
|
verifyFormat("@import");
|
|
|
|
|
verifyFormat("@interface");
|
|
|
|
|
verifyFormat("@optional");
|
|
|
|
|
verifyFormat("@package");
|
2013-01-08 00:14:28 +08:00
|
|
|
|
verifyFormat("@private");
|
2013-01-08 00:07:07 +08:00
|
|
|
|
verifyFormat("@property");
|
2013-01-08 00:14:28 +08:00
|
|
|
|
verifyFormat("@protected");
|
2013-01-08 00:07:07 +08:00
|
|
|
|
verifyFormat("@protocol");
|
2013-01-08 00:14:28 +08:00
|
|
|
|
verifyFormat("@public");
|
2013-01-08 00:07:07 +08:00
|
|
|
|
verifyFormat("@required");
|
|
|
|
|
verifyFormat("@selector");
|
|
|
|
|
verifyFormat("@synchronized");
|
|
|
|
|
verifyFormat("@synthesize");
|
2013-01-08 00:14:28 +08:00
|
|
|
|
verifyFormat("@throw");
|
|
|
|
|
verifyFormat("@try");
|
2013-01-08 00:07:07 +08:00
|
|
|
|
|
2013-01-07 23:56:25 +08:00
|
|
|
|
EXPECT_EQ("@interface", format("@ interface"));
|
|
|
|
|
|
|
|
|
|
// The precise formatting of this doesn't matter, nobody writes code like
|
|
|
|
|
// this.
|
|
|
|
|
verifyFormat("@ /*foo*/ interface");
|
2013-01-07 23:17:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 03:15:23 +08:00
|
|
|
|
TEST_F(FormatTest, ObjCSnippets) {
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@autoreleasepool {\n"
|
|
|
|
|
" foo();\n"
|
|
|
|
|
"}");
|
2013-01-09 01:56:31 +08:00
|
|
|
|
verifyFormat("@class Foo, Bar;");
|
2013-01-09 03:15:23 +08:00
|
|
|
|
verifyFormat("@compatibility_alias AliasName ExistingClass;");
|
2013-01-09 01:56:31 +08:00
|
|
|
|
verifyFormat("@dynamic textColor;");
|
2013-01-18 13:11:47 +08:00
|
|
|
|
verifyFormat("char *buf1 = @encode(int *);");
|
|
|
|
|
verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
|
Formatter: Correctly format stars in `sizeof(int**)` and similar places.
This redoes how '*' and '&' are classified as pointer / reference markers when
followed by ')', '>', or ','.
Previously, determineStarAmpUsage() marked a single '*' and '&' followed by
')', '>', or ',' as pointer or reference marker. Now, all '*'s and '&'s
preceding ')', '>', or ',' are marked as pointer / reference markers. Fixes
PR14884.
Since only the last '*' in 'int ***' was marked as pointer before (the rest
were unary operators, which don't reach spaceRequiredBetween()),
spaceRequiredBetween() now had to be thought about handing multiple '*'s in
sequence.
Before:
return sizeof(int * *);
Type **A = static_cast<Type * *>(P);
Now:
return sizeof(int**);
Type **A = static_cast<Type **>(P);
While here, also make all methods of AnnotatingParser except parseLine()
private.
Review URL: http://llvm-reviews.chandlerc.com/D384
llvm-svn: 174975
2013-02-13 00:17:07 +08:00
|
|
|
|
verifyFormat("char *buf1 = @encode(int **);");
|
2013-01-09 04:16:23 +08:00
|
|
|
|
verifyFormat("Protocol *proto = @protocol(p1);");
|
2013-01-18 10:43:57 +08:00
|
|
|
|
verifyFormat("SEL s = @selector(foo:);");
|
2013-01-12 03:17:44 +08:00
|
|
|
|
verifyFormat("@synchronized(self) {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"}");
|
2013-01-11 05:30:42 +08:00
|
|
|
|
|
2013-01-09 03:15:23 +08:00
|
|
|
|
verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
|
2013-01-11 05:30:42 +08:00
|
|
|
|
verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
|
2013-01-09 01:56:31 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
|
2013-01-11 05:30:42 +08:00
|
|
|
|
verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
|
|
|
|
|
verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
|
2013-06-25 08:25:40 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("@import foo.bar;\n"
|
|
|
|
|
"@import baz;");
|
2013-01-09 01:56:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 08:19:29 +08:00
|
|
|
|
TEST_F(FormatTest, ObjCLiterals) {
|
|
|
|
|
verifyFormat("@\"String\"");
|
|
|
|
|
verifyFormat("@1");
|
|
|
|
|
verifyFormat("@+4.8");
|
|
|
|
|
verifyFormat("@-4");
|
|
|
|
|
verifyFormat("@1LL");
|
|
|
|
|
verifyFormat("@.5");
|
|
|
|
|
verifyFormat("@'c'");
|
|
|
|
|
verifyFormat("@true");
|
|
|
|
|
|
|
|
|
|
verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
|
|
|
|
|
verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
|
|
|
|
|
verifyFormat("NSNumber *favoriteColor = @(Green);");
|
|
|
|
|
verifyFormat("NSString *path = @(getenv(\"PATH\"));");
|
2013-10-24 18:31:50 +08:00
|
|
|
|
}
|
2013-02-07 08:19:29 +08:00
|
|
|
|
|
2013-10-24 18:31:50 +08:00
|
|
|
|
TEST_F(FormatTest, ObjCDictLiterals) {
|
2013-02-07 08:19:29 +08:00
|
|
|
|
verifyFormat("@{");
|
2013-02-11 04:35:35 +08:00
|
|
|
|
verifyFormat("@{}");
|
|
|
|
|
verifyFormat("@{ @\"one\" : @1 }");
|
|
|
|
|
verifyFormat("return @{ @\"one\" : @1 };");
|
|
|
|
|
verifyFormat("@{ @\"one\" : @1, }");
|
2013-04-12 22:13:36 +08:00
|
|
|
|
|
2013-05-29 02:50:02 +08:00
|
|
|
|
verifyFormat("@{ @\"one\" : @{ @2 : @1 } }");
|
|
|
|
|
verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }");
|
2013-04-12 22:13:36 +08:00
|
|
|
|
|
2013-02-11 04:35:35 +08:00
|
|
|
|
verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
|
|
|
|
|
verifyFormat("[self setDict:@{}");
|
|
|
|
|
verifyFormat("[self setDict:@{ @1 : @2 }");
|
|
|
|
|
verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
|
2013-02-11 04:39:05 +08:00
|
|
|
|
verifyFormat(
|
|
|
|
|
"NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };");
|
2013-02-18 19:59:17 +08:00
|
|
|
|
|
2013-02-11 04:48:24 +08:00
|
|
|
|
verifyFormat(
|
2013-10-21 00:45:46 +08:00
|
|
|
|
"NSDictionary *d = @{\n"
|
|
|
|
|
" @\"nam\" : NSUserNam(),\n"
|
|
|
|
|
" @\"dte\" : [NSDate date],\n"
|
|
|
|
|
" @\"processInfo\" : [NSProcessInfo processInfo]\n"
|
|
|
|
|
"};");
|
2013-05-26 13:39:26 +08:00
|
|
|
|
verifyFormat(
|
2013-10-21 00:45:46 +08:00
|
|
|
|
"@{\n"
|
|
|
|
|
" NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
|
|
|
|
|
"regularFont,\n"
|
|
|
|
|
"};");
|
2013-10-22 23:45:58 +08:00
|
|
|
|
verifyGoogleFormat(
|
|
|
|
|
"@{\n"
|
|
|
|
|
" NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
|
|
|
|
|
"regularFont,\n"
|
|
|
|
|
"};");
|
2013-10-24 18:31:50 +08:00
|
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
|
"};");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, ObjCArrayLiterals) {
|
|
|
|
|
verifyFormat("@[");
|
|
|
|
|
verifyFormat("@[]");
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
|
|
|
|
|
verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
|
|
|
|
|
verifyFormat("NSArray *array = @[ [foo description] ];");
|
|
|
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"NSArray *some_variable = @[\n"
|
|
|
|
|
" aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\"\n"
|
2013-10-22 23:45:58 +08:00
|
|
|
|
"];");
|
2013-10-24 18:31:50 +08:00
|
|
|
|
verifyFormat("NSArray *some_variable = @[\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
"];");
|
|
|
|
|
verifyGoogleFormat("NSArray *some_variable = @[\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\"\n"
|
|
|
|
|
"];");
|
|
|
|
|
|
|
|
|
|
// We should try to be robust in case someone forgets the "@".
|
|
|
|
|
verifyFormat("NSArray *some_variable = [\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
" @\"aaaaaaaaaaaaaaaaa\",\n"
|
|
|
|
|
"];");
|
2013-02-07 08:19:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-09 01:38:27 +08:00
|
|
|
|
TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
|
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
"a;\n"
|
|
|
|
|
"b;\n"
|
|
|
|
|
"}\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
"a;\n"
|
|
|
|
|
" b;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"}",
|
|
|
|
|
13, 2, getLLVMStyle()));
|
2013-02-09 01:38:27 +08:00
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" a;\n"
|
|
|
|
|
"b;\n"
|
|
|
|
|
"}\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" a;\n"
|
|
|
|
|
"b;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"}",
|
|
|
|
|
9, 2, getLLVMStyle()));
|
2013-02-09 01:38:27 +08:00
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
"public:\n"
|
|
|
|
|
" b;\n"
|
|
|
|
|
"}\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
"public:\n"
|
|
|
|
|
" b;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"}",
|
|
|
|
|
17, 2, getLLVMStyle()));
|
2013-02-09 01:38:27 +08:00
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
"a;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"{\n"
|
2013-05-14 17:30:02 +08:00
|
|
|
|
" b; //\n"
|
2013-02-09 01:38:27 +08:00
|
|
|
|
"}\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
"a;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"{\n"
|
2013-05-14 17:30:02 +08:00
|
|
|
|
" b; //\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"}\n"
|
|
|
|
|
"}",
|
|
|
|
|
22, 2, getLLVMStyle()));
|
2013-02-09 03:53:32 +08:00
|
|
|
|
EXPECT_EQ(" {\n"
|
2013-05-14 17:30:02 +08:00
|
|
|
|
" a; //\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" }",
|
|
|
|
|
format(" {\n"
|
2013-05-14 17:30:02 +08:00
|
|
|
|
"a; //\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
" }",
|
|
|
|
|
4, 2, getLLVMStyle()));
|
2013-02-13 00:51:23 +08:00
|
|
|
|
EXPECT_EQ("void f() {}\n"
|
2013-02-18 19:59:17 +08:00
|
|
|
|
"void g() {}",
|
|
|
|
|
format("void f() {}\n"
|
|
|
|
|
"void g() {}",
|
|
|
|
|
13, 0, getLLVMStyle()));
|
2013-03-20 22:31:47 +08:00
|
|
|
|
EXPECT_EQ("int a; // comment\n"
|
|
|
|
|
" // line 2\n"
|
|
|
|
|
"int b;",
|
|
|
|
|
format("int a; // comment\n"
|
|
|
|
|
" // line 2\n"
|
|
|
|
|
" int b;",
|
|
|
|
|
35, 0, getLLVMStyle()));
|
2013-07-12 05:27:40 +08:00
|
|
|
|
EXPECT_EQ(" int a;\n"
|
|
|
|
|
" void\n"
|
|
|
|
|
" ffffff() {\n"
|
|
|
|
|
" }",
|
|
|
|
|
format(" int a;\n"
|
|
|
|
|
"void ffffff() {}",
|
|
|
|
|
11, 0, getLLVMStyleWithColumns(11)));
|
2013-02-09 03:53:32 +08:00
|
|
|
|
}
|
2013-02-09 01:38:27 +08:00
|
|
|
|
|
2013-02-20 18:15:13 +08:00
|
|
|
|
TEST_F(FormatTest, BreakStringLiterals) {
|
|
|
|
|
EXPECT_EQ("\"some text \"\n"
|
|
|
|
|
"\"other\";",
|
|
|
|
|
format("\"some text other\";", getLLVMStyleWithColumns(12)));
|
2013-04-18 01:34:05 +08:00
|
|
|
|
EXPECT_EQ("\"some text \"\n"
|
|
|
|
|
"\"other\";",
|
|
|
|
|
format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
|
2013-02-20 18:15:13 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"#define A \\\n"
|
|
|
|
|
" \"some \" \\\n"
|
|
|
|
|
" \"text \" \\\n"
|
|
|
|
|
" \"other\";",
|
|
|
|
|
format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"#define A \\\n"
|
|
|
|
|
" \"so \" \\\n"
|
|
|
|
|
" \"text \" \\\n"
|
|
|
|
|
" \"other\";",
|
|
|
|
|
format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("\"some text\"",
|
|
|
|
|
format("\"some text\"", getLLVMStyleWithColumns(1)));
|
|
|
|
|
EXPECT_EQ("\"some text\"",
|
|
|
|
|
format("\"some text\"", getLLVMStyleWithColumns(11)));
|
|
|
|
|
EXPECT_EQ("\"some \"\n"
|
|
|
|
|
"\"text\"",
|
|
|
|
|
format("\"some text\"", getLLVMStyleWithColumns(10)));
|
|
|
|
|
EXPECT_EQ("\"some \"\n"
|
|
|
|
|
"\"text\"",
|
|
|
|
|
format("\"some text\"", getLLVMStyleWithColumns(7)));
|
2013-03-01 21:14:08 +08:00
|
|
|
|
EXPECT_EQ("\"some\"\n"
|
2013-04-15 22:28:00 +08:00
|
|
|
|
"\" tex\"\n"
|
|
|
|
|
"\"t\"",
|
2013-02-20 18:15:13 +08:00
|
|
|
|
format("\"some text\"", getLLVMStyleWithColumns(6)));
|
2013-03-05 04:03:38 +08:00
|
|
|
|
EXPECT_EQ("\"some\"\n"
|
|
|
|
|
"\" tex\"\n"
|
|
|
|
|
"\" and\"",
|
|
|
|
|
format("\"some tex and\"", getLLVMStyleWithColumns(6)));
|
|
|
|
|
EXPECT_EQ("\"some\"\n"
|
|
|
|
|
"\"/tex\"\n"
|
|
|
|
|
"\"/and\"",
|
|
|
|
|
format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
|
2013-02-20 18:15:13 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("variable =\n"
|
|
|
|
|
" \"long string \"\n"
|
|
|
|
|
" \"literal\";",
|
|
|
|
|
format("variable = \"long string literal\";",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("variable = f(\n"
|
|
|
|
|
" \"long string \"\n"
|
2013-02-27 17:47:53 +08:00
|
|
|
|
" \"literal\",\n"
|
|
|
|
|
" short,\n"
|
2013-02-20 18:15:13 +08:00
|
|
|
|
" loooooooooooooooooooong);",
|
|
|
|
|
format("variable = f(\"long string literal\", short, "
|
|
|
|
|
"loooooooooooooooooooong);",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
2013-06-17 20:59:44 +08:00
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
" b);",
|
|
|
|
|
format("f(g(\"long string literal\", a), b);",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
2013-02-20 18:15:13 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"f(\"one two\".split(\n"
|
2013-08-30 16:29:25 +08:00
|
|
|
|
" variable));",
|
2013-02-20 18:15:13 +08:00
|
|
|
|
format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("f(\"one two three four five six \"\n"
|
|
|
|
|
" \"seven\".split(\n"
|
|
|
|
|
" really_looooong_variable));",
|
|
|
|
|
format("f(\"one two three four five six seven\"."
|
|
|
|
|
"split(really_looooong_variable));",
|
|
|
|
|
getLLVMStyleWithColumns(33)));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("f(\"some \"\n"
|
|
|
|
|
" \"text\",\n"
|
|
|
|
|
" other);",
|
|
|
|
|
format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
|
2013-02-26 20:52:34 +08:00
|
|
|
|
|
|
|
|
|
// Only break as a last resort.
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaa(\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
" aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
|
2013-03-01 21:14:08 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"\"splitmea\"\n"
|
2013-03-01 21:29:19 +08:00
|
|
|
|
"\"trandomp\"\n"
|
|
|
|
|
"\"oint\"",
|
2013-03-01 21:14:08 +08:00
|
|
|
|
format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
|
2013-03-01 21:29:19 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"\"split/\"\n"
|
|
|
|
|
"\"pathat/\"\n"
|
|
|
|
|
"\"slashes\"",
|
|
|
|
|
format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
|
2013-04-25 16:56:26 +08:00
|
|
|
|
|
2013-06-19 22:22:47 +08:00
|
|
|
|
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"
|
|
|
|
|
"\"non-alphanumeric%\"\n"
|
|
|
|
|
"\"1111111111characte\"\n"
|
|
|
|
|
"\"rs\"",
|
|
|
|
|
format("\"split at "
|
|
|
|
|
"spaces/at/"
|
|
|
|
|
"slashes.at."
|
|
|
|
|
"any$non-"
|
|
|
|
|
"alphanumeric%"
|
|
|
|
|
"1111111111characte"
|
|
|
|
|
"rs\"",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
|
|
|
|
|
2013-07-12 19:37:05 +08:00
|
|
|
|
// Verify that splitting the strings understands
|
|
|
|
|
// Style::AlwaysBreakBeforeMultilineStrings.
|
|
|
|
|
EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
|
|
|
|
|
" \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
|
|
|
|
|
" \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
|
|
|
|
|
format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaa\");",
|
|
|
|
|
getGoogleStyle()));
|
2013-10-19 00:47:55 +08:00
|
|
|
|
EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
|
|
|
|
|
" \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
|
|
|
|
|
format("return \"aaaaaaaaaaaaaaaaaaaaaa "
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaa\";",
|
|
|
|
|
getGoogleStyle()));
|
2013-08-02 19:01:15 +08:00
|
|
|
|
EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
|
|
|
|
|
" \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
|
|
|
|
|
format("llvm::outs() << "
|
|
|
|
|
"\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaa\";"));
|
2013-08-23 19:57:34 +08:00
|
|
|
|
EXPECT_EQ("ffff(\n"
|
|
|
|
|
" {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
|
|
|
|
|
" \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
|
|
|
|
|
format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
|
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
|
|
|
|
|
getGoogleStyle()));
|
2013-07-12 19:37:05 +08:00
|
|
|
|
|
2013-04-25 16:56:26 +08:00
|
|
|
|
FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
|
|
|
|
|
AlignLeft.AlignEscapedNewlinesLeft = true;
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"#define A \\\n"
|
|
|
|
|
" \"some \" \\\n"
|
|
|
|
|
" \"text \" \\\n"
|
|
|
|
|
" \"other\";",
|
|
|
|
|
format("#define A \"some text other\";", AlignLeft));
|
2013-02-20 18:15:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 04:20:49 +08:00
|
|
|
|
TEST_F(FormatTest, BreaksWideStringLiterals) {
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"u8\"utf8 string \"\n"
|
|
|
|
|
"u8\"literal\";",
|
|
|
|
|
format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"u\"utf16 string \"\n"
|
|
|
|
|
"u\"literal\";",
|
|
|
|
|
format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"U\"utf32 string \"\n"
|
|
|
|
|
"U\"literal\";",
|
|
|
|
|
format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
|
|
|
|
|
EXPECT_EQ("L\"wide string \"\n"
|
|
|
|
|
"L\"literal\";",
|
|
|
|
|
format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, BreaksRawStringLiterals) {
|
|
|
|
|
EXPECT_EQ("R\"x(raw )x\"\n"
|
|
|
|
|
"R\"x(literal)x\";",
|
|
|
|
|
format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
|
|
|
|
|
EXPECT_EQ("uR\"x(raw )x\"\n"
|
|
|
|
|
"uR\"x(literal)x\";",
|
|
|
|
|
format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
|
|
|
|
|
EXPECT_EQ("u8R\"x(raw )x\"\n"
|
|
|
|
|
"u8R\"x(literal)x\";",
|
|
|
|
|
format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17)));
|
|
|
|
|
EXPECT_EQ("LR\"x(raw )x\"\n"
|
|
|
|
|
"LR\"x(literal)x\";",
|
|
|
|
|
format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
|
|
|
|
|
EXPECT_EQ("UR\"x(raw )x\"\n"
|
|
|
|
|
"UR\"x(literal)x\";",
|
|
|
|
|
format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
|
|
|
|
|
FormatStyle Style = getLLVMStyleWithColumns(20);
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"_T(\"aaaaaaaaaaaaaa\")\n"
|
|
|
|
|
"_T(\"aaaaaaaaaaaaaa\")\n"
|
|
|
|
|
"_T(\"aaaaaaaaaaaa\")",
|
|
|
|
|
format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
|
|
|
|
|
EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
|
|
|
|
|
" _T(\"aaaaaa\"),\n"
|
|
|
|
|
" z);",
|
|
|
|
|
format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
|
|
|
|
|
|
|
|
|
|
// FIXME: Handle embedded spaces in one iteration.
|
|
|
|
|
// EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
|
|
|
|
|
// "_T(\"aaaaaaaaaaaaa\")\n"
|
|
|
|
|
// "_T(\"aaaaaaaaaaaaa\")\n"
|
|
|
|
|
// "_T(\"a\")",
|
|
|
|
|
// format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
|
|
|
|
|
// getLLVMStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
|
|
|
|
|
format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-17 05:06:13 +08:00
|
|
|
|
TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
|
2013-08-30 01:32:57 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
|
|
|
|
|
format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
|
|
|
|
|
EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
|
|
|
|
|
format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
|
|
|
|
|
EXPECT_EQ("fffffffffff(g(R\"x(\n"
|
|
|
|
|
"multiline raw string literal xxxxxxxxxxxxxx\n"
|
|
|
|
|
")x\",\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" b);",
|
|
|
|
|
format("fffffffffff(g(R\"x(\n"
|
|
|
|
|
"multiline raw string literal xxxxxxxxxxxxxx\n"
|
|
|
|
|
")x\", a), b);",
|
|
|
|
|
getGoogleStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("fffffffffff(\n"
|
|
|
|
|
" g(R\"x(qqq\n"
|
|
|
|
|
"multiline raw string literal xxxxxxxxxxxxxx\n"
|
|
|
|
|
")x\",\n"
|
|
|
|
|
" a),\n"
|
|
|
|
|
" b);",
|
|
|
|
|
format("fffffffffff(g(R\"x(qqq\n"
|
|
|
|
|
"multiline raw string literal xxxxxxxxxxxxxx\n"
|
|
|
|
|
")x\", a), b);",
|
|
|
|
|
getGoogleStyleWithColumns(20)));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("fffffffffff(R\"x(\n"
|
|
|
|
|
"multiline raw string literal xxxxxxxxxxxxxx\n"
|
|
|
|
|
")x\");",
|
|
|
|
|
format("fffffffffff(R\"x(\n"
|
|
|
|
|
"multiline raw string literal xxxxxxxxxxxxxx\n"
|
|
|
|
|
")x\");",
|
|
|
|
|
getGoogleStyleWithColumns(20)));
|
|
|
|
|
EXPECT_EQ("fffffffffff(R\"x(\n"
|
|
|
|
|
"multiline raw string literal xxxxxxxxxxxxxx\n"
|
|
|
|
|
")x\" +\n"
|
|
|
|
|
" bbbbbb);",
|
|
|
|
|
format("fffffffffff(R\"x(\n"
|
|
|
|
|
"multiline raw string literal xxxxxxxxxxxxxx\n"
|
|
|
|
|
")x\" + bbbbbb);",
|
|
|
|
|
getGoogleStyleWithColumns(20)));
|
2013-07-17 05:06:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-14 19:46:10 +08:00
|
|
|
|
TEST_F(FormatTest, SkipsUnknownStringLiterals) {
|
2013-07-17 04:28:33 +08:00
|
|
|
|
verifyFormat("string a = \"unterminated;");
|
|
|
|
|
EXPECT_EQ("function(\"unterminated,\n"
|
|
|
|
|
" OtherParameter);",
|
|
|
|
|
format("function( \"unterminated,\n"
|
|
|
|
|
" OtherParameter);"));
|
2013-06-28 20:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
|
|
|
|
|
EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
|
|
|
|
|
format("#define x(_a) printf(\"foo\"_a);", getLLVMStyle()));
|
2013-06-14 19:46:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-14 17:04:24 +08:00
|
|
|
|
TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
|
|
|
|
|
EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
|
|
|
|
|
" \"ddeeefff\");",
|
|
|
|
|
format("someFunction(\"aaabbbcccdddeeefff\");",
|
|
|
|
|
getLLVMStyleWithColumns(25)));
|
|
|
|
|
EXPECT_EQ("someFunction1234567890(\n"
|
|
|
|
|
" \"aaabbbcccdddeeefff\");",
|
|
|
|
|
format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
|
|
|
|
|
getLLVMStyleWithColumns(26)));
|
|
|
|
|
EXPECT_EQ("someFunction1234567890(\n"
|
|
|
|
|
" \"aaabbbcccdddeeeff\"\n"
|
|
|
|
|
" \"f\");",
|
|
|
|
|
format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
|
|
|
|
|
getLLVMStyleWithColumns(25)));
|
|
|
|
|
EXPECT_EQ("someFunction1234567890(\n"
|
|
|
|
|
" \"aaabbbcccdddeeeff\"\n"
|
|
|
|
|
" \"f\");",
|
|
|
|
|
format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
|
|
|
|
|
getLLVMStyleWithColumns(24)));
|
2013-08-28 18:03:58 +08:00
|
|
|
|
EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
|
|
|
|
|
" \"ddde \"\n"
|
|
|
|
|
" \"efff\");",
|
|
|
|
|
format("someFunction(\"aaabbbcc ddde efff\");",
|
2013-05-14 17:04:24 +08:00
|
|
|
|
getLLVMStyleWithColumns(25)));
|
|
|
|
|
EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
|
|
|
|
|
" \"ddeeefff\");",
|
|
|
|
|
format("someFunction(\"aaabbbccc ddeeefff\");",
|
|
|
|
|
getLLVMStyleWithColumns(25)));
|
|
|
|
|
EXPECT_EQ("someFunction1234567890(\n"
|
|
|
|
|
" \"aaabb \"\n"
|
|
|
|
|
" \"cccdddeeefff\");",
|
|
|
|
|
format("someFunction1234567890(\"aaabb cccdddeeefff\");",
|
|
|
|
|
getLLVMStyleWithColumns(25)));
|
|
|
|
|
EXPECT_EQ("#define A \\\n"
|
|
|
|
|
" string s = \\\n"
|
|
|
|
|
" \"123456789\" \\\n"
|
|
|
|
|
" \"0\"; \\\n"
|
|
|
|
|
" int i;",
|
|
|
|
|
format("#define A string s = \"1234567890\"; int i;",
|
|
|
|
|
getLLVMStyleWithColumns(20)));
|
2013-08-28 18:03:58 +08:00
|
|
|
|
// FIXME: Put additional penalties on breaking at non-whitespace locations.
|
|
|
|
|
EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
|
|
|
|
|
" \"dddeeeff\"\n"
|
|
|
|
|
" \"f\");",
|
|
|
|
|
format("someFunction(\"aaabbbcc dddeeefff\");",
|
|
|
|
|
getLLVMStyleWithColumns(25)));
|
2013-05-14 17:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-09 02:59:48 +08:00
|
|
|
|
TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
|
|
|
|
|
EXPECT_EQ("\"\\a\"",
|
|
|
|
|
format("\"\\a\"", getLLVMStyleWithColumns(3)));
|
|
|
|
|
EXPECT_EQ("\"\\\"",
|
|
|
|
|
format("\"\\\"", getLLVMStyleWithColumns(2)));
|
|
|
|
|
EXPECT_EQ("\"test\"\n"
|
|
|
|
|
"\"\\n\"",
|
|
|
|
|
format("\"test\\n\"", getLLVMStyleWithColumns(7)));
|
|
|
|
|
EXPECT_EQ("\"tes\\\\\"\n"
|
|
|
|
|
"\"n\"",
|
|
|
|
|
format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
|
|
|
|
|
EXPECT_EQ("\"\\\\\\\\\"\n"
|
|
|
|
|
"\"\\n\"",
|
|
|
|
|
format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
|
|
|
|
|
EXPECT_EQ("\"\\uff01\"",
|
|
|
|
|
format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
|
|
|
|
|
EXPECT_EQ("\"\\uff01\"\n"
|
|
|
|
|
"\"test\"",
|
|
|
|
|
format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
|
|
|
|
|
EXPECT_EQ("\"\\Uff01ff02\"",
|
|
|
|
|
format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
|
|
|
|
|
EXPECT_EQ("\"\\x000000000001\"\n"
|
|
|
|
|
"\"next\"",
|
|
|
|
|
format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
|
|
|
|
|
EXPECT_EQ("\"\\x000000000001next\"",
|
|
|
|
|
format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
|
|
|
|
|
EXPECT_EQ("\"\\x000000000001\"",
|
|
|
|
|
format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
|
|
|
|
|
EXPECT_EQ("\"test\"\n"
|
|
|
|
|
"\"\\000000\"\n"
|
|
|
|
|
"\"000001\"",
|
|
|
|
|
format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
|
|
|
|
|
EXPECT_EQ("\"test\\000\"\n"
|
2013-04-15 22:28:00 +08:00
|
|
|
|
"\"00000000\"\n"
|
|
|
|
|
"\"1\"",
|
2013-03-09 02:59:48 +08:00
|
|
|
|
format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
|
2013-09-17 04:20:49 +08:00
|
|
|
|
// FIXME: We probably don't need to care about escape sequences in raw
|
|
|
|
|
// literals.
|
|
|
|
|
EXPECT_EQ("R\"(\\x)\"\n"
|
|
|
|
|
"R\"(\\x00)\"\n",
|
2013-06-28 20:51:24 +08:00
|
|
|
|
format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
|
2013-03-09 02:59:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 17:52:05 +08:00
|
|
|
|
TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
|
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" return g() {}\n"
|
|
|
|
|
" void h() {}");
|
|
|
|
|
verifyFormat("if (foo)\n"
|
|
|
|
|
" return { forgot_closing_brace();\n"
|
|
|
|
|
"test();");
|
2013-05-29 02:50:02 +08:00
|
|
|
|
verifyFormat("int a[] = { void forgot_closing_brace() { f();\n"
|
|
|
|
|
"g();\n"
|
2013-04-10 17:52:05 +08:00
|
|
|
|
"}");
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 16:42:42 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
|
|
|
|
|
verifyFormat("class X {\n"
|
|
|
|
|
" void f() {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"};",
|
|
|
|
|
getLLVMStyleWithColumns(12));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, ConfigurableIndentWidth) {
|
|
|
|
|
FormatStyle EightIndent = getLLVMStyleWithColumns(18);
|
|
|
|
|
EightIndent.IndentWidth = 8;
|
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" someFunction();\n"
|
|
|
|
|
" if (true) {\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}",
|
|
|
|
|
EightIndent);
|
|
|
|
|
verifyFormat("class X {\n"
|
|
|
|
|
" void f() {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"};",
|
|
|
|
|
EightIndent);
|
|
|
|
|
verifyFormat("int x[] = {\n"
|
|
|
|
|
" call(),\n"
|
|
|
|
|
" call(),\n"
|
|
|
|
|
"};",
|
|
|
|
|
EightIndent);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-22 09:35:36 +08:00
|
|
|
|
TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
|
2013-06-22 01:25:42 +08:00
|
|
|
|
verifyFormat("void\n"
|
|
|
|
|
"f();",
|
|
|
|
|
getLLVMStyleWithColumns(8));
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 17:22:11 +08:00
|
|
|
|
TEST_F(FormatTest, ConfigurableUseOfTab) {
|
|
|
|
|
FormatStyle Tab = getLLVMStyleWithColumns(42);
|
|
|
|
|
Tab.IndentWidth = 8;
|
2013-09-28 00:14:22 +08:00
|
|
|
|
Tab.UseTab = FormatStyle::UT_Always;
|
2013-05-13 17:22:11 +08:00
|
|
|
|
Tab.AlignEscapedNewlinesLeft = true;
|
2013-09-27 17:45:40 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("if (aaaaaaaa && // q\n"
|
|
|
|
|
" bb)\t\t// w\n"
|
|
|
|
|
"\t;",
|
|
|
|
|
format("if (aaaaaaaa &&// q\n"
|
|
|
|
|
"bb)// w\n"
|
|
|
|
|
";",
|
|
|
|
|
Tab));
|
|
|
|
|
EXPECT_EQ("if (aaa && bbb) // w\n"
|
|
|
|
|
"\t;",
|
|
|
|
|
format("if(aaa&&bbb)// w\n"
|
|
|
|
|
";",
|
|
|
|
|
Tab));
|
|
|
|
|
|
2013-05-13 17:22:11 +08:00
|
|
|
|
verifyFormat("class X {\n"
|
|
|
|
|
"\tvoid f() {\n"
|
|
|
|
|
"\t\tsomeFunction(parameter1,\n"
|
|
|
|
|
"\t\t\t parameter2);\n"
|
|
|
|
|
"\t}\n"
|
|
|
|
|
"};",
|
|
|
|
|
Tab);
|
|
|
|
|
verifyFormat("#define A \\\n"
|
|
|
|
|
"\tvoid f() { \\\n"
|
|
|
|
|
"\t\tsomeFunction( \\\n"
|
|
|
|
|
"\t\t parameter1, \\\n"
|
|
|
|
|
"\t\t parameter2); \\\n"
|
|
|
|
|
"\t}",
|
|
|
|
|
Tab);
|
2013-08-29 23:21:40 +08:00
|
|
|
|
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));
|
2013-05-28 18:01:59 +08:00
|
|
|
|
|
2013-09-05 22:08:34 +08:00
|
|
|
|
Tab.TabWidth = 4;
|
|
|
|
|
Tab.IndentWidth = 8;
|
|
|
|
|
verifyFormat("class TabWidth4Indent8 {\n"
|
|
|
|
|
"\t\tvoid f() {\n"
|
|
|
|
|
"\t\t\t\tsomeFunction(parameter1,\n"
|
|
|
|
|
"\t\t\t\t\t\t\t parameter2);\n"
|
|
|
|
|
"\t\t}\n"
|
|
|
|
|
"};",
|
|
|
|
|
Tab);
|
|
|
|
|
|
|
|
|
|
Tab.TabWidth = 4;
|
|
|
|
|
Tab.IndentWidth = 4;
|
|
|
|
|
verifyFormat("class TabWidth4Indent4 {\n"
|
|
|
|
|
"\tvoid f() {\n"
|
|
|
|
|
"\t\tsomeFunction(parameter1,\n"
|
|
|
|
|
"\t\t\t\t\t parameter2);\n"
|
|
|
|
|
"\t}\n"
|
|
|
|
|
"};",
|
|
|
|
|
Tab);
|
|
|
|
|
|
|
|
|
|
Tab.TabWidth = 8;
|
|
|
|
|
Tab.IndentWidth = 4;
|
|
|
|
|
verifyFormat("class TabWidth8Indent4 {\n"
|
|
|
|
|
" void f() {\n"
|
|
|
|
|
"\tsomeFunction(parameter1,\n"
|
|
|
|
|
"\t\t parameter2);\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"};",
|
|
|
|
|
Tab);
|
|
|
|
|
|
2013-09-10 17:38:25 +08:00
|
|
|
|
Tab.TabWidth = 8;
|
|
|
|
|
Tab.IndentWidth = 8;
|
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
"\t a\t\tcomment\n"
|
|
|
|
|
"\t in multiple lines\n"
|
|
|
|
|
" */",
|
|
|
|
|
format(" /*\t \t \n"
|
|
|
|
|
" \t \t a\t\tcomment\t \t\n"
|
|
|
|
|
" \t \t in multiple lines\t\n"
|
|
|
|
|
" \t */",
|
|
|
|
|
Tab));
|
2013-09-27 17:45:40 +08:00
|
|
|
|
|
2013-09-28 00:14:22 +08:00
|
|
|
|
Tab.UseTab = FormatStyle::UT_ForIndentation;
|
2013-10-14 08:46:35 +08:00
|
|
|
|
verifyFormat("T t[] = {\n"
|
|
|
|
|
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
|
|
|
|
|
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
"};",
|
|
|
|
|
Tab);
|
|
|
|
|
verifyFormat("enum A {\n"
|
|
|
|
|
"\ta1,\n"
|
|
|
|
|
"\ta2,\n"
|
|
|
|
|
"\ta3\n"
|
|
|
|
|
"};",
|
|
|
|
|
Tab);
|
2013-09-28 00:14:22 +08:00
|
|
|
|
EXPECT_EQ("if (aaaaaaaa && // q\n"
|
|
|
|
|
" bb) // w\n"
|
|
|
|
|
"\t;",
|
|
|
|
|
format("if (aaaaaaaa &&// q\n"
|
|
|
|
|
"bb)// w\n"
|
|
|
|
|
";",
|
|
|
|
|
Tab));
|
|
|
|
|
verifyFormat("class X {\n"
|
|
|
|
|
"\tvoid f() {\n"
|
|
|
|
|
"\t\tsomeFunction(parameter1,\n"
|
|
|
|
|
"\t\t parameter2);\n"
|
|
|
|
|
"\t}\n"
|
|
|
|
|
"};",
|
|
|
|
|
Tab);
|
|
|
|
|
verifyFormat("{\n"
|
|
|
|
|
"\tQ({\n"
|
|
|
|
|
"\t\t int a;\n"
|
|
|
|
|
"\t\t someFunction(aaaaaaaaaa,\n"
|
|
|
|
|
"\t\t bbbbbbbbb);\n"
|
|
|
|
|
"\t },\n"
|
|
|
|
|
"\t p);\n"
|
|
|
|
|
"}",
|
|
|
|
|
Tab);
|
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"\t/* aaaa\n"
|
|
|
|
|
"\t bbbb */\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"/* aaaa\n"
|
|
|
|
|
" bbbb */\n"
|
|
|
|
|
"}",
|
|
|
|
|
Tab));
|
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"\t/*\n"
|
|
|
|
|
"\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
"\t bbbbbbbbbbbbb\n"
|
|
|
|
|
"\t*/\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"/*\n"
|
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
|
|
|
|
|
"*/\n"
|
|
|
|
|
"}",
|
|
|
|
|
Tab));
|
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
"\t// bbbbbbbbbbbbb\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
|
|
|
|
|
"}",
|
|
|
|
|
Tab));
|
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"\t/*\n"
|
|
|
|
|
"\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
|
"\t bbbbbbbbbbbbb\n"
|
|
|
|
|
"\t*/\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"\t/*\n"
|
|
|
|
|
"\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
|
|
|
|
|
"\t*/\n"
|
|
|
|
|
"}",
|
|
|
|
|
Tab));
|
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"\t/*\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"\t*/\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"\t/*\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"\t*/\n"
|
2013-09-28 00:40:11 +08:00
|
|
|
|
"}",
|
|
|
|
|
Tab));
|
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
"\t/*\n"
|
|
|
|
|
" asdf\n"
|
|
|
|
|
"\t*/\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"\t/*\n"
|
|
|
|
|
" asdf\n"
|
|
|
|
|
"\t*/\n"
|
|
|
|
|
"}",
|
|
|
|
|
Tab));
|
2013-09-28 00:14:22 +08:00
|
|
|
|
|
|
|
|
|
Tab.UseTab = FormatStyle::UT_Never;
|
2013-09-10 17:38:25 +08:00
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
|
" a\t\tcomment\n"
|
|
|
|
|
" in multiple lines\n"
|
|
|
|
|
" */",
|
|
|
|
|
format(" /*\t \t \n"
|
|
|
|
|
" \t \t a\t\tcomment\t \t\n"
|
|
|
|
|
" \t \t in multiple lines\t\n"
|
|
|
|
|
" \t */",
|
|
|
|
|
Tab));
|
|
|
|
|
EXPECT_EQ("/* some\n"
|
|
|
|
|
" comment */",
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("int a; /* some\n"
|
|
|
|
|
"comment */",
|
|
|
|
|
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));
|
2013-05-28 18:01:59 +08:00
|
|
|
|
EXPECT_EQ("{\n"
|
|
|
|
|
" /*\n"
|
|
|
|
|
" * Comment\n"
|
|
|
|
|
" */\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("{\n"
|
|
|
|
|
"\t/*\n"
|
|
|
|
|
"\t * Comment\n"
|
|
|
|
|
"\t */\n"
|
|
|
|
|
"\t int i;\n"
|
|
|
|
|
"}"));
|
2013-05-13 17:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-10 20:29:48 +08:00
|
|
|
|
TEST_F(FormatTest, CalculatesOriginalColumn) {
|
|
|
|
|
EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
|
|
|
|
|
"q\"; /* some\n"
|
|
|
|
|
" comment */",
|
|
|
|
|
format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
|
|
|
|
|
"q\"; /* some\n"
|
|
|
|
|
" comment */",
|
|
|
|
|
getLLVMStyle()));
|
|
|
|
|
EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
|
|
|
|
|
"/* some\n"
|
|
|
|
|
" comment */",
|
|
|
|
|
format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
|
|
|
|
|
" /* some\n"
|
|
|
|
|
" comment */",
|
|
|
|
|
getLLVMStyle()));
|
|
|
|
|
EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
|
|
|
|
|
"qqq\n"
|
|
|
|
|
"/* some\n"
|
|
|
|
|
" comment */",
|
|
|
|
|
format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
|
|
|
|
|
"qqq\n"
|
|
|
|
|
" /* some\n"
|
|
|
|
|
" comment */",
|
|
|
|
|
getLLVMStyle()));
|
|
|
|
|
EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
|
|
|
|
|
"wwww; /* some\n"
|
|
|
|
|
" comment */",
|
|
|
|
|
format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
|
|
|
|
|
"wwww; /* some\n"
|
|
|
|
|
" comment */",
|
|
|
|
|
getLLVMStyle()));
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-20 20:36:34 +08:00
|
|
|
|
TEST_F(FormatTest, ConfigurableSpaceAfterControlStatementKeyword) {
|
|
|
|
|
FormatStyle NoSpace = getLLVMStyle();
|
|
|
|
|
NoSpace.SpaceAfterControlStatementKeyword = false;
|
|
|
|
|
|
|
|
|
|
verifyFormat("while(true)\n"
|
|
|
|
|
" continue;", NoSpace);
|
|
|
|
|
verifyFormat("for(;;)\n"
|
|
|
|
|
" continue;", NoSpace);
|
|
|
|
|
verifyFormat("if(true)\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"else if(true)\n"
|
|
|
|
|
" f();", NoSpace);
|
|
|
|
|
verifyFormat("do {\n"
|
|
|
|
|
" do_something();\n"
|
|
|
|
|
"} while(something());", NoSpace);
|
|
|
|
|
verifyFormat("switch(x) {\n"
|
|
|
|
|
"default:\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}", NoSpace);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
|
|
|
|
|
FormatStyle Spaces = getLLVMStyle();
|
|
|
|
|
|
|
|
|
|
Spaces.SpacesInParentheses = true;
|
|
|
|
|
verifyFormat("call( x, y, z );", Spaces);
|
|
|
|
|
verifyFormat("while ( (bool)1 )\n"
|
|
|
|
|
" continue;", Spaces);
|
|
|
|
|
verifyFormat("for ( ;; )\n"
|
|
|
|
|
" continue;", Spaces);
|
|
|
|
|
verifyFormat("if ( true )\n"
|
|
|
|
|
" f();\n"
|
|
|
|
|
"else if ( true )\n"
|
|
|
|
|
" f();", Spaces);
|
|
|
|
|
verifyFormat("do {\n"
|
|
|
|
|
" do_something( (int)i );\n"
|
|
|
|
|
"} while ( something() );", Spaces);
|
|
|
|
|
verifyFormat("switch ( x ) {\n"
|
|
|
|
|
"default:\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}", Spaces);
|
|
|
|
|
|
|
|
|
|
Spaces.SpacesInParentheses = false;
|
|
|
|
|
Spaces.SpacesInCStyleCastParentheses = true;
|
|
|
|
|
verifyFormat("Type *A = ( Type * )P;", Spaces);
|
|
|
|
|
verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
|
|
|
|
|
verifyFormat("x = ( int32 )y;", Spaces);
|
|
|
|
|
verifyFormat("int a = ( int )(2.0f);", Spaces);
|
|
|
|
|
verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
|
|
|
|
|
verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
|
|
|
|
|
verifyFormat("#define x (( int )-1)", Spaces);
|
|
|
|
|
|
|
|
|
|
Spaces.SpacesInParentheses = false;
|
|
|
|
|
Spaces.SpaceInEmptyParentheses = true;
|
|
|
|
|
verifyFormat("call(x, y, z);", Spaces);
|
|
|
|
|
verifyFormat("call( )", Spaces);
|
|
|
|
|
|
|
|
|
|
// Run the first set of tests again with
|
|
|
|
|
// Spaces.SpacesInParentheses = false,
|
|
|
|
|
// Spaces.SpaceInEmptyParentheses = true and
|
|
|
|
|
// Spaces.SpacesInCStyleCastParentheses = true
|
|
|
|
|
Spaces.SpacesInParentheses = false,
|
|
|
|
|
Spaces.SpaceInEmptyParentheses = true;
|
|
|
|
|
Spaces.SpacesInCStyleCastParentheses = true;
|
|
|
|
|
verifyFormat("call(x, y, z);", Spaces);
|
|
|
|
|
verifyFormat("while (( bool )1)\n"
|
|
|
|
|
" continue;", Spaces);
|
|
|
|
|
verifyFormat("for (;;)\n"
|
|
|
|
|
" continue;", Spaces);
|
|
|
|
|
verifyFormat("if (true)\n"
|
|
|
|
|
" f( );\n"
|
|
|
|
|
"else if (true)\n"
|
|
|
|
|
" f( );", Spaces);
|
|
|
|
|
verifyFormat("do {\n"
|
|
|
|
|
" do_something(( int )i);\n"
|
|
|
|
|
"} while (something( ));", Spaces);
|
|
|
|
|
verifyFormat("switch (x) {\n"
|
|
|
|
|
"default:\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"}", Spaces);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-25 23:15:02 +08:00
|
|
|
|
TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
|
|
|
|
|
verifyFormat("int a = 5;");
|
|
|
|
|
verifyFormat("a += 42;");
|
|
|
|
|
verifyFormat("a or_eq 8;");
|
|
|
|
|
|
|
|
|
|
FormatStyle Spaces = getLLVMStyle();
|
|
|
|
|
Spaces.SpaceBeforeAssignmentOperators = false;
|
|
|
|
|
verifyFormat("int a= 5;", Spaces);
|
|
|
|
|
verifyFormat("a+= 42;", Spaces);
|
|
|
|
|
verifyFormat("a or_eq 8;", Spaces);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 20:51:40 +08:00
|
|
|
|
TEST_F(FormatTest, LinuxBraceBreaking) {
|
|
|
|
|
FormatStyle BreakBeforeBrace = getLLVMStyle();
|
|
|
|
|
BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
|
|
|
|
|
verifyFormat("namespace a\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
"class A\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" void f()\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" if (true) {\n"
|
|
|
|
|
" a();\n"
|
|
|
|
|
" b();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" }\n"
|
2013-05-15 16:30:06 +08:00
|
|
|
|
" void g()\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" return;\n"
|
|
|
|
|
" }\n"
|
2013-05-13 20:51:40 +08:00
|
|
|
|
"}\n"
|
|
|
|
|
"}",
|
|
|
|
|
BreakBeforeBrace);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, StroustrupBraceBreaking) {
|
|
|
|
|
FormatStyle BreakBeforeBrace = getLLVMStyle();
|
|
|
|
|
BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
|
|
|
|
|
verifyFormat("namespace a {\n"
|
|
|
|
|
"class A {\n"
|
|
|
|
|
" void f()\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" if (true) {\n"
|
|
|
|
|
" a();\n"
|
|
|
|
|
" b();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" }\n"
|
2013-05-15 16:30:06 +08:00
|
|
|
|
" void g()\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" return;\n"
|
|
|
|
|
" }\n"
|
2013-05-13 20:51:40 +08:00
|
|
|
|
"}\n"
|
|
|
|
|
"}",
|
|
|
|
|
BreakBeforeBrace);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-03 05:31:59 +08:00
|
|
|
|
TEST_F(FormatTest, AllmanBraceBreaking) {
|
|
|
|
|
FormatStyle BreakBeforeBrace = getLLVMStyle();
|
|
|
|
|
BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman;
|
|
|
|
|
verifyFormat("namespace a\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
"class A\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" void f()\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" if (true)\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" a();\n"
|
|
|
|
|
" b();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" void g()\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" return;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"}",
|
|
|
|
|
BreakBeforeBrace);
|
|
|
|
|
|
|
|
|
|
verifyFormat("void f()\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" if (true)\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" a();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" else if (false)\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" b();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" else\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" c();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
BreakBeforeBrace);
|
|
|
|
|
|
|
|
|
|
verifyFormat("void f()\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" for (int i = 0; i < 10; ++i)\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" a();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" while (false)\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" b();\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" do\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" c();\n"
|
|
|
|
|
" } while (false)\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
BreakBeforeBrace);
|
|
|
|
|
|
|
|
|
|
verifyFormat("void f(int a)\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" switch (a)\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" case 0:\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" case 1:\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" case 2:\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" default:\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
BreakBeforeBrace);
|
2013-08-08 03:20:45 +08:00
|
|
|
|
|
|
|
|
|
verifyFormat("enum X\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" Y = 0,\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
BreakBeforeBrace);
|
|
|
|
|
|
|
|
|
|
FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
|
|
|
|
|
BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
|
|
|
|
|
BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
|
|
|
|
|
verifyFormat("void f(bool b)\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" if (b)\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" return;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
BreakBeforeBraceShortIfs);
|
|
|
|
|
verifyFormat("void f(bool b)\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" if (b) return;\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
BreakBeforeBraceShortIfs);
|
|
|
|
|
verifyFormat("void f(bool b)\n"
|
|
|
|
|
"{\n"
|
|
|
|
|
" while (b)\n"
|
|
|
|
|
" {\n"
|
|
|
|
|
" return;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
BreakBeforeBraceShortIfs);
|
2013-08-03 05:31:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-12 11:51:17 +08:00
|
|
|
|
TEST_F(FormatTest, CatchExceptionReferenceBinding) {
|
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" try {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" catch (const Exception &e) {\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
"}\n",
|
|
|
|
|
getLLVMStyle());
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 00:29:23 +08:00
|
|
|
|
TEST_F(FormatTest, UnderstandsPragmas) {
|
|
|
|
|
verifyFormat("#pragma omp reduction(| : var)");
|
|
|
|
|
verifyFormat("#pragma omp reduction(+ : var)");
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 23:32:14 +08:00
|
|
|
|
bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
|
|
|
|
|
for (size_t i = 1; i < Styles.size(); ++i)
|
|
|
|
|
if (!(Styles[0] == Styles[i]))
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, GetsPredefinedStyleByName) {
|
2013-05-19 08:53:30 +08:00
|
|
|
|
FormatStyle Styles[3];
|
2013-05-07 23:32:14 +08:00
|
|
|
|
|
2013-05-19 08:53:30 +08:00
|
|
|
|
Styles[0] = getLLVMStyle();
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("LLVM", &Styles[1]));
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("lLvM", &Styles[2]));
|
|
|
|
|
EXPECT_TRUE(allStylesEqual(Styles));
|
2013-05-07 23:32:14 +08:00
|
|
|
|
|
2013-05-19 08:53:30 +08:00
|
|
|
|
Styles[0] = getGoogleStyle();
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("Google", &Styles[1]));
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("gOOgle", &Styles[2]));
|
|
|
|
|
EXPECT_TRUE(allStylesEqual(Styles));
|
2013-05-07 23:32:14 +08:00
|
|
|
|
|
2013-05-19 08:53:30 +08:00
|
|
|
|
Styles[0] = getChromiumStyle();
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("Chromium", &Styles[1]));
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", &Styles[2]));
|
|
|
|
|
EXPECT_TRUE(allStylesEqual(Styles));
|
|
|
|
|
|
|
|
|
|
Styles[0] = getMozillaStyle();
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("Mozilla", &Styles[1]));
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("moZILla", &Styles[2]));
|
|
|
|
|
EXPECT_TRUE(allStylesEqual(Styles));
|
|
|
|
|
|
2013-07-24 21:10:59 +08:00
|
|
|
|
Styles[0] = getWebKitStyle();
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("WebKit", &Styles[1]));
|
|
|
|
|
EXPECT_TRUE(getPredefinedStyle("wEbKit", &Styles[2]));
|
|
|
|
|
EXPECT_TRUE(allStylesEqual(Styles));
|
|
|
|
|
|
2013-05-19 08:53:30 +08:00
|
|
|
|
EXPECT_FALSE(getPredefinedStyle("qwerty", &Styles[0]));
|
2013-05-07 23:32:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, ParsesConfiguration) {
|
|
|
|
|
FormatStyle Style = {};
|
|
|
|
|
#define CHECK_PARSE(TEXT, FIELD, VALUE) \
|
|
|
|
|
EXPECT_NE(VALUE, Style.FIELD); \
|
|
|
|
|
EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
|
|
|
|
|
EXPECT_EQ(VALUE, Style.FIELD)
|
|
|
|
|
|
|
|
|
|
#define CHECK_PARSE_BOOL(FIELD) \
|
|
|
|
|
Style.FIELD = false; \
|
|
|
|
|
EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \
|
2013-05-19 08:53:30 +08:00
|
|
|
|
EXPECT_TRUE(Style.FIELD); \
|
2013-05-07 23:32:14 +08:00
|
|
|
|
EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \
|
2013-05-14 15:53:53 +08:00
|
|
|
|
EXPECT_FALSE(Style.FIELD);
|
2013-05-07 23:32:14 +08:00
|
|
|
|
|
|
|
|
|
CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
|
2013-08-01 07:55:15 +08:00
|
|
|
|
CHECK_PARSE_BOOL(AlignTrailingComments);
|
2013-05-07 23:32:14 +08:00
|
|
|
|
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
|
|
|
|
|
CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
|
2013-05-16 20:16:23 +08:00
|
|
|
|
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
|
2013-05-29 20:07:31 +08:00
|
|
|
|
CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
|
2013-05-07 23:32:14 +08:00
|
|
|
|
CHECK_PARSE_BOOL(BinPackParameters);
|
2013-07-27 00:56:36 +08:00
|
|
|
|
CHECK_PARSE_BOOL(BreakBeforeBinaryOperators);
|
|
|
|
|
CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
|
2013-05-07 23:32:14 +08:00
|
|
|
|
CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
|
|
|
|
|
CHECK_PARSE_BOOL(DerivePointerBinding);
|
|
|
|
|
CHECK_PARSE_BOOL(IndentCaseLabels);
|
|
|
|
|
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
|
|
|
|
|
CHECK_PARSE_BOOL(PointerBindsToType);
|
2013-07-17 02:22:10 +08:00
|
|
|
|
CHECK_PARSE_BOOL(Cpp11BracedListStyle);
|
2013-06-22 01:25:42 +08:00
|
|
|
|
CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
|
2013-08-20 20:36:34 +08:00
|
|
|
|
CHECK_PARSE_BOOL(SpacesInParentheses);
|
2013-10-29 22:52:02 +08:00
|
|
|
|
CHECK_PARSE_BOOL(SpacesInAngles);
|
2013-08-20 20:36:34 +08:00
|
|
|
|
CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
|
|
|
|
|
CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
|
|
|
|
|
CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);
|
2013-09-25 23:15:02 +08:00
|
|
|
|
CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
|
2013-05-07 23:32:14 +08:00
|
|
|
|
|
|
|
|
|
CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
|
2013-08-13 18:58:30 +08:00
|
|
|
|
CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
|
|
|
|
|
ConstructorInitializerIndentWidth, 1234u);
|
2013-05-07 23:32:14 +08:00
|
|
|
|
CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
|
|
|
|
|
CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
|
2013-10-25 22:29:37 +08:00
|
|
|
|
CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
|
|
|
|
|
PenaltyBreakBeforeFirstCallParameter, 1234u);
|
2013-05-07 23:32:14 +08:00
|
|
|
|
CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
|
|
|
|
|
CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
|
|
|
|
|
PenaltyReturnTypeOnItsOwnLine, 1234u);
|
|
|
|
|
CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
|
|
|
|
|
SpacesBeforeTrailingComments, 1234u);
|
2013-05-13 16:42:42 +08:00
|
|
|
|
CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
|
2013-10-18 18:38:14 +08:00
|
|
|
|
CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
|
2013-05-07 23:32:14 +08:00
|
|
|
|
|
|
|
|
|
Style.Standard = FormatStyle::LS_Auto;
|
2013-09-04 22:09:13 +08:00
|
|
|
|
CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
|
|
|
|
|
CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
|
2013-05-07 23:32:14 +08:00
|
|
|
|
CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
|
|
|
|
|
CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
|
|
|
|
|
CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
|
|
|
|
|
|
2013-09-28 00:14:22 +08:00
|
|
|
|
Style.UseTab = FormatStyle::UT_ForIndentation;
|
|
|
|
|
CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
|
|
|
|
|
CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
|
|
|
|
|
CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
|
|
|
|
|
CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
|
|
|
|
|
CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
|
|
|
|
|
|
2013-05-07 23:32:14 +08:00
|
|
|
|
Style.ColumnLimit = 123;
|
|
|
|
|
FormatStyle BaseStyle = getLLVMStyle();
|
|
|
|
|
CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
|
|
|
|
|
CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
|
|
|
|
|
|
2013-05-13 20:51:40 +08:00
|
|
|
|
Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
|
|
|
|
|
CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
|
|
|
|
|
FormatStyle::BS_Attach);
|
|
|
|
|
CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
|
|
|
|
|
FormatStyle::BS_Linux);
|
|
|
|
|
CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
|
|
|
|
|
FormatStyle::BS_Stroustrup);
|
|
|
|
|
|
2013-08-01 07:16:02 +08:00
|
|
|
|
Style.NamespaceIndentation = FormatStyle::NI_All;
|
|
|
|
|
CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
|
|
|
|
|
FormatStyle::NI_None);
|
|
|
|
|
CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
|
|
|
|
|
FormatStyle::NI_Inner);
|
|
|
|
|
CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
|
|
|
|
|
FormatStyle::NI_All);
|
|
|
|
|
|
2013-05-07 23:32:14 +08:00
|
|
|
|
#undef CHECK_PARSE
|
|
|
|
|
#undef CHECK_PARSE_BOOL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, ConfigurationRoundTripTest) {
|
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
|
|
|
|
std::string YAML = configurationAsText(Style);
|
|
|
|
|
FormatStyle ParsedStyle = {};
|
|
|
|
|
EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
|
|
|
|
|
EXPECT_EQ(Style, ParsedStyle);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-05 22:09:10 +08:00
|
|
|
|
TEST_F(FormatTest, WorksFor8bitEncodings) {
|
|
|
|
|
EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
|
|
|
|
|
"\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
|
|
|
|
|
"\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
|
|
|
|
|
"\"\xef\xee\xf0\xf3...\"",
|
|
|
|
|
format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
|
|
|
|
|
"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
|
|
|
|
|
"\xef\xee\xf0\xf3...\"",
|
|
|
|
|
getLLVMStyleWithColumns(12)));
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-06 09:14:58 +08:00
|
|
|
|
// FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
|
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
|
|
2013-06-05 22:09:10 +08:00
|
|
|
|
TEST_F(FormatTest, CountsUTF8CharactersProperly) {
|
|
|
|
|
verifyFormat("\"Однажды в студёную зимнюю пору...\"",
|
|
|
|
|
getLLVMStyleWithColumns(35));
|
|
|
|
|
verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
|
2013-09-05 22:08:34 +08:00
|
|
|
|
getLLVMStyleWithColumns(31));
|
2013-06-05 22:09:10 +08:00
|
|
|
|
verifyFormat("// Однажды в студёную зимнюю пору...",
|
|
|
|
|
getLLVMStyleWithColumns(36));
|
|
|
|
|
verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
|
2013-09-05 22:08:34 +08:00
|
|
|
|
getLLVMStyleWithColumns(32));
|
2013-06-05 22:09:10 +08:00
|
|
|
|
verifyFormat("/* Однажды в студёную зимнюю пору... */",
|
|
|
|
|
getLLVMStyleWithColumns(39));
|
|
|
|
|
verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
|
2013-09-05 22:08:34 +08:00
|
|
|
|
getLLVMStyleWithColumns(35));
|
2013-06-05 22:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, SplitsUTF8Strings) {
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"\"Однажды, в \"\n"
|
|
|
|
|
"\"студёную \"\n"
|
|
|
|
|
"\"зимнюю \"\n"
|
|
|
|
|
"\"пору,\"",
|
|
|
|
|
format("\"Однажды, в студёную зимнюю пору,\"",
|
|
|
|
|
getLLVMStyleWithColumns(13)));
|
2013-09-05 22:08:34 +08:00
|
|
|
|
EXPECT_EQ("\"一 二 三 \"\n"
|
|
|
|
|
"\"四 五六 \"\n"
|
|
|
|
|
"\"七 八 九 \"\n"
|
|
|
|
|
"\"十\"",
|
|
|
|
|
format("\"一 二 三 四 五六 七 八 九 十\"",
|
|
|
|
|
getLLVMStyleWithColumns(11)));
|
|
|
|
|
EXPECT_EQ("\"一\t二 \"\n"
|
|
|
|
|
"\"\t三 \"\n"
|
|
|
|
|
"\"四 五\t六 \"\n"
|
|
|
|
|
"\"\t七 \"\n"
|
|
|
|
|
"\"八九十\tqq\"",
|
|
|
|
|
format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
|
|
|
|
|
getLLVMStyleWithColumns(11)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
|
|
|
|
|
EXPECT_EQ("const char *sssss =\n"
|
|
|
|
|
" \"一二三四五六七八\\\n"
|
|
|
|
|
" 九 十\";",
|
|
|
|
|
format("const char *sssss = \"一二三四五六七八\\\n"
|
|
|
|
|
" 九 十\";",
|
|
|
|
|
getLLVMStyleWithColumns(30)));
|
2013-06-05 22:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, SplitsUTF8LineComments) {
|
|
|
|
|
EXPECT_EQ("// Я из лесу\n"
|
|
|
|
|
"// вышел; был\n"
|
|
|
|
|
"// сильный\n"
|
|
|
|
|
"// мороз.",
|
|
|
|
|
format("// Я из лесу вышел; был сильный мороз.",
|
|
|
|
|
getLLVMStyleWithColumns(13)));
|
|
|
|
|
EXPECT_EQ("// 一二三\n"
|
|
|
|
|
"// 四五六七\n"
|
2013-09-05 22:08:34 +08:00
|
|
|
|
"// 八 九\n"
|
|
|
|
|
"// 十",
|
|
|
|
|
format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
|
2013-06-05 22:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(FormatTest, SplitsUTF8BlockComments) {
|
|
|
|
|
EXPECT_EQ("/* Гляжу,\n"
|
|
|
|
|
" * поднимается\n"
|
|
|
|
|
" * медленно в\n"
|
|
|
|
|
" * гору\n"
|
|
|
|
|
" * Лошадка,\n"
|
|
|
|
|
" * везущая\n"
|
|
|
|
|
" * хворосту\n"
|
|
|
|
|
" * воз. */",
|
|
|
|
|
format("/* Гляжу, поднимается медленно в гору\n"
|
|
|
|
|
" * Лошадка, везущая хворосту воз. */",
|
|
|
|
|
getLLVMStyleWithColumns(13)));
|
2013-09-05 22:08:34 +08:00
|
|
|
|
EXPECT_EQ(
|
|
|
|
|
"/* 一二三\n"
|
|
|
|
|
" * 四五六七\n"
|
|
|
|
|
" * 八 九\n"
|
|
|
|
|
" * 十 */",
|
|
|
|
|
format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
|
2013-06-05 23:08:20 +08:00
|
|
|
|
EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
|
|
|
|
|
" * 𝕓𝕪𝕥𝕖\n"
|
|
|
|
|
" * 𝖀𝕿𝕱-𝟠 */",
|
|
|
|
|
format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
|
2013-06-05 22:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-05 22:08:34 +08:00
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
|
2013-08-13 18:58:30 +08:00
|
|
|
|
TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
|
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
|
|
|
|
|
|
|
|
|
Style.ConstructorInitializerIndentWidth = 4;
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"SomeClass::Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
|
|
|
|
|
Style);
|
|
|
|
|
|
|
|
|
|
Style.ConstructorInitializerIndentWidth = 2;
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"SomeClass::Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
|
|
|
|
|
Style);
|
|
|
|
|
|
|
|
|
|
Style.ConstructorInitializerIndentWidth = 0;
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"SomeClass::Constructor()\n"
|
|
|
|
|
": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
|
|
|
|
|
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
|
|
|
|
|
Style);
|
|
|
|
|
|
|
|
|
|
Style.BreakConstructorInitializersBeforeComma = true;
|
|
|
|
|
Style.ConstructorInitializerIndentWidth = 4;
|
|
|
|
|
verifyFormat("SomeClass::Constructor()\n"
|
|
|
|
|
" : a(a)\n"
|
|
|
|
|
" , b(b)\n"
|
|
|
|
|
" , c(c) {}",
|
|
|
|
|
Style);
|
|
|
|
|
|
|
|
|
|
Style.ConstructorInitializerIndentWidth = 2;
|
|
|
|
|
verifyFormat("SomeClass::Constructor()\n"
|
|
|
|
|
" : a(a)\n"
|
|
|
|
|
" , b(b)\n"
|
|
|
|
|
" , c(c) {}",
|
|
|
|
|
Style);
|
|
|
|
|
|
|
|
|
|
Style.ConstructorInitializerIndentWidth = 0;
|
|
|
|
|
verifyFormat("SomeClass::Constructor()\n"
|
|
|
|
|
": a(a)\n"
|
|
|
|
|
", b(b)\n"
|
|
|
|
|
", c(c) {}",
|
|
|
|
|
Style);
|
2013-10-08 13:11:18 +08:00
|
|
|
|
|
|
|
|
|
Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
|
|
|
|
|
Style.ConstructorInitializerIndentWidth = 4;
|
|
|
|
|
verifyFormat(
|
|
|
|
|
"SomeClass::Constructor()\n"
|
|
|
|
|
" : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
|
|
|
|
|
Style);
|
|
|
|
|
Style.ConstructorInitializerIndentWidth = 4;
|
|
|
|
|
Style.ColumnLimit = 60;
|
|
|
|
|
verifyFormat("SomeClass::Constructor()\n"
|
|
|
|
|
" : aaaaaaaa(aaaaaaaa)\n"
|
|
|
|
|
" , aaaaaaaa(aaaaaaaa)\n"
|
|
|
|
|
" , aaaaaaaa(aaaaaaaa) {}",
|
|
|
|
|
Style);
|
2013-08-13 18:58:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-24 21:10:59 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsWithWebKitStyle) {
|
|
|
|
|
FormatStyle Style = getWebKitStyle();
|
|
|
|
|
|
|
|
|
|
// Don't indent in outer namespaces.
|
|
|
|
|
verifyFormat("namespace outer {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"namespace inner {\n"
|
2013-08-01 07:16:02 +08:00
|
|
|
|
" int i;\n"
|
2013-07-24 21:10:59 +08:00
|
|
|
|
"} // namespace inner\n"
|
|
|
|
|
"} // namespace outer\n"
|
|
|
|
|
"namespace other_outer {\n"
|
|
|
|
|
"int i;\n"
|
|
|
|
|
"}",
|
|
|
|
|
Style);
|
|
|
|
|
|
|
|
|
|
// Don't indent case labels.
|
|
|
|
|
verifyFormat("switch (variable) {\n"
|
|
|
|
|
"case 1:\n"
|
|
|
|
|
"case 2:\n"
|
|
|
|
|
" doSomething();\n"
|
|
|
|
|
" break;\n"
|
|
|
|
|
"default:\n"
|
|
|
|
|
" ++variable;\n"
|
|
|
|
|
"}",
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
// Constructor initializers are formatted one per line with the "," on the
|
|
|
|
|
// new line.
|
2013-07-27 00:56:36 +08:00
|
|
|
|
verifyFormat("Constructor()\n"
|
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
|
" , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
|
|
|
|
|
" aaaaaaaaaaaaaa)\n"
|
|
|
|
|
" , aaaaaaaaaaaaaaaaaaaaaaa()\n{\n}",
|
|
|
|
|
Style);
|
2013-07-24 21:10:59 +08:00
|
|
|
|
|
2013-08-01 07:16:02 +08:00
|
|
|
|
// Access specifiers should be aligned left.
|
|
|
|
|
verifyFormat("class C {\n"
|
|
|
|
|
"public:\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
"};",
|
|
|
|
|
Style);
|
|
|
|
|
|
2013-07-24 21:10:59 +08:00
|
|
|
|
// Do not align comments.
|
2013-08-01 07:55:15 +08:00
|
|
|
|
verifyFormat("int a; // Do not\n"
|
|
|
|
|
"double b; // align comments.",
|
|
|
|
|
Style);
|
2013-07-24 21:10:59 +08:00
|
|
|
|
|
|
|
|
|
// Accept input's line breaks.
|
|
|
|
|
EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
|
|
|
|
|
" || bbbbbbbbbbbbbbb) {\n"
|
|
|
|
|
" i++;\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("if (aaaaaaaaaaaaaaa\n"
|
|
|
|
|
"|| bbbbbbbbbbbbbbb) { i++; }",
|
|
|
|
|
Style));
|
|
|
|
|
EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
|
|
|
|
|
" i++;\n"
|
|
|
|
|
"}",
|
|
|
|
|
format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 18:36:58 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsProtocolBufferDefinitions) {
|
|
|
|
|
// It seems that clang-format can format protocol buffer definitions
|
|
|
|
|
// (see https://code.google.com/p/protobuf/).
|
|
|
|
|
verifyFormat("message SomeMessage {\n"
|
|
|
|
|
" required int32 field1 = 1;\n"
|
|
|
|
|
" optional string field2 = 2 [default = \"2\"]\n"
|
|
|
|
|
"}");
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-03 23:10:01 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsLambdas) {
|
2013-09-05 17:29:45 +08:00
|
|
|
|
verifyFormat("int c = [b]() mutable {\n"
|
|
|
|
|
" return [&b] { return b++; }();\n"
|
|
|
|
|
"}();\n");
|
|
|
|
|
verifyFormat("int c = [&] {\n"
|
|
|
|
|
" [=] { return b++; }();\n"
|
|
|
|
|
"}();\n");
|
|
|
|
|
verifyFormat("int c = [&, &a, a] {\n"
|
|
|
|
|
" [=, c, &d] { return b++; }();\n"
|
|
|
|
|
"}();\n");
|
|
|
|
|
verifyFormat("int c = [&a, &a, a] {\n"
|
|
|
|
|
" [=, a, b, &c] { return b++; }();\n"
|
|
|
|
|
"}();\n");
|
|
|
|
|
verifyFormat("auto c = { [&a, &a, a] {\n"
|
|
|
|
|
" [=, a, b, &c] { return b++; }();\n"
|
|
|
|
|
"} }\n");
|
2013-09-08 22:07:57 +08:00
|
|
|
|
verifyFormat("auto c = { [&a, &a, a] { [=, a, b, &c] {}(); } }\n");
|
2013-09-05 17:29:45 +08:00
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
|
|
|
|
|
"}\n");
|
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
|
" other(x.begin(), //\n"
|
|
|
|
|
" x.end(), //\n"
|
2013-09-05 18:04:31 +08:00
|
|
|
|
" [&](int, int) { return 1; });\n"
|
2013-09-05 17:29:45 +08:00
|
|
|
|
"}\n");
|
2013-09-05 19:49:39 +08:00
|
|
|
|
|
|
|
|
|
// Not lambdas.
|
|
|
|
|
verifyFormat("constexpr char hello[]{ \"hello\" };");
|
2013-09-07 05:25:51 +08:00
|
|
|
|
verifyFormat("double &operator[](int i) { return 0; }\n"
|
|
|
|
|
"int i;");
|
2013-09-03 23:10:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-04 21:25:30 +08:00
|
|
|
|
TEST_F(FormatTest, FormatsBlocks) {
|
|
|
|
|
// FIXME: Make whitespace formatting consistent. Ask a ObjC dev how
|
|
|
|
|
// it would ideally look.
|
2013-09-05 17:29:45 +08:00
|
|
|
|
verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];");
|
|
|
|
|
verifyFormat("int i = {[operation setCompletionBlock : ^{ [self "
|
|
|
|
|
"onOperationDone]; }] };");
|
2013-09-04 21:25:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 20:25:57 +08:00
|
|
|
|
TEST_F(FormatTest, SupportsCRLF) {
|
|
|
|
|
EXPECT_EQ("int a;\r\n"
|
|
|
|
|
"int b;\r\n"
|
|
|
|
|
"int c;\r\n",
|
|
|
|
|
format("int a;\r\n"
|
|
|
|
|
" int b;\r\n"
|
|
|
|
|
" int c;\r\n",
|
|
|
|
|
getLLVMStyle()));
|
|
|
|
|
EXPECT_EQ("int a;\r\n"
|
|
|
|
|
"int b;\r\n"
|
|
|
|
|
"int c;\r\n",
|
|
|
|
|
format("int a;\r\n"
|
|
|
|
|
" int b;\n"
|
|
|
|
|
" int c;\r\n",
|
|
|
|
|
getLLVMStyle()));
|
|
|
|
|
EXPECT_EQ("int a;\n"
|
|
|
|
|
"int b;\n"
|
|
|
|
|
"int c;\n",
|
|
|
|
|
format("int a;\r\n"
|
|
|
|
|
" int b;\n"
|
|
|
|
|
" int c;\n",
|
|
|
|
|
getLLVMStyle()));
|
|
|
|
|
EXPECT_EQ("\"aaaaaaa \"\r\n"
|
|
|
|
|
"\"bbbbbbb\";\r\n",
|
|
|
|
|
format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
|
|
|
|
|
EXPECT_EQ("#define A \\\r\n"
|
|
|
|
|
" b; \\\r\n"
|
|
|
|
|
" c; \\\r\n"
|
|
|
|
|
" d;\r\n",
|
|
|
|
|
format("#define A \\\r\n"
|
|
|
|
|
" b; \\\r\n"
|
|
|
|
|
" c; d; \r\n",
|
|
|
|
|
getGoogleStyle()));
|
2013-10-30 15:36:40 +08:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ("/*\r\n"
|
|
|
|
|
"multi line block comments\r\n"
|
|
|
|
|
"should not introduce\r\n"
|
|
|
|
|
"an extra carriage return\r\n"
|
|
|
|
|
"*/\r\n",
|
|
|
|
|
format("/*\r\n"
|
|
|
|
|
"multi line block comments\r\n"
|
|
|
|
|
"should not introduce\r\n"
|
|
|
|
|
"an extra carriage return\r\n"
|
|
|
|
|
"*/\r\n"));
|
2013-09-11 20:25:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-13 06:46:56 +08:00
|
|
|
|
TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
|
|
|
|
|
verifyFormat("MY_CLASS(C) {\n"
|
|
|
|
|
" int i;\n"
|
|
|
|
|
" int j;\n"
|
|
|
|
|
"};");
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-18 18:38:14 +08:00
|
|
|
|
TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
|
|
|
|
|
FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
|
|
|
|
|
TwoIndent.ContinuationIndentWidth = 2;
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("int i =\n"
|
|
|
|
|
" longFunction(\n"
|
|
|
|
|
" arg);",
|
|
|
|
|
format("int i = longFunction(arg);", TwoIndent));
|
|
|
|
|
|
|
|
|
|
FormatStyle SixIndent = getLLVMStyleWithColumns(20);
|
|
|
|
|
SixIndent.ContinuationIndentWidth = 6;
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("int i =\n"
|
|
|
|
|
" longFunction(\n"
|
|
|
|
|
" arg);",
|
|
|
|
|
format("int i = longFunction(arg);", SixIndent));
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-29 22:52:02 +08:00
|
|
|
|
TEST_F(FormatTest, SpacesInAngles) {
|
|
|
|
|
FormatStyle Spaces = getLLVMStyle();
|
|
|
|
|
Spaces.SpacesInAngles = true;
|
|
|
|
|
|
|
|
|
|
verifyFormat("static_cast< int >(arg);", Spaces);
|
|
|
|
|
verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
|
|
|
|
|
verifyFormat("f< int, float >();", Spaces);
|
|
|
|
|
verifyFormat("template <> g() {}", Spaces);
|
|
|
|
|
verifyFormat("template < std::vector< int > > f() {}", Spaces);
|
|
|
|
|
|
|
|
|
|
Spaces.Standard = FormatStyle::LS_Cpp03;
|
|
|
|
|
Spaces.SpacesInAngles = true;
|
|
|
|
|
verifyFormat("A< A< int > >();", Spaces);
|
|
|
|
|
|
|
|
|
|
Spaces.SpacesInAngles = false;
|
|
|
|
|
verifyFormat("A<A<int> >();", Spaces);
|
|
|
|
|
|
|
|
|
|
Spaces.Standard = FormatStyle::LS_Cpp11;
|
|
|
|
|
Spaces.SpacesInAngles = true;
|
|
|
|
|
verifyFormat("A< A< int > >();", Spaces);
|
|
|
|
|
|
|
|
|
|
Spaces.SpacesInAngles = false;
|
|
|
|
|
verifyFormat("A<A<int>>();", Spaces);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-07 21:26:07 +08:00
|
|
|
|
} // end namespace tooling
|
|
|
|
|
} // end namespace clang
|