blob: 0c6385b7a16eabdae94fefae050fbded59dbc07e [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 }
103};
104
Manuel Klimek526ed112013-01-09 15:25:02 +0000105TEST_F(FormatTest, MessUp) {
106 EXPECT_EQ("1 2 3", messUp("1 2 3"));
107 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
108 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
109 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
110 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
111}
112
Alexander Kornienko15757312012-12-06 18:03:27 +0000113//===----------------------------------------------------------------------===//
114// Basic function tests.
115//===----------------------------------------------------------------------===//
116
Daniel Jasperbac016b2012-12-03 18:12:45 +0000117TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
118 EXPECT_EQ(";", format(";"));
119}
120
121TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
122 EXPECT_EQ("int i;", format(" int i;"));
123 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
124 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
125 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
126}
127
128TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
129 EXPECT_EQ("int i;", format("int\ni;"));
130}
131
132TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000133 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000134}
135
Alexander Kornienko15757312012-12-06 18:03:27 +0000136TEST_F(FormatTest, FormatsNestedCall) {
137 verifyFormat("Method(f1, f2(f3));");
138 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper4e9008a2013-01-13 08:19:51 +0000139 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000140}
141
Daniel Jasper6b825c22013-01-16 07:19:28 +0000142TEST_F(FormatTest, ImportantSpaces) {
143 verifyFormat("vector< ::Type> v;");
144}
145
Alexander Kornienko15757312012-12-06 18:03:27 +0000146//===----------------------------------------------------------------------===//
147// Tests for control statements.
148//===----------------------------------------------------------------------===//
149
150TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000151 verifyFormat("if (true)\n f();\ng();");
152 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000153 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jasperdf3736a2013-01-16 15:44:34 +0000154
155 FormatStyle AllowsMergedIf = getGoogleStyle();
156 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
157 verifyFormat("if (a)\n"
158 " // comment\n"
159 " f();", AllowsMergedIf);
160
161 verifyFormat("if (a) // Can't merge this\n"
162 " f();\n", AllowsMergedIf);
163 verifyFormat("if (a) /* still don't merge */\n"
164 " f();", AllowsMergedIf);
165 verifyFormat("if (a) { // Never merge this\n"
166 " f();\n"
167 "}", AllowsMergedIf);
168 verifyFormat("if (a) { /* Never merge this */\n"
169 " f();\n"
170 "}", AllowsMergedIf);
171
172 AllowsMergedIf.ColumnLimit = 14;
173 verifyFormat("if (a) return;", AllowsMergedIf);
Daniel Jasperfd0ca972013-01-14 16:02:06 +0000174 verifyFormat("if (aaaaaaaaa)\n"
Daniel Jasperdf3736a2013-01-16 15:44:34 +0000175 " return;", AllowsMergedIf);
176
177 AllowsMergedIf.ColumnLimit = 13;
178 verifyFormat("if (a)\n return;", AllowsMergedIf);
Alexander Kornienko15757312012-12-06 18:03:27 +0000179}
180
181TEST_F(FormatTest, ParseIfElse) {
182 verifyFormat("if (true)\n"
183 " if (true)\n"
184 " if (true)\n"
185 " f();\n"
186 " else\n"
187 " g();\n"
188 " else\n"
189 " h();\n"
190 "else\n"
191 " i();");
192 verifyFormat("if (true)\n"
193 " if (true)\n"
194 " if (true) {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000195 " if (true)\n"
196 " f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000197 " } else {\n"
198 " g();\n"
199 " }\n"
200 " else\n"
201 " h();\n"
202 "else {\n"
203 " i();\n"
204 "}");
205}
206
207TEST_F(FormatTest, ElseIf) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000208 verifyFormat("if (a) {\n} else if (b) {\n}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000209 verifyFormat("if (a)\n"
210 " f();\n"
211 "else if (b)\n"
212 " g();\n"
213 "else\n"
214 " h();");
215}
216
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217TEST_F(FormatTest, FormatsForLoop) {
218 verifyFormat(
219 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000220 " ++VeryVeryLongLoopVariable)\n"
221 " ;");
222 verifyFormat("for (;;)\n"
223 " f();");
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000224 verifyFormat("for (;;) {\n}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000225 verifyFormat("for (;;) {\n"
226 " f();\n"
227 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000228
229 verifyFormat(
230 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
231 " E = UnwrappedLines.end();\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000232 " I != E; ++I) {\n}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000233
234 verifyFormat(
235 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000236 " ++IIIII) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000237}
238
239TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000240 verifyFormat("while (true) {\n}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000241 verifyFormat("while (true)\n"
242 " f();");
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000243 verifyFormat("while () {\n}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000244 verifyFormat("while () {\n"
245 " f();\n"
246 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000247}
248
Alexander Kornienko15757312012-12-06 18:03:27 +0000249TEST_F(FormatTest, FormatsDoWhile) {
250 verifyFormat("do {\n"
251 " do_something();\n"
252 "} while (something());");
253 verifyFormat("do\n"
254 " do_something();\n"
255 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000256}
257
Alexander Kornienko15757312012-12-06 18:03:27 +0000258TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000259 verifyFormat("switch (x) {\n"
260 "case 1:\n"
261 " f();\n"
262 " break;\n"
263 "case kFoo:\n"
264 "case ns::kBar:\n"
265 "case kBaz:\n"
266 " break;\n"
267 "default:\n"
268 " g();\n"
269 " break;\n"
270 "}");
271 verifyFormat("switch (x) {\n"
272 "case 1: {\n"
273 " f();\n"
274 " break;\n"
275 "}\n"
276 "}");
Nico Weber94fb7292013-01-18 05:50:57 +0000277 verifyFormat("switch (x) {\n"
278 "case 1: {\n"
279 " f();\n"
280 " {\n"
281 " g();\n"
282 " h();\n"
283 " }\n"
284 " break;\n"
285 "}\n"
286 "}");
287 verifyFormat("switch (x) {\n"
288 "case 1: {\n"
289 " f();\n"
290 " if (foo) {\n"
291 " g();\n"
292 " h();\n"
293 " }\n"
294 " break;\n"
295 "}\n"
296 "}");
297 verifyFormat("switch (x) {\n"
298 "case 1: {\n"
299 " f();\n"
300 " g();\n"
301 "} break;\n"
302 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000303 verifyFormat("switch (test)\n"
304 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000305 verifyGoogleFormat("switch (x) {\n"
306 " case 1:\n"
307 " f();\n"
308 " break;\n"
309 " case kFoo:\n"
310 " case ns::kBar:\n"
311 " case kBaz:\n"
312 " break;\n"
313 " default:\n"
314 " g();\n"
315 " break;\n"
316 "}");
317 verifyGoogleFormat("switch (x) {\n"
318 " case 1: {\n"
319 " f();\n"
320 " break;\n"
321 " }\n"
322 "}");
323 verifyGoogleFormat("switch (test)\n"
324 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000325}
326
Alexander Kornienko15757312012-12-06 18:03:27 +0000327TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000328 verifyFormat("void f() {\n"
329 " some_code();\n"
330 "test_label:\n"
331 " some_other_code();\n"
332 " {\n"
333 " some_more_code();\n"
334 " another_label:\n"
335 " some_more_code();\n"
336 " }\n"
337 "}");
338 verifyFormat("some_code();\n"
339 "test_label:\n"
340 "some_other_code();");
341}
342
Alexander Kornienko15757312012-12-06 18:03:27 +0000343//===----------------------------------------------------------------------===//
344// Tests for comments.
345//===----------------------------------------------------------------------===//
346
347TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000348 verifyFormat("// line 1\n"
349 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000350 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000351
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000352 verifyFormat("void f() {\n"
353 " // Doesn't do anything\n"
354 "}");
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000355 verifyFormat("void f(int i, // some comment (probably for i)\n"
356 " int j, // some comment (probably for j)\n"
Daniel Jasper487f64b2013-01-13 16:10:20 +0000357 " int k); // some comment (probably for k)");
358 verifyFormat("void f(int i,\n"
359 " // some comment (probably for j)\n"
360 " int j,\n"
361 " // some comment (probably for k)\n"
362 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000363
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000364 verifyFormat("int i // This is a fancy variable\n"
365 " = 5; // with nicely aligned comment.");
366
367 verifyFormat("// Leading comment.\n"
368 "int a; // Trailing comment.");
369 verifyFormat("int a; // Trailing comment\n"
370 " // on 2\n"
371 " // or 3 lines.\n"
372 "int b;");
373 verifyFormat("int a; // Trailing comment\n"
374 "\n"
375 "// Leading comment.\n"
376 "int b;");
377 verifyFormat("int a; // Comment.\n"
378 " // More details.\n"
379 "int bbbb; // Another comment.");
380 verifyFormat(
381 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
382 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
383 "int cccccccccccccccccccccccccccccc; // comment\n"
384 "int ddd; // looooooooooooooooooooooooong comment\n"
385 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
386 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
387 "int ccccccccccccccccccc; // comment");
388
Daniel Jasper7d1185d2013-01-18 09:19:33 +0000389 verifyFormat("#include \"a\" // comment\n"
390 "#include \"a/b/c\" // comment");
391 verifyFormat("#include <a> // comment\n"
392 "#include <a/b/c> // comment");
Alexander Kornienko15757312012-12-06 18:03:27 +0000393
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000394 verifyFormat("enum E {\n"
395 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000396 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000397 " VAL_B\n"
398 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000399
400 verifyFormat(
401 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000402 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000403 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
404 " // Comment inside a statement.\n"
405 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000406
Daniel Jasper821627e2013-01-21 22:49:20 +0000407 EXPECT_EQ("void f() { // This does something ..\n"
408 "}\n"
409 "int a; // This is unrelated",
410 format("void f() { // This does something ..\n"
411 " }\n"
412 "int a; // This is unrelated"));
413 EXPECT_EQ("void f() { // This does something ..\n"
414 "} // awesome..\n"
415 "\n"
416 "int a; // This is unrelated",
417 format("void f() { // This does something ..\n"
418 " } // awesome..\n"
419 " \n"
420 "int a; // This is unrelated"));
421
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000422 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000423 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000424
425 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000426}
427
428TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000429 verifyFormat("f(/*test=*/ true);");
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000430 EXPECT_EQ(
431 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
432 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
433 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n"
434 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
435 EXPECT_EQ(
436 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
437 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
438 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
439 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
440}
441
442TEST_F(FormatTest, CommentsInStaticInitializers) {
443 EXPECT_EQ(
444 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
445 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
446 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
447 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
448 " aaaaaaaaaaaaaaaaaaaa };",
449 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
450 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
451 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
452 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
453 " aaaaaaaaaaaaaaaaaaaa };"));
454 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
455 " bbbbbbbbbbb, ccccccccccc };");
456 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
457 " // comment for bb....\n"
458 " bbbbbbbbbbb, ccccccccccc };");
459 verifyGoogleFormat(
460 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
461 " bbbbbbbbbbb,\n"
462 " ccccccccccc };");
463 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
464 " // comment for bb....\n"
465 " bbbbbbbbbbb,\n"
466 " ccccccccccc };");
467
Alexander Kornienko15757312012-12-06 18:03:27 +0000468}
469
Alexander Kornienko15757312012-12-06 18:03:27 +0000470//===----------------------------------------------------------------------===//
471// Tests for classes, namespaces, etc.
472//===----------------------------------------------------------------------===//
473
474TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000475 verifyFormat("class A {\n};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000476}
477
478TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
479 verifyFormat("class A {\n"
480 "public:\n"
481 "protected:\n"
482 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000483 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000484 "};");
485 verifyGoogleFormat("class A {\n"
486 " public:\n"
487 " protected:\n"
488 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000489 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000490 "};");
491}
492
493TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000494 verifyFormat("class A : public B {\n};");
495 verifyFormat("class A : public ::B {\n};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000496}
497
Manuel Klimekde768542013-01-07 18:10:23 +0000498TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000499 verifyFormat("class A {\n} a, b;");
500 verifyFormat("struct A {\n} a, b;");
501 verifyFormat("union A {\n} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000502}
503
Alexander Kornienko15757312012-12-06 18:03:27 +0000504TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000505 verifyFormat("enum {\n"
506 " Zero,\n"
507 " One = 1,\n"
508 " Two = One + 1,\n"
509 " Three = (One + Two),\n"
510 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
511 " Five = (One, Two, Three, Four, 5)\n"
512 "};");
513 verifyFormat("enum Enum {\n"
514 "};");
515 verifyFormat("enum {\n"
516 "};");
Manuel Klimek308232c2013-01-21 19:17:52 +0000517 verifyFormat("enum X E {\n} d;");
518 verifyFormat("enum __attribute__((...)) E {\n} d;");
519 verifyFormat("enum __declspec__((...)) E {\n} d;");
520 verifyFormat("enum X f() {\n a();\n return 42;\n}");
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000521}
522
Nico Weberefaddc02013-01-14 05:49:49 +0000523TEST_F(FormatTest, FormatsBitfields) {
524 verifyFormat("struct Bitfields {\n"
525 " unsigned sClass : 8;\n"
526 " unsigned ValueKind : 2;\n"
527 "};");
528}
529
Alexander Kornienko15757312012-12-06 18:03:27 +0000530TEST_F(FormatTest, FormatsNamespaces) {
531 verifyFormat("namespace some_namespace {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000532 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000533 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000534 "}");
535 verifyFormat("namespace {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000536 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000537 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000538 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000539 verifyFormat("inline namespace X {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000540 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000541 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000542 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000543 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000544 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000545 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000546}
547
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000548TEST_F(FormatTest, FormatsExternC) {
549 verifyFormat("extern \"C\" {\nint a;");
550}
551
Nico Webera9ccdd12013-01-07 16:36:17 +0000552TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000553 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
554 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000555 verifyFormat("try {\n"
556 " throw a * b;\n"
557 "}\n"
558 "catch (int a) {\n"
559 " // Do nothing.\n"
560 "}\n"
561 "catch (...) {\n"
562 " exit(42);\n"
563 "}");
564
565 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000566 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000567 "catch (...) {\n"
568 " return 5;\n"
569 "}");
570 verifyFormat("class A {\n"
571 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000572 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000573 " catch (...) {\n"
574 " throw;\n"
575 " }\n"
576 "};\n");
577}
578
579TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000580 verifyFormat("@try {\n"
581 " f();\n"
582 "}\n"
583 "@catch (NSException e) {\n"
584 " @throw;\n"
585 "}\n"
586 "@finally {\n"
587 " exit(42);\n"
588 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000589}
590
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000591TEST_F(FormatTest, StaticInitializers) {
592 verifyFormat("static SomeClass SC = { 1, 'a' };");
593
594 // FIXME: Format like enums if the static initializer does not fit on a line.
595 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000596 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000597 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
598 "};");
599
600 verifyFormat(
601 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
602 " looooooooooooooooooooooooooooooooooongname,\n"
603 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000604}
605
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000606TEST_F(FormatTest, NestedStaticInitializers) {
607 verifyFormat("static A x = { { {} } };\n");
608 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000609 "static A x = { { { init1, init2, init3, init4 },\n"
610 " { init1, init2, init3, init4 } } };");
611
Nico Weber6a21a552013-01-18 02:43:57 +0000612 // FIXME: Fix this in general and verify that it works in LLVM style again.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000613 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000614 "somes Status::global_reps[3] = {\n"
615 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
616 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
617 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
618 "};");
619 verifyFormat(
620 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
621 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
622 " } };");
623
Nico Weber6a21a552013-01-18 02:43:57 +0000624 // FIXME: We might at some point want to handle this similar to parameter
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000625 // lists, where we have an option to put each on a single line.
626 verifyFormat("struct {\n"
627 " unsigned bit;\n"
628 " const char *const name;\n"
629 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
630 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
631}
632
Manuel Klimeka080a182013-01-02 16:30:12 +0000633TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
634 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
635 " \\\n"
636 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
637}
638
Daniel Jasper71607512013-01-07 10:48:50 +0000639TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000640 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
641 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000642}
643
Manuel Klimeka080a182013-01-02 16:30:12 +0000644TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
645 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000646 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000647}
648
649TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
650 EXPECT_EQ("#line 42 \"test\"\n",
651 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000652 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000653 format("# \\\n define \\\n A \\\n B\n",
654 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000655}
656
657TEST_F(FormatTest, EndOfFileEndsPPDirective) {
658 EXPECT_EQ("#line 42 \"test\"",
659 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000660 EXPECT_EQ("#define A B",
661 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000662}
663
Manuel Klimek060143e2013-01-02 18:33:23 +0000664TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000665 // If the macro fits in one line, we still do not get the full
666 // line, as only the next line decides whether we need an escaped newline and
667 // thus use the last column.
668 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000669
Manuel Klimekd544c572013-01-07 09:24:17 +0000670 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
671 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000672 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000673
674 verifyFormat("#define A A\n#define A A");
675 verifyFormat("#define A(X) A\n#define A A");
676
677 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
678 verifyFormat("#define Something \\\n"
679 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000680}
681
682TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000683 EXPECT_EQ("// some comment\n"
684 "#include \"a.h\"\n"
685 "#define A(A,\\\n"
686 " B)\n"
687 "#include \"b.h\"\n"
688 "// some comment\n",
689 format(" // some comment\n"
690 " #include \"a.h\"\n"
691 "#define A(A,\\\n"
692 " B)\n"
693 " #include \"b.h\"\n"
694 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000695}
696
Manuel Klimekd4397b92013-01-04 23:34:14 +0000697TEST_F(FormatTest, LayoutSingleHash) {
698 EXPECT_EQ("#\na;", format("#\na;"));
699}
700
701TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
702 EXPECT_EQ("#define A \\\n"
703 " c; \\\n"
704 " e;\n"
705 "f;", format("#define A c; e;\n"
706 "f;", getLLVMStyleWithColumns(14)));
707}
708
709TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000710 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000711}
712
713TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000714 EXPECT_EQ("# define A\\\n b;",
715 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000716}
717
718TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000719 EXPECT_EQ("int x,\n"
720 "#define A\n"
721 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000722}
723
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000724TEST_F(FormatTest, HashInMacroDefinition) {
725 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
726 verifyFormat("#define A \\\n"
727 " { \\\n"
728 " f(#c);\\\n"
729 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000730
731 verifyFormat("#define A(X) \\\n"
732 " void function##X()", getLLVMStyleWithColumns(22));
733
734 verifyFormat("#define A(a, b, c) \\\n"
735 " void a##b##c()", getLLVMStyleWithColumns(22));
736
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000737 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000738}
739
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000740TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
741 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
742}
743
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000744TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000745 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000746}
747
Manuel Klimeka5342db2013-01-06 20:07:31 +0000748TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
749 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
750 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
751 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
752 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
753}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000754
Manuel Klimek95419382013-01-07 07:56:50 +0000755TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000756 EXPECT_EQ(
757 "#define A \\\n int i; \\\n int j;",
758 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000759}
760
Manuel Klimekd544c572013-01-07 09:24:17 +0000761TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
762 verifyFormat("#define A \\\n"
763 " int v( \\\n"
764 " a); \\\n"
765 " int i;", getLLVMStyleWithColumns(11));
766}
767
Manuel Klimeka080a182013-01-02 16:30:12 +0000768TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000769 EXPECT_EQ(
770 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
771 " \\\n"
772 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
773 "\n"
774 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
775 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
776 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
777 "\\\n"
778 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
779 " \n"
780 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
781 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000782}
783
Manuel Klimek526ed112013-01-09 15:25:02 +0000784TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
785 EXPECT_EQ("int\n"
786 "#define A\n"
787 " a;",
788 format("int\n#define A\na;"));
789 verifyFormat(
790 "functionCallTo(someOtherFunction(\n"
791 " withSomeParameters, whichInSequence,\n"
792 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000793 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000794 " withMoreParamters,\n"
795 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000796 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000797}
798
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000799TEST_F(FormatTest, LayoutBlockInsideParens) {
800 EXPECT_EQ("functionCall({\n"
801 " int i;\n"
802 "});", format(" functionCall ( {int i;} );"));
803}
804
805TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000806 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000807 "int i;", format(" SOME_MACRO {int i;} int i;"));
808}
809
810TEST_F(FormatTest, LayoutNestedBlocks) {
811 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
812 " struct s {\n"
813 " int i;\n"
814 " };\n"
815 " s kBitsToOs[] = { { 10 } };\n"
816 " for (int i = 0; i < 10; ++i)\n"
817 " return;\n"
818 "}");
819}
820
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000821TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
822 EXPECT_EQ("{}", format("{}"));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000823
824 // Negative test for enum.
825 verifyFormat("enum E {\n};");
826
827 // Note that when there's a missing ';', we still join...
828 verifyFormat("enum E {}");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000829}
830
Alexander Kornienko15757312012-12-06 18:03:27 +0000831//===----------------------------------------------------------------------===//
832// Line break tests.
833//===----------------------------------------------------------------------===//
834
835TEST_F(FormatTest, FormatsFunctionDefinition) {
836 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
837 " int h, int j, int f,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000838 " int c, int ddddddddddddd) {\n}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000839}
840
841TEST_F(FormatTest, FormatsAwesomeMethodCall) {
842 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000843 "SomeLongMethodName(SomeReallyLongMethod(\n"
844 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
845 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000846}
847
Daniel Jasper1321eb52012-12-18 21:05:13 +0000848TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000849 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000850 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
851 getLLVMStyleWithColumns(45));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000852 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}",
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000853 getLLVMStyleWithColumns(44));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000854 verifyFormat("Constructor()\n"
855 " : Inttializer(FitsOnTheLine) {\n}",
856 getLLVMStyleWithColumns(43));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000857
858 verifyFormat(
859 "SomeClass::Constructor()\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000860 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000861
862 verifyFormat(
863 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000864 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000865 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000866 verifyGoogleFormat(
867 "SomeClass::Constructor()\n"
868 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
869 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000870 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper16618242013-01-16 17:00:50 +0000871 verifyGoogleFormat(
872 "SomeClass::Constructor()\n"
873 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
874 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000875 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000876
877 verifyFormat(
878 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000879 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000880 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000881
882 verifyFormat("Constructor()\n"
883 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
884 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
885 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000886 " aaaaaaaaaaaaaaaaaaaaaaa() {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000887
888 // Here a line could be saved by splitting the second initializer onto two
889 // lines, but that is not desireable.
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000890 verifyFormat(
891 "Constructor()\n"
892 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
893 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
894 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000895
896 verifyGoogleFormat("MyClass::MyClass(int var)\n"
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000897 " : some_var_(var), // 4 space indent\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000898 " some_other_var_(var + 1) { // lined up\n"
899 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000900
901 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000902 std::string input = "Constructor()\n"
903 " : aaaa(a,\n";
904 for (unsigned i = 0, e = 80; i != e; ++i) {
905 input += " a,\n";
906 }
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000907 input += " a) {\n}";
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000908 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000909}
910
Alexander Kornienko15757312012-12-06 18:03:27 +0000911TEST_F(FormatTest, BreaksAsHighAsPossible) {
912 verifyFormat(
913 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
914 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
915 " f();");
916}
917
Daniel Jasperbac016b2012-12-03 18:12:45 +0000918TEST_F(FormatTest, BreaksDesireably) {
919 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
920 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000921 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000922
923 verifyFormat(
924 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000925 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000926
927 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
929 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000930
931 verifyFormat(
932 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
934 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000936
937 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
938 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
939
Daniel Jasper723f0302013-01-02 14:40:02 +0000940 verifyFormat(
941 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
943
Daniel Jasper33182dd2012-12-05 14:57:28 +0000944 // This test case breaks on an incorrect memoization, i.e. an optimization not
945 // taking into account the StopAt value.
946 verifyFormat(
947 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000948 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
949 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
950 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000951
Daniel Jaspercd162382013-01-07 13:26:07 +0000952 verifyFormat("{\n {\n {\n"
953 " Annotation.SpaceRequiredBefore =\n"
954 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
955 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
956 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000957}
958
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000959TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
960 verifyGoogleFormat(
961 "aaaaaaaa(aaaaaaaaaaaaa,\n"
962 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
964 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
966 verifyGoogleFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +0000967 "aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa)\n"
968 " .aaaaaaaaaaaaaaaaaa();");
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000969 verifyGoogleFormat(
970 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
971 " ddddddddddddddddddddddddddddd),\n"
972 " test);");
973
974 verifyGoogleFormat(
975 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
976 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
977 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
978 verifyGoogleFormat("a(\"a\"\n"
979 " \"a\",\n"
980 " a);");
981}
982
Daniel Jasperc79afda2013-01-18 10:56:38 +0000983TEST_F(FormatTest, FormatsBuilderPattern) {
984 verifyFormat(
985 "return llvm::StringSwitch<Reference::Kind>(name)\n"
986 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
987 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
988 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
989 " .Default(ORDER_TEXT);\n");
990}
991
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000992TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
993 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
994 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +0000995 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
996 " GUARDED_BY(aaaaaaaaaaaaa);");
997 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000998 " GUARDED_BY(aaaaaaaaaaaaa) {\n}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000999}
1000
Daniel Jaspere2c7acf2012-12-24 00:13:23 +00001001TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
1002 verifyFormat(
1003 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001004 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +00001005 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001006 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +00001007 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001008 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +00001009 verifyFormat(
1010 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001011 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +00001012}
1013
Daniel Jasper9cda8002013-01-07 13:08:40 +00001014TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
1015 verifyFormat(
1016 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
1017 " SI->getAlignment(),\n"
1018 " SI->getPointerAddressSpaceee());\n");
1019 verifyFormat(
1020 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
1021 " Line.Tokens.front().Tok.getLocation(),\n"
1022 " Line.Tokens.back().Tok.getLocation());");
1023}
1024
Daniel Jaspercf225b62012-12-24 13:43:52 +00001025TEST_F(FormatTest, AlignsAfterAssignments) {
1026 verifyFormat(
1027 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001028 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001029 verifyFormat(
1030 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001031 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001032 verifyFormat(
1033 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001034 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001035 verifyFormat(
1036 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001037 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001038 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +00001039 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1040 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1041 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001042}
1043
1044TEST_F(FormatTest, AlignsAfterReturn) {
1045 verifyFormat(
1046 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1047 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
1048 verifyFormat(
1049 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1050 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
1051}
1052
Daniel Jasper9c837d02013-01-09 07:06:56 +00001053TEST_F(FormatTest, BreaksConditionalExpressions) {
1054 verifyFormat(
1055 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1056 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1058 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
1059 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +00001060 verifyFormat(
1061 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
1062 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +00001063}
1064
Nico Weber7d37b8b2013-01-12 01:28:06 +00001065TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1066 verifyFormat("arr[foo ? bar : baz];");
1067 verifyFormat("f()[foo ? bar : baz];");
1068 verifyFormat("(a + b)[foo ? bar : baz];");
1069 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1070}
1071
Daniel Jasperbac016b2012-12-03 18:12:45 +00001072TEST_F(FormatTest, AlignsStringLiterals) {
1073 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1074 " \"short literal\");");
1075 verifyFormat(
1076 "looooooooooooooooooooooooongFunction(\n"
1077 " \"short literal\"\n"
1078 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1079}
1080
Alexander Kornienko15757312012-12-06 18:03:27 +00001081TEST_F(FormatTest, AlignsPipes) {
1082 verifyFormat(
1083 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1084 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1085 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1086 verifyFormat(
1087 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1088 " << aaaaaaaaaaaaaaaaaaaa;");
1089 verifyFormat(
1090 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1091 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1092 verifyFormat(
1093 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1094 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1095 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1096 verifyFormat(
1097 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1098 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1099 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1100}
1101
Daniel Jasperbac016b2012-12-03 18:12:45 +00001102TEST_F(FormatTest, UnderstandsEquals) {
1103 verifyFormat(
1104 "aaaaaaaaaaaaaaaaa =\n"
1105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1106 verifyFormat(
1107 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001108 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001109 verifyFormat(
1110 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001111 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001112 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1114 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001115
Daniel Jasper9cda8002013-01-07 13:08:40 +00001116 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001117 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001118 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001119 " 10000000) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001120}
1121
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001122TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001123 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1124 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001125
Daniel Jasper1321eb52012-12-18 21:05:13 +00001126 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1127 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001128
1129 verifyFormat(
1130 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1131 " Parameter2);");
1132
1133 verifyFormat(
1134 "ShortObject->shortFunction(\n"
1135 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1136 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1137
1138 verifyFormat("loooooooooooooongFunction(\n"
1139 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1140
1141 verifyFormat(
1142 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1143 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1144
Daniel Jasper46a46a22013-01-07 07:13:20 +00001145 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001146 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001147 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001148 verifyFormat(
1149 "aaaaaaaaaaa->aaaaaaaaa(\n"
1150 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1151 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001152}
1153
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001154TEST_F(FormatTest, WrapsTemplateDeclarations) {
1155 verifyFormat("template <typename T>\n"
1156 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1157 verifyFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +00001158 "template <typename T>\n"
1159 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1160 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001161 verifyFormat(
1162 "template <typename T>\n"
1163 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1164 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001165 verifyFormat(
1166 "template <typename T>\n"
1167 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1168 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1169 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001170 verifyFormat("template <typename T>\n"
1171 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1172 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001173 verifyFormat(
1174 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1175 " typename T4 = char>\n"
1176 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001177}
1178
Daniel Jasperbac016b2012-12-03 18:12:45 +00001179TEST_F(FormatTest, UnderstandsTemplateParameters) {
1180 verifyFormat("A<int> a;");
1181 verifyFormat("A<A<A<int> > > a;");
1182 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1183 verifyFormat("bool x = a < 1 || 2 > a;");
1184 verifyFormat("bool x = 5 < f<int>();");
1185 verifyFormat("bool x = f<int>() > 5;");
1186 verifyFormat("bool x = 5 < a<int>::x;");
1187 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1188 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1189
1190 verifyGoogleFormat("A<A<int>> a;");
1191 verifyGoogleFormat("A<A<A<int>>> a;");
1192 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1193
1194 verifyFormat("test >> a >> b;");
1195 verifyFormat("test << a >> b;");
1196
1197 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001198 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001199}
1200
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001201TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001202 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001203 verifyFormat("f(-1, -2, -3);");
1204 verifyFormat("a[-1] = 5;");
1205 verifyFormat("int a = 5 + -2;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001206 verifyFormat("if (i == -1) {\n}");
1207 verifyFormat("if (i != -1) {\n}");
1208 verifyFormat("if (i > -1) {\n}");
1209 verifyFormat("if (i < -1) {\n}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001210 verifyFormat("++(a->f());");
1211 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001212 verifyFormat("(a->f())++;");
1213 verifyFormat("a[42]++;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001214 verifyFormat("if (!(a->f())) {\n}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001215
1216 verifyFormat("a-- > b;");
1217 verifyFormat("b ? -a : c;");
1218 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001219 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001220 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001221 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001222
1223 verifyFormat("return -1;");
1224 verifyFormat("switch (a) {\n"
1225 "case -1:\n"
1226 " break;\n"
1227 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001228
1229 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1230 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001231
1232 verifyFormat("int a = /* confusing comment */ -1;");
1233 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1234 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001235}
1236
1237TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001238 verifyFormat("bool operator<();");
1239 verifyFormat("bool operator>();");
1240 verifyFormat("bool operator=();");
1241 verifyFormat("bool operator==();");
1242 verifyFormat("bool operator!=();");
1243 verifyFormat("int operator+();");
1244 verifyFormat("int operator++();");
1245 verifyFormat("bool operator();");
1246 verifyFormat("bool operator()();");
1247 verifyFormat("bool operator[]();");
1248 verifyFormat("operator bool();");
1249 verifyFormat("operator SomeType<int>();");
1250 verifyFormat("void *operator new(std::size_t size);");
1251 verifyFormat("void *operator new[](std::size_t size);");
1252 verifyFormat("void operator delete(void *ptr);");
1253 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001254}
1255
Daniel Jasper088dab52013-01-11 16:09:04 +00001256TEST_F(FormatTest, UnderstandsNewAndDelete) {
1257 verifyFormat("A *a = new A;");
1258 verifyFormat("A *a = new (placement) A;");
1259 verifyFormat("delete a;");
1260 verifyFormat("delete (A *)a;");
1261}
1262
Daniel Jasper5d334402013-01-02 08:57:10 +00001263TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001264 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001265 verifyFormat("f(a, *a);");
1266 verifyFormat("f(*a);");
1267 verifyFormat("int a = b * 10;");
1268 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001269 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001270 verifyFormat("int a += b * c;");
1271 verifyFormat("int a -= b * c;");
1272 verifyFormat("int a *= b * c;");
1273 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001274 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001275 verifyFormat("int a = *b * c;");
1276 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001277 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001278 verifyFormat("return 10 * b;");
1279 verifyFormat("return *b * *c;");
1280 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001281 verifyFormat("f(b ? *c : *d);");
1282 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001283 verifyFormat("*b = a;");
1284 verifyFormat("a * ~b;");
1285 verifyFormat("a * !b;");
1286 verifyFormat("a * +b;");
1287 verifyFormat("a * -b;");
1288 verifyFormat("a * ++b;");
1289 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001290 verifyFormat("a[4] * b;");
1291 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001292 verifyFormat("a * [self dostuff];");
1293 verifyFormat("a * (a + b);");
1294 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001295 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001296
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001297 verifyFormat("InvalidRegions[*R] = 0;");
1298
Daniel Jasper8b39c662012-12-10 18:59:13 +00001299 verifyFormat("A<int *> a;");
1300 verifyFormat("A<int **> a;");
1301 verifyFormat("A<int *, int *> a;");
1302 verifyFormat("A<int **, int **> a;");
1303
Daniel Jasper2db356d2013-01-08 20:03:18 +00001304 verifyFormat(
1305 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1307
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001308 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001309 verifyGoogleFormat("A<int*> a;");
1310 verifyGoogleFormat("A<int**> a;");
1311 verifyGoogleFormat("A<int*, int*> a;");
1312 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001313 verifyGoogleFormat("f(b ? *c : *d);");
1314 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001315 verifyGoogleFormat("Type* t = **x;");
1316 verifyGoogleFormat("Type* t = *++*x;");
1317 verifyGoogleFormat("*++*x;");
1318 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1319 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001320
1321 verifyFormat("a = *(x + y);");
1322 verifyFormat("a = &(x + y);");
1323 verifyFormat("*(x + y).call();");
1324 verifyFormat("&(x + y)->call();");
1325 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001326
1327 verifyFormat("f(b * /* confusing comment */ ++c);");
1328 verifyFormat(
1329 "int *MyValues = {\n"
1330 " *A, // Operator detection might be confused by the '{'\n"
1331 " *BB // Operator detection might be confused by previous comment\n"
1332 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001333
1334 verifyFormat("if (int *a = &b)");
1335 verifyFormat("if (int &a = *b)");
1336 verifyFormat("if (a & b[i])");
1337 verifyFormat("if (a::b::c::d & b[i])");
1338 verifyFormat("if (*b[i])");
1339 verifyFormat("if (int *a = (&b))");
1340 verifyFormat("while (int *a = &b)");
Daniel Jasperffee1712013-01-22 11:46:26 +00001341
1342 verifyFormat("A = new SomeType *[Length]();");
1343 verifyGoogleFormat("A = new SomeType* [Length]();");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001344}
1345
Daniel Jasper4981bd02013-01-13 08:01:36 +00001346TEST_F(FormatTest, FormatsCasts) {
1347 verifyFormat("Type *A = static_cast<Type *>(P);");
1348 verifyFormat("Type *A = (Type *)P;");
1349 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1350 verifyFormat("int a = (int)(2.0f);");
1351
1352 // FIXME: These also need to be identified.
1353 verifyFormat("int a = (int) 2.0f;");
1354 verifyFormat("int a = (int) * b;");
1355
1356 // These are not casts.
1357 verifyFormat("void f(int *) {}");
1358 verifyFormat("void f(int *);");
1359 verifyFormat("void f(int *) = 0;");
1360 verifyFormat("void f(SmallVector<int>) {}");
1361 verifyFormat("void f(SmallVector<int>);");
1362 verifyFormat("void f(SmallVector<int>) = 0;");
1363}
1364
Daniel Jasper46ef8522013-01-10 13:08:12 +00001365TEST_F(FormatTest, FormatsFunctionTypes) {
1366 // FIXME: Determine the cases that need a space after the return type and fix.
1367 verifyFormat("A<bool()> a;");
1368 verifyFormat("A<SomeType()> a;");
1369 verifyFormat("A<void(*)(int, std::string)> a;");
1370
1371 verifyFormat("int(*func)(void *);");
1372}
1373
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001374TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001375 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001376 " int LoooooooooooooooongParam2) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001377 verifyFormat(
1378 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1379 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001380 " Type *T) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001381}
1382
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001383TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1384 verifyFormat("(a)->b();");
1385 verifyFormat("--a;");
1386}
1387
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001388TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001389 verifyFormat("#include <string>\n"
1390 "#include <a/b/c.h>\n"
1391 "#include \"a/b/string\"\n"
1392 "#include \"string.h\"\n"
1393 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001394 "#include <a-a>\n"
1395 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001396
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001397 verifyFormat("#import <string>");
1398 verifyFormat("#import <a/b/c.h>");
1399 verifyFormat("#import \"a/b/string\"");
1400 verifyFormat("#import \"string.h\"");
1401 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001402}
1403
Alexander Kornienko15757312012-12-06 18:03:27 +00001404//===----------------------------------------------------------------------===//
1405// Error recovery tests.
1406//===----------------------------------------------------------------------===//
1407
Daniel Jasper700e7102013-01-10 09:26:47 +00001408TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001409 verifyFormat("void f() { return; }\n42");
1410 verifyFormat("void f() {\n"
1411 " if (0)\n"
1412 " return;\n"
1413 "}\n"
1414 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001415 verifyFormat("void f() { return }\n42");
1416 verifyFormat("void f() {\n"
1417 " if (0)\n"
1418 " return\n"
1419 "}\n"
1420 "42");
1421}
1422
1423TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1424 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1425 EXPECT_EQ("void f() {\n"
1426 " if (a)\n"
1427 " return\n"
1428 "}", format("void f ( ) { if ( a ) return }"));
1429 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1430 EXPECT_EQ("namespace N {\n"
1431 "void f() {}\n"
1432 "void g()\n"
1433 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001434}
1435
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001436TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1437 verifyFormat("int aaaaaaaa =\n"
1438 " // Overly long comment\n"
1439 " b;", getLLVMStyleWithColumns(20));
1440 verifyFormat("function(\n"
1441 " ShortArgument,\n"
1442 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1443}
1444
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001445TEST_F(FormatTest, IncorrectAccessSpecifier) {
1446 verifyFormat("public:");
1447 verifyFormat("class A {\n"
1448 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001449 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001450 "};");
1451 verifyFormat("public\n"
1452 "int qwerty;");
1453 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001454 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001455 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001456 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001457 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001458 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001459}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001460
Alexander Kornienko393b0082012-12-04 15:40:36 +00001461TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1462 verifyFormat("{");
1463}
1464
1465TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001466 verifyFormat("do {\n}");
1467 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001468 "f();");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001469 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001470 "wheeee(fun);");
1471 verifyFormat("do {\n"
1472 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001473 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001474}
1475
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001476TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001477 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001478 verifyFormat("switch {\n foo;\n foo();\n}");
1479 verifyFormat("for {\n foo;\n foo();\n}");
1480 verifyFormat("while {\n foo;\n foo();\n}");
1481 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001482}
1483
Daniel Jasper1f42f112013-01-04 18:52:56 +00001484TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1485 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001486 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001487}
1488
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001489TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001490 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1491 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1492 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1493 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001494
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001495 EXPECT_EQ("{\n"
1496 " {\n"
1497 " breakme(\n"
1498 " qwe);\n"
1499 "}\n", format("{\n"
1500 " {\n"
1501 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001502 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001503}
1504
Manuel Klimek2851c162013-01-10 14:36:46 +00001505TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1506 verifyFormat(
1507 "int x = {\n"
1508 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001509 " b(alongervariable)\n"
1510 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001511}
1512
Manuel Klimekc44ee892013-01-21 10:07:49 +00001513TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
1514 verifyFormat("return (a)(b) { 1, 2, 3 };");
1515}
1516
Manuel Klimek2851c162013-01-10 14:36:46 +00001517TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1518 verifyFormat(
1519 "Aaa({\n"
1520 " int i;\n"
1521 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1522 " ccccccccccccccccc));");
1523}
1524
Manuel Klimek517e8942013-01-11 17:54:10 +00001525TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1526 verifyFormat("void f() { return 42; }");
1527 verifyFormat("void f() {\n"
1528 " // Comment\n"
1529 "}");
1530 verifyFormat("{\n"
1531 "#error {\n"
1532 " int a;\n"
1533 "}");
1534 verifyFormat("{\n"
1535 " int a;\n"
1536 "#error {\n"
1537 "}");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001538
1539 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
1540 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
1541
1542 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
1543 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
Manuel Klimek517e8942013-01-11 17:54:10 +00001544}
1545
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001546TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1547 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001548 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001549 verifyFormat("class foo a = { bar };\nint n;");
1550 verifyFormat("union foo a = { bar };\nint n;");
1551
1552 // Elaborate types inside function definitions.
1553 verifyFormat("struct foo f() {}\nint n;");
1554 verifyFormat("class foo f() {}\nint n;");
1555 verifyFormat("union foo f() {}\nint n;");
1556
1557 // Templates.
1558 verifyFormat("template <class X> void f() {}\nint n;");
1559 verifyFormat("template <struct X> void f() {}\nint n;");
1560 verifyFormat("template <union X> void f() {}\nint n;");
1561
1562 // Actual definitions...
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001563 verifyFormat("struct {\n} n;");
1564 verifyFormat(
1565 "template <template <class T, class Y>, class Z> class X {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001566 verifyFormat("union Z {\n int n;\n} x;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001567 verifyFormat("class MACRO Z {\n} n;");
1568 verifyFormat("class MACRO(X) Z {\n} n;");
1569 verifyFormat("class __attribute__(X) Z {\n} n;");
1570 verifyFormat("class __declspec(X) Z {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001571
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001572 // Redefinition from nested context:
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001573 verifyFormat("class A::B::C {\n} n;");
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001574
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001575 // Template definitions.
1576 // FIXME: This is still incorrectly handled at the formatter side.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001577 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001578
1579 // FIXME:
1580 // This now gets parsed incorrectly as class definition.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001581 // verifyFormat("class A<int> f() {\n}\nint n;");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001582
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001583 // Elaborate types where incorrectly parsing the structural element would
1584 // break the indent.
1585 verifyFormat("if (true)\n"
1586 " class X x;\n"
1587 "else\n"
1588 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001589}
1590
Manuel Klimek407a31a2013-01-15 15:50:27 +00001591TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1592 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1593 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1594 EXPECT_EQ("#error 1", format(" # error 1"));
1595 EXPECT_EQ("#warning 1", format(" # warning 1"));
1596}
1597
Manuel Klimek525fe162013-01-18 14:04:34 +00001598TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
1599 FormatStyle AllowsMergedIf = getGoogleStyle();
1600 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
1601 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
1602 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
Manuel Klimek4c128122013-01-18 14:46:43 +00001603 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
1604 EXPECT_EQ("if (true) return 42;",
1605 format("if (true)\nreturn 42;", AllowsMergedIf));
1606 FormatStyle ShortMergedIf = AllowsMergedIf;
1607 ShortMergedIf.ColumnLimit = 25;
1608 verifyFormat("#define A \\\n"
1609 " if (true) return 42;", ShortMergedIf);
1610 verifyFormat("#define A \\\n"
1611 " f(); \\\n"
1612 " if (true)\n"
1613 "#define B", ShortMergedIf);
1614 verifyFormat("#define A \\\n"
1615 " f(); \\\n"
1616 " if (true)\n"
1617 "g();", ShortMergedIf);
Manuel Klimek0fbe0082013-01-21 14:16:56 +00001618 verifyFormat("{\n"
1619 "#ifdef A\n"
1620 " // Comment\n"
1621 " if (true) continue;\n"
1622 "#endif\n"
1623 " // Comment\n"
1624 " if (true) continue;", ShortMergedIf);
Manuel Klimek525fe162013-01-18 14:04:34 +00001625}
1626
Nico Webercf4a79c2013-01-08 17:56:31 +00001627//===----------------------------------------------------------------------===//
1628// Objective-C tests.
1629//===----------------------------------------------------------------------===//
1630
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001631TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1632 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1633 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1634 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001635 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001636 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1637 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1638 format("-(NSInteger)Method3:(id)anObject;"));
1639 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1640 format("-(NSInteger)Method4:(id)anObject;"));
1641 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1642 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1643 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1644 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001645 EXPECT_EQ(
1646 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1647 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001648
1649 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001650 EXPECT_EQ(
1651 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1652 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1653 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1654 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1655 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1656 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1657 format(
1658 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1659 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1660 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1661 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1662 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1663 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001664
1665 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001666 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001667 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1668 // protocol lists (but not for template classes):
1669 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001670
1671 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001672 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001673
1674 // If there's no return type (very rare in practice!), LLVM and Google style
1675 // agree.
1676 verifyFormat("- foo:(int)f;");
1677 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001678}
1679
Daniel Jasper886568d2013-01-09 08:36:49 +00001680TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001681 verifyFormat("int (^Block)(int, int);");
1682 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001683}
1684
Nico Weber27d13672013-01-09 20:25:35 +00001685TEST_F(FormatTest, FormatObjCInterface) {
1686 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001687 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001688 "@public\n"
1689 " int field1;\n"
1690 "@protected\n"
1691 " int field2;\n"
1692 "@private\n"
1693 " int field3;\n"
1694 "@package\n"
1695 " int field4;\n"
1696 "}\n"
1697 "+ (id)init;\n"
1698 "@end");
1699
Nico Weber27d13672013-01-09 20:25:35 +00001700 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1701 " @public\n"
1702 " int field1;\n"
1703 " @protected\n"
1704 " int field2;\n"
1705 " @private\n"
1706 " int field3;\n"
1707 " @package\n"
1708 " int field4;\n"
1709 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001710 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001711 "@end");
1712
1713 verifyFormat("@interface Foo\n"
1714 "+ (id)init;\n"
1715 "// Look, a comment!\n"
1716 "- (int)answerWith:(int)i;\n"
1717 "@end");
1718
1719 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001720 "@end\n"
1721 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001722 "@end");
1723
1724 verifyFormat("@interface Foo : Bar\n"
1725 "+ (id)init;\n"
1726 "@end");
1727
Nico Weber5f500df2013-01-10 20:12:55 +00001728 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001729 "+ (id)init;\n"
1730 "@end");
1731
Nico Weber5f500df2013-01-10 20:12:55 +00001732 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001733 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001734 "@end");
1735
Nico Webered91bba2013-01-10 19:19:14 +00001736 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001737 "+ (id)init;\n"
1738 "@end");
1739
Nico Webered91bba2013-01-10 19:19:14 +00001740 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001741 "+ (id)init;\n"
1742 "@end");
1743
Nico Weber5f500df2013-01-10 20:12:55 +00001744 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001745 "+ (id)init;\n"
1746 "@end");
1747
Nico Weber5f500df2013-01-10 20:12:55 +00001748 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001749 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001750 "@end");
1751
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001752 verifyFormat("@interface Foo {\n"
1753 " int _i;\n"
1754 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001755 "+ (id)init;\n"
1756 "@end");
1757
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001758 verifyFormat("@interface Foo : Bar {\n"
1759 " int _i;\n"
1760 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001761 "+ (id)init;\n"
1762 "@end");
1763
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001764 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1765 " int _i;\n"
1766 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001767 "+ (id)init;\n"
1768 "@end");
1769
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001770 verifyFormat("@interface Foo (HackStuff) {\n"
1771 " int _i;\n"
1772 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001773 "+ (id)init;\n"
1774 "@end");
1775
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001776 verifyFormat("@interface Foo () {\n"
1777 " int _i;\n"
1778 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001779 "+ (id)init;\n"
1780 "@end");
1781
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001782 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1783 " int _i;\n"
1784 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001785 "+ (id)init;\n"
1786 "@end");
1787}
1788
Nico Weber50767d82013-01-09 23:25:37 +00001789TEST_F(FormatTest, FormatObjCImplementation) {
1790 verifyFormat("@implementation Foo : NSObject {\n"
1791 "@public\n"
1792 " int field1;\n"
1793 "@protected\n"
1794 " int field2;\n"
1795 "@private\n"
1796 " int field3;\n"
1797 "@package\n"
1798 " int field4;\n"
1799 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001800 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001801 "@end");
1802
1803 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1804 " @public\n"
1805 " int field1;\n"
1806 " @protected\n"
1807 " int field2;\n"
1808 " @private\n"
1809 " int field3;\n"
1810 " @package\n"
1811 " int field4;\n"
1812 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001813 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001814 "@end");
1815
1816 verifyFormat("@implementation Foo\n"
1817 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001818 " if (true)\n"
1819 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001820 "}\n"
1821 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001822 "- (int)answerWith:(int)i {\n"
1823 " return i;\n"
1824 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001825 "+ (int)answerWith:(int)i {\n"
1826 " return i;\n"
1827 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001828 "@end");
1829
1830 verifyFormat("@implementation Foo\n"
1831 "@end\n"
1832 "@implementation Bar\n"
1833 "@end");
1834
1835 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001836 "+ (id)init {\n}\n"
1837 "- (void)foo {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001838 "@end");
1839
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001840 verifyFormat("@implementation Foo {\n"
1841 " int _i;\n"
1842 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001843 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001844 "@end");
1845
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001846 verifyFormat("@implementation Foo : Bar {\n"
1847 " int _i;\n"
1848 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001849 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001850 "@end");
1851
Nico Webered91bba2013-01-10 19:19:14 +00001852 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001853 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001854 "@end");
1855}
1856
Nico Weber1abe6ea2013-01-09 21:15:03 +00001857TEST_F(FormatTest, FormatObjCProtocol) {
1858 verifyFormat("@protocol Foo\n"
1859 "@property(weak) id delegate;\n"
1860 "- (NSUInteger)numberOfThings;\n"
1861 "@end");
1862
Nico Weber5f500df2013-01-10 20:12:55 +00001863 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001864 "- (NSUInteger)numberOfThings;\n"
1865 "@end");
1866
Nico Weber5f500df2013-01-10 20:12:55 +00001867 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001868 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001869 "@end");
1870
Nico Weber1abe6ea2013-01-09 21:15:03 +00001871 verifyFormat("@protocol Foo;\n"
1872 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001873
1874 verifyFormat("@protocol Foo\n"
1875 "@end\n"
1876 "@protocol Bar\n"
1877 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001878
1879 verifyFormat("@protocol myProtocol\n"
1880 "- (void)mandatoryWithInt:(int)i;\n"
1881 "@optional\n"
1882 "- (void)optional;\n"
1883 "@required\n"
1884 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001885 "@optional\n"
1886 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001887 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001888}
1889
Nico Weberbcfdd262013-01-12 06:18:40 +00001890TEST_F(FormatTest, FormatObjCMethodExpr) {
1891 verifyFormat("[foo bar:baz];");
1892 verifyFormat("return [foo bar:baz];");
1893 verifyFormat("f([foo bar:baz]);");
1894 verifyFormat("f(2, [foo bar:baz]);");
1895 verifyFormat("f(2, a ? b : c);");
1896 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1897
1898 verifyFormat("[foo bar:baz], [foo bar:baz];");
1899 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1900 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1901 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1902 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1903 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1904 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1905 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1906 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1907 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1908 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1909 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1910 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1911 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1912 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1913 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1914 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1915 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1916 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1917 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1918 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1919 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1920 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1921 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1922 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1923 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1924 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1925 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1926 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1927 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1928 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1929 // Whew!
1930
1931 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1932 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1933 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1934 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1935 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001936 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001937 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001938
Nico Weberbcfdd262013-01-12 06:18:40 +00001939 verifyFormat("arr[[self indexForFoo:a]];");
1940 verifyFormat("throw [self errorFor:a];");
1941 verifyFormat("@throw [self errorFor:a];");
1942
Nico Webere8ccc812013-01-12 22:48:47 +00001943 // This tests that the formatter doesn't break after "backing" but before ":",
1944 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001945 verifyFormat(
1946 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001947 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1948 " backing:NSBackingStoreBuffered defer:YES]))");
1949
Nico Webere8ccc812013-01-12 22:48:47 +00001950 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1951 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001952
1953}
1954
Nico Weber581f5572013-01-07 15:56:25 +00001955TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001956 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001957 verifyFormat("@catch");
1958 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001959 verifyFormat("@compatibility_alias");
1960 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001961 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001962 verifyFormat("@encode");
1963 verifyFormat("@end");
1964 verifyFormat("@finally");
1965 verifyFormat("@implementation");
1966 verifyFormat("@import");
1967 verifyFormat("@interface");
1968 verifyFormat("@optional");
1969 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001970 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001971 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001972 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001973 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001974 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001975 verifyFormat("@required");
1976 verifyFormat("@selector");
1977 verifyFormat("@synchronized");
1978 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001979 verifyFormat("@throw");
1980 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001981
Nico Webercb4d6902013-01-08 19:40:21 +00001982 verifyFormat("@\"String\"");
1983 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001984 verifyFormat("@+4.8");
1985 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001986 verifyFormat("@1LL");
1987 verifyFormat("@.5");
1988 verifyFormat("@'c'");
1989 verifyFormat("@true");
1990 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001991 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001992 verifyFormat("@[");
1993 verifyFormat("@{");
1994
Nico Weber581f5572013-01-07 15:56:25 +00001995 EXPECT_EQ("@interface", format("@ interface"));
1996
1997 // The precise formatting of this doesn't matter, nobody writes code like
1998 // this.
1999 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00002000}
2001
Nico Weberc31689a2013-01-08 19:15:23 +00002002TEST_F(FormatTest, ObjCSnippets) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +00002003 verifyFormat("@autoreleasepool {\n"
2004 " foo();\n"
2005 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00002006 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00002007 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00002008 verifyFormat("@dynamic textColor;");
Nico Weber5dfe9b42013-01-18 05:11:47 +00002009 verifyFormat("char *buf1 = @encode(int *);");
2010 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
2011 // FIXME: Enable once PR14884 is fixed:
Nico Weberc31689a2013-01-08 19:15:23 +00002012 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00002013 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weber6a21a552013-01-18 02:43:57 +00002014 verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00002015 verifyFormat("@synchronized(self) {\n"
2016 " f();\n"
2017 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00002018
Nico Weber70848232013-01-10 21:30:42 +00002019 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2020 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2021
Nico Webercf4a79c2013-01-08 17:56:31 +00002022 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00002023 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
2024 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00002025}
2026
Daniel Jaspercd162382013-01-07 13:26:07 +00002027} // end namespace tooling
2028} // end namespace clang