blob: bda3196b02ac4fff665b34dcbb06a461f40d5c49 [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 Jasper7ad4eff2013-01-07 11:09:06 +0000407 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000408 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000409
410 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000411}
412
413TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000414 verifyFormat("f(/*test=*/ true);");
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000415 EXPECT_EQ(
416 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
417 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
418 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n"
419 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
420 EXPECT_EQ(
421 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
422 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
423 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
424 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
425}
426
427TEST_F(FormatTest, CommentsInStaticInitializers) {
428 EXPECT_EQ(
429 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
430 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
431 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
432 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
433 " aaaaaaaaaaaaaaaaaaaa };",
434 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
435 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
436 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
437 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
438 " aaaaaaaaaaaaaaaaaaaa };"));
439 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
440 " bbbbbbbbbbb, ccccccccccc };");
441 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
442 " // comment for bb....\n"
443 " bbbbbbbbbbb, ccccccccccc };");
444 verifyGoogleFormat(
445 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
446 " bbbbbbbbbbb,\n"
447 " ccccccccccc };");
448 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
449 " // comment for bb....\n"
450 " bbbbbbbbbbb,\n"
451 " ccccccccccc };");
452
Alexander Kornienko15757312012-12-06 18:03:27 +0000453}
454
Alexander Kornienko15757312012-12-06 18:03:27 +0000455//===----------------------------------------------------------------------===//
456// Tests for classes, namespaces, etc.
457//===----------------------------------------------------------------------===//
458
459TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000460 verifyFormat("class A {\n};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000461}
462
463TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
464 verifyFormat("class A {\n"
465 "public:\n"
466 "protected:\n"
467 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000468 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000469 "};");
470 verifyGoogleFormat("class A {\n"
471 " public:\n"
472 " protected:\n"
473 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000474 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000475 "};");
476}
477
478TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000479 verifyFormat("class A : public B {\n};");
480 verifyFormat("class A : public ::B {\n};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000481}
482
Manuel Klimekde768542013-01-07 18:10:23 +0000483TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000484 verifyFormat("class A {\n} a, b;");
485 verifyFormat("struct A {\n} a, b;");
486 verifyFormat("union A {\n} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000487}
488
Alexander Kornienko15757312012-12-06 18:03:27 +0000489TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000490 verifyFormat("enum {\n"
491 " Zero,\n"
492 " One = 1,\n"
493 " Two = One + 1,\n"
494 " Three = (One + Two),\n"
495 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
496 " Five = (One, Two, Three, Four, 5)\n"
497 "};");
498 verifyFormat("enum Enum {\n"
499 "};");
500 verifyFormat("enum {\n"
501 "};");
502}
503
Nico Weberefaddc02013-01-14 05:49:49 +0000504TEST_F(FormatTest, FormatsBitfields) {
505 verifyFormat("struct Bitfields {\n"
506 " unsigned sClass : 8;\n"
507 " unsigned ValueKind : 2;\n"
508 "};");
509}
510
Alexander Kornienko15757312012-12-06 18:03:27 +0000511TEST_F(FormatTest, FormatsNamespaces) {
512 verifyFormat("namespace some_namespace {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000513 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000514 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000515 "}");
516 verifyFormat("namespace {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000517 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000518 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000519 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000520 verifyFormat("inline namespace X {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000521 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000522 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000523 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000524 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000525 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000526 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000527}
528
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000529TEST_F(FormatTest, FormatsExternC) {
530 verifyFormat("extern \"C\" {\nint a;");
531}
532
Nico Webera9ccdd12013-01-07 16:36:17 +0000533TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000534 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
535 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000536 verifyFormat("try {\n"
537 " throw a * b;\n"
538 "}\n"
539 "catch (int a) {\n"
540 " // Do nothing.\n"
541 "}\n"
542 "catch (...) {\n"
543 " exit(42);\n"
544 "}");
545
546 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000547 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000548 "catch (...) {\n"
549 " return 5;\n"
550 "}");
551 verifyFormat("class A {\n"
552 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000553 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000554 " catch (...) {\n"
555 " throw;\n"
556 " }\n"
557 "};\n");
558}
559
560TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000561 verifyFormat("@try {\n"
562 " f();\n"
563 "}\n"
564 "@catch (NSException e) {\n"
565 " @throw;\n"
566 "}\n"
567 "@finally {\n"
568 " exit(42);\n"
569 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000570}
571
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000572TEST_F(FormatTest, StaticInitializers) {
573 verifyFormat("static SomeClass SC = { 1, 'a' };");
574
575 // FIXME: Format like enums if the static initializer does not fit on a line.
576 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000577 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000578 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
579 "};");
580
581 verifyFormat(
582 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
583 " looooooooooooooooooooooooooooooooooongname,\n"
584 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000585}
586
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000587TEST_F(FormatTest, NestedStaticInitializers) {
588 verifyFormat("static A x = { { {} } };\n");
589 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000590 "static A x = { { { init1, init2, init3, init4 },\n"
591 " { init1, init2, init3, init4 } } };");
592
Nico Weber6a21a552013-01-18 02:43:57 +0000593 // FIXME: Fix this in general and verify that it works in LLVM style again.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000594 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000595 "somes Status::global_reps[3] = {\n"
596 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
597 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
598 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
599 "};");
600 verifyFormat(
601 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
602 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
603 " } };");
604
Nico Weber6a21a552013-01-18 02:43:57 +0000605 // FIXME: We might at some point want to handle this similar to parameter
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000606 // lists, where we have an option to put each on a single line.
607 verifyFormat("struct {\n"
608 " unsigned bit;\n"
609 " const char *const name;\n"
610 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
611 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
612}
613
Manuel Klimeka080a182013-01-02 16:30:12 +0000614TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
615 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
616 " \\\n"
617 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
618}
619
Daniel Jasper71607512013-01-07 10:48:50 +0000620TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000621 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
622 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000623}
624
Manuel Klimeka080a182013-01-02 16:30:12 +0000625TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
626 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000627 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000628}
629
630TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
631 EXPECT_EQ("#line 42 \"test\"\n",
632 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000633 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000634 format("# \\\n define \\\n A \\\n B\n",
635 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000636}
637
638TEST_F(FormatTest, EndOfFileEndsPPDirective) {
639 EXPECT_EQ("#line 42 \"test\"",
640 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000641 EXPECT_EQ("#define A B",
642 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000643}
644
Manuel Klimek060143e2013-01-02 18:33:23 +0000645TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000646 // If the macro fits in one line, we still do not get the full
647 // line, as only the next line decides whether we need an escaped newline and
648 // thus use the last column.
649 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000650
Manuel Klimekd544c572013-01-07 09:24:17 +0000651 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
652 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000653 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000654
655 verifyFormat("#define A A\n#define A A");
656 verifyFormat("#define A(X) A\n#define A A");
657
658 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
659 verifyFormat("#define Something \\\n"
660 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000661}
662
663TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000664 EXPECT_EQ("// some comment\n"
665 "#include \"a.h\"\n"
666 "#define A(A,\\\n"
667 " B)\n"
668 "#include \"b.h\"\n"
669 "// some comment\n",
670 format(" // some comment\n"
671 " #include \"a.h\"\n"
672 "#define A(A,\\\n"
673 " B)\n"
674 " #include \"b.h\"\n"
675 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000676}
677
Manuel Klimekd4397b92013-01-04 23:34:14 +0000678TEST_F(FormatTest, LayoutSingleHash) {
679 EXPECT_EQ("#\na;", format("#\na;"));
680}
681
682TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
683 EXPECT_EQ("#define A \\\n"
684 " c; \\\n"
685 " e;\n"
686 "f;", format("#define A c; e;\n"
687 "f;", getLLVMStyleWithColumns(14)));
688}
689
690TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000691 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000692}
693
694TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000695 EXPECT_EQ("# define A\\\n b;",
696 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000697}
698
699TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000700 EXPECT_EQ("int x,\n"
701 "#define A\n"
702 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000703}
704
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000705TEST_F(FormatTest, HashInMacroDefinition) {
706 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
707 verifyFormat("#define A \\\n"
708 " { \\\n"
709 " f(#c);\\\n"
710 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000711
712 verifyFormat("#define A(X) \\\n"
713 " void function##X()", getLLVMStyleWithColumns(22));
714
715 verifyFormat("#define A(a, b, c) \\\n"
716 " void a##b##c()", getLLVMStyleWithColumns(22));
717
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000718 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000719}
720
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000721TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
722 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
723}
724
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000725TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000726 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000727}
728
Manuel Klimeka5342db2013-01-06 20:07:31 +0000729TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
730 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
731 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
732 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
733 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
734}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000735
Manuel Klimek95419382013-01-07 07:56:50 +0000736TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000737 EXPECT_EQ(
738 "#define A \\\n int i; \\\n int j;",
739 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000740}
741
Manuel Klimekd544c572013-01-07 09:24:17 +0000742TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
743 verifyFormat("#define A \\\n"
744 " int v( \\\n"
745 " a); \\\n"
746 " int i;", getLLVMStyleWithColumns(11));
747}
748
Manuel Klimeka080a182013-01-02 16:30:12 +0000749TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000750 EXPECT_EQ(
751 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
752 " \\\n"
753 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
754 "\n"
755 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
756 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
757 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
758 "\\\n"
759 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
760 " \n"
761 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
762 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000763}
764
Manuel Klimek526ed112013-01-09 15:25:02 +0000765TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
766 EXPECT_EQ("int\n"
767 "#define A\n"
768 " a;",
769 format("int\n#define A\na;"));
770 verifyFormat(
771 "functionCallTo(someOtherFunction(\n"
772 " withSomeParameters, whichInSequence,\n"
773 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000774 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000775 " withMoreParamters,\n"
776 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000777 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000778}
779
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000780TEST_F(FormatTest, LayoutBlockInsideParens) {
781 EXPECT_EQ("functionCall({\n"
782 " int i;\n"
783 "});", format(" functionCall ( {int i;} );"));
784}
785
786TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000787 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000788 "int i;", format(" SOME_MACRO {int i;} int i;"));
789}
790
791TEST_F(FormatTest, LayoutNestedBlocks) {
792 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
793 " struct s {\n"
794 " int i;\n"
795 " };\n"
796 " s kBitsToOs[] = { { 10 } };\n"
797 " for (int i = 0; i < 10; ++i)\n"
798 " return;\n"
799 "}");
800}
801
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000802TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
803 EXPECT_EQ("{}", format("{}"));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000804
805 // Negative test for enum.
806 verifyFormat("enum E {\n};");
807
808 // Note that when there's a missing ';', we still join...
809 verifyFormat("enum E {}");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000810}
811
Alexander Kornienko15757312012-12-06 18:03:27 +0000812//===----------------------------------------------------------------------===//
813// Line break tests.
814//===----------------------------------------------------------------------===//
815
816TEST_F(FormatTest, FormatsFunctionDefinition) {
817 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
818 " int h, int j, int f,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000819 " int c, int ddddddddddddd) {\n}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000820}
821
822TEST_F(FormatTest, FormatsAwesomeMethodCall) {
823 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000824 "SomeLongMethodName(SomeReallyLongMethod(\n"
825 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
826 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000827}
828
Daniel Jasper1321eb52012-12-18 21:05:13 +0000829TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000830 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000831 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
832 getLLVMStyleWithColumns(45));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000833 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}",
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000834 getLLVMStyleWithColumns(44));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000835 verifyFormat("Constructor()\n"
836 " : Inttializer(FitsOnTheLine) {\n}",
837 getLLVMStyleWithColumns(43));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000838
839 verifyFormat(
840 "SomeClass::Constructor()\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000841 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000842
843 verifyFormat(
844 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000845 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000846 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000847 verifyGoogleFormat(
848 "SomeClass::Constructor()\n"
849 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
850 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000851 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper16618242013-01-16 17:00:50 +0000852 verifyGoogleFormat(
853 "SomeClass::Constructor()\n"
854 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
855 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000856 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000857
858 verifyFormat(
859 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000860 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000861 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000862
863 verifyFormat("Constructor()\n"
864 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
865 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
866 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000867 " aaaaaaaaaaaaaaaaaaaaaaa() {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000868
869 // Here a line could be saved by splitting the second initializer onto two
870 // lines, but that is not desireable.
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000871 verifyFormat(
872 "Constructor()\n"
873 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
874 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
875 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000876
877 verifyGoogleFormat("MyClass::MyClass(int var)\n"
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000878 " : some_var_(var), // 4 space indent\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000879 " some_other_var_(var + 1) { // lined up\n"
880 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000881
882 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000883 std::string input = "Constructor()\n"
884 " : aaaa(a,\n";
885 for (unsigned i = 0, e = 80; i != e; ++i) {
886 input += " a,\n";
887 }
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000888 input += " a) {\n}";
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000889 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000890}
891
Alexander Kornienko15757312012-12-06 18:03:27 +0000892TEST_F(FormatTest, BreaksAsHighAsPossible) {
893 verifyFormat(
894 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
895 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
896 " f();");
897}
898
Daniel Jasperbac016b2012-12-03 18:12:45 +0000899TEST_F(FormatTest, BreaksDesireably) {
900 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
901 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000902 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000903
904 verifyFormat(
905 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000907
908 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000911
912 verifyFormat(
913 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
915 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000917
918 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
919 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
920
Daniel Jasper723f0302013-01-02 14:40:02 +0000921 verifyFormat(
922 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
924
Daniel Jasper33182dd2012-12-05 14:57:28 +0000925 // This test case breaks on an incorrect memoization, i.e. an optimization not
926 // taking into account the StopAt value.
927 verifyFormat(
928 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000929 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
930 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
931 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000932
Daniel Jaspercd162382013-01-07 13:26:07 +0000933 verifyFormat("{\n {\n {\n"
934 " Annotation.SpaceRequiredBefore =\n"
935 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
936 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
937 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000938}
939
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000940TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
941 verifyGoogleFormat(
942 "aaaaaaaa(aaaaaaaaaaaaa,\n"
943 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
945 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
947 verifyGoogleFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +0000948 "aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa)\n"
949 " .aaaaaaaaaaaaaaaaaa();");
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000950 verifyGoogleFormat(
951 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
952 " ddddddddddddddddddddddddddddd),\n"
953 " test);");
954
955 verifyGoogleFormat(
956 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
957 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
958 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
959 verifyGoogleFormat("a(\"a\"\n"
960 " \"a\",\n"
961 " a);");
962}
963
Daniel Jasperc79afda2013-01-18 10:56:38 +0000964TEST_F(FormatTest, FormatsBuilderPattern) {
965 verifyFormat(
966 "return llvm::StringSwitch<Reference::Kind>(name)\n"
967 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
968 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
969 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
970 " .Default(ORDER_TEXT);\n");
971}
972
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000973TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
974 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
975 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +0000976 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
977 " GUARDED_BY(aaaaaaaaaaaaa);");
978 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000979 " GUARDED_BY(aaaaaaaaaaaaa) {\n}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000980}
981
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000982TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
983 verifyFormat(
984 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000985 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000986 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000987 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000988 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000989 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000990 verifyFormat(
991 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000992 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000993}
994
Daniel Jasper9cda8002013-01-07 13:08:40 +0000995TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
996 verifyFormat(
997 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
998 " SI->getAlignment(),\n"
999 " SI->getPointerAddressSpaceee());\n");
1000 verifyFormat(
1001 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
1002 " Line.Tokens.front().Tok.getLocation(),\n"
1003 " Line.Tokens.back().Tok.getLocation());");
1004}
1005
Daniel Jaspercf225b62012-12-24 13:43:52 +00001006TEST_F(FormatTest, AlignsAfterAssignments) {
1007 verifyFormat(
1008 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001009 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001010 verifyFormat(
1011 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001012 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001013 verifyFormat(
1014 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001015 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001016 verifyFormat(
1017 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001018 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001019 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +00001020 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1021 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1022 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001023}
1024
1025TEST_F(FormatTest, AlignsAfterReturn) {
1026 verifyFormat(
1027 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1028 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
1029 verifyFormat(
1030 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1031 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
1032}
1033
Daniel Jasper9c837d02013-01-09 07:06:56 +00001034TEST_F(FormatTest, BreaksConditionalExpressions) {
1035 verifyFormat(
1036 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1037 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1039 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
1040 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +00001041 verifyFormat(
1042 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
1043 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +00001044}
1045
Nico Weber7d37b8b2013-01-12 01:28:06 +00001046TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1047 verifyFormat("arr[foo ? bar : baz];");
1048 verifyFormat("f()[foo ? bar : baz];");
1049 verifyFormat("(a + b)[foo ? bar : baz];");
1050 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1051}
1052
Daniel Jasperbac016b2012-12-03 18:12:45 +00001053TEST_F(FormatTest, AlignsStringLiterals) {
1054 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1055 " \"short literal\");");
1056 verifyFormat(
1057 "looooooooooooooooooooooooongFunction(\n"
1058 " \"short literal\"\n"
1059 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1060}
1061
Alexander Kornienko15757312012-12-06 18:03:27 +00001062TEST_F(FormatTest, AlignsPipes) {
1063 verifyFormat(
1064 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1065 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1066 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1067 verifyFormat(
1068 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1069 " << aaaaaaaaaaaaaaaaaaaa;");
1070 verifyFormat(
1071 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1072 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1073 verifyFormat(
1074 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1075 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1076 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1077 verifyFormat(
1078 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1079 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1080 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1081}
1082
Daniel Jasperbac016b2012-12-03 18:12:45 +00001083TEST_F(FormatTest, UnderstandsEquals) {
1084 verifyFormat(
1085 "aaaaaaaaaaaaaaaaa =\n"
1086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1087 verifyFormat(
1088 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001090 verifyFormat(
1091 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001092 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001093 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1095 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001096
Daniel Jasper9cda8002013-01-07 13:08:40 +00001097 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001098 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001099 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001100 " 10000000) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001101}
1102
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001103TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001104 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1105 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001106
Daniel Jasper1321eb52012-12-18 21:05:13 +00001107 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1108 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001109
1110 verifyFormat(
1111 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1112 " Parameter2);");
1113
1114 verifyFormat(
1115 "ShortObject->shortFunction(\n"
1116 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1117 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1118
1119 verifyFormat("loooooooooooooongFunction(\n"
1120 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1121
1122 verifyFormat(
1123 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1124 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1125
Daniel Jasper46a46a22013-01-07 07:13:20 +00001126 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001127 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001128 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001129 verifyFormat(
1130 "aaaaaaaaaaa->aaaaaaaaa(\n"
1131 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1132 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001133}
1134
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001135TEST_F(FormatTest, WrapsTemplateDeclarations) {
1136 verifyFormat("template <typename T>\n"
1137 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1138 verifyFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +00001139 "template <typename T>\n"
1140 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1141 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001142 verifyFormat(
1143 "template <typename T>\n"
1144 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1145 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001146 verifyFormat(
1147 "template <typename T>\n"
1148 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1149 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001151 verifyFormat("template <typename T>\n"
1152 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1153 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001154 verifyFormat(
1155 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1156 " typename T4 = char>\n"
1157 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001158}
1159
Daniel Jasperbac016b2012-12-03 18:12:45 +00001160TEST_F(FormatTest, UnderstandsTemplateParameters) {
1161 verifyFormat("A<int> a;");
1162 verifyFormat("A<A<A<int> > > a;");
1163 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1164 verifyFormat("bool x = a < 1 || 2 > a;");
1165 verifyFormat("bool x = 5 < f<int>();");
1166 verifyFormat("bool x = f<int>() > 5;");
1167 verifyFormat("bool x = 5 < a<int>::x;");
1168 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1169 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1170
1171 verifyGoogleFormat("A<A<int>> a;");
1172 verifyGoogleFormat("A<A<A<int>>> a;");
1173 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1174
1175 verifyFormat("test >> a >> b;");
1176 verifyFormat("test << a >> b;");
1177
1178 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001179 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001180}
1181
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001182TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001183 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001184 verifyFormat("f(-1, -2, -3);");
1185 verifyFormat("a[-1] = 5;");
1186 verifyFormat("int a = 5 + -2;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001187 verifyFormat("if (i == -1) {\n}");
1188 verifyFormat("if (i != -1) {\n}");
1189 verifyFormat("if (i > -1) {\n}");
1190 verifyFormat("if (i < -1) {\n}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001191 verifyFormat("++(a->f());");
1192 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001193 verifyFormat("(a->f())++;");
1194 verifyFormat("a[42]++;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001195 verifyFormat("if (!(a->f())) {\n}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001196
1197 verifyFormat("a-- > b;");
1198 verifyFormat("b ? -a : c;");
1199 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001200 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001201 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001202 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001203
1204 verifyFormat("return -1;");
1205 verifyFormat("switch (a) {\n"
1206 "case -1:\n"
1207 " break;\n"
1208 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001209
1210 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1211 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001212
1213 verifyFormat("int a = /* confusing comment */ -1;");
1214 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1215 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001216}
1217
1218TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001219 verifyFormat("bool operator<();");
1220 verifyFormat("bool operator>();");
1221 verifyFormat("bool operator=();");
1222 verifyFormat("bool operator==();");
1223 verifyFormat("bool operator!=();");
1224 verifyFormat("int operator+();");
1225 verifyFormat("int operator++();");
1226 verifyFormat("bool operator();");
1227 verifyFormat("bool operator()();");
1228 verifyFormat("bool operator[]();");
1229 verifyFormat("operator bool();");
1230 verifyFormat("operator SomeType<int>();");
1231 verifyFormat("void *operator new(std::size_t size);");
1232 verifyFormat("void *operator new[](std::size_t size);");
1233 verifyFormat("void operator delete(void *ptr);");
1234 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001235}
1236
Daniel Jasper088dab52013-01-11 16:09:04 +00001237TEST_F(FormatTest, UnderstandsNewAndDelete) {
1238 verifyFormat("A *a = new A;");
1239 verifyFormat("A *a = new (placement) A;");
1240 verifyFormat("delete a;");
1241 verifyFormat("delete (A *)a;");
1242}
1243
Daniel Jasper5d334402013-01-02 08:57:10 +00001244TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001245 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001246 verifyFormat("f(a, *a);");
1247 verifyFormat("f(*a);");
1248 verifyFormat("int a = b * 10;");
1249 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001250 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001251 verifyFormat("int a += b * c;");
1252 verifyFormat("int a -= b * c;");
1253 verifyFormat("int a *= b * c;");
1254 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001255 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001256 verifyFormat("int a = *b * c;");
1257 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001258 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001259 verifyFormat("return 10 * b;");
1260 verifyFormat("return *b * *c;");
1261 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001262 verifyFormat("f(b ? *c : *d);");
1263 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001264 verifyFormat("*b = a;");
1265 verifyFormat("a * ~b;");
1266 verifyFormat("a * !b;");
1267 verifyFormat("a * +b;");
1268 verifyFormat("a * -b;");
1269 verifyFormat("a * ++b;");
1270 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001271 verifyFormat("a[4] * b;");
1272 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001273 verifyFormat("a * [self dostuff];");
1274 verifyFormat("a * (a + b);");
1275 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001276 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001277
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001278 verifyFormat("InvalidRegions[*R] = 0;");
1279
Daniel Jasper8b39c662012-12-10 18:59:13 +00001280 verifyFormat("A<int *> a;");
1281 verifyFormat("A<int **> a;");
1282 verifyFormat("A<int *, int *> a;");
1283 verifyFormat("A<int **, int **> a;");
1284
Daniel Jasper2db356d2013-01-08 20:03:18 +00001285 verifyFormat(
1286 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1288
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001289 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001290 verifyGoogleFormat("A<int*> a;");
1291 verifyGoogleFormat("A<int**> a;");
1292 verifyGoogleFormat("A<int*, int*> a;");
1293 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001294 verifyGoogleFormat("f(b ? *c : *d);");
1295 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001296 verifyGoogleFormat("Type* t = **x;");
1297 verifyGoogleFormat("Type* t = *++*x;");
1298 verifyGoogleFormat("*++*x;");
1299 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1300 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001301
1302 verifyFormat("a = *(x + y);");
1303 verifyFormat("a = &(x + y);");
1304 verifyFormat("*(x + y).call();");
1305 verifyFormat("&(x + y)->call();");
1306 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001307
1308 verifyFormat("f(b * /* confusing comment */ ++c);");
1309 verifyFormat(
1310 "int *MyValues = {\n"
1311 " *A, // Operator detection might be confused by the '{'\n"
1312 " *BB // Operator detection might be confused by previous comment\n"
1313 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001314
1315 verifyFormat("if (int *a = &b)");
1316 verifyFormat("if (int &a = *b)");
1317 verifyFormat("if (a & b[i])");
1318 verifyFormat("if (a::b::c::d & b[i])");
1319 verifyFormat("if (*b[i])");
1320 verifyFormat("if (int *a = (&b))");
1321 verifyFormat("while (int *a = &b)");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001322}
1323
Daniel Jasper4981bd02013-01-13 08:01:36 +00001324TEST_F(FormatTest, FormatsCasts) {
1325 verifyFormat("Type *A = static_cast<Type *>(P);");
1326 verifyFormat("Type *A = (Type *)P;");
1327 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1328 verifyFormat("int a = (int)(2.0f);");
1329
1330 // FIXME: These also need to be identified.
1331 verifyFormat("int a = (int) 2.0f;");
1332 verifyFormat("int a = (int) * b;");
1333
1334 // These are not casts.
1335 verifyFormat("void f(int *) {}");
1336 verifyFormat("void f(int *);");
1337 verifyFormat("void f(int *) = 0;");
1338 verifyFormat("void f(SmallVector<int>) {}");
1339 verifyFormat("void f(SmallVector<int>);");
1340 verifyFormat("void f(SmallVector<int>) = 0;");
1341}
1342
Daniel Jasper46ef8522013-01-10 13:08:12 +00001343TEST_F(FormatTest, FormatsFunctionTypes) {
1344 // FIXME: Determine the cases that need a space after the return type and fix.
1345 verifyFormat("A<bool()> a;");
1346 verifyFormat("A<SomeType()> a;");
1347 verifyFormat("A<void(*)(int, std::string)> a;");
1348
1349 verifyFormat("int(*func)(void *);");
1350}
1351
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001352TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001353 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001354 " int LoooooooooooooooongParam2) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001355 verifyFormat(
1356 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1357 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001358 " Type *T) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001359}
1360
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001361TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1362 verifyFormat("(a)->b();");
1363 verifyFormat("--a;");
1364}
1365
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001366TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001367 verifyFormat("#include <string>\n"
1368 "#include <a/b/c.h>\n"
1369 "#include \"a/b/string\"\n"
1370 "#include \"string.h\"\n"
1371 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001372 "#include <a-a>\n"
1373 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001374
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001375 verifyFormat("#import <string>");
1376 verifyFormat("#import <a/b/c.h>");
1377 verifyFormat("#import \"a/b/string\"");
1378 verifyFormat("#import \"string.h\"");
1379 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001380}
1381
Alexander Kornienko15757312012-12-06 18:03:27 +00001382//===----------------------------------------------------------------------===//
1383// Error recovery tests.
1384//===----------------------------------------------------------------------===//
1385
Daniel Jasper700e7102013-01-10 09:26:47 +00001386TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001387 verifyFormat("void f() { return; }\n42");
1388 verifyFormat("void f() {\n"
1389 " if (0)\n"
1390 " return;\n"
1391 "}\n"
1392 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001393 verifyFormat("void f() { return }\n42");
1394 verifyFormat("void f() {\n"
1395 " if (0)\n"
1396 " return\n"
1397 "}\n"
1398 "42");
1399}
1400
1401TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1402 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1403 EXPECT_EQ("void f() {\n"
1404 " if (a)\n"
1405 " return\n"
1406 "}", format("void f ( ) { if ( a ) return }"));
1407 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1408 EXPECT_EQ("namespace N {\n"
1409 "void f() {}\n"
1410 "void g()\n"
1411 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001412}
1413
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001414TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1415 verifyFormat("int aaaaaaaa =\n"
1416 " // Overly long comment\n"
1417 " b;", getLLVMStyleWithColumns(20));
1418 verifyFormat("function(\n"
1419 " ShortArgument,\n"
1420 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1421}
1422
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001423TEST_F(FormatTest, IncorrectAccessSpecifier) {
1424 verifyFormat("public:");
1425 verifyFormat("class A {\n"
1426 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001427 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001428 "};");
1429 verifyFormat("public\n"
1430 "int qwerty;");
1431 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001432 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001433 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001434 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001435 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001436 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001437}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001438
Alexander Kornienko393b0082012-12-04 15:40:36 +00001439TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1440 verifyFormat("{");
1441}
1442
1443TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001444 verifyFormat("do {\n}");
1445 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001446 "f();");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001447 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001448 "wheeee(fun);");
1449 verifyFormat("do {\n"
1450 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001451 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001452}
1453
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001454TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001455 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001456 verifyFormat("switch {\n foo;\n foo();\n}");
1457 verifyFormat("for {\n foo;\n foo();\n}");
1458 verifyFormat("while {\n foo;\n foo();\n}");
1459 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001460}
1461
Daniel Jasper1f42f112013-01-04 18:52:56 +00001462TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1463 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001464 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001465}
1466
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001467TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001468 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1469 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1470 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1471 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001472
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001473 EXPECT_EQ("{\n"
1474 " {\n"
1475 " breakme(\n"
1476 " qwe);\n"
1477 "}\n", format("{\n"
1478 " {\n"
1479 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001480 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001481}
1482
Manuel Klimek2851c162013-01-10 14:36:46 +00001483TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1484 verifyFormat(
1485 "int x = {\n"
1486 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001487 " b(alongervariable)\n"
1488 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001489}
1490
Manuel Klimekc44ee892013-01-21 10:07:49 +00001491TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
1492 verifyFormat("return (a)(b) { 1, 2, 3 };");
1493}
1494
Manuel Klimek2851c162013-01-10 14:36:46 +00001495TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1496 verifyFormat(
1497 "Aaa({\n"
1498 " int i;\n"
1499 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1500 " ccccccccccccccccc));");
1501}
1502
Manuel Klimek517e8942013-01-11 17:54:10 +00001503TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1504 verifyFormat("void f() { return 42; }");
1505 verifyFormat("void f() {\n"
1506 " // Comment\n"
1507 "}");
1508 verifyFormat("{\n"
1509 "#error {\n"
1510 " int a;\n"
1511 "}");
1512 verifyFormat("{\n"
1513 " int a;\n"
1514 "#error {\n"
1515 "}");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001516
1517 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
1518 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
1519
1520 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
1521 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
Manuel Klimek517e8942013-01-11 17:54:10 +00001522}
1523
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001524TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1525 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001526 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001527 verifyFormat("class foo a = { bar };\nint n;");
1528 verifyFormat("union foo a = { bar };\nint n;");
1529
1530 // Elaborate types inside function definitions.
1531 verifyFormat("struct foo f() {}\nint n;");
1532 verifyFormat("class foo f() {}\nint n;");
1533 verifyFormat("union foo f() {}\nint n;");
1534
1535 // Templates.
1536 verifyFormat("template <class X> void f() {}\nint n;");
1537 verifyFormat("template <struct X> void f() {}\nint n;");
1538 verifyFormat("template <union X> void f() {}\nint n;");
1539
1540 // Actual definitions...
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001541 verifyFormat("struct {\n} n;");
1542 verifyFormat(
1543 "template <template <class T, class Y>, class Z> class X {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001544 verifyFormat("union Z {\n int n;\n} x;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001545 verifyFormat("class MACRO Z {\n} n;");
1546 verifyFormat("class MACRO(X) Z {\n} n;");
1547 verifyFormat("class __attribute__(X) Z {\n} n;");
1548 verifyFormat("class __declspec(X) Z {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001549
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001550 // Redefinition from nested context:
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001551 verifyFormat("class A::B::C {\n} n;");
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001552
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001553 // Template definitions.
1554 // FIXME: This is still incorrectly handled at the formatter side.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001555 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001556
1557 // FIXME:
1558 // This now gets parsed incorrectly as class definition.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001559 // verifyFormat("class A<int> f() {\n}\nint n;");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001560
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001561 // Elaborate types where incorrectly parsing the structural element would
1562 // break the indent.
1563 verifyFormat("if (true)\n"
1564 " class X x;\n"
1565 "else\n"
1566 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001567}
1568
Manuel Klimek407a31a2013-01-15 15:50:27 +00001569TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1570 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1571 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1572 EXPECT_EQ("#error 1", format(" # error 1"));
1573 EXPECT_EQ("#warning 1", format(" # warning 1"));
1574}
1575
Manuel Klimek525fe162013-01-18 14:04:34 +00001576TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
1577 FormatStyle AllowsMergedIf = getGoogleStyle();
1578 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
1579 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
1580 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
Manuel Klimek4c128122013-01-18 14:46:43 +00001581 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
1582 EXPECT_EQ("if (true) return 42;",
1583 format("if (true)\nreturn 42;", AllowsMergedIf));
1584 FormatStyle ShortMergedIf = AllowsMergedIf;
1585 ShortMergedIf.ColumnLimit = 25;
1586 verifyFormat("#define A \\\n"
1587 " if (true) return 42;", ShortMergedIf);
1588 verifyFormat("#define A \\\n"
1589 " f(); \\\n"
1590 " if (true)\n"
1591 "#define B", ShortMergedIf);
1592 verifyFormat("#define A \\\n"
1593 " f(); \\\n"
1594 " if (true)\n"
1595 "g();", ShortMergedIf);
Manuel Klimek0fbe0082013-01-21 14:16:56 +00001596 verifyFormat("{\n"
1597 "#ifdef A\n"
1598 " // Comment\n"
1599 " if (true) continue;\n"
1600 "#endif\n"
1601 " // Comment\n"
1602 " if (true) continue;", ShortMergedIf);
Manuel Klimek525fe162013-01-18 14:04:34 +00001603}
1604
Nico Webercf4a79c2013-01-08 17:56:31 +00001605//===----------------------------------------------------------------------===//
1606// Objective-C tests.
1607//===----------------------------------------------------------------------===//
1608
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001609TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1610 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1611 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1612 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001613 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001614 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1615 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1616 format("-(NSInteger)Method3:(id)anObject;"));
1617 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1618 format("-(NSInteger)Method4:(id)anObject;"));
1619 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1620 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1621 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1622 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001623 EXPECT_EQ(
1624 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1625 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001626
1627 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001628 EXPECT_EQ(
1629 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1630 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1631 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1632 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1633 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1634 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1635 format(
1636 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1637 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1638 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1639 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1640 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1641 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001642
1643 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001644 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001645 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1646 // protocol lists (but not for template classes):
1647 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001648
1649 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001650 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001651
1652 // If there's no return type (very rare in practice!), LLVM and Google style
1653 // agree.
1654 verifyFormat("- foo:(int)f;");
1655 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001656}
1657
Daniel Jasper886568d2013-01-09 08:36:49 +00001658TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001659 verifyFormat("int (^Block)(int, int);");
1660 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001661}
1662
Nico Weber27d13672013-01-09 20:25:35 +00001663TEST_F(FormatTest, FormatObjCInterface) {
1664 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001665 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001666 "@public\n"
1667 " int field1;\n"
1668 "@protected\n"
1669 " int field2;\n"
1670 "@private\n"
1671 " int field3;\n"
1672 "@package\n"
1673 " int field4;\n"
1674 "}\n"
1675 "+ (id)init;\n"
1676 "@end");
1677
Nico Weber27d13672013-01-09 20:25:35 +00001678 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1679 " @public\n"
1680 " int field1;\n"
1681 " @protected\n"
1682 " int field2;\n"
1683 " @private\n"
1684 " int field3;\n"
1685 " @package\n"
1686 " int field4;\n"
1687 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001688 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001689 "@end");
1690
1691 verifyFormat("@interface Foo\n"
1692 "+ (id)init;\n"
1693 "// Look, a comment!\n"
1694 "- (int)answerWith:(int)i;\n"
1695 "@end");
1696
1697 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001698 "@end\n"
1699 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001700 "@end");
1701
1702 verifyFormat("@interface Foo : Bar\n"
1703 "+ (id)init;\n"
1704 "@end");
1705
Nico Weber5f500df2013-01-10 20:12:55 +00001706 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001707 "+ (id)init;\n"
1708 "@end");
1709
Nico Weber5f500df2013-01-10 20:12:55 +00001710 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001711 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001712 "@end");
1713
Nico Webered91bba2013-01-10 19:19:14 +00001714 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001715 "+ (id)init;\n"
1716 "@end");
1717
Nico Webered91bba2013-01-10 19:19:14 +00001718 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001719 "+ (id)init;\n"
1720 "@end");
1721
Nico Weber5f500df2013-01-10 20:12:55 +00001722 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001723 "+ (id)init;\n"
1724 "@end");
1725
Nico Weber5f500df2013-01-10 20:12:55 +00001726 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001727 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001728 "@end");
1729
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001730 verifyFormat("@interface Foo {\n"
1731 " int _i;\n"
1732 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001733 "+ (id)init;\n"
1734 "@end");
1735
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001736 verifyFormat("@interface Foo : Bar {\n"
1737 " int _i;\n"
1738 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001739 "+ (id)init;\n"
1740 "@end");
1741
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001742 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1743 " int _i;\n"
1744 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001745 "+ (id)init;\n"
1746 "@end");
1747
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001748 verifyFormat("@interface Foo (HackStuff) {\n"
1749 " int _i;\n"
1750 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001751 "+ (id)init;\n"
1752 "@end");
1753
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001754 verifyFormat("@interface Foo () {\n"
1755 " int _i;\n"
1756 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001757 "+ (id)init;\n"
1758 "@end");
1759
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001760 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1761 " int _i;\n"
1762 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001763 "+ (id)init;\n"
1764 "@end");
1765}
1766
Nico Weber50767d82013-01-09 23:25:37 +00001767TEST_F(FormatTest, FormatObjCImplementation) {
1768 verifyFormat("@implementation Foo : NSObject {\n"
1769 "@public\n"
1770 " int field1;\n"
1771 "@protected\n"
1772 " int field2;\n"
1773 "@private\n"
1774 " int field3;\n"
1775 "@package\n"
1776 " int field4;\n"
1777 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001778 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001779 "@end");
1780
1781 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1782 " @public\n"
1783 " int field1;\n"
1784 " @protected\n"
1785 " int field2;\n"
1786 " @private\n"
1787 " int field3;\n"
1788 " @package\n"
1789 " int field4;\n"
1790 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001791 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001792 "@end");
1793
1794 verifyFormat("@implementation Foo\n"
1795 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001796 " if (true)\n"
1797 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001798 "}\n"
1799 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001800 "- (int)answerWith:(int)i {\n"
1801 " return i;\n"
1802 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001803 "+ (int)answerWith:(int)i {\n"
1804 " return i;\n"
1805 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001806 "@end");
1807
1808 verifyFormat("@implementation Foo\n"
1809 "@end\n"
1810 "@implementation Bar\n"
1811 "@end");
1812
1813 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001814 "+ (id)init {\n}\n"
1815 "- (void)foo {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001816 "@end");
1817
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001818 verifyFormat("@implementation Foo {\n"
1819 " int _i;\n"
1820 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001821 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001822 "@end");
1823
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001824 verifyFormat("@implementation Foo : Bar {\n"
1825 " int _i;\n"
1826 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001827 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001828 "@end");
1829
Nico Webered91bba2013-01-10 19:19:14 +00001830 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001831 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001832 "@end");
1833}
1834
Nico Weber1abe6ea2013-01-09 21:15:03 +00001835TEST_F(FormatTest, FormatObjCProtocol) {
1836 verifyFormat("@protocol Foo\n"
1837 "@property(weak) id delegate;\n"
1838 "- (NSUInteger)numberOfThings;\n"
1839 "@end");
1840
Nico Weber5f500df2013-01-10 20:12:55 +00001841 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001842 "- (NSUInteger)numberOfThings;\n"
1843 "@end");
1844
Nico Weber5f500df2013-01-10 20:12:55 +00001845 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001846 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001847 "@end");
1848
Nico Weber1abe6ea2013-01-09 21:15:03 +00001849 verifyFormat("@protocol Foo;\n"
1850 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001851
1852 verifyFormat("@protocol Foo\n"
1853 "@end\n"
1854 "@protocol Bar\n"
1855 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001856
1857 verifyFormat("@protocol myProtocol\n"
1858 "- (void)mandatoryWithInt:(int)i;\n"
1859 "@optional\n"
1860 "- (void)optional;\n"
1861 "@required\n"
1862 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001863 "@optional\n"
1864 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001865 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001866}
1867
Nico Weberbcfdd262013-01-12 06:18:40 +00001868TEST_F(FormatTest, FormatObjCMethodExpr) {
1869 verifyFormat("[foo bar:baz];");
1870 verifyFormat("return [foo bar:baz];");
1871 verifyFormat("f([foo bar:baz]);");
1872 verifyFormat("f(2, [foo bar:baz]);");
1873 verifyFormat("f(2, a ? b : c);");
1874 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1875
1876 verifyFormat("[foo bar:baz], [foo bar:baz];");
1877 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1878 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1879 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1880 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1881 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1882 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1883 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1884 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1885 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1886 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1887 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1888 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1889 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1890 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1891 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1892 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1893 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1894 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1895 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1896 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1897 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1898 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1899 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1900 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1901 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1902 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1903 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1904 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1905 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1906 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1907 // Whew!
1908
1909 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1910 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1911 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1912 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1913 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001914 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001915 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001916
Nico Weberbcfdd262013-01-12 06:18:40 +00001917 verifyFormat("arr[[self indexForFoo:a]];");
1918 verifyFormat("throw [self errorFor:a];");
1919 verifyFormat("@throw [self errorFor:a];");
1920
Nico Webere8ccc812013-01-12 22:48:47 +00001921 // This tests that the formatter doesn't break after "backing" but before ":",
1922 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001923 verifyFormat(
1924 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001925 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1926 " backing:NSBackingStoreBuffered defer:YES]))");
1927
Nico Webere8ccc812013-01-12 22:48:47 +00001928 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1929 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001930
1931}
1932
Nico Weber581f5572013-01-07 15:56:25 +00001933TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001934 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001935 verifyFormat("@catch");
1936 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001937 verifyFormat("@compatibility_alias");
1938 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001939 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001940 verifyFormat("@encode");
1941 verifyFormat("@end");
1942 verifyFormat("@finally");
1943 verifyFormat("@implementation");
1944 verifyFormat("@import");
1945 verifyFormat("@interface");
1946 verifyFormat("@optional");
1947 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001948 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001949 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001950 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001951 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001952 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001953 verifyFormat("@required");
1954 verifyFormat("@selector");
1955 verifyFormat("@synchronized");
1956 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001957 verifyFormat("@throw");
1958 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001959
Nico Webercb4d6902013-01-08 19:40:21 +00001960 verifyFormat("@\"String\"");
1961 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001962 verifyFormat("@+4.8");
1963 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001964 verifyFormat("@1LL");
1965 verifyFormat("@.5");
1966 verifyFormat("@'c'");
1967 verifyFormat("@true");
1968 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001969 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001970 verifyFormat("@[");
1971 verifyFormat("@{");
1972
Nico Weber581f5572013-01-07 15:56:25 +00001973 EXPECT_EQ("@interface", format("@ interface"));
1974
1975 // The precise formatting of this doesn't matter, nobody writes code like
1976 // this.
1977 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001978}
1979
Nico Weberc31689a2013-01-08 19:15:23 +00001980TEST_F(FormatTest, ObjCSnippets) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001981 verifyFormat("@autoreleasepool {\n"
1982 " foo();\n"
1983 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001984 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001985 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001986 verifyFormat("@dynamic textColor;");
Nico Weber5dfe9b42013-01-18 05:11:47 +00001987 verifyFormat("char *buf1 = @encode(int *);");
1988 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1989 // FIXME: Enable once PR14884 is fixed:
Nico Weberc31689a2013-01-08 19:15:23 +00001990 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001991 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weber6a21a552013-01-18 02:43:57 +00001992 verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001993 verifyFormat("@synchronized(self) {\n"
1994 " f();\n"
1995 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001996
Nico Weber70848232013-01-10 21:30:42 +00001997 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1998 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1999
Nico Webercf4a79c2013-01-08 17:56:31 +00002000 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00002001 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
2002 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00002003}
2004
Daniel Jaspercd162382013-01-07 13:26:07 +00002005} // end namespace tooling
2006} // end namespace clang