blob: 57252f547d6bef3df8350dbb7a57726d1bbb2330 [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
Manuel Klimekca547db2013-01-16 14:55:28 +000010#define DEBUG_TYPE "format-test"
11
Chandler Carruth1050e8b2012-12-04 09:45:34 +000012#include "clang/Format/Format.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000013#include "../Tooling/RewriterTestContext.h"
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000014#include "clang/Lex/Lexer.h"
15#include "llvm/Support/Debug.h"
16#include "gtest/gtest.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000017
18// Uncomment to get debug output from tests:
19// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
Daniel Jasperbac016b2012-12-03 18:12:45 +000020
21namespace clang {
22namespace format {
23
24class FormatTest : public ::testing::Test {
25protected:
26 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
27 const FormatStyle &Style) {
Manuel Klimekca547db2013-01-16 14:55:28 +000028 DEBUG(llvm::errs() << "---\n");
Daniel Jasperbac016b2012-12-03 18:12:45 +000029 RewriterTestContext Context;
30 FileID ID = Context.createInMemoryFile("input.cc", Code);
31 SourceLocation Start =
32 Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
33 std::vector<CharSourceRange> Ranges(
34 1,
35 CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
Daniel Jasper46ef8522013-01-10 13:08:12 +000036 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
37 getFormattingLangOpts());
Daniel Jaspercd162382013-01-07 13:26:07 +000038 tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +000039 Ranges,
40 new IgnoringDiagConsumer());
Daniel Jasperbac016b2012-12-03 18:12:45 +000041 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
Manuel Klimekca547db2013-01-16 14:55:28 +000042 DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n");
Daniel Jasperbac016b2012-12-03 18:12:45 +000043 return Context.getRewrittenText(ID);
44 }
45
46 std::string format(llvm::StringRef Code,
47 const FormatStyle &Style = getLLVMStyle()) {
48 return format(Code, 0, Code.size(), Style);
49 }
50
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000051 std::string messUp(llvm::StringRef Code) {
52 std::string MessedUp(Code.str());
53 bool InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000054 bool InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000055 bool JustReplacedNewline = false;
56 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
57 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
58 if (JustReplacedNewline)
59 MessedUp[i - 1] = '\n';
60 InComment = true;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +000061 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
62 if (i != 0) MessedUp[i - 1] = '\n';
Manuel Klimek526ed112013-01-09 15:25:02 +000063 InPreprocessorDirective = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +000064 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
65 MessedUp[i] = ' ';
Manuel Klimek526ed112013-01-09 15:25:02 +000066 MessedUp[i + 1] = ' ';
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000067 } else if (MessedUp[i] == '\n') {
68 if (InComment) {
69 InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000070 } else if (InPreprocessorDirective) {
71 InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000072 } else {
73 JustReplacedNewline = true;
74 MessedUp[i] = ' ';
75 }
Manuel Klimekd4397b92013-01-04 23:34:14 +000076 } else if (MessedUp[i] != ' ') {
77 JustReplacedNewline = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000078 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000079 }
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000080 return MessedUp;
81 }
82
Manuel Klimek060143e2013-01-02 18:33:23 +000083 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
84 FormatStyle Style = getLLVMStyle();
85 Style.ColumnLimit = ColumnLimit;
86 return Style;
87 }
88
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000089 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
90 FormatStyle Style = getGoogleStyle();
91 Style.ColumnLimit = ColumnLimit;
92 return Style;
93 }
94
Manuel Klimek060143e2013-01-02 18:33:23 +000095 void verifyFormat(llvm::StringRef Code,
96 const FormatStyle &Style = getLLVMStyle()) {
97 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000098 }
99
100 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +0000101 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102 }
Daniel Jasper4bfc65a2013-01-23 12:10:53 +0000103
104 void verifyIndependentOfContext(llvm::StringRef text) {
105 verifyFormat(text);
106 verifyFormat(llvm::Twine("void f() { " + text + " }").str());
107 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000108};
109
Manuel Klimek526ed112013-01-09 15:25:02 +0000110TEST_F(FormatTest, MessUp) {
111 EXPECT_EQ("1 2 3", messUp("1 2 3"));
112 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
113 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
114 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
115 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
116}
117
Alexander Kornienko15757312012-12-06 18:03:27 +0000118//===----------------------------------------------------------------------===//
119// Basic function tests.
120//===----------------------------------------------------------------------===//
121
Daniel Jasperbac016b2012-12-03 18:12:45 +0000122TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
123 EXPECT_EQ(";", format(";"));
124}
125
126TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
127 EXPECT_EQ("int i;", format(" int i;"));
128 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
129 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
130 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
131}
132
133TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
134 EXPECT_EQ("int i;", format("int\ni;"));
135}
136
137TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000138 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000139}
140
Alexander Kornienko15757312012-12-06 18:03:27 +0000141TEST_F(FormatTest, FormatsNestedCall) {
142 verifyFormat("Method(f1, f2(f3));");
143 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper4e9008a2013-01-13 08:19:51 +0000144 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000145}
146
Daniel Jasper6b825c22013-01-16 07:19:28 +0000147TEST_F(FormatTest, ImportantSpaces) {
148 verifyFormat("vector< ::Type> v;");
149}
150
Alexander Kornienko15757312012-12-06 18:03:27 +0000151//===----------------------------------------------------------------------===//
152// Tests for control statements.
153//===----------------------------------------------------------------------===//
154
155TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000156 verifyFormat("if (true)\n f();\ng();");
157 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000158 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jasperdf3736a2013-01-16 15:44:34 +0000159
160 FormatStyle AllowsMergedIf = getGoogleStyle();
161 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
162 verifyFormat("if (a)\n"
163 " // comment\n"
164 " f();", AllowsMergedIf);
165
166 verifyFormat("if (a) // Can't merge this\n"
167 " f();\n", AllowsMergedIf);
168 verifyFormat("if (a) /* still don't merge */\n"
169 " f();", AllowsMergedIf);
170 verifyFormat("if (a) { // Never merge this\n"
171 " f();\n"
172 "}", AllowsMergedIf);
173 verifyFormat("if (a) { /* Never merge this */\n"
174 " f();\n"
175 "}", AllowsMergedIf);
176
177 AllowsMergedIf.ColumnLimit = 14;
178 verifyFormat("if (a) return;", AllowsMergedIf);
Daniel Jasperfd0ca972013-01-14 16:02:06 +0000179 verifyFormat("if (aaaaaaaaa)\n"
Daniel Jasperdf3736a2013-01-16 15:44:34 +0000180 " return;", AllowsMergedIf);
181
182 AllowsMergedIf.ColumnLimit = 13;
183 verifyFormat("if (a)\n return;", AllowsMergedIf);
Alexander Kornienko15757312012-12-06 18:03:27 +0000184}
185
186TEST_F(FormatTest, ParseIfElse) {
187 verifyFormat("if (true)\n"
188 " if (true)\n"
189 " if (true)\n"
190 " f();\n"
191 " else\n"
192 " g();\n"
193 " else\n"
194 " h();\n"
195 "else\n"
196 " i();");
197 verifyFormat("if (true)\n"
198 " if (true)\n"
199 " if (true) {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000200 " if (true)\n"
201 " f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000202 " } else {\n"
203 " g();\n"
204 " }\n"
205 " else\n"
206 " h();\n"
207 "else {\n"
208 " i();\n"
209 "}");
210}
211
212TEST_F(FormatTest, ElseIf) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000213 verifyFormat("if (a) {\n} else if (b) {\n}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000214 verifyFormat("if (a)\n"
215 " f();\n"
216 "else if (b)\n"
217 " g();\n"
218 "else\n"
219 " h();");
220}
221
Daniel Jasperbac016b2012-12-03 18:12:45 +0000222TEST_F(FormatTest, FormatsForLoop) {
223 verifyFormat(
224 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000225 " ++VeryVeryLongLoopVariable)\n"
226 " ;");
227 verifyFormat("for (;;)\n"
228 " f();");
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000229 verifyFormat("for (;;) {\n}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000230 verifyFormat("for (;;) {\n"
231 " f();\n"
232 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000233
234 verifyFormat(
235 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
236 " E = UnwrappedLines.end();\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000237 " I != E; ++I) {\n}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000238
239 verifyFormat(
240 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000241 " ++IIIII) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000242}
243
244TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000245 verifyFormat("while (true) {\n}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000246 verifyFormat("while (true)\n"
247 " f();");
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000248 verifyFormat("while () {\n}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000249 verifyFormat("while () {\n"
250 " f();\n"
251 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000252}
253
Alexander Kornienko15757312012-12-06 18:03:27 +0000254TEST_F(FormatTest, FormatsDoWhile) {
255 verifyFormat("do {\n"
256 " do_something();\n"
257 "} while (something());");
258 verifyFormat("do\n"
259 " do_something();\n"
260 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000261}
262
Alexander Kornienko15757312012-12-06 18:03:27 +0000263TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000264 verifyFormat("switch (x) {\n"
265 "case 1:\n"
266 " f();\n"
267 " break;\n"
268 "case kFoo:\n"
269 "case ns::kBar:\n"
270 "case kBaz:\n"
271 " break;\n"
272 "default:\n"
273 " g();\n"
274 " break;\n"
275 "}");
276 verifyFormat("switch (x) {\n"
277 "case 1: {\n"
278 " f();\n"
279 " break;\n"
280 "}\n"
281 "}");
Nico Weber94fb7292013-01-18 05:50:57 +0000282 verifyFormat("switch (x) {\n"
283 "case 1: {\n"
284 " f();\n"
285 " {\n"
286 " g();\n"
287 " h();\n"
288 " }\n"
289 " break;\n"
290 "}\n"
291 "}");
292 verifyFormat("switch (x) {\n"
293 "case 1: {\n"
294 " f();\n"
295 " if (foo) {\n"
296 " g();\n"
297 " h();\n"
298 " }\n"
299 " break;\n"
300 "}\n"
301 "}");
302 verifyFormat("switch (x) {\n"
303 "case 1: {\n"
304 " f();\n"
305 " g();\n"
306 "} break;\n"
307 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000308 verifyFormat("switch (test)\n"
309 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000310 verifyGoogleFormat("switch (x) {\n"
311 " case 1:\n"
312 " f();\n"
313 " break;\n"
314 " case kFoo:\n"
315 " case ns::kBar:\n"
316 " case kBaz:\n"
317 " break;\n"
318 " default:\n"
319 " g();\n"
320 " break;\n"
321 "}");
322 verifyGoogleFormat("switch (x) {\n"
323 " case 1: {\n"
324 " f();\n"
325 " break;\n"
326 " }\n"
327 "}");
328 verifyGoogleFormat("switch (test)\n"
329 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000330}
331
Alexander Kornienko15757312012-12-06 18:03:27 +0000332TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000333 verifyFormat("void f() {\n"
334 " some_code();\n"
335 "test_label:\n"
336 " some_other_code();\n"
337 " {\n"
338 " some_more_code();\n"
339 " another_label:\n"
340 " some_more_code();\n"
341 " }\n"
342 "}");
343 verifyFormat("some_code();\n"
344 "test_label:\n"
345 "some_other_code();");
346}
347
Alexander Kornienko15757312012-12-06 18:03:27 +0000348//===----------------------------------------------------------------------===//
349// Tests for comments.
350//===----------------------------------------------------------------------===//
351
352TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000353 verifyFormat("// line 1\n"
354 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000355 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000356
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000357 verifyFormat("void f() {\n"
358 " // Doesn't do anything\n"
359 "}");
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000360 verifyFormat("void f(int i, // some comment (probably for i)\n"
361 " int j, // some comment (probably for j)\n"
Daniel Jasper487f64b2013-01-13 16:10:20 +0000362 " int k); // some comment (probably for k)");
363 verifyFormat("void f(int i,\n"
364 " // some comment (probably for j)\n"
365 " int j,\n"
366 " // some comment (probably for k)\n"
367 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000368
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000369 verifyFormat("int i // This is a fancy variable\n"
370 " = 5; // with nicely aligned comment.");
371
372 verifyFormat("// Leading comment.\n"
373 "int a; // Trailing comment.");
374 verifyFormat("int a; // Trailing comment\n"
375 " // on 2\n"
376 " // or 3 lines.\n"
377 "int b;");
378 verifyFormat("int a; // Trailing comment\n"
379 "\n"
380 "// Leading comment.\n"
381 "int b;");
382 verifyFormat("int a; // Comment.\n"
383 " // More details.\n"
384 "int bbbb; // Another comment.");
385 verifyFormat(
386 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
387 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
388 "int cccccccccccccccccccccccccccccc; // comment\n"
389 "int ddd; // looooooooooooooooooooooooong comment\n"
390 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
391 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
392 "int ccccccccccccccccccc; // comment");
393
Daniel Jasper7d1185d2013-01-18 09:19:33 +0000394 verifyFormat("#include \"a\" // comment\n"
395 "#include \"a/b/c\" // comment");
396 verifyFormat("#include <a> // comment\n"
397 "#include <a/b/c> // comment");
Alexander Kornienko15757312012-12-06 18:03:27 +0000398
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000399 verifyFormat("enum E {\n"
400 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000401 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000402 " VAL_B\n"
403 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000404
405 verifyFormat(
406 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000407 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000408 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
409 " // Comment inside a statement.\n"
410 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000411
Daniel Jasper821627e2013-01-21 22:49:20 +0000412 EXPECT_EQ("void f() { // This does something ..\n"
413 "}\n"
414 "int a; // This is unrelated",
415 format("void f() { // This does something ..\n"
416 " }\n"
417 "int a; // This is unrelated"));
418 EXPECT_EQ("void f() { // This does something ..\n"
419 "} // awesome..\n"
420 "\n"
421 "int a; // This is unrelated",
422 format("void f() { // This does something ..\n"
423 " } // awesome..\n"
424 " \n"
425 "int a; // This is unrelated"));
426
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000427 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000428 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000429
430 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000431}
432
433TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000434 verifyFormat("f(/*test=*/ true);");
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000435 EXPECT_EQ(
436 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
437 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
438 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n"
439 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
440 EXPECT_EQ(
441 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
442 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
443 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
444 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
445}
446
447TEST_F(FormatTest, CommentsInStaticInitializers) {
448 EXPECT_EQ(
449 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
450 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
451 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
452 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
453 " aaaaaaaaaaaaaaaaaaaa };",
454 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
455 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
456 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
457 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
458 " aaaaaaaaaaaaaaaaaaaa };"));
459 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
460 " bbbbbbbbbbb, ccccccccccc };");
461 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
462 " // comment for bb....\n"
463 " bbbbbbbbbbb, ccccccccccc };");
464 verifyGoogleFormat(
465 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
466 " bbbbbbbbbbb,\n"
467 " ccccccccccc };");
468 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
469 " // comment for bb....\n"
470 " bbbbbbbbbbb,\n"
471 " ccccccccccc };");
472
Alexander Kornienko15757312012-12-06 18:03:27 +0000473}
474
Alexander Kornienko15757312012-12-06 18:03:27 +0000475//===----------------------------------------------------------------------===//
476// Tests for classes, namespaces, etc.
477//===----------------------------------------------------------------------===//
478
479TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000480 verifyFormat("class A {\n};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000481}
482
483TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
484 verifyFormat("class A {\n"
485 "public:\n"
486 "protected:\n"
487 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000488 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000489 "};");
490 verifyGoogleFormat("class A {\n"
491 " public:\n"
492 " protected:\n"
493 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000494 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000495 "};");
496}
497
498TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000499 verifyFormat("class A : public B {\n};");
500 verifyFormat("class A : public ::B {\n};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000501}
502
Manuel Klimekde768542013-01-07 18:10:23 +0000503TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000504 verifyFormat("class A {\n} a, b;");
505 verifyFormat("struct A {\n} a, b;");
506 verifyFormat("union A {\n} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000507}
508
Alexander Kornienko15757312012-12-06 18:03:27 +0000509TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000510 verifyFormat("enum {\n"
511 " Zero,\n"
512 " One = 1,\n"
513 " Two = One + 1,\n"
514 " Three = (One + Two),\n"
515 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
516 " Five = (One, Two, Three, Four, 5)\n"
517 "};");
518 verifyFormat("enum Enum {\n"
519 "};");
520 verifyFormat("enum {\n"
521 "};");
Manuel Klimek308232c2013-01-21 19:17:52 +0000522 verifyFormat("enum X E {\n} d;");
523 verifyFormat("enum __attribute__((...)) E {\n} d;");
524 verifyFormat("enum __declspec__((...)) E {\n} d;");
525 verifyFormat("enum X f() {\n a();\n return 42;\n}");
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000526}
527
Nico Weberefaddc02013-01-14 05:49:49 +0000528TEST_F(FormatTest, FormatsBitfields) {
529 verifyFormat("struct Bitfields {\n"
530 " unsigned sClass : 8;\n"
531 " unsigned ValueKind : 2;\n"
532 "};");
533}
534
Alexander Kornienko15757312012-12-06 18:03:27 +0000535TEST_F(FormatTest, FormatsNamespaces) {
536 verifyFormat("namespace some_namespace {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000537 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000538 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000539 "}");
540 verifyFormat("namespace {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000541 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000542 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000543 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000544 verifyFormat("inline namespace X {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000545 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000546 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000547 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000548 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000549 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000550 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000551}
552
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000553TEST_F(FormatTest, FormatsExternC) {
554 verifyFormat("extern \"C\" {\nint a;");
555}
556
Nico Webera9ccdd12013-01-07 16:36:17 +0000557TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000558 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
559 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000560 verifyFormat("try {\n"
561 " throw a * b;\n"
562 "}\n"
563 "catch (int a) {\n"
564 " // Do nothing.\n"
565 "}\n"
566 "catch (...) {\n"
567 " exit(42);\n"
568 "}");
569
570 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000571 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000572 "catch (...) {\n"
573 " return 5;\n"
574 "}");
575 verifyFormat("class A {\n"
576 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000577 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000578 " catch (...) {\n"
579 " throw;\n"
580 " }\n"
581 "};\n");
582}
583
584TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000585 verifyFormat("@try {\n"
586 " f();\n"
587 "}\n"
588 "@catch (NSException e) {\n"
589 " @throw;\n"
590 "}\n"
591 "@finally {\n"
592 " exit(42);\n"
593 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000594}
595
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000596TEST_F(FormatTest, StaticInitializers) {
597 verifyFormat("static SomeClass SC = { 1, 'a' };");
598
599 // FIXME: Format like enums if the static initializer does not fit on a line.
600 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000601 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000602 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
603 "};");
604
605 verifyFormat(
606 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
607 " looooooooooooooooooooooooooooooooooongname,\n"
608 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000609}
610
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000611TEST_F(FormatTest, NestedStaticInitializers) {
612 verifyFormat("static A x = { { {} } };\n");
613 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000614 "static A x = { { { init1, init2, init3, init4 },\n"
615 " { init1, init2, init3, init4 } } };");
616
Nico Weber6a21a552013-01-18 02:43:57 +0000617 // FIXME: Fix this in general and verify that it works in LLVM style again.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000618 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000619 "somes Status::global_reps[3] = {\n"
620 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
621 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
622 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
623 "};");
624 verifyFormat(
625 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
626 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
627 " } };");
628
Nico Weber6a21a552013-01-18 02:43:57 +0000629 // FIXME: We might at some point want to handle this similar to parameter
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000630 // lists, where we have an option to put each on a single line.
631 verifyFormat("struct {\n"
632 " unsigned bit;\n"
633 " const char *const name;\n"
634 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
635 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
636}
637
Manuel Klimeka080a182013-01-02 16:30:12 +0000638TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
639 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
640 " \\\n"
641 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
642}
643
Daniel Jasper71607512013-01-07 10:48:50 +0000644TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000645 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
646 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000647}
648
Manuel Klimeka080a182013-01-02 16:30:12 +0000649TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
650 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000651 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000652}
653
654TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
655 EXPECT_EQ("#line 42 \"test\"\n",
656 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000657 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000658 format("# \\\n define \\\n A \\\n B\n",
659 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000660}
661
662TEST_F(FormatTest, EndOfFileEndsPPDirective) {
663 EXPECT_EQ("#line 42 \"test\"",
664 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000665 EXPECT_EQ("#define A B",
666 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000667}
668
Manuel Klimek060143e2013-01-02 18:33:23 +0000669TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000670 // If the macro fits in one line, we still do not get the full
671 // line, as only the next line decides whether we need an escaped newline and
672 // thus use the last column.
673 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000674
Manuel Klimekd544c572013-01-07 09:24:17 +0000675 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
676 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000677 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000678
679 verifyFormat("#define A A\n#define A A");
680 verifyFormat("#define A(X) A\n#define A A");
681
682 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
683 verifyFormat("#define Something \\\n"
684 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000685}
686
687TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000688 EXPECT_EQ("// some comment\n"
689 "#include \"a.h\"\n"
690 "#define A(A,\\\n"
691 " B)\n"
692 "#include \"b.h\"\n"
693 "// some comment\n",
694 format(" // some comment\n"
695 " #include \"a.h\"\n"
696 "#define A(A,\\\n"
697 " B)\n"
698 " #include \"b.h\"\n"
699 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000700}
701
Manuel Klimekd4397b92013-01-04 23:34:14 +0000702TEST_F(FormatTest, LayoutSingleHash) {
703 EXPECT_EQ("#\na;", format("#\na;"));
704}
705
706TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
707 EXPECT_EQ("#define A \\\n"
708 " c; \\\n"
709 " e;\n"
710 "f;", format("#define A c; e;\n"
711 "f;", getLLVMStyleWithColumns(14)));
712}
713
714TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000715 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000716}
717
718TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000719 EXPECT_EQ("# define A\\\n b;",
720 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000721}
722
723TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000724 EXPECT_EQ("int x,\n"
725 "#define A\n"
726 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000727}
728
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000729TEST_F(FormatTest, HashInMacroDefinition) {
730 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
731 verifyFormat("#define A \\\n"
732 " { \\\n"
733 " f(#c);\\\n"
734 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000735
736 verifyFormat("#define A(X) \\\n"
737 " void function##X()", getLLVMStyleWithColumns(22));
738
739 verifyFormat("#define A(a, b, c) \\\n"
740 " void a##b##c()", getLLVMStyleWithColumns(22));
741
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000742 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000743}
744
Manuel Klimek7ccbc212013-01-23 14:37:36 +0000745TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
746 verifyFormat("#define A (1)");
747}
748
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000749TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
750 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
751}
752
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000753TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000754 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000755}
756
Manuel Klimeka5342db2013-01-06 20:07:31 +0000757TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
758 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
759 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
760 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
761 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
762}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000763
Manuel Klimek95419382013-01-07 07:56:50 +0000764TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000765 EXPECT_EQ(
766 "#define A \\\n int i; \\\n int j;",
767 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000768}
769
Manuel Klimekd544c572013-01-07 09:24:17 +0000770TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
771 verifyFormat("#define A \\\n"
772 " int v( \\\n"
773 " a); \\\n"
774 " int i;", getLLVMStyleWithColumns(11));
775}
776
Manuel Klimeka080a182013-01-02 16:30:12 +0000777TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000778 EXPECT_EQ(
779 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
780 " \\\n"
781 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
782 "\n"
783 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
784 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
785 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
786 "\\\n"
787 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
788 " \n"
789 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
790 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000791}
792
Manuel Klimek526ed112013-01-09 15:25:02 +0000793TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
794 EXPECT_EQ("int\n"
795 "#define A\n"
796 " a;",
797 format("int\n#define A\na;"));
798 verifyFormat(
Daniel Jasper986e17f2013-01-28 07:35:34 +0000799 "functionCallTo(\n"
800 " someOtherFunction(\n"
801 " withSomeParameters, whichInSequence,\n"
802 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000803 "#define A B\n"
Daniel Jasper986e17f2013-01-28 07:35:34 +0000804 " withMoreParamters,\n"
805 " whichStronglyInfluenceTheLayout),\n"
806 " andMoreParameters), trailing);",
807 getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000808}
809
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000810TEST_F(FormatTest, LayoutBlockInsideParens) {
811 EXPECT_EQ("functionCall({\n"
812 " int i;\n"
813 "});", format(" functionCall ( {int i;} );"));
814}
815
816TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000817 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000818 "int i;", format(" SOME_MACRO {int i;} int i;"));
819}
820
821TEST_F(FormatTest, LayoutNestedBlocks) {
822 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
823 " struct s {\n"
824 " int i;\n"
825 " };\n"
826 " s kBitsToOs[] = { { 10 } };\n"
827 " for (int i = 0; i < 10; ++i)\n"
828 " return;\n"
829 "}");
830}
831
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000832TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
833 EXPECT_EQ("{}", format("{}"));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000834
835 // Negative test for enum.
836 verifyFormat("enum E {\n};");
837
838 // Note that when there's a missing ';', we still join...
839 verifyFormat("enum E {}");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000840}
841
Alexander Kornienko15757312012-12-06 18:03:27 +0000842//===----------------------------------------------------------------------===//
843// Line break tests.
844//===----------------------------------------------------------------------===//
845
846TEST_F(FormatTest, FormatsFunctionDefinition) {
847 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
848 " int h, int j, int f,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000849 " int c, int ddddddddddddd) {\n}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000850}
851
852TEST_F(FormatTest, FormatsAwesomeMethodCall) {
853 verifyFormat(
Daniel Jasper986e17f2013-01-28 07:35:34 +0000854 "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
855 " parameter, parameter, parameter)),\n"
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000856 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000857}
858
Daniel Jasperae8699b2013-01-28 09:35:24 +0000859TEST_F(FormatTest, PreventConfusingIndents) {
Daniel Jasper986e17f2013-01-28 07:35:34 +0000860 verifyFormat(
861 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
862 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
864 " aaaaaaaaaaaaaaaaaaaaaaaa);");
865 verifyFormat(
866 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n"
867 " aaaaaaaaaaaaaaaaaaaaaaaa[\n"
868 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n"
869 " aaaaaaaaaaaaaaaaaaaaaaaa];");
870 verifyFormat(
871 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
872 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
873 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
874 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
Daniel Jasperae8699b2013-01-28 09:35:24 +0000875 verifyFormat("int a = bbbb && ccc && fffff(\n"
876 "#define A Just forcing a new line\n"
877 " ddd);");
Daniel Jasper986e17f2013-01-28 07:35:34 +0000878}
879
Daniel Jasper1321eb52012-12-18 21:05:13 +0000880TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000881 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000882 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
883 getLLVMStyleWithColumns(45));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000884 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}",
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000885 getLLVMStyleWithColumns(44));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000886 verifyFormat("Constructor()\n"
887 " : Inttializer(FitsOnTheLine) {\n}",
888 getLLVMStyleWithColumns(43));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000889
890 verifyFormat(
891 "SomeClass::Constructor()\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000892 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000893
894 verifyFormat(
895 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000896 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000897 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000898 verifyGoogleFormat(
899 "SomeClass::Constructor()\n"
900 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
901 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000902 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper16618242013-01-16 17:00:50 +0000903 verifyGoogleFormat(
904 "SomeClass::Constructor()\n"
905 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
906 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000907 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000908
909 verifyFormat(
910 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000911 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000912 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000913
914 verifyFormat("Constructor()\n"
915 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
916 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
917 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000918 " aaaaaaaaaaaaaaaaaaaaaaa() {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000919
920 // Here a line could be saved by splitting the second initializer onto two
921 // lines, but that is not desireable.
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000922 verifyFormat(
923 "Constructor()\n"
924 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
925 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
926 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000927
928 verifyGoogleFormat("MyClass::MyClass(int var)\n"
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000929 " : some_var_(var), // 4 space indent\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000930 " some_other_var_(var + 1) { // lined up\n"
931 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000932
933 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000934 std::string input = "Constructor()\n"
Daniel Jasper3499dda2013-01-25 15:43:32 +0000935 " : aaaa(a,\n";
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000936 for (unsigned i = 0, e = 80; i != e; ++i) {
937 input += " a,\n";
938 }
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000939 input += " a) {\n}";
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000940 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000941}
942
Alexander Kornienko15757312012-12-06 18:03:27 +0000943TEST_F(FormatTest, BreaksAsHighAsPossible) {
944 verifyFormat(
945 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
946 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
947 " f();");
948}
949
Daniel Jasperbac016b2012-12-03 18:12:45 +0000950TEST_F(FormatTest, BreaksDesireably) {
951 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
952 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000953 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
Daniel Jaspere438bac2013-01-23 20:41:06 +0000954 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
956 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000957
958 verifyFormat(
959 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000961
962 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
964 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000965
966 verifyFormat(
967 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
969 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000971
972 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
973 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
974
Daniel Jasper723f0302013-01-02 14:40:02 +0000975 verifyFormat(
976 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper3499dda2013-01-25 15:43:32 +0000978 verifyFormat(
979 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
980 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
981 verifyFormat(
982 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
983 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jasper723f0302013-01-02 14:40:02 +0000984
Daniel Jasper33182dd2012-12-05 14:57:28 +0000985 // This test case breaks on an incorrect memoization, i.e. an optimization not
986 // taking into account the StopAt value.
987 verifyFormat(
988 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000989 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
990 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
991 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000992
Daniel Jaspercd162382013-01-07 13:26:07 +0000993 verifyFormat("{\n {\n {\n"
994 " Annotation.SpaceRequiredBefore =\n"
995 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
996 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
997 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000998}
999
Daniel Jasper0df6acd2013-01-16 14:59:02 +00001000TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
1001 verifyGoogleFormat(
1002 "aaaaaaaa(aaaaaaaaaaaaa,\n"
1003 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
1005 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
1007 verifyGoogleFormat(
Daniel Jasper8f4bd7a2013-01-23 10:08:28 +00001008 "aaaaaaaaaaaaaaa(\n"
1009 " aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();");
1010 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1011 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);");
1012
1013 verifyGoogleFormat(
1014 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1015 " aaaaaaaaaaaa,\n"
1016 " aaaaaaaaaaaa);");
Daniel Jasper0df6acd2013-01-16 14:59:02 +00001017 verifyGoogleFormat(
1018 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
1019 " ddddddddddddddddddddddddddddd),\n"
1020 " test);");
1021
1022 verifyGoogleFormat(
1023 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
1024 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
1025 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
1026 verifyGoogleFormat("a(\"a\"\n"
1027 " \"a\",\n"
1028 " a);");
Daniel Jasper8f4bd7a2013-01-23 10:08:28 +00001029
1030 FormatStyle Style = getGoogleStyle();
1031 Style.AllowAllParametersOnNextLine = false;
1032 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa,\n"
1033 " aaaaaaaaa,\n"
1034 " aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();",
1035 Style);
Daniel Jasper0df6acd2013-01-16 14:59:02 +00001036}
1037
Daniel Jasperc79afda2013-01-18 10:56:38 +00001038TEST_F(FormatTest, FormatsBuilderPattern) {
1039 verifyFormat(
1040 "return llvm::StringSwitch<Reference::Kind>(name)\n"
Daniel Jasperf39c8852013-01-23 16:58:21 +00001041 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
1042 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
1043 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
1044 " .Default(ORDER_TEXT);\n");
Daniel Jasperc79afda2013-01-18 10:56:38 +00001045}
1046
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001047TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
1048 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1049 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +00001050 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
1051 " GUARDED_BY(aaaaaaaaaaaaa);");
1052 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001053 " GUARDED_BY(aaaaaaaaaaaaa) {\n}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001054}
1055
Daniel Jaspere2c7acf2012-12-24 00:13:23 +00001056TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
1057 verifyFormat(
1058 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001059 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +00001060 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001061 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +00001062 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001063 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +00001064 verifyFormat(
1065 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001066 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +00001067}
1068
Daniel Jasper7006e7e2013-01-23 12:27:43 +00001069TEST_F(FormatTest, BreaksAfterAssignments) {
Daniel Jasper9cda8002013-01-07 13:08:40 +00001070 verifyFormat(
Daniel Jasper7006e7e2013-01-23 12:27:43 +00001071 "unsigned Cost =\n"
1072 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
1073 " SI->getPointerAddressSpaceee());\n");
Daniel Jasper9cda8002013-01-07 13:08:40 +00001074 verifyFormat(
Daniel Jasper13cb7c22013-01-23 15:55:19 +00001075 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
1076 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
Daniel Jasperf39c8852013-01-23 16:58:21 +00001077
1078 verifyFormat(
1079 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n"
1080 " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper9cda8002013-01-07 13:08:40 +00001081}
1082
Daniel Jaspercf225b62012-12-24 13:43:52 +00001083TEST_F(FormatTest, AlignsAfterAssignments) {
1084 verifyFormat(
1085 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001086 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001087 verifyFormat(
1088 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001089 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001090 verifyFormat(
1091 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001092 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001093 verifyFormat(
1094 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001095 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001096 verifyFormat(
Daniel Jasper7006e7e2013-01-23 12:27:43 +00001097 "double LooooooooooooooooooooooooongResult =\n"
1098 " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1099 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001100}
1101
1102TEST_F(FormatTest, AlignsAfterReturn) {
1103 verifyFormat(
1104 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1105 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
1106 verifyFormat(
1107 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1108 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
1109}
1110
Daniel Jasper9c837d02013-01-09 07:06:56 +00001111TEST_F(FormatTest, BreaksConditionalExpressions) {
1112 verifyFormat(
1113 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1114 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1115 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1116 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
1117 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +00001118 verifyFormat(
1119 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
1120 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +00001121}
1122
Nico Weber7d37b8b2013-01-12 01:28:06 +00001123TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1124 verifyFormat("arr[foo ? bar : baz];");
1125 verifyFormat("f()[foo ? bar : baz];");
1126 verifyFormat("(a + b)[foo ? bar : baz];");
1127 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1128}
1129
Daniel Jasperbac016b2012-12-03 18:12:45 +00001130TEST_F(FormatTest, AlignsStringLiterals) {
1131 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1132 " \"short literal\");");
1133 verifyFormat(
1134 "looooooooooooooooooooooooongFunction(\n"
1135 " \"short literal\"\n"
1136 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1137}
1138
Alexander Kornienko15757312012-12-06 18:03:27 +00001139TEST_F(FormatTest, AlignsPipes) {
1140 verifyFormat(
1141 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1142 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1143 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1144 verifyFormat(
1145 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1146 " << aaaaaaaaaaaaaaaaaaaa;");
1147 verifyFormat(
1148 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1149 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1150 verifyFormat(
1151 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1152 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1153 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1154 verifyFormat(
1155 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1156 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1157 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1158}
1159
Daniel Jasperbac016b2012-12-03 18:12:45 +00001160TEST_F(FormatTest, UnderstandsEquals) {
1161 verifyFormat(
1162 "aaaaaaaaaaaaaaaaa =\n"
1163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1164 verifyFormat(
1165 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001167 verifyFormat(
1168 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001169 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001170 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1172 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001173
Daniel Jasper9cda8002013-01-07 13:08:40 +00001174 verifyFormat(
Daniel Jasper7006e7e2013-01-23 12:27:43 +00001175 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
1176 " 100000000 + 10000000) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001177}
1178
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001179TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001180 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1181 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001182
Daniel Jasper1321eb52012-12-18 21:05:13 +00001183 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1184 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001185
1186 verifyFormat(
1187 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1188 " Parameter2);");
1189
1190 verifyFormat(
1191 "ShortObject->shortFunction(\n"
1192 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1193 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1194
1195 verifyFormat("loooooooooooooongFunction(\n"
1196 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1197
1198 verifyFormat(
1199 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1200 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1201
Daniel Jasper46a46a22013-01-07 07:13:20 +00001202 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001203 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001204 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001205 verifyFormat(
1206 "aaaaaaaaaaa->aaaaaaaaa(\n"
1207 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1208 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001209}
1210
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001211TEST_F(FormatTest, WrapsTemplateDeclarations) {
1212 verifyFormat("template <typename T>\n"
1213 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1214 verifyFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +00001215 "template <typename T>\n"
1216 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1217 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001218 verifyFormat(
1219 "template <typename T>\n"
1220 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1221 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001222 verifyFormat(
1223 "template <typename T>\n"
1224 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1225 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001227 verifyFormat("template <typename T>\n"
1228 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1229 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001230 verifyFormat(
1231 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1232 " typename T4 = char>\n"
1233 "void f();");
Daniel Jasper3499dda2013-01-25 15:43:32 +00001234 verifyFormat(
1235 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
1236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001237}
1238
Daniel Jasper63f00362013-01-25 10:57:27 +00001239TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
1240 verifyFormat(
1241 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1242 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
1243 verifyFormat(
1244 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
1247
1248 // FIXME: Should we have an extra indent after the second break?
1249 verifyFormat(
1250 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
1253
1254 // FIXME: Look into whether we should indent 4 from the start or 4 from
1255 // "bbbbb..." here instead of what we are doing now.
1256 verifyFormat(
1257 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
Daniel Jasper5f2173e2013-01-28 07:43:15 +00001258 " cccccccccccccccccccccccccccccccccccccccccccccc());");
Daniel Jasper63f00362013-01-25 10:57:27 +00001259
1260 // Breaking at nested name specifiers is generally not desirable.
1261 verifyFormat(
1262 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1263 " aaaaaaaaaaaaaaaaaaaaaaa);");
1264}
1265
Daniel Jasperbac016b2012-12-03 18:12:45 +00001266TEST_F(FormatTest, UnderstandsTemplateParameters) {
1267 verifyFormat("A<int> a;");
1268 verifyFormat("A<A<A<int> > > a;");
1269 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1270 verifyFormat("bool x = a < 1 || 2 > a;");
1271 verifyFormat("bool x = 5 < f<int>();");
1272 verifyFormat("bool x = f<int>() > 5;");
1273 verifyFormat("bool x = 5 < a<int>::x;");
1274 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1275 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1276
1277 verifyGoogleFormat("A<A<int>> a;");
1278 verifyGoogleFormat("A<A<A<int>>> a;");
1279 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1280
1281 verifyFormat("test >> a >> b;");
1282 verifyFormat("test << a >> b;");
1283
1284 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001285 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001286}
1287
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001288TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001289 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001290 verifyFormat("f(-1, -2, -3);");
1291 verifyFormat("a[-1] = 5;");
1292 verifyFormat("int a = 5 + -2;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001293 verifyFormat("if (i == -1) {\n}");
1294 verifyFormat("if (i != -1) {\n}");
1295 verifyFormat("if (i > -1) {\n}");
1296 verifyFormat("if (i < -1) {\n}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001297 verifyFormat("++(a->f());");
1298 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001299 verifyFormat("(a->f())++;");
1300 verifyFormat("a[42]++;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001301 verifyFormat("if (!(a->f())) {\n}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001302
1303 verifyFormat("a-- > b;");
1304 verifyFormat("b ? -a : c;");
1305 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001306 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001307 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001308 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001309
1310 verifyFormat("return -1;");
1311 verifyFormat("switch (a) {\n"
1312 "case -1:\n"
1313 " break;\n"
1314 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001315
1316 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1317 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001318
1319 verifyFormat("int a = /* confusing comment */ -1;");
1320 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1321 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001322}
1323
1324TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001325 verifyFormat("bool operator<();");
1326 verifyFormat("bool operator>();");
1327 verifyFormat("bool operator=();");
1328 verifyFormat("bool operator==();");
1329 verifyFormat("bool operator!=();");
1330 verifyFormat("int operator+();");
1331 verifyFormat("int operator++();");
1332 verifyFormat("bool operator();");
1333 verifyFormat("bool operator()();");
1334 verifyFormat("bool operator[]();");
1335 verifyFormat("operator bool();");
1336 verifyFormat("operator SomeType<int>();");
1337 verifyFormat("void *operator new(std::size_t size);");
1338 verifyFormat("void *operator new[](std::size_t size);");
1339 verifyFormat("void operator delete(void *ptr);");
1340 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001341}
1342
Daniel Jasper088dab52013-01-11 16:09:04 +00001343TEST_F(FormatTest, UnderstandsNewAndDelete) {
1344 verifyFormat("A *a = new A;");
1345 verifyFormat("A *a = new (placement) A;");
1346 verifyFormat("delete a;");
1347 verifyFormat("delete (A *)a;");
1348}
1349
Daniel Jasper5d334402013-01-02 08:57:10 +00001350TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001351 verifyFormat("int *f(int *a) {}");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001352 verifyFormat("int main(int argc, char **argv) {}");
Daniel Jasper4bfc65a2013-01-23 12:10:53 +00001353 verifyIndependentOfContext("f(a, *a);");
1354 verifyIndependentOfContext("f(*a);");
1355 verifyIndependentOfContext("int a = b * 10;");
1356 verifyIndependentOfContext("int a = 10 * b;");
1357 verifyIndependentOfContext("int a = b * c;");
1358 verifyIndependentOfContext("int a += b * c;");
1359 verifyIndependentOfContext("int a -= b * c;");
1360 verifyIndependentOfContext("int a *= b * c;");
1361 verifyIndependentOfContext("int a /= b * c;");
1362 verifyIndependentOfContext("int a = *b;");
1363 verifyIndependentOfContext("int a = *b * c;");
1364 verifyIndependentOfContext("int a = b * *c;");
1365 verifyIndependentOfContext("return 10 * b;");
1366 verifyIndependentOfContext("return *b * *c;");
1367 verifyIndependentOfContext("return a & ~b;");
1368 verifyIndependentOfContext("f(b ? *c : *d);");
1369 verifyIndependentOfContext("int a = b ? *c : *d;");
1370 verifyIndependentOfContext("*b = a;");
1371 verifyIndependentOfContext("a * ~b;");
1372 verifyIndependentOfContext("a * !b;");
1373 verifyIndependentOfContext("a * +b;");
1374 verifyIndependentOfContext("a * -b;");
1375 verifyIndependentOfContext("a * ++b;");
1376 verifyIndependentOfContext("a * --b;");
1377 verifyIndependentOfContext("a[4] * b;");
1378 verifyIndependentOfContext("f() * b;");
1379 verifyIndependentOfContext("a * [self dostuff];");
1380 verifyIndependentOfContext("a * (a + b);");
1381 verifyIndependentOfContext("(a *)(a + b);");
1382 verifyIndependentOfContext("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001383
Daniel Jasper4bfc65a2013-01-23 12:10:53 +00001384 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001385
Daniel Jasper4bfc65a2013-01-23 12:10:53 +00001386 verifyIndependentOfContext("A<int *> a;");
1387 verifyIndependentOfContext("A<int **> a;");
1388 verifyIndependentOfContext("A<int *, int *> a;");
1389 verifyIndependentOfContext("A<int **, int **> a;");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001390
Daniel Jasper2db356d2013-01-08 20:03:18 +00001391 verifyFormat(
1392 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1393 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1394
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001395 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001396 verifyGoogleFormat("A<int*> a;");
1397 verifyGoogleFormat("A<int**> a;");
1398 verifyGoogleFormat("A<int*, int*> a;");
1399 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001400 verifyGoogleFormat("f(b ? *c : *d);");
1401 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001402 verifyGoogleFormat("Type* t = **x;");
1403 verifyGoogleFormat("Type* t = *++*x;");
1404 verifyGoogleFormat("*++*x;");
1405 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1406 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001407
Daniel Jasper4bfc65a2013-01-23 12:10:53 +00001408 verifyIndependentOfContext("a = *(x + y);");
1409 verifyIndependentOfContext("a = &(x + y);");
1410 verifyIndependentOfContext("*(x + y).call();");
1411 verifyIndependentOfContext("&(x + y)->call();");
1412 verifyIndependentOfContext("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001413
Daniel Jasper4bfc65a2013-01-23 12:10:53 +00001414 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001415 verifyFormat(
1416 "int *MyValues = {\n"
1417 " *A, // Operator detection might be confused by the '{'\n"
1418 " *BB // Operator detection might be confused by previous comment\n"
1419 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001420
Daniel Jasper4bfc65a2013-01-23 12:10:53 +00001421 verifyIndependentOfContext("if (int *a = &b)");
1422 verifyIndependentOfContext("if (int &a = *b)");
1423 verifyIndependentOfContext("if (a & b[i])");
1424 verifyIndependentOfContext("if (a::b::c::d & b[i])");
1425 verifyIndependentOfContext("if (*b[i])");
1426 verifyIndependentOfContext("if (int *a = (&b))");
1427 verifyIndependentOfContext("while (int *a = &b)");
Daniel Jasper20d35832013-01-23 12:58:14 +00001428 verifyFormat("void f() {\n"
1429 " for (const int &v : Values) {\n"
1430 " }\n"
1431 "}");
Daniel Jasperffee1712013-01-22 11:46:26 +00001432
Daniel Jasper4bfc65a2013-01-23 12:10:53 +00001433 verifyIndependentOfContext("A = new SomeType *[Length]();");
Daniel Jasperffee1712013-01-22 11:46:26 +00001434 verifyGoogleFormat("A = new SomeType* [Length]();");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001435}
1436
Manuel Klimeka32a7fd2013-01-23 14:08:21 +00001437TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
1438 verifyFormat("void f() {\n"
1439 " x[aaaaaaaaa -\n"
1440 " b] = 23;\n"
1441 "}", getLLVMStyleWithColumns(15));
1442}
1443
Daniel Jasper4981bd02013-01-13 08:01:36 +00001444TEST_F(FormatTest, FormatsCasts) {
1445 verifyFormat("Type *A = static_cast<Type *>(P);");
1446 verifyFormat("Type *A = (Type *)P;");
1447 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1448 verifyFormat("int a = (int)(2.0f);");
1449
1450 // FIXME: These also need to be identified.
1451 verifyFormat("int a = (int) 2.0f;");
1452 verifyFormat("int a = (int) * b;");
1453
1454 // These are not casts.
1455 verifyFormat("void f(int *) {}");
1456 verifyFormat("void f(int *);");
1457 verifyFormat("void f(int *) = 0;");
1458 verifyFormat("void f(SmallVector<int>) {}");
1459 verifyFormat("void f(SmallVector<int>);");
1460 verifyFormat("void f(SmallVector<int>) = 0;");
1461}
1462
Daniel Jasper46ef8522013-01-10 13:08:12 +00001463TEST_F(FormatTest, FormatsFunctionTypes) {
1464 // FIXME: Determine the cases that need a space after the return type and fix.
1465 verifyFormat("A<bool()> a;");
1466 verifyFormat("A<SomeType()> a;");
1467 verifyFormat("A<void(*)(int, std::string)> a;");
1468
1469 verifyFormat("int(*func)(void *);");
1470}
1471
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001472TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001473 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001474 " int LoooooooooooooooongParam2) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001475 verifyFormat(
1476 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1477 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001478 " Type *T) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001479}
1480
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001481TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1482 verifyFormat("(a)->b();");
1483 verifyFormat("--a;");
1484}
1485
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001486TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001487 verifyFormat("#include <string>\n"
1488 "#include <a/b/c.h>\n"
1489 "#include \"a/b/string\"\n"
1490 "#include \"string.h\"\n"
1491 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001492 "#include <a-a>\n"
1493 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001494
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001495 verifyFormat("#import <string>");
1496 verifyFormat("#import <a/b/c.h>");
1497 verifyFormat("#import \"a/b/string\"");
1498 verifyFormat("#import \"string.h\"");
1499 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001500}
1501
Alexander Kornienko15757312012-12-06 18:03:27 +00001502//===----------------------------------------------------------------------===//
1503// Error recovery tests.
1504//===----------------------------------------------------------------------===//
1505
Daniel Jasper700e7102013-01-10 09:26:47 +00001506TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001507 verifyFormat("void f() { return; }\n42");
1508 verifyFormat("void f() {\n"
1509 " if (0)\n"
1510 " return;\n"
1511 "}\n"
1512 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001513 verifyFormat("void f() { return }\n42");
1514 verifyFormat("void f() {\n"
1515 " if (0)\n"
1516 " return\n"
1517 "}\n"
1518 "42");
1519}
1520
1521TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1522 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1523 EXPECT_EQ("void f() {\n"
1524 " if (a)\n"
1525 " return\n"
1526 "}", format("void f ( ) { if ( a ) return }"));
1527 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1528 EXPECT_EQ("namespace N {\n"
1529 "void f() {}\n"
1530 "void g()\n"
1531 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001532}
1533
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001534TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1535 verifyFormat("int aaaaaaaa =\n"
1536 " // Overly long comment\n"
1537 " b;", getLLVMStyleWithColumns(20));
1538 verifyFormat("function(\n"
1539 " ShortArgument,\n"
1540 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1541}
1542
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001543TEST_F(FormatTest, IncorrectAccessSpecifier) {
1544 verifyFormat("public:");
1545 verifyFormat("class A {\n"
1546 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001547 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001548 "};");
1549 verifyFormat("public\n"
1550 "int qwerty;");
1551 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001552 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001553 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001554 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001555 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001556 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001557}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001558
Alexander Kornienko393b0082012-12-04 15:40:36 +00001559TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1560 verifyFormat("{");
1561}
1562
1563TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001564 verifyFormat("do {\n}");
1565 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001566 "f();");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001567 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001568 "wheeee(fun);");
1569 verifyFormat("do {\n"
1570 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001571 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001572}
1573
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001574TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001575 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001576 verifyFormat("switch {\n foo;\n foo();\n}");
1577 verifyFormat("for {\n foo;\n foo();\n}");
1578 verifyFormat("while {\n foo;\n foo();\n}");
1579 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001580}
1581
Daniel Jasper1f42f112013-01-04 18:52:56 +00001582TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1583 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001584 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001585}
1586
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001587TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001588 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1589 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1590 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1591 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001592
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001593 EXPECT_EQ("{\n"
1594 " {\n"
1595 " breakme(\n"
1596 " qwe);\n"
1597 "}\n", format("{\n"
1598 " {\n"
1599 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001600 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001601}
1602
Manuel Klimek2851c162013-01-10 14:36:46 +00001603TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1604 verifyFormat(
1605 "int x = {\n"
1606 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001607 " b(alongervariable)\n"
1608 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001609}
1610
Manuel Klimekc44ee892013-01-21 10:07:49 +00001611TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
1612 verifyFormat("return (a)(b) { 1, 2, 3 };");
1613}
1614
Manuel Klimek2851c162013-01-10 14:36:46 +00001615TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1616 verifyFormat(
1617 "Aaa({\n"
1618 " int i;\n"
1619 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1620 " ccccccccccccccccc));");
1621}
1622
Manuel Klimek517e8942013-01-11 17:54:10 +00001623TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1624 verifyFormat("void f() { return 42; }");
1625 verifyFormat("void f() {\n"
1626 " // Comment\n"
1627 "}");
1628 verifyFormat("{\n"
1629 "#error {\n"
1630 " int a;\n"
1631 "}");
1632 verifyFormat("{\n"
1633 " int a;\n"
1634 "#error {\n"
1635 "}");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001636
1637 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
1638 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
1639
1640 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
1641 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
Manuel Klimek517e8942013-01-11 17:54:10 +00001642}
1643
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001644TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1645 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001646 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001647 verifyFormat("class foo a = { bar };\nint n;");
1648 verifyFormat("union foo a = { bar };\nint n;");
1649
1650 // Elaborate types inside function definitions.
1651 verifyFormat("struct foo f() {}\nint n;");
1652 verifyFormat("class foo f() {}\nint n;");
1653 verifyFormat("union foo f() {}\nint n;");
1654
1655 // Templates.
1656 verifyFormat("template <class X> void f() {}\nint n;");
1657 verifyFormat("template <struct X> void f() {}\nint n;");
1658 verifyFormat("template <union X> void f() {}\nint n;");
1659
1660 // Actual definitions...
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001661 verifyFormat("struct {\n} n;");
1662 verifyFormat(
1663 "template <template <class T, class Y>, class Z> class X {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001664 verifyFormat("union Z {\n int n;\n} x;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001665 verifyFormat("class MACRO Z {\n} n;");
1666 verifyFormat("class MACRO(X) Z {\n} n;");
1667 verifyFormat("class __attribute__(X) Z {\n} n;");
1668 verifyFormat("class __declspec(X) Z {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001669
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001670 // Redefinition from nested context:
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001671 verifyFormat("class A::B::C {\n} n;");
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001672
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001673 // Template definitions.
1674 // FIXME: This is still incorrectly handled at the formatter side.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001675 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001676
1677 // FIXME:
1678 // This now gets parsed incorrectly as class definition.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001679 // verifyFormat("class A<int> f() {\n}\nint n;");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001680
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001681 // Elaborate types where incorrectly parsing the structural element would
1682 // break the indent.
1683 verifyFormat("if (true)\n"
1684 " class X x;\n"
1685 "else\n"
1686 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001687}
1688
Manuel Klimek407a31a2013-01-15 15:50:27 +00001689TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1690 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1691 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1692 EXPECT_EQ("#error 1", format(" # error 1"));
1693 EXPECT_EQ("#warning 1", format(" # warning 1"));
1694}
1695
Manuel Klimek525fe162013-01-18 14:04:34 +00001696TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
1697 FormatStyle AllowsMergedIf = getGoogleStyle();
1698 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
1699 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
1700 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
Manuel Klimek4c128122013-01-18 14:46:43 +00001701 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
1702 EXPECT_EQ("if (true) return 42;",
1703 format("if (true)\nreturn 42;", AllowsMergedIf));
1704 FormatStyle ShortMergedIf = AllowsMergedIf;
1705 ShortMergedIf.ColumnLimit = 25;
1706 verifyFormat("#define A \\\n"
1707 " if (true) return 42;", ShortMergedIf);
1708 verifyFormat("#define A \\\n"
1709 " f(); \\\n"
1710 " if (true)\n"
1711 "#define B", ShortMergedIf);
1712 verifyFormat("#define A \\\n"
1713 " f(); \\\n"
1714 " if (true)\n"
1715 "g();", ShortMergedIf);
Manuel Klimek0fbe0082013-01-21 14:16:56 +00001716 verifyFormat("{\n"
1717 "#ifdef A\n"
1718 " // Comment\n"
1719 " if (true) continue;\n"
1720 "#endif\n"
1721 " // Comment\n"
1722 " if (true) continue;", ShortMergedIf);
Manuel Klimek525fe162013-01-18 14:04:34 +00001723}
1724
Manuel Klimek86721d22013-01-22 16:31:55 +00001725TEST_F(FormatTest, BlockCommentsInControlLoops) {
1726 verifyFormat("if (0) /* a comment in a strange place */ {\n"
1727 " f();\n"
1728 "}");
1729 verifyFormat("if (0) /* a comment in a strange place */ {\n"
1730 " f();\n"
1731 "} /* another comment */ else /* comment #3 */ {\n"
1732 " g();\n"
1733 "}");
1734 verifyFormat("while (0) /* a comment in a strange place */ {\n"
1735 " f();\n"
1736 "}");
1737 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
1738 " f();\n"
1739 "}");
1740 verifyFormat("do /* a comment in a strange place */ {\n"
1741 " f();\n"
1742 "} /* another comment */ while (0);");
1743}
1744
1745TEST_F(FormatTest, BlockComments) {
1746 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
1747 format("/* *//* */ /* */\n/* *//* */ /* */"));
1748 EXPECT_EQ("/* */ a /* */ b;",
1749 format(" /* */ a/* */ b;"));
1750 EXPECT_EQ("#define A /* */\\\n"
1751 " b\n"
1752 "/* */\n"
1753 "someCall(\n"
1754 " parameter);",
1755 format("#define A /* */ b\n"
1756 "/* */\n"
1757 "someCall(parameter);", getLLVMStyleWithColumns(15)));
1758
1759 EXPECT_EQ("#define A\n"
1760 "/* */ someCall(\n"
1761 " parameter);",
1762 format("#define A\n"
1763 "/* */someCall(parameter);", getLLVMStyleWithColumns(15)));
1764}
1765
Manuel Klimek092a2c72013-01-23 10:09:28 +00001766TEST_F(FormatTest, FormatStarDependingOnContext) {
Manuel Klimek70b03f42013-01-23 09:32:48 +00001767 verifyFormat("void f(int *a);");
1768 verifyFormat("void f() { f(fint * b); }");
Manuel Klimek836b58f2013-01-23 11:03:04 +00001769 verifyFormat("class A {\n void f(int *a);\n};");
1770 verifyFormat("class A {\n int *a;\n};");
1771 verifyFormat("namespace a {\n"
1772 "namespace b {\n"
1773 "class A {\n"
1774 " void f() {}\n"
1775 " int *a;\n"
1776 "};\n"
1777 "}\n"
1778 "}");
Manuel Klimek70b03f42013-01-23 09:32:48 +00001779}
1780
Manuel Klimek092a2c72013-01-23 10:09:28 +00001781TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
1782 verifyFormat("while");
1783 verifyFormat("operator");
1784}
1785
Nico Webercf4a79c2013-01-08 17:56:31 +00001786//===----------------------------------------------------------------------===//
1787// Objective-C tests.
1788//===----------------------------------------------------------------------===//
1789
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001790TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1791 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1792 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1793 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001794 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001795 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1796 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1797 format("-(NSInteger)Method3:(id)anObject;"));
1798 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1799 format("-(NSInteger)Method4:(id)anObject;"));
1800 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1801 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1802 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1803 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001804 EXPECT_EQ(
1805 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1806 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001807
1808 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001809 EXPECT_EQ(
1810 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1811 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1812 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1813 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1814 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1815 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1816 format(
1817 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1818 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1819 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1820 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1821 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1822 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001823
1824 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001825 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001826 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1827 // protocol lists (but not for template classes):
1828 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001829
1830 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001831 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001832
1833 // If there's no return type (very rare in practice!), LLVM and Google style
1834 // agree.
1835 verifyFormat("- foo:(int)f;");
1836 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001837}
1838
Daniel Jasper886568d2013-01-09 08:36:49 +00001839TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001840 verifyFormat("int (^Block)(int, int);");
1841 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001842}
1843
Nico Weber27d13672013-01-09 20:25:35 +00001844TEST_F(FormatTest, FormatObjCInterface) {
Nico Weber5f500df2013-01-10 20:12:55 +00001845 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001846 "@public\n"
1847 " int field1;\n"
1848 "@protected\n"
1849 " int field2;\n"
1850 "@private\n"
1851 " int field3;\n"
1852 "@package\n"
1853 " int field4;\n"
1854 "}\n"
1855 "+ (id)init;\n"
1856 "@end");
1857
Nico Weber27d13672013-01-09 20:25:35 +00001858 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1859 " @public\n"
1860 " int field1;\n"
1861 " @protected\n"
1862 " int field2;\n"
1863 " @private\n"
1864 " int field3;\n"
1865 " @package\n"
1866 " int field4;\n"
1867 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001868 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001869 "@end");
1870
Nico Weberf5ecfa52013-01-22 16:53:59 +00001871 verifyFormat("@interface /* wait for it */ Foo\n"
Nico Weber27d13672013-01-09 20:25:35 +00001872 "+ (id)init;\n"
1873 "// Look, a comment!\n"
1874 "- (int)answerWith:(int)i;\n"
1875 "@end");
1876
1877 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001878 "@end\n"
1879 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001880 "@end");
1881
1882 verifyFormat("@interface Foo : Bar\n"
1883 "+ (id)init;\n"
1884 "@end");
1885
Nico Weberf5ecfa52013-01-22 16:53:59 +00001886 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001887 "+ (id)init;\n"
1888 "@end");
1889
Nico Weber5f500df2013-01-10 20:12:55 +00001890 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001891 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001892 "@end");
1893
Nico Webered91bba2013-01-10 19:19:14 +00001894 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001895 "+ (id)init;\n"
1896 "@end");
1897
Nico Webered91bba2013-01-10 19:19:14 +00001898 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001899 "+ (id)init;\n"
1900 "@end");
1901
Nico Weber5f500df2013-01-10 20:12:55 +00001902 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001903 "+ (id)init;\n"
1904 "@end");
1905
Nico Weber5f500df2013-01-10 20:12:55 +00001906 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001907 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001908 "@end");
1909
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001910 verifyFormat("@interface Foo {\n"
1911 " int _i;\n"
1912 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001913 "+ (id)init;\n"
1914 "@end");
1915
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001916 verifyFormat("@interface Foo : Bar {\n"
1917 " int _i;\n"
1918 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001919 "+ (id)init;\n"
1920 "@end");
1921
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001922 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1923 " int _i;\n"
1924 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001925 "+ (id)init;\n"
1926 "@end");
1927
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001928 verifyFormat("@interface Foo (HackStuff) {\n"
1929 " int _i;\n"
1930 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001931 "+ (id)init;\n"
1932 "@end");
1933
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001934 verifyFormat("@interface Foo () {\n"
1935 " int _i;\n"
1936 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001937 "+ (id)init;\n"
1938 "@end");
1939
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001940 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1941 " int _i;\n"
1942 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001943 "+ (id)init;\n"
1944 "@end");
1945}
1946
Nico Weber50767d82013-01-09 23:25:37 +00001947TEST_F(FormatTest, FormatObjCImplementation) {
1948 verifyFormat("@implementation Foo : NSObject {\n"
1949 "@public\n"
1950 " int field1;\n"
1951 "@protected\n"
1952 " int field2;\n"
1953 "@private\n"
1954 " int field3;\n"
1955 "@package\n"
1956 " int field4;\n"
1957 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001958 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001959 "@end");
1960
1961 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1962 " @public\n"
1963 " int field1;\n"
1964 " @protected\n"
1965 " int field2;\n"
1966 " @private\n"
1967 " int field3;\n"
1968 " @package\n"
1969 " int field4;\n"
1970 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001971 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001972 "@end");
1973
1974 verifyFormat("@implementation Foo\n"
1975 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001976 " if (true)\n"
1977 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001978 "}\n"
1979 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001980 "- (int)answerWith:(int)i {\n"
1981 " return i;\n"
1982 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001983 "+ (int)answerWith:(int)i {\n"
1984 " return i;\n"
1985 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001986 "@end");
1987
1988 verifyFormat("@implementation Foo\n"
1989 "@end\n"
1990 "@implementation Bar\n"
1991 "@end");
1992
1993 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001994 "+ (id)init {\n}\n"
1995 "- (void)foo {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001996 "@end");
1997
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001998 verifyFormat("@implementation Foo {\n"
1999 " int _i;\n"
2000 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00002001 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00002002 "@end");
2003
Manuel Klimekd5688cf2013-01-11 19:17:44 +00002004 verifyFormat("@implementation Foo : Bar {\n"
2005 " int _i;\n"
2006 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00002007 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00002008 "@end");
2009
Nico Webered91bba2013-01-10 19:19:14 +00002010 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00002011 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00002012 "@end");
2013}
2014
Nico Weber1abe6ea2013-01-09 21:15:03 +00002015TEST_F(FormatTest, FormatObjCProtocol) {
2016 verifyFormat("@protocol Foo\n"
2017 "@property(weak) id delegate;\n"
2018 "- (NSUInteger)numberOfThings;\n"
2019 "@end");
2020
Nico Weber5f500df2013-01-10 20:12:55 +00002021 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00002022 "- (NSUInteger)numberOfThings;\n"
2023 "@end");
2024
Nico Weber5f500df2013-01-10 20:12:55 +00002025 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00002026 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00002027 "@end");
2028
Nico Weber1abe6ea2013-01-09 21:15:03 +00002029 verifyFormat("@protocol Foo;\n"
2030 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00002031
2032 verifyFormat("@protocol Foo\n"
2033 "@end\n"
2034 "@protocol Bar\n"
2035 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00002036
2037 verifyFormat("@protocol myProtocol\n"
2038 "- (void)mandatoryWithInt:(int)i;\n"
2039 "@optional\n"
2040 "- (void)optional;\n"
2041 "@required\n"
2042 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00002043 "@optional\n"
2044 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00002045 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00002046}
2047
Nico Weberbcfdd262013-01-12 06:18:40 +00002048TEST_F(FormatTest, FormatObjCMethodExpr) {
2049 verifyFormat("[foo bar:baz];");
2050 verifyFormat("return [foo bar:baz];");
2051 verifyFormat("f([foo bar:baz]);");
2052 verifyFormat("f(2, [foo bar:baz]);");
2053 verifyFormat("f(2, a ? b : c);");
2054 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
2055
2056 verifyFormat("[foo bar:baz], [foo bar:baz];");
2057 verifyFormat("[foo bar:baz] = [foo bar:baz];");
2058 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
2059 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
2060 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
2061 verifyFormat("[foo bar:baz] += [foo bar:baz];");
2062 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
2063 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
2064 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
2065 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
2066 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
2067 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
2068 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
2069 verifyFormat("[foo bar:baz] || [foo bar:baz];");
2070 verifyFormat("[foo bar:baz] && [foo bar:baz];");
2071 verifyFormat("[foo bar:baz] | [foo bar:baz];");
2072 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
2073 verifyFormat("[foo bar:baz] & [foo bar:baz];");
2074 verifyFormat("[foo bar:baz] == [foo bar:baz];");
2075 verifyFormat("[foo bar:baz] != [foo bar:baz];");
2076 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
2077 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
2078 verifyFormat("[foo bar:baz] > [foo bar:baz];");
2079 verifyFormat("[foo bar:baz] < [foo bar:baz];");
2080 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
2081 verifyFormat("[foo bar:baz] << [foo bar:baz];");
2082 verifyFormat("[foo bar:baz] - [foo bar:baz];");
2083 verifyFormat("[foo bar:baz] + [foo bar:baz];");
2084 verifyFormat("[foo bar:baz] * [foo bar:baz];");
2085 verifyFormat("[foo bar:baz] / [foo bar:baz];");
2086 verifyFormat("[foo bar:baz] % [foo bar:baz];");
2087 // Whew!
2088
2089 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
2090 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
2091 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
2092 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
2093 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00002094 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00002095 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00002096
Nico Weberbcfdd262013-01-12 06:18:40 +00002097 verifyFormat("arr[[self indexForFoo:a]];");
2098 verifyFormat("throw [self errorFor:a];");
2099 verifyFormat("@throw [self errorFor:a];");
2100
Nico Webere8ccc812013-01-12 22:48:47 +00002101 // This tests that the formatter doesn't break after "backing" but before ":",
2102 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00002103 verifyFormat(
2104 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00002105 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
Daniel Jasper3499dda2013-01-25 15:43:32 +00002106 " backing:NSBackingStoreBuffered defer:YES]))");
Nico Webere8ccc812013-01-12 22:48:47 +00002107
Nico Webere8ccc812013-01-12 22:48:47 +00002108 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
2109 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00002110
2111}
2112
Nico Weber581f5572013-01-07 15:56:25 +00002113TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00002114 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00002115 verifyFormat("@catch");
2116 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00002117 verifyFormat("@compatibility_alias");
2118 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00002119 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00002120 verifyFormat("@encode");
2121 verifyFormat("@end");
2122 verifyFormat("@finally");
2123 verifyFormat("@implementation");
2124 verifyFormat("@import");
2125 verifyFormat("@interface");
2126 verifyFormat("@optional");
2127 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00002128 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00002129 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00002130 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00002131 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00002132 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00002133 verifyFormat("@required");
2134 verifyFormat("@selector");
2135 verifyFormat("@synchronized");
2136 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00002137 verifyFormat("@throw");
2138 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00002139
Nico Webercb4d6902013-01-08 19:40:21 +00002140 verifyFormat("@\"String\"");
2141 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00002142 verifyFormat("@+4.8");
2143 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00002144 verifyFormat("@1LL");
2145 verifyFormat("@.5");
2146 verifyFormat("@'c'");
2147 verifyFormat("@true");
2148 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00002149 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00002150 verifyFormat("@[");
2151 verifyFormat("@{");
2152
Nico Weber581f5572013-01-07 15:56:25 +00002153 EXPECT_EQ("@interface", format("@ interface"));
2154
2155 // The precise formatting of this doesn't matter, nobody writes code like
2156 // this.
2157 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00002158}
2159
Nico Weberc31689a2013-01-08 19:15:23 +00002160TEST_F(FormatTest, ObjCSnippets) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +00002161 verifyFormat("@autoreleasepool {\n"
2162 " foo();\n"
2163 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00002164 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00002165 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00002166 verifyFormat("@dynamic textColor;");
Nico Weber5dfe9b42013-01-18 05:11:47 +00002167 verifyFormat("char *buf1 = @encode(int *);");
2168 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
2169 // FIXME: Enable once PR14884 is fixed:
Nico Weberc31689a2013-01-08 19:15:23 +00002170 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00002171 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weber6a21a552013-01-18 02:43:57 +00002172 verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00002173 verifyFormat("@synchronized(self) {\n"
2174 " f();\n"
2175 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00002176
Nico Weber70848232013-01-10 21:30:42 +00002177 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2178 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2179
Nico Webercf4a79c2013-01-08 17:56:31 +00002180 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00002181 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
2182 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00002183}
2184
Daniel Jaspercd162382013-01-07 13:26:07 +00002185} // end namespace tooling
2186} // end namespace clang