blob: ac61af3bfaeddb61692dad046870c339965f9f4e [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 Jasper55b08e72013-01-16 07:02:34 +0000148 " return;", getGoogleStyleWithColumns(14));
149 verifyGoogleFormat("if (a) // Can't merge this\n"
150 " f();\n");
151 verifyGoogleFormat("if (a) /* still don't merge */\n"
152 " f();");
153 verifyGoogleFormat("if (a) { // Never merge this\n"
154 " f();\n"
155 "}");
156 verifyGoogleFormat("if (a) { /* Never merge this */\n"
157 " f();\n"
158 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000159}
160
161TEST_F(FormatTest, ParseIfElse) {
162 verifyFormat("if (true)\n"
163 " if (true)\n"
164 " if (true)\n"
165 " f();\n"
166 " else\n"
167 " g();\n"
168 " else\n"
169 " h();\n"
170 "else\n"
171 " i();");
172 verifyFormat("if (true)\n"
173 " if (true)\n"
174 " if (true) {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000175 " if (true)\n"
176 " f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000177 " } else {\n"
178 " g();\n"
179 " }\n"
180 " else\n"
181 " h();\n"
182 "else {\n"
183 " i();\n"
184 "}");
185}
186
187TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000188 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000189 verifyFormat("if (a)\n"
190 " f();\n"
191 "else if (b)\n"
192 " g();\n"
193 "else\n"
194 " h();");
195}
196
Daniel Jasperbac016b2012-12-03 18:12:45 +0000197TEST_F(FormatTest, FormatsForLoop) {
198 verifyFormat(
199 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000200 " ++VeryVeryLongLoopVariable)\n"
201 " ;");
202 verifyFormat("for (;;)\n"
203 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000204 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000205 verifyFormat("for (;;) {\n"
206 " f();\n"
207 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000208
209 verifyFormat(
210 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
211 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000212 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000213
214 verifyFormat(
215 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000216 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217}
218
219TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000220 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000221 verifyFormat("while (true)\n"
222 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000223 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000224 verifyFormat("while () {\n"
225 " f();\n"
226 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000227}
228
Alexander Kornienko15757312012-12-06 18:03:27 +0000229TEST_F(FormatTest, FormatsDoWhile) {
230 verifyFormat("do {\n"
231 " do_something();\n"
232 "} while (something());");
233 verifyFormat("do\n"
234 " do_something();\n"
235 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000236}
237
Alexander Kornienko15757312012-12-06 18:03:27 +0000238TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000239 verifyFormat("switch (x) {\n"
240 "case 1:\n"
241 " f();\n"
242 " break;\n"
243 "case kFoo:\n"
244 "case ns::kBar:\n"
245 "case kBaz:\n"
246 " break;\n"
247 "default:\n"
248 " g();\n"
249 " break;\n"
250 "}");
251 verifyFormat("switch (x) {\n"
252 "case 1: {\n"
253 " f();\n"
254 " break;\n"
255 "}\n"
256 "}");
257 verifyFormat("switch (test)\n"
258 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000259 verifyGoogleFormat("switch (x) {\n"
260 " case 1:\n"
261 " f();\n"
262 " break;\n"
263 " case kFoo:\n"
264 " case ns::kBar:\n"
265 " case kBaz:\n"
266 " break;\n"
267 " default:\n"
268 " g();\n"
269 " break;\n"
270 "}");
271 verifyGoogleFormat("switch (x) {\n"
272 " case 1: {\n"
273 " f();\n"
274 " break;\n"
275 " }\n"
276 "}");
277 verifyGoogleFormat("switch (test)\n"
278 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000279}
280
Alexander Kornienko15757312012-12-06 18:03:27 +0000281TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000282 verifyFormat("void f() {\n"
283 " some_code();\n"
284 "test_label:\n"
285 " some_other_code();\n"
286 " {\n"
287 " some_more_code();\n"
288 " another_label:\n"
289 " some_more_code();\n"
290 " }\n"
291 "}");
292 verifyFormat("some_code();\n"
293 "test_label:\n"
294 "some_other_code();");
295}
296
Alexander Kornienko15757312012-12-06 18:03:27 +0000297//===----------------------------------------------------------------------===//
298// Tests for comments.
299//===----------------------------------------------------------------------===//
300
301TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000302 verifyFormat("// line 1\n"
303 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000304 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000305
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000306 verifyFormat("void f() {\n"
307 " // Doesn't do anything\n"
308 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000309 verifyFormat("void f(int i, // some comment (probably for i)\n"
310 " int j, // some comment (probably for j)\n"
311 " int k); // some comment (probably for k)");
312 verifyFormat("void f(int i,\n"
313 " // some comment (probably for j)\n"
314 " int j,\n"
315 " // some comment (probably for k)\n"
316 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000317
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000318 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000319 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000320
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000321 verifyFormat("enum E {\n"
322 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000323 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000324 " VAL_B\n"
325 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000326
327 verifyFormat(
328 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000329 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000330 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
331 " // Comment inside a statement.\n"
332 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000333
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000334 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000335 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000336
337 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000338}
339
340TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000341 verifyFormat("f(/*test=*/ true);");
342}
343
Alexander Kornienko15757312012-12-06 18:03:27 +0000344//===----------------------------------------------------------------------===//
345// Tests for classes, namespaces, etc.
346//===----------------------------------------------------------------------===//
347
348TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000349 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000350}
351
352TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
353 verifyFormat("class A {\n"
354 "public:\n"
355 "protected:\n"
356 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000357 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000358 "};");
359 verifyGoogleFormat("class A {\n"
360 " public:\n"
361 " protected:\n"
362 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000363 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000364 "};");
365}
366
367TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000368 verifyFormat("class A : public B {};");
369 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000370}
371
Manuel Klimekde768542013-01-07 18:10:23 +0000372TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000373 verifyFormat("class A {} a, b;");
374 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000375 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000376}
377
Alexander Kornienko15757312012-12-06 18:03:27 +0000378TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000379 verifyFormat("enum {\n"
380 " Zero,\n"
381 " One = 1,\n"
382 " Two = One + 1,\n"
383 " Three = (One + Two),\n"
384 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
385 " Five = (One, Two, Three, Four, 5)\n"
386 "};");
387 verifyFormat("enum Enum {\n"
388 "};");
389 verifyFormat("enum {\n"
390 "};");
391}
392
Nico Weberefaddc02013-01-14 05:49:49 +0000393TEST_F(FormatTest, FormatsBitfields) {
394 verifyFormat("struct Bitfields {\n"
395 " unsigned sClass : 8;\n"
396 " unsigned ValueKind : 2;\n"
397 "};");
398}
399
Alexander Kornienko15757312012-12-06 18:03:27 +0000400TEST_F(FormatTest, FormatsNamespaces) {
401 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000402 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000403 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000404 "}");
405 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000406 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000407 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000408 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000409 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000410 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000411 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000412 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000413 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000414 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000415 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000416}
417
Nico Webera9ccdd12013-01-07 16:36:17 +0000418TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000419 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
420 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000421 verifyFormat("try {\n"
422 " throw a * b;\n"
423 "}\n"
424 "catch (int a) {\n"
425 " // Do nothing.\n"
426 "}\n"
427 "catch (...) {\n"
428 " exit(42);\n"
429 "}");
430
431 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000432 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000433 "catch (...) {\n"
434 " return 5;\n"
435 "}");
436 verifyFormat("class A {\n"
437 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000438 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000439 " catch (...) {\n"
440 " throw;\n"
441 " }\n"
442 "};\n");
443}
444
445TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000446 verifyFormat("@try {\n"
447 " f();\n"
448 "}\n"
449 "@catch (NSException e) {\n"
450 " @throw;\n"
451 "}\n"
452 "@finally {\n"
453 " exit(42);\n"
454 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000455}
456
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000457TEST_F(FormatTest, StaticInitializers) {
458 verifyFormat("static SomeClass SC = { 1, 'a' };");
459
460 // FIXME: Format like enums if the static initializer does not fit on a line.
461 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000462 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000463 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
464 "};");
465
466 verifyFormat(
467 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
468 " looooooooooooooooooooooooooooooooooongname,\n"
469 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000470}
471
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000472TEST_F(FormatTest, NestedStaticInitializers) {
473 verifyFormat("static A x = { { {} } };\n");
474 verifyFormat(
475 "static A x = {\n"
476 " { { init1, init2, init3, init4 }, { init1, init2, init3, init4 } }\n"
477 "};\n");
478 verifyFormat(
479 "somes Status::global_reps[3] = {\n"
480 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
481 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
482 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
483 "};");
484 verifyFormat(
485 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
486 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
487 " } };");
488
489 // FIXME: We might at some point want to handle this similar to parameters
490 // lists, where we have an option to put each on a single line.
491 verifyFormat("struct {\n"
492 " unsigned bit;\n"
493 " const char *const name;\n"
494 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
495 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
496}
497
Manuel Klimeka080a182013-01-02 16:30:12 +0000498TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
499 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
500 " \\\n"
501 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
502}
503
Daniel Jasper71607512013-01-07 10:48:50 +0000504TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000505 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
506 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000507}
508
Manuel Klimeka080a182013-01-02 16:30:12 +0000509TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
510 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000511 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000512}
513
514TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
515 EXPECT_EQ("#line 42 \"test\"\n",
516 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000517 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000518 format("# \\\n define \\\n A \\\n B\n",
519 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000520}
521
522TEST_F(FormatTest, EndOfFileEndsPPDirective) {
523 EXPECT_EQ("#line 42 \"test\"",
524 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000525 EXPECT_EQ("#define A B",
526 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000527}
528
Manuel Klimek060143e2013-01-02 18:33:23 +0000529TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000530 // If the macro fits in one line, we still do not get the full
531 // line, as only the next line decides whether we need an escaped newline and
532 // thus use the last column.
533 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000534
Manuel Klimekd544c572013-01-07 09:24:17 +0000535 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
536 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000537 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000538
539 verifyFormat("#define A A\n#define A A");
540 verifyFormat("#define A(X) A\n#define A A");
541
542 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
543 verifyFormat("#define Something \\\n"
544 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000545}
546
547TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000548 EXPECT_EQ("// some comment\n"
549 "#include \"a.h\"\n"
550 "#define A(A,\\\n"
551 " B)\n"
552 "#include \"b.h\"\n"
553 "// some comment\n",
554 format(" // some comment\n"
555 " #include \"a.h\"\n"
556 "#define A(A,\\\n"
557 " B)\n"
558 " #include \"b.h\"\n"
559 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000560}
561
Manuel Klimekd4397b92013-01-04 23:34:14 +0000562TEST_F(FormatTest, LayoutSingleHash) {
563 EXPECT_EQ("#\na;", format("#\na;"));
564}
565
566TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
567 EXPECT_EQ("#define A \\\n"
568 " c; \\\n"
569 " e;\n"
570 "f;", format("#define A c; e;\n"
571 "f;", getLLVMStyleWithColumns(14)));
572}
573
574TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000575 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000576}
577
578TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000579 EXPECT_EQ("# define A\\\n b;",
580 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000581}
582
583TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000584 EXPECT_EQ("int x,\n"
585 "#define A\n"
586 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000587}
588
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000589TEST_F(FormatTest, HashInMacroDefinition) {
590 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
591 verifyFormat("#define A \\\n"
592 " { \\\n"
593 " f(#c);\\\n"
594 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000595
596 verifyFormat("#define A(X) \\\n"
597 " void function##X()", getLLVMStyleWithColumns(22));
598
599 verifyFormat("#define A(a, b, c) \\\n"
600 " void a##b##c()", getLLVMStyleWithColumns(22));
601
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000602 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000603}
604
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000605TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
606 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
607}
608
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000609TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000610 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000611}
612
Manuel Klimeka5342db2013-01-06 20:07:31 +0000613TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
614 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
615 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
616 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
617 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
618}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000619
Manuel Klimek95419382013-01-07 07:56:50 +0000620TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000621 EXPECT_EQ(
622 "#define A \\\n int i; \\\n int j;",
623 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000624}
625
Manuel Klimekd544c572013-01-07 09:24:17 +0000626TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
627 verifyFormat("#define A \\\n"
628 " int v( \\\n"
629 " a); \\\n"
630 " int i;", getLLVMStyleWithColumns(11));
631}
632
Manuel Klimeka080a182013-01-02 16:30:12 +0000633TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000634 EXPECT_EQ(
635 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
636 " \\\n"
637 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
638 "\n"
639 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
640 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
641 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
642 "\\\n"
643 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
644 " \n"
645 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
646 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000647}
648
Manuel Klimek526ed112013-01-09 15:25:02 +0000649TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
650 EXPECT_EQ("int\n"
651 "#define A\n"
652 " a;",
653 format("int\n#define A\na;"));
654 verifyFormat(
655 "functionCallTo(someOtherFunction(\n"
656 " withSomeParameters, whichInSequence,\n"
657 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000658 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000659 " withMoreParamters,\n"
660 " whichStronglyInfluenceTheLayout),\n"
661 " andMoreParameters),\n"
662 " trailing);", getLLVMStyleWithColumns(69));
663}
664
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000665TEST_F(FormatTest, LayoutBlockInsideParens) {
666 EXPECT_EQ("functionCall({\n"
667 " int i;\n"
668 "});", format(" functionCall ( {int i;} );"));
669}
670
671TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000672 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000673 "int i;", format(" SOME_MACRO {int i;} int i;"));
674}
675
676TEST_F(FormatTest, LayoutNestedBlocks) {
677 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
678 " struct s {\n"
679 " int i;\n"
680 " };\n"
681 " s kBitsToOs[] = { { 10 } };\n"
682 " for (int i = 0; i < 10; ++i)\n"
683 " return;\n"
684 "}");
685}
686
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000687TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
688 EXPECT_EQ("{}", format("{}"));
689}
690
Alexander Kornienko15757312012-12-06 18:03:27 +0000691//===----------------------------------------------------------------------===//
692// Line break tests.
693//===----------------------------------------------------------------------===//
694
695TEST_F(FormatTest, FormatsFunctionDefinition) {
696 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
697 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000698 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000699}
700
701TEST_F(FormatTest, FormatsAwesomeMethodCall) {
702 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000703 "SomeLongMethodName(SomeReallyLongMethod(\n"
704 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
705 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000706}
707
Daniel Jasper1321eb52012-12-18 21:05:13 +0000708TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000709 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000710 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
711 getLLVMStyleWithColumns(45));
712 verifyFormat("Constructor()\n"
713 " : Inttializer(FitsOnTheLine) {}",
714 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000715
716 verifyFormat(
717 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000718 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000719
720 verifyFormat(
721 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000722 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
723 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
724 verifyGoogleFormat(
725 "SomeClass::Constructor()\n"
726 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
727 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
728 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
729
730 verifyFormat(
731 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000732 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000733 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000734
735 verifyFormat("Constructor()\n"
736 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
737 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
738 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000739 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000740
741 // Here a line could be saved by splitting the second initializer onto two
742 // lines, but that is not desireable.
743 verifyFormat("Constructor()\n"
744 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
745 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000746 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000747
748 verifyGoogleFormat("MyClass::MyClass(int var)\n"
749 " : some_var_(var), // 4 space indent\n"
750 " some_other_var_(var + 1) { // lined up\n"
751 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000752
753 // This test takes VERY long when memoization is broken.
754 verifyGoogleFormat(
755 "Constructor()\n"
756 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
757 " a, a, a,\n"
758 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
759 " a, a, a,\n"
760 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
761 " a, a, a,\n"
762 " a, a, a, a, a, a, a, a, a, a, a)\n"
763 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
764 " a, a, a,\n"
765 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
766 " a, a, a,\n"
767 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
768 " a, a, a,\n"
769 " a, a, a, a, a, a, a, a, a, a, a) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000770}
771
Alexander Kornienko15757312012-12-06 18:03:27 +0000772TEST_F(FormatTest, BreaksAsHighAsPossible) {
773 verifyFormat(
774 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
775 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
776 " f();");
777}
778
Daniel Jasperbac016b2012-12-03 18:12:45 +0000779TEST_F(FormatTest, BreaksDesireably) {
780 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
781 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000782 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000783
784 verifyFormat(
785 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000787
788 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
789 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000791
792 verifyFormat(
793 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
794 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
795 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000797
798 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
799 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
800
Daniel Jasper723f0302013-01-02 14:40:02 +0000801 verifyFormat(
802 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
804
Daniel Jasper33182dd2012-12-05 14:57:28 +0000805 // This test case breaks on an incorrect memoization, i.e. an optimization not
806 // taking into account the StopAt value.
807 verifyFormat(
808 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000809 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
810 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
811 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000812
Daniel Jaspercd162382013-01-07 13:26:07 +0000813 verifyFormat("{\n {\n {\n"
814 " Annotation.SpaceRequiredBefore =\n"
815 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
816 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
817 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000818}
819
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000820TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
821 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
822 " GUARDED_BY(aaaaaaaaaaaaa);");
823}
824
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000825TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
826 verifyFormat(
827 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000828 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000829 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000830 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000831 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000832 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000833 verifyFormat(
834 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000835 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000836}
837
Daniel Jasper9cda8002013-01-07 13:08:40 +0000838TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
839 verifyFormat(
840 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
841 " SI->getAlignment(),\n"
842 " SI->getPointerAddressSpaceee());\n");
843 verifyFormat(
844 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
845 " Line.Tokens.front().Tok.getLocation(),\n"
846 " Line.Tokens.back().Tok.getLocation());");
847}
848
Daniel Jaspercf225b62012-12-24 13:43:52 +0000849TEST_F(FormatTest, AlignsAfterAssignments) {
850 verifyFormat(
851 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000852 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000853 verifyFormat(
854 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000855 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000856 verifyFormat(
857 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000858 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000859 verifyFormat(
860 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000861 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000862 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000863 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
864 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
865 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000866}
867
868TEST_F(FormatTest, AlignsAfterReturn) {
869 verifyFormat(
870 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
871 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
872 verifyFormat(
873 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
874 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
875}
876
Daniel Jasper9c837d02013-01-09 07:06:56 +0000877TEST_F(FormatTest, BreaksConditionalExpressions) {
878 verifyFormat(
879 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
880 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
881 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
882 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
883 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
884}
885
Nico Weber7d37b8b2013-01-12 01:28:06 +0000886TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
887 verifyFormat("arr[foo ? bar : baz];");
888 verifyFormat("f()[foo ? bar : baz];");
889 verifyFormat("(a + b)[foo ? bar : baz];");
890 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
891}
892
Daniel Jasperbac016b2012-12-03 18:12:45 +0000893TEST_F(FormatTest, AlignsStringLiterals) {
894 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
895 " \"short literal\");");
896 verifyFormat(
897 "looooooooooooooooooooooooongFunction(\n"
898 " \"short literal\"\n"
899 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
900}
901
Alexander Kornienko15757312012-12-06 18:03:27 +0000902TEST_F(FormatTest, AlignsPipes) {
903 verifyFormat(
904 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
905 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
906 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
907 verifyFormat(
908 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
909 " << aaaaaaaaaaaaaaaaaaaa;");
910 verifyFormat(
911 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
912 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
913 verifyFormat(
914 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
915 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
916 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
917 verifyFormat(
918 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
919 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
920 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
921}
922
Daniel Jasperbac016b2012-12-03 18:12:45 +0000923TEST_F(FormatTest, UnderstandsEquals) {
924 verifyFormat(
925 "aaaaaaaaaaaaaaaaa =\n"
926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
927 verifyFormat(
928 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000929 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000930 verifyFormat(
931 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000932 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000933 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000934 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000935
Daniel Jasper9cda8002013-01-07 13:08:40 +0000936 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000937 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000938 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000939 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000940}
941
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000942TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000943 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
944 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000945
Daniel Jasper1321eb52012-12-18 21:05:13 +0000946 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
947 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000948
949 verifyFormat(
950 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
951 " Parameter2);");
952
953 verifyFormat(
954 "ShortObject->shortFunction(\n"
955 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
956 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
957
958 verifyFormat("loooooooooooooongFunction(\n"
959 " LoooooooooooooongObject->looooooooooooooooongFunction());");
960
961 verifyFormat(
962 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
963 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
964
Daniel Jasper46a46a22013-01-07 07:13:20 +0000965 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000966 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000967 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000968 verifyFormat(
969 "aaaaaaaaaaa->aaaaaaaaa(\n"
970 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
971 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000972}
973
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000974TEST_F(FormatTest, WrapsTemplateDeclarations) {
975 verifyFormat("template <typename T>\n"
976 "virtual void loooooooooooongFunction(int Param1, int Param2);");
977 verifyFormat(
978 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
979 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
980 verifyFormat(
981 "template <typename T>\n"
982 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
983 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000984 verifyFormat(
985 "template <typename T>\n"
986 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
987 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
988 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000989 verifyFormat("template <typename T>\n"
990 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
991 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000992 verifyFormat(
993 "template <typename T1, typename T2 = char, typename T3 = char,\n"
994 " typename T4 = char>\n"
995 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000996}
997
Daniel Jasperbac016b2012-12-03 18:12:45 +0000998TEST_F(FormatTest, UnderstandsTemplateParameters) {
999 verifyFormat("A<int> a;");
1000 verifyFormat("A<A<A<int> > > a;");
1001 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1002 verifyFormat("bool x = a < 1 || 2 > a;");
1003 verifyFormat("bool x = 5 < f<int>();");
1004 verifyFormat("bool x = f<int>() > 5;");
1005 verifyFormat("bool x = 5 < a<int>::x;");
1006 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1007 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1008
1009 verifyGoogleFormat("A<A<int>> a;");
1010 verifyGoogleFormat("A<A<A<int>>> a;");
1011 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1012
1013 verifyFormat("test >> a >> b;");
1014 verifyFormat("test << a >> b;");
1015
1016 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001017 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001018}
1019
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001020TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001021 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001022 verifyFormat("f(-1, -2, -3);");
1023 verifyFormat("a[-1] = 5;");
1024 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001025 verifyFormat("if (i == -1) {}");
1026 verifyFormat("if (i != -1) {}");
1027 verifyFormat("if (i > -1) {}");
1028 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001029 verifyFormat("++(a->f());");
1030 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001031 verifyFormat("(a->f())++;");
1032 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001033 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001034
1035 verifyFormat("a-- > b;");
1036 verifyFormat("b ? -a : c;");
1037 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001038 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001039 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001040 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001041
1042 verifyFormat("return -1;");
1043 verifyFormat("switch (a) {\n"
1044 "case -1:\n"
1045 " break;\n"
1046 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001047
1048 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1049 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001050
1051 verifyFormat("int a = /* confusing comment */ -1;");
1052 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1053 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001054}
1055
1056TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001057 verifyFormat("bool operator<();");
1058 verifyFormat("bool operator>();");
1059 verifyFormat("bool operator=();");
1060 verifyFormat("bool operator==();");
1061 verifyFormat("bool operator!=();");
1062 verifyFormat("int operator+();");
1063 verifyFormat("int operator++();");
1064 verifyFormat("bool operator();");
1065 verifyFormat("bool operator()();");
1066 verifyFormat("bool operator[]();");
1067 verifyFormat("operator bool();");
1068 verifyFormat("operator SomeType<int>();");
1069 verifyFormat("void *operator new(std::size_t size);");
1070 verifyFormat("void *operator new[](std::size_t size);");
1071 verifyFormat("void operator delete(void *ptr);");
1072 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001073}
1074
Daniel Jasper088dab52013-01-11 16:09:04 +00001075TEST_F(FormatTest, UnderstandsNewAndDelete) {
1076 verifyFormat("A *a = new A;");
1077 verifyFormat("A *a = new (placement) A;");
1078 verifyFormat("delete a;");
1079 verifyFormat("delete (A *)a;");
1080}
1081
Daniel Jasper5d334402013-01-02 08:57:10 +00001082TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001083 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001084 verifyFormat("f(a, *a);");
1085 verifyFormat("f(*a);");
1086 verifyFormat("int a = b * 10;");
1087 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001088 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001089 verifyFormat("int a += b * c;");
1090 verifyFormat("int a -= b * c;");
1091 verifyFormat("int a *= b * c;");
1092 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001093 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001094 verifyFormat("int a = *b * c;");
1095 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001096 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001097 verifyFormat("return 10 * b;");
1098 verifyFormat("return *b * *c;");
1099 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001100 verifyFormat("f(b ? *c : *d);");
1101 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001102 verifyFormat("*b = a;");
1103 verifyFormat("a * ~b;");
1104 verifyFormat("a * !b;");
1105 verifyFormat("a * +b;");
1106 verifyFormat("a * -b;");
1107 verifyFormat("a * ++b;");
1108 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001109 verifyFormat("a[4] * b;");
1110 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001111 verifyFormat("a * [self dostuff];");
1112 verifyFormat("a * (a + b);");
1113 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001114 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001115
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001116 verifyFormat("InvalidRegions[*R] = 0;");
1117
Daniel Jasper8b39c662012-12-10 18:59:13 +00001118 verifyFormat("A<int *> a;");
1119 verifyFormat("A<int **> a;");
1120 verifyFormat("A<int *, int *> a;");
1121 verifyFormat("A<int **, int **> a;");
1122
Daniel Jasper2db356d2013-01-08 20:03:18 +00001123 verifyFormat(
1124 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1126
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001127 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001128 verifyGoogleFormat("A<int*> a;");
1129 verifyGoogleFormat("A<int**> a;");
1130 verifyGoogleFormat("A<int*, int*> a;");
1131 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001132 verifyGoogleFormat("f(b ? *c : *d);");
1133 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001134
1135 verifyFormat("a = *(x + y);");
1136 verifyFormat("a = &(x + y);");
1137 verifyFormat("*(x + y).call();");
1138 verifyFormat("&(x + y)->call();");
1139 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001140
1141 verifyFormat("f(b * /* confusing comment */ ++c);");
1142 verifyFormat(
1143 "int *MyValues = {\n"
1144 " *A, // Operator detection might be confused by the '{'\n"
1145 " *BB // Operator detection might be confused by previous comment\n"
1146 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001147}
1148
Daniel Jasper4981bd02013-01-13 08:01:36 +00001149TEST_F(FormatTest, FormatsCasts) {
1150 verifyFormat("Type *A = static_cast<Type *>(P);");
1151 verifyFormat("Type *A = (Type *)P;");
1152 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1153 verifyFormat("int a = (int)(2.0f);");
1154
1155 // FIXME: These also need to be identified.
1156 verifyFormat("int a = (int) 2.0f;");
1157 verifyFormat("int a = (int) * b;");
1158
1159 // These are not casts.
1160 verifyFormat("void f(int *) {}");
1161 verifyFormat("void f(int *);");
1162 verifyFormat("void f(int *) = 0;");
1163 verifyFormat("void f(SmallVector<int>) {}");
1164 verifyFormat("void f(SmallVector<int>);");
1165 verifyFormat("void f(SmallVector<int>) = 0;");
1166}
1167
Daniel Jasper46ef8522013-01-10 13:08:12 +00001168TEST_F(FormatTest, FormatsFunctionTypes) {
1169 // FIXME: Determine the cases that need a space after the return type and fix.
1170 verifyFormat("A<bool()> a;");
1171 verifyFormat("A<SomeType()> a;");
1172 verifyFormat("A<void(*)(int, std::string)> a;");
1173
1174 verifyFormat("int(*func)(void *);");
1175}
1176
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001177TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001178 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001179 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001180 verifyFormat(
1181 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1182 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001183 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001184}
1185
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001186TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1187 verifyFormat("(a)->b();");
1188 verifyFormat("--a;");
1189}
1190
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001191TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001192 verifyFormat("#include <string>\n"
1193 "#include <a/b/c.h>\n"
1194 "#include \"a/b/string\"\n"
1195 "#include \"string.h\"\n"
1196 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001197 "#include <a-a>\n"
1198 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001199
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001200 verifyFormat("#import <string>");
1201 verifyFormat("#import <a/b/c.h>");
1202 verifyFormat("#import \"a/b/string\"");
1203 verifyFormat("#import \"string.h\"");
1204 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001205}
1206
Alexander Kornienko15757312012-12-06 18:03:27 +00001207//===----------------------------------------------------------------------===//
1208// Error recovery tests.
1209//===----------------------------------------------------------------------===//
1210
Daniel Jasper700e7102013-01-10 09:26:47 +00001211TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1212 verifyFormat("void f() { return } 42");
1213}
1214
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001215TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1216 verifyFormat("int aaaaaaaa =\n"
1217 " // Overly long comment\n"
1218 " b;", getLLVMStyleWithColumns(20));
1219 verifyFormat("function(\n"
1220 " ShortArgument,\n"
1221 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1222}
1223
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001224TEST_F(FormatTest, IncorrectAccessSpecifier) {
1225 verifyFormat("public:");
1226 verifyFormat("class A {\n"
1227 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001228 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001229 "};");
1230 verifyFormat("public\n"
1231 "int qwerty;");
1232 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001233 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001234 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001235 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001236 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001237 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001238}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001239
Alexander Kornienko393b0082012-12-04 15:40:36 +00001240TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1241 verifyFormat("{");
1242}
1243
1244TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001245 verifyFormat("do {}");
1246 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001247 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001248 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001249 "wheeee(fun);");
1250 verifyFormat("do {\n"
1251 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001252 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001253}
1254
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001255TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001256 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001257 verifyFormat("switch {\n foo;\n foo();\n}");
1258 verifyFormat("for {\n foo;\n foo();\n}");
1259 verifyFormat("while {\n foo;\n foo();\n}");
1260 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001261}
1262
Daniel Jasper1f42f112013-01-04 18:52:56 +00001263TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1264 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001265 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001266}
1267
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001268TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001269 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1270 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1271 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1272 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001273
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001274 EXPECT_EQ("{\n"
1275 " {\n"
1276 " breakme(\n"
1277 " qwe);\n"
1278 "}\n", format("{\n"
1279 " {\n"
1280 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001281 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001282}
1283
Manuel Klimek2851c162013-01-10 14:36:46 +00001284TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1285 verifyFormat(
1286 "int x = {\n"
1287 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001288 " b(alongervariable)\n"
1289 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001290}
1291
1292TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1293 verifyFormat(
1294 "Aaa({\n"
1295 " int i;\n"
1296 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1297 " ccccccccccccccccc));");
1298}
1299
Manuel Klimek517e8942013-01-11 17:54:10 +00001300TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1301 verifyFormat("void f() { return 42; }");
1302 verifyFormat("void f() {\n"
1303 " // Comment\n"
1304 "}");
1305 verifyFormat("{\n"
1306 "#error {\n"
1307 " int a;\n"
1308 "}");
1309 verifyFormat("{\n"
1310 " int a;\n"
1311 "#error {\n"
1312 "}");
1313}
1314
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001315TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1316 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001317 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001318 verifyFormat("class foo a = { bar };\nint n;");
1319 verifyFormat("union foo a = { bar };\nint n;");
1320
1321 // Elaborate types inside function definitions.
1322 verifyFormat("struct foo f() {}\nint n;");
1323 verifyFormat("class foo f() {}\nint n;");
1324 verifyFormat("union foo f() {}\nint n;");
1325
1326 // Templates.
1327 verifyFormat("template <class X> void f() {}\nint n;");
1328 verifyFormat("template <struct X> void f() {}\nint n;");
1329 verifyFormat("template <union X> void f() {}\nint n;");
1330
1331 // Actual definitions...
1332 verifyFormat("struct {} n;");
1333 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1334 verifyFormat("union Z {\n int n;\n} x;");
1335 verifyFormat("class MACRO Z {} n;");
1336 verifyFormat("class MACRO(X) Z {} n;");
1337 verifyFormat("class __attribute__(X) Z {} n;");
1338 verifyFormat("class __declspec(X) Z {} n;");
1339
1340 // Elaborate types where incorrectly parsing the structural element would
1341 // break the indent.
1342 verifyFormat("if (true)\n"
1343 " class X x;\n"
1344 "else\n"
1345 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001346}
1347
Manuel Klimek407a31a2013-01-15 15:50:27 +00001348TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1349 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1350 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1351 EXPECT_EQ("#error 1", format(" # error 1"));
1352 EXPECT_EQ("#warning 1", format(" # warning 1"));
1353}
1354
Manuel Klimek517e8942013-01-11 17:54:10 +00001355// FIXME: This breaks the order of the unwrapped lines:
1356// TEST_F(FormatTest, OrderUnwrappedLines) {
1357// verifyFormat("{\n"
1358// " bool a; //\n"
1359// "#error {\n"
1360// " int a;\n"
1361// "}");
1362// }
1363
Nico Webercf4a79c2013-01-08 17:56:31 +00001364//===----------------------------------------------------------------------===//
1365// Objective-C tests.
1366//===----------------------------------------------------------------------===//
1367
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001368TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1369 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1370 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1371 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001372 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001373 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1374 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1375 format("-(NSInteger)Method3:(id)anObject;"));
1376 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1377 format("-(NSInteger)Method4:(id)anObject;"));
1378 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1379 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1380 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1381 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001382 EXPECT_EQ(
1383 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1384 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001385
1386 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001387 EXPECT_EQ(
1388 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1389 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1390 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1391 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1392 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1393 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1394 format(
1395 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1396 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1397 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1398 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1399 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1400 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001401
1402 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001403 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001404 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1405 // protocol lists (but not for template classes):
1406 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001407
1408 verifyFormat("- (int(*)())foo:(int(*)())f;");
1409 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1410
1411 // If there's no return type (very rare in practice!), LLVM and Google style
1412 // agree.
1413 verifyFormat("- foo:(int)f;");
1414 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001415}
1416
Daniel Jasper886568d2013-01-09 08:36:49 +00001417TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001418 verifyFormat("int (^Block)(int, int);");
1419 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001420}
1421
Nico Weber27d13672013-01-09 20:25:35 +00001422TEST_F(FormatTest, FormatObjCInterface) {
1423 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001424 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001425 "@public\n"
1426 " int field1;\n"
1427 "@protected\n"
1428 " int field2;\n"
1429 "@private\n"
1430 " int field3;\n"
1431 "@package\n"
1432 " int field4;\n"
1433 "}\n"
1434 "+ (id)init;\n"
1435 "@end");
1436
Nico Weber27d13672013-01-09 20:25:35 +00001437 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1438 " @public\n"
1439 " int field1;\n"
1440 " @protected\n"
1441 " int field2;\n"
1442 " @private\n"
1443 " int field3;\n"
1444 " @package\n"
1445 " int field4;\n"
1446 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001447 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001448 "@end");
1449
1450 verifyFormat("@interface Foo\n"
1451 "+ (id)init;\n"
1452 "// Look, a comment!\n"
1453 "- (int)answerWith:(int)i;\n"
1454 "@end");
1455
1456 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001457 "@end\n"
1458 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001459 "@end");
1460
1461 verifyFormat("@interface Foo : Bar\n"
1462 "+ (id)init;\n"
1463 "@end");
1464
Nico Weber5f500df2013-01-10 20:12:55 +00001465 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001466 "+ (id)init;\n"
1467 "@end");
1468
Nico Weber5f500df2013-01-10 20:12:55 +00001469 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001470 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001471 "@end");
1472
Nico Webered91bba2013-01-10 19:19:14 +00001473 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001474 "+ (id)init;\n"
1475 "@end");
1476
Nico Webered91bba2013-01-10 19:19:14 +00001477 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001478 "+ (id)init;\n"
1479 "@end");
1480
Nico Weber5f500df2013-01-10 20:12:55 +00001481 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001482 "+ (id)init;\n"
1483 "@end");
1484
Nico Weber5f500df2013-01-10 20:12:55 +00001485 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001486 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001487 "@end");
1488
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001489 verifyFormat("@interface Foo {\n"
1490 " int _i;\n"
1491 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001492 "+ (id)init;\n"
1493 "@end");
1494
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001495 verifyFormat("@interface Foo : Bar {\n"
1496 " int _i;\n"
1497 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001498 "+ (id)init;\n"
1499 "@end");
1500
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001501 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1502 " int _i;\n"
1503 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001504 "+ (id)init;\n"
1505 "@end");
1506
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001507 verifyFormat("@interface Foo (HackStuff) {\n"
1508 " int _i;\n"
1509 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001510 "+ (id)init;\n"
1511 "@end");
1512
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001513 verifyFormat("@interface Foo () {\n"
1514 " int _i;\n"
1515 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001516 "+ (id)init;\n"
1517 "@end");
1518
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001519 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1520 " int _i;\n"
1521 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001522 "+ (id)init;\n"
1523 "@end");
1524}
1525
Nico Weber50767d82013-01-09 23:25:37 +00001526TEST_F(FormatTest, FormatObjCImplementation) {
1527 verifyFormat("@implementation Foo : NSObject {\n"
1528 "@public\n"
1529 " int field1;\n"
1530 "@protected\n"
1531 " int field2;\n"
1532 "@private\n"
1533 " int field3;\n"
1534 "@package\n"
1535 " int field4;\n"
1536 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001537 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001538 "@end");
1539
1540 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1541 " @public\n"
1542 " int field1;\n"
1543 " @protected\n"
1544 " int field2;\n"
1545 " @private\n"
1546 " int field3;\n"
1547 " @package\n"
1548 " int field4;\n"
1549 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001550 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001551 "@end");
1552
1553 verifyFormat("@implementation Foo\n"
1554 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001555 " if (true)\n"
1556 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001557 "}\n"
1558 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001559 "- (int)answerWith:(int)i {\n"
1560 " return i;\n"
1561 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001562 "+ (int)answerWith:(int)i {\n"
1563 " return i;\n"
1564 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001565 "@end");
1566
1567 verifyFormat("@implementation Foo\n"
1568 "@end\n"
1569 "@implementation Bar\n"
1570 "@end");
1571
1572 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001573 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001574 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001575 "@end");
1576
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001577 verifyFormat("@implementation Foo {\n"
1578 " int _i;\n"
1579 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001580 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001581 "@end");
1582
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001583 verifyFormat("@implementation Foo : Bar {\n"
1584 " int _i;\n"
1585 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001586 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001587 "@end");
1588
Nico Webered91bba2013-01-10 19:19:14 +00001589 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001590 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001591 "@end");
1592}
1593
Nico Weber1abe6ea2013-01-09 21:15:03 +00001594TEST_F(FormatTest, FormatObjCProtocol) {
1595 verifyFormat("@protocol Foo\n"
1596 "@property(weak) id delegate;\n"
1597 "- (NSUInteger)numberOfThings;\n"
1598 "@end");
1599
Nico Weber5f500df2013-01-10 20:12:55 +00001600 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001601 "- (NSUInteger)numberOfThings;\n"
1602 "@end");
1603
Nico Weber5f500df2013-01-10 20:12:55 +00001604 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001605 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001606 "@end");
1607
Nico Weber1abe6ea2013-01-09 21:15:03 +00001608 verifyFormat("@protocol Foo;\n"
1609 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001610
1611 verifyFormat("@protocol Foo\n"
1612 "@end\n"
1613 "@protocol Bar\n"
1614 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001615
1616 verifyFormat("@protocol myProtocol\n"
1617 "- (void)mandatoryWithInt:(int)i;\n"
1618 "@optional\n"
1619 "- (void)optional;\n"
1620 "@required\n"
1621 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001622 "@optional\n"
1623 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001624 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001625}
1626
Nico Weberbcfdd262013-01-12 06:18:40 +00001627TEST_F(FormatTest, FormatObjCMethodExpr) {
1628 verifyFormat("[foo bar:baz];");
1629 verifyFormat("return [foo bar:baz];");
1630 verifyFormat("f([foo bar:baz]);");
1631 verifyFormat("f(2, [foo bar:baz]);");
1632 verifyFormat("f(2, a ? b : c);");
1633 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1634
1635 verifyFormat("[foo bar:baz], [foo bar:baz];");
1636 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1637 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1638 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1639 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1640 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1641 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1642 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1643 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1644 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1645 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1646 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1647 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [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 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1657 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1658 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1659 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1660 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1661 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1662 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1663 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1664 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1665 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1666 // Whew!
1667
1668 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1669 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1670 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1671 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1672 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001673 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001674 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001675
Nico Weberbcfdd262013-01-12 06:18:40 +00001676 verifyFormat("arr[[self indexForFoo:a]];");
1677 verifyFormat("throw [self errorFor:a];");
1678 verifyFormat("@throw [self errorFor:a];");
1679
Nico Webere8ccc812013-01-12 22:48:47 +00001680 // This tests that the formatter doesn't break after "backing" but before ":",
1681 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001682 verifyFormat(
1683 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001684 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1685 " backing:NSBackingStoreBuffered defer:YES]))");
1686
Nico Webere8ccc812013-01-12 22:48:47 +00001687 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1688 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001689
1690}
1691
Nico Weber581f5572013-01-07 15:56:25 +00001692TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001693 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001694 verifyFormat("@catch");
1695 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001696 verifyFormat("@compatibility_alias");
1697 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001698 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001699 verifyFormat("@encode");
1700 verifyFormat("@end");
1701 verifyFormat("@finally");
1702 verifyFormat("@implementation");
1703 verifyFormat("@import");
1704 verifyFormat("@interface");
1705 verifyFormat("@optional");
1706 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001707 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001708 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001709 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001710 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001711 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001712 verifyFormat("@required");
1713 verifyFormat("@selector");
1714 verifyFormat("@synchronized");
1715 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001716 verifyFormat("@throw");
1717 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001718
Nico Webercb4d6902013-01-08 19:40:21 +00001719 verifyFormat("@\"String\"");
1720 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001721 verifyFormat("@+4.8");
1722 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001723 verifyFormat("@1LL");
1724 verifyFormat("@.5");
1725 verifyFormat("@'c'");
1726 verifyFormat("@true");
1727 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001728 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001729 verifyFormat("@[");
1730 verifyFormat("@{");
1731
Nico Weber581f5572013-01-07 15:56:25 +00001732 EXPECT_EQ("@interface", format("@ interface"));
1733
1734 // The precise formatting of this doesn't matter, nobody writes code like
1735 // this.
1736 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001737}
1738
Nico Weberc31689a2013-01-08 19:15:23 +00001739TEST_F(FormatTest, ObjCSnippets) {
1740 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001741 verifyFormat("@autoreleasepool {\n"
1742 " foo();\n"
1743 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001744 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001745 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001746 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001747 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001748 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001749 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001750 verifyFormat("@synchronized(self) {\n"
1751 " f();\n"
1752 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001753
Nico Weber70848232013-01-10 21:30:42 +00001754 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1755 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1756
Nico Webercf4a79c2013-01-08 17:56:31 +00001757 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001758 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1759 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001760}
1761
Daniel Jaspercd162382013-01-07 13:26:07 +00001762} // end namespace tooling
1763} // end namespace clang