blob: c9b255ce4145faaf3a1d7ec8aa8a05f1ff31354b [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 "};");
Manuel Klimek308232c2013-01-21 19:17:52 +0000502 verifyFormat("enum X E {\n} d;");
503 verifyFormat("enum __attribute__((...)) E {\n} d;");
504 verifyFormat("enum __declspec__((...)) E {\n} d;");
505 verifyFormat("enum X f() {\n a();\n return 42;\n}");
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000506}
507
Nico Weberefaddc02013-01-14 05:49:49 +0000508TEST_F(FormatTest, FormatsBitfields) {
509 verifyFormat("struct Bitfields {\n"
510 " unsigned sClass : 8;\n"
511 " unsigned ValueKind : 2;\n"
512 "};");
513}
514
Alexander Kornienko15757312012-12-06 18:03:27 +0000515TEST_F(FormatTest, FormatsNamespaces) {
516 verifyFormat("namespace some_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 "}");
520 verifyFormat("namespace {\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"
Alexander Kornienko15757312012-12-06 18:03:27 +0000523 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000524 verifyFormat("inline namespace X {\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000525 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000526 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000527 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000528 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000529 "class A {\n};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000530 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000531}
532
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000533TEST_F(FormatTest, FormatsExternC) {
534 verifyFormat("extern \"C\" {\nint a;");
535}
536
Nico Webera9ccdd12013-01-07 16:36:17 +0000537TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000538 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
539 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000540 verifyFormat("try {\n"
541 " throw a * b;\n"
542 "}\n"
543 "catch (int a) {\n"
544 " // Do nothing.\n"
545 "}\n"
546 "catch (...) {\n"
547 " exit(42);\n"
548 "}");
549
550 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000551 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000552 "catch (...) {\n"
553 " return 5;\n"
554 "}");
555 verifyFormat("class A {\n"
556 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000557 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000558 " catch (...) {\n"
559 " throw;\n"
560 " }\n"
561 "};\n");
562}
563
564TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000565 verifyFormat("@try {\n"
566 " f();\n"
567 "}\n"
568 "@catch (NSException e) {\n"
569 " @throw;\n"
570 "}\n"
571 "@finally {\n"
572 " exit(42);\n"
573 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000574}
575
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000576TEST_F(FormatTest, StaticInitializers) {
577 verifyFormat("static SomeClass SC = { 1, 'a' };");
578
579 // FIXME: Format like enums if the static initializer does not fit on a line.
580 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000581 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000582 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
583 "};");
584
585 verifyFormat(
586 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
587 " looooooooooooooooooooooooooooooooooongname,\n"
588 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000589}
590
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000591TEST_F(FormatTest, NestedStaticInitializers) {
592 verifyFormat("static A x = { { {} } };\n");
593 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000594 "static A x = { { { init1, init2, init3, init4 },\n"
595 " { init1, init2, init3, init4 } } };");
596
Nico Weber6a21a552013-01-18 02:43:57 +0000597 // FIXME: Fix this in general and verify that it works in LLVM style again.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000598 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000599 "somes Status::global_reps[3] = {\n"
600 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
601 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
602 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
603 "};");
604 verifyFormat(
605 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
606 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
607 " } };");
608
Nico Weber6a21a552013-01-18 02:43:57 +0000609 // FIXME: We might at some point want to handle this similar to parameter
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000610 // lists, where we have an option to put each on a single line.
611 verifyFormat("struct {\n"
612 " unsigned bit;\n"
613 " const char *const name;\n"
614 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
615 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
616}
617
Manuel Klimeka080a182013-01-02 16:30:12 +0000618TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
619 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
620 " \\\n"
621 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
622}
623
Daniel Jasper71607512013-01-07 10:48:50 +0000624TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000625 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
626 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000627}
628
Manuel Klimeka080a182013-01-02 16:30:12 +0000629TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
630 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000631 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000632}
633
634TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
635 EXPECT_EQ("#line 42 \"test\"\n",
636 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000637 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000638 format("# \\\n define \\\n A \\\n B\n",
639 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000640}
641
642TEST_F(FormatTest, EndOfFileEndsPPDirective) {
643 EXPECT_EQ("#line 42 \"test\"",
644 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000645 EXPECT_EQ("#define A B",
646 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000647}
648
Manuel Klimek060143e2013-01-02 18:33:23 +0000649TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000650 // If the macro fits in one line, we still do not get the full
651 // line, as only the next line decides whether we need an escaped newline and
652 // thus use the last column.
653 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000654
Manuel Klimekd544c572013-01-07 09:24:17 +0000655 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
656 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000657 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000658
659 verifyFormat("#define A A\n#define A A");
660 verifyFormat("#define A(X) A\n#define A A");
661
662 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
663 verifyFormat("#define Something \\\n"
664 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000665}
666
667TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000668 EXPECT_EQ("// some comment\n"
669 "#include \"a.h\"\n"
670 "#define A(A,\\\n"
671 " B)\n"
672 "#include \"b.h\"\n"
673 "// some comment\n",
674 format(" // some comment\n"
675 " #include \"a.h\"\n"
676 "#define A(A,\\\n"
677 " B)\n"
678 " #include \"b.h\"\n"
679 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000680}
681
Manuel Klimekd4397b92013-01-04 23:34:14 +0000682TEST_F(FormatTest, LayoutSingleHash) {
683 EXPECT_EQ("#\na;", format("#\na;"));
684}
685
686TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
687 EXPECT_EQ("#define A \\\n"
688 " c; \\\n"
689 " e;\n"
690 "f;", format("#define A c; e;\n"
691 "f;", getLLVMStyleWithColumns(14)));
692}
693
694TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000695 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000696}
697
698TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000699 EXPECT_EQ("# define A\\\n b;",
700 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000701}
702
703TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000704 EXPECT_EQ("int x,\n"
705 "#define A\n"
706 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000707}
708
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000709TEST_F(FormatTest, HashInMacroDefinition) {
710 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
711 verifyFormat("#define A \\\n"
712 " { \\\n"
713 " f(#c);\\\n"
714 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000715
716 verifyFormat("#define A(X) \\\n"
717 " void function##X()", getLLVMStyleWithColumns(22));
718
719 verifyFormat("#define A(a, b, c) \\\n"
720 " void a##b##c()", getLLVMStyleWithColumns(22));
721
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000722 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000723}
724
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000725TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
726 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
727}
728
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000729TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000730 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000731}
732
Manuel Klimeka5342db2013-01-06 20:07:31 +0000733TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
734 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
735 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
736 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
737 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
738}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000739
Manuel Klimek95419382013-01-07 07:56:50 +0000740TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000741 EXPECT_EQ(
742 "#define A \\\n int i; \\\n int j;",
743 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000744}
745
Manuel Klimekd544c572013-01-07 09:24:17 +0000746TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
747 verifyFormat("#define A \\\n"
748 " int v( \\\n"
749 " a); \\\n"
750 " int i;", getLLVMStyleWithColumns(11));
751}
752
Manuel Klimeka080a182013-01-02 16:30:12 +0000753TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000754 EXPECT_EQ(
755 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
756 " \\\n"
757 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
758 "\n"
759 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
760 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
761 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
762 "\\\n"
763 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
764 " \n"
765 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
766 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000767}
768
Manuel Klimek526ed112013-01-09 15:25:02 +0000769TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
770 EXPECT_EQ("int\n"
771 "#define A\n"
772 " a;",
773 format("int\n#define A\na;"));
774 verifyFormat(
775 "functionCallTo(someOtherFunction(\n"
776 " withSomeParameters, whichInSequence,\n"
777 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000778 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000779 " withMoreParamters,\n"
780 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000781 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000782}
783
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000784TEST_F(FormatTest, LayoutBlockInsideParens) {
785 EXPECT_EQ("functionCall({\n"
786 " int i;\n"
787 "});", format(" functionCall ( {int i;} );"));
788}
789
790TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000791 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000792 "int i;", format(" SOME_MACRO {int i;} int i;"));
793}
794
795TEST_F(FormatTest, LayoutNestedBlocks) {
796 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
797 " struct s {\n"
798 " int i;\n"
799 " };\n"
800 " s kBitsToOs[] = { { 10 } };\n"
801 " for (int i = 0; i < 10; ++i)\n"
802 " return;\n"
803 "}");
804}
805
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000806TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
807 EXPECT_EQ("{}", format("{}"));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000808
809 // Negative test for enum.
810 verifyFormat("enum E {\n};");
811
812 // Note that when there's a missing ';', we still join...
813 verifyFormat("enum E {}");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000814}
815
Alexander Kornienko15757312012-12-06 18:03:27 +0000816//===----------------------------------------------------------------------===//
817// Line break tests.
818//===----------------------------------------------------------------------===//
819
820TEST_F(FormatTest, FormatsFunctionDefinition) {
821 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
822 " int h, int j, int f,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000823 " int c, int ddddddddddddd) {\n}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000824}
825
826TEST_F(FormatTest, FormatsAwesomeMethodCall) {
827 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000828 "SomeLongMethodName(SomeReallyLongMethod(\n"
829 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
830 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000831}
832
Daniel Jasper1321eb52012-12-18 21:05:13 +0000833TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000834 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000835 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
836 getLLVMStyleWithColumns(45));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000837 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}",
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000838 getLLVMStyleWithColumns(44));
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000839 verifyFormat("Constructor()\n"
840 " : Inttializer(FitsOnTheLine) {\n}",
841 getLLVMStyleWithColumns(43));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000842
843 verifyFormat(
844 "SomeClass::Constructor()\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000845 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000846
847 verifyFormat(
848 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000849 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000850 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000851 verifyGoogleFormat(
852 "SomeClass::Constructor()\n"
853 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
854 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000855 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper16618242013-01-16 17:00:50 +0000856 verifyGoogleFormat(
857 "SomeClass::Constructor()\n"
858 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
859 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000860 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000861
862 verifyFormat(
863 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000864 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000865 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000866
867 verifyFormat("Constructor()\n"
868 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
869 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
870 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000871 " aaaaaaaaaaaaaaaaaaaaaaa() {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000872
873 // Here a line could be saved by splitting the second initializer onto two
874 // lines, but that is not desireable.
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000875 verifyFormat(
876 "Constructor()\n"
877 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
878 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
879 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000880
881 verifyGoogleFormat("MyClass::MyClass(int var)\n"
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000882 " : some_var_(var), // 4 space indent\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000883 " some_other_var_(var + 1) { // lined up\n"
884 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000885
886 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000887 std::string input = "Constructor()\n"
888 " : aaaa(a,\n";
889 for (unsigned i = 0, e = 80; i != e; ++i) {
890 input += " a,\n";
891 }
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000892 input += " a) {\n}";
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000893 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000894}
895
Alexander Kornienko15757312012-12-06 18:03:27 +0000896TEST_F(FormatTest, BreaksAsHighAsPossible) {
897 verifyFormat(
898 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
899 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
900 " f();");
901}
902
Daniel Jasperbac016b2012-12-03 18:12:45 +0000903TEST_F(FormatTest, BreaksDesireably) {
904 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
905 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000906 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000907
908 verifyFormat(
909 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000911
912 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
913 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000915
916 verifyFormat(
917 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
919 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
920 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000921
922 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
923 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
924
Daniel Jasper723f0302013-01-02 14:40:02 +0000925 verifyFormat(
926 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
927 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
928
Daniel Jasper33182dd2012-12-05 14:57:28 +0000929 // This test case breaks on an incorrect memoization, i.e. an optimization not
930 // taking into account the StopAt value.
931 verifyFormat(
932 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000933 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
934 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
935 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000936
Daniel Jaspercd162382013-01-07 13:26:07 +0000937 verifyFormat("{\n {\n {\n"
938 " Annotation.SpaceRequiredBefore =\n"
939 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
940 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
941 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000942}
943
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000944TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
945 verifyGoogleFormat(
946 "aaaaaaaa(aaaaaaaaaaaaa,\n"
947 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
948 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
949 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
950 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
951 verifyGoogleFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +0000952 "aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa)\n"
953 " .aaaaaaaaaaaaaaaaaa();");
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000954 verifyGoogleFormat(
955 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
956 " ddddddddddddddddddddddddddddd),\n"
957 " test);");
958
959 verifyGoogleFormat(
960 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
961 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
962 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
963 verifyGoogleFormat("a(\"a\"\n"
964 " \"a\",\n"
965 " a);");
966}
967
Daniel Jasperc79afda2013-01-18 10:56:38 +0000968TEST_F(FormatTest, FormatsBuilderPattern) {
969 verifyFormat(
970 "return llvm::StringSwitch<Reference::Kind>(name)\n"
971 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
972 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
973 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
974 " .Default(ORDER_TEXT);\n");
975}
976
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000977TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
978 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
979 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +0000980 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
981 " GUARDED_BY(aaaaaaaaaaaaa);");
982 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000983 " GUARDED_BY(aaaaaaaaaaaaa) {\n}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000984}
985
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000986TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
987 verifyFormat(
988 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000989 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000990 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000991 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000992 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000993 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000994 verifyFormat(
995 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000996 " ccccccccccccccccccccccccc) {\n}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000997}
998
Daniel Jasper9cda8002013-01-07 13:08:40 +0000999TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
1000 verifyFormat(
1001 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
1002 " SI->getAlignment(),\n"
1003 " SI->getPointerAddressSpaceee());\n");
1004 verifyFormat(
1005 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
1006 " Line.Tokens.front().Tok.getLocation(),\n"
1007 " Line.Tokens.back().Tok.getLocation());");
1008}
1009
Daniel Jaspercf225b62012-12-24 13:43:52 +00001010TEST_F(FormatTest, AlignsAfterAssignments) {
1011 verifyFormat(
1012 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001013 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001014 verifyFormat(
1015 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001016 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001017 verifyFormat(
1018 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001019 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001020 verifyFormat(
1021 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001022 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001023 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +00001024 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1025 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1026 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001027}
1028
1029TEST_F(FormatTest, AlignsAfterReturn) {
1030 verifyFormat(
1031 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1032 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
1033 verifyFormat(
1034 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1035 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
1036}
1037
Daniel Jasper9c837d02013-01-09 07:06:56 +00001038TEST_F(FormatTest, BreaksConditionalExpressions) {
1039 verifyFormat(
1040 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1041 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1043 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
1044 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +00001045 verifyFormat(
1046 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
1047 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +00001048}
1049
Nico Weber7d37b8b2013-01-12 01:28:06 +00001050TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1051 verifyFormat("arr[foo ? bar : baz];");
1052 verifyFormat("f()[foo ? bar : baz];");
1053 verifyFormat("(a + b)[foo ? bar : baz];");
1054 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1055}
1056
Daniel Jasperbac016b2012-12-03 18:12:45 +00001057TEST_F(FormatTest, AlignsStringLiterals) {
1058 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1059 " \"short literal\");");
1060 verifyFormat(
1061 "looooooooooooooooooooooooongFunction(\n"
1062 " \"short literal\"\n"
1063 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1064}
1065
Alexander Kornienko15757312012-12-06 18:03:27 +00001066TEST_F(FormatTest, AlignsPipes) {
1067 verifyFormat(
1068 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1069 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1070 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1071 verifyFormat(
1072 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1073 " << aaaaaaaaaaaaaaaaaaaa;");
1074 verifyFormat(
1075 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1076 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1077 verifyFormat(
1078 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1079 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1080 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1081 verifyFormat(
1082 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1083 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1084 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1085}
1086
Daniel Jasperbac016b2012-12-03 18:12:45 +00001087TEST_F(FormatTest, UnderstandsEquals) {
1088 verifyFormat(
1089 "aaaaaaaaaaaaaaaaa =\n"
1090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1091 verifyFormat(
1092 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001094 verifyFormat(
1095 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001096 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001097 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1099 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001100
Daniel Jasper9cda8002013-01-07 13:08:40 +00001101 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001102 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001103 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001104 " 10000000) {\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001105}
1106
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001107TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001108 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1109 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001110
Daniel Jasper1321eb52012-12-18 21:05:13 +00001111 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1112 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001113
1114 verifyFormat(
1115 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1116 " Parameter2);");
1117
1118 verifyFormat(
1119 "ShortObject->shortFunction(\n"
1120 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1121 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1122
1123 verifyFormat("loooooooooooooongFunction(\n"
1124 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1125
1126 verifyFormat(
1127 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1128 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1129
Daniel Jasper46a46a22013-01-07 07:13:20 +00001130 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001131 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001132 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001133 verifyFormat(
1134 "aaaaaaaaaaa->aaaaaaaaa(\n"
1135 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1136 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001137}
1138
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001139TEST_F(FormatTest, WrapsTemplateDeclarations) {
1140 verifyFormat("template <typename T>\n"
1141 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1142 verifyFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +00001143 "template <typename T>\n"
1144 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1145 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001146 verifyFormat(
1147 "template <typename T>\n"
1148 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1149 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001150 verifyFormat(
1151 "template <typename T>\n"
1152 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1153 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001155 verifyFormat("template <typename T>\n"
1156 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1157 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001158 verifyFormat(
1159 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1160 " typename T4 = char>\n"
1161 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001162}
1163
Daniel Jasperbac016b2012-12-03 18:12:45 +00001164TEST_F(FormatTest, UnderstandsTemplateParameters) {
1165 verifyFormat("A<int> a;");
1166 verifyFormat("A<A<A<int> > > a;");
1167 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1168 verifyFormat("bool x = a < 1 || 2 > a;");
1169 verifyFormat("bool x = 5 < f<int>();");
1170 verifyFormat("bool x = f<int>() > 5;");
1171 verifyFormat("bool x = 5 < a<int>::x;");
1172 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1173 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1174
1175 verifyGoogleFormat("A<A<int>> a;");
1176 verifyGoogleFormat("A<A<A<int>>> a;");
1177 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1178
1179 verifyFormat("test >> a >> b;");
1180 verifyFormat("test << a >> b;");
1181
1182 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001183 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001184}
1185
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001186TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001187 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001188 verifyFormat("f(-1, -2, -3);");
1189 verifyFormat("a[-1] = 5;");
1190 verifyFormat("int a = 5 + -2;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001191 verifyFormat("if (i == -1) {\n}");
1192 verifyFormat("if (i != -1) {\n}");
1193 verifyFormat("if (i > -1) {\n}");
1194 verifyFormat("if (i < -1) {\n}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001195 verifyFormat("++(a->f());");
1196 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001197 verifyFormat("(a->f())++;");
1198 verifyFormat("a[42]++;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001199 verifyFormat("if (!(a->f())) {\n}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001200
1201 verifyFormat("a-- > b;");
1202 verifyFormat("b ? -a : c;");
1203 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001204 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001205 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001206 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001207
1208 verifyFormat("return -1;");
1209 verifyFormat("switch (a) {\n"
1210 "case -1:\n"
1211 " break;\n"
1212 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001213
1214 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1215 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001216
1217 verifyFormat("int a = /* confusing comment */ -1;");
1218 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1219 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001220}
1221
1222TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001223 verifyFormat("bool operator<();");
1224 verifyFormat("bool operator>();");
1225 verifyFormat("bool operator=();");
1226 verifyFormat("bool operator==();");
1227 verifyFormat("bool operator!=();");
1228 verifyFormat("int operator+();");
1229 verifyFormat("int operator++();");
1230 verifyFormat("bool operator();");
1231 verifyFormat("bool operator()();");
1232 verifyFormat("bool operator[]();");
1233 verifyFormat("operator bool();");
1234 verifyFormat("operator SomeType<int>();");
1235 verifyFormat("void *operator new(std::size_t size);");
1236 verifyFormat("void *operator new[](std::size_t size);");
1237 verifyFormat("void operator delete(void *ptr);");
1238 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001239}
1240
Daniel Jasper088dab52013-01-11 16:09:04 +00001241TEST_F(FormatTest, UnderstandsNewAndDelete) {
1242 verifyFormat("A *a = new A;");
1243 verifyFormat("A *a = new (placement) A;");
1244 verifyFormat("delete a;");
1245 verifyFormat("delete (A *)a;");
1246}
1247
Daniel Jasper5d334402013-01-02 08:57:10 +00001248TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001249 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001250 verifyFormat("f(a, *a);");
1251 verifyFormat("f(*a);");
1252 verifyFormat("int a = b * 10;");
1253 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001254 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001255 verifyFormat("int a += b * c;");
1256 verifyFormat("int a -= b * c;");
1257 verifyFormat("int a *= b * c;");
1258 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001259 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001260 verifyFormat("int a = *b * c;");
1261 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001262 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001263 verifyFormat("return 10 * b;");
1264 verifyFormat("return *b * *c;");
1265 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001266 verifyFormat("f(b ? *c : *d);");
1267 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001268 verifyFormat("*b = a;");
1269 verifyFormat("a * ~b;");
1270 verifyFormat("a * !b;");
1271 verifyFormat("a * +b;");
1272 verifyFormat("a * -b;");
1273 verifyFormat("a * ++b;");
1274 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001275 verifyFormat("a[4] * b;");
1276 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001277 verifyFormat("a * [self dostuff];");
1278 verifyFormat("a * (a + b);");
1279 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001280 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001281
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001282 verifyFormat("InvalidRegions[*R] = 0;");
1283
Daniel Jasper8b39c662012-12-10 18:59:13 +00001284 verifyFormat("A<int *> a;");
1285 verifyFormat("A<int **> a;");
1286 verifyFormat("A<int *, int *> a;");
1287 verifyFormat("A<int **, int **> a;");
1288
Daniel Jasper2db356d2013-01-08 20:03:18 +00001289 verifyFormat(
1290 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1292
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001293 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001294 verifyGoogleFormat("A<int*> a;");
1295 verifyGoogleFormat("A<int**> a;");
1296 verifyGoogleFormat("A<int*, int*> a;");
1297 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001298 verifyGoogleFormat("f(b ? *c : *d);");
1299 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001300 verifyGoogleFormat("Type* t = **x;");
1301 verifyGoogleFormat("Type* t = *++*x;");
1302 verifyGoogleFormat("*++*x;");
1303 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1304 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001305
1306 verifyFormat("a = *(x + y);");
1307 verifyFormat("a = &(x + y);");
1308 verifyFormat("*(x + y).call();");
1309 verifyFormat("&(x + y)->call();");
1310 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001311
1312 verifyFormat("f(b * /* confusing comment */ ++c);");
1313 verifyFormat(
1314 "int *MyValues = {\n"
1315 " *A, // Operator detection might be confused by the '{'\n"
1316 " *BB // Operator detection might be confused by previous comment\n"
1317 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001318
1319 verifyFormat("if (int *a = &b)");
1320 verifyFormat("if (int &a = *b)");
1321 verifyFormat("if (a & b[i])");
1322 verifyFormat("if (a::b::c::d & b[i])");
1323 verifyFormat("if (*b[i])");
1324 verifyFormat("if (int *a = (&b))");
1325 verifyFormat("while (int *a = &b)");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001326}
1327
Daniel Jasper4981bd02013-01-13 08:01:36 +00001328TEST_F(FormatTest, FormatsCasts) {
1329 verifyFormat("Type *A = static_cast<Type *>(P);");
1330 verifyFormat("Type *A = (Type *)P;");
1331 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1332 verifyFormat("int a = (int)(2.0f);");
1333
1334 // FIXME: These also need to be identified.
1335 verifyFormat("int a = (int) 2.0f;");
1336 verifyFormat("int a = (int) * b;");
1337
1338 // These are not casts.
1339 verifyFormat("void f(int *) {}");
1340 verifyFormat("void f(int *);");
1341 verifyFormat("void f(int *) = 0;");
1342 verifyFormat("void f(SmallVector<int>) {}");
1343 verifyFormat("void f(SmallVector<int>);");
1344 verifyFormat("void f(SmallVector<int>) = 0;");
1345}
1346
Daniel Jasper46ef8522013-01-10 13:08:12 +00001347TEST_F(FormatTest, FormatsFunctionTypes) {
1348 // FIXME: Determine the cases that need a space after the return type and fix.
1349 verifyFormat("A<bool()> a;");
1350 verifyFormat("A<SomeType()> a;");
1351 verifyFormat("A<void(*)(int, std::string)> a;");
1352
1353 verifyFormat("int(*func)(void *);");
1354}
1355
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001356TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001357 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001358 " int LoooooooooooooooongParam2) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001359 verifyFormat(
1360 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1361 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001362 " Type *T) {\n}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001363}
1364
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001365TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1366 verifyFormat("(a)->b();");
1367 verifyFormat("--a;");
1368}
1369
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001370TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001371 verifyFormat("#include <string>\n"
1372 "#include <a/b/c.h>\n"
1373 "#include \"a/b/string\"\n"
1374 "#include \"string.h\"\n"
1375 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001376 "#include <a-a>\n"
1377 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001378
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001379 verifyFormat("#import <string>");
1380 verifyFormat("#import <a/b/c.h>");
1381 verifyFormat("#import \"a/b/string\"");
1382 verifyFormat("#import \"string.h\"");
1383 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001384}
1385
Alexander Kornienko15757312012-12-06 18:03:27 +00001386//===----------------------------------------------------------------------===//
1387// Error recovery tests.
1388//===----------------------------------------------------------------------===//
1389
Daniel Jasper700e7102013-01-10 09:26:47 +00001390TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001391 verifyFormat("void f() { return; }\n42");
1392 verifyFormat("void f() {\n"
1393 " if (0)\n"
1394 " return;\n"
1395 "}\n"
1396 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001397 verifyFormat("void f() { return }\n42");
1398 verifyFormat("void f() {\n"
1399 " if (0)\n"
1400 " return\n"
1401 "}\n"
1402 "42");
1403}
1404
1405TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1406 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1407 EXPECT_EQ("void f() {\n"
1408 " if (a)\n"
1409 " return\n"
1410 "}", format("void f ( ) { if ( a ) return }"));
1411 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1412 EXPECT_EQ("namespace N {\n"
1413 "void f() {}\n"
1414 "void g()\n"
1415 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001416}
1417
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001418TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1419 verifyFormat("int aaaaaaaa =\n"
1420 " // Overly long comment\n"
1421 " b;", getLLVMStyleWithColumns(20));
1422 verifyFormat("function(\n"
1423 " ShortArgument,\n"
1424 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1425}
1426
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001427TEST_F(FormatTest, IncorrectAccessSpecifier) {
1428 verifyFormat("public:");
1429 verifyFormat("class A {\n"
1430 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001431 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001432 "};");
1433 verifyFormat("public\n"
1434 "int qwerty;");
1435 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001436 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001437 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001438 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001439 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001440 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001441}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001442
Alexander Kornienko393b0082012-12-04 15:40:36 +00001443TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1444 verifyFormat("{");
1445}
1446
1447TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001448 verifyFormat("do {\n}");
1449 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001450 "f();");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001451 verifyFormat("do {\n}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001452 "wheeee(fun);");
1453 verifyFormat("do {\n"
1454 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001455 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001456}
1457
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001458TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001459 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001460 verifyFormat("switch {\n foo;\n foo();\n}");
1461 verifyFormat("for {\n foo;\n foo();\n}");
1462 verifyFormat("while {\n foo;\n foo();\n}");
1463 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001464}
1465
Daniel Jasper1f42f112013-01-04 18:52:56 +00001466TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1467 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001468 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001469}
1470
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001471TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001472 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1473 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1474 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1475 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001476
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001477 EXPECT_EQ("{\n"
1478 " {\n"
1479 " breakme(\n"
1480 " qwe);\n"
1481 "}\n", format("{\n"
1482 " {\n"
1483 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001484 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001485}
1486
Manuel Klimek2851c162013-01-10 14:36:46 +00001487TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1488 verifyFormat(
1489 "int x = {\n"
1490 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001491 " b(alongervariable)\n"
1492 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001493}
1494
Manuel Klimekc44ee892013-01-21 10:07:49 +00001495TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
1496 verifyFormat("return (a)(b) { 1, 2, 3 };");
1497}
1498
Manuel Klimek2851c162013-01-10 14:36:46 +00001499TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1500 verifyFormat(
1501 "Aaa({\n"
1502 " int i;\n"
1503 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1504 " ccccccccccccccccc));");
1505}
1506
Manuel Klimek517e8942013-01-11 17:54:10 +00001507TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1508 verifyFormat("void f() { return 42; }");
1509 verifyFormat("void f() {\n"
1510 " // Comment\n"
1511 "}");
1512 verifyFormat("{\n"
1513 "#error {\n"
1514 " int a;\n"
1515 "}");
1516 verifyFormat("{\n"
1517 " int a;\n"
1518 "#error {\n"
1519 "}");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001520
1521 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
1522 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
1523
1524 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
1525 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
Manuel Klimek517e8942013-01-11 17:54:10 +00001526}
1527
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001528TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1529 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001530 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001531 verifyFormat("class foo a = { bar };\nint n;");
1532 verifyFormat("union foo a = { bar };\nint n;");
1533
1534 // Elaborate types inside function definitions.
1535 verifyFormat("struct foo f() {}\nint n;");
1536 verifyFormat("class foo f() {}\nint n;");
1537 verifyFormat("union foo f() {}\nint n;");
1538
1539 // Templates.
1540 verifyFormat("template <class X> void f() {}\nint n;");
1541 verifyFormat("template <struct X> void f() {}\nint n;");
1542 verifyFormat("template <union X> void f() {}\nint n;");
1543
1544 // Actual definitions...
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001545 verifyFormat("struct {\n} n;");
1546 verifyFormat(
1547 "template <template <class T, class Y>, class Z> class X {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001548 verifyFormat("union Z {\n int n;\n} x;");
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001549 verifyFormat("class MACRO Z {\n} n;");
1550 verifyFormat("class MACRO(X) Z {\n} n;");
1551 verifyFormat("class __attribute__(X) Z {\n} n;");
1552 verifyFormat("class __declspec(X) Z {\n} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001553
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001554 // Redefinition from nested context:
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001555 verifyFormat("class A::B::C {\n} n;");
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001556
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001557 // Template definitions.
1558 // FIXME: This is still incorrectly handled at the formatter side.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001559 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001560
1561 // FIXME:
1562 // This now gets parsed incorrectly as class definition.
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001563 // verifyFormat("class A<int> f() {\n}\nint n;");
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001564
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001565 // Elaborate types where incorrectly parsing the structural element would
1566 // break the indent.
1567 verifyFormat("if (true)\n"
1568 " class X x;\n"
1569 "else\n"
1570 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001571}
1572
Manuel Klimek407a31a2013-01-15 15:50:27 +00001573TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1574 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1575 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1576 EXPECT_EQ("#error 1", format(" # error 1"));
1577 EXPECT_EQ("#warning 1", format(" # warning 1"));
1578}
1579
Manuel Klimek525fe162013-01-18 14:04:34 +00001580TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
1581 FormatStyle AllowsMergedIf = getGoogleStyle();
1582 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
1583 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
1584 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
Manuel Klimek4c128122013-01-18 14:46:43 +00001585 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
1586 EXPECT_EQ("if (true) return 42;",
1587 format("if (true)\nreturn 42;", AllowsMergedIf));
1588 FormatStyle ShortMergedIf = AllowsMergedIf;
1589 ShortMergedIf.ColumnLimit = 25;
1590 verifyFormat("#define A \\\n"
1591 " if (true) return 42;", ShortMergedIf);
1592 verifyFormat("#define A \\\n"
1593 " f(); \\\n"
1594 " if (true)\n"
1595 "#define B", ShortMergedIf);
1596 verifyFormat("#define A \\\n"
1597 " f(); \\\n"
1598 " if (true)\n"
1599 "g();", ShortMergedIf);
Manuel Klimek0fbe0082013-01-21 14:16:56 +00001600 verifyFormat("{\n"
1601 "#ifdef A\n"
1602 " // Comment\n"
1603 " if (true) continue;\n"
1604 "#endif\n"
1605 " // Comment\n"
1606 " if (true) continue;", ShortMergedIf);
Manuel Klimek525fe162013-01-18 14:04:34 +00001607}
1608
Nico Webercf4a79c2013-01-08 17:56:31 +00001609//===----------------------------------------------------------------------===//
1610// Objective-C tests.
1611//===----------------------------------------------------------------------===//
1612
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001613TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1614 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1615 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1616 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001617 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001618 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1619 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1620 format("-(NSInteger)Method3:(id)anObject;"));
1621 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1622 format("-(NSInteger)Method4:(id)anObject;"));
1623 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1624 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1625 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1626 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001627 EXPECT_EQ(
1628 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1629 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001630
1631 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001632 EXPECT_EQ(
1633 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1634 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1635 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1636 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1637 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1638 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1639 format(
1640 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1641 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1642 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1643 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1644 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1645 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001646
1647 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001648 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001649 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1650 // protocol lists (but not for template classes):
1651 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001652
1653 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001654 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001655
1656 // If there's no return type (very rare in practice!), LLVM and Google style
1657 // agree.
1658 verifyFormat("- foo:(int)f;");
1659 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001660}
1661
Daniel Jasper886568d2013-01-09 08:36:49 +00001662TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001663 verifyFormat("int (^Block)(int, int);");
1664 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001665}
1666
Nico Weber27d13672013-01-09 20:25:35 +00001667TEST_F(FormatTest, FormatObjCInterface) {
1668 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001669 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001670 "@public\n"
1671 " int field1;\n"
1672 "@protected\n"
1673 " int field2;\n"
1674 "@private\n"
1675 " int field3;\n"
1676 "@package\n"
1677 " int field4;\n"
1678 "}\n"
1679 "+ (id)init;\n"
1680 "@end");
1681
Nico Weber27d13672013-01-09 20:25:35 +00001682 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1683 " @public\n"
1684 " int field1;\n"
1685 " @protected\n"
1686 " int field2;\n"
1687 " @private\n"
1688 " int field3;\n"
1689 " @package\n"
1690 " int field4;\n"
1691 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001692 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001693 "@end");
1694
1695 verifyFormat("@interface Foo\n"
1696 "+ (id)init;\n"
1697 "// Look, a comment!\n"
1698 "- (int)answerWith:(int)i;\n"
1699 "@end");
1700
1701 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001702 "@end\n"
1703 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001704 "@end");
1705
1706 verifyFormat("@interface Foo : Bar\n"
1707 "+ (id)init;\n"
1708 "@end");
1709
Nico Weber5f500df2013-01-10 20:12:55 +00001710 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001711 "+ (id)init;\n"
1712 "@end");
1713
Nico Weber5f500df2013-01-10 20:12:55 +00001714 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001715 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001716 "@end");
1717
Nico Webered91bba2013-01-10 19:19:14 +00001718 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001719 "+ (id)init;\n"
1720 "@end");
1721
Nico Webered91bba2013-01-10 19:19:14 +00001722 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001723 "+ (id)init;\n"
1724 "@end");
1725
Nico Weber5f500df2013-01-10 20:12:55 +00001726 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001727 "+ (id)init;\n"
1728 "@end");
1729
Nico Weber5f500df2013-01-10 20:12:55 +00001730 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001731 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001732 "@end");
1733
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001734 verifyFormat("@interface Foo {\n"
1735 " int _i;\n"
1736 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001737 "+ (id)init;\n"
1738 "@end");
1739
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001740 verifyFormat("@interface Foo : Bar {\n"
1741 " int _i;\n"
1742 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001743 "+ (id)init;\n"
1744 "@end");
1745
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001746 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1747 " int _i;\n"
1748 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001749 "+ (id)init;\n"
1750 "@end");
1751
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001752 verifyFormat("@interface Foo (HackStuff) {\n"
1753 " int _i;\n"
1754 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001755 "+ (id)init;\n"
1756 "@end");
1757
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001758 verifyFormat("@interface Foo () {\n"
1759 " int _i;\n"
1760 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001761 "+ (id)init;\n"
1762 "@end");
1763
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001764 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1765 " int _i;\n"
1766 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001767 "+ (id)init;\n"
1768 "@end");
1769}
1770
Nico Weber50767d82013-01-09 23:25:37 +00001771TEST_F(FormatTest, FormatObjCImplementation) {
1772 verifyFormat("@implementation Foo : NSObject {\n"
1773 "@public\n"
1774 " int field1;\n"
1775 "@protected\n"
1776 " int field2;\n"
1777 "@private\n"
1778 " int field3;\n"
1779 "@package\n"
1780 " int field4;\n"
1781 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001782 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001783 "@end");
1784
1785 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1786 " @public\n"
1787 " int field1;\n"
1788 " @protected\n"
1789 " int field2;\n"
1790 " @private\n"
1791 " int field3;\n"
1792 " @package\n"
1793 " int field4;\n"
1794 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001795 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001796 "@end");
1797
1798 verifyFormat("@implementation Foo\n"
1799 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001800 " if (true)\n"
1801 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001802 "}\n"
1803 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001804 "- (int)answerWith:(int)i {\n"
1805 " return i;\n"
1806 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001807 "+ (int)answerWith:(int)i {\n"
1808 " return i;\n"
1809 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001810 "@end");
1811
1812 verifyFormat("@implementation Foo\n"
1813 "@end\n"
1814 "@implementation Bar\n"
1815 "@end");
1816
1817 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001818 "+ (id)init {\n}\n"
1819 "- (void)foo {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001820 "@end");
1821
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001822 verifyFormat("@implementation Foo {\n"
1823 " int _i;\n"
1824 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001825 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001826 "@end");
1827
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001828 verifyFormat("@implementation Foo : Bar {\n"
1829 " int _i;\n"
1830 "}\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001831 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001832 "@end");
1833
Nico Webered91bba2013-01-10 19:19:14 +00001834 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001835 "+ (id)init {\n}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001836 "@end");
1837}
1838
Nico Weber1abe6ea2013-01-09 21:15:03 +00001839TEST_F(FormatTest, FormatObjCProtocol) {
1840 verifyFormat("@protocol Foo\n"
1841 "@property(weak) id delegate;\n"
1842 "- (NSUInteger)numberOfThings;\n"
1843 "@end");
1844
Nico Weber5f500df2013-01-10 20:12:55 +00001845 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001846 "- (NSUInteger)numberOfThings;\n"
1847 "@end");
1848
Nico Weber5f500df2013-01-10 20:12:55 +00001849 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001850 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001851 "@end");
1852
Nico Weber1abe6ea2013-01-09 21:15:03 +00001853 verifyFormat("@protocol Foo;\n"
1854 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001855
1856 verifyFormat("@protocol Foo\n"
1857 "@end\n"
1858 "@protocol Bar\n"
1859 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001860
1861 verifyFormat("@protocol myProtocol\n"
1862 "- (void)mandatoryWithInt:(int)i;\n"
1863 "@optional\n"
1864 "- (void)optional;\n"
1865 "@required\n"
1866 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001867 "@optional\n"
1868 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001869 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001870}
1871
Nico Weberbcfdd262013-01-12 06:18:40 +00001872TEST_F(FormatTest, FormatObjCMethodExpr) {
1873 verifyFormat("[foo bar:baz];");
1874 verifyFormat("return [foo bar:baz];");
1875 verifyFormat("f([foo bar:baz]);");
1876 verifyFormat("f(2, [foo bar:baz]);");
1877 verifyFormat("f(2, a ? b : c);");
1878 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1879
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];");
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] : [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 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1908 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1909 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1910 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1911 // Whew!
1912
1913 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1914 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1915 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1916 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1917 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001918 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001919 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001920
Nico Weberbcfdd262013-01-12 06:18:40 +00001921 verifyFormat("arr[[self indexForFoo:a]];");
1922 verifyFormat("throw [self errorFor:a];");
1923 verifyFormat("@throw [self errorFor:a];");
1924
Nico Webere8ccc812013-01-12 22:48:47 +00001925 // This tests that the formatter doesn't break after "backing" but before ":",
1926 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001927 verifyFormat(
1928 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001929 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1930 " backing:NSBackingStoreBuffered defer:YES]))");
1931
Nico Webere8ccc812013-01-12 22:48:47 +00001932 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1933 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001934
1935}
1936
Nico Weber581f5572013-01-07 15:56:25 +00001937TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001938 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001939 verifyFormat("@catch");
1940 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001941 verifyFormat("@compatibility_alias");
1942 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001943 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001944 verifyFormat("@encode");
1945 verifyFormat("@end");
1946 verifyFormat("@finally");
1947 verifyFormat("@implementation");
1948 verifyFormat("@import");
1949 verifyFormat("@interface");
1950 verifyFormat("@optional");
1951 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001952 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001953 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001954 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001955 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001956 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001957 verifyFormat("@required");
1958 verifyFormat("@selector");
1959 verifyFormat("@synchronized");
1960 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001961 verifyFormat("@throw");
1962 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001963
Nico Webercb4d6902013-01-08 19:40:21 +00001964 verifyFormat("@\"String\"");
1965 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001966 verifyFormat("@+4.8");
1967 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001968 verifyFormat("@1LL");
1969 verifyFormat("@.5");
1970 verifyFormat("@'c'");
1971 verifyFormat("@true");
1972 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001973 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001974 verifyFormat("@[");
1975 verifyFormat("@{");
1976
Nico Weber581f5572013-01-07 15:56:25 +00001977 EXPECT_EQ("@interface", format("@ interface"));
1978
1979 // The precise formatting of this doesn't matter, nobody writes code like
1980 // this.
1981 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001982}
1983
Nico Weberc31689a2013-01-08 19:15:23 +00001984TEST_F(FormatTest, ObjCSnippets) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001985 verifyFormat("@autoreleasepool {\n"
1986 " foo();\n"
1987 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001988 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001989 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001990 verifyFormat("@dynamic textColor;");
Nico Weber5dfe9b42013-01-18 05:11:47 +00001991 verifyFormat("char *buf1 = @encode(int *);");
1992 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1993 // FIXME: Enable once PR14884 is fixed:
Nico Weberc31689a2013-01-08 19:15:23 +00001994 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001995 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weber6a21a552013-01-18 02:43:57 +00001996 verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001997 verifyFormat("@synchronized(self) {\n"
1998 " f();\n"
1999 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00002000
Nico Weber70848232013-01-10 21:30:42 +00002001 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2002 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2003
Nico Webercf4a79c2013-01-08 17:56:31 +00002004 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00002005 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
2006 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00002007}
2008
Daniel Jaspercd162382013-01-07 13:26:07 +00002009} // end namespace tooling
2010} // end namespace clang