blob: 39d8bb8f774aacd2bb947861cd5bb97357a8d0d2 [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,
32 Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +000033 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
34 return Context.getRewrittenText(ID);
35 }
36
37 std::string format(llvm::StringRef Code,
38 const FormatStyle &Style = getLLVMStyle()) {
39 return format(Code, 0, Code.size(), Style);
40 }
41
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000042 std::string messUp(llvm::StringRef Code) {
43 std::string MessedUp(Code.str());
44 bool InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000045 bool InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000046 bool JustReplacedNewline = false;
47 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
48 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
49 if (JustReplacedNewline)
50 MessedUp[i - 1] = '\n';
51 InComment = true;
Manuel Klimek526ed112013-01-09 15:25:02 +000052 } else if (MessedUp[i] == '#' && JustReplacedNewline) {
53 MessedUp[i - 1] = '\n';
54 InPreprocessorDirective = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +000055 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
56 MessedUp[i] = ' ';
Manuel Klimek526ed112013-01-09 15:25:02 +000057 MessedUp[i + 1] = ' ';
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000058 } else if (MessedUp[i] == '\n') {
59 if (InComment) {
60 InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000061 } else if (InPreprocessorDirective) {
62 InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000063 } else {
64 JustReplacedNewline = true;
65 MessedUp[i] = ' ';
66 }
Manuel Klimekd4397b92013-01-04 23:34:14 +000067 } else if (MessedUp[i] != ' ') {
68 JustReplacedNewline = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000069 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000070 }
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000071 return MessedUp;
72 }
73
Manuel Klimek060143e2013-01-02 18:33:23 +000074 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
75 FormatStyle Style = getLLVMStyle();
76 Style.ColumnLimit = ColumnLimit;
77 return Style;
78 }
79
80 void verifyFormat(llvm::StringRef Code,
81 const FormatStyle &Style = getLLVMStyle()) {
82 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000083 }
84
85 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +000086 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +000087 }
88};
89
Manuel Klimek526ed112013-01-09 15:25:02 +000090TEST_F(FormatTest, MessUp) {
91 EXPECT_EQ("1 2 3", messUp("1 2 3"));
92 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
93 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
94 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
95 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
96}
97
Alexander Kornienko15757312012-12-06 18:03:27 +000098//===----------------------------------------------------------------------===//
99// Basic function tests.
100//===----------------------------------------------------------------------===//
101
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
103 EXPECT_EQ(";", format(";"));
104}
105
106TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
107 EXPECT_EQ("int i;", format(" int i;"));
108 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
109 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
110 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
111}
112
113TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
114 EXPECT_EQ("int i;", format("int\ni;"));
115}
116
117TEST_F(FormatTest, FormatsNestedBlockStatements) {
118 EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}"));
119}
120
Alexander Kornienko15757312012-12-06 18:03:27 +0000121TEST_F(FormatTest, FormatsNestedCall) {
122 verifyFormat("Method(f1, f2(f3));");
123 verifyFormat("Method(f1(f2, f3()));");
124}
125
Alexander Kornienko15757312012-12-06 18:03:27 +0000126//===----------------------------------------------------------------------===//
127// Tests for control statements.
128//===----------------------------------------------------------------------===//
129
130TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
131 verifyFormat("if (true)\n f();\ng();");
132 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
133 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000134 verifyFormat("if (a)\n"
135 " // comment\n"
136 " f();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000137}
138
139TEST_F(FormatTest, ParseIfElse) {
140 verifyFormat("if (true)\n"
141 " if (true)\n"
142 " if (true)\n"
143 " f();\n"
144 " else\n"
145 " g();\n"
146 " else\n"
147 " h();\n"
148 "else\n"
149 " i();");
150 verifyFormat("if (true)\n"
151 " if (true)\n"
152 " if (true) {\n"
153 " if (true)\n"
154 " f();\n"
155 " } else {\n"
156 " g();\n"
157 " }\n"
158 " else\n"
159 " h();\n"
160 "else {\n"
161 " i();\n"
162 "}");
163}
164
165TEST_F(FormatTest, ElseIf) {
166 verifyFormat("if (a) {\n"
167 "} else if (b) {\n"
168 "}");
169 verifyFormat("if (a)\n"
170 " f();\n"
171 "else if (b)\n"
172 " g();\n"
173 "else\n"
174 " h();");
175}
176
Daniel Jasperbac016b2012-12-03 18:12:45 +0000177TEST_F(FormatTest, FormatsForLoop) {
178 verifyFormat(
179 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000180 " ++VeryVeryLongLoopVariable)\n"
181 " ;");
182 verifyFormat("for (;;)\n"
183 " f();");
184 verifyFormat("for (;;) {\n"
185 "}");
186 verifyFormat("for (;;) {\n"
187 " f();\n"
188 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000189
190 verifyFormat(
191 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
192 " E = UnwrappedLines.end();\n"
193 " I != E; ++I) {\n}");
194
195 verifyFormat(
196 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
197 " ++IIIII) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000198}
199
200TEST_F(FormatTest, FormatsWhileLoop) {
201 verifyFormat("while (true) {\n}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000202 verifyFormat("while (true)\n"
203 " f();");
204 verifyFormat("while () {\n"
205 "}");
206 verifyFormat("while () {\n"
207 " f();\n"
208 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000209}
210
Alexander Kornienko15757312012-12-06 18:03:27 +0000211TEST_F(FormatTest, FormatsDoWhile) {
212 verifyFormat("do {\n"
213 " do_something();\n"
214 "} while (something());");
215 verifyFormat("do\n"
216 " do_something();\n"
217 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000218}
219
Alexander Kornienko15757312012-12-06 18:03:27 +0000220TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000221 verifyFormat("switch (x) {\n"
222 "case 1:\n"
223 " f();\n"
224 " break;\n"
225 "case kFoo:\n"
226 "case ns::kBar:\n"
227 "case kBaz:\n"
228 " break;\n"
229 "default:\n"
230 " g();\n"
231 " break;\n"
232 "}");
233 verifyFormat("switch (x) {\n"
234 "case 1: {\n"
235 " f();\n"
236 " break;\n"
237 "}\n"
238 "}");
239 verifyFormat("switch (test)\n"
240 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000241 verifyGoogleFormat("switch (x) {\n"
242 " case 1:\n"
243 " f();\n"
244 " break;\n"
245 " case kFoo:\n"
246 " case ns::kBar:\n"
247 " case kBaz:\n"
248 " break;\n"
249 " default:\n"
250 " g();\n"
251 " break;\n"
252 "}");
253 verifyGoogleFormat("switch (x) {\n"
254 " case 1: {\n"
255 " f();\n"
256 " break;\n"
257 " }\n"
258 "}");
259 verifyGoogleFormat("switch (test)\n"
260 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000261}
262
Alexander Kornienko15757312012-12-06 18:03:27 +0000263TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000264 verifyFormat("void f() {\n"
265 " some_code();\n"
266 "test_label:\n"
267 " some_other_code();\n"
268 " {\n"
269 " some_more_code();\n"
270 " another_label:\n"
271 " some_more_code();\n"
272 " }\n"
273 "}");
274 verifyFormat("some_code();\n"
275 "test_label:\n"
276 "some_other_code();");
277}
278
Alexander Kornienko15757312012-12-06 18:03:27 +0000279//===----------------------------------------------------------------------===//
280// Tests for comments.
281//===----------------------------------------------------------------------===//
282
283TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000284 verifyFormat("// line 1\n"
285 "// line 2\n"
286 "void f() {\n}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000287
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000288 verifyFormat("void f() {\n"
289 " // Doesn't do anything\n"
290 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000291
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000292 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000293 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000294
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000295 verifyFormat("enum E {\n"
296 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000297 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000298 " VAL_B\n"
299 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000300
301 verifyFormat(
302 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000303 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000304 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
305 " // Comment inside a statement.\n"
306 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000307
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000308 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000309 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000310
311 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000312}
313
314TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000315 verifyFormat("f(/*test=*/ true);");
316}
317
Alexander Kornienko15757312012-12-06 18:03:27 +0000318//===----------------------------------------------------------------------===//
319// Tests for classes, namespaces, etc.
320//===----------------------------------------------------------------------===//
321
322TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
323 verifyFormat("class A {\n};");
324}
325
326TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
327 verifyFormat("class A {\n"
328 "public:\n"
329 "protected:\n"
330 "private:\n"
331 " void f() {\n"
332 " }\n"
333 "};");
334 verifyGoogleFormat("class A {\n"
335 " public:\n"
336 " protected:\n"
337 " private:\n"
338 " void f() {\n"
339 " }\n"
340 "};");
341}
342
343TEST_F(FormatTest, FormatsDerivedClass) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000344 verifyFormat("class A : public B {\n"
345 "};");
Daniel Jasperc74e2792012-12-07 09:52:15 +0000346 verifyFormat("class A : public ::B {\n"
347 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000348}
349
Manuel Klimekde768542013-01-07 18:10:23 +0000350TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
351 verifyFormat("class A {\n"
352 "} a, b;");
353 verifyFormat("struct A {\n"
354 "} a, b;");
355}
356
Alexander Kornienko15757312012-12-06 18:03:27 +0000357TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000358 verifyFormat("enum {\n"
359 " Zero,\n"
360 " One = 1,\n"
361 " Two = One + 1,\n"
362 " Three = (One + Two),\n"
363 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
364 " Five = (One, Two, Three, Four, 5)\n"
365 "};");
366 verifyFormat("enum Enum {\n"
367 "};");
368 verifyFormat("enum {\n"
369 "};");
370}
371
Alexander Kornienko15757312012-12-06 18:03:27 +0000372TEST_F(FormatTest, FormatsNamespaces) {
373 verifyFormat("namespace some_namespace {\n"
374 "class A {\n"
375 "};\n"
376 "void f() {\n"
377 " f();\n"
378 "}\n"
379 "}");
380 verifyFormat("namespace {\n"
381 "class A {\n"
382 "};\n"
383 "void f() {\n"
384 " f();\n"
385 "}\n"
386 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000387 verifyFormat("inline namespace X {\n"
388 "class A {\n"
389 "};\n"
390 "void f() {\n"
391 " f();\n"
392 "}\n"
393 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000394 verifyFormat("using namespace some_namespace;\n"
395 "class A {\n"
396 "};\n"
397 "void f() {\n"
398 " f();\n"
399 "}");
400}
401
Nico Webera9ccdd12013-01-07 16:36:17 +0000402TEST_F(FormatTest, FormatTryCatch) {
403 verifyFormat("try {\n"
404 " throw a * b;\n"
405 "}\n"
406 "catch (int a) {\n"
407 " // Do nothing.\n"
408 "}\n"
409 "catch (...) {\n"
410 " exit(42);\n"
411 "}");
412
413 // Function-level try statements.
414 verifyFormat("int f() try {\n"
415 " return 4;\n"
416 "}\n"
417 "catch (...) {\n"
418 " return 5;\n"
419 "}");
420 verifyFormat("class A {\n"
421 " int a;\n"
422 " A() try : a(0) {\n"
423 " }\n"
424 " catch (...) {\n"
425 " throw;\n"
426 " }\n"
427 "};\n");
428}
429
430TEST_F(FormatTest, FormatObjCTryCatch) {
431 verifyFormat("@try {\n"
432 " f();\n"
433 "}\n"
434 "@catch (NSException e) {\n"
435 " @throw;\n"
436 "}\n"
437 "@finally {\n"
438 " exit(42);\n"
439 "}");
440}
441
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000442TEST_F(FormatTest, StaticInitializers) {
443 verifyFormat("static SomeClass SC = { 1, 'a' };");
444
445 // FIXME: Format like enums if the static initializer does not fit on a line.
446 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000447 "static SomeClass WithALoooooooooooooooooooongName = {\n"
448 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000449}
450
Manuel Klimeka080a182013-01-02 16:30:12 +0000451TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
452 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
453 " \\\n"
454 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
455}
456
Daniel Jasper71607512013-01-07 10:48:50 +0000457TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000458 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
459 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000460}
461
Manuel Klimeka080a182013-01-02 16:30:12 +0000462TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
463 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000464 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000465}
466
467TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
468 EXPECT_EQ("#line 42 \"test\"\n",
469 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000470 EXPECT_EQ("#define A \\\n B\n",
471 format("# \\\n define \\\n A \\\n B\n",
472 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000473}
474
475TEST_F(FormatTest, EndOfFileEndsPPDirective) {
476 EXPECT_EQ("#line 42 \"test\"",
477 format("# \\\n line \\\n 42 \\\n \"test\""));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000478 EXPECT_EQ("#define A \\\n B",
479 format("# \\\n define \\\n A \\\n B",
480 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000481}
482
Manuel Klimek060143e2013-01-02 18:33:23 +0000483TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000484 // If the macro fits in one line, we still do not get the full
485 // line, as only the next line decides whether we need an escaped newline and
486 // thus use the last column.
487 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000488
Manuel Klimekd544c572013-01-07 09:24:17 +0000489 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
490 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000491 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
492}
493
494TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000495 EXPECT_EQ("// some comment\n"
496 "#include \"a.h\"\n"
497 "#define A(A,\\\n"
498 " B)\n"
499 "#include \"b.h\"\n"
500 "// some comment\n",
501 format(" // some comment\n"
502 " #include \"a.h\"\n"
503 "#define A(A,\\\n"
504 " B)\n"
505 " #include \"b.h\"\n"
506 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000507}
508
Manuel Klimekd4397b92013-01-04 23:34:14 +0000509TEST_F(FormatTest, LayoutSingleHash) {
510 EXPECT_EQ("#\na;", format("#\na;"));
511}
512
513TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
514 EXPECT_EQ("#define A \\\n"
515 " c; \\\n"
516 " e;\n"
517 "f;", format("#define A c; e;\n"
518 "f;", getLLVMStyleWithColumns(14)));
519}
520
521TEST_F(FormatTest, LayoutRemainingTokens) {
522 EXPECT_EQ("{\n}", format("{}"));
523}
524
525TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000526 EXPECT_EQ("# define A\\\n b;",
527 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000528}
529
530TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000531 EXPECT_EQ("int x,\n"
532 "#define A\n"
533 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000534}
535
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000536TEST_F(FormatTest, HashInMacroDefinition) {
537 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
538 verifyFormat("#define A \\\n"
539 " { \\\n"
540 " f(#c);\\\n"
541 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000542
543 verifyFormat("#define A(X) \\\n"
544 " void function##X()", getLLVMStyleWithColumns(22));
545
546 verifyFormat("#define A(a, b, c) \\\n"
547 " void a##b##c()", getLLVMStyleWithColumns(22));
548
549 verifyFormat("#define A \\\n"
550 " void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000551}
552
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000553TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
554 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
555}
556
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000557TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
558 verifyFormat("{\n {\n a #c;\n }\n}");
559}
560
Manuel Klimeka5342db2013-01-06 20:07:31 +0000561TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
562 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
563 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
564 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
565 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
566}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000567
Manuel Klimek95419382013-01-07 07:56:50 +0000568TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000569 EXPECT_EQ(
570 "#define A \\\n int i; \\\n int j;",
571 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000572}
573
Manuel Klimekd544c572013-01-07 09:24:17 +0000574TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
575 verifyFormat("#define A \\\n"
576 " int v( \\\n"
577 " a); \\\n"
578 " int i;", getLLVMStyleWithColumns(11));
579}
580
Manuel Klimeka080a182013-01-02 16:30:12 +0000581TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000582 EXPECT_EQ(
583 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
584 " \\\n"
585 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
586 "\n"
587 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
588 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
589 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
590 "\\\n"
591 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
592 " \n"
593 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
594 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000595}
596
Manuel Klimek526ed112013-01-09 15:25:02 +0000597TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
598 EXPECT_EQ("int\n"
599 "#define A\n"
600 " a;",
601 format("int\n#define A\na;"));
602 verifyFormat(
603 "functionCallTo(someOtherFunction(\n"
604 " withSomeParameters, whichInSequence,\n"
605 " areLongerThanALine(andAnotherCall,\n"
606 "#define A \\\n"
607 " B\n"
608 " withMoreParamters,\n"
609 " whichStronglyInfluenceTheLayout),\n"
610 " andMoreParameters),\n"
611 " trailing);", getLLVMStyleWithColumns(69));
612}
613
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000614TEST_F(FormatTest, LayoutBlockInsideParens) {
615 EXPECT_EQ("functionCall({\n"
616 " int i;\n"
617 "});", format(" functionCall ( {int i;} );"));
618}
619
620TEST_F(FormatTest, LayoutBlockInsideStatement) {
621 EXPECT_EQ("SOME_MACRO {\n"
622 " int i;\n"
623 "}\n"
624 "int i;", format(" SOME_MACRO {int i;} int i;"));
625}
626
627TEST_F(FormatTest, LayoutNestedBlocks) {
628 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
629 " struct s {\n"
630 " int i;\n"
631 " };\n"
632 " s kBitsToOs[] = { { 10 } };\n"
633 " for (int i = 0; i < 10; ++i)\n"
634 " return;\n"
635 "}");
636}
637
Alexander Kornienko15757312012-12-06 18:03:27 +0000638//===----------------------------------------------------------------------===//
639// Line break tests.
640//===----------------------------------------------------------------------===//
641
642TEST_F(FormatTest, FormatsFunctionDefinition) {
643 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
644 " int h, int j, int f,\n"
645 " int c, int ddddddddddddd) {\n"
646 "}");
647}
648
649TEST_F(FormatTest, FormatsAwesomeMethodCall) {
650 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000651 "SomeLongMethodName(SomeReallyLongMethod(\n"
652 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
653 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000654}
655
Daniel Jasper1321eb52012-12-18 21:05:13 +0000656TEST_F(FormatTest, ConstructorInitializers) {
657 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}");
658
659 verifyFormat(
660 "SomeClass::Constructor()\n"
661 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
662 "}");
663
664 verifyFormat(
665 "SomeClass::Constructor()\n"
666 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
667 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
668 "}");
669
670 verifyFormat("Constructor()\n"
671 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
672 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
673 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
674 " aaaaaaaaaaaaaaaaaaaaaaa() {\n"
675 "}");
676
677 // Here a line could be saved by splitting the second initializer onto two
678 // lines, but that is not desireable.
679 verifyFormat("Constructor()\n"
680 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
681 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
682 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
683 "}");
684
685 verifyGoogleFormat("MyClass::MyClass(int var)\n"
686 " : some_var_(var), // 4 space indent\n"
687 " some_other_var_(var + 1) { // lined up\n"
688 "}");
689}
690
Alexander Kornienko15757312012-12-06 18:03:27 +0000691TEST_F(FormatTest, BreaksAsHighAsPossible) {
692 verifyFormat(
693 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
694 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
695 " f();");
696}
697
Daniel Jasperbac016b2012-12-03 18:12:45 +0000698TEST_F(FormatTest, BreaksDesireably) {
699 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
700 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimekde768542013-01-07 18:10:23 +0000701 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000702
703 verifyFormat(
704 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
706
707 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000710
711 verifyFormat(
712 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
714 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000716
717 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
718 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
719
Daniel Jasper723f0302013-01-02 14:40:02 +0000720 verifyFormat(
721 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
723
Daniel Jasper33182dd2012-12-05 14:57:28 +0000724 // This test case breaks on an incorrect memoization, i.e. an optimization not
725 // taking into account the StopAt value.
726 verifyFormat(
727 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000728 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
729 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
730 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000731
Daniel Jaspercd162382013-01-07 13:26:07 +0000732 verifyFormat("{\n {\n {\n"
733 " Annotation.SpaceRequiredBefore =\n"
734 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
735 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
736 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000737}
738
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000739TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
740 verifyFormat(
741 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
742 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000743 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
744 " ccccccccccccccccccccccccc) {\n}");
745 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
746 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000747 verifyFormat(
748 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
749 " ccccccccccccccccccccccccc) {\n}");
750}
751
Daniel Jasper9cda8002013-01-07 13:08:40 +0000752TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
753 verifyFormat(
754 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
755 " SI->getAlignment(),\n"
756 " SI->getPointerAddressSpaceee());\n");
757 verifyFormat(
758 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
759 " Line.Tokens.front().Tok.getLocation(),\n"
760 " Line.Tokens.back().Tok.getLocation());");
761}
762
Daniel Jaspercf225b62012-12-24 13:43:52 +0000763TEST_F(FormatTest, AlignsAfterAssignments) {
764 verifyFormat(
765 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000766 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000767 verifyFormat(
768 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000769 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000770 verifyFormat(
771 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000772 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000773 verifyFormat(
774 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000775 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000776 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000777 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
778 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
779 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000780}
781
782TEST_F(FormatTest, AlignsAfterReturn) {
783 verifyFormat(
784 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
785 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
786 verifyFormat(
787 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
788 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
789}
790
Daniel Jasper9c837d02013-01-09 07:06:56 +0000791TEST_F(FormatTest, BreaksConditionalExpressions) {
792 verifyFormat(
793 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
794 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
796 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
797 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
798}
799
Daniel Jasperbac016b2012-12-03 18:12:45 +0000800TEST_F(FormatTest, AlignsStringLiterals) {
801 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
802 " \"short literal\");");
803 verifyFormat(
804 "looooooooooooooooooooooooongFunction(\n"
805 " \"short literal\"\n"
806 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
807}
808
Alexander Kornienko15757312012-12-06 18:03:27 +0000809TEST_F(FormatTest, AlignsPipes) {
810 verifyFormat(
811 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
812 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
813 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
814 verifyFormat(
815 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
816 " << aaaaaaaaaaaaaaaaaaaa;");
817 verifyFormat(
818 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
819 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
820 verifyFormat(
821 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
822 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
823 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
824 verifyFormat(
825 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
826 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
827 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
828}
829
Daniel Jasperbac016b2012-12-03 18:12:45 +0000830TEST_F(FormatTest, UnderstandsEquals) {
831 verifyFormat(
832 "aaaaaaaaaaaaaaaaa =\n"
833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
834 verifyFormat(
835 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
837 "}");
838 verifyFormat(
839 "if (a) {\n"
840 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
842 "}");
843
Daniel Jasper9cda8002013-01-07 13:08:40 +0000844 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000845 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000846 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000847 " 10000000) {\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000848 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000849}
850
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000851TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000852 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
853 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000854
Daniel Jasper1321eb52012-12-18 21:05:13 +0000855 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
856 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000857
858 verifyFormat(
859 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
860 " Parameter2);");
861
862 verifyFormat(
863 "ShortObject->shortFunction(\n"
864 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
865 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
866
867 verifyFormat("loooooooooooooongFunction(\n"
868 " LoooooooooooooongObject->looooooooooooooooongFunction());");
869
870 verifyFormat(
871 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
872 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
873
Daniel Jasper46a46a22013-01-07 07:13:20 +0000874 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000875 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
876 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
877 "}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000878 verifyFormat(
879 "aaaaaaaaaaa->aaaaaaaaa(\n"
880 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
881 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000882}
883
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000884TEST_F(FormatTest, WrapsTemplateDeclarations) {
885 verifyFormat("template <typename T>\n"
886 "virtual void loooooooooooongFunction(int Param1, int Param2);");
887 verifyFormat(
888 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
889 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
890 verifyFormat(
891 "template <typename T>\n"
892 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
893 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000894 verifyFormat(
895 "template <typename T>\n"
896 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
897 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000899 verifyFormat("template <typename T>\n"
900 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
901 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000902 verifyFormat(
903 "template <typename T1, typename T2 = char, typename T3 = char,\n"
904 " typename T4 = char>\n"
905 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000906}
907
Daniel Jasperbac016b2012-12-03 18:12:45 +0000908TEST_F(FormatTest, UnderstandsTemplateParameters) {
909 verifyFormat("A<int> a;");
910 verifyFormat("A<A<A<int> > > a;");
911 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
912 verifyFormat("bool x = a < 1 || 2 > a;");
913 verifyFormat("bool x = 5 < f<int>();");
914 verifyFormat("bool x = f<int>() > 5;");
915 verifyFormat("bool x = 5 < a<int>::x;");
916 verifyFormat("bool x = a < 4 ? a > 2 : false;");
917 verifyFormat("bool x = f() ? a < 2 : a > 2;");
918
919 verifyGoogleFormat("A<A<int>> a;");
920 verifyGoogleFormat("A<A<A<int>>> a;");
921 verifyGoogleFormat("A<A<A<A<int>>>> a;");
922
923 verifyFormat("test >> a >> b;");
924 verifyFormat("test << a >> b;");
925
926 verifyFormat("f<int>();");
927 verifyFormat("template <typename T> void f() {\n}");
928}
929
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000930TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000931 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000932 verifyFormat("f(-1, -2, -3);");
933 verifyFormat("a[-1] = 5;");
934 verifyFormat("int a = 5 + -2;");
Daniel Jasper112fb272012-12-05 07:51:39 +0000935 verifyFormat("if (i == -1) {\n}");
936 verifyFormat("if (i != -1) {\n}");
937 verifyFormat("if (i > -1) {\n}");
938 verifyFormat("if (i < -1) {\n}");
Daniel Jasperd56a7372012-12-06 13:16:39 +0000939 verifyFormat("++(a->f());");
940 verifyFormat("--(a->f());");
941 verifyFormat("if (!(a->f())) {\n}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000942
943 verifyFormat("a-- > b;");
944 verifyFormat("b ? -a : c;");
945 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000946 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000947 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000948 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +0000949
950 verifyFormat("return -1;");
951 verifyFormat("switch (a) {\n"
952 "case -1:\n"
953 " break;\n"
954 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000955}
956
957TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +0000958 verifyFormat("bool operator<();");
959 verifyFormat("bool operator>();");
960 verifyFormat("bool operator=();");
961 verifyFormat("bool operator==();");
962 verifyFormat("bool operator!=();");
963 verifyFormat("int operator+();");
964 verifyFormat("int operator++();");
965 verifyFormat("bool operator();");
966 verifyFormat("bool operator()();");
967 verifyFormat("bool operator[]();");
968 verifyFormat("operator bool();");
969 verifyFormat("operator SomeType<int>();");
970 verifyFormat("void *operator new(std::size_t size);");
971 verifyFormat("void *operator new[](std::size_t size);");
972 verifyFormat("void operator delete(void *ptr);");
973 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000974}
975
Daniel Jasper5d334402013-01-02 08:57:10 +0000976TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000977 verifyFormat("int *f(int *a) {\n}");
978 verifyFormat("f(a, *a);");
979 verifyFormat("f(*a);");
980 verifyFormat("int a = b * 10;");
981 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +0000982 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000983 verifyFormat("int a += b * c;");
984 verifyFormat("int a -= b * c;");
985 verifyFormat("int a *= b * c;");
986 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000987 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +0000988 verifyFormat("int a = *b * c;");
989 verifyFormat("int a = b * *c;");
Daniel Jasperc74e2792012-12-07 09:52:15 +0000990 verifyFormat("int main(int argc, char **argv) {\n}");
Nico Weber00d5a042012-12-23 01:07:46 +0000991 verifyFormat("return 10 * b;");
992 verifyFormat("return *b * *c;");
993 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +0000994 verifyFormat("f(b ? *c : *d);");
995 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000996 verifyFormat("*b = a;");
997 verifyFormat("a * ~b;");
998 verifyFormat("a * !b;");
999 verifyFormat("a * +b;");
1000 verifyFormat("a * -b;");
1001 verifyFormat("a * ++b;");
1002 verifyFormat("a * --b;");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001003 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001004
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001005 verifyFormat("InvalidRegions[*R] = 0;");
1006
Daniel Jasper8b39c662012-12-10 18:59:13 +00001007 verifyFormat("A<int *> a;");
1008 verifyFormat("A<int **> a;");
1009 verifyFormat("A<int *, int *> a;");
1010 verifyFormat("A<int **, int **> a;");
Daniel Jasperef5b9c32013-01-02 15:46:59 +00001011 verifyFormat("Type *A = static_cast<Type *>(P);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001012 verifyFormat("Type *A = (Type *)P;");
1013 verifyFormat("Type *A = (vector<Type *, int *>)P;");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001014
Daniel Jasper2db356d2013-01-08 20:03:18 +00001015 verifyFormat(
1016 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1018
Daniel Jasperc74e2792012-12-07 09:52:15 +00001019 verifyGoogleFormat("int main(int argc, char** argv) {\n}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001020 verifyGoogleFormat("A<int*> a;");
1021 verifyGoogleFormat("A<int**> a;");
1022 verifyGoogleFormat("A<int*, int*> a;");
1023 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001024 verifyGoogleFormat("f(b ? *c : *d);");
1025 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001026}
1027
Daniel Jasper46ef8522013-01-10 13:08:12 +00001028TEST_F(FormatTest, FormatsFunctionTypes) {
1029 // FIXME: Determine the cases that need a space after the return type and fix.
1030 verifyFormat("A<bool()> a;");
1031 verifyFormat("A<SomeType()> a;");
1032 verifyFormat("A<void(*)(int, std::string)> a;");
1033
1034 verifyFormat("int(*func)(void *);");
1035}
1036
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001037TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001038 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
1039 " int LoooooooooooooooongParam2) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001040 verifyFormat(
1041 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1042 " SourceLocation L, IdentifierIn *II,\n"
1043 " Type *T) {\n}");
1044}
1045
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001046TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1047 verifyFormat("(a)->b();");
1048 verifyFormat("--a;");
1049}
1050
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001051TEST_F(FormatTest, HandlesIncludeDirectives) {
1052 EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001053 EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n"));
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001054 EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
1055 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
1056 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
Nico Weberb23ae0c2012-12-21 18:21:56 +00001057
1058 EXPECT_EQ("#import <string>\n", format("#import <string>\n"));
1059 EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n"));
1060 EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n"));
1061 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
1062 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001063}
1064
Alexander Kornienko15757312012-12-06 18:03:27 +00001065//===----------------------------------------------------------------------===//
1066// Error recovery tests.
1067//===----------------------------------------------------------------------===//
1068
Daniel Jasper700e7102013-01-10 09:26:47 +00001069TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1070 verifyFormat("void f() { return } 42");
1071}
1072
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001073TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1074 verifyFormat("int aaaaaaaa =\n"
1075 " // Overly long comment\n"
1076 " b;", getLLVMStyleWithColumns(20));
1077 verifyFormat("function(\n"
1078 " ShortArgument,\n"
1079 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1080}
1081
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001082TEST_F(FormatTest, IncorrectAccessSpecifier) {
1083 verifyFormat("public:");
1084 verifyFormat("class A {\n"
1085 "public\n"
1086 " void f() {\n"
1087 " }\n"
1088 "};");
1089 verifyFormat("public\n"
1090 "int qwerty;");
1091 verifyFormat("public\n"
1092 "B {\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001093 "}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001094 verifyFormat("public\n"
1095 "{\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001096 "}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001097 verifyFormat("public\n"
1098 "B {\n"
1099 " int x;\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001100 "}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001101}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001102
Alexander Kornienko393b0082012-12-04 15:40:36 +00001103TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1104 verifyFormat("{");
1105}
1106
1107TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
1108 verifyFormat("do {\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001109 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001110 verifyFormat("do {\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001111 "}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001112 "f();");
1113 verifyFormat("do {\n"
1114 "}\n"
1115 "wheeee(fun);");
1116 verifyFormat("do {\n"
1117 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001118 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001119}
1120
Daniel Jasper1f42f112013-01-04 18:52:56 +00001121TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1122 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001123 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001124}
1125
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001126TEST_F(FormatTest, IncorrectCodeErrorDetection) {
1127 EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n"));
1128 EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n"));
1129 EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n"));
Alexander Kornienkoa3a2b3a2012-12-06 17:49:17 +00001130 EXPECT_EQ("{\n {\n }\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001131
1132 FormatStyle Style = getLLVMStyle();
1133 Style.ColumnLimit = 10;
1134 EXPECT_EQ("{\n"
1135 " {\n"
1136 " breakme(\n"
1137 " qwe);\n"
1138 "}\n", format("{\n"
1139 " {\n"
1140 " breakme(qwe);\n"
1141 "}\n", Style));
1142
1143}
1144
Nico Webercf4a79c2013-01-08 17:56:31 +00001145//===----------------------------------------------------------------------===//
1146// Objective-C tests.
1147//===----------------------------------------------------------------------===//
1148
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001149TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1150 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1151 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1152 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001153 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001154 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1155 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1156 format("-(NSInteger)Method3:(id)anObject;"));
1157 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1158 format("-(NSInteger)Method4:(id)anObject;"));
1159 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1160 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1161 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1162 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001163 EXPECT_EQ(
1164 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1165 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001166
1167 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001168 EXPECT_EQ(
1169 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1170 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1171 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1172 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1173 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1174 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1175 format(
1176 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1177 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1178 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1179 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1180 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1181 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001182}
1183
Daniel Jasper886568d2013-01-09 08:36:49 +00001184TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001185 verifyFormat("int (^Block)(int, int);");
1186 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001187}
1188
Nico Weber27d13672013-01-09 20:25:35 +00001189TEST_F(FormatTest, FormatObjCInterface) {
1190 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
1191 // FIXME: In google style, it's "+(id) init", not "+ (id)init".
1192 verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1193 "@public\n"
1194 " int field1;\n"
1195 "@protected\n"
1196 " int field2;\n"
1197 "@private\n"
1198 " int field3;\n"
1199 "@package\n"
1200 " int field4;\n"
1201 "}\n"
1202 "+ (id)init;\n"
1203 "@end");
1204
1205 // FIXME: In LLVM style, there should be a space before '<' for protocols.
1206 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1207 " @public\n"
1208 " int field1;\n"
1209 " @protected\n"
1210 " int field2;\n"
1211 " @private\n"
1212 " int field3;\n"
1213 " @package\n"
1214 " int field4;\n"
1215 "}\n"
1216 "+ (id)init;\n"
1217 "@end");
1218
1219 verifyFormat("@interface Foo\n"
1220 "+ (id)init;\n"
1221 "// Look, a comment!\n"
1222 "- (int)answerWith:(int)i;\n"
1223 "@end");
1224
1225 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001226 "@end\n"
1227 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001228 "@end");
1229
1230 verifyFormat("@interface Foo : Bar\n"
1231 "+ (id)init;\n"
1232 "@end");
1233
1234 verifyFormat("@interface Foo : Bar<Baz, Quux>\n"
1235 "+ (id)init;\n"
1236 "@end");
1237
1238 // FIXME: there should be a space before '(' for categories.
1239 verifyFormat("@interface Foo(HackStuff)\n"
1240 "+ (id)init;\n"
1241 "@end");
1242
1243 verifyFormat("@interface Foo()\n"
1244 "+ (id)init;\n"
1245 "@end");
1246
1247 verifyFormat("@interface Foo(HackStuff)<MyProtocol>\n"
1248 "+ (id)init;\n"
1249 "@end");
1250
1251 verifyFormat("@interface Foo {\n"
1252 " int _i;\n"
1253 "}\n"
1254 "+ (id)init;\n"
1255 "@end");
1256
1257 verifyFormat("@interface Foo : Bar {\n"
1258 " int _i;\n"
1259 "}\n"
1260 "+ (id)init;\n"
1261 "@end");
1262
1263 verifyFormat("@interface Foo : Bar<Baz, Quux> {\n"
1264 " int _i;\n"
1265 "}\n"
1266 "+ (id)init;\n"
1267 "@end");
1268
1269 verifyFormat("@interface Foo(HackStuff) {\n"
1270 " int _i;\n"
1271 "}\n"
1272 "+ (id)init;\n"
1273 "@end");
1274
1275 verifyFormat("@interface Foo() {\n"
1276 " int _i;\n"
1277 "}\n"
1278 "+ (id)init;\n"
1279 "@end");
1280
1281 verifyFormat("@interface Foo(HackStuff)<MyProtocol> {\n"
1282 " int _i;\n"
1283 "}\n"
1284 "+ (id)init;\n"
1285 "@end");
1286}
1287
Nico Weber50767d82013-01-09 23:25:37 +00001288TEST_F(FormatTest, FormatObjCImplementation) {
1289 verifyFormat("@implementation Foo : NSObject {\n"
1290 "@public\n"
1291 " int field1;\n"
1292 "@protected\n"
1293 " int field2;\n"
1294 "@private\n"
1295 " int field3;\n"
1296 "@package\n"
1297 " int field4;\n"
1298 "}\n"
1299 "+ (id)init {\n"
1300 "}\n"
1301 "@end");
1302
1303 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1304 " @public\n"
1305 " int field1;\n"
1306 " @protected\n"
1307 " int field2;\n"
1308 " @private\n"
1309 " int field3;\n"
1310 " @package\n"
1311 " int field4;\n"
1312 "}\n"
1313 "+ (id)init {\n"
1314 "}\n"
1315 "@end");
1316
1317 verifyFormat("@implementation Foo\n"
1318 "+ (id)init {\n"
1319 " if (true)\n"
1320 " return nil;\n"
1321 "}\n"
1322 "// Look, a comment!\n"
1323 "- (int)answerWith:(int)i {\n"
1324 " return i;\n"
1325 "}\n"
1326 "@end");
1327
1328 verifyFormat("@implementation Foo\n"
1329 "@end\n"
1330 "@implementation Bar\n"
1331 "@end");
1332
1333 verifyFormat("@implementation Foo : Bar\n"
1334 "+ (id)init {\n"
1335 "}\n"
1336 "@end");
1337
1338 verifyFormat("@implementation Foo {\n"
1339 " int _i;\n"
1340 "}\n"
1341 "+ (id)init {\n"
1342 "}\n"
1343 "@end");
1344
1345 verifyFormat("@implementation Foo : Bar {\n"
1346 " int _i;\n"
1347 "}\n"
1348 "+ (id)init {\n"
1349 "}\n"
1350 "@end");
1351
1352 // FIXME: there should be a space before '(' for categories.
1353 verifyFormat("@implementation Foo(HackStuff)\n"
1354 "+ (id)init {\n"
1355 "}\n"
1356 "@end");
1357}
1358
Nico Weber1abe6ea2013-01-09 21:15:03 +00001359TEST_F(FormatTest, FormatObjCProtocol) {
1360 verifyFormat("@protocol Foo\n"
1361 "@property(weak) id delegate;\n"
1362 "- (NSUInteger)numberOfThings;\n"
1363 "@end");
1364
1365 // FIXME: In LLVM style, there should be a space before '<' for protocols.
1366 verifyFormat("@protocol MyProtocol<NSObject>\n"
1367 "- (NSUInteger)numberOfThings;\n"
1368 "@end");
1369
1370 verifyFormat("@protocol Foo;\n"
1371 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001372
1373 verifyFormat("@protocol Foo\n"
1374 "@end\n"
1375 "@protocol Bar\n"
1376 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001377
1378 verifyFormat("@protocol myProtocol\n"
1379 "- (void)mandatoryWithInt:(int)i;\n"
1380 "@optional\n"
1381 "- (void)optional;\n"
1382 "@required\n"
1383 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001384 "@optional\n"
1385 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001386 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001387}
1388
Nico Weber581f5572013-01-07 15:56:25 +00001389TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001390 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001391 verifyFormat("@catch");
1392 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001393 verifyFormat("@compatibility_alias");
1394 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001395 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001396 verifyFormat("@encode");
1397 verifyFormat("@end");
1398 verifyFormat("@finally");
1399 verifyFormat("@implementation");
1400 verifyFormat("@import");
1401 verifyFormat("@interface");
1402 verifyFormat("@optional");
1403 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001404 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001405 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001406 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001407 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001408 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001409 verifyFormat("@required");
1410 verifyFormat("@selector");
1411 verifyFormat("@synchronized");
1412 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001413 verifyFormat("@throw");
1414 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001415
Nico Webercb4d6902013-01-08 19:40:21 +00001416 // FIXME: Make the uncommented lines below pass.
1417 verifyFormat("@\"String\"");
1418 verifyFormat("@1");
1419 //verifyFormat("@+4.8");
1420 //verifyFormat("@-4");
1421 verifyFormat("@1LL");
1422 verifyFormat("@.5");
1423 verifyFormat("@'c'");
1424 verifyFormat("@true");
1425 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
1426 verifyFormat("@[");
1427 verifyFormat("@{");
1428
1429
Nico Weber581f5572013-01-07 15:56:25 +00001430 EXPECT_EQ("@interface", format("@ interface"));
1431
1432 // The precise formatting of this doesn't matter, nobody writes code like
1433 // this.
1434 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001435}
1436
Nico Weberc31689a2013-01-08 19:15:23 +00001437TEST_F(FormatTest, ObjCSnippets) {
1438 // FIXME: Make the uncommented lines below pass.
1439 verifyFormat("@autoreleasepool {\n"
1440 " foo();\n"
1441 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001442 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001443 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001444 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001445 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001446 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001447 //verifyFormat("SEL s = @selector(foo:);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001448 verifyFormat("@synchronized(self) {\n"
1449 " f();\n"
1450 "}");
Nico Weberc31689a2013-01-08 19:15:23 +00001451 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001452
1453 // FIXME: "getter=bar" should not be surround by spaces in @property.
1454 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
1455}
1456
Daniel Jaspercd162382013-01-07 13:26:07 +00001457} // end namespace tooling
1458} // end namespace clang