blob: 9565e3a855343bbca56a0c151dfbf29cce723d0e [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,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +000032 Ranges,
33 new IgnoringDiagConsumer());
Daniel Jasperbac016b2012-12-03 18:12:45 +000034 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
35 return Context.getRewrittenText(ID);
36 }
37
38 std::string format(llvm::StringRef Code,
39 const FormatStyle &Style = getLLVMStyle()) {
40 return format(Code, 0, Code.size(), Style);
41 }
42
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000043 std::string messUp(llvm::StringRef Code) {
44 std::string MessedUp(Code.str());
45 bool InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000046 bool InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000047 bool JustReplacedNewline = false;
48 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
49 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
50 if (JustReplacedNewline)
51 MessedUp[i - 1] = '\n';
52 InComment = true;
Manuel Klimek526ed112013-01-09 15:25:02 +000053 } else if (MessedUp[i] == '#' && JustReplacedNewline) {
54 MessedUp[i - 1] = '\n';
55 InPreprocessorDirective = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +000056 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
57 MessedUp[i] = ' ';
Manuel Klimek526ed112013-01-09 15:25:02 +000058 MessedUp[i + 1] = ' ';
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000059 } else if (MessedUp[i] == '\n') {
60 if (InComment) {
61 InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000062 } else if (InPreprocessorDirective) {
63 InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000064 } else {
65 JustReplacedNewline = true;
66 MessedUp[i] = ' ';
67 }
Manuel Klimekd4397b92013-01-04 23:34:14 +000068 } else if (MessedUp[i] != ' ') {
69 JustReplacedNewline = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000070 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000071 }
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000072 return MessedUp;
73 }
74
Manuel Klimek060143e2013-01-02 18:33:23 +000075 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
76 FormatStyle Style = getLLVMStyle();
77 Style.ColumnLimit = ColumnLimit;
78 return Style;
79 }
80
81 void verifyFormat(llvm::StringRef Code,
82 const FormatStyle &Style = getLLVMStyle()) {
83 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000084 }
85
86 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +000087 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +000088 }
89};
90
Manuel Klimek526ed112013-01-09 15:25:02 +000091TEST_F(FormatTest, MessUp) {
92 EXPECT_EQ("1 2 3", messUp("1 2 3"));
93 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
94 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
95 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
96 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
97}
98
Alexander Kornienko15757312012-12-06 18:03:27 +000099//===----------------------------------------------------------------------===//
100// Basic function tests.
101//===----------------------------------------------------------------------===//
102
Daniel Jasperbac016b2012-12-03 18:12:45 +0000103TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
104 EXPECT_EQ(";", format(";"));
105}
106
107TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
108 EXPECT_EQ("int i;", format(" int i;"));
109 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
110 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
111 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
112}
113
114TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
115 EXPECT_EQ("int i;", format("int\ni;"));
116}
117
118TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000119 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000120}
121
Alexander Kornienko15757312012-12-06 18:03:27 +0000122TEST_F(FormatTest, FormatsNestedCall) {
123 verifyFormat("Method(f1, f2(f3));");
124 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper4e9008a2013-01-13 08:19:51 +0000125 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000126}
127
Alexander Kornienko15757312012-12-06 18:03:27 +0000128//===----------------------------------------------------------------------===//
129// Tests for control statements.
130//===----------------------------------------------------------------------===//
131
132TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
133 verifyFormat("if (true)\n f();\ng();");
134 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
135 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000136 verifyFormat("if (a)\n"
137 " // comment\n"
138 " f();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000139}
140
141TEST_F(FormatTest, ParseIfElse) {
142 verifyFormat("if (true)\n"
143 " if (true)\n"
144 " if (true)\n"
145 " f();\n"
146 " else\n"
147 " g();\n"
148 " else\n"
149 " h();\n"
150 "else\n"
151 " i();");
152 verifyFormat("if (true)\n"
153 " if (true)\n"
154 " if (true) {\n"
155 " if (true)\n"
156 " f();\n"
157 " } else {\n"
158 " g();\n"
159 " }\n"
160 " else\n"
161 " h();\n"
162 "else {\n"
163 " i();\n"
164 "}");
165}
166
167TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000168 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000169 verifyFormat("if (a)\n"
170 " f();\n"
171 "else if (b)\n"
172 " g();\n"
173 "else\n"
174 " h();");
175}
176
Daniel Jasperbac016b2012-12-03 18:12:45 +0000177TEST_F(FormatTest, FormatsForLoop) {
178 verifyFormat(
179 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000180 " ++VeryVeryLongLoopVariable)\n"
181 " ;");
182 verifyFormat("for (;;)\n"
183 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000184 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000185 verifyFormat("for (;;) {\n"
186 " f();\n"
187 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000188
189 verifyFormat(
190 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
191 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000192 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000193
194 verifyFormat(
195 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000196 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000197}
198
199TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000200 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000201 verifyFormat("while (true)\n"
202 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000203 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000204 verifyFormat("while () {\n"
205 " f();\n"
206 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000207}
208
Alexander Kornienko15757312012-12-06 18:03:27 +0000209TEST_F(FormatTest, FormatsDoWhile) {
210 verifyFormat("do {\n"
211 " do_something();\n"
212 "} while (something());");
213 verifyFormat("do\n"
214 " do_something();\n"
215 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000216}
217
Alexander Kornienko15757312012-12-06 18:03:27 +0000218TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000219 verifyFormat("switch (x) {\n"
220 "case 1:\n"
221 " f();\n"
222 " break;\n"
223 "case kFoo:\n"
224 "case ns::kBar:\n"
225 "case kBaz:\n"
226 " break;\n"
227 "default:\n"
228 " g();\n"
229 " break;\n"
230 "}");
231 verifyFormat("switch (x) {\n"
232 "case 1: {\n"
233 " f();\n"
234 " break;\n"
235 "}\n"
236 "}");
237 verifyFormat("switch (test)\n"
238 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000239 verifyGoogleFormat("switch (x) {\n"
240 " case 1:\n"
241 " f();\n"
242 " break;\n"
243 " case kFoo:\n"
244 " case ns::kBar:\n"
245 " case kBaz:\n"
246 " break;\n"
247 " default:\n"
248 " g();\n"
249 " break;\n"
250 "}");
251 verifyGoogleFormat("switch (x) {\n"
252 " case 1: {\n"
253 " f();\n"
254 " break;\n"
255 " }\n"
256 "}");
257 verifyGoogleFormat("switch (test)\n"
258 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000259}
260
Alexander Kornienko15757312012-12-06 18:03:27 +0000261TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000262 verifyFormat("void f() {\n"
263 " some_code();\n"
264 "test_label:\n"
265 " some_other_code();\n"
266 " {\n"
267 " some_more_code();\n"
268 " another_label:\n"
269 " some_more_code();\n"
270 " }\n"
271 "}");
272 verifyFormat("some_code();\n"
273 "test_label:\n"
274 "some_other_code();");
275}
276
Alexander Kornienko15757312012-12-06 18:03:27 +0000277//===----------------------------------------------------------------------===//
278// Tests for comments.
279//===----------------------------------------------------------------------===//
280
281TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000282 verifyFormat("// line 1\n"
283 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000284 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000285
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000286 verifyFormat("void f() {\n"
287 " // Doesn't do anything\n"
288 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000289 verifyFormat("void f(int i, // some comment (probably for i)\n"
290 " int j, // some comment (probably for j)\n"
291 " int k); // some comment (probably for k)");
292 verifyFormat("void f(int i,\n"
293 " // some comment (probably for j)\n"
294 " int j,\n"
295 " // some comment (probably for k)\n"
296 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000297
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000298 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000299 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000300
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000301 verifyFormat("enum E {\n"
302 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000303 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000304 " VAL_B\n"
305 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000306
307 verifyFormat(
308 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000309 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000310 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
311 " // Comment inside a statement.\n"
312 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000313
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000314 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000315 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000316
317 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000318}
319
320TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000321 verifyFormat("f(/*test=*/ true);");
322}
323
Alexander Kornienko15757312012-12-06 18:03:27 +0000324//===----------------------------------------------------------------------===//
325// Tests for classes, namespaces, etc.
326//===----------------------------------------------------------------------===//
327
328TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000329 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000330}
331
332TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
333 verifyFormat("class A {\n"
334 "public:\n"
335 "protected:\n"
336 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000337 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000338 "};");
339 verifyGoogleFormat("class A {\n"
340 " public:\n"
341 " protected:\n"
342 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000343 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000344 "};");
345}
346
347TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000348 verifyFormat("class A : public B {};");
349 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000350}
351
Manuel Klimekde768542013-01-07 18:10:23 +0000352TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000353 verifyFormat("class A {} a, b;");
354 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000355 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000356}
357
Alexander Kornienko15757312012-12-06 18:03:27 +0000358TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000359 verifyFormat("enum {\n"
360 " Zero,\n"
361 " One = 1,\n"
362 " Two = One + 1,\n"
363 " Three = (One + Two),\n"
364 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
365 " Five = (One, Two, Three, Four, 5)\n"
366 "};");
367 verifyFormat("enum Enum {\n"
368 "};");
369 verifyFormat("enum {\n"
370 "};");
371}
372
Nico Weberefaddc02013-01-14 05:49:49 +0000373TEST_F(FormatTest, FormatsBitfields) {
374 verifyFormat("struct Bitfields {\n"
375 " unsigned sClass : 8;\n"
376 " unsigned ValueKind : 2;\n"
377 "};");
378}
379
Alexander Kornienko15757312012-12-06 18:03:27 +0000380TEST_F(FormatTest, FormatsNamespaces) {
381 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000382 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000383 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000384 "}");
385 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000386 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000387 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000388 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000389 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000390 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000391 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000392 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000393 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000394 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000395 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000396}
397
Nico Webera9ccdd12013-01-07 16:36:17 +0000398TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000399 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
400 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000401 verifyFormat("try {\n"
402 " throw a * b;\n"
403 "}\n"
404 "catch (int a) {\n"
405 " // Do nothing.\n"
406 "}\n"
407 "catch (...) {\n"
408 " exit(42);\n"
409 "}");
410
411 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000412 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000413 "catch (...) {\n"
414 " return 5;\n"
415 "}");
416 verifyFormat("class A {\n"
417 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000418 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000419 " catch (...) {\n"
420 " throw;\n"
421 " }\n"
422 "};\n");
423}
424
425TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000426 verifyFormat("@try {\n"
427 " f();\n"
428 "}\n"
429 "@catch (NSException e) {\n"
430 " @throw;\n"
431 "}\n"
432 "@finally {\n"
433 " exit(42);\n"
434 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000435}
436
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000437TEST_F(FormatTest, StaticInitializers) {
438 verifyFormat("static SomeClass SC = { 1, 'a' };");
439
440 // FIXME: Format like enums if the static initializer does not fit on a line.
441 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000442 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000443 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
444 "};");
445
446 verifyFormat(
447 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
448 " looooooooooooooooooooooooooooooooooongname,\n"
449 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000450}
451
Manuel Klimeka080a182013-01-02 16:30:12 +0000452TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
453 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
454 " \\\n"
455 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
456}
457
Daniel Jasper71607512013-01-07 10:48:50 +0000458TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000459 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
460 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000461}
462
Manuel Klimeka080a182013-01-02 16:30:12 +0000463TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
464 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000465 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000466}
467
468TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
469 EXPECT_EQ("#line 42 \"test\"\n",
470 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000471 EXPECT_EQ("#define A \\\n B\n",
472 format("# \\\n define \\\n A \\\n B\n",
473 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000474}
475
476TEST_F(FormatTest, EndOfFileEndsPPDirective) {
477 EXPECT_EQ("#line 42 \"test\"",
478 format("# \\\n line \\\n 42 \\\n \"test\""));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000479 EXPECT_EQ("#define A \\\n B",
480 format("# \\\n define \\\n A \\\n B",
481 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000482}
483
Manuel Klimek060143e2013-01-02 18:33:23 +0000484TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000485 // If the macro fits in one line, we still do not get the full
486 // line, as only the next line decides whether we need an escaped newline and
487 // thus use the last column.
488 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000489
Manuel Klimekd544c572013-01-07 09:24:17 +0000490 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
491 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000492 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
493}
494
495TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000496 EXPECT_EQ("// some comment\n"
497 "#include \"a.h\"\n"
498 "#define A(A,\\\n"
499 " B)\n"
500 "#include \"b.h\"\n"
501 "// some comment\n",
502 format(" // some comment\n"
503 " #include \"a.h\"\n"
504 "#define A(A,\\\n"
505 " B)\n"
506 " #include \"b.h\"\n"
507 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000508}
509
Manuel Klimekd4397b92013-01-04 23:34:14 +0000510TEST_F(FormatTest, LayoutSingleHash) {
511 EXPECT_EQ("#\na;", format("#\na;"));
512}
513
514TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
515 EXPECT_EQ("#define A \\\n"
516 " c; \\\n"
517 " e;\n"
518 "f;", format("#define A c; e;\n"
519 "f;", getLLVMStyleWithColumns(14)));
520}
521
522TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000523 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000524}
525
526TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000527 EXPECT_EQ("# define A\\\n b;",
528 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000529}
530
531TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000532 EXPECT_EQ("int x,\n"
533 "#define A\n"
534 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000535}
536
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000537TEST_F(FormatTest, HashInMacroDefinition) {
538 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
539 verifyFormat("#define A \\\n"
540 " { \\\n"
541 " f(#c);\\\n"
542 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000543
544 verifyFormat("#define A(X) \\\n"
545 " void function##X()", getLLVMStyleWithColumns(22));
546
547 verifyFormat("#define A(a, b, c) \\\n"
548 " void a##b##c()", getLLVMStyleWithColumns(22));
549
550 verifyFormat("#define A \\\n"
551 " void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000552}
553
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000554TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
555 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
556}
557
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000558TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000559 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000560}
561
Manuel Klimeka5342db2013-01-06 20:07:31 +0000562TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
563 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
564 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
565 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
566 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
567}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000568
Manuel Klimek95419382013-01-07 07:56:50 +0000569TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000570 EXPECT_EQ(
571 "#define A \\\n int i; \\\n int j;",
572 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000573}
574
Manuel Klimekd544c572013-01-07 09:24:17 +0000575TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
576 verifyFormat("#define A \\\n"
577 " int v( \\\n"
578 " a); \\\n"
579 " int i;", getLLVMStyleWithColumns(11));
580}
581
Manuel Klimeka080a182013-01-02 16:30:12 +0000582TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000583 EXPECT_EQ(
584 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
585 " \\\n"
586 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
587 "\n"
588 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
589 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
590 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
591 "\\\n"
592 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
593 " \n"
594 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
595 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000596}
597
Manuel Klimek526ed112013-01-09 15:25:02 +0000598TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
599 EXPECT_EQ("int\n"
600 "#define A\n"
601 " a;",
602 format("int\n#define A\na;"));
603 verifyFormat(
604 "functionCallTo(someOtherFunction(\n"
605 " withSomeParameters, whichInSequence,\n"
606 " areLongerThanALine(andAnotherCall,\n"
607 "#define A \\\n"
608 " B\n"
609 " withMoreParamters,\n"
610 " whichStronglyInfluenceTheLayout),\n"
611 " andMoreParameters),\n"
612 " trailing);", getLLVMStyleWithColumns(69));
613}
614
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000615TEST_F(FormatTest, LayoutBlockInsideParens) {
616 EXPECT_EQ("functionCall({\n"
617 " int i;\n"
618 "});", format(" functionCall ( {int i;} );"));
619}
620
621TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000622 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000623 "int i;", format(" SOME_MACRO {int i;} int i;"));
624}
625
626TEST_F(FormatTest, LayoutNestedBlocks) {
627 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
628 " struct s {\n"
629 " int i;\n"
630 " };\n"
631 " s kBitsToOs[] = { { 10 } };\n"
632 " for (int i = 0; i < 10; ++i)\n"
633 " return;\n"
634 "}");
635}
636
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000637TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
638 EXPECT_EQ("{}", format("{}"));
639}
640
Alexander Kornienko15757312012-12-06 18:03:27 +0000641//===----------------------------------------------------------------------===//
642// Line break tests.
643//===----------------------------------------------------------------------===//
644
645TEST_F(FormatTest, FormatsFunctionDefinition) {
646 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
647 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000648 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000649}
650
651TEST_F(FormatTest, FormatsAwesomeMethodCall) {
652 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000653 "SomeLongMethodName(SomeReallyLongMethod(\n"
654 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
655 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000656}
657
Daniel Jasper1321eb52012-12-18 21:05:13 +0000658TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000659 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000660 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
661 getLLVMStyleWithColumns(45));
662 verifyFormat("Constructor()\n"
663 " : Inttializer(FitsOnTheLine) {}",
664 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000665
666 verifyFormat(
667 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000668 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000669
670 verifyFormat(
671 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000672 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
673 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
674 verifyGoogleFormat(
675 "SomeClass::Constructor()\n"
676 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
677 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
678 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
679
680 verifyFormat(
681 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000682 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000683 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000684
685 verifyFormat("Constructor()\n"
686 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
687 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
688 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000689 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000690
691 // Here a line could be saved by splitting the second initializer onto two
692 // lines, but that is not desireable.
693 verifyFormat("Constructor()\n"
694 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
695 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000696 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000697
698 verifyGoogleFormat("MyClass::MyClass(int var)\n"
699 " : some_var_(var), // 4 space indent\n"
700 " some_other_var_(var + 1) { // lined up\n"
701 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000702
703 // This test takes VERY long when memoization is broken.
704 verifyGoogleFormat(
705 "Constructor()\n"
706 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
707 " a, a, a,\n"
708 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
709 " a, a, a,\n"
710 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
711 " a, a, a,\n"
712 " a, a, a, a, a, a, a, a, a, a, a)\n"
713 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
714 " a, a, a,\n"
715 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
716 " a, a, a,\n"
717 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
718 " a, a, a,\n"
719 " a, a, a, a, a, a, a, a, a, a, a) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000720}
721
Alexander Kornienko15757312012-12-06 18:03:27 +0000722TEST_F(FormatTest, BreaksAsHighAsPossible) {
723 verifyFormat(
724 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
725 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
726 " f();");
727}
728
Daniel Jasperbac016b2012-12-03 18:12:45 +0000729TEST_F(FormatTest, BreaksDesireably) {
730 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
731 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000732 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000733
734 verifyFormat(
735 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000737
738 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000741
742 verifyFormat(
743 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
745 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
746 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000747
748 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
749 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
750
Daniel Jasper723f0302013-01-02 14:40:02 +0000751 verifyFormat(
752 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
754
Daniel Jasper33182dd2012-12-05 14:57:28 +0000755 // This test case breaks on an incorrect memoization, i.e. an optimization not
756 // taking into account the StopAt value.
757 verifyFormat(
758 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000759 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
760 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
761 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000762
Daniel Jaspercd162382013-01-07 13:26:07 +0000763 verifyFormat("{\n {\n {\n"
764 " Annotation.SpaceRequiredBefore =\n"
765 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
766 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
767 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000768}
769
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000770TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
771 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
772 " GUARDED_BY(aaaaaaaaaaaaa);");
773}
774
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000775TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
776 verifyFormat(
777 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000778 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000779 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000780 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000781 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000782 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000783 verifyFormat(
784 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000785 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000786}
787
Daniel Jasper9cda8002013-01-07 13:08:40 +0000788TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
789 verifyFormat(
790 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
791 " SI->getAlignment(),\n"
792 " SI->getPointerAddressSpaceee());\n");
793 verifyFormat(
794 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
795 " Line.Tokens.front().Tok.getLocation(),\n"
796 " Line.Tokens.back().Tok.getLocation());");
797}
798
Daniel Jaspercf225b62012-12-24 13:43:52 +0000799TEST_F(FormatTest, AlignsAfterAssignments) {
800 verifyFormat(
801 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000802 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000803 verifyFormat(
804 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000805 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000806 verifyFormat(
807 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000808 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000809 verifyFormat(
810 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000811 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000812 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000813 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
814 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
815 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000816}
817
818TEST_F(FormatTest, AlignsAfterReturn) {
819 verifyFormat(
820 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
821 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
822 verifyFormat(
823 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
824 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
825}
826
Daniel Jasper9c837d02013-01-09 07:06:56 +0000827TEST_F(FormatTest, BreaksConditionalExpressions) {
828 verifyFormat(
829 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
830 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
832 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
833 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
834}
835
Nico Weber7d37b8b2013-01-12 01:28:06 +0000836TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
837 verifyFormat("arr[foo ? bar : baz];");
838 verifyFormat("f()[foo ? bar : baz];");
839 verifyFormat("(a + b)[foo ? bar : baz];");
840 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
841}
842
Daniel Jasperbac016b2012-12-03 18:12:45 +0000843TEST_F(FormatTest, AlignsStringLiterals) {
844 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
845 " \"short literal\");");
846 verifyFormat(
847 "looooooooooooooooooooooooongFunction(\n"
848 " \"short literal\"\n"
849 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
850}
851
Alexander Kornienko15757312012-12-06 18:03:27 +0000852TEST_F(FormatTest, AlignsPipes) {
853 verifyFormat(
854 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
855 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
856 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
857 verifyFormat(
858 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
859 " << aaaaaaaaaaaaaaaaaaaa;");
860 verifyFormat(
861 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
862 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
863 verifyFormat(
864 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
865 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
866 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
867 verifyFormat(
868 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
869 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
870 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
871}
872
Daniel Jasperbac016b2012-12-03 18:12:45 +0000873TEST_F(FormatTest, UnderstandsEquals) {
874 verifyFormat(
875 "aaaaaaaaaaaaaaaaa =\n"
876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
877 verifyFormat(
878 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000880 verifyFormat(
881 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000882 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000883 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000884 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000885
Daniel Jasper9cda8002013-01-07 13:08:40 +0000886 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000887 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000888 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000889 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000890}
891
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000892TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000893 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
894 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000895
Daniel Jasper1321eb52012-12-18 21:05:13 +0000896 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
897 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000898
899 verifyFormat(
900 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
901 " Parameter2);");
902
903 verifyFormat(
904 "ShortObject->shortFunction(\n"
905 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
906 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
907
908 verifyFormat("loooooooooooooongFunction(\n"
909 " LoooooooooooooongObject->looooooooooooooooongFunction());");
910
911 verifyFormat(
912 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
913 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
914
Daniel Jasper46a46a22013-01-07 07:13:20 +0000915 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000916 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000917 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000918 verifyFormat(
919 "aaaaaaaaaaa->aaaaaaaaa(\n"
920 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
921 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000922}
923
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000924TEST_F(FormatTest, WrapsTemplateDeclarations) {
925 verifyFormat("template <typename T>\n"
926 "virtual void loooooooooooongFunction(int Param1, int Param2);");
927 verifyFormat(
928 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
929 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
930 verifyFormat(
931 "template <typename T>\n"
932 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
933 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000934 verifyFormat(
935 "template <typename T>\n"
936 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
937 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000939 verifyFormat("template <typename T>\n"
940 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
941 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000942 verifyFormat(
943 "template <typename T1, typename T2 = char, typename T3 = char,\n"
944 " typename T4 = char>\n"
945 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000946}
947
Daniel Jasperbac016b2012-12-03 18:12:45 +0000948TEST_F(FormatTest, UnderstandsTemplateParameters) {
949 verifyFormat("A<int> a;");
950 verifyFormat("A<A<A<int> > > a;");
951 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
952 verifyFormat("bool x = a < 1 || 2 > a;");
953 verifyFormat("bool x = 5 < f<int>();");
954 verifyFormat("bool x = f<int>() > 5;");
955 verifyFormat("bool x = 5 < a<int>::x;");
956 verifyFormat("bool x = a < 4 ? a > 2 : false;");
957 verifyFormat("bool x = f() ? a < 2 : a > 2;");
958
959 verifyGoogleFormat("A<A<int>> a;");
960 verifyGoogleFormat("A<A<A<int>>> a;");
961 verifyGoogleFormat("A<A<A<A<int>>>> a;");
962
963 verifyFormat("test >> a >> b;");
964 verifyFormat("test << a >> b;");
965
966 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000967 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000968}
969
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000970TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000971 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000972 verifyFormat("f(-1, -2, -3);");
973 verifyFormat("a[-1] = 5;");
974 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000975 verifyFormat("if (i == -1) {}");
976 verifyFormat("if (i != -1) {}");
977 verifyFormat("if (i > -1) {}");
978 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +0000979 verifyFormat("++(a->f());");
980 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +0000981 verifyFormat("(a->f())++;");
982 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000983 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000984
985 verifyFormat("a-- > b;");
986 verifyFormat("b ? -a : c;");
987 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000988 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000989 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000990 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +0000991
992 verifyFormat("return -1;");
993 verifyFormat("switch (a) {\n"
994 "case -1:\n"
995 " break;\n"
996 "}");
Nico Webercc191d12013-01-12 05:41:23 +0000997
998 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
999 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001000}
1001
1002TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001003 verifyFormat("bool operator<();");
1004 verifyFormat("bool operator>();");
1005 verifyFormat("bool operator=();");
1006 verifyFormat("bool operator==();");
1007 verifyFormat("bool operator!=();");
1008 verifyFormat("int operator+();");
1009 verifyFormat("int operator++();");
1010 verifyFormat("bool operator();");
1011 verifyFormat("bool operator()();");
1012 verifyFormat("bool operator[]();");
1013 verifyFormat("operator bool();");
1014 verifyFormat("operator SomeType<int>();");
1015 verifyFormat("void *operator new(std::size_t size);");
1016 verifyFormat("void *operator new[](std::size_t size);");
1017 verifyFormat("void operator delete(void *ptr);");
1018 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001019}
1020
Daniel Jasper088dab52013-01-11 16:09:04 +00001021TEST_F(FormatTest, UnderstandsNewAndDelete) {
1022 verifyFormat("A *a = new A;");
1023 verifyFormat("A *a = new (placement) A;");
1024 verifyFormat("delete a;");
1025 verifyFormat("delete (A *)a;");
1026}
1027
Daniel Jasper5d334402013-01-02 08:57:10 +00001028TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001029 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001030 verifyFormat("f(a, *a);");
1031 verifyFormat("f(*a);");
1032 verifyFormat("int a = b * 10;");
1033 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001034 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001035 verifyFormat("int a += b * c;");
1036 verifyFormat("int a -= b * c;");
1037 verifyFormat("int a *= b * c;");
1038 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001039 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001040 verifyFormat("int a = *b * c;");
1041 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001042 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001043 verifyFormat("return 10 * b;");
1044 verifyFormat("return *b * *c;");
1045 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001046 verifyFormat("f(b ? *c : *d);");
1047 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001048 verifyFormat("*b = a;");
1049 verifyFormat("a * ~b;");
1050 verifyFormat("a * !b;");
1051 verifyFormat("a * +b;");
1052 verifyFormat("a * -b;");
1053 verifyFormat("a * ++b;");
1054 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001055 verifyFormat("a[4] * b;");
1056 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001057 verifyFormat("a * [self dostuff];");
1058 verifyFormat("a * (a + b);");
1059 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001060 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001061
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001062 verifyFormat("InvalidRegions[*R] = 0;");
1063
Daniel Jasper8b39c662012-12-10 18:59:13 +00001064 verifyFormat("A<int *> a;");
1065 verifyFormat("A<int **> a;");
1066 verifyFormat("A<int *, int *> a;");
1067 verifyFormat("A<int **, int **> a;");
1068
Daniel Jasper2db356d2013-01-08 20:03:18 +00001069 verifyFormat(
1070 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1072
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001073 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001074 verifyGoogleFormat("A<int*> a;");
1075 verifyGoogleFormat("A<int**> a;");
1076 verifyGoogleFormat("A<int*, int*> a;");
1077 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001078 verifyGoogleFormat("f(b ? *c : *d);");
1079 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001080
1081 verifyFormat("a = *(x + y);");
1082 verifyFormat("a = &(x + y);");
1083 verifyFormat("*(x + y).call();");
1084 verifyFormat("&(x + y)->call();");
1085 verifyFormat("&(*I).first");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001086}
1087
Daniel Jasper4981bd02013-01-13 08:01:36 +00001088TEST_F(FormatTest, FormatsCasts) {
1089 verifyFormat("Type *A = static_cast<Type *>(P);");
1090 verifyFormat("Type *A = (Type *)P;");
1091 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1092 verifyFormat("int a = (int)(2.0f);");
1093
1094 // FIXME: These also need to be identified.
1095 verifyFormat("int a = (int) 2.0f;");
1096 verifyFormat("int a = (int) * b;");
1097
1098 // These are not casts.
1099 verifyFormat("void f(int *) {}");
1100 verifyFormat("void f(int *);");
1101 verifyFormat("void f(int *) = 0;");
1102 verifyFormat("void f(SmallVector<int>) {}");
1103 verifyFormat("void f(SmallVector<int>);");
1104 verifyFormat("void f(SmallVector<int>) = 0;");
1105}
1106
Daniel Jasper46ef8522013-01-10 13:08:12 +00001107TEST_F(FormatTest, FormatsFunctionTypes) {
1108 // FIXME: Determine the cases that need a space after the return type and fix.
1109 verifyFormat("A<bool()> a;");
1110 verifyFormat("A<SomeType()> a;");
1111 verifyFormat("A<void(*)(int, std::string)> a;");
1112
1113 verifyFormat("int(*func)(void *);");
1114}
1115
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001116TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001117 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001118 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001119 verifyFormat(
1120 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1121 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001122 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001123}
1124
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001125TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1126 verifyFormat("(a)->b();");
1127 verifyFormat("--a;");
1128}
1129
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001130TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001131 verifyFormat("#include <string>");
1132 verifyFormat("#include <a/b/c.h>");
1133 verifyFormat("#include \"a/b/string\"");
1134 verifyFormat("#include \"string.h\"");
1135 verifyFormat("#include \"string.h\"");
1136 verifyFormat("#include <a-a>");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001137
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001138 verifyFormat("#import <string>");
1139 verifyFormat("#import <a/b/c.h>");
1140 verifyFormat("#import \"a/b/string\"");
1141 verifyFormat("#import \"string.h\"");
1142 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001143}
1144
Alexander Kornienko15757312012-12-06 18:03:27 +00001145//===----------------------------------------------------------------------===//
1146// Error recovery tests.
1147//===----------------------------------------------------------------------===//
1148
Daniel Jasper700e7102013-01-10 09:26:47 +00001149TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1150 verifyFormat("void f() { return } 42");
1151}
1152
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001153TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1154 verifyFormat("int aaaaaaaa =\n"
1155 " // Overly long comment\n"
1156 " b;", getLLVMStyleWithColumns(20));
1157 verifyFormat("function(\n"
1158 " ShortArgument,\n"
1159 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1160}
1161
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001162TEST_F(FormatTest, IncorrectAccessSpecifier) {
1163 verifyFormat("public:");
1164 verifyFormat("class A {\n"
1165 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001166 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001167 "};");
1168 verifyFormat("public\n"
1169 "int qwerty;");
1170 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001171 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001172 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001173 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001174 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001175 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001176}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001177
Alexander Kornienko393b0082012-12-04 15:40:36 +00001178TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1179 verifyFormat("{");
1180}
1181
1182TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001183 verifyFormat("do {}");
1184 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001185 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001186 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001187 "wheeee(fun);");
1188 verifyFormat("do {\n"
1189 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001190 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001191}
1192
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001193TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001194 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001195 verifyFormat("switch {\n foo;\n foo();\n}");
1196 verifyFormat("for {\n foo;\n foo();\n}");
1197 verifyFormat("while {\n foo;\n foo();\n}");
1198 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001199}
1200
Daniel Jasper1f42f112013-01-04 18:52:56 +00001201TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1202 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001203 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001204}
1205
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001206TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001207 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1208 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1209 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1210 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001211
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001212 EXPECT_EQ("{\n"
1213 " {\n"
1214 " breakme(\n"
1215 " qwe);\n"
1216 "}\n", format("{\n"
1217 " {\n"
1218 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001219 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001220}
1221
Manuel Klimek2851c162013-01-10 14:36:46 +00001222TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1223 verifyFormat(
1224 "int x = {\n"
1225 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001226 " b(alongervariable)\n"
1227 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001228}
1229
1230TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1231 verifyFormat(
1232 "Aaa({\n"
1233 " int i;\n"
1234 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1235 " ccccccccccccccccc));");
1236}
1237
Manuel Klimek517e8942013-01-11 17:54:10 +00001238TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1239 verifyFormat("void f() { return 42; }");
1240 verifyFormat("void f() {\n"
1241 " // Comment\n"
1242 "}");
1243 verifyFormat("{\n"
1244 "#error {\n"
1245 " int a;\n"
1246 "}");
1247 verifyFormat("{\n"
1248 " int a;\n"
1249 "#error {\n"
1250 "}");
1251}
1252
Manuel Klimek606e07e2013-01-11 18:13:04 +00001253TEST_F(FormatTest, BracedInitListWithElaboratedTypeSpecifier) {
1254 verifyFormat("struct foo a = { bar };\nint n;");
1255}
1256
Manuel Klimek517e8942013-01-11 17:54:10 +00001257// FIXME: This breaks the order of the unwrapped lines:
1258// TEST_F(FormatTest, OrderUnwrappedLines) {
1259// verifyFormat("{\n"
1260// " bool a; //\n"
1261// "#error {\n"
1262// " int a;\n"
1263// "}");
1264// }
1265
Nico Webercf4a79c2013-01-08 17:56:31 +00001266//===----------------------------------------------------------------------===//
1267// Objective-C tests.
1268//===----------------------------------------------------------------------===//
1269
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001270TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1271 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1272 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1273 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001274 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001275 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1276 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1277 format("-(NSInteger)Method3:(id)anObject;"));
1278 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1279 format("-(NSInteger)Method4:(id)anObject;"));
1280 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1281 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1282 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1283 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001284 EXPECT_EQ(
1285 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1286 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001287
1288 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001289 EXPECT_EQ(
1290 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1291 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1292 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1293 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1294 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1295 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1296 format(
1297 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1298 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1299 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1300 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1301 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1302 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001303
1304 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001305 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001306 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1307 // protocol lists (but not for template classes):
1308 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001309
1310 verifyFormat("- (int(*)())foo:(int(*)())f;");
1311 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1312
1313 // If there's no return type (very rare in practice!), LLVM and Google style
1314 // agree.
1315 verifyFormat("- foo:(int)f;");
1316 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001317}
1318
Daniel Jasper886568d2013-01-09 08:36:49 +00001319TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001320 verifyFormat("int (^Block)(int, int);");
1321 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001322}
1323
Nico Weber27d13672013-01-09 20:25:35 +00001324TEST_F(FormatTest, FormatObjCInterface) {
1325 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001326 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001327 "@public\n"
1328 " int field1;\n"
1329 "@protected\n"
1330 " int field2;\n"
1331 "@private\n"
1332 " int field3;\n"
1333 "@package\n"
1334 " int field4;\n"
1335 "}\n"
1336 "+ (id)init;\n"
1337 "@end");
1338
Nico Weber27d13672013-01-09 20:25:35 +00001339 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1340 " @public\n"
1341 " int field1;\n"
1342 " @protected\n"
1343 " int field2;\n"
1344 " @private\n"
1345 " int field3;\n"
1346 " @package\n"
1347 " int field4;\n"
1348 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001349 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001350 "@end");
1351
1352 verifyFormat("@interface Foo\n"
1353 "+ (id)init;\n"
1354 "// Look, a comment!\n"
1355 "- (int)answerWith:(int)i;\n"
1356 "@end");
1357
1358 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001359 "@end\n"
1360 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001361 "@end");
1362
1363 verifyFormat("@interface Foo : Bar\n"
1364 "+ (id)init;\n"
1365 "@end");
1366
Nico Weber5f500df2013-01-10 20:12:55 +00001367 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001368 "+ (id)init;\n"
1369 "@end");
1370
Nico Weber5f500df2013-01-10 20:12:55 +00001371 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001372 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001373 "@end");
1374
Nico Webered91bba2013-01-10 19:19:14 +00001375 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001376 "+ (id)init;\n"
1377 "@end");
1378
Nico Webered91bba2013-01-10 19:19:14 +00001379 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001380 "+ (id)init;\n"
1381 "@end");
1382
Nico Weber5f500df2013-01-10 20:12:55 +00001383 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001384 "+ (id)init;\n"
1385 "@end");
1386
Nico Weber5f500df2013-01-10 20:12:55 +00001387 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001388 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001389 "@end");
1390
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001391 verifyFormat("@interface Foo {\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 {\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 : Bar <Baz, Quux> {\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 (HackStuff) {\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 () {\n"
1416 " int _i;\n"
1417 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001418 "+ (id)init;\n"
1419 "@end");
1420
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001421 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1422 " int _i;\n"
1423 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001424 "+ (id)init;\n"
1425 "@end");
1426}
1427
Nico Weber50767d82013-01-09 23:25:37 +00001428TEST_F(FormatTest, FormatObjCImplementation) {
1429 verifyFormat("@implementation Foo : NSObject {\n"
1430 "@public\n"
1431 " int field1;\n"
1432 "@protected\n"
1433 " int field2;\n"
1434 "@private\n"
1435 " int field3;\n"
1436 "@package\n"
1437 " int field4;\n"
1438 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001439 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001440 "@end");
1441
1442 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1443 " @public\n"
1444 " int field1;\n"
1445 " @protected\n"
1446 " int field2;\n"
1447 " @private\n"
1448 " int field3;\n"
1449 " @package\n"
1450 " int field4;\n"
1451 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001452 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001453 "@end");
1454
1455 verifyFormat("@implementation Foo\n"
1456 "+ (id)init {\n"
1457 " if (true)\n"
1458 " return nil;\n"
1459 "}\n"
1460 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001461 "- (int)answerWith:(int)i {\n"
1462 " return i;\n"
1463 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001464 "+ (int)answerWith:(int)i {\n"
1465 " return i;\n"
1466 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001467 "@end");
1468
1469 verifyFormat("@implementation Foo\n"
1470 "@end\n"
1471 "@implementation Bar\n"
1472 "@end");
1473
1474 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001475 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001476 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001477 "@end");
1478
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001479 verifyFormat("@implementation Foo {\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
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001485 verifyFormat("@implementation Foo : Bar {\n"
1486 " int _i;\n"
1487 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001488 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001489 "@end");
1490
Nico Webered91bba2013-01-10 19:19:14 +00001491 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001492 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001493 "@end");
1494}
1495
Nico Weber1abe6ea2013-01-09 21:15:03 +00001496TEST_F(FormatTest, FormatObjCProtocol) {
1497 verifyFormat("@protocol Foo\n"
1498 "@property(weak) id delegate;\n"
1499 "- (NSUInteger)numberOfThings;\n"
1500 "@end");
1501
Nico Weber5f500df2013-01-10 20:12:55 +00001502 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001503 "- (NSUInteger)numberOfThings;\n"
1504 "@end");
1505
Nico Weber5f500df2013-01-10 20:12:55 +00001506 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001507 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001508 "@end");
1509
Nico Weber1abe6ea2013-01-09 21:15:03 +00001510 verifyFormat("@protocol Foo;\n"
1511 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001512
1513 verifyFormat("@protocol Foo\n"
1514 "@end\n"
1515 "@protocol Bar\n"
1516 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001517
1518 verifyFormat("@protocol myProtocol\n"
1519 "- (void)mandatoryWithInt:(int)i;\n"
1520 "@optional\n"
1521 "- (void)optional;\n"
1522 "@required\n"
1523 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001524 "@optional\n"
1525 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001526 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001527}
1528
Nico Weberbcfdd262013-01-12 06:18:40 +00001529TEST_F(FormatTest, FormatObjCMethodExpr) {
1530 verifyFormat("[foo bar:baz];");
1531 verifyFormat("return [foo bar:baz];");
1532 verifyFormat("f([foo bar:baz]);");
1533 verifyFormat("f(2, [foo bar:baz]);");
1534 verifyFormat("f(2, a ? b : c);");
1535 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1536
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];");
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] : [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 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1563 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1564 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1565 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1566 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1567 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1568 // Whew!
1569
1570 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1571 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1572 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1573 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1574 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001575 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001576 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001577
Nico Weberbcfdd262013-01-12 06:18:40 +00001578 verifyFormat("arr[[self indexForFoo:a]];");
1579 verifyFormat("throw [self errorFor:a];");
1580 verifyFormat("@throw [self errorFor:a];");
1581
Nico Webere8ccc812013-01-12 22:48:47 +00001582 // This tests that the formatter doesn't break after "backing" but before ":",
1583 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001584 verifyFormat(
1585 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001586 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1587 " backing:NSBackingStoreBuffered defer:YES]))");
1588
Nico Webere8ccc812013-01-12 22:48:47 +00001589 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1590 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001591
1592}
1593
Nico Weber581f5572013-01-07 15:56:25 +00001594TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001595 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001596 verifyFormat("@catch");
1597 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001598 verifyFormat("@compatibility_alias");
1599 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001600 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001601 verifyFormat("@encode");
1602 verifyFormat("@end");
1603 verifyFormat("@finally");
1604 verifyFormat("@implementation");
1605 verifyFormat("@import");
1606 verifyFormat("@interface");
1607 verifyFormat("@optional");
1608 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001609 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001610 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001611 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001612 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001613 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001614 verifyFormat("@required");
1615 verifyFormat("@selector");
1616 verifyFormat("@synchronized");
1617 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001618 verifyFormat("@throw");
1619 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001620
Nico Webercb4d6902013-01-08 19:40:21 +00001621 verifyFormat("@\"String\"");
1622 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001623 verifyFormat("@+4.8");
1624 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001625 verifyFormat("@1LL");
1626 verifyFormat("@.5");
1627 verifyFormat("@'c'");
1628 verifyFormat("@true");
1629 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001630 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001631 verifyFormat("@[");
1632 verifyFormat("@{");
1633
Nico Weber581f5572013-01-07 15:56:25 +00001634 EXPECT_EQ("@interface", format("@ interface"));
1635
1636 // The precise formatting of this doesn't matter, nobody writes code like
1637 // this.
1638 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001639}
1640
Nico Weberc31689a2013-01-08 19:15:23 +00001641TEST_F(FormatTest, ObjCSnippets) {
1642 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001643 verifyFormat("@autoreleasepool {\n"
1644 " foo();\n"
1645 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001646 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001647 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001648 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001649 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001650 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001651 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001652 verifyFormat("@synchronized(self) {\n"
1653 " f();\n"
1654 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001655
Nico Weber70848232013-01-10 21:30:42 +00001656 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1657 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1658
Nico Webercf4a79c2013-01-08 17:56:31 +00001659 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001660 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1661 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001662}
1663
Daniel Jaspercd162382013-01-07 13:26:07 +00001664} // end namespace tooling
1665} // end namespace clang