blob: eb00d017ff302d9c9fbd5f4aa3556f5da4df9b8a [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"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001187 "#include <a-a>\n"
1188 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001189
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001190 verifyFormat("#import <string>");
1191 verifyFormat("#import <a/b/c.h>");
1192 verifyFormat("#import \"a/b/string\"");
1193 verifyFormat("#import \"string.h\"");
1194 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001195}
1196
Alexander Kornienko15757312012-12-06 18:03:27 +00001197//===----------------------------------------------------------------------===//
1198// Error recovery tests.
1199//===----------------------------------------------------------------------===//
1200
Daniel Jasper700e7102013-01-10 09:26:47 +00001201TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1202 verifyFormat("void f() { return } 42");
1203}
1204
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001205TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1206 verifyFormat("int aaaaaaaa =\n"
1207 " // Overly long comment\n"
1208 " b;", getLLVMStyleWithColumns(20));
1209 verifyFormat("function(\n"
1210 " ShortArgument,\n"
1211 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1212}
1213
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001214TEST_F(FormatTest, IncorrectAccessSpecifier) {
1215 verifyFormat("public:");
1216 verifyFormat("class A {\n"
1217 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001218 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001219 "};");
1220 verifyFormat("public\n"
1221 "int qwerty;");
1222 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001223 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001224 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001225 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001226 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001227 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001228}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001229
Alexander Kornienko393b0082012-12-04 15:40:36 +00001230TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1231 verifyFormat("{");
1232}
1233
1234TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001235 verifyFormat("do {}");
1236 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001237 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001238 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001239 "wheeee(fun);");
1240 verifyFormat("do {\n"
1241 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001242 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001243}
1244
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001245TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001246 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001247 verifyFormat("switch {\n foo;\n foo();\n}");
1248 verifyFormat("for {\n foo;\n foo();\n}");
1249 verifyFormat("while {\n foo;\n foo();\n}");
1250 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001251}
1252
Daniel Jasper1f42f112013-01-04 18:52:56 +00001253TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1254 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001255 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001256}
1257
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001258TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001259 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1260 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1261 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1262 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001263
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001264 EXPECT_EQ("{\n"
1265 " {\n"
1266 " breakme(\n"
1267 " qwe);\n"
1268 "}\n", format("{\n"
1269 " {\n"
1270 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001271 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001272}
1273
Manuel Klimek2851c162013-01-10 14:36:46 +00001274TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1275 verifyFormat(
1276 "int x = {\n"
1277 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001278 " b(alongervariable)\n"
1279 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001280}
1281
1282TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1283 verifyFormat(
1284 "Aaa({\n"
1285 " int i;\n"
1286 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1287 " ccccccccccccccccc));");
1288}
1289
Manuel Klimek517e8942013-01-11 17:54:10 +00001290TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1291 verifyFormat("void f() { return 42; }");
1292 verifyFormat("void f() {\n"
1293 " // Comment\n"
1294 "}");
1295 verifyFormat("{\n"
1296 "#error {\n"
1297 " int a;\n"
1298 "}");
1299 verifyFormat("{\n"
1300 " int a;\n"
1301 "#error {\n"
1302 "}");
1303}
1304
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001305TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1306 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001307 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001308 verifyFormat("class foo a = { bar };\nint n;");
1309 verifyFormat("union foo a = { bar };\nint n;");
1310
1311 // Elaborate types inside function definitions.
1312 verifyFormat("struct foo f() {}\nint n;");
1313 verifyFormat("class foo f() {}\nint n;");
1314 verifyFormat("union foo f() {}\nint n;");
1315
1316 // Templates.
1317 verifyFormat("template <class X> void f() {}\nint n;");
1318 verifyFormat("template <struct X> void f() {}\nint n;");
1319 verifyFormat("template <union X> void f() {}\nint n;");
1320
1321 // Actual definitions...
1322 verifyFormat("struct {} n;");
1323 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1324 verifyFormat("union Z {\n int n;\n} x;");
1325 verifyFormat("class MACRO Z {} n;");
1326 verifyFormat("class MACRO(X) Z {} n;");
1327 verifyFormat("class __attribute__(X) Z {} n;");
1328 verifyFormat("class __declspec(X) Z {} n;");
1329
1330 // Elaborate types where incorrectly parsing the structural element would
1331 // break the indent.
1332 verifyFormat("if (true)\n"
1333 " class X x;\n"
1334 "else\n"
1335 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001336}
1337
Manuel Klimek407a31a2013-01-15 15:50:27 +00001338TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1339 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1340 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1341 EXPECT_EQ("#error 1", format(" # error 1"));
1342 EXPECT_EQ("#warning 1", format(" # warning 1"));
1343}
1344
Manuel Klimek517e8942013-01-11 17:54:10 +00001345// FIXME: This breaks the order of the unwrapped lines:
1346// TEST_F(FormatTest, OrderUnwrappedLines) {
1347// verifyFormat("{\n"
1348// " bool a; //\n"
1349// "#error {\n"
1350// " int a;\n"
1351// "}");
1352// }
1353
Nico Webercf4a79c2013-01-08 17:56:31 +00001354//===----------------------------------------------------------------------===//
1355// Objective-C tests.
1356//===----------------------------------------------------------------------===//
1357
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001358TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1359 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1360 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1361 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001362 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001363 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1364 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1365 format("-(NSInteger)Method3:(id)anObject;"));
1366 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1367 format("-(NSInteger)Method4:(id)anObject;"));
1368 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1369 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1370 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1371 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001372 EXPECT_EQ(
1373 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1374 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001375
1376 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001377 EXPECT_EQ(
1378 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1379 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1380 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1381 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1382 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1383 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1384 format(
1385 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1386 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1387 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1388 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1389 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1390 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001391
1392 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001393 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001394 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1395 // protocol lists (but not for template classes):
1396 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001397
1398 verifyFormat("- (int(*)())foo:(int(*)())f;");
1399 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1400
1401 // If there's no return type (very rare in practice!), LLVM and Google style
1402 // agree.
1403 verifyFormat("- foo:(int)f;");
1404 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001405}
1406
Daniel Jasper886568d2013-01-09 08:36:49 +00001407TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001408 verifyFormat("int (^Block)(int, int);");
1409 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001410}
1411
Nico Weber27d13672013-01-09 20:25:35 +00001412TEST_F(FormatTest, FormatObjCInterface) {
1413 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001414 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001415 "@public\n"
1416 " int field1;\n"
1417 "@protected\n"
1418 " int field2;\n"
1419 "@private\n"
1420 " int field3;\n"
1421 "@package\n"
1422 " int field4;\n"
1423 "}\n"
1424 "+ (id)init;\n"
1425 "@end");
1426
Nico Weber27d13672013-01-09 20:25:35 +00001427 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1428 " @public\n"
1429 " int field1;\n"
1430 " @protected\n"
1431 " int field2;\n"
1432 " @private\n"
1433 " int field3;\n"
1434 " @package\n"
1435 " int field4;\n"
1436 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001437 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001438 "@end");
1439
1440 verifyFormat("@interface Foo\n"
1441 "+ (id)init;\n"
1442 "// Look, a comment!\n"
1443 "- (int)answerWith:(int)i;\n"
1444 "@end");
1445
1446 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001447 "@end\n"
1448 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001449 "@end");
1450
1451 verifyFormat("@interface Foo : Bar\n"
1452 "+ (id)init;\n"
1453 "@end");
1454
Nico Weber5f500df2013-01-10 20:12:55 +00001455 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001456 "+ (id)init;\n"
1457 "@end");
1458
Nico Weber5f500df2013-01-10 20:12:55 +00001459 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001460 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001461 "@end");
1462
Nico Webered91bba2013-01-10 19:19:14 +00001463 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001464 "+ (id)init;\n"
1465 "@end");
1466
Nico Webered91bba2013-01-10 19:19:14 +00001467 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001468 "+ (id)init;\n"
1469 "@end");
1470
Nico Weber5f500df2013-01-10 20:12:55 +00001471 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001472 "+ (id)init;\n"
1473 "@end");
1474
Nico Weber5f500df2013-01-10 20:12:55 +00001475 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001476 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001477 "@end");
1478
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001479 verifyFormat("@interface Foo {\n"
1480 " int _i;\n"
1481 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001482 "+ (id)init;\n"
1483 "@end");
1484
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001485 verifyFormat("@interface Foo : Bar {\n"
1486 " int _i;\n"
1487 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001488 "+ (id)init;\n"
1489 "@end");
1490
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001491 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1492 " int _i;\n"
1493 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001494 "+ (id)init;\n"
1495 "@end");
1496
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001497 verifyFormat("@interface Foo (HackStuff) {\n"
1498 " int _i;\n"
1499 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001500 "+ (id)init;\n"
1501 "@end");
1502
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001503 verifyFormat("@interface Foo () {\n"
1504 " int _i;\n"
1505 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001506 "+ (id)init;\n"
1507 "@end");
1508
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001509 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1510 " int _i;\n"
1511 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001512 "+ (id)init;\n"
1513 "@end");
1514}
1515
Nico Weber50767d82013-01-09 23:25:37 +00001516TEST_F(FormatTest, FormatObjCImplementation) {
1517 verifyFormat("@implementation Foo : NSObject {\n"
1518 "@public\n"
1519 " int field1;\n"
1520 "@protected\n"
1521 " int field2;\n"
1522 "@private\n"
1523 " int field3;\n"
1524 "@package\n"
1525 " int field4;\n"
1526 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001527 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001528 "@end");
1529
1530 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1531 " @public\n"
1532 " int field1;\n"
1533 " @protected\n"
1534 " int field2;\n"
1535 " @private\n"
1536 " int field3;\n"
1537 " @package\n"
1538 " int field4;\n"
1539 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001540 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001541 "@end");
1542
1543 verifyFormat("@implementation Foo\n"
1544 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001545 " if (true)\n"
1546 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001547 "}\n"
1548 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001549 "- (int)answerWith:(int)i {\n"
1550 " return i;\n"
1551 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001552 "+ (int)answerWith:(int)i {\n"
1553 " return i;\n"
1554 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001555 "@end");
1556
1557 verifyFormat("@implementation Foo\n"
1558 "@end\n"
1559 "@implementation Bar\n"
1560 "@end");
1561
1562 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001563 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001564 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001565 "@end");
1566
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001567 verifyFormat("@implementation Foo {\n"
1568 " int _i;\n"
1569 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001570 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001571 "@end");
1572
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001573 verifyFormat("@implementation Foo : Bar {\n"
1574 " int _i;\n"
1575 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001576 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001577 "@end");
1578
Nico Webered91bba2013-01-10 19:19:14 +00001579 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001580 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001581 "@end");
1582}
1583
Nico Weber1abe6ea2013-01-09 21:15:03 +00001584TEST_F(FormatTest, FormatObjCProtocol) {
1585 verifyFormat("@protocol Foo\n"
1586 "@property(weak) id delegate;\n"
1587 "- (NSUInteger)numberOfThings;\n"
1588 "@end");
1589
Nico Weber5f500df2013-01-10 20:12:55 +00001590 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001591 "- (NSUInteger)numberOfThings;\n"
1592 "@end");
1593
Nico Weber5f500df2013-01-10 20:12:55 +00001594 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001595 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001596 "@end");
1597
Nico Weber1abe6ea2013-01-09 21:15:03 +00001598 verifyFormat("@protocol Foo;\n"
1599 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001600
1601 verifyFormat("@protocol Foo\n"
1602 "@end\n"
1603 "@protocol Bar\n"
1604 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001605
1606 verifyFormat("@protocol myProtocol\n"
1607 "- (void)mandatoryWithInt:(int)i;\n"
1608 "@optional\n"
1609 "- (void)optional;\n"
1610 "@required\n"
1611 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001612 "@optional\n"
1613 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001614 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001615}
1616
Nico Weberbcfdd262013-01-12 06:18:40 +00001617TEST_F(FormatTest, FormatObjCMethodExpr) {
1618 verifyFormat("[foo bar:baz];");
1619 verifyFormat("return [foo bar:baz];");
1620 verifyFormat("f([foo bar:baz]);");
1621 verifyFormat("f(2, [foo bar:baz]);");
1622 verifyFormat("f(2, a ? b : c);");
1623 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1624
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];");
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] : [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 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1649 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1650 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1651 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1652 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1653 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1654 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1655 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1656 // Whew!
1657
1658 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1659 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1660 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1661 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1662 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001663 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001664 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001665
Nico Weberbcfdd262013-01-12 06:18:40 +00001666 verifyFormat("arr[[self indexForFoo:a]];");
1667 verifyFormat("throw [self errorFor:a];");
1668 verifyFormat("@throw [self errorFor:a];");
1669
Nico Webere8ccc812013-01-12 22:48:47 +00001670 // This tests that the formatter doesn't break after "backing" but before ":",
1671 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001672 verifyFormat(
1673 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001674 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1675 " backing:NSBackingStoreBuffered defer:YES]))");
1676
Nico Webere8ccc812013-01-12 22:48:47 +00001677 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1678 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001679
1680}
1681
Nico Weber581f5572013-01-07 15:56:25 +00001682TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001683 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001684 verifyFormat("@catch");
1685 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001686 verifyFormat("@compatibility_alias");
1687 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001688 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001689 verifyFormat("@encode");
1690 verifyFormat("@end");
1691 verifyFormat("@finally");
1692 verifyFormat("@implementation");
1693 verifyFormat("@import");
1694 verifyFormat("@interface");
1695 verifyFormat("@optional");
1696 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001697 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001698 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001699 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001700 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001701 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001702 verifyFormat("@required");
1703 verifyFormat("@selector");
1704 verifyFormat("@synchronized");
1705 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001706 verifyFormat("@throw");
1707 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001708
Nico Webercb4d6902013-01-08 19:40:21 +00001709 verifyFormat("@\"String\"");
1710 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001711 verifyFormat("@+4.8");
1712 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001713 verifyFormat("@1LL");
1714 verifyFormat("@.5");
1715 verifyFormat("@'c'");
1716 verifyFormat("@true");
1717 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001718 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001719 verifyFormat("@[");
1720 verifyFormat("@{");
1721
Nico Weber581f5572013-01-07 15:56:25 +00001722 EXPECT_EQ("@interface", format("@ interface"));
1723
1724 // The precise formatting of this doesn't matter, nobody writes code like
1725 // this.
1726 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001727}
1728
Nico Weberc31689a2013-01-08 19:15:23 +00001729TEST_F(FormatTest, ObjCSnippets) {
1730 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001731 verifyFormat("@autoreleasepool {\n"
1732 " foo();\n"
1733 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001734 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001735 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001736 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001737 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001738 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001739 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001740 verifyFormat("@synchronized(self) {\n"
1741 " f();\n"
1742 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001743
Nico Weber70848232013-01-10 21:30:42 +00001744 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1745 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1746
Nico Webercf4a79c2013-01-08 17:56:31 +00001747 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001748 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1749 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001750}
1751
Daniel Jaspercd162382013-01-07 13:26:07 +00001752} // end namespace tooling
1753} // end namespace clang