blob: b05d19c6529b72a50372e4f9c57bbb1749bc540a [file] [log] [blame]
Daniel Jasperbac016b2012-12-03 18:12:45 +00001//===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth1050e8b2012-12-04 09:45:34 +000010#include "clang/Format/Format.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000011#include "../Tooling/RewriterTestContext.h"
12#include "clang/Lex/Lexer.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000013#include "gtest/gtest.h"
14
15namespace clang {
16namespace format {
17
18class FormatTest : public ::testing::Test {
19protected:
20 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
21 const FormatStyle &Style) {
22 RewriterTestContext Context;
23 FileID ID = Context.createInMemoryFile("input.cc", Code);
24 SourceLocation Start =
25 Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
26 std::vector<CharSourceRange> Ranges(
27 1,
28 CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
Daniel Jasper46ef8522013-01-10 13:08:12 +000029 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
30 getFormattingLangOpts());
Daniel Jaspercd162382013-01-07 13:26:07 +000031 tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
32 Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +000033 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
34 return Context.getRewrittenText(ID);
35 }
36
37 std::string format(llvm::StringRef Code,
38 const FormatStyle &Style = getLLVMStyle()) {
39 return format(Code, 0, Code.size(), Style);
40 }
41
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000042 std::string messUp(llvm::StringRef Code) {
43 std::string MessedUp(Code.str());
44 bool InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000045 bool InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000046 bool JustReplacedNewline = false;
47 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
48 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
49 if (JustReplacedNewline)
50 MessedUp[i - 1] = '\n';
51 InComment = true;
Manuel Klimek526ed112013-01-09 15:25:02 +000052 } else if (MessedUp[i] == '#' && JustReplacedNewline) {
53 MessedUp[i - 1] = '\n';
54 InPreprocessorDirective = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +000055 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
56 MessedUp[i] = ' ';
Manuel Klimek526ed112013-01-09 15:25:02 +000057 MessedUp[i + 1] = ' ';
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000058 } else if (MessedUp[i] == '\n') {
59 if (InComment) {
60 InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000061 } else if (InPreprocessorDirective) {
62 InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000063 } else {
64 JustReplacedNewline = true;
65 MessedUp[i] = ' ';
66 }
Manuel Klimekd4397b92013-01-04 23:34:14 +000067 } else if (MessedUp[i] != ' ') {
68 JustReplacedNewline = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000069 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000070 }
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000071 return MessedUp;
72 }
73
Manuel Klimek060143e2013-01-02 18:33:23 +000074 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
75 FormatStyle Style = getLLVMStyle();
76 Style.ColumnLimit = ColumnLimit;
77 return Style;
78 }
79
80 void verifyFormat(llvm::StringRef Code,
81 const FormatStyle &Style = getLLVMStyle()) {
82 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000083 }
84
85 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +000086 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +000087 }
88};
89
Manuel Klimek526ed112013-01-09 15:25:02 +000090TEST_F(FormatTest, MessUp) {
91 EXPECT_EQ("1 2 3", messUp("1 2 3"));
92 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
93 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
94 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
95 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
96}
97
Alexander Kornienko15757312012-12-06 18:03:27 +000098//===----------------------------------------------------------------------===//
99// Basic function tests.
100//===----------------------------------------------------------------------===//
101
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
103 EXPECT_EQ(";", format(";"));
104}
105
106TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
107 EXPECT_EQ("int i;", format(" int i;"));
108 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
109 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
110 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
111}
112
113TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
114 EXPECT_EQ("int i;", format("int\ni;"));
115}
116
117TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000118 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000119}
120
Alexander Kornienko15757312012-12-06 18:03:27 +0000121TEST_F(FormatTest, FormatsNestedCall) {
122 verifyFormat("Method(f1, f2(f3));");
123 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper4e9008a2013-01-13 08:19:51 +0000124 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000125}
126
Alexander Kornienko15757312012-12-06 18:03:27 +0000127//===----------------------------------------------------------------------===//
128// Tests for control statements.
129//===----------------------------------------------------------------------===//
130
131TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
132 verifyFormat("if (true)\n f();\ng();");
133 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
134 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000135 verifyFormat("if (a)\n"
136 " // comment\n"
137 " f();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000138}
139
140TEST_F(FormatTest, ParseIfElse) {
141 verifyFormat("if (true)\n"
142 " if (true)\n"
143 " if (true)\n"
144 " f();\n"
145 " else\n"
146 " g();\n"
147 " else\n"
148 " h();\n"
149 "else\n"
150 " i();");
151 verifyFormat("if (true)\n"
152 " if (true)\n"
153 " if (true) {\n"
154 " if (true)\n"
155 " f();\n"
156 " } else {\n"
157 " g();\n"
158 " }\n"
159 " else\n"
160 " h();\n"
161 "else {\n"
162 " i();\n"
163 "}");
164}
165
166TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000167 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000168 verifyFormat("if (a)\n"
169 " f();\n"
170 "else if (b)\n"
171 " g();\n"
172 "else\n"
173 " h();");
174}
175
Daniel Jasperbac016b2012-12-03 18:12:45 +0000176TEST_F(FormatTest, FormatsForLoop) {
177 verifyFormat(
178 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000179 " ++VeryVeryLongLoopVariable)\n"
180 " ;");
181 verifyFormat("for (;;)\n"
182 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000183 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000184 verifyFormat("for (;;) {\n"
185 " f();\n"
186 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000187
188 verifyFormat(
189 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
190 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000191 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000192
193 verifyFormat(
194 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000195 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000196}
197
198TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000199 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000200 verifyFormat("while (true)\n"
201 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000202 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000203 verifyFormat("while () {\n"
204 " f();\n"
205 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000206}
207
Alexander Kornienko15757312012-12-06 18:03:27 +0000208TEST_F(FormatTest, FormatsDoWhile) {
209 verifyFormat("do {\n"
210 " do_something();\n"
211 "} while (something());");
212 verifyFormat("do\n"
213 " do_something();\n"
214 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000215}
216
Alexander Kornienko15757312012-12-06 18:03:27 +0000217TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000218 verifyFormat("switch (x) {\n"
219 "case 1:\n"
220 " f();\n"
221 " break;\n"
222 "case kFoo:\n"
223 "case ns::kBar:\n"
224 "case kBaz:\n"
225 " break;\n"
226 "default:\n"
227 " g();\n"
228 " break;\n"
229 "}");
230 verifyFormat("switch (x) {\n"
231 "case 1: {\n"
232 " f();\n"
233 " break;\n"
234 "}\n"
235 "}");
236 verifyFormat("switch (test)\n"
237 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000238 verifyGoogleFormat("switch (x) {\n"
239 " case 1:\n"
240 " f();\n"
241 " break;\n"
242 " case kFoo:\n"
243 " case ns::kBar:\n"
244 " case kBaz:\n"
245 " break;\n"
246 " default:\n"
247 " g();\n"
248 " break;\n"
249 "}");
250 verifyGoogleFormat("switch (x) {\n"
251 " case 1: {\n"
252 " f();\n"
253 " break;\n"
254 " }\n"
255 "}");
256 verifyGoogleFormat("switch (test)\n"
257 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000258}
259
Alexander Kornienko15757312012-12-06 18:03:27 +0000260TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000261 verifyFormat("void f() {\n"
262 " some_code();\n"
263 "test_label:\n"
264 " some_other_code();\n"
265 " {\n"
266 " some_more_code();\n"
267 " another_label:\n"
268 " some_more_code();\n"
269 " }\n"
270 "}");
271 verifyFormat("some_code();\n"
272 "test_label:\n"
273 "some_other_code();");
274}
275
Alexander Kornienko15757312012-12-06 18:03:27 +0000276//===----------------------------------------------------------------------===//
277// Tests for comments.
278//===----------------------------------------------------------------------===//
279
280TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000281 verifyFormat("// line 1\n"
282 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000283 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000284
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000285 verifyFormat("void f() {\n"
286 " // Doesn't do anything\n"
287 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000288 verifyFormat("void f(int i, // some comment (probably for i)\n"
289 " int j, // some comment (probably for j)\n"
290 " int k); // some comment (probably for k)");
291 verifyFormat("void f(int i,\n"
292 " // some comment (probably for j)\n"
293 " int j,\n"
294 " // some comment (probably for k)\n"
295 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000296
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000297 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000298 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000299
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000300 verifyFormat("enum E {\n"
301 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000302 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000303 " VAL_B\n"
304 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000305
306 verifyFormat(
307 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000308 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000309 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
310 " // Comment inside a statement.\n"
311 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000312
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000313 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000314 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000315
316 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000317}
318
319TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000320 verifyFormat("f(/*test=*/ true);");
321}
322
Alexander Kornienko15757312012-12-06 18:03:27 +0000323//===----------------------------------------------------------------------===//
324// Tests for classes, namespaces, etc.
325//===----------------------------------------------------------------------===//
326
327TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000328 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000329}
330
331TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
332 verifyFormat("class A {\n"
333 "public:\n"
334 "protected:\n"
335 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000336 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000337 "};");
338 verifyGoogleFormat("class A {\n"
339 " public:\n"
340 " protected:\n"
341 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000342 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000343 "};");
344}
345
346TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000347 verifyFormat("class A : public B {};");
348 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000349}
350
Manuel Klimekde768542013-01-07 18:10:23 +0000351TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000352 verifyFormat("class A {} a, b;");
353 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000354 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000355}
356
Alexander Kornienko15757312012-12-06 18:03:27 +0000357TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000358 verifyFormat("enum {\n"
359 " Zero,\n"
360 " One = 1,\n"
361 " Two = One + 1,\n"
362 " Three = (One + Two),\n"
363 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
364 " Five = (One, Two, Three, Four, 5)\n"
365 "};");
366 verifyFormat("enum Enum {\n"
367 "};");
368 verifyFormat("enum {\n"
369 "};");
370}
371
Nico Weberefaddc02013-01-14 05:49:49 +0000372TEST_F(FormatTest, FormatsBitfields) {
373 verifyFormat("struct Bitfields {\n"
374 " unsigned sClass : 8;\n"
375 " unsigned ValueKind : 2;\n"
376 "};");
377}
378
Alexander Kornienko15757312012-12-06 18:03:27 +0000379TEST_F(FormatTest, FormatsNamespaces) {
380 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000381 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000382 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000383 "}");
384 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000385 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000386 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000387 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000388 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000389 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000390 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000391 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000392 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000393 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000394 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000395}
396
Nico Webera9ccdd12013-01-07 16:36:17 +0000397TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000398 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
399 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000400 verifyFormat("try {\n"
401 " throw a * b;\n"
402 "}\n"
403 "catch (int a) {\n"
404 " // Do nothing.\n"
405 "}\n"
406 "catch (...) {\n"
407 " exit(42);\n"
408 "}");
409
410 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000411 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000412 "catch (...) {\n"
413 " return 5;\n"
414 "}");
415 verifyFormat("class A {\n"
416 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000417 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000418 " catch (...) {\n"
419 " throw;\n"
420 " }\n"
421 "};\n");
422}
423
424TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000425 verifyFormat("@try {\n"
426 " f();\n"
427 "}\n"
428 "@catch (NSException e) {\n"
429 " @throw;\n"
430 "}\n"
431 "@finally {\n"
432 " exit(42);\n"
433 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000434}
435
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000436TEST_F(FormatTest, StaticInitializers) {
437 verifyFormat("static SomeClass SC = { 1, 'a' };");
438
439 // FIXME: Format like enums if the static initializer does not fit on a line.
440 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000441 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000442 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
443 "};");
444
445 verifyFormat(
446 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
447 " looooooooooooooooooooooooooooooooooongname,\n"
448 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000449}
450
Manuel Klimeka080a182013-01-02 16:30:12 +0000451TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
452 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
453 " \\\n"
454 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
455}
456
Daniel Jasper71607512013-01-07 10:48:50 +0000457TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000458 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
459 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000460}
461
Manuel Klimeka080a182013-01-02 16:30:12 +0000462TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
463 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000464 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000465}
466
467TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
468 EXPECT_EQ("#line 42 \"test\"\n",
469 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000470 EXPECT_EQ("#define A \\\n B\n",
471 format("# \\\n define \\\n A \\\n B\n",
472 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000473}
474
475TEST_F(FormatTest, EndOfFileEndsPPDirective) {
476 EXPECT_EQ("#line 42 \"test\"",
477 format("# \\\n line \\\n 42 \\\n \"test\""));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000478 EXPECT_EQ("#define A \\\n B",
479 format("# \\\n define \\\n A \\\n B",
480 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000481}
482
Manuel Klimek060143e2013-01-02 18:33:23 +0000483TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000484 // If the macro fits in one line, we still do not get the full
485 // line, as only the next line decides whether we need an escaped newline and
486 // thus use the last column.
487 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000488
Manuel Klimekd544c572013-01-07 09:24:17 +0000489 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
490 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000491 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
492}
493
494TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000495 EXPECT_EQ("// some comment\n"
496 "#include \"a.h\"\n"
497 "#define A(A,\\\n"
498 " B)\n"
499 "#include \"b.h\"\n"
500 "// some comment\n",
501 format(" // some comment\n"
502 " #include \"a.h\"\n"
503 "#define A(A,\\\n"
504 " B)\n"
505 " #include \"b.h\"\n"
506 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000507}
508
Manuel Klimekd4397b92013-01-04 23:34:14 +0000509TEST_F(FormatTest, LayoutSingleHash) {
510 EXPECT_EQ("#\na;", format("#\na;"));
511}
512
513TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
514 EXPECT_EQ("#define A \\\n"
515 " c; \\\n"
516 " e;\n"
517 "f;", format("#define A c; e;\n"
518 "f;", getLLVMStyleWithColumns(14)));
519}
520
521TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000522 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000523}
524
525TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000526 EXPECT_EQ("# define A\\\n b;",
527 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000528}
529
530TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000531 EXPECT_EQ("int x,\n"
532 "#define A\n"
533 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000534}
535
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000536TEST_F(FormatTest, HashInMacroDefinition) {
537 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
538 verifyFormat("#define A \\\n"
539 " { \\\n"
540 " f(#c);\\\n"
541 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000542
543 verifyFormat("#define A(X) \\\n"
544 " void function##X()", getLLVMStyleWithColumns(22));
545
546 verifyFormat("#define A(a, b, c) \\\n"
547 " void a##b##c()", getLLVMStyleWithColumns(22));
548
549 verifyFormat("#define A \\\n"
550 " void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000551}
552
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000553TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
554 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
555}
556
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000557TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000558 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000559}
560
Manuel Klimeka5342db2013-01-06 20:07:31 +0000561TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
562 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
563 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
564 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
565 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
566}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000567
Manuel Klimek95419382013-01-07 07:56:50 +0000568TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000569 EXPECT_EQ(
570 "#define A \\\n int i; \\\n int j;",
571 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000572}
573
Manuel Klimekd544c572013-01-07 09:24:17 +0000574TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
575 verifyFormat("#define A \\\n"
576 " int v( \\\n"
577 " a); \\\n"
578 " int i;", getLLVMStyleWithColumns(11));
579}
580
Manuel Klimeka080a182013-01-02 16:30:12 +0000581TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000582 EXPECT_EQ(
583 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
584 " \\\n"
585 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
586 "\n"
587 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
588 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
589 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
590 "\\\n"
591 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
592 " \n"
593 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
594 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000595}
596
Manuel Klimek526ed112013-01-09 15:25:02 +0000597TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
598 EXPECT_EQ("int\n"
599 "#define A\n"
600 " a;",
601 format("int\n#define A\na;"));
602 verifyFormat(
603 "functionCallTo(someOtherFunction(\n"
604 " withSomeParameters, whichInSequence,\n"
605 " areLongerThanALine(andAnotherCall,\n"
606 "#define A \\\n"
607 " B\n"
608 " withMoreParamters,\n"
609 " whichStronglyInfluenceTheLayout),\n"
610 " andMoreParameters),\n"
611 " trailing);", getLLVMStyleWithColumns(69));
612}
613
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000614TEST_F(FormatTest, LayoutBlockInsideParens) {
615 EXPECT_EQ("functionCall({\n"
616 " int i;\n"
617 "});", format(" functionCall ( {int i;} );"));
618}
619
620TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000621 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000622 "int i;", format(" SOME_MACRO {int i;} int i;"));
623}
624
625TEST_F(FormatTest, LayoutNestedBlocks) {
626 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
627 " struct s {\n"
628 " int i;\n"
629 " };\n"
630 " s kBitsToOs[] = { { 10 } };\n"
631 " for (int i = 0; i < 10; ++i)\n"
632 " return;\n"
633 "}");
634}
635
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000636TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
637 EXPECT_EQ("{}", format("{}"));
638}
639
Alexander Kornienko15757312012-12-06 18:03:27 +0000640//===----------------------------------------------------------------------===//
641// Line break tests.
642//===----------------------------------------------------------------------===//
643
644TEST_F(FormatTest, FormatsFunctionDefinition) {
645 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
646 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000647 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000648}
649
650TEST_F(FormatTest, FormatsAwesomeMethodCall) {
651 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000652 "SomeLongMethodName(SomeReallyLongMethod(\n"
653 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
654 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000655}
656
Daniel Jasper1321eb52012-12-18 21:05:13 +0000657TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000658 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000659 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
660 getLLVMStyleWithColumns(45));
661 verifyFormat("Constructor()\n"
662 " : Inttializer(FitsOnTheLine) {}",
663 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000664
665 verifyFormat(
666 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000667 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000668
669 verifyFormat(
670 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000671 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
672 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
673 verifyGoogleFormat(
674 "SomeClass::Constructor()\n"
675 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
676 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
677 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
678
679 verifyFormat(
680 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000681 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000682 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000683
684 verifyFormat("Constructor()\n"
685 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
686 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
687 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000688 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000689
690 // Here a line could be saved by splitting the second initializer onto two
691 // lines, but that is not desireable.
692 verifyFormat("Constructor()\n"
693 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
694 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000695 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000696
697 verifyGoogleFormat("MyClass::MyClass(int var)\n"
698 " : some_var_(var), // 4 space indent\n"
699 " some_other_var_(var + 1) { // lined up\n"
700 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000701
702 // This test takes VERY long when memoization is broken.
703 verifyGoogleFormat(
704 "Constructor()\n"
705 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
706 " a, a, a,\n"
707 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
708 " a, a, a,\n"
709 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
710 " a, a, a,\n"
711 " a, a, a, a, a, a, a, a, a, a, a)\n"
712 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
713 " a, a, a,\n"
714 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
715 " a, a, a,\n"
716 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
717 " a, a, a,\n"
718 " a, a, a, a, a, a, a, a, a, a, a) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000719}
720
Alexander Kornienko15757312012-12-06 18:03:27 +0000721TEST_F(FormatTest, BreaksAsHighAsPossible) {
722 verifyFormat(
723 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
724 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
725 " f();");
726}
727
Daniel Jasperbac016b2012-12-03 18:12:45 +0000728TEST_F(FormatTest, BreaksDesireably) {
729 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
730 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000731 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000732
733 verifyFormat(
734 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000736
737 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000740
741 verifyFormat(
742 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
744 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000746
747 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
748 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
749
Daniel Jasper723f0302013-01-02 14:40:02 +0000750 verifyFormat(
751 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
753
Daniel Jasper33182dd2012-12-05 14:57:28 +0000754 // This test case breaks on an incorrect memoization, i.e. an optimization not
755 // taking into account the StopAt value.
756 verifyFormat(
757 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000758 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
759 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
760 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000761
Daniel Jaspercd162382013-01-07 13:26:07 +0000762 verifyFormat("{\n {\n {\n"
763 " Annotation.SpaceRequiredBefore =\n"
764 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
765 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
766 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000767}
768
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000769TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
770 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
771 " GUARDED_BY(aaaaaaaaaaaaa);");
772}
773
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000774TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
775 verifyFormat(
776 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000777 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000778 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000779 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000780 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000781 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000782 verifyFormat(
783 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000784 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000785}
786
Daniel Jasper9cda8002013-01-07 13:08:40 +0000787TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
788 verifyFormat(
789 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
790 " SI->getAlignment(),\n"
791 " SI->getPointerAddressSpaceee());\n");
792 verifyFormat(
793 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
794 " Line.Tokens.front().Tok.getLocation(),\n"
795 " Line.Tokens.back().Tok.getLocation());");
796}
797
Daniel Jaspercf225b62012-12-24 13:43:52 +0000798TEST_F(FormatTest, AlignsAfterAssignments) {
799 verifyFormat(
800 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000801 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000802 verifyFormat(
803 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000804 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000805 verifyFormat(
806 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000807 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000808 verifyFormat(
809 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000810 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000811 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000812 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
813 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
814 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000815}
816
817TEST_F(FormatTest, AlignsAfterReturn) {
818 verifyFormat(
819 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
820 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
821 verifyFormat(
822 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
823 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
824}
825
Daniel Jasper9c837d02013-01-09 07:06:56 +0000826TEST_F(FormatTest, BreaksConditionalExpressions) {
827 verifyFormat(
828 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
829 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
831 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
832 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
833}
834
Nico Weber7d37b8b2013-01-12 01:28:06 +0000835TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
836 verifyFormat("arr[foo ? bar : baz];");
837 verifyFormat("f()[foo ? bar : baz];");
838 verifyFormat("(a + b)[foo ? bar : baz];");
839 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
840}
841
Daniel Jasperbac016b2012-12-03 18:12:45 +0000842TEST_F(FormatTest, AlignsStringLiterals) {
843 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
844 " \"short literal\");");
845 verifyFormat(
846 "looooooooooooooooooooooooongFunction(\n"
847 " \"short literal\"\n"
848 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
849}
850
Alexander Kornienko15757312012-12-06 18:03:27 +0000851TEST_F(FormatTest, AlignsPipes) {
852 verifyFormat(
853 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
854 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
855 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
856 verifyFormat(
857 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
858 " << aaaaaaaaaaaaaaaaaaaa;");
859 verifyFormat(
860 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
861 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
862 verifyFormat(
863 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
864 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
865 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
866 verifyFormat(
867 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
868 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
869 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
870}
871
Daniel Jasperbac016b2012-12-03 18:12:45 +0000872TEST_F(FormatTest, UnderstandsEquals) {
873 verifyFormat(
874 "aaaaaaaaaaaaaaaaa =\n"
875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
876 verifyFormat(
877 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000879 verifyFormat(
880 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000881 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000882 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000884
Daniel Jasper9cda8002013-01-07 13:08:40 +0000885 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000886 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000887 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000888 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000889}
890
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000891TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000892 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
893 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000894
Daniel Jasper1321eb52012-12-18 21:05:13 +0000895 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
896 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000897
898 verifyFormat(
899 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
900 " Parameter2);");
901
902 verifyFormat(
903 "ShortObject->shortFunction(\n"
904 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
905 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
906
907 verifyFormat("loooooooooooooongFunction(\n"
908 " LoooooooooooooongObject->looooooooooooooooongFunction());");
909
910 verifyFormat(
911 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
912 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
913
Daniel Jasper46a46a22013-01-07 07:13:20 +0000914 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000915 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000916 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000917 verifyFormat(
918 "aaaaaaaaaaa->aaaaaaaaa(\n"
919 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
920 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000921}
922
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000923TEST_F(FormatTest, WrapsTemplateDeclarations) {
924 verifyFormat("template <typename T>\n"
925 "virtual void loooooooooooongFunction(int Param1, int Param2);");
926 verifyFormat(
927 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
928 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
929 verifyFormat(
930 "template <typename T>\n"
931 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
932 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000933 verifyFormat(
934 "template <typename T>\n"
935 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
936 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000938 verifyFormat("template <typename T>\n"
939 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
940 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000941 verifyFormat(
942 "template <typename T1, typename T2 = char, typename T3 = char,\n"
943 " typename T4 = char>\n"
944 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000945}
946
Daniel Jasperbac016b2012-12-03 18:12:45 +0000947TEST_F(FormatTest, UnderstandsTemplateParameters) {
948 verifyFormat("A<int> a;");
949 verifyFormat("A<A<A<int> > > a;");
950 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
951 verifyFormat("bool x = a < 1 || 2 > a;");
952 verifyFormat("bool x = 5 < f<int>();");
953 verifyFormat("bool x = f<int>() > 5;");
954 verifyFormat("bool x = 5 < a<int>::x;");
955 verifyFormat("bool x = a < 4 ? a > 2 : false;");
956 verifyFormat("bool x = f() ? a < 2 : a > 2;");
957
958 verifyGoogleFormat("A<A<int>> a;");
959 verifyGoogleFormat("A<A<A<int>>> a;");
960 verifyGoogleFormat("A<A<A<A<int>>>> a;");
961
962 verifyFormat("test >> a >> b;");
963 verifyFormat("test << a >> b;");
964
965 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000966 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000967}
968
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000969TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000970 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000971 verifyFormat("f(-1, -2, -3);");
972 verifyFormat("a[-1] = 5;");
973 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000974 verifyFormat("if (i == -1) {}");
975 verifyFormat("if (i != -1) {}");
976 verifyFormat("if (i > -1) {}");
977 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +0000978 verifyFormat("++(a->f());");
979 verifyFormat("--(a->f());");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000980 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000981
982 verifyFormat("a-- > b;");
983 verifyFormat("b ? -a : c;");
984 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000985 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000986 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000987 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +0000988
989 verifyFormat("return -1;");
990 verifyFormat("switch (a) {\n"
991 "case -1:\n"
992 " break;\n"
993 "}");
Nico Webercc191d12013-01-12 05:41:23 +0000994
995 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
996 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000997}
998
999TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001000 verifyFormat("bool operator<();");
1001 verifyFormat("bool operator>();");
1002 verifyFormat("bool operator=();");
1003 verifyFormat("bool operator==();");
1004 verifyFormat("bool operator!=();");
1005 verifyFormat("int operator+();");
1006 verifyFormat("int operator++();");
1007 verifyFormat("bool operator();");
1008 verifyFormat("bool operator()();");
1009 verifyFormat("bool operator[]();");
1010 verifyFormat("operator bool();");
1011 verifyFormat("operator SomeType<int>();");
1012 verifyFormat("void *operator new(std::size_t size);");
1013 verifyFormat("void *operator new[](std::size_t size);");
1014 verifyFormat("void operator delete(void *ptr);");
1015 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001016}
1017
Daniel Jasper088dab52013-01-11 16:09:04 +00001018TEST_F(FormatTest, UnderstandsNewAndDelete) {
1019 verifyFormat("A *a = new A;");
1020 verifyFormat("A *a = new (placement) A;");
1021 verifyFormat("delete a;");
1022 verifyFormat("delete (A *)a;");
1023}
1024
Daniel Jasper5d334402013-01-02 08:57:10 +00001025TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001026 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001027 verifyFormat("f(a, *a);");
1028 verifyFormat("f(*a);");
1029 verifyFormat("int a = b * 10;");
1030 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001031 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001032 verifyFormat("int a += b * c;");
1033 verifyFormat("int a -= b * c;");
1034 verifyFormat("int a *= b * c;");
1035 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001036 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001037 verifyFormat("int a = *b * c;");
1038 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001039 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001040 verifyFormat("return 10 * b;");
1041 verifyFormat("return *b * *c;");
1042 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001043 verifyFormat("f(b ? *c : *d);");
1044 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001045 verifyFormat("*b = a;");
1046 verifyFormat("a * ~b;");
1047 verifyFormat("a * !b;");
1048 verifyFormat("a * +b;");
1049 verifyFormat("a * -b;");
1050 verifyFormat("a * ++b;");
1051 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001052 verifyFormat("a[4] * b;");
1053 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001054 verifyFormat("a * [self dostuff];");
1055 verifyFormat("a * (a + b);");
1056 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001057 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001058
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001059 verifyFormat("InvalidRegions[*R] = 0;");
1060
Daniel Jasper8b39c662012-12-10 18:59:13 +00001061 verifyFormat("A<int *> a;");
1062 verifyFormat("A<int **> a;");
1063 verifyFormat("A<int *, int *> a;");
1064 verifyFormat("A<int **, int **> a;");
1065
Daniel Jasper2db356d2013-01-08 20:03:18 +00001066 verifyFormat(
1067 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1068 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1069
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001070 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001071 verifyGoogleFormat("A<int*> a;");
1072 verifyGoogleFormat("A<int**> a;");
1073 verifyGoogleFormat("A<int*, int*> a;");
1074 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001075 verifyGoogleFormat("f(b ? *c : *d);");
1076 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001077}
1078
Daniel Jasper4981bd02013-01-13 08:01:36 +00001079TEST_F(FormatTest, FormatsCasts) {
1080 verifyFormat("Type *A = static_cast<Type *>(P);");
1081 verifyFormat("Type *A = (Type *)P;");
1082 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1083 verifyFormat("int a = (int)(2.0f);");
1084
1085 // FIXME: These also need to be identified.
1086 verifyFormat("int a = (int) 2.0f;");
1087 verifyFormat("int a = (int) * b;");
1088
1089 // These are not casts.
1090 verifyFormat("void f(int *) {}");
1091 verifyFormat("void f(int *);");
1092 verifyFormat("void f(int *) = 0;");
1093 verifyFormat("void f(SmallVector<int>) {}");
1094 verifyFormat("void f(SmallVector<int>);");
1095 verifyFormat("void f(SmallVector<int>) = 0;");
1096}
1097
Daniel Jasper46ef8522013-01-10 13:08:12 +00001098TEST_F(FormatTest, FormatsFunctionTypes) {
1099 // FIXME: Determine the cases that need a space after the return type and fix.
1100 verifyFormat("A<bool()> a;");
1101 verifyFormat("A<SomeType()> a;");
1102 verifyFormat("A<void(*)(int, std::string)> a;");
1103
1104 verifyFormat("int(*func)(void *);");
1105}
1106
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001107TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001108 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001109 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001110 verifyFormat(
1111 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1112 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001113 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001114}
1115
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001116TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1117 verifyFormat("(a)->b();");
1118 verifyFormat("--a;");
1119}
1120
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001121TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001122 verifyFormat("#include <string>");
1123 verifyFormat("#include <a/b/c.h>");
1124 verifyFormat("#include \"a/b/string\"");
1125 verifyFormat("#include \"string.h\"");
1126 verifyFormat("#include \"string.h\"");
1127 verifyFormat("#include <a-a>");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001128
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001129 verifyFormat("#import <string>");
1130 verifyFormat("#import <a/b/c.h>");
1131 verifyFormat("#import \"a/b/string\"");
1132 verifyFormat("#import \"string.h\"");
1133 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001134}
1135
Alexander Kornienko15757312012-12-06 18:03:27 +00001136//===----------------------------------------------------------------------===//
1137// Error recovery tests.
1138//===----------------------------------------------------------------------===//
1139
Daniel Jasper700e7102013-01-10 09:26:47 +00001140TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1141 verifyFormat("void f() { return } 42");
1142}
1143
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001144TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1145 verifyFormat("int aaaaaaaa =\n"
1146 " // Overly long comment\n"
1147 " b;", getLLVMStyleWithColumns(20));
1148 verifyFormat("function(\n"
1149 " ShortArgument,\n"
1150 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1151}
1152
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001153TEST_F(FormatTest, IncorrectAccessSpecifier) {
1154 verifyFormat("public:");
1155 verifyFormat("class A {\n"
1156 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001157 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001158 "};");
1159 verifyFormat("public\n"
1160 "int qwerty;");
1161 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001162 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001163 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001164 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001165 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001166 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001167}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001168
Alexander Kornienko393b0082012-12-04 15:40:36 +00001169TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1170 verifyFormat("{");
1171}
1172
1173TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001174 verifyFormat("do {}");
1175 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001176 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001177 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001178 "wheeee(fun);");
1179 verifyFormat("do {\n"
1180 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001181 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001182}
1183
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001184TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001185 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001186 verifyFormat("switch {\n foo;\n foo();\n}");
1187 verifyFormat("for {\n foo;\n foo();\n}");
1188 verifyFormat("while {\n foo;\n foo();\n}");
1189 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001190}
1191
Daniel Jasper1f42f112013-01-04 18:52:56 +00001192TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1193 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001194 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001195}
1196
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001197TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001198 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1199 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1200 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1201 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001202
1203 FormatStyle Style = getLLVMStyle();
1204 Style.ColumnLimit = 10;
1205 EXPECT_EQ("{\n"
1206 " {\n"
1207 " breakme(\n"
1208 " qwe);\n"
1209 "}\n", format("{\n"
1210 " {\n"
1211 " breakme(qwe);\n"
1212 "}\n", Style));
1213
1214}
1215
Manuel Klimek2851c162013-01-10 14:36:46 +00001216TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1217 verifyFormat(
1218 "int x = {\n"
1219 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001220 " b(alongervariable)\n"
1221 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001222}
1223
1224TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1225 verifyFormat(
1226 "Aaa({\n"
1227 " int i;\n"
1228 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1229 " ccccccccccccccccc));");
1230}
1231
Manuel Klimek517e8942013-01-11 17:54:10 +00001232TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1233 verifyFormat("void f() { return 42; }");
1234 verifyFormat("void f() {\n"
1235 " // Comment\n"
1236 "}");
1237 verifyFormat("{\n"
1238 "#error {\n"
1239 " int a;\n"
1240 "}");
1241 verifyFormat("{\n"
1242 " int a;\n"
1243 "#error {\n"
1244 "}");
1245}
1246
Manuel Klimek606e07e2013-01-11 18:13:04 +00001247TEST_F(FormatTest, BracedInitListWithElaboratedTypeSpecifier) {
1248 verifyFormat("struct foo a = { bar };\nint n;");
1249}
1250
Manuel Klimek517e8942013-01-11 17:54:10 +00001251// FIXME: This breaks the order of the unwrapped lines:
1252// TEST_F(FormatTest, OrderUnwrappedLines) {
1253// verifyFormat("{\n"
1254// " bool a; //\n"
1255// "#error {\n"
1256// " int a;\n"
1257// "}");
1258// }
1259
Nico Webercf4a79c2013-01-08 17:56:31 +00001260//===----------------------------------------------------------------------===//
1261// Objective-C tests.
1262//===----------------------------------------------------------------------===//
1263
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001264TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1265 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1266 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1267 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001268 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001269 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1270 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1271 format("-(NSInteger)Method3:(id)anObject;"));
1272 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1273 format("-(NSInteger)Method4:(id)anObject;"));
1274 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1275 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1276 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1277 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001278 EXPECT_EQ(
1279 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1280 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001281
1282 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001283 EXPECT_EQ(
1284 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1285 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1286 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1287 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1288 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1289 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1290 format(
1291 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1292 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1293 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1294 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1295 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1296 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001297
1298 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001299 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001300 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1301 // protocol lists (but not for template classes):
1302 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001303
1304 verifyFormat("- (int(*)())foo:(int(*)())f;");
1305 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1306
1307 // If there's no return type (very rare in practice!), LLVM and Google style
1308 // agree.
1309 verifyFormat("- foo:(int)f;");
1310 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001311}
1312
Daniel Jasper886568d2013-01-09 08:36:49 +00001313TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001314 verifyFormat("int (^Block)(int, int);");
1315 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001316}
1317
Nico Weber27d13672013-01-09 20:25:35 +00001318TEST_F(FormatTest, FormatObjCInterface) {
1319 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001320 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001321 "@public\n"
1322 " int field1;\n"
1323 "@protected\n"
1324 " int field2;\n"
1325 "@private\n"
1326 " int field3;\n"
1327 "@package\n"
1328 " int field4;\n"
1329 "}\n"
1330 "+ (id)init;\n"
1331 "@end");
1332
Nico Weber27d13672013-01-09 20:25:35 +00001333 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1334 " @public\n"
1335 " int field1;\n"
1336 " @protected\n"
1337 " int field2;\n"
1338 " @private\n"
1339 " int field3;\n"
1340 " @package\n"
1341 " int field4;\n"
1342 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001343 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001344 "@end");
1345
1346 verifyFormat("@interface Foo\n"
1347 "+ (id)init;\n"
1348 "// Look, a comment!\n"
1349 "- (int)answerWith:(int)i;\n"
1350 "@end");
1351
1352 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001353 "@end\n"
1354 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001355 "@end");
1356
1357 verifyFormat("@interface Foo : Bar\n"
1358 "+ (id)init;\n"
1359 "@end");
1360
Nico Weber5f500df2013-01-10 20:12:55 +00001361 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001362 "+ (id)init;\n"
1363 "@end");
1364
Nico Weber5f500df2013-01-10 20:12:55 +00001365 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001366 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001367 "@end");
1368
Nico Webered91bba2013-01-10 19:19:14 +00001369 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001370 "+ (id)init;\n"
1371 "@end");
1372
Nico Webered91bba2013-01-10 19:19:14 +00001373 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001374 "+ (id)init;\n"
1375 "@end");
1376
Nico Weber5f500df2013-01-10 20:12:55 +00001377 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001378 "+ (id)init;\n"
1379 "@end");
1380
Nico Weber5f500df2013-01-10 20:12:55 +00001381 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001382 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001383 "@end");
1384
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001385 verifyFormat("@interface Foo {\n"
1386 " int _i;\n"
1387 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001388 "+ (id)init;\n"
1389 "@end");
1390
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001391 verifyFormat("@interface Foo : Bar {\n"
1392 " int _i;\n"
1393 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001394 "+ (id)init;\n"
1395 "@end");
1396
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001397 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1398 " int _i;\n"
1399 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001400 "+ (id)init;\n"
1401 "@end");
1402
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001403 verifyFormat("@interface Foo (HackStuff) {\n"
1404 " int _i;\n"
1405 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001406 "+ (id)init;\n"
1407 "@end");
1408
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001409 verifyFormat("@interface Foo () {\n"
1410 " int _i;\n"
1411 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001412 "+ (id)init;\n"
1413 "@end");
1414
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001415 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1416 " int _i;\n"
1417 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001418 "+ (id)init;\n"
1419 "@end");
1420}
1421
Nico Weber50767d82013-01-09 23:25:37 +00001422TEST_F(FormatTest, FormatObjCImplementation) {
1423 verifyFormat("@implementation Foo : NSObject {\n"
1424 "@public\n"
1425 " int field1;\n"
1426 "@protected\n"
1427 " int field2;\n"
1428 "@private\n"
1429 " int field3;\n"
1430 "@package\n"
1431 " int field4;\n"
1432 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001433 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001434 "@end");
1435
1436 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1437 " @public\n"
1438 " int field1;\n"
1439 " @protected\n"
1440 " int field2;\n"
1441 " @private\n"
1442 " int field3;\n"
1443 " @package\n"
1444 " int field4;\n"
1445 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001446 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001447 "@end");
1448
1449 verifyFormat("@implementation Foo\n"
1450 "+ (id)init {\n"
1451 " if (true)\n"
1452 " return nil;\n"
1453 "}\n"
1454 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001455 "- (int)answerWith:(int)i {\n"
1456 " return i;\n"
1457 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001458 "+ (int)answerWith:(int)i {\n"
1459 " return i;\n"
1460 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001461 "@end");
1462
1463 verifyFormat("@implementation Foo\n"
1464 "@end\n"
1465 "@implementation Bar\n"
1466 "@end");
1467
1468 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001469 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001470 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001471 "@end");
1472
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001473 verifyFormat("@implementation Foo {\n"
1474 " int _i;\n"
1475 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001476 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001477 "@end");
1478
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001479 verifyFormat("@implementation Foo : Bar {\n"
1480 " int _i;\n"
1481 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001482 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001483 "@end");
1484
Nico Webered91bba2013-01-10 19:19:14 +00001485 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001486 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001487 "@end");
1488}
1489
Nico Weber1abe6ea2013-01-09 21:15:03 +00001490TEST_F(FormatTest, FormatObjCProtocol) {
1491 verifyFormat("@protocol Foo\n"
1492 "@property(weak) id delegate;\n"
1493 "- (NSUInteger)numberOfThings;\n"
1494 "@end");
1495
Nico Weber5f500df2013-01-10 20:12:55 +00001496 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001497 "- (NSUInteger)numberOfThings;\n"
1498 "@end");
1499
Nico Weber5f500df2013-01-10 20:12:55 +00001500 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001501 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001502 "@end");
1503
Nico Weber1abe6ea2013-01-09 21:15:03 +00001504 verifyFormat("@protocol Foo;\n"
1505 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001506
1507 verifyFormat("@protocol Foo\n"
1508 "@end\n"
1509 "@protocol Bar\n"
1510 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001511
1512 verifyFormat("@protocol myProtocol\n"
1513 "- (void)mandatoryWithInt:(int)i;\n"
1514 "@optional\n"
1515 "- (void)optional;\n"
1516 "@required\n"
1517 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001518 "@optional\n"
1519 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001520 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001521}
1522
Nico Weberbcfdd262013-01-12 06:18:40 +00001523TEST_F(FormatTest, FormatObjCMethodExpr) {
1524 verifyFormat("[foo bar:baz];");
1525 verifyFormat("return [foo bar:baz];");
1526 verifyFormat("f([foo bar:baz]);");
1527 verifyFormat("f(2, [foo bar:baz]);");
1528 verifyFormat("f(2, a ? b : c);");
1529 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1530
1531 verifyFormat("[foo bar:baz], [foo bar:baz];");
1532 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1533 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1534 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1535 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1536 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1537 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1538 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1539 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1540 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1541 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1542 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1543 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1544 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1545 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1546 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1547 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1548 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1549 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1550 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1551 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1552 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1553 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1554 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1555 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1556 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1557 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1558 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1559 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1560 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1561 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1562 // Whew!
1563
1564 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1565 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1566 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1567 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1568 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001569 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001570 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001571
Nico Weberbcfdd262013-01-12 06:18:40 +00001572 verifyFormat("arr[[self indexForFoo:a]];");
1573 verifyFormat("throw [self errorFor:a];");
1574 verifyFormat("@throw [self errorFor:a];");
1575
Nico Webere8ccc812013-01-12 22:48:47 +00001576 // This tests that the formatter doesn't break after "backing" but before ":",
1577 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001578 verifyFormat(
1579 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001580 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1581 " backing:NSBackingStoreBuffered defer:YES]))");
1582
Nico Webere8ccc812013-01-12 22:48:47 +00001583 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1584 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001585
1586}
1587
Nico Weber581f5572013-01-07 15:56:25 +00001588TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001589 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001590 verifyFormat("@catch");
1591 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001592 verifyFormat("@compatibility_alias");
1593 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001594 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001595 verifyFormat("@encode");
1596 verifyFormat("@end");
1597 verifyFormat("@finally");
1598 verifyFormat("@implementation");
1599 verifyFormat("@import");
1600 verifyFormat("@interface");
1601 verifyFormat("@optional");
1602 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001603 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001604 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001605 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001606 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001607 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001608 verifyFormat("@required");
1609 verifyFormat("@selector");
1610 verifyFormat("@synchronized");
1611 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001612 verifyFormat("@throw");
1613 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001614
Nico Webercb4d6902013-01-08 19:40:21 +00001615 verifyFormat("@\"String\"");
1616 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001617 verifyFormat("@+4.8");
1618 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001619 verifyFormat("@1LL");
1620 verifyFormat("@.5");
1621 verifyFormat("@'c'");
1622 verifyFormat("@true");
1623 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001624 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001625 verifyFormat("@[");
1626 verifyFormat("@{");
1627
Nico Weber581f5572013-01-07 15:56:25 +00001628 EXPECT_EQ("@interface", format("@ interface"));
1629
1630 // The precise formatting of this doesn't matter, nobody writes code like
1631 // this.
1632 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001633}
1634
Nico Weberc31689a2013-01-08 19:15:23 +00001635TEST_F(FormatTest, ObjCSnippets) {
1636 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001637 verifyFormat("@autoreleasepool {\n"
1638 " foo();\n"
1639 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001640 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001641 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001642 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001643 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001644 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001645 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001646 verifyFormat("@synchronized(self) {\n"
1647 " f();\n"
1648 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001649
Nico Weber70848232013-01-10 21:30:42 +00001650 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1651 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1652
Nico Webercf4a79c2013-01-08 17:56:31 +00001653 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001654 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1655 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001656}
1657
Daniel Jaspercd162382013-01-07 13:26:07 +00001658} // end namespace tooling
1659} // end namespace clang