blob: b0f0fdba36089f13fdb381e8464c4dd6bfe21d45 [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 Jasper32983272013-01-22 14:28:24 +0000967 "aaaaaaaaaaaaaaa(aaaaaaaaa,\n"
968 " aaaaaaaaa,\n"
969 " aaaaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();");
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000970 verifyGoogleFormat(
971 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
972 " ddddddddddddddddddddddddddddd),\n"
973 " test);");
974
975 verifyGoogleFormat(
976 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
977 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
978 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
979 verifyGoogleFormat("a(\"a\"\n"
980 " \"a\",\n"
981 " a);");
982}
983
Daniel Jasperc79afda2013-01-18 10:56:38 +0000984TEST_F(FormatTest, FormatsBuilderPattern) {
985 verifyFormat(
986 "return llvm::StringSwitch<Reference::Kind>(name)\n"
987 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
988 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
989 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
990 " .Default(ORDER_TEXT);\n");
991}
992
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000993TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
994 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
995 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +0000996 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
997 " GUARDED_BY(aaaaaaaaaaaaa);");
998 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000999 " GUARDED_BY(aaaaaaaaaaaaa) {\n}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001000}
1001
Daniel Jaspere2c7acf2012-12-24 00:13:23 +00001002TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
1003 verifyFormat(
1004 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001005 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +00001006 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001007 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +00001008 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001009 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +00001010 verifyFormat(
1011 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001012 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +00001013}
1014
Daniel Jasper9cda8002013-01-07 13:08:40 +00001015TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
1016 verifyFormat(
1017 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
1018 " SI->getAlignment(),\n"
1019 " SI->getPointerAddressSpaceee());\n");
1020 verifyFormat(
1021 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
1022 " Line.Tokens.front().Tok.getLocation(),\n"
1023 " Line.Tokens.back().Tok.getLocation());");
1024}
1025
Daniel Jaspercf225b62012-12-24 13:43:52 +00001026TEST_F(FormatTest, AlignsAfterAssignments) {
1027 verifyFormat(
1028 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001029 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001030 verifyFormat(
1031 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001032 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001033 verifyFormat(
1034 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001035 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001036 verifyFormat(
1037 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001038 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001039 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +00001040 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1041 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1042 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001043}
1044
1045TEST_F(FormatTest, AlignsAfterReturn) {
1046 verifyFormat(
1047 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1048 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
1049 verifyFormat(
1050 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1051 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
1052}
1053
Daniel Jasper9c837d02013-01-09 07:06:56 +00001054TEST_F(FormatTest, BreaksConditionalExpressions) {
1055 verifyFormat(
1056 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1057 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1059 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
1060 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +00001061 verifyFormat(
1062 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
1063 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +00001064}
1065
Nico Weber7d37b8b2013-01-12 01:28:06 +00001066TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1067 verifyFormat("arr[foo ? bar : baz];");
1068 verifyFormat("f()[foo ? bar : baz];");
1069 verifyFormat("(a + b)[foo ? bar : baz];");
1070 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1071}
1072
Daniel Jasperbac016b2012-12-03 18:12:45 +00001073TEST_F(FormatTest, AlignsStringLiterals) {
1074 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1075 " \"short literal\");");
1076 verifyFormat(
1077 "looooooooooooooooooooooooongFunction(\n"
1078 " \"short literal\"\n"
1079 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1080}
1081
Alexander Kornienko15757312012-12-06 18:03:27 +00001082TEST_F(FormatTest, AlignsPipes) {
1083 verifyFormat(
1084 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1085 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1086 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1087 verifyFormat(
1088 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1089 " << aaaaaaaaaaaaaaaaaaaa;");
1090 verifyFormat(
1091 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1092 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1093 verifyFormat(
1094 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1095 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1096 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1097 verifyFormat(
1098 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1099 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1100 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1101}
1102
Daniel Jasperbac016b2012-12-03 18:12:45 +00001103TEST_F(FormatTest, UnderstandsEquals) {
1104 verifyFormat(
1105 "aaaaaaaaaaaaaaaaa =\n"
1106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1107 verifyFormat(
1108 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001110 verifyFormat(
1111 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001112 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001113 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001114 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1115 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001116
Daniel Jasper9cda8002013-01-07 13:08:40 +00001117 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001118 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001119 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001120 " 10000000) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001121}
1122
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001123TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001124 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1125 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001126
Daniel Jasper1321eb52012-12-18 21:05:13 +00001127 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1128 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001129
1130 verifyFormat(
1131 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1132 " Parameter2);");
1133
1134 verifyFormat(
1135 "ShortObject->shortFunction(\n"
1136 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1137 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1138
1139 verifyFormat("loooooooooooooongFunction(\n"
1140 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1141
1142 verifyFormat(
1143 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1144 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1145
Daniel Jasper46a46a22013-01-07 07:13:20 +00001146 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001147 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001148 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001149 verifyFormat(
1150 "aaaaaaaaaaa->aaaaaaaaa(\n"
1151 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1152 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001153}
1154
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001155TEST_F(FormatTest, WrapsTemplateDeclarations) {
1156 verifyFormat("template <typename T>\n"
1157 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1158 verifyFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +00001159 "template <typename T>\n"
1160 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1161 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001162 verifyFormat(
1163 "template <typename T>\n"
1164 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1165 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001166 verifyFormat(
1167 "template <typename T>\n"
1168 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1169 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001171 verifyFormat("template <typename T>\n"
1172 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1173 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001174 verifyFormat(
1175 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1176 " typename T4 = char>\n"
1177 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001178}
1179
Daniel Jasperbac016b2012-12-03 18:12:45 +00001180TEST_F(FormatTest, UnderstandsTemplateParameters) {
1181 verifyFormat("A<int> a;");
1182 verifyFormat("A<A<A<int> > > a;");
1183 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1184 verifyFormat("bool x = a < 1 || 2 > a;");
1185 verifyFormat("bool x = 5 < f<int>();");
1186 verifyFormat("bool x = f<int>() > 5;");
1187 verifyFormat("bool x = 5 < a<int>::x;");
1188 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1189 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1190
1191 verifyGoogleFormat("A<A<int>> a;");
1192 verifyGoogleFormat("A<A<A<int>>> a;");
1193 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1194
1195 verifyFormat("test >> a >> b;");
1196 verifyFormat("test << a >> b;");
1197
1198 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001199 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001200}
1201
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001202TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001203 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001204 verifyFormat("f(-1, -2, -3);");
1205 verifyFormat("a[-1] = 5;");
1206 verifyFormat("int a = 5 + -2;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001207 verifyFormat("if (i == -1) {\n}");
1208 verifyFormat("if (i != -1) {\n}");
1209 verifyFormat("if (i > -1) {\n}");
1210 verifyFormat("if (i < -1) {\n}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001211 verifyFormat("++(a->f());");
1212 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001213 verifyFormat("(a->f())++;");
1214 verifyFormat("a[42]++;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001215 verifyFormat("if (!(a->f())) {\n}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001216
1217 verifyFormat("a-- > b;");
1218 verifyFormat("b ? -a : c;");
1219 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001220 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001221 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001222 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001223
1224 verifyFormat("return -1;");
1225 verifyFormat("switch (a) {\n"
1226 "case -1:\n"
1227 " break;\n"
1228 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001229
1230 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1231 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001232
1233 verifyFormat("int a = /* confusing comment */ -1;");
1234 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1235 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001236}
1237
1238TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001239 verifyFormat("bool operator<();");
1240 verifyFormat("bool operator>();");
1241 verifyFormat("bool operator=();");
1242 verifyFormat("bool operator==();");
1243 verifyFormat("bool operator!=();");
1244 verifyFormat("int operator+();");
1245 verifyFormat("int operator++();");
1246 verifyFormat("bool operator();");
1247 verifyFormat("bool operator()();");
1248 verifyFormat("bool operator[]();");
1249 verifyFormat("operator bool();");
1250 verifyFormat("operator SomeType<int>();");
1251 verifyFormat("void *operator new(std::size_t size);");
1252 verifyFormat("void *operator new[](std::size_t size);");
1253 verifyFormat("void operator delete(void *ptr);");
1254 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001255}
1256
Daniel Jasper088dab52013-01-11 16:09:04 +00001257TEST_F(FormatTest, UnderstandsNewAndDelete) {
1258 verifyFormat("A *a = new A;");
1259 verifyFormat("A *a = new (placement) A;");
1260 verifyFormat("delete a;");
1261 verifyFormat("delete (A *)a;");
1262}
1263
Daniel Jasper5d334402013-01-02 08:57:10 +00001264TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001265 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001266 verifyFormat("f(a, *a);");
1267 verifyFormat("f(*a);");
1268 verifyFormat("int a = b * 10;");
1269 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001270 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001271 verifyFormat("int a += b * c;");
1272 verifyFormat("int a -= b * c;");
1273 verifyFormat("int a *= b * c;");
1274 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001275 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001276 verifyFormat("int a = *b * c;");
1277 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001278 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001279 verifyFormat("return 10 * b;");
1280 verifyFormat("return *b * *c;");
1281 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001282 verifyFormat("f(b ? *c : *d);");
1283 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001284 verifyFormat("*b = a;");
1285 verifyFormat("a * ~b;");
1286 verifyFormat("a * !b;");
1287 verifyFormat("a * +b;");
1288 verifyFormat("a * -b;");
1289 verifyFormat("a * ++b;");
1290 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001291 verifyFormat("a[4] * b;");
1292 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001293 verifyFormat("a * [self dostuff];");
1294 verifyFormat("a * (a + b);");
1295 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001296 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001297
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001298 verifyFormat("InvalidRegions[*R] = 0;");
1299
Daniel Jasper8b39c662012-12-10 18:59:13 +00001300 verifyFormat("A<int *> a;");
1301 verifyFormat("A<int **> a;");
1302 verifyFormat("A<int *, int *> a;");
1303 verifyFormat("A<int **, int **> a;");
1304
Daniel Jasper2db356d2013-01-08 20:03:18 +00001305 verifyFormat(
1306 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1308
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001309 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001310 verifyGoogleFormat("A<int*> a;");
1311 verifyGoogleFormat("A<int**> a;");
1312 verifyGoogleFormat("A<int*, int*> a;");
1313 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001314 verifyGoogleFormat("f(b ? *c : *d);");
1315 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001316 verifyGoogleFormat("Type* t = **x;");
1317 verifyGoogleFormat("Type* t = *++*x;");
1318 verifyGoogleFormat("*++*x;");
1319 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1320 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001321
1322 verifyFormat("a = *(x + y);");
1323 verifyFormat("a = &(x + y);");
1324 verifyFormat("*(x + y).call();");
1325 verifyFormat("&(x + y)->call();");
1326 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001327
1328 verifyFormat("f(b * /* confusing comment */ ++c);");
1329 verifyFormat(
1330 "int *MyValues = {\n"
1331 " *A, // Operator detection might be confused by the '{'\n"
1332 " *BB // Operator detection might be confused by previous comment\n"
1333 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001334
1335 verifyFormat("if (int *a = &b)");
1336 verifyFormat("if (int &a = *b)");
1337 verifyFormat("if (a & b[i])");
1338 verifyFormat("if (a::b::c::d & b[i])");
1339 verifyFormat("if (*b[i])");
1340 verifyFormat("if (int *a = (&b))");
1341 verifyFormat("while (int *a = &b)");
Daniel Jasperffee1712013-01-22 11:46:26 +00001342
1343 verifyFormat("A = new SomeType *[Length]();");
1344 verifyGoogleFormat("A = new SomeType* [Length]();");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001345}
1346
Daniel Jasper4981bd02013-01-13 08:01:36 +00001347TEST_F(FormatTest, FormatsCasts) {
1348 verifyFormat("Type *A = static_cast<Type *>(P);");
1349 verifyFormat("Type *A = (Type *)P;");
1350 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1351 verifyFormat("int a = (int)(2.0f);");
1352
1353 // FIXME: These also need to be identified.
1354 verifyFormat("int a = (int) 2.0f;");
1355 verifyFormat("int a = (int) * b;");
1356
1357 // These are not casts.
1358 verifyFormat("void f(int *) {}");
1359 verifyFormat("void f(int *);");
1360 verifyFormat("void f(int *) = 0;");
1361 verifyFormat("void f(SmallVector<int>) {}");
1362 verifyFormat("void f(SmallVector<int>);");
1363 verifyFormat("void f(SmallVector<int>) = 0;");
1364}
1365
Daniel Jasper46ef8522013-01-10 13:08:12 +00001366TEST_F(FormatTest, FormatsFunctionTypes) {
1367 // FIXME: Determine the cases that need a space after the return type and fix.
1368 verifyFormat("A<bool()> a;");
1369 verifyFormat("A<SomeType()> a;");
1370 verifyFormat("A<void(*)(int, std::string)> a;");
1371
1372 verifyFormat("int(*func)(void *);");
1373}
1374
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001375TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001376 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001377 " int LoooooooooooooooongParam2) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001378 verifyFormat(
1379 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1380 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001381 " Type *T) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001382}
1383
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001384TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1385 verifyFormat("(a)->b();");
1386 verifyFormat("--a;");
1387}
1388
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001389TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001390 verifyFormat("#include <string>\n"
1391 "#include <a/b/c.h>\n"
1392 "#include \"a/b/string\"\n"
1393 "#include \"string.h\"\n"
1394 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001395 "#include <a-a>\n"
1396 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001397
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001398 verifyFormat("#import <string>");
1399 verifyFormat("#import <a/b/c.h>");
1400 verifyFormat("#import \"a/b/string\"");
1401 verifyFormat("#import \"string.h\"");
1402 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001403}
1404
Alexander Kornienko15757312012-12-06 18:03:27 +00001405//===----------------------------------------------------------------------===//
1406// Error recovery tests.
1407//===----------------------------------------------------------------------===//
1408
Daniel Jasper700e7102013-01-10 09:26:47 +00001409TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001410 verifyFormat("void f() { return; }\n42");
1411 verifyFormat("void f() {\n"
1412 " if (0)\n"
1413 " return;\n"
1414 "}\n"
1415 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001416 verifyFormat("void f() { return }\n42");
1417 verifyFormat("void f() {\n"
1418 " if (0)\n"
1419 " return\n"
1420 "}\n"
1421 "42");
1422}
1423
1424TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1425 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1426 EXPECT_EQ("void f() {\n"
1427 " if (a)\n"
1428 " return\n"
1429 "}", format("void f ( ) { if ( a ) return }"));
1430 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1431 EXPECT_EQ("namespace N {\n"
1432 "void f() {}\n"
1433 "void g()\n"
1434 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001435}
1436
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001437TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1438 verifyFormat("int aaaaaaaa =\n"
1439 " // Overly long comment\n"
1440 " b;", getLLVMStyleWithColumns(20));
1441 verifyFormat("function(\n"
1442 " ShortArgument,\n"
1443 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1444}
1445
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001446TEST_F(FormatTest, IncorrectAccessSpecifier) {
1447 verifyFormat("public:");
1448 verifyFormat("class A {\n"
1449 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001450 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001451 "};");
1452 verifyFormat("public\n"
1453 "int qwerty;");
1454 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001455 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001456 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001457 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001458 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001459 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001460}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001461
Alexander Kornienko393b0082012-12-04 15:40:36 +00001462TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1463 verifyFormat("{");
1464}
1465
1466TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001467 verifyFormat("do {\n}");
1468 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001469 "f();");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001470 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001471 "wheeee(fun);");
1472 verifyFormat("do {\n"
1473 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001474 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001475}
1476
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001477TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001478 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001479 verifyFormat("switch {\n foo;\n foo();\n}");
1480 verifyFormat("for {\n foo;\n foo();\n}");
1481 verifyFormat("while {\n foo;\n foo();\n}");
1482 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001483}
1484
Daniel Jasper1f42f112013-01-04 18:52:56 +00001485TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1486 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001487 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001488}
1489
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001490TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001491 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1492 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1493 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1494 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001495
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001496 EXPECT_EQ("{\n"
1497 " {\n"
1498 " breakme(\n"
1499 " qwe);\n"
1500 "}\n", format("{\n"
1501 " {\n"
1502 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001503 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001504}
1505
Manuel Klimek2851c162013-01-10 14:36:46 +00001506TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1507 verifyFormat(
1508 "int x = {\n"
1509 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001510 " b(alongervariable)\n"
1511 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001512}
1513
Manuel Klimekc44ee892013-01-21 10:07:49 +00001514TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
1515 verifyFormat("return (a)(b) { 1, 2, 3 };");
1516}
1517
Manuel Klimek2851c162013-01-10 14:36:46 +00001518TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1519 verifyFormat(
1520 "Aaa({\n"
1521 " int i;\n"
1522 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1523 " ccccccccccccccccc));");
1524}
1525
Manuel Klimek517e8942013-01-11 17:54:10 +00001526TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1527 verifyFormat("void f() { return 42; }");
1528 verifyFormat("void f() {\n"
1529 " // Comment\n"
1530 "}");
1531 verifyFormat("{\n"
1532 "#error {\n"
1533 " int a;\n"
1534 "}");
1535 verifyFormat("{\n"
1536 " int a;\n"
1537 "#error {\n"
1538 "}");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001539
1540 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
1541 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
1542
1543 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
1544 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
Manuel Klimek517e8942013-01-11 17:54:10 +00001545}
1546
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001547TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1548 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001549 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001550 verifyFormat("class foo a = { bar };\nint n;");
1551 verifyFormat("union foo a = { bar };\nint n;");
1552
1553 // Elaborate types inside function definitions.
1554 verifyFormat("struct foo f() {}\nint n;");
1555 verifyFormat("class foo f() {}\nint n;");
1556 verifyFormat("union foo f() {}\nint n;");
1557
1558 // Templates.
1559 verifyFormat("template <class X> void f() {}\nint n;");
1560 verifyFormat("template <struct X> void f() {}\nint n;");
1561 verifyFormat("template <union X> void f() {}\nint n;");
1562
1563 // Actual definitions...
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001564 verifyFormat("struct {\n} n;");
1565 verifyFormat(
1566 "template <template <class T, class Y>, class Z> class X {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001567 verifyFormat("union Z {\n int n;\n} x;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001568 verifyFormat("class MACRO Z {\n} n;");
1569 verifyFormat("class MACRO(X) Z {\n} n;");
1570 verifyFormat("class __attribute__(X) Z {\n} n;");
1571 verifyFormat("class __declspec(X) Z {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001572
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001573 // Redefinition from nested context:
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001574 verifyFormat("class A::B::C {\n} n;");
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001575
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001576 // Template definitions.
1577 // FIXME: This is still incorrectly handled at the formatter side.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001578 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001579
1580 // FIXME:
1581 // This now gets parsed incorrectly as class definition.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001582 // verifyFormat("class A<int> f() {\n}\nint n;");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001583
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001584 // Elaborate types where incorrectly parsing the structural element would
1585 // break the indent.
1586 verifyFormat("if (true)\n"
1587 " class X x;\n"
1588 "else\n"
1589 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001590}
1591
Manuel Klimek407a31a2013-01-15 15:50:27 +00001592TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1593 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1594 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1595 EXPECT_EQ("#error 1", format(" # error 1"));
1596 EXPECT_EQ("#warning 1", format(" # warning 1"));
1597}
1598
Manuel Klimek525fe162013-01-18 14:04:34 +00001599TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
1600 FormatStyle AllowsMergedIf = getGoogleStyle();
1601 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
1602 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
1603 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
Manuel Klimek4c128122013-01-18 14:46:43 +00001604 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
1605 EXPECT_EQ("if (true) return 42;",
1606 format("if (true)\nreturn 42;", AllowsMergedIf));
1607 FormatStyle ShortMergedIf = AllowsMergedIf;
1608 ShortMergedIf.ColumnLimit = 25;
1609 verifyFormat("#define A \\\n"
1610 " if (true) return 42;", ShortMergedIf);
1611 verifyFormat("#define A \\\n"
1612 " f(); \\\n"
1613 " if (true)\n"
1614 "#define B", ShortMergedIf);
1615 verifyFormat("#define A \\\n"
1616 " f(); \\\n"
1617 " if (true)\n"
1618 "g();", ShortMergedIf);
Manuel Klimek0fbe0082013-01-21 14:16:56 +00001619 verifyFormat("{\n"
1620 "#ifdef A\n"
1621 " // Comment\n"
1622 " if (true) continue;\n"
1623 "#endif\n"
1624 " // Comment\n"
1625 " if (true) continue;", ShortMergedIf);
Manuel Klimek525fe162013-01-18 14:04:34 +00001626}
1627
Manuel Klimek86721d22013-01-22 16:31:55 +00001628TEST_F(FormatTest, BlockCommentsInControlLoops) {
1629 verifyFormat("if (0) /* a comment in a strange place */ {\n"
1630 " f();\n"
1631 "}");
1632 verifyFormat("if (0) /* a comment in a strange place */ {\n"
1633 " f();\n"
1634 "} /* another comment */ else /* comment #3 */ {\n"
1635 " g();\n"
1636 "}");
1637 verifyFormat("while (0) /* a comment in a strange place */ {\n"
1638 " f();\n"
1639 "}");
1640 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
1641 " f();\n"
1642 "}");
1643 verifyFormat("do /* a comment in a strange place */ {\n"
1644 " f();\n"
1645 "} /* another comment */ while (0);");
1646}
1647
1648TEST_F(FormatTest, BlockComments) {
1649 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
1650 format("/* *//* */ /* */\n/* *//* */ /* */"));
1651 EXPECT_EQ("/* */ a /* */ b;",
1652 format(" /* */ a/* */ b;"));
1653 EXPECT_EQ("#define A /* */\\\n"
1654 " b\n"
1655 "/* */\n"
1656 "someCall(\n"
1657 " parameter);",
1658 format("#define A /* */ b\n"
1659 "/* */\n"
1660 "someCall(parameter);", getLLVMStyleWithColumns(15)));
1661
1662 EXPECT_EQ("#define A\n"
1663 "/* */ someCall(\n"
1664 " parameter);",
1665 format("#define A\n"
1666 "/* */someCall(parameter);", getLLVMStyleWithColumns(15)));
1667}
1668
Nico Webercf4a79c2013-01-08 17:56:31 +00001669//===----------------------------------------------------------------------===//
1670// Objective-C tests.
1671//===----------------------------------------------------------------------===//
1672
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001673TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1674 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1675 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1676 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001677 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001678 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1679 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1680 format("-(NSInteger)Method3:(id)anObject;"));
1681 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1682 format("-(NSInteger)Method4:(id)anObject;"));
1683 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1684 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1685 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1686 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001687 EXPECT_EQ(
1688 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1689 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001690
1691 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001692 EXPECT_EQ(
1693 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1694 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1695 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1696 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1697 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1698 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1699 format(
1700 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1701 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1702 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1703 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1704 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1705 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001706
1707 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001708 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001709 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1710 // protocol lists (but not for template classes):
1711 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001712
1713 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001714 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001715
1716 // If there's no return type (very rare in practice!), LLVM and Google style
1717 // agree.
1718 verifyFormat("- foo:(int)f;");
1719 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001720}
1721
Daniel Jasper886568d2013-01-09 08:36:49 +00001722TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001723 verifyFormat("int (^Block)(int, int);");
1724 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001725}
1726
Nico Weber27d13672013-01-09 20:25:35 +00001727TEST_F(FormatTest, FormatObjCInterface) {
1728 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001729 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001730 "@public\n"
1731 " int field1;\n"
1732 "@protected\n"
1733 " int field2;\n"
1734 "@private\n"
1735 " int field3;\n"
1736 "@package\n"
1737 " int field4;\n"
1738 "}\n"
1739 "+ (id)init;\n"
1740 "@end");
1741
Nico Weber27d13672013-01-09 20:25:35 +00001742 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1743 " @public\n"
1744 " int field1;\n"
1745 " @protected\n"
1746 " int field2;\n"
1747 " @private\n"
1748 " int field3;\n"
1749 " @package\n"
1750 " int field4;\n"
1751 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001752 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001753 "@end");
1754
1755 verifyFormat("@interface Foo\n"
1756 "+ (id)init;\n"
1757 "// Look, a comment!\n"
1758 "- (int)answerWith:(int)i;\n"
1759 "@end");
1760
1761 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001762 "@end\n"
1763 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001764 "@end");
1765
1766 verifyFormat("@interface Foo : Bar\n"
1767 "+ (id)init;\n"
1768 "@end");
1769
Nico Weber5f500df2013-01-10 20:12:55 +00001770 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001771 "+ (id)init;\n"
1772 "@end");
1773
Nico Weber5f500df2013-01-10 20:12:55 +00001774 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001775 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001776 "@end");
1777
Nico Webered91bba2013-01-10 19:19:14 +00001778 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001779 "+ (id)init;\n"
1780 "@end");
1781
Nico Webered91bba2013-01-10 19:19:14 +00001782 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001783 "+ (id)init;\n"
1784 "@end");
1785
Nico Weber5f500df2013-01-10 20:12:55 +00001786 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001787 "+ (id)init;\n"
1788 "@end");
1789
Nico Weber5f500df2013-01-10 20:12:55 +00001790 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001791 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001792 "@end");
1793
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001794 verifyFormat("@interface Foo {\n"
1795 " int _i;\n"
1796 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001797 "+ (id)init;\n"
1798 "@end");
1799
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001800 verifyFormat("@interface Foo : Bar {\n"
1801 " int _i;\n"
1802 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001803 "+ (id)init;\n"
1804 "@end");
1805
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001806 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1807 " int _i;\n"
1808 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001809 "+ (id)init;\n"
1810 "@end");
1811
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001812 verifyFormat("@interface Foo (HackStuff) {\n"
1813 " int _i;\n"
1814 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001815 "+ (id)init;\n"
1816 "@end");
1817
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001818 verifyFormat("@interface Foo () {\n"
1819 " int _i;\n"
1820 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001821 "+ (id)init;\n"
1822 "@end");
1823
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001824 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1825 " int _i;\n"
1826 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001827 "+ (id)init;\n"
1828 "@end");
1829}
1830
Nico Weber50767d82013-01-09 23:25:37 +00001831TEST_F(FormatTest, FormatObjCImplementation) {
1832 verifyFormat("@implementation Foo : NSObject {\n"
1833 "@public\n"
1834 " int field1;\n"
1835 "@protected\n"
1836 " int field2;\n"
1837 "@private\n"
1838 " int field3;\n"
1839 "@package\n"
1840 " int field4;\n"
1841 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001842 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001843 "@end");
1844
1845 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1846 " @public\n"
1847 " int field1;\n"
1848 " @protected\n"
1849 " int field2;\n"
1850 " @private\n"
1851 " int field3;\n"
1852 " @package\n"
1853 " int field4;\n"
1854 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001855 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001856 "@end");
1857
1858 verifyFormat("@implementation Foo\n"
1859 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001860 " if (true)\n"
1861 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001862 "}\n"
1863 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001864 "- (int)answerWith:(int)i {\n"
1865 " return i;\n"
1866 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001867 "+ (int)answerWith:(int)i {\n"
1868 " return i;\n"
1869 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001870 "@end");
1871
1872 verifyFormat("@implementation Foo\n"
1873 "@end\n"
1874 "@implementation Bar\n"
1875 "@end");
1876
1877 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001878 "+ (id)init {\n}\n"
1879 "- (void)foo {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001880 "@end");
1881
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001882 verifyFormat("@implementation Foo {\n"
1883 " int _i;\n"
1884 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001885 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001886 "@end");
1887
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001888 verifyFormat("@implementation Foo : Bar {\n"
1889 " int _i;\n"
1890 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001891 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001892 "@end");
1893
Nico Webered91bba2013-01-10 19:19:14 +00001894 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001895 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001896 "@end");
1897}
1898
Nico Weber1abe6ea2013-01-09 21:15:03 +00001899TEST_F(FormatTest, FormatObjCProtocol) {
1900 verifyFormat("@protocol Foo\n"
1901 "@property(weak) id delegate;\n"
1902 "- (NSUInteger)numberOfThings;\n"
1903 "@end");
1904
Nico Weber5f500df2013-01-10 20:12:55 +00001905 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001906 "- (NSUInteger)numberOfThings;\n"
1907 "@end");
1908
Nico Weber5f500df2013-01-10 20:12:55 +00001909 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001910 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001911 "@end");
1912
Nico Weber1abe6ea2013-01-09 21:15:03 +00001913 verifyFormat("@protocol Foo;\n"
1914 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001915
1916 verifyFormat("@protocol Foo\n"
1917 "@end\n"
1918 "@protocol Bar\n"
1919 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001920
1921 verifyFormat("@protocol myProtocol\n"
1922 "- (void)mandatoryWithInt:(int)i;\n"
1923 "@optional\n"
1924 "- (void)optional;\n"
1925 "@required\n"
1926 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001927 "@optional\n"
1928 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001929 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001930}
1931
Nico Weberbcfdd262013-01-12 06:18:40 +00001932TEST_F(FormatTest, FormatObjCMethodExpr) {
1933 verifyFormat("[foo bar:baz];");
1934 verifyFormat("return [foo bar:baz];");
1935 verifyFormat("f([foo bar:baz]);");
1936 verifyFormat("f(2, [foo bar:baz]);");
1937 verifyFormat("f(2, a ? b : c);");
1938 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1939
1940 verifyFormat("[foo bar:baz], [foo bar:baz];");
1941 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1942 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1943 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1944 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1945 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1946 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1947 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1948 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1949 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1950 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1951 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1952 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1953 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1954 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1955 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1956 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1957 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1958 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1959 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1960 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1961 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1962 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1963 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1964 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1965 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1966 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1967 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1968 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1969 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1970 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1971 // Whew!
1972
1973 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1974 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1975 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1976 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1977 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001978 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001979 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001980
Nico Weberbcfdd262013-01-12 06:18:40 +00001981 verifyFormat("arr[[self indexForFoo:a]];");
1982 verifyFormat("throw [self errorFor:a];");
1983 verifyFormat("@throw [self errorFor:a];");
1984
Nico Webere8ccc812013-01-12 22:48:47 +00001985 // This tests that the formatter doesn't break after "backing" but before ":",
1986 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001987 verifyFormat(
1988 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001989 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1990 " backing:NSBackingStoreBuffered defer:YES]))");
1991
Nico Webere8ccc812013-01-12 22:48:47 +00001992 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1993 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001994
1995}
1996
Nico Weber581f5572013-01-07 15:56:25 +00001997TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001998 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001999 verifyFormat("@catch");
2000 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00002001 verifyFormat("@compatibility_alias");
2002 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00002003 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00002004 verifyFormat("@encode");
2005 verifyFormat("@end");
2006 verifyFormat("@finally");
2007 verifyFormat("@implementation");
2008 verifyFormat("@import");
2009 verifyFormat("@interface");
2010 verifyFormat("@optional");
2011 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00002012 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00002013 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00002014 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00002015 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00002016 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00002017 verifyFormat("@required");
2018 verifyFormat("@selector");
2019 verifyFormat("@synchronized");
2020 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00002021 verifyFormat("@throw");
2022 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00002023
Nico Webercb4d6902013-01-08 19:40:21 +00002024 verifyFormat("@\"String\"");
2025 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00002026 verifyFormat("@+4.8");
2027 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00002028 verifyFormat("@1LL");
2029 verifyFormat("@.5");
2030 verifyFormat("@'c'");
2031 verifyFormat("@true");
2032 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00002033 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00002034 verifyFormat("@[");
2035 verifyFormat("@{");
2036
Nico Weber581f5572013-01-07 15:56:25 +00002037 EXPECT_EQ("@interface", format("@ interface"));
2038
2039 // The precise formatting of this doesn't matter, nobody writes code like
2040 // this.
2041 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00002042}
2043
Nico Weberc31689a2013-01-08 19:15:23 +00002044TEST_F(FormatTest, ObjCSnippets) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +00002045 verifyFormat("@autoreleasepool {\n"
2046 " foo();\n"
2047 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00002048 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00002049 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00002050 verifyFormat("@dynamic textColor;");
Nico Weber5dfe9b42013-01-18 05:11:47 +00002051 verifyFormat("char *buf1 = @encode(int *);");
2052 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
2053 // FIXME: Enable once PR14884 is fixed:
Nico Weberc31689a2013-01-08 19:15:23 +00002054 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00002055 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weber6a21a552013-01-18 02:43:57 +00002056 verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00002057 verifyFormat("@synchronized(self) {\n"
2058 " f();\n"
2059 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00002060
Nico Weber70848232013-01-10 21:30:42 +00002061 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2062 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2063
Nico Webercf4a79c2013-01-08 17:56:31 +00002064 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00002065 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
2066 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00002067}
2068
Daniel Jaspercd162382013-01-07 13:26:07 +00002069} // end namespace tooling
2070} // end namespace clang