blob: d5f74d3daa5d919ec995c7d090cd255dd78b5d8b [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)));
29 LangOptions LangOpts;
30 LangOpts.CPlusPlus = 1;
Daniel Jasperba3d3072013-01-02 17:21:36 +000031 LangOpts.CPlusPlus11 = 1;
Nico Weberefcfe732013-01-07 15:17:23 +000032 LangOpts.ObjC1 = 1;
Nico Weber6092d4e2013-01-07 19:05:19 +000033 LangOpts.ObjC2 = 1;
Daniel Jasperbac016b2012-12-03 18:12:45 +000034 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts);
Daniel Jaspercd162382013-01-07 13:26:07 +000035 tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
36 Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +000037 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
38 return Context.getRewrittenText(ID);
39 }
40
41 std::string format(llvm::StringRef Code,
42 const FormatStyle &Style = getLLVMStyle()) {
43 return format(Code, 0, Code.size(), Style);
44 }
45
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000046 std::string messUp(llvm::StringRef Code) {
47 std::string MessedUp(Code.str());
48 bool InComment = false;
49 bool JustReplacedNewline = false;
50 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
51 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
52 if (JustReplacedNewline)
53 MessedUp[i - 1] = '\n';
54 InComment = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +000055 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
56 MessedUp[i] = ' ';
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000057 } else if (MessedUp[i] == '\n') {
58 if (InComment) {
59 InComment = false;
60 } else {
61 JustReplacedNewline = true;
62 MessedUp[i] = ' ';
63 }
Manuel Klimekd4397b92013-01-04 23:34:14 +000064 } else if (MessedUp[i] != ' ') {
65 JustReplacedNewline = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000066 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000067 }
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000068 return MessedUp;
69 }
70
Manuel Klimek060143e2013-01-02 18:33:23 +000071 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
72 FormatStyle Style = getLLVMStyle();
73 Style.ColumnLimit = ColumnLimit;
74 return Style;
75 }
76
77 void verifyFormat(llvm::StringRef Code,
78 const FormatStyle &Style = getLLVMStyle()) {
79 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000080 }
81
82 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +000083 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +000084 }
85};
86
Alexander Kornienko15757312012-12-06 18:03:27 +000087//===----------------------------------------------------------------------===//
88// Basic function tests.
89//===----------------------------------------------------------------------===//
90
Daniel Jasperbac016b2012-12-03 18:12:45 +000091TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
92 EXPECT_EQ(";", format(";"));
93}
94
95TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
96 EXPECT_EQ("int i;", format(" int i;"));
97 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
98 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
99 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
100}
101
102TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
103 EXPECT_EQ("int i;", format("int\ni;"));
104}
105
106TEST_F(FormatTest, FormatsNestedBlockStatements) {
107 EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}"));
108}
109
Alexander Kornienko15757312012-12-06 18:03:27 +0000110TEST_F(FormatTest, FormatsNestedCall) {
111 verifyFormat("Method(f1, f2(f3));");
112 verifyFormat("Method(f1(f2, f3()));");
113}
114
Alexander Kornienko15757312012-12-06 18:03:27 +0000115//===----------------------------------------------------------------------===//
116// Tests for control statements.
117//===----------------------------------------------------------------------===//
118
119TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
120 verifyFormat("if (true)\n f();\ng();");
121 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
122 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000123 verifyFormat("if (a)\n"
124 " // comment\n"
125 " f();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000126}
127
128TEST_F(FormatTest, ParseIfElse) {
129 verifyFormat("if (true)\n"
130 " if (true)\n"
131 " if (true)\n"
132 " f();\n"
133 " else\n"
134 " g();\n"
135 " else\n"
136 " h();\n"
137 "else\n"
138 " i();");
139 verifyFormat("if (true)\n"
140 " if (true)\n"
141 " if (true) {\n"
142 " if (true)\n"
143 " f();\n"
144 " } else {\n"
145 " g();\n"
146 " }\n"
147 " else\n"
148 " h();\n"
149 "else {\n"
150 " i();\n"
151 "}");
152}
153
154TEST_F(FormatTest, ElseIf) {
155 verifyFormat("if (a) {\n"
156 "} else if (b) {\n"
157 "}");
158 verifyFormat("if (a)\n"
159 " f();\n"
160 "else if (b)\n"
161 " g();\n"
162 "else\n"
163 " h();");
164}
165
Daniel Jasperbac016b2012-12-03 18:12:45 +0000166TEST_F(FormatTest, FormatsForLoop) {
167 verifyFormat(
168 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000169 " ++VeryVeryLongLoopVariable)\n"
170 " ;");
171 verifyFormat("for (;;)\n"
172 " f();");
173 verifyFormat("for (;;) {\n"
174 "}");
175 verifyFormat("for (;;) {\n"
176 " f();\n"
177 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000178
179 verifyFormat(
180 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
181 " E = UnwrappedLines.end();\n"
182 " I != E; ++I) {\n}");
183
184 verifyFormat(
185 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
186 " ++IIIII) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000187}
188
189TEST_F(FormatTest, FormatsWhileLoop) {
190 verifyFormat("while (true) {\n}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000191 verifyFormat("while (true)\n"
192 " f();");
193 verifyFormat("while () {\n"
194 "}");
195 verifyFormat("while () {\n"
196 " f();\n"
197 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000198}
199
Alexander Kornienko15757312012-12-06 18:03:27 +0000200TEST_F(FormatTest, FormatsDoWhile) {
201 verifyFormat("do {\n"
202 " do_something();\n"
203 "} while (something());");
204 verifyFormat("do\n"
205 " do_something();\n"
206 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000207}
208
Alexander Kornienko15757312012-12-06 18:03:27 +0000209TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000210 verifyFormat("switch (x) {\n"
211 "case 1:\n"
212 " f();\n"
213 " break;\n"
214 "case kFoo:\n"
215 "case ns::kBar:\n"
216 "case kBaz:\n"
217 " break;\n"
218 "default:\n"
219 " g();\n"
220 " break;\n"
221 "}");
222 verifyFormat("switch (x) {\n"
223 "case 1: {\n"
224 " f();\n"
225 " break;\n"
226 "}\n"
227 "}");
228 verifyFormat("switch (test)\n"
229 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000230 verifyGoogleFormat("switch (x) {\n"
231 " case 1:\n"
232 " f();\n"
233 " break;\n"
234 " case kFoo:\n"
235 " case ns::kBar:\n"
236 " case kBaz:\n"
237 " break;\n"
238 " default:\n"
239 " g();\n"
240 " break;\n"
241 "}");
242 verifyGoogleFormat("switch (x) {\n"
243 " case 1: {\n"
244 " f();\n"
245 " break;\n"
246 " }\n"
247 "}");
248 verifyGoogleFormat("switch (test)\n"
249 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000250}
251
Alexander Kornienko15757312012-12-06 18:03:27 +0000252TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000253 verifyFormat("void f() {\n"
254 " some_code();\n"
255 "test_label:\n"
256 " some_other_code();\n"
257 " {\n"
258 " some_more_code();\n"
259 " another_label:\n"
260 " some_more_code();\n"
261 " }\n"
262 "}");
263 verifyFormat("some_code();\n"
264 "test_label:\n"
265 "some_other_code();");
266}
267
Alexander Kornienko15757312012-12-06 18:03:27 +0000268//===----------------------------------------------------------------------===//
269// Tests for comments.
270//===----------------------------------------------------------------------===//
271
272TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000273 verifyFormat("// line 1\n"
274 "// line 2\n"
275 "void f() {\n}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000276
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000277 verifyFormat("void f() {\n"
278 " // Doesn't do anything\n"
279 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000280
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000281 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000282 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000283
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000284 verifyFormat("enum E {\n"
285 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000286 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000287 " VAL_B\n"
288 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000289
290 verifyFormat(
291 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000292 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000293
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000294 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000295 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000296
297 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000298}
299
300TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000301 verifyFormat("f(/*test=*/ true);");
302}
303
Alexander Kornienko15757312012-12-06 18:03:27 +0000304//===----------------------------------------------------------------------===//
305// Tests for classes, namespaces, etc.
306//===----------------------------------------------------------------------===//
307
308TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
309 verifyFormat("class A {\n};");
310}
311
312TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
313 verifyFormat("class A {\n"
314 "public:\n"
315 "protected:\n"
316 "private:\n"
317 " void f() {\n"
318 " }\n"
319 "};");
320 verifyGoogleFormat("class A {\n"
321 " public:\n"
322 " protected:\n"
323 " private:\n"
324 " void f() {\n"
325 " }\n"
326 "};");
327}
328
329TEST_F(FormatTest, FormatsDerivedClass) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000330 verifyFormat("class A : public B {\n"
331 "};");
Daniel Jasperc74e2792012-12-07 09:52:15 +0000332 verifyFormat("class A : public ::B {\n"
333 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000334}
335
Manuel Klimekde768542013-01-07 18:10:23 +0000336TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
337 verifyFormat("class A {\n"
338 "} a, b;");
339 verifyFormat("struct A {\n"
340 "} a, b;");
341}
342
Alexander Kornienko15757312012-12-06 18:03:27 +0000343TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000344 verifyFormat("enum {\n"
345 " Zero,\n"
346 " One = 1,\n"
347 " Two = One + 1,\n"
348 " Three = (One + Two),\n"
349 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
350 " Five = (One, Two, Three, Four, 5)\n"
351 "};");
352 verifyFormat("enum Enum {\n"
353 "};");
354 verifyFormat("enum {\n"
355 "};");
356}
357
Alexander Kornienko15757312012-12-06 18:03:27 +0000358TEST_F(FormatTest, FormatsNamespaces) {
359 verifyFormat("namespace some_namespace {\n"
360 "class A {\n"
361 "};\n"
362 "void f() {\n"
363 " f();\n"
364 "}\n"
365 "}");
366 verifyFormat("namespace {\n"
367 "class A {\n"
368 "};\n"
369 "void f() {\n"
370 " f();\n"
371 "}\n"
372 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000373 verifyFormat("inline namespace X {\n"
374 "class A {\n"
375 "};\n"
376 "void f() {\n"
377 " f();\n"
378 "}\n"
379 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000380 verifyFormat("using namespace some_namespace;\n"
381 "class A {\n"
382 "};\n"
383 "void f() {\n"
384 " f();\n"
385 "}");
386}
387
Nico Webera9ccdd12013-01-07 16:36:17 +0000388TEST_F(FormatTest, FormatTryCatch) {
389 verifyFormat("try {\n"
390 " throw a * b;\n"
391 "}\n"
392 "catch (int a) {\n"
393 " // Do nothing.\n"
394 "}\n"
395 "catch (...) {\n"
396 " exit(42);\n"
397 "}");
398
399 // Function-level try statements.
400 verifyFormat("int f() try {\n"
401 " return 4;\n"
402 "}\n"
403 "catch (...) {\n"
404 " return 5;\n"
405 "}");
406 verifyFormat("class A {\n"
407 " int a;\n"
408 " A() try : a(0) {\n"
409 " }\n"
410 " catch (...) {\n"
411 " throw;\n"
412 " }\n"
413 "};\n");
414}
415
416TEST_F(FormatTest, FormatObjCTryCatch) {
417 verifyFormat("@try {\n"
418 " f();\n"
419 "}\n"
420 "@catch (NSException e) {\n"
421 " @throw;\n"
422 "}\n"
423 "@finally {\n"
424 " exit(42);\n"
425 "}");
426}
427
Nico Weber6092d4e2013-01-07 19:05:19 +0000428TEST_F(FormatTest, FormatObjCInterface) {
429 verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
430 "@public\n"
431 " int field1;\n"
432 "@protected\n"
433 " int field2;\n"
434 "@private\n"
435 " int field3;\n"
436 "@package\n"
437 " int field4;\n"
438 "}\n"
439 "+ (id)init;\n"
440 "@end");
441
442 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
443 " @public\n"
444 " int field1;\n"
445 " @protected\n"
446 " int field2;\n"
447 " @private\n"
448 " int field3;\n"
449 " @package\n"
450 " int field4;\n"
451 "}\n"
452 "+ (id)init;\n"
453 "@end");
454}
455
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000456TEST_F(FormatTest, StaticInitializers) {
457 verifyFormat("static SomeClass SC = { 1, 'a' };");
458
459 // FIXME: Format like enums if the static initializer does not fit on a line.
460 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000461 "static SomeClass WithALoooooooooooooooooooongName = {\n"
462 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000463}
464
Manuel Klimeka080a182013-01-02 16:30:12 +0000465TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
466 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
467 " \\\n"
468 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
469}
470
Daniel Jasper71607512013-01-07 10:48:50 +0000471TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000472 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
473 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000474}
475
Manuel Klimeka080a182013-01-02 16:30:12 +0000476TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
477 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000478 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000479}
480
481TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
482 EXPECT_EQ("#line 42 \"test\"\n",
483 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000484 EXPECT_EQ("#define A \\\n B\n",
485 format("# \\\n define \\\n A \\\n B\n",
486 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000487}
488
489TEST_F(FormatTest, EndOfFileEndsPPDirective) {
490 EXPECT_EQ("#line 42 \"test\"",
491 format("# \\\n line \\\n 42 \\\n \"test\""));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000492 EXPECT_EQ("#define A \\\n B",
493 format("# \\\n define \\\n A \\\n B",
494 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000495}
496
Manuel Klimek060143e2013-01-02 18:33:23 +0000497TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000498 // If the macro fits in one line, we still do not get the full
499 // line, as only the next line decides whether we need an escaped newline and
500 // thus use the last column.
501 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000502
Manuel Klimekd544c572013-01-07 09:24:17 +0000503 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
504 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000505 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
506}
507
508TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000509 EXPECT_EQ("// some comment\n"
510 "#include \"a.h\"\n"
511 "#define A(A,\\\n"
512 " B)\n"
513 "#include \"b.h\"\n"
514 "// some comment\n",
515 format(" // some comment\n"
516 " #include \"a.h\"\n"
517 "#define A(A,\\\n"
518 " B)\n"
519 " #include \"b.h\"\n"
520 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000521}
522
Manuel Klimekd4397b92013-01-04 23:34:14 +0000523TEST_F(FormatTest, LayoutSingleHash) {
524 EXPECT_EQ("#\na;", format("#\na;"));
525}
526
527TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
528 EXPECT_EQ("#define A \\\n"
529 " c; \\\n"
530 " e;\n"
531 "f;", format("#define A c; e;\n"
532 "f;", getLLVMStyleWithColumns(14)));
533}
534
535TEST_F(FormatTest, LayoutRemainingTokens) {
536 EXPECT_EQ("{\n}", format("{}"));
537}
538
539TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000540 EXPECT_EQ("# define A\\\n b;",
541 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000542}
543
544TEST_F(FormatTest, MacroDefinitionInsideStatement) {
545 EXPECT_EQ("int x,\n#define A\ny;", format("int x,\n#define A\ny;"));
546}
547
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000548TEST_F(FormatTest, HashInMacroDefinition) {
549 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
550 verifyFormat("#define A \\\n"
551 " { \\\n"
552 " f(#c);\\\n"
553 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000554
555 verifyFormat("#define A(X) \\\n"
556 " void function##X()", getLLVMStyleWithColumns(22));
557
558 verifyFormat("#define A(a, b, c) \\\n"
559 " void a##b##c()", getLLVMStyleWithColumns(22));
560
561 verifyFormat("#define A \\\n"
562 " void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000563}
564
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000565TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
566 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
567}
568
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000569TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
570 verifyFormat("{\n {\n a #c;\n }\n}");
571}
572
Manuel Klimeka5342db2013-01-06 20:07:31 +0000573TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
574 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
575 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
576 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
577 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
578}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000579
Manuel Klimek95419382013-01-07 07:56:50 +0000580TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000581 EXPECT_EQ(
582 "#define A \\\n int i; \\\n int j;",
583 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000584}
585
Manuel Klimekd544c572013-01-07 09:24:17 +0000586TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
587 verifyFormat("#define A \\\n"
588 " int v( \\\n"
589 " a); \\\n"
590 " int i;", getLLVMStyleWithColumns(11));
591}
592
Manuel Klimeka080a182013-01-02 16:30:12 +0000593TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000594 EXPECT_EQ(
595 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
596 " \\\n"
597 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
598 "\n"
599 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
600 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
601 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
602 "\\\n"
603 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
604 " \n"
605 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
606 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000607}
608
Alexander Kornienko15757312012-12-06 18:03:27 +0000609//===----------------------------------------------------------------------===//
610// Line break tests.
611//===----------------------------------------------------------------------===//
612
613TEST_F(FormatTest, FormatsFunctionDefinition) {
614 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
615 " int h, int j, int f,\n"
616 " int c, int ddddddddddddd) {\n"
617 "}");
618}
619
620TEST_F(FormatTest, FormatsAwesomeMethodCall) {
621 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000622 "SomeLongMethodName(SomeReallyLongMethod(\n"
623 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
624 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000625}
626
Daniel Jasper1321eb52012-12-18 21:05:13 +0000627TEST_F(FormatTest, ConstructorInitializers) {
628 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}");
629
630 verifyFormat(
631 "SomeClass::Constructor()\n"
632 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
633 "}");
634
635 verifyFormat(
636 "SomeClass::Constructor()\n"
637 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
638 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
639 "}");
640
641 verifyFormat("Constructor()\n"
642 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
643 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
644 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
645 " aaaaaaaaaaaaaaaaaaaaaaa() {\n"
646 "}");
647
648 // Here a line could be saved by splitting the second initializer onto two
649 // lines, but that is not desireable.
650 verifyFormat("Constructor()\n"
651 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
652 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
653 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
654 "}");
655
656 verifyGoogleFormat("MyClass::MyClass(int var)\n"
657 " : some_var_(var), // 4 space indent\n"
658 " some_other_var_(var + 1) { // lined up\n"
659 "}");
660}
661
Alexander Kornienko15757312012-12-06 18:03:27 +0000662TEST_F(FormatTest, BreaksAsHighAsPossible) {
663 verifyFormat(
664 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
665 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
666 " f();");
667}
668
Daniel Jasperbac016b2012-12-03 18:12:45 +0000669TEST_F(FormatTest, BreaksDesireably) {
670 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
671 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimekde768542013-01-07 18:10:23 +0000672 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000673
674 verifyFormat(
675 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
677
678 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000681
682 verifyFormat(
683 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
684 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
685 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
686 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000687
688 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
689 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
690
Daniel Jasper723f0302013-01-02 14:40:02 +0000691 verifyFormat(
692 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
693 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
694
Daniel Jasper33182dd2012-12-05 14:57:28 +0000695 // This test case breaks on an incorrect memoization, i.e. an optimization not
696 // taking into account the StopAt value.
697 verifyFormat(
698 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000699 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
700 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
701 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000702
Daniel Jaspercd162382013-01-07 13:26:07 +0000703 verifyFormat("{\n {\n {\n"
704 " Annotation.SpaceRequiredBefore =\n"
705 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
706 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
707 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000708}
709
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000710TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
711 verifyFormat(
712 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
713 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000714 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
715 " ccccccccccccccccccccccccc) {\n}");
716 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
717 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000718 verifyFormat(
719 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
720 " ccccccccccccccccccccccccc) {\n}");
721}
722
Daniel Jasper9cda8002013-01-07 13:08:40 +0000723TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
724 verifyFormat(
725 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
726 " SI->getAlignment(),\n"
727 " SI->getPointerAddressSpaceee());\n");
728 verifyFormat(
729 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
730 " Line.Tokens.front().Tok.getLocation(),\n"
731 " Line.Tokens.back().Tok.getLocation());");
732}
733
Daniel Jaspercf225b62012-12-24 13:43:52 +0000734TEST_F(FormatTest, AlignsAfterAssignments) {
735 verifyFormat(
736 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000737 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000738 verifyFormat(
739 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000740 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000741 verifyFormat(
742 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000743 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000744 verifyFormat(
745 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000746 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000747 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000748 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
749 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
750 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000751}
752
753TEST_F(FormatTest, AlignsAfterReturn) {
754 verifyFormat(
755 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
756 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
757 verifyFormat(
758 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
759 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
760}
761
Daniel Jasperbac016b2012-12-03 18:12:45 +0000762TEST_F(FormatTest, AlignsStringLiterals) {
763 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
764 " \"short literal\");");
765 verifyFormat(
766 "looooooooooooooooooooooooongFunction(\n"
767 " \"short literal\"\n"
768 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
769}
770
Alexander Kornienko15757312012-12-06 18:03:27 +0000771TEST_F(FormatTest, AlignsPipes) {
772 verifyFormat(
773 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
774 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
775 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
776 verifyFormat(
777 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
778 " << aaaaaaaaaaaaaaaaaaaa;");
779 verifyFormat(
780 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
781 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
782 verifyFormat(
783 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
784 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
785 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
786 verifyFormat(
787 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
788 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
789 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
790}
791
Daniel Jasperbac016b2012-12-03 18:12:45 +0000792TEST_F(FormatTest, UnderstandsEquals) {
793 verifyFormat(
794 "aaaaaaaaaaaaaaaaa =\n"
795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
796 verifyFormat(
797 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
799 "}");
800 verifyFormat(
801 "if (a) {\n"
802 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
804 "}");
805
Daniel Jasper9cda8002013-01-07 13:08:40 +0000806 verifyFormat(
807 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
808 " 10000000) {\n"
809 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000810}
811
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000812TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000813 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
814 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000815
Daniel Jasper1321eb52012-12-18 21:05:13 +0000816 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
817 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000818
819 verifyFormat(
820 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
821 " Parameter2);");
822
823 verifyFormat(
824 "ShortObject->shortFunction(\n"
825 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
826 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
827
828 verifyFormat("loooooooooooooongFunction(\n"
829 " LoooooooooooooongObject->looooooooooooooooongFunction());");
830
831 verifyFormat(
832 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
833 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
834
Daniel Jasper46a46a22013-01-07 07:13:20 +0000835 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000836 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
837 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
838 "}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000839 verifyFormat(
840 "aaaaaaaaaaa->aaaaaaaaa(\n"
841 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
842 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000843}
844
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000845TEST_F(FormatTest, WrapsTemplateDeclarations) {
846 verifyFormat("template <typename T>\n"
847 "virtual void loooooooooooongFunction(int Param1, int Param2);");
848 verifyFormat(
849 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
850 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
851 verifyFormat(
852 "template <typename T>\n"
853 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
854 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000855 verifyFormat(
856 "template <typename T>\n"
857 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
858 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000860
861}
862
Daniel Jasperbac016b2012-12-03 18:12:45 +0000863TEST_F(FormatTest, UnderstandsTemplateParameters) {
864 verifyFormat("A<int> a;");
865 verifyFormat("A<A<A<int> > > a;");
866 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
867 verifyFormat("bool x = a < 1 || 2 > a;");
868 verifyFormat("bool x = 5 < f<int>();");
869 verifyFormat("bool x = f<int>() > 5;");
870 verifyFormat("bool x = 5 < a<int>::x;");
871 verifyFormat("bool x = a < 4 ? a > 2 : false;");
872 verifyFormat("bool x = f() ? a < 2 : a > 2;");
873
874 verifyGoogleFormat("A<A<int>> a;");
875 verifyGoogleFormat("A<A<A<int>>> a;");
876 verifyGoogleFormat("A<A<A<A<int>>>> a;");
877
878 verifyFormat("test >> a >> b;");
879 verifyFormat("test << a >> b;");
880
881 verifyFormat("f<int>();");
882 verifyFormat("template <typename T> void f() {\n}");
883}
884
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000885TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000886 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000887 verifyFormat("f(-1, -2, -3);");
888 verifyFormat("a[-1] = 5;");
889 verifyFormat("int a = 5 + -2;");
Daniel Jasper112fb272012-12-05 07:51:39 +0000890 verifyFormat("if (i == -1) {\n}");
891 verifyFormat("if (i != -1) {\n}");
892 verifyFormat("if (i > -1) {\n}");
893 verifyFormat("if (i < -1) {\n}");
Daniel Jasperd56a7372012-12-06 13:16:39 +0000894 verifyFormat("++(a->f());");
895 verifyFormat("--(a->f());");
896 verifyFormat("if (!(a->f())) {\n}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000897
898 verifyFormat("a-- > b;");
899 verifyFormat("b ? -a : c;");
900 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000901 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000902 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000903 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +0000904
905 verifyFormat("return -1;");
906 verifyFormat("switch (a) {\n"
907 "case -1:\n"
908 " break;\n"
909 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000910}
911
912TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +0000913 verifyFormat("bool operator<();");
914 verifyFormat("bool operator>();");
915 verifyFormat("bool operator=();");
916 verifyFormat("bool operator==();");
917 verifyFormat("bool operator!=();");
918 verifyFormat("int operator+();");
919 verifyFormat("int operator++();");
920 verifyFormat("bool operator();");
921 verifyFormat("bool operator()();");
922 verifyFormat("bool operator[]();");
923 verifyFormat("operator bool();");
924 verifyFormat("operator SomeType<int>();");
925 verifyFormat("void *operator new(std::size_t size);");
926 verifyFormat("void *operator new[](std::size_t size);");
927 verifyFormat("void operator delete(void *ptr);");
928 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000929}
930
Daniel Jasper5d334402013-01-02 08:57:10 +0000931TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000932 verifyFormat("int *f(int *a) {\n}");
933 verifyFormat("f(a, *a);");
934 verifyFormat("f(*a);");
935 verifyFormat("int a = b * 10;");
936 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +0000937 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000938 verifyFormat("int a += b * c;");
939 verifyFormat("int a -= b * c;");
940 verifyFormat("int a *= b * c;");
941 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000942 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +0000943 verifyFormat("int a = *b * c;");
944 verifyFormat("int a = b * *c;");
Daniel Jasperc74e2792012-12-07 09:52:15 +0000945 verifyFormat("int main(int argc, char **argv) {\n}");
Nico Weber00d5a042012-12-23 01:07:46 +0000946 verifyFormat("return 10 * b;");
947 verifyFormat("return *b * *c;");
948 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +0000949 verifyFormat("f(b ? *c : *d);");
950 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000951 verifyFormat("*b = a;");
952 verifyFormat("a * ~b;");
953 verifyFormat("a * !b;");
954 verifyFormat("a * +b;");
955 verifyFormat("a * -b;");
956 verifyFormat("a * ++b;");
957 verifyFormat("a * --b;");
Daniel Jasperc74e2792012-12-07 09:52:15 +0000958
Daniel Jasper9bb0d282013-01-04 20:46:38 +0000959 verifyFormat("InvalidRegions[*R] = 0;");
960
Daniel Jasper8b39c662012-12-10 18:59:13 +0000961 // FIXME: Is this desired for LLVM? Fix if not.
962 verifyFormat("A<int *> a;");
963 verifyFormat("A<int **> a;");
964 verifyFormat("A<int *, int *> a;");
965 verifyFormat("A<int **, int **> a;");
Daniel Jasperef5b9c32013-01-02 15:46:59 +0000966 verifyFormat("Type *A = static_cast<Type *>(P);");
967 verifyFormat("Type *A = (Type *) P;");
968 verifyFormat("Type *A = (vector<Type *, int *>) P;");
Daniel Jasper8b39c662012-12-10 18:59:13 +0000969
Daniel Jasperc74e2792012-12-07 09:52:15 +0000970 verifyGoogleFormat("int main(int argc, char** argv) {\n}");
Daniel Jasper8b39c662012-12-10 18:59:13 +0000971 verifyGoogleFormat("A<int*> a;");
972 verifyGoogleFormat("A<int**> a;");
973 verifyGoogleFormat("A<int*, int*> a;");
974 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +0000975 verifyGoogleFormat("f(b ? *c : *d);");
976 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000977}
978
Daniel Jasper4dc41de2013-01-02 08:44:14 +0000979TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000980 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
981 " int LoooooooooooooooongParam2) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +0000982 verifyFormat(
983 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
984 " SourceLocation L, IdentifierIn *II,\n"
985 " Type *T) {\n}");
986}
987
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000988TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
989 verifyFormat("(a)->b();");
990 verifyFormat("--a;");
991}
992
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000993TEST_F(FormatTest, HandlesIncludeDirectives) {
994 EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000995 EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n"));
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000996 EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
997 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
998 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
Nico Weberb23ae0c2012-12-21 18:21:56 +0000999
1000 EXPECT_EQ("#import <string>\n", format("#import <string>\n"));
1001 EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n"));
1002 EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n"));
1003 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
1004 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001005}
1006
Alexander Kornienko15757312012-12-06 18:03:27 +00001007//===----------------------------------------------------------------------===//
1008// Error recovery tests.
1009//===----------------------------------------------------------------------===//
1010
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001011TEST_F(FormatTest, IncorrectAccessSpecifier) {
1012 verifyFormat("public:");
1013 verifyFormat("class A {\n"
1014 "public\n"
1015 " void f() {\n"
1016 " }\n"
1017 "};");
1018 verifyFormat("public\n"
1019 "int qwerty;");
1020 verifyFormat("public\n"
1021 "B {\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001022 "}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001023 verifyFormat("public\n"
1024 "{\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001025 "}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001026 verifyFormat("public\n"
1027 "B {\n"
1028 " int x;\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001029 "}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001030}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001031
Alexander Kornienko393b0082012-12-04 15:40:36 +00001032TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1033 verifyFormat("{");
1034}
1035
1036TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
1037 verifyFormat("do {\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001038 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001039 verifyFormat("do {\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001040 "}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001041 "f();");
1042 verifyFormat("do {\n"
1043 "}\n"
1044 "wheeee(fun);");
1045 verifyFormat("do {\n"
1046 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001047 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001048}
1049
Daniel Jasper1f42f112013-01-04 18:52:56 +00001050TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1051 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001052 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001053}
1054
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001055TEST_F(FormatTest, IncorrectCodeErrorDetection) {
1056 EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n"));
1057 EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n"));
1058 EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n"));
Alexander Kornienkoa3a2b3a2012-12-06 17:49:17 +00001059 EXPECT_EQ("{\n {\n }\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001060
1061 FormatStyle Style = getLLVMStyle();
1062 Style.ColumnLimit = 10;
1063 EXPECT_EQ("{\n"
1064 " {\n"
1065 " breakme(\n"
1066 " qwe);\n"
1067 "}\n", format("{\n"
1068 " {\n"
1069 " breakme(qwe);\n"
1070 "}\n", Style));
1071
1072}
1073
Nico Webercf4a79c2013-01-08 17:56:31 +00001074//===----------------------------------------------------------------------===//
1075// Objective-C tests.
1076//===----------------------------------------------------------------------===//
1077
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001078TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1079 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1080 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1081 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001082 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001083 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1084 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1085 format("-(NSInteger)Method3:(id)anObject;"));
1086 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1087 format("-(NSInteger)Method4:(id)anObject;"));
1088 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1089 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1090 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1091 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001092 EXPECT_EQ(
1093 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1094 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001095
1096 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001097 EXPECT_EQ(
1098 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1099 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1100 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1101 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1102 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1103 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1104 format(
1105 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1106 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1107 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1108 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1109 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1110 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001111}
1112
Nico Weber581f5572013-01-07 15:56:25 +00001113TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001114 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001115 verifyFormat("@catch");
1116 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001117 verifyFormat("@compatibility_alias");
1118 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001119 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001120 verifyFormat("@encode");
1121 verifyFormat("@end");
1122 verifyFormat("@finally");
1123 verifyFormat("@implementation");
1124 verifyFormat("@import");
1125 verifyFormat("@interface");
1126 verifyFormat("@optional");
1127 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001128 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001129 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001130 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001131 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001132 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001133 verifyFormat("@required");
1134 verifyFormat("@selector");
1135 verifyFormat("@synchronized");
1136 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001137 verifyFormat("@throw");
1138 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001139
Nico Webercb4d6902013-01-08 19:40:21 +00001140 // FIXME: Make the uncommented lines below pass.
1141 verifyFormat("@\"String\"");
1142 verifyFormat("@1");
1143 //verifyFormat("@+4.8");
1144 //verifyFormat("@-4");
1145 verifyFormat("@1LL");
1146 verifyFormat("@.5");
1147 verifyFormat("@'c'");
1148 verifyFormat("@true");
1149 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
1150 verifyFormat("@[");
1151 verifyFormat("@{");
1152
1153
Nico Weber581f5572013-01-07 15:56:25 +00001154 EXPECT_EQ("@interface", format("@ interface"));
1155
1156 // The precise formatting of this doesn't matter, nobody writes code like
1157 // this.
1158 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001159}
1160
Nico Weberc31689a2013-01-08 19:15:23 +00001161TEST_F(FormatTest, ObjCSnippets) {
1162 // FIXME: Make the uncommented lines below pass.
1163 verifyFormat("@autoreleasepool {\n"
1164 " foo();\n"
1165 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001166 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001167 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001168 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001169 //verifyFormat("char *buf1 = @encode(int **);");
1170 //verifyFormat("SEL s = @selector(foo:);");
1171 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001172
1173 // FIXME: "getter=bar" should not be surround by spaces in @property.
1174 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
1175}
1176
Daniel Jaspercd162382013-01-07 13:26:07 +00001177} // end namespace tooling
1178} // end namespace clang