blob: 1ab4a53c404df2ac99f4d72588a6c67e5252437f [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 Klimeka080a182013-01-02 16:30:12 +0000462TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
463 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
464 " \\\n"
465 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
466}
467
Daniel Jasper71607512013-01-07 10:48:50 +0000468TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000469 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
470 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000471}
472
Manuel Klimeka080a182013-01-02 16:30:12 +0000473TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
474 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000475 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000476}
477
478TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
479 EXPECT_EQ("#line 42 \"test\"\n",
480 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000481 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000482 format("# \\\n define \\\n A \\\n B\n",
483 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000484}
485
486TEST_F(FormatTest, EndOfFileEndsPPDirective) {
487 EXPECT_EQ("#line 42 \"test\"",
488 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000489 EXPECT_EQ("#define A B",
490 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000491}
492
Manuel Klimek060143e2013-01-02 18:33:23 +0000493TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000494 // If the macro fits in one line, we still do not get the full
495 // line, as only the next line decides whether we need an escaped newline and
496 // thus use the last column.
497 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000498
Manuel Klimekd544c572013-01-07 09:24:17 +0000499 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
500 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000501 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000502
503 verifyFormat("#define A A\n#define A A");
504 verifyFormat("#define A(X) A\n#define A A");
505
506 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
507 verifyFormat("#define Something \\\n"
508 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000509}
510
511TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000512 EXPECT_EQ("// some comment\n"
513 "#include \"a.h\"\n"
514 "#define A(A,\\\n"
515 " B)\n"
516 "#include \"b.h\"\n"
517 "// some comment\n",
518 format(" // some comment\n"
519 " #include \"a.h\"\n"
520 "#define A(A,\\\n"
521 " B)\n"
522 " #include \"b.h\"\n"
523 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000524}
525
Manuel Klimekd4397b92013-01-04 23:34:14 +0000526TEST_F(FormatTest, LayoutSingleHash) {
527 EXPECT_EQ("#\na;", format("#\na;"));
528}
529
530TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
531 EXPECT_EQ("#define A \\\n"
532 " c; \\\n"
533 " e;\n"
534 "f;", format("#define A c; e;\n"
535 "f;", getLLVMStyleWithColumns(14)));
536}
537
538TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000539 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000540}
541
542TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000543 EXPECT_EQ("# define A\\\n b;",
544 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000545}
546
547TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000548 EXPECT_EQ("int x,\n"
549 "#define A\n"
550 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000551}
552
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000553TEST_F(FormatTest, HashInMacroDefinition) {
554 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
555 verifyFormat("#define A \\\n"
556 " { \\\n"
557 " f(#c);\\\n"
558 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000559
560 verifyFormat("#define A(X) \\\n"
561 " void function##X()", getLLVMStyleWithColumns(22));
562
563 verifyFormat("#define A(a, b, c) \\\n"
564 " void a##b##c()", getLLVMStyleWithColumns(22));
565
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000566 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000567}
568
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000569TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
570 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
571}
572
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000573TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000574 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000575}
576
Manuel Klimeka5342db2013-01-06 20:07:31 +0000577TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
578 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
579 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
580 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
581 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
582}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000583
Manuel Klimek95419382013-01-07 07:56:50 +0000584TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000585 EXPECT_EQ(
586 "#define A \\\n int i; \\\n int j;",
587 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000588}
589
Manuel Klimekd544c572013-01-07 09:24:17 +0000590TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
591 verifyFormat("#define A \\\n"
592 " int v( \\\n"
593 " a); \\\n"
594 " int i;", getLLVMStyleWithColumns(11));
595}
596
Manuel Klimeka080a182013-01-02 16:30:12 +0000597TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000598 EXPECT_EQ(
599 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
600 " \\\n"
601 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
602 "\n"
603 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
604 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
605 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
606 "\\\n"
607 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
608 " \n"
609 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
610 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000611}
612
Manuel Klimek526ed112013-01-09 15:25:02 +0000613TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
614 EXPECT_EQ("int\n"
615 "#define A\n"
616 " a;",
617 format("int\n#define A\na;"));
618 verifyFormat(
619 "functionCallTo(someOtherFunction(\n"
620 " withSomeParameters, whichInSequence,\n"
621 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000622 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000623 " withMoreParamters,\n"
624 " whichStronglyInfluenceTheLayout),\n"
625 " andMoreParameters),\n"
626 " trailing);", getLLVMStyleWithColumns(69));
627}
628
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000629TEST_F(FormatTest, LayoutBlockInsideParens) {
630 EXPECT_EQ("functionCall({\n"
631 " int i;\n"
632 "});", format(" functionCall ( {int i;} );"));
633}
634
635TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000636 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000637 "int i;", format(" SOME_MACRO {int i;} int i;"));
638}
639
640TEST_F(FormatTest, LayoutNestedBlocks) {
641 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
642 " struct s {\n"
643 " int i;\n"
644 " };\n"
645 " s kBitsToOs[] = { { 10 } };\n"
646 " for (int i = 0; i < 10; ++i)\n"
647 " return;\n"
648 "}");
649}
650
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000651TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
652 EXPECT_EQ("{}", format("{}"));
653}
654
Alexander Kornienko15757312012-12-06 18:03:27 +0000655//===----------------------------------------------------------------------===//
656// Line break tests.
657//===----------------------------------------------------------------------===//
658
659TEST_F(FormatTest, FormatsFunctionDefinition) {
660 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
661 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000662 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000663}
664
665TEST_F(FormatTest, FormatsAwesomeMethodCall) {
666 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000667 "SomeLongMethodName(SomeReallyLongMethod(\n"
668 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
669 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000670}
671
Daniel Jasper1321eb52012-12-18 21:05:13 +0000672TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000673 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000674 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
675 getLLVMStyleWithColumns(45));
676 verifyFormat("Constructor()\n"
677 " : Inttializer(FitsOnTheLine) {}",
678 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000679
680 verifyFormat(
681 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000682 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000683
684 verifyFormat(
685 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000686 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
687 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
688 verifyGoogleFormat(
689 "SomeClass::Constructor()\n"
690 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
691 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
692 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
693
694 verifyFormat(
695 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000696 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000697 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000698
699 verifyFormat("Constructor()\n"
700 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
701 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
702 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000703 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000704
705 // Here a line could be saved by splitting the second initializer onto two
706 // lines, but that is not desireable.
707 verifyFormat("Constructor()\n"
708 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
709 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000710 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000711
712 verifyGoogleFormat("MyClass::MyClass(int var)\n"
713 " : some_var_(var), // 4 space indent\n"
714 " some_other_var_(var + 1) { // lined up\n"
715 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000716
717 // This test takes VERY long when memoization is broken.
718 verifyGoogleFormat(
719 "Constructor()\n"
720 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
721 " a, a, a,\n"
722 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
723 " a, a, a,\n"
724 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
725 " a, a, a,\n"
726 " a, a, a, a, a, a, a, a, a, a, a)\n"
727 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
728 " a, a, a,\n"
729 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
730 " a, a, a,\n"
731 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
732 " a, a, a,\n"
733 " a, a, a, a, a, a, a, a, a, a, a) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000734}
735
Alexander Kornienko15757312012-12-06 18:03:27 +0000736TEST_F(FormatTest, BreaksAsHighAsPossible) {
737 verifyFormat(
738 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
739 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
740 " f();");
741}
742
Daniel Jasperbac016b2012-12-03 18:12:45 +0000743TEST_F(FormatTest, BreaksDesireably) {
744 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
745 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000746 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000747
748 verifyFormat(
749 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000751
752 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000755
756 verifyFormat(
757 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
759 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000761
762 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
763 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
764
Daniel Jasper723f0302013-01-02 14:40:02 +0000765 verifyFormat(
766 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
768
Daniel Jasper33182dd2012-12-05 14:57:28 +0000769 // This test case breaks on an incorrect memoization, i.e. an optimization not
770 // taking into account the StopAt value.
771 verifyFormat(
772 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000773 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
774 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
775 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000776
Daniel Jaspercd162382013-01-07 13:26:07 +0000777 verifyFormat("{\n {\n {\n"
778 " Annotation.SpaceRequiredBefore =\n"
779 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
780 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
781 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000782}
783
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000784TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
785 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
786 " GUARDED_BY(aaaaaaaaaaaaa);");
787}
788
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000789TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
790 verifyFormat(
791 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000792 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000793 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000794 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000795 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000796 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000797 verifyFormat(
798 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000799 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000800}
801
Daniel Jasper9cda8002013-01-07 13:08:40 +0000802TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
803 verifyFormat(
804 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
805 " SI->getAlignment(),\n"
806 " SI->getPointerAddressSpaceee());\n");
807 verifyFormat(
808 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
809 " Line.Tokens.front().Tok.getLocation(),\n"
810 " Line.Tokens.back().Tok.getLocation());");
811}
812
Daniel Jaspercf225b62012-12-24 13:43:52 +0000813TEST_F(FormatTest, AlignsAfterAssignments) {
814 verifyFormat(
815 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000816 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000817 verifyFormat(
818 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000819 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000820 verifyFormat(
821 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000822 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000823 verifyFormat(
824 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000825 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000826 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000827 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
828 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
829 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000830}
831
832TEST_F(FormatTest, AlignsAfterReturn) {
833 verifyFormat(
834 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
835 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
836 verifyFormat(
837 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
838 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
839}
840
Daniel Jasper9c837d02013-01-09 07:06:56 +0000841TEST_F(FormatTest, BreaksConditionalExpressions) {
842 verifyFormat(
843 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
844 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
845 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
846 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
847 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
848}
849
Nico Weber7d37b8b2013-01-12 01:28:06 +0000850TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
851 verifyFormat("arr[foo ? bar : baz];");
852 verifyFormat("f()[foo ? bar : baz];");
853 verifyFormat("(a + b)[foo ? bar : baz];");
854 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
855}
856
Daniel Jasperbac016b2012-12-03 18:12:45 +0000857TEST_F(FormatTest, AlignsStringLiterals) {
858 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
859 " \"short literal\");");
860 verifyFormat(
861 "looooooooooooooooooooooooongFunction(\n"
862 " \"short literal\"\n"
863 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
864}
865
Alexander Kornienko15757312012-12-06 18:03:27 +0000866TEST_F(FormatTest, AlignsPipes) {
867 verifyFormat(
868 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
869 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
870 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
871 verifyFormat(
872 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
873 " << aaaaaaaaaaaaaaaaaaaa;");
874 verifyFormat(
875 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
876 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
877 verifyFormat(
878 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
879 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
880 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
881 verifyFormat(
882 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
883 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
884 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
885}
886
Daniel Jasperbac016b2012-12-03 18:12:45 +0000887TEST_F(FormatTest, UnderstandsEquals) {
888 verifyFormat(
889 "aaaaaaaaaaaaaaaaa =\n"
890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
891 verifyFormat(
892 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000894 verifyFormat(
895 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000896 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000897 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000899
Daniel Jasper9cda8002013-01-07 13:08:40 +0000900 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000901 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000902 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000903 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000904}
905
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000906TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000907 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
908 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000909
Daniel Jasper1321eb52012-12-18 21:05:13 +0000910 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
911 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000912
913 verifyFormat(
914 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
915 " Parameter2);");
916
917 verifyFormat(
918 "ShortObject->shortFunction(\n"
919 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
920 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
921
922 verifyFormat("loooooooooooooongFunction(\n"
923 " LoooooooooooooongObject->looooooooooooooooongFunction());");
924
925 verifyFormat(
926 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
927 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
928
Daniel Jasper46a46a22013-01-07 07:13:20 +0000929 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000930 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000931 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000932 verifyFormat(
933 "aaaaaaaaaaa->aaaaaaaaa(\n"
934 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
935 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000936}
937
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000938TEST_F(FormatTest, WrapsTemplateDeclarations) {
939 verifyFormat("template <typename T>\n"
940 "virtual void loooooooooooongFunction(int Param1, int Param2);");
941 verifyFormat(
942 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
943 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
944 verifyFormat(
945 "template <typename T>\n"
946 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
947 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000948 verifyFormat(
949 "template <typename T>\n"
950 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
951 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000953 verifyFormat("template <typename T>\n"
954 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
955 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000956 verifyFormat(
957 "template <typename T1, typename T2 = char, typename T3 = char,\n"
958 " typename T4 = char>\n"
959 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000960}
961
Daniel Jasperbac016b2012-12-03 18:12:45 +0000962TEST_F(FormatTest, UnderstandsTemplateParameters) {
963 verifyFormat("A<int> a;");
964 verifyFormat("A<A<A<int> > > a;");
965 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
966 verifyFormat("bool x = a < 1 || 2 > a;");
967 verifyFormat("bool x = 5 < f<int>();");
968 verifyFormat("bool x = f<int>() > 5;");
969 verifyFormat("bool x = 5 < a<int>::x;");
970 verifyFormat("bool x = a < 4 ? a > 2 : false;");
971 verifyFormat("bool x = f() ? a < 2 : a > 2;");
972
973 verifyGoogleFormat("A<A<int>> a;");
974 verifyGoogleFormat("A<A<A<int>>> a;");
975 verifyGoogleFormat("A<A<A<A<int>>>> a;");
976
977 verifyFormat("test >> a >> b;");
978 verifyFormat("test << a >> b;");
979
980 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000981 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000982}
983
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000984TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000985 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000986 verifyFormat("f(-1, -2, -3);");
987 verifyFormat("a[-1] = 5;");
988 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000989 verifyFormat("if (i == -1) {}");
990 verifyFormat("if (i != -1) {}");
991 verifyFormat("if (i > -1) {}");
992 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +0000993 verifyFormat("++(a->f());");
994 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +0000995 verifyFormat("(a->f())++;");
996 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000997 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000998
999 verifyFormat("a-- > b;");
1000 verifyFormat("b ? -a : c;");
1001 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001002 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001003 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001004 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001005
1006 verifyFormat("return -1;");
1007 verifyFormat("switch (a) {\n"
1008 "case -1:\n"
1009 " break;\n"
1010 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001011
1012 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1013 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001014}
1015
1016TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001017 verifyFormat("bool operator<();");
1018 verifyFormat("bool operator>();");
1019 verifyFormat("bool operator=();");
1020 verifyFormat("bool operator==();");
1021 verifyFormat("bool operator!=();");
1022 verifyFormat("int operator+();");
1023 verifyFormat("int operator++();");
1024 verifyFormat("bool operator();");
1025 verifyFormat("bool operator()();");
1026 verifyFormat("bool operator[]();");
1027 verifyFormat("operator bool();");
1028 verifyFormat("operator SomeType<int>();");
1029 verifyFormat("void *operator new(std::size_t size);");
1030 verifyFormat("void *operator new[](std::size_t size);");
1031 verifyFormat("void operator delete(void *ptr);");
1032 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001033}
1034
Daniel Jasper088dab52013-01-11 16:09:04 +00001035TEST_F(FormatTest, UnderstandsNewAndDelete) {
1036 verifyFormat("A *a = new A;");
1037 verifyFormat("A *a = new (placement) A;");
1038 verifyFormat("delete a;");
1039 verifyFormat("delete (A *)a;");
1040}
1041
Daniel Jasper5d334402013-01-02 08:57:10 +00001042TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001043 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001044 verifyFormat("f(a, *a);");
1045 verifyFormat("f(*a);");
1046 verifyFormat("int a = b * 10;");
1047 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001048 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001049 verifyFormat("int a += b * c;");
1050 verifyFormat("int a -= b * c;");
1051 verifyFormat("int a *= b * c;");
1052 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001053 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001054 verifyFormat("int a = *b * c;");
1055 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001056 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001057 verifyFormat("return 10 * b;");
1058 verifyFormat("return *b * *c;");
1059 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001060 verifyFormat("f(b ? *c : *d);");
1061 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001062 verifyFormat("*b = a;");
1063 verifyFormat("a * ~b;");
1064 verifyFormat("a * !b;");
1065 verifyFormat("a * +b;");
1066 verifyFormat("a * -b;");
1067 verifyFormat("a * ++b;");
1068 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001069 verifyFormat("a[4] * b;");
1070 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001071 verifyFormat("a * [self dostuff];");
1072 verifyFormat("a * (a + b);");
1073 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001074 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001075
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001076 verifyFormat("InvalidRegions[*R] = 0;");
1077
Daniel Jasper8b39c662012-12-10 18:59:13 +00001078 verifyFormat("A<int *> a;");
1079 verifyFormat("A<int **> a;");
1080 verifyFormat("A<int *, int *> a;");
1081 verifyFormat("A<int **, int **> a;");
1082
Daniel Jasper2db356d2013-01-08 20:03:18 +00001083 verifyFormat(
1084 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1086
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001087 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001088 verifyGoogleFormat("A<int*> a;");
1089 verifyGoogleFormat("A<int**> a;");
1090 verifyGoogleFormat("A<int*, int*> a;");
1091 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001092 verifyGoogleFormat("f(b ? *c : *d);");
1093 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001094
1095 verifyFormat("a = *(x + y);");
1096 verifyFormat("a = &(x + y);");
1097 verifyFormat("*(x + y).call();");
1098 verifyFormat("&(x + y)->call();");
1099 verifyFormat("&(*I).first");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001100}
1101
Daniel Jasper4981bd02013-01-13 08:01:36 +00001102TEST_F(FormatTest, FormatsCasts) {
1103 verifyFormat("Type *A = static_cast<Type *>(P);");
1104 verifyFormat("Type *A = (Type *)P;");
1105 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1106 verifyFormat("int a = (int)(2.0f);");
1107
1108 // FIXME: These also need to be identified.
1109 verifyFormat("int a = (int) 2.0f;");
1110 verifyFormat("int a = (int) * b;");
1111
1112 // These are not casts.
1113 verifyFormat("void f(int *) {}");
1114 verifyFormat("void f(int *);");
1115 verifyFormat("void f(int *) = 0;");
1116 verifyFormat("void f(SmallVector<int>) {}");
1117 verifyFormat("void f(SmallVector<int>);");
1118 verifyFormat("void f(SmallVector<int>) = 0;");
1119}
1120
Daniel Jasper46ef8522013-01-10 13:08:12 +00001121TEST_F(FormatTest, FormatsFunctionTypes) {
1122 // FIXME: Determine the cases that need a space after the return type and fix.
1123 verifyFormat("A<bool()> a;");
1124 verifyFormat("A<SomeType()> a;");
1125 verifyFormat("A<void(*)(int, std::string)> a;");
1126
1127 verifyFormat("int(*func)(void *);");
1128}
1129
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001130TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001131 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001132 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001133 verifyFormat(
1134 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1135 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001136 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001137}
1138
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001139TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1140 verifyFormat("(a)->b();");
1141 verifyFormat("--a;");
1142}
1143
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001144TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001145 verifyFormat("#include <string>\n"
1146 "#include <a/b/c.h>\n"
1147 "#include \"a/b/string\"\n"
1148 "#include \"string.h\"\n"
1149 "#include \"string.h\"\n"
1150 "#include <a-a>");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001151
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001152 verifyFormat("#import <string>");
1153 verifyFormat("#import <a/b/c.h>");
1154 verifyFormat("#import \"a/b/string\"");
1155 verifyFormat("#import \"string.h\"");
1156 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001157}
1158
Alexander Kornienko15757312012-12-06 18:03:27 +00001159//===----------------------------------------------------------------------===//
1160// Error recovery tests.
1161//===----------------------------------------------------------------------===//
1162
Daniel Jasper700e7102013-01-10 09:26:47 +00001163TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1164 verifyFormat("void f() { return } 42");
1165}
1166
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001167TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1168 verifyFormat("int aaaaaaaa =\n"
1169 " // Overly long comment\n"
1170 " b;", getLLVMStyleWithColumns(20));
1171 verifyFormat("function(\n"
1172 " ShortArgument,\n"
1173 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1174}
1175
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001176TEST_F(FormatTest, IncorrectAccessSpecifier) {
1177 verifyFormat("public:");
1178 verifyFormat("class A {\n"
1179 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001180 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001181 "};");
1182 verifyFormat("public\n"
1183 "int qwerty;");
1184 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001185 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001186 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001187 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001188 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001189 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001190}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001191
Alexander Kornienko393b0082012-12-04 15:40:36 +00001192TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1193 verifyFormat("{");
1194}
1195
1196TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001197 verifyFormat("do {}");
1198 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001199 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001200 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001201 "wheeee(fun);");
1202 verifyFormat("do {\n"
1203 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001204 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001205}
1206
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001207TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001208 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001209 verifyFormat("switch {\n foo;\n foo();\n}");
1210 verifyFormat("for {\n foo;\n foo();\n}");
1211 verifyFormat("while {\n foo;\n foo();\n}");
1212 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001213}
1214
Daniel Jasper1f42f112013-01-04 18:52:56 +00001215TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1216 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001217 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001218}
1219
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001220TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001221 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1222 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1223 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1224 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001225
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001226 EXPECT_EQ("{\n"
1227 " {\n"
1228 " breakme(\n"
1229 " qwe);\n"
1230 "}\n", format("{\n"
1231 " {\n"
1232 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001233 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001234}
1235
Manuel Klimek2851c162013-01-10 14:36:46 +00001236TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1237 verifyFormat(
1238 "int x = {\n"
1239 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001240 " b(alongervariable)\n"
1241 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001242}
1243
1244TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1245 verifyFormat(
1246 "Aaa({\n"
1247 " int i;\n"
1248 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1249 " ccccccccccccccccc));");
1250}
1251
Manuel Klimek517e8942013-01-11 17:54:10 +00001252TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1253 verifyFormat("void f() { return 42; }");
1254 verifyFormat("void f() {\n"
1255 " // Comment\n"
1256 "}");
1257 verifyFormat("{\n"
1258 "#error {\n"
1259 " int a;\n"
1260 "}");
1261 verifyFormat("{\n"
1262 " int a;\n"
1263 "#error {\n"
1264 "}");
1265}
1266
Manuel Klimek606e07e2013-01-11 18:13:04 +00001267TEST_F(FormatTest, BracedInitListWithElaboratedTypeSpecifier) {
1268 verifyFormat("struct foo a = { bar };\nint n;");
1269}
1270
Manuel Klimek517e8942013-01-11 17:54:10 +00001271// FIXME: This breaks the order of the unwrapped lines:
1272// TEST_F(FormatTest, OrderUnwrappedLines) {
1273// verifyFormat("{\n"
1274// " bool a; //\n"
1275// "#error {\n"
1276// " int a;\n"
1277// "}");
1278// }
1279
Nico Webercf4a79c2013-01-08 17:56:31 +00001280//===----------------------------------------------------------------------===//
1281// Objective-C tests.
1282//===----------------------------------------------------------------------===//
1283
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001284TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1285 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1286 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1287 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001288 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001289 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1290 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1291 format("-(NSInteger)Method3:(id)anObject;"));
1292 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1293 format("-(NSInteger)Method4:(id)anObject;"));
1294 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1295 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1296 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1297 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001298 EXPECT_EQ(
1299 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1300 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001301
1302 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001303 EXPECT_EQ(
1304 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1305 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1306 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1307 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1308 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1309 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1310 format(
1311 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1312 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1313 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1314 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1315 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1316 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001317
1318 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001319 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001320 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1321 // protocol lists (but not for template classes):
1322 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001323
1324 verifyFormat("- (int(*)())foo:(int(*)())f;");
1325 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1326
1327 // If there's no return type (very rare in practice!), LLVM and Google style
1328 // agree.
1329 verifyFormat("- foo:(int)f;");
1330 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001331}
1332
Daniel Jasper886568d2013-01-09 08:36:49 +00001333TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001334 verifyFormat("int (^Block)(int, int);");
1335 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001336}
1337
Nico Weber27d13672013-01-09 20:25:35 +00001338TEST_F(FormatTest, FormatObjCInterface) {
1339 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001340 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001341 "@public\n"
1342 " int field1;\n"
1343 "@protected\n"
1344 " int field2;\n"
1345 "@private\n"
1346 " int field3;\n"
1347 "@package\n"
1348 " int field4;\n"
1349 "}\n"
1350 "+ (id)init;\n"
1351 "@end");
1352
Nico Weber27d13672013-01-09 20:25:35 +00001353 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1354 " @public\n"
1355 " int field1;\n"
1356 " @protected\n"
1357 " int field2;\n"
1358 " @private\n"
1359 " int field3;\n"
1360 " @package\n"
1361 " int field4;\n"
1362 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001363 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001364 "@end");
1365
1366 verifyFormat("@interface Foo\n"
1367 "+ (id)init;\n"
1368 "// Look, a comment!\n"
1369 "- (int)answerWith:(int)i;\n"
1370 "@end");
1371
1372 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001373 "@end\n"
1374 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001375 "@end");
1376
1377 verifyFormat("@interface Foo : Bar\n"
1378 "+ (id)init;\n"
1379 "@end");
1380
Nico Weber5f500df2013-01-10 20:12:55 +00001381 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001382 "+ (id)init;\n"
1383 "@end");
1384
Nico Weber5f500df2013-01-10 20:12:55 +00001385 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001386 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001387 "@end");
1388
Nico Webered91bba2013-01-10 19:19:14 +00001389 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001390 "+ (id)init;\n"
1391 "@end");
1392
Nico Webered91bba2013-01-10 19:19:14 +00001393 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001394 "+ (id)init;\n"
1395 "@end");
1396
Nico Weber5f500df2013-01-10 20:12:55 +00001397 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001398 "+ (id)init;\n"
1399 "@end");
1400
Nico Weber5f500df2013-01-10 20:12:55 +00001401 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001402 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001403 "@end");
1404
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001405 verifyFormat("@interface Foo {\n"
1406 " int _i;\n"
1407 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001408 "+ (id)init;\n"
1409 "@end");
1410
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001411 verifyFormat("@interface Foo : Bar {\n"
1412 " int _i;\n"
1413 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001414 "+ (id)init;\n"
1415 "@end");
1416
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001417 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1418 " int _i;\n"
1419 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001420 "+ (id)init;\n"
1421 "@end");
1422
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001423 verifyFormat("@interface Foo (HackStuff) {\n"
1424 " int _i;\n"
1425 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001426 "+ (id)init;\n"
1427 "@end");
1428
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001429 verifyFormat("@interface Foo () {\n"
1430 " int _i;\n"
1431 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001432 "+ (id)init;\n"
1433 "@end");
1434
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001435 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1436 " int _i;\n"
1437 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001438 "+ (id)init;\n"
1439 "@end");
1440}
1441
Nico Weber50767d82013-01-09 23:25:37 +00001442TEST_F(FormatTest, FormatObjCImplementation) {
1443 verifyFormat("@implementation Foo : NSObject {\n"
1444 "@public\n"
1445 " int field1;\n"
1446 "@protected\n"
1447 " int field2;\n"
1448 "@private\n"
1449 " int field3;\n"
1450 "@package\n"
1451 " int field4;\n"
1452 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001453 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001454 "@end");
1455
1456 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1457 " @public\n"
1458 " int field1;\n"
1459 " @protected\n"
1460 " int field2;\n"
1461 " @private\n"
1462 " int field3;\n"
1463 " @package\n"
1464 " int field4;\n"
1465 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001466 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001467 "@end");
1468
1469 verifyFormat("@implementation Foo\n"
1470 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001471 " if (true)\n"
1472 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001473 "}\n"
1474 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001475 "- (int)answerWith:(int)i {\n"
1476 " return i;\n"
1477 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001478 "+ (int)answerWith:(int)i {\n"
1479 " return i;\n"
1480 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001481 "@end");
1482
1483 verifyFormat("@implementation Foo\n"
1484 "@end\n"
1485 "@implementation Bar\n"
1486 "@end");
1487
1488 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001489 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001490 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001491 "@end");
1492
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001493 verifyFormat("@implementation Foo {\n"
1494 " int _i;\n"
1495 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001496 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001497 "@end");
1498
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001499 verifyFormat("@implementation Foo : Bar {\n"
1500 " int _i;\n"
1501 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001502 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001503 "@end");
1504
Nico Webered91bba2013-01-10 19:19:14 +00001505 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001506 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001507 "@end");
1508}
1509
Nico Weber1abe6ea2013-01-09 21:15:03 +00001510TEST_F(FormatTest, FormatObjCProtocol) {
1511 verifyFormat("@protocol Foo\n"
1512 "@property(weak) id delegate;\n"
1513 "- (NSUInteger)numberOfThings;\n"
1514 "@end");
1515
Nico Weber5f500df2013-01-10 20:12:55 +00001516 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001517 "- (NSUInteger)numberOfThings;\n"
1518 "@end");
1519
Nico Weber5f500df2013-01-10 20:12:55 +00001520 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001521 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001522 "@end");
1523
Nico Weber1abe6ea2013-01-09 21:15:03 +00001524 verifyFormat("@protocol Foo;\n"
1525 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001526
1527 verifyFormat("@protocol Foo\n"
1528 "@end\n"
1529 "@protocol Bar\n"
1530 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001531
1532 verifyFormat("@protocol myProtocol\n"
1533 "- (void)mandatoryWithInt:(int)i;\n"
1534 "@optional\n"
1535 "- (void)optional;\n"
1536 "@required\n"
1537 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001538 "@optional\n"
1539 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001540 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001541}
1542
Nico Weberbcfdd262013-01-12 06:18:40 +00001543TEST_F(FormatTest, FormatObjCMethodExpr) {
1544 verifyFormat("[foo bar:baz];");
1545 verifyFormat("return [foo bar:baz];");
1546 verifyFormat("f([foo bar:baz]);");
1547 verifyFormat("f(2, [foo bar:baz]);");
1548 verifyFormat("f(2, a ? b : c);");
1549 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1550
1551 verifyFormat("[foo bar:baz], [foo bar:baz];");
1552 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1553 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1554 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1555 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1556 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1557 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1558 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1559 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1560 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1561 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1562 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1563 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1564 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1565 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1566 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1567 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1568 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1569 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1570 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1571 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1572 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1573 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1574 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1575 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1576 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1577 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1578 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1579 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1580 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1581 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1582 // Whew!
1583
1584 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1585 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1586 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1587 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1588 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001589 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001590 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001591
Nico Weberbcfdd262013-01-12 06:18:40 +00001592 verifyFormat("arr[[self indexForFoo:a]];");
1593 verifyFormat("throw [self errorFor:a];");
1594 verifyFormat("@throw [self errorFor:a];");
1595
Nico Webere8ccc812013-01-12 22:48:47 +00001596 // This tests that the formatter doesn't break after "backing" but before ":",
1597 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001598 verifyFormat(
1599 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001600 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1601 " backing:NSBackingStoreBuffered defer:YES]))");
1602
Nico Webere8ccc812013-01-12 22:48:47 +00001603 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1604 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001605
1606}
1607
Nico Weber581f5572013-01-07 15:56:25 +00001608TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001609 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001610 verifyFormat("@catch");
1611 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001612 verifyFormat("@compatibility_alias");
1613 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001614 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001615 verifyFormat("@encode");
1616 verifyFormat("@end");
1617 verifyFormat("@finally");
1618 verifyFormat("@implementation");
1619 verifyFormat("@import");
1620 verifyFormat("@interface");
1621 verifyFormat("@optional");
1622 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001623 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001624 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001625 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001626 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001627 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001628 verifyFormat("@required");
1629 verifyFormat("@selector");
1630 verifyFormat("@synchronized");
1631 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001632 verifyFormat("@throw");
1633 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001634
Nico Webercb4d6902013-01-08 19:40:21 +00001635 verifyFormat("@\"String\"");
1636 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001637 verifyFormat("@+4.8");
1638 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001639 verifyFormat("@1LL");
1640 verifyFormat("@.5");
1641 verifyFormat("@'c'");
1642 verifyFormat("@true");
1643 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001644 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001645 verifyFormat("@[");
1646 verifyFormat("@{");
1647
Nico Weber581f5572013-01-07 15:56:25 +00001648 EXPECT_EQ("@interface", format("@ interface"));
1649
1650 // The precise formatting of this doesn't matter, nobody writes code like
1651 // this.
1652 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001653}
1654
Nico Weberc31689a2013-01-08 19:15:23 +00001655TEST_F(FormatTest, ObjCSnippets) {
1656 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001657 verifyFormat("@autoreleasepool {\n"
1658 " foo();\n"
1659 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001660 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001661 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001662 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001663 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001664 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001665 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001666 verifyFormat("@synchronized(self) {\n"
1667 " f();\n"
1668 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001669
Nico Weber70848232013-01-10 21:30:42 +00001670 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1671 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1672
Nico Webercf4a79c2013-01-08 17:56:31 +00001673 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001674 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1675 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001676}
1677
Daniel Jaspercd162382013-01-07 13:26:07 +00001678} // end namespace tooling
1679} // end namespace clang