blob: c6f1c8e4ec7d0a4079c36febc743b6776d48119c [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;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +000053 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
54 if (i != 0) MessedUp[i - 1] = '\n';
Manuel Klimek526ed112013-01-09 15:25:02 +000055 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
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000081 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
82 FormatStyle Style = getGoogleStyle();
83 Style.ColumnLimit = ColumnLimit;
84 return Style;
85 }
86
Manuel Klimek060143e2013-01-02 18:33:23 +000087 void verifyFormat(llvm::StringRef Code,
88 const FormatStyle &Style = getLLVMStyle()) {
89 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000090 }
91
92 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +000093 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +000094 }
95};
96
Manuel Klimek526ed112013-01-09 15:25:02 +000097TEST_F(FormatTest, MessUp) {
98 EXPECT_EQ("1 2 3", messUp("1 2 3"));
99 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
100 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
101 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
102 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
103}
104
Alexander Kornienko15757312012-12-06 18:03:27 +0000105//===----------------------------------------------------------------------===//
106// Basic function tests.
107//===----------------------------------------------------------------------===//
108
Daniel Jasperbac016b2012-12-03 18:12:45 +0000109TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
110 EXPECT_EQ(";", format(";"));
111}
112
113TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
114 EXPECT_EQ("int i;", format(" int i;"));
115 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
116 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
117 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
118}
119
120TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
121 EXPECT_EQ("int i;", format("int\ni;"));
122}
123
124TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000125 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000126}
127
Alexander Kornienko15757312012-12-06 18:03:27 +0000128TEST_F(FormatTest, FormatsNestedCall) {
129 verifyFormat("Method(f1, f2(f3));");
130 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper4e9008a2013-01-13 08:19:51 +0000131 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000132}
133
Alexander Kornienko15757312012-12-06 18:03:27 +0000134//===----------------------------------------------------------------------===//
135// Tests for control statements.
136//===----------------------------------------------------------------------===//
137
138TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000139 verifyFormat("if (true)\n f();\ng();");
140 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000141 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000142 verifyGoogleFormat("if (a)\n"
143 " // comment\n"
144 " f();");
145 verifyFormat("if (a) return;", getGoogleStyleWithColumns(14));
146 verifyFormat("if (a)\n return;", getGoogleStyleWithColumns(13));
Daniel Jasperfd0ca972013-01-14 16:02:06 +0000147 verifyFormat("if (aaaaaaaaa)\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000148 " return;", getGoogleStyleWithColumns(14));
Alexander Kornienko15757312012-12-06 18:03:27 +0000149}
150
151TEST_F(FormatTest, ParseIfElse) {
152 verifyFormat("if (true)\n"
153 " if (true)\n"
154 " if (true)\n"
155 " f();\n"
156 " else\n"
157 " g();\n"
158 " else\n"
159 " h();\n"
160 "else\n"
161 " i();");
162 verifyFormat("if (true)\n"
163 " if (true)\n"
164 " if (true) {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000165 " if (true)\n"
166 " f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000167 " } else {\n"
168 " g();\n"
169 " }\n"
170 " else\n"
171 " h();\n"
172 "else {\n"
173 " i();\n"
174 "}");
175}
176
177TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000178 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000179 verifyFormat("if (a)\n"
180 " f();\n"
181 "else if (b)\n"
182 " g();\n"
183 "else\n"
184 " h();");
185}
186
Daniel Jasperbac016b2012-12-03 18:12:45 +0000187TEST_F(FormatTest, FormatsForLoop) {
188 verifyFormat(
189 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000190 " ++VeryVeryLongLoopVariable)\n"
191 " ;");
192 verifyFormat("for (;;)\n"
193 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000194 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000195 verifyFormat("for (;;) {\n"
196 " f();\n"
197 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000198
199 verifyFormat(
200 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
201 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000202 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000203
204 verifyFormat(
205 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000206 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000207}
208
209TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000210 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000211 verifyFormat("while (true)\n"
212 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000213 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000214 verifyFormat("while () {\n"
215 " f();\n"
216 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217}
218
Alexander Kornienko15757312012-12-06 18:03:27 +0000219TEST_F(FormatTest, FormatsDoWhile) {
220 verifyFormat("do {\n"
221 " do_something();\n"
222 "} while (something());");
223 verifyFormat("do\n"
224 " do_something();\n"
225 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000226}
227
Alexander Kornienko15757312012-12-06 18:03:27 +0000228TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000229 verifyFormat("switch (x) {\n"
230 "case 1:\n"
231 " f();\n"
232 " break;\n"
233 "case kFoo:\n"
234 "case ns::kBar:\n"
235 "case kBaz:\n"
236 " break;\n"
237 "default:\n"
238 " g();\n"
239 " break;\n"
240 "}");
241 verifyFormat("switch (x) {\n"
242 "case 1: {\n"
243 " f();\n"
244 " break;\n"
245 "}\n"
246 "}");
247 verifyFormat("switch (test)\n"
248 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000249 verifyGoogleFormat("switch (x) {\n"
250 " case 1:\n"
251 " f();\n"
252 " break;\n"
253 " case kFoo:\n"
254 " case ns::kBar:\n"
255 " case kBaz:\n"
256 " break;\n"
257 " default:\n"
258 " g();\n"
259 " break;\n"
260 "}");
261 verifyGoogleFormat("switch (x) {\n"
262 " case 1: {\n"
263 " f();\n"
264 " break;\n"
265 " }\n"
266 "}");
267 verifyGoogleFormat("switch (test)\n"
268 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000269}
270
Alexander Kornienko15757312012-12-06 18:03:27 +0000271TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000272 verifyFormat("void f() {\n"
273 " some_code();\n"
274 "test_label:\n"
275 " some_other_code();\n"
276 " {\n"
277 " some_more_code();\n"
278 " another_label:\n"
279 " some_more_code();\n"
280 " }\n"
281 "}");
282 verifyFormat("some_code();\n"
283 "test_label:\n"
284 "some_other_code();");
285}
286
Alexander Kornienko15757312012-12-06 18:03:27 +0000287//===----------------------------------------------------------------------===//
288// Tests for comments.
289//===----------------------------------------------------------------------===//
290
291TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000292 verifyFormat("// line 1\n"
293 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000294 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000295
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000296 verifyFormat("void f() {\n"
297 " // Doesn't do anything\n"
298 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000299 verifyFormat("void f(int i, // some comment (probably for i)\n"
300 " int j, // some comment (probably for j)\n"
301 " int k); // some comment (probably for k)");
302 verifyFormat("void f(int i,\n"
303 " // some comment (probably for j)\n"
304 " int j,\n"
305 " // some comment (probably for k)\n"
306 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000307
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000308 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000309 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000310
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000311 verifyFormat("enum E {\n"
312 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000313 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000314 " VAL_B\n"
315 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000316
317 verifyFormat(
318 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000319 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000320 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
321 " // Comment inside a statement.\n"
322 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000323
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000324 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000325 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000326
327 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000328}
329
330TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000331 verifyFormat("f(/*test=*/ true);");
332}
333
Alexander Kornienko15757312012-12-06 18:03:27 +0000334//===----------------------------------------------------------------------===//
335// Tests for classes, namespaces, etc.
336//===----------------------------------------------------------------------===//
337
338TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000339 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000340}
341
342TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
343 verifyFormat("class A {\n"
344 "public:\n"
345 "protected:\n"
346 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000347 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000348 "};");
349 verifyGoogleFormat("class A {\n"
350 " public:\n"
351 " protected:\n"
352 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000353 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000354 "};");
355}
356
357TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000358 verifyFormat("class A : public B {};");
359 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000360}
361
Manuel Klimekde768542013-01-07 18:10:23 +0000362TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000363 verifyFormat("class A {} a, b;");
364 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000365 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000366}
367
Alexander Kornienko15757312012-12-06 18:03:27 +0000368TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000369 verifyFormat("enum {\n"
370 " Zero,\n"
371 " One = 1,\n"
372 " Two = One + 1,\n"
373 " Three = (One + Two),\n"
374 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
375 " Five = (One, Two, Three, Four, 5)\n"
376 "};");
377 verifyFormat("enum Enum {\n"
378 "};");
379 verifyFormat("enum {\n"
380 "};");
381}
382
Nico Weberefaddc02013-01-14 05:49:49 +0000383TEST_F(FormatTest, FormatsBitfields) {
384 verifyFormat("struct Bitfields {\n"
385 " unsigned sClass : 8;\n"
386 " unsigned ValueKind : 2;\n"
387 "};");
388}
389
Alexander Kornienko15757312012-12-06 18:03:27 +0000390TEST_F(FormatTest, FormatsNamespaces) {
391 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000392 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000393 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000394 "}");
395 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000396 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000397 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000398 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000399 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000400 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000401 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000402 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000403 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000404 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000405 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000406}
407
Nico Webera9ccdd12013-01-07 16:36:17 +0000408TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000409 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
410 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000411 verifyFormat("try {\n"
412 " throw a * b;\n"
413 "}\n"
414 "catch (int a) {\n"
415 " // Do nothing.\n"
416 "}\n"
417 "catch (...) {\n"
418 " exit(42);\n"
419 "}");
420
421 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000422 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000423 "catch (...) {\n"
424 " return 5;\n"
425 "}");
426 verifyFormat("class A {\n"
427 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000428 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000429 " catch (...) {\n"
430 " throw;\n"
431 " }\n"
432 "};\n");
433}
434
435TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000436 verifyFormat("@try {\n"
437 " f();\n"
438 "}\n"
439 "@catch (NSException e) {\n"
440 " @throw;\n"
441 "}\n"
442 "@finally {\n"
443 " exit(42);\n"
444 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000445}
446
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000447TEST_F(FormatTest, StaticInitializers) {
448 verifyFormat("static SomeClass SC = { 1, 'a' };");
449
450 // FIXME: Format like enums if the static initializer does not fit on a line.
451 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000452 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000453 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
454 "};");
455
456 verifyFormat(
457 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
458 " looooooooooooooooooooooooooooooooooongname,\n"
459 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000460}
461
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000462TEST_F(FormatTest, NestedStaticInitializers) {
463 verifyFormat("static A x = { { {} } };\n");
464 verifyFormat(
465 "static A x = {\n"
466 " { { init1, init2, init3, init4 }, { init1, init2, init3, init4 } }\n"
467 "};\n");
468 verifyFormat(
469 "somes Status::global_reps[3] = {\n"
470 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
471 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
472 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
473 "};");
474 verifyFormat(
475 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
476 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
477 " } };");
478
479 // FIXME: We might at some point want to handle this similar to parameters
480 // lists, where we have an option to put each on a single line.
481 verifyFormat("struct {\n"
482 " unsigned bit;\n"
483 " const char *const name;\n"
484 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
485 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
486}
487
Manuel Klimeka080a182013-01-02 16:30:12 +0000488TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
489 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
490 " \\\n"
491 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
492}
493
Daniel Jasper71607512013-01-07 10:48:50 +0000494TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000495 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
496 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000497}
498
Manuel Klimeka080a182013-01-02 16:30:12 +0000499TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
500 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000501 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000502}
503
504TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
505 EXPECT_EQ("#line 42 \"test\"\n",
506 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000507 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000508 format("# \\\n define \\\n A \\\n B\n",
509 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000510}
511
512TEST_F(FormatTest, EndOfFileEndsPPDirective) {
513 EXPECT_EQ("#line 42 \"test\"",
514 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000515 EXPECT_EQ("#define A B",
516 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000517}
518
Manuel Klimek060143e2013-01-02 18:33:23 +0000519TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000520 // If the macro fits in one line, we still do not get the full
521 // line, as only the next line decides whether we need an escaped newline and
522 // thus use the last column.
523 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000524
Manuel Klimekd544c572013-01-07 09:24:17 +0000525 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
526 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000527 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000528
529 verifyFormat("#define A A\n#define A A");
530 verifyFormat("#define A(X) A\n#define A A");
531
532 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
533 verifyFormat("#define Something \\\n"
534 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000535}
536
537TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000538 EXPECT_EQ("// some comment\n"
539 "#include \"a.h\"\n"
540 "#define A(A,\\\n"
541 " B)\n"
542 "#include \"b.h\"\n"
543 "// some comment\n",
544 format(" // some comment\n"
545 " #include \"a.h\"\n"
546 "#define A(A,\\\n"
547 " B)\n"
548 " #include \"b.h\"\n"
549 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000550}
551
Manuel Klimekd4397b92013-01-04 23:34:14 +0000552TEST_F(FormatTest, LayoutSingleHash) {
553 EXPECT_EQ("#\na;", format("#\na;"));
554}
555
556TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
557 EXPECT_EQ("#define A \\\n"
558 " c; \\\n"
559 " e;\n"
560 "f;", format("#define A c; e;\n"
561 "f;", getLLVMStyleWithColumns(14)));
562}
563
564TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000565 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000566}
567
568TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000569 EXPECT_EQ("# define A\\\n b;",
570 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000571}
572
573TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000574 EXPECT_EQ("int x,\n"
575 "#define A\n"
576 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000577}
578
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000579TEST_F(FormatTest, HashInMacroDefinition) {
580 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
581 verifyFormat("#define A \\\n"
582 " { \\\n"
583 " f(#c);\\\n"
584 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000585
586 verifyFormat("#define A(X) \\\n"
587 " void function##X()", getLLVMStyleWithColumns(22));
588
589 verifyFormat("#define A(a, b, c) \\\n"
590 " void a##b##c()", getLLVMStyleWithColumns(22));
591
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000592 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000593}
594
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000595TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
596 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
597}
598
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000599TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000600 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000601}
602
Manuel Klimeka5342db2013-01-06 20:07:31 +0000603TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
604 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
605 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
606 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
607 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
608}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000609
Manuel Klimek95419382013-01-07 07:56:50 +0000610TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000611 EXPECT_EQ(
612 "#define A \\\n int i; \\\n int j;",
613 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000614}
615
Manuel Klimekd544c572013-01-07 09:24:17 +0000616TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
617 verifyFormat("#define A \\\n"
618 " int v( \\\n"
619 " a); \\\n"
620 " int i;", getLLVMStyleWithColumns(11));
621}
622
Manuel Klimeka080a182013-01-02 16:30:12 +0000623TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000624 EXPECT_EQ(
625 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
626 " \\\n"
627 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
628 "\n"
629 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
630 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
631 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
632 "\\\n"
633 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
634 " \n"
635 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
636 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000637}
638
Manuel Klimek526ed112013-01-09 15:25:02 +0000639TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
640 EXPECT_EQ("int\n"
641 "#define A\n"
642 " a;",
643 format("int\n#define A\na;"));
644 verifyFormat(
645 "functionCallTo(someOtherFunction(\n"
646 " withSomeParameters, whichInSequence,\n"
647 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000648 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000649 " withMoreParamters,\n"
650 " whichStronglyInfluenceTheLayout),\n"
651 " andMoreParameters),\n"
652 " trailing);", getLLVMStyleWithColumns(69));
653}
654
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000655TEST_F(FormatTest, LayoutBlockInsideParens) {
656 EXPECT_EQ("functionCall({\n"
657 " int i;\n"
658 "});", format(" functionCall ( {int i;} );"));
659}
660
661TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000662 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000663 "int i;", format(" SOME_MACRO {int i;} int i;"));
664}
665
666TEST_F(FormatTest, LayoutNestedBlocks) {
667 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
668 " struct s {\n"
669 " int i;\n"
670 " };\n"
671 " s kBitsToOs[] = { { 10 } };\n"
672 " for (int i = 0; i < 10; ++i)\n"
673 " return;\n"
674 "}");
675}
676
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000677TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
678 EXPECT_EQ("{}", format("{}"));
679}
680
Alexander Kornienko15757312012-12-06 18:03:27 +0000681//===----------------------------------------------------------------------===//
682// Line break tests.
683//===----------------------------------------------------------------------===//
684
685TEST_F(FormatTest, FormatsFunctionDefinition) {
686 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
687 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000688 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000689}
690
691TEST_F(FormatTest, FormatsAwesomeMethodCall) {
692 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000693 "SomeLongMethodName(SomeReallyLongMethod(\n"
694 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
695 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000696}
697
Daniel Jasper1321eb52012-12-18 21:05:13 +0000698TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000699 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000700 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
701 getLLVMStyleWithColumns(45));
702 verifyFormat("Constructor()\n"
703 " : Inttializer(FitsOnTheLine) {}",
704 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000705
706 verifyFormat(
707 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000708 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000709
710 verifyFormat(
711 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000712 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
713 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
714 verifyGoogleFormat(
715 "SomeClass::Constructor()\n"
716 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
717 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
718 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
719
720 verifyFormat(
721 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000722 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000723 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000724
725 verifyFormat("Constructor()\n"
726 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
727 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
728 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000729 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000730
731 // Here a line could be saved by splitting the second initializer onto two
732 // lines, but that is not desireable.
733 verifyFormat("Constructor()\n"
734 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
735 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000736 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000737
738 verifyGoogleFormat("MyClass::MyClass(int var)\n"
739 " : some_var_(var), // 4 space indent\n"
740 " some_other_var_(var + 1) { // lined up\n"
741 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000742
743 // This test takes VERY long when memoization is broken.
744 verifyGoogleFormat(
745 "Constructor()\n"
746 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
747 " a, a, a,\n"
748 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
749 " a, a, a,\n"
750 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
751 " a, a, a,\n"
752 " a, a, a, a, a, a, a, a, a, a, a)\n"
753 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
754 " a, a, a,\n"
755 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
756 " a, a, a,\n"
757 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
758 " a, a, a,\n"
759 " a, a, a, a, a, a, a, a, a, a, a) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000760}
761
Alexander Kornienko15757312012-12-06 18:03:27 +0000762TEST_F(FormatTest, BreaksAsHighAsPossible) {
763 verifyFormat(
764 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
765 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
766 " f();");
767}
768
Daniel Jasperbac016b2012-12-03 18:12:45 +0000769TEST_F(FormatTest, BreaksDesireably) {
770 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
771 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000772 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000773
774 verifyFormat(
775 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000777
778 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
779 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
780 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000781
782 verifyFormat(
783 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
785 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000787
788 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
789 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
790
Daniel Jasper723f0302013-01-02 14:40:02 +0000791 verifyFormat(
792 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
794
Daniel Jasper33182dd2012-12-05 14:57:28 +0000795 // This test case breaks on an incorrect memoization, i.e. an optimization not
796 // taking into account the StopAt value.
797 verifyFormat(
798 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000799 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
800 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
801 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000802
Daniel Jaspercd162382013-01-07 13:26:07 +0000803 verifyFormat("{\n {\n {\n"
804 " Annotation.SpaceRequiredBefore =\n"
805 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
806 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
807 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000808}
809
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000810TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
811 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
812 " GUARDED_BY(aaaaaaaaaaaaa);");
813}
814
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000815TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
816 verifyFormat(
817 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000818 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000819 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000820 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000821 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000822 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000823 verifyFormat(
824 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000825 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000826}
827
Daniel Jasper9cda8002013-01-07 13:08:40 +0000828TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
829 verifyFormat(
830 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
831 " SI->getAlignment(),\n"
832 " SI->getPointerAddressSpaceee());\n");
833 verifyFormat(
834 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
835 " Line.Tokens.front().Tok.getLocation(),\n"
836 " Line.Tokens.back().Tok.getLocation());");
837}
838
Daniel Jaspercf225b62012-12-24 13:43:52 +0000839TEST_F(FormatTest, AlignsAfterAssignments) {
840 verifyFormat(
841 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000842 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000843 verifyFormat(
844 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000845 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000846 verifyFormat(
847 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000848 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000849 verifyFormat(
850 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000851 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000852 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000853 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
854 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
855 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000856}
857
858TEST_F(FormatTest, AlignsAfterReturn) {
859 verifyFormat(
860 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
861 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
862 verifyFormat(
863 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
864 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
865}
866
Daniel Jasper9c837d02013-01-09 07:06:56 +0000867TEST_F(FormatTest, BreaksConditionalExpressions) {
868 verifyFormat(
869 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
870 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
872 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
873 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
874}
875
Nico Weber7d37b8b2013-01-12 01:28:06 +0000876TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
877 verifyFormat("arr[foo ? bar : baz];");
878 verifyFormat("f()[foo ? bar : baz];");
879 verifyFormat("(a + b)[foo ? bar : baz];");
880 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
881}
882
Daniel Jasperbac016b2012-12-03 18:12:45 +0000883TEST_F(FormatTest, AlignsStringLiterals) {
884 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
885 " \"short literal\");");
886 verifyFormat(
887 "looooooooooooooooooooooooongFunction(\n"
888 " \"short literal\"\n"
889 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
890}
891
Alexander Kornienko15757312012-12-06 18:03:27 +0000892TEST_F(FormatTest, AlignsPipes) {
893 verifyFormat(
894 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
895 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
896 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
897 verifyFormat(
898 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
899 " << aaaaaaaaaaaaaaaaaaaa;");
900 verifyFormat(
901 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
902 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
903 verifyFormat(
904 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
905 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
906 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
907 verifyFormat(
908 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
909 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
910 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
911}
912
Daniel Jasperbac016b2012-12-03 18:12:45 +0000913TEST_F(FormatTest, UnderstandsEquals) {
914 verifyFormat(
915 "aaaaaaaaaaaaaaaaa =\n"
916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
917 verifyFormat(
918 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000919 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000920 verifyFormat(
921 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000922 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000923 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000924 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000925
Daniel Jasper9cda8002013-01-07 13:08:40 +0000926 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000927 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000928 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000929 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000930}
931
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000932TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000933 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
934 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000935
Daniel Jasper1321eb52012-12-18 21:05:13 +0000936 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
937 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000938
939 verifyFormat(
940 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
941 " Parameter2);");
942
943 verifyFormat(
944 "ShortObject->shortFunction(\n"
945 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
946 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
947
948 verifyFormat("loooooooooooooongFunction(\n"
949 " LoooooooooooooongObject->looooooooooooooooongFunction());");
950
951 verifyFormat(
952 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
953 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
954
Daniel Jasper46a46a22013-01-07 07:13:20 +0000955 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000956 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000957 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000958 verifyFormat(
959 "aaaaaaaaaaa->aaaaaaaaa(\n"
960 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
961 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000962}
963
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000964TEST_F(FormatTest, WrapsTemplateDeclarations) {
965 verifyFormat("template <typename T>\n"
966 "virtual void loooooooooooongFunction(int Param1, int Param2);");
967 verifyFormat(
968 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
969 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
970 verifyFormat(
971 "template <typename T>\n"
972 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
973 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000974 verifyFormat(
975 "template <typename T>\n"
976 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
977 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
978 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000979 verifyFormat("template <typename T>\n"
980 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
981 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000982 verifyFormat(
983 "template <typename T1, typename T2 = char, typename T3 = char,\n"
984 " typename T4 = char>\n"
985 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000986}
987
Daniel Jasperbac016b2012-12-03 18:12:45 +0000988TEST_F(FormatTest, UnderstandsTemplateParameters) {
989 verifyFormat("A<int> a;");
990 verifyFormat("A<A<A<int> > > a;");
991 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
992 verifyFormat("bool x = a < 1 || 2 > a;");
993 verifyFormat("bool x = 5 < f<int>();");
994 verifyFormat("bool x = f<int>() > 5;");
995 verifyFormat("bool x = 5 < a<int>::x;");
996 verifyFormat("bool x = a < 4 ? a > 2 : false;");
997 verifyFormat("bool x = f() ? a < 2 : a > 2;");
998
999 verifyGoogleFormat("A<A<int>> a;");
1000 verifyGoogleFormat("A<A<A<int>>> a;");
1001 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1002
1003 verifyFormat("test >> a >> b;");
1004 verifyFormat("test << a >> b;");
1005
1006 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001007 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001008}
1009
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001010TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001011 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001012 verifyFormat("f(-1, -2, -3);");
1013 verifyFormat("a[-1] = 5;");
1014 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001015 verifyFormat("if (i == -1) {}");
1016 verifyFormat("if (i != -1) {}");
1017 verifyFormat("if (i > -1) {}");
1018 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001019 verifyFormat("++(a->f());");
1020 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001021 verifyFormat("(a->f())++;");
1022 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001023 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001024
1025 verifyFormat("a-- > b;");
1026 verifyFormat("b ? -a : c;");
1027 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001028 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001029 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001030 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001031
1032 verifyFormat("return -1;");
1033 verifyFormat("switch (a) {\n"
1034 "case -1:\n"
1035 " break;\n"
1036 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001037
1038 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1039 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001040
1041 verifyFormat("int a = /* confusing comment */ -1;");
1042 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1043 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001044}
1045
1046TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001047 verifyFormat("bool operator<();");
1048 verifyFormat("bool operator>();");
1049 verifyFormat("bool operator=();");
1050 verifyFormat("bool operator==();");
1051 verifyFormat("bool operator!=();");
1052 verifyFormat("int operator+();");
1053 verifyFormat("int operator++();");
1054 verifyFormat("bool operator();");
1055 verifyFormat("bool operator()();");
1056 verifyFormat("bool operator[]();");
1057 verifyFormat("operator bool();");
1058 verifyFormat("operator SomeType<int>();");
1059 verifyFormat("void *operator new(std::size_t size);");
1060 verifyFormat("void *operator new[](std::size_t size);");
1061 verifyFormat("void operator delete(void *ptr);");
1062 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001063}
1064
Daniel Jasper088dab52013-01-11 16:09:04 +00001065TEST_F(FormatTest, UnderstandsNewAndDelete) {
1066 verifyFormat("A *a = new A;");
1067 verifyFormat("A *a = new (placement) A;");
1068 verifyFormat("delete a;");
1069 verifyFormat("delete (A *)a;");
1070}
1071
Daniel Jasper5d334402013-01-02 08:57:10 +00001072TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001073 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001074 verifyFormat("f(a, *a);");
1075 verifyFormat("f(*a);");
1076 verifyFormat("int a = b * 10;");
1077 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001078 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001079 verifyFormat("int a += b * c;");
1080 verifyFormat("int a -= b * c;");
1081 verifyFormat("int a *= b * c;");
1082 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001083 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001084 verifyFormat("int a = *b * c;");
1085 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001086 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001087 verifyFormat("return 10 * b;");
1088 verifyFormat("return *b * *c;");
1089 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001090 verifyFormat("f(b ? *c : *d);");
1091 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001092 verifyFormat("*b = a;");
1093 verifyFormat("a * ~b;");
1094 verifyFormat("a * !b;");
1095 verifyFormat("a * +b;");
1096 verifyFormat("a * -b;");
1097 verifyFormat("a * ++b;");
1098 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001099 verifyFormat("a[4] * b;");
1100 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001101 verifyFormat("a * [self dostuff];");
1102 verifyFormat("a * (a + b);");
1103 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001104 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001105
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001106 verifyFormat("InvalidRegions[*R] = 0;");
1107
Daniel Jasper8b39c662012-12-10 18:59:13 +00001108 verifyFormat("A<int *> a;");
1109 verifyFormat("A<int **> a;");
1110 verifyFormat("A<int *, int *> a;");
1111 verifyFormat("A<int **, int **> a;");
1112
Daniel Jasper2db356d2013-01-08 20:03:18 +00001113 verifyFormat(
1114 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1115 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1116
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001117 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001118 verifyGoogleFormat("A<int*> a;");
1119 verifyGoogleFormat("A<int**> a;");
1120 verifyGoogleFormat("A<int*, int*> a;");
1121 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001122 verifyGoogleFormat("f(b ? *c : *d);");
1123 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001124
1125 verifyFormat("a = *(x + y);");
1126 verifyFormat("a = &(x + y);");
1127 verifyFormat("*(x + y).call();");
1128 verifyFormat("&(x + y)->call();");
1129 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001130
1131 verifyFormat("f(b * /* confusing comment */ ++c);");
1132 verifyFormat(
1133 "int *MyValues = {\n"
1134 " *A, // Operator detection might be confused by the '{'\n"
1135 " *BB // Operator detection might be confused by previous comment\n"
1136 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001137}
1138
Daniel Jasper4981bd02013-01-13 08:01:36 +00001139TEST_F(FormatTest, FormatsCasts) {
1140 verifyFormat("Type *A = static_cast<Type *>(P);");
1141 verifyFormat("Type *A = (Type *)P;");
1142 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1143 verifyFormat("int a = (int)(2.0f);");
1144
1145 // FIXME: These also need to be identified.
1146 verifyFormat("int a = (int) 2.0f;");
1147 verifyFormat("int a = (int) * b;");
1148
1149 // These are not casts.
1150 verifyFormat("void f(int *) {}");
1151 verifyFormat("void f(int *);");
1152 verifyFormat("void f(int *) = 0;");
1153 verifyFormat("void f(SmallVector<int>) {}");
1154 verifyFormat("void f(SmallVector<int>);");
1155 verifyFormat("void f(SmallVector<int>) = 0;");
1156}
1157
Daniel Jasper46ef8522013-01-10 13:08:12 +00001158TEST_F(FormatTest, FormatsFunctionTypes) {
1159 // FIXME: Determine the cases that need a space after the return type and fix.
1160 verifyFormat("A<bool()> a;");
1161 verifyFormat("A<SomeType()> a;");
1162 verifyFormat("A<void(*)(int, std::string)> a;");
1163
1164 verifyFormat("int(*func)(void *);");
1165}
1166
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001167TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001168 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001169 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001170 verifyFormat(
1171 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1172 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001173 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001174}
1175
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001176TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1177 verifyFormat("(a)->b();");
1178 verifyFormat("--a;");
1179}
1180
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001181TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001182 verifyFormat("#include <string>\n"
1183 "#include <a/b/c.h>\n"
1184 "#include \"a/b/string\"\n"
1185 "#include \"string.h\"\n"
1186 "#include \"string.h\"\n"
1187 "#include <a-a>");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001188
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001189 verifyFormat("#import <string>");
1190 verifyFormat("#import <a/b/c.h>");
1191 verifyFormat("#import \"a/b/string\"");
1192 verifyFormat("#import \"string.h\"");
1193 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001194}
1195
Alexander Kornienko15757312012-12-06 18:03:27 +00001196//===----------------------------------------------------------------------===//
1197// Error recovery tests.
1198//===----------------------------------------------------------------------===//
1199
Daniel Jasper700e7102013-01-10 09:26:47 +00001200TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1201 verifyFormat("void f() { return } 42");
1202}
1203
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001204TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1205 verifyFormat("int aaaaaaaa =\n"
1206 " // Overly long comment\n"
1207 " b;", getLLVMStyleWithColumns(20));
1208 verifyFormat("function(\n"
1209 " ShortArgument,\n"
1210 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1211}
1212
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001213TEST_F(FormatTest, IncorrectAccessSpecifier) {
1214 verifyFormat("public:");
1215 verifyFormat("class A {\n"
1216 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001217 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001218 "};");
1219 verifyFormat("public\n"
1220 "int qwerty;");
1221 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001222 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001223 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001224 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001225 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001226 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001227}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001228
Alexander Kornienko393b0082012-12-04 15:40:36 +00001229TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1230 verifyFormat("{");
1231}
1232
1233TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001234 verifyFormat("do {}");
1235 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001236 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001237 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001238 "wheeee(fun);");
1239 verifyFormat("do {\n"
1240 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001241 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001242}
1243
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001244TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001245 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001246 verifyFormat("switch {\n foo;\n foo();\n}");
1247 verifyFormat("for {\n foo;\n foo();\n}");
1248 verifyFormat("while {\n foo;\n foo();\n}");
1249 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001250}
1251
Daniel Jasper1f42f112013-01-04 18:52:56 +00001252TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1253 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001254 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001255}
1256
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001257TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001258 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1259 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1260 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1261 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001262
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001263 EXPECT_EQ("{\n"
1264 " {\n"
1265 " breakme(\n"
1266 " qwe);\n"
1267 "}\n", format("{\n"
1268 " {\n"
1269 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001270 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001271}
1272
Manuel Klimek2851c162013-01-10 14:36:46 +00001273TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1274 verifyFormat(
1275 "int x = {\n"
1276 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001277 " b(alongervariable)\n"
1278 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001279}
1280
1281TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1282 verifyFormat(
1283 "Aaa({\n"
1284 " int i;\n"
1285 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1286 " ccccccccccccccccc));");
1287}
1288
Manuel Klimek517e8942013-01-11 17:54:10 +00001289TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1290 verifyFormat("void f() { return 42; }");
1291 verifyFormat("void f() {\n"
1292 " // Comment\n"
1293 "}");
1294 verifyFormat("{\n"
1295 "#error {\n"
1296 " int a;\n"
1297 "}");
1298 verifyFormat("{\n"
1299 " int a;\n"
1300 "#error {\n"
1301 "}");
1302}
1303
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001304TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1305 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001306 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001307 verifyFormat("class foo a = { bar };\nint n;");
1308 verifyFormat("union foo a = { bar };\nint n;");
1309
1310 // Elaborate types inside function definitions.
1311 verifyFormat("struct foo f() {}\nint n;");
1312 verifyFormat("class foo f() {}\nint n;");
1313 verifyFormat("union foo f() {}\nint n;");
1314
1315 // Templates.
1316 verifyFormat("template <class X> void f() {}\nint n;");
1317 verifyFormat("template <struct X> void f() {}\nint n;");
1318 verifyFormat("template <union X> void f() {}\nint n;");
1319
1320 // Actual definitions...
1321 verifyFormat("struct {} n;");
1322 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1323 verifyFormat("union Z {\n int n;\n} x;");
1324 verifyFormat("class MACRO Z {} n;");
1325 verifyFormat("class MACRO(X) Z {} n;");
1326 verifyFormat("class __attribute__(X) Z {} n;");
1327 verifyFormat("class __declspec(X) Z {} n;");
1328
1329 // Elaborate types where incorrectly parsing the structural element would
1330 // break the indent.
1331 verifyFormat("if (true)\n"
1332 " class X x;\n"
1333 "else\n"
1334 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001335}
1336
Manuel Klimek517e8942013-01-11 17:54:10 +00001337// FIXME: This breaks the order of the unwrapped lines:
1338// TEST_F(FormatTest, OrderUnwrappedLines) {
1339// verifyFormat("{\n"
1340// " bool a; //\n"
1341// "#error {\n"
1342// " int a;\n"
1343// "}");
1344// }
1345
Nico Webercf4a79c2013-01-08 17:56:31 +00001346//===----------------------------------------------------------------------===//
1347// Objective-C tests.
1348//===----------------------------------------------------------------------===//
1349
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001350TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1351 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1352 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1353 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001354 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001355 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1356 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1357 format("-(NSInteger)Method3:(id)anObject;"));
1358 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1359 format("-(NSInteger)Method4:(id)anObject;"));
1360 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1361 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1362 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1363 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001364 EXPECT_EQ(
1365 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1366 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001367
1368 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001369 EXPECT_EQ(
1370 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1371 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1372 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1373 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1374 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1375 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1376 format(
1377 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1378 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1379 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1380 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1381 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1382 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001383
1384 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001385 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001386 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1387 // protocol lists (but not for template classes):
1388 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001389
1390 verifyFormat("- (int(*)())foo:(int(*)())f;");
1391 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1392
1393 // If there's no return type (very rare in practice!), LLVM and Google style
1394 // agree.
1395 verifyFormat("- foo:(int)f;");
1396 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001397}
1398
Daniel Jasper886568d2013-01-09 08:36:49 +00001399TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001400 verifyFormat("int (^Block)(int, int);");
1401 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001402}
1403
Nico Weber27d13672013-01-09 20:25:35 +00001404TEST_F(FormatTest, FormatObjCInterface) {
1405 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001406 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001407 "@public\n"
1408 " int field1;\n"
1409 "@protected\n"
1410 " int field2;\n"
1411 "@private\n"
1412 " int field3;\n"
1413 "@package\n"
1414 " int field4;\n"
1415 "}\n"
1416 "+ (id)init;\n"
1417 "@end");
1418
Nico Weber27d13672013-01-09 20:25:35 +00001419 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1420 " @public\n"
1421 " int field1;\n"
1422 " @protected\n"
1423 " int field2;\n"
1424 " @private\n"
1425 " int field3;\n"
1426 " @package\n"
1427 " int field4;\n"
1428 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001429 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001430 "@end");
1431
1432 verifyFormat("@interface Foo\n"
1433 "+ (id)init;\n"
1434 "// Look, a comment!\n"
1435 "- (int)answerWith:(int)i;\n"
1436 "@end");
1437
1438 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001439 "@end\n"
1440 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001441 "@end");
1442
1443 verifyFormat("@interface Foo : Bar\n"
1444 "+ (id)init;\n"
1445 "@end");
1446
Nico Weber5f500df2013-01-10 20:12:55 +00001447 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001448 "+ (id)init;\n"
1449 "@end");
1450
Nico Weber5f500df2013-01-10 20:12:55 +00001451 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001452 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001453 "@end");
1454
Nico Webered91bba2013-01-10 19:19:14 +00001455 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001456 "+ (id)init;\n"
1457 "@end");
1458
Nico Webered91bba2013-01-10 19:19:14 +00001459 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001460 "+ (id)init;\n"
1461 "@end");
1462
Nico Weber5f500df2013-01-10 20:12:55 +00001463 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001464 "+ (id)init;\n"
1465 "@end");
1466
Nico Weber5f500df2013-01-10 20:12:55 +00001467 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001468 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001469 "@end");
1470
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001471 verifyFormat("@interface Foo {\n"
1472 " int _i;\n"
1473 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001474 "+ (id)init;\n"
1475 "@end");
1476
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001477 verifyFormat("@interface Foo : Bar {\n"
1478 " int _i;\n"
1479 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001480 "+ (id)init;\n"
1481 "@end");
1482
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001483 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1484 " int _i;\n"
1485 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001486 "+ (id)init;\n"
1487 "@end");
1488
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001489 verifyFormat("@interface Foo (HackStuff) {\n"
1490 " int _i;\n"
1491 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001492 "+ (id)init;\n"
1493 "@end");
1494
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001495 verifyFormat("@interface Foo () {\n"
1496 " int _i;\n"
1497 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001498 "+ (id)init;\n"
1499 "@end");
1500
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001501 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1502 " int _i;\n"
1503 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001504 "+ (id)init;\n"
1505 "@end");
1506}
1507
Nico Weber50767d82013-01-09 23:25:37 +00001508TEST_F(FormatTest, FormatObjCImplementation) {
1509 verifyFormat("@implementation Foo : NSObject {\n"
1510 "@public\n"
1511 " int field1;\n"
1512 "@protected\n"
1513 " int field2;\n"
1514 "@private\n"
1515 " int field3;\n"
1516 "@package\n"
1517 " int field4;\n"
1518 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001519 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001520 "@end");
1521
1522 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1523 " @public\n"
1524 " int field1;\n"
1525 " @protected\n"
1526 " int field2;\n"
1527 " @private\n"
1528 " int field3;\n"
1529 " @package\n"
1530 " int field4;\n"
1531 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001532 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001533 "@end");
1534
1535 verifyFormat("@implementation Foo\n"
1536 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001537 " if (true)\n"
1538 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001539 "}\n"
1540 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001541 "- (int)answerWith:(int)i {\n"
1542 " return i;\n"
1543 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001544 "+ (int)answerWith:(int)i {\n"
1545 " return i;\n"
1546 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001547 "@end");
1548
1549 verifyFormat("@implementation Foo\n"
1550 "@end\n"
1551 "@implementation Bar\n"
1552 "@end");
1553
1554 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001555 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001556 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001557 "@end");
1558
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001559 verifyFormat("@implementation Foo {\n"
1560 " int _i;\n"
1561 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001562 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001563 "@end");
1564
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001565 verifyFormat("@implementation Foo : Bar {\n"
1566 " int _i;\n"
1567 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001568 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001569 "@end");
1570
Nico Webered91bba2013-01-10 19:19:14 +00001571 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001572 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001573 "@end");
1574}
1575
Nico Weber1abe6ea2013-01-09 21:15:03 +00001576TEST_F(FormatTest, FormatObjCProtocol) {
1577 verifyFormat("@protocol Foo\n"
1578 "@property(weak) id delegate;\n"
1579 "- (NSUInteger)numberOfThings;\n"
1580 "@end");
1581
Nico Weber5f500df2013-01-10 20:12:55 +00001582 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001583 "- (NSUInteger)numberOfThings;\n"
1584 "@end");
1585
Nico Weber5f500df2013-01-10 20:12:55 +00001586 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001587 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001588 "@end");
1589
Nico Weber1abe6ea2013-01-09 21:15:03 +00001590 verifyFormat("@protocol Foo;\n"
1591 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001592
1593 verifyFormat("@protocol Foo\n"
1594 "@end\n"
1595 "@protocol Bar\n"
1596 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001597
1598 verifyFormat("@protocol myProtocol\n"
1599 "- (void)mandatoryWithInt:(int)i;\n"
1600 "@optional\n"
1601 "- (void)optional;\n"
1602 "@required\n"
1603 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001604 "@optional\n"
1605 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001606 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001607}
1608
Nico Weberbcfdd262013-01-12 06:18:40 +00001609TEST_F(FormatTest, FormatObjCMethodExpr) {
1610 verifyFormat("[foo bar:baz];");
1611 verifyFormat("return [foo bar:baz];");
1612 verifyFormat("f([foo bar:baz]);");
1613 verifyFormat("f(2, [foo bar:baz]);");
1614 verifyFormat("f(2, a ? b : c);");
1615 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1616
1617 verifyFormat("[foo bar:baz], [foo bar:baz];");
1618 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1619 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1620 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1621 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1622 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1623 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1624 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1625 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1626 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1627 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1628 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1629 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1630 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1631 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1632 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1633 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1634 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1635 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1636 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1637 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1638 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1639 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1640 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1641 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1642 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1643 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1644 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1645 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1646 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1647 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1648 // Whew!
1649
1650 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1651 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1652 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1653 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1654 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001655 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001656 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001657
Nico Weberbcfdd262013-01-12 06:18:40 +00001658 verifyFormat("arr[[self indexForFoo:a]];");
1659 verifyFormat("throw [self errorFor:a];");
1660 verifyFormat("@throw [self errorFor:a];");
1661
Nico Webere8ccc812013-01-12 22:48:47 +00001662 // This tests that the formatter doesn't break after "backing" but before ":",
1663 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001664 verifyFormat(
1665 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001666 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1667 " backing:NSBackingStoreBuffered defer:YES]))");
1668
Nico Webere8ccc812013-01-12 22:48:47 +00001669 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1670 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001671
1672}
1673
Nico Weber581f5572013-01-07 15:56:25 +00001674TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001675 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001676 verifyFormat("@catch");
1677 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001678 verifyFormat("@compatibility_alias");
1679 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001680 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001681 verifyFormat("@encode");
1682 verifyFormat("@end");
1683 verifyFormat("@finally");
1684 verifyFormat("@implementation");
1685 verifyFormat("@import");
1686 verifyFormat("@interface");
1687 verifyFormat("@optional");
1688 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001689 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001690 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001691 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001692 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001693 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001694 verifyFormat("@required");
1695 verifyFormat("@selector");
1696 verifyFormat("@synchronized");
1697 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001698 verifyFormat("@throw");
1699 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001700
Nico Webercb4d6902013-01-08 19:40:21 +00001701 verifyFormat("@\"String\"");
1702 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001703 verifyFormat("@+4.8");
1704 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001705 verifyFormat("@1LL");
1706 verifyFormat("@.5");
1707 verifyFormat("@'c'");
1708 verifyFormat("@true");
1709 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001710 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001711 verifyFormat("@[");
1712 verifyFormat("@{");
1713
Nico Weber581f5572013-01-07 15:56:25 +00001714 EXPECT_EQ("@interface", format("@ interface"));
1715
1716 // The precise formatting of this doesn't matter, nobody writes code like
1717 // this.
1718 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001719}
1720
Nico Weberc31689a2013-01-08 19:15:23 +00001721TEST_F(FormatTest, ObjCSnippets) {
1722 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001723 verifyFormat("@autoreleasepool {\n"
1724 " foo();\n"
1725 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001726 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001727 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001728 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001729 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001730 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001731 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001732 verifyFormat("@synchronized(self) {\n"
1733 " f();\n"
1734 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001735
Nico Weber70848232013-01-10 21:30:42 +00001736 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1737 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1738
Nico Webercf4a79c2013-01-08 17:56:31 +00001739 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001740 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1741 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001742}
1743
Daniel Jaspercd162382013-01-07 13:26:07 +00001744} // end namespace tooling
1745} // end namespace clang