blob: e75d61e662b4eac892b1929b7633c4d278860647 [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
Chandler Carruth1050e8b2012-12-04 09:45:34 +000010#include "clang/Format/Format.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000011#include "../Tooling/RewriterTestContext.h"
12#include "clang/Lex/Lexer.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000013#include "gtest/gtest.h"
14
15namespace clang {
16namespace format {
17
18class FormatTest : public ::testing::Test {
19protected:
20 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
21 const FormatStyle &Style) {
22 RewriterTestContext Context;
23 FileID ID = Context.createInMemoryFile("input.cc", Code);
24 SourceLocation Start =
25 Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
26 std::vector<CharSourceRange> Ranges(
27 1,
28 CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
Daniel Jasper46ef8522013-01-10 13:08:12 +000029 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
30 getFormattingLangOpts());
Daniel Jaspercd162382013-01-07 13:26:07 +000031 tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +000032 Ranges,
33 new IgnoringDiagConsumer());
Daniel Jasperbac016b2012-12-03 18:12:45 +000034 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
35 return Context.getRewrittenText(ID);
36 }
37
38 std::string format(llvm::StringRef Code,
39 const FormatStyle &Style = getLLVMStyle()) {
40 return format(Code, 0, Code.size(), Style);
41 }
42
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000043 std::string messUp(llvm::StringRef Code) {
44 std::string MessedUp(Code.str());
45 bool InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000046 bool InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000047 bool JustReplacedNewline = false;
48 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
49 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
50 if (JustReplacedNewline)
51 MessedUp[i - 1] = '\n';
52 InComment = true;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +000053 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
54 if (i != 0) MessedUp[i - 1] = '\n';
Manuel Klimek526ed112013-01-09 15:25:02 +000055 InPreprocessorDirective = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +000056 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
57 MessedUp[i] = ' ';
Manuel Klimek526ed112013-01-09 15:25:02 +000058 MessedUp[i + 1] = ' ';
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000059 } else if (MessedUp[i] == '\n') {
60 if (InComment) {
61 InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000062 } else if (InPreprocessorDirective) {
63 InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000064 } else {
65 JustReplacedNewline = true;
66 MessedUp[i] = ' ';
67 }
Manuel Klimekd4397b92013-01-04 23:34:14 +000068 } else if (MessedUp[i] != ' ') {
69 JustReplacedNewline = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000070 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000071 }
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000072 return MessedUp;
73 }
74
Manuel Klimek060143e2013-01-02 18:33:23 +000075 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
76 FormatStyle Style = getLLVMStyle();
77 Style.ColumnLimit = ColumnLimit;
78 return Style;
79 }
80
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000081 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
82 FormatStyle Style = getGoogleStyle();
83 Style.ColumnLimit = ColumnLimit;
84 return Style;
85 }
86
Manuel Klimek060143e2013-01-02 18:33:23 +000087 void verifyFormat(llvm::StringRef Code,
88 const FormatStyle &Style = getLLVMStyle()) {
89 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000090 }
91
92 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +000093 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +000094 }
95};
96
Manuel Klimek526ed112013-01-09 15:25:02 +000097TEST_F(FormatTest, MessUp) {
98 EXPECT_EQ("1 2 3", messUp("1 2 3"));
99 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
100 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
101 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
102 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
103}
104
Alexander Kornienko15757312012-12-06 18:03:27 +0000105//===----------------------------------------------------------------------===//
106// Basic function tests.
107//===----------------------------------------------------------------------===//
108
Daniel Jasperbac016b2012-12-03 18:12:45 +0000109TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
110 EXPECT_EQ(";", format(";"));
111}
112
113TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
114 EXPECT_EQ("int i;", format(" int i;"));
115 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
116 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
117 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
118}
119
120TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
121 EXPECT_EQ("int i;", format("int\ni;"));
122}
123
124TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000125 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000126}
127
Alexander Kornienko15757312012-12-06 18:03:27 +0000128TEST_F(FormatTest, FormatsNestedCall) {
129 verifyFormat("Method(f1, f2(f3));");
130 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper4e9008a2013-01-13 08:19:51 +0000131 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000132}
133
Daniel Jasper6b825c22013-01-16 07:19:28 +0000134TEST_F(FormatTest, ImportantSpaces) {
135 verifyFormat("vector< ::Type> v;");
136}
137
Alexander Kornienko15757312012-12-06 18:03:27 +0000138//===----------------------------------------------------------------------===//
139// Tests for control statements.
140//===----------------------------------------------------------------------===//
141
142TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000143 verifyFormat("if (true)\n f();\ng();");
144 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000145 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000146 verifyGoogleFormat("if (a)\n"
147 " // comment\n"
148 " f();");
149 verifyFormat("if (a) return;", getGoogleStyleWithColumns(14));
150 verifyFormat("if (a)\n return;", getGoogleStyleWithColumns(13));
Daniel Jasperfd0ca972013-01-14 16:02:06 +0000151 verifyFormat("if (aaaaaaaaa)\n"
Daniel Jasper55b08e72013-01-16 07:02:34 +0000152 " return;", getGoogleStyleWithColumns(14));
153 verifyGoogleFormat("if (a) // Can't merge this\n"
154 " f();\n");
155 verifyGoogleFormat("if (a) /* still don't merge */\n"
156 " f();");
157 verifyGoogleFormat("if (a) { // Never merge this\n"
158 " f();\n"
159 "}");
160 verifyGoogleFormat("if (a) { /* Never merge this */\n"
161 " f();\n"
162 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000163}
164
165TEST_F(FormatTest, ParseIfElse) {
166 verifyFormat("if (true)\n"
167 " if (true)\n"
168 " if (true)\n"
169 " f();\n"
170 " else\n"
171 " g();\n"
172 " else\n"
173 " h();\n"
174 "else\n"
175 " i();");
176 verifyFormat("if (true)\n"
177 " if (true)\n"
178 " if (true) {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000179 " if (true)\n"
180 " f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000181 " } else {\n"
182 " g();\n"
183 " }\n"
184 " else\n"
185 " h();\n"
186 "else {\n"
187 " i();\n"
188 "}");
189}
190
191TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000192 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000193 verifyFormat("if (a)\n"
194 " f();\n"
195 "else if (b)\n"
196 " g();\n"
197 "else\n"
198 " h();");
199}
200
Daniel Jasperbac016b2012-12-03 18:12:45 +0000201TEST_F(FormatTest, FormatsForLoop) {
202 verifyFormat(
203 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000204 " ++VeryVeryLongLoopVariable)\n"
205 " ;");
206 verifyFormat("for (;;)\n"
207 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000208 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000209 verifyFormat("for (;;) {\n"
210 " f();\n"
211 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000212
213 verifyFormat(
214 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
215 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000216 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000217
218 verifyFormat(
219 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000220 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000221}
222
223TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000224 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000225 verifyFormat("while (true)\n"
226 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000227 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000228 verifyFormat("while () {\n"
229 " f();\n"
230 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000231}
232
Alexander Kornienko15757312012-12-06 18:03:27 +0000233TEST_F(FormatTest, FormatsDoWhile) {
234 verifyFormat("do {\n"
235 " do_something();\n"
236 "} while (something());");
237 verifyFormat("do\n"
238 " do_something();\n"
239 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000240}
241
Alexander Kornienko15757312012-12-06 18:03:27 +0000242TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000243 verifyFormat("switch (x) {\n"
244 "case 1:\n"
245 " f();\n"
246 " break;\n"
247 "case kFoo:\n"
248 "case ns::kBar:\n"
249 "case kBaz:\n"
250 " break;\n"
251 "default:\n"
252 " g();\n"
253 " break;\n"
254 "}");
255 verifyFormat("switch (x) {\n"
256 "case 1: {\n"
257 " f();\n"
258 " break;\n"
259 "}\n"
260 "}");
261 verifyFormat("switch (test)\n"
262 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000263 verifyGoogleFormat("switch (x) {\n"
264 " case 1:\n"
265 " f();\n"
266 " break;\n"
267 " case kFoo:\n"
268 " case ns::kBar:\n"
269 " case kBaz:\n"
270 " break;\n"
271 " default:\n"
272 " g();\n"
273 " break;\n"
274 "}");
275 verifyGoogleFormat("switch (x) {\n"
276 " case 1: {\n"
277 " f();\n"
278 " break;\n"
279 " }\n"
280 "}");
281 verifyGoogleFormat("switch (test)\n"
282 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000283}
284
Alexander Kornienko15757312012-12-06 18:03:27 +0000285TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000286 verifyFormat("void f() {\n"
287 " some_code();\n"
288 "test_label:\n"
289 " some_other_code();\n"
290 " {\n"
291 " some_more_code();\n"
292 " another_label:\n"
293 " some_more_code();\n"
294 " }\n"
295 "}");
296 verifyFormat("some_code();\n"
297 "test_label:\n"
298 "some_other_code();");
299}
300
Alexander Kornienko15757312012-12-06 18:03:27 +0000301//===----------------------------------------------------------------------===//
302// Tests for comments.
303//===----------------------------------------------------------------------===//
304
305TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000306 verifyFormat("// line 1\n"
307 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000308 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000309
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000310 verifyFormat("void f() {\n"
311 " // Doesn't do anything\n"
312 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000313 verifyFormat("void f(int i, // some comment (probably for i)\n"
314 " int j, // some comment (probably for j)\n"
315 " int k); // some comment (probably for k)");
316 verifyFormat("void f(int i,\n"
317 " // some comment (probably for j)\n"
318 " int j,\n"
319 " // some comment (probably for k)\n"
320 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000321
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000322 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000323 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000324
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000325 verifyFormat("enum E {\n"
326 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000327 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000328 " VAL_B\n"
329 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000330
331 verifyFormat(
332 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000333 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000334 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
335 " // Comment inside a statement.\n"
336 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000337
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000338 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000339 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000340
341 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000342}
343
344TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000345 verifyFormat("f(/*test=*/ true);");
346}
347
Alexander Kornienko15757312012-12-06 18:03:27 +0000348//===----------------------------------------------------------------------===//
349// Tests for classes, namespaces, etc.
350//===----------------------------------------------------------------------===//
351
352TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000353 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000354}
355
356TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
357 verifyFormat("class A {\n"
358 "public:\n"
359 "protected:\n"
360 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000361 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000362 "};");
363 verifyGoogleFormat("class A {\n"
364 " public:\n"
365 " protected:\n"
366 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000367 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000368 "};");
369}
370
371TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000372 verifyFormat("class A : public B {};");
373 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000374}
375
Manuel Klimekde768542013-01-07 18:10:23 +0000376TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000377 verifyFormat("class A {} a, b;");
378 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000379 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000380}
381
Alexander Kornienko15757312012-12-06 18:03:27 +0000382TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000383 verifyFormat("enum {\n"
384 " Zero,\n"
385 " One = 1,\n"
386 " Two = One + 1,\n"
387 " Three = (One + Two),\n"
388 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
389 " Five = (One, Two, Three, Four, 5)\n"
390 "};");
391 verifyFormat("enum Enum {\n"
392 "};");
393 verifyFormat("enum {\n"
394 "};");
395}
396
Nico Weberefaddc02013-01-14 05:49:49 +0000397TEST_F(FormatTest, FormatsBitfields) {
398 verifyFormat("struct Bitfields {\n"
399 " unsigned sClass : 8;\n"
400 " unsigned ValueKind : 2;\n"
401 "};");
402}
403
Alexander Kornienko15757312012-12-06 18:03:27 +0000404TEST_F(FormatTest, FormatsNamespaces) {
405 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000406 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000407 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000408 "}");
409 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000410 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000411 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000412 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000413 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000414 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000415 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000416 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000417 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000418 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000419 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000420}
421
Nico Webera9ccdd12013-01-07 16:36:17 +0000422TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000423 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
424 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000425 verifyFormat("try {\n"
426 " throw a * b;\n"
427 "}\n"
428 "catch (int a) {\n"
429 " // Do nothing.\n"
430 "}\n"
431 "catch (...) {\n"
432 " exit(42);\n"
433 "}");
434
435 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000436 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000437 "catch (...) {\n"
438 " return 5;\n"
439 "}");
440 verifyFormat("class A {\n"
441 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000442 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000443 " catch (...) {\n"
444 " throw;\n"
445 " }\n"
446 "};\n");
447}
448
449TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000450 verifyFormat("@try {\n"
451 " f();\n"
452 "}\n"
453 "@catch (NSException e) {\n"
454 " @throw;\n"
455 "}\n"
456 "@finally {\n"
457 " exit(42);\n"
458 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000459}
460
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000461TEST_F(FormatTest, StaticInitializers) {
462 verifyFormat("static SomeClass SC = { 1, 'a' };");
463
464 // FIXME: Format like enums if the static initializer does not fit on a line.
465 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000466 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000467 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
468 "};");
469
470 verifyFormat(
471 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
472 " looooooooooooooooooooooooooooooooooongname,\n"
473 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000474}
475
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000476TEST_F(FormatTest, NestedStaticInitializers) {
477 verifyFormat("static A x = { { {} } };\n");
478 verifyFormat(
479 "static A x = {\n"
480 " { { init1, init2, init3, init4 }, { init1, init2, init3, init4 } }\n"
481 "};\n");
482 verifyFormat(
483 "somes Status::global_reps[3] = {\n"
484 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
485 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
486 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
487 "};");
488 verifyFormat(
489 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
490 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
491 " } };");
492
493 // FIXME: We might at some point want to handle this similar to parameters
494 // lists, where we have an option to put each on a single line.
495 verifyFormat("struct {\n"
496 " unsigned bit;\n"
497 " const char *const name;\n"
498 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
499 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
500}
501
Manuel Klimeka080a182013-01-02 16:30:12 +0000502TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
503 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
504 " \\\n"
505 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
506}
507
Daniel Jasper71607512013-01-07 10:48:50 +0000508TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000509 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
510 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000511}
512
Manuel Klimeka080a182013-01-02 16:30:12 +0000513TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
514 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000515 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000516}
517
518TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
519 EXPECT_EQ("#line 42 \"test\"\n",
520 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000521 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000522 format("# \\\n define \\\n A \\\n B\n",
523 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000524}
525
526TEST_F(FormatTest, EndOfFileEndsPPDirective) {
527 EXPECT_EQ("#line 42 \"test\"",
528 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000529 EXPECT_EQ("#define A B",
530 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000531}
532
Manuel Klimek060143e2013-01-02 18:33:23 +0000533TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000534 // If the macro fits in one line, we still do not get the full
535 // line, as only the next line decides whether we need an escaped newline and
536 // thus use the last column.
537 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000538
Manuel Klimekd544c572013-01-07 09:24:17 +0000539 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
540 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000541 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000542
543 verifyFormat("#define A A\n#define A A");
544 verifyFormat("#define A(X) A\n#define A A");
545
546 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
547 verifyFormat("#define Something \\\n"
548 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000549}
550
551TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000552 EXPECT_EQ("// some comment\n"
553 "#include \"a.h\"\n"
554 "#define A(A,\\\n"
555 " B)\n"
556 "#include \"b.h\"\n"
557 "// some comment\n",
558 format(" // some comment\n"
559 " #include \"a.h\"\n"
560 "#define A(A,\\\n"
561 " B)\n"
562 " #include \"b.h\"\n"
563 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000564}
565
Manuel Klimekd4397b92013-01-04 23:34:14 +0000566TEST_F(FormatTest, LayoutSingleHash) {
567 EXPECT_EQ("#\na;", format("#\na;"));
568}
569
570TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
571 EXPECT_EQ("#define A \\\n"
572 " c; \\\n"
573 " e;\n"
574 "f;", format("#define A c; e;\n"
575 "f;", getLLVMStyleWithColumns(14)));
576}
577
578TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000579 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000580}
581
582TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000583 EXPECT_EQ("# define A\\\n b;",
584 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000585}
586
587TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000588 EXPECT_EQ("int x,\n"
589 "#define A\n"
590 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000591}
592
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000593TEST_F(FormatTest, HashInMacroDefinition) {
594 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
595 verifyFormat("#define A \\\n"
596 " { \\\n"
597 " f(#c);\\\n"
598 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000599
600 verifyFormat("#define A(X) \\\n"
601 " void function##X()", getLLVMStyleWithColumns(22));
602
603 verifyFormat("#define A(a, b, c) \\\n"
604 " void a##b##c()", getLLVMStyleWithColumns(22));
605
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000606 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000607}
608
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000609TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
610 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
611}
612
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000613TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000614 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000615}
616
Manuel Klimeka5342db2013-01-06 20:07:31 +0000617TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
618 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
619 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
620 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
621 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
622}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000623
Manuel Klimek95419382013-01-07 07:56:50 +0000624TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000625 EXPECT_EQ(
626 "#define A \\\n int i; \\\n int j;",
627 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000628}
629
Manuel Klimekd544c572013-01-07 09:24:17 +0000630TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
631 verifyFormat("#define A \\\n"
632 " int v( \\\n"
633 " a); \\\n"
634 " int i;", getLLVMStyleWithColumns(11));
635}
636
Manuel Klimeka080a182013-01-02 16:30:12 +0000637TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000638 EXPECT_EQ(
639 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
640 " \\\n"
641 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
642 "\n"
643 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
644 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
645 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
646 "\\\n"
647 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
648 " \n"
649 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
650 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000651}
652
Manuel Klimek526ed112013-01-09 15:25:02 +0000653TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
654 EXPECT_EQ("int\n"
655 "#define A\n"
656 " a;",
657 format("int\n#define A\na;"));
658 verifyFormat(
659 "functionCallTo(someOtherFunction(\n"
660 " withSomeParameters, whichInSequence,\n"
661 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000662 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000663 " withMoreParamters,\n"
664 " whichStronglyInfluenceTheLayout),\n"
665 " andMoreParameters),\n"
666 " trailing);", getLLVMStyleWithColumns(69));
667}
668
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000669TEST_F(FormatTest, LayoutBlockInsideParens) {
670 EXPECT_EQ("functionCall({\n"
671 " int i;\n"
672 "});", format(" functionCall ( {int i;} );"));
673}
674
675TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000676 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000677 "int i;", format(" SOME_MACRO {int i;} int i;"));
678}
679
680TEST_F(FormatTest, LayoutNestedBlocks) {
681 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
682 " struct s {\n"
683 " int i;\n"
684 " };\n"
685 " s kBitsToOs[] = { { 10 } };\n"
686 " for (int i = 0; i < 10; ++i)\n"
687 " return;\n"
688 "}");
689}
690
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000691TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
692 EXPECT_EQ("{}", format("{}"));
693}
694
Alexander Kornienko15757312012-12-06 18:03:27 +0000695//===----------------------------------------------------------------------===//
696// Line break tests.
697//===----------------------------------------------------------------------===//
698
699TEST_F(FormatTest, FormatsFunctionDefinition) {
700 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
701 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000702 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000703}
704
705TEST_F(FormatTest, FormatsAwesomeMethodCall) {
706 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000707 "SomeLongMethodName(SomeReallyLongMethod(\n"
708 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
709 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000710}
711
Daniel Jasper1321eb52012-12-18 21:05:13 +0000712TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000713 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000714 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
715 getLLVMStyleWithColumns(45));
716 verifyFormat("Constructor()\n"
717 " : Inttializer(FitsOnTheLine) {}",
718 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000719
720 verifyFormat(
721 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000722 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000723
724 verifyFormat(
725 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000726 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
727 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
728 verifyGoogleFormat(
729 "SomeClass::Constructor()\n"
730 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
731 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
732 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
733
734 verifyFormat(
735 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000736 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000737 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000738
739 verifyFormat("Constructor()\n"
740 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
741 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
742 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000743 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000744
745 // Here a line could be saved by splitting the second initializer onto two
746 // lines, but that is not desireable.
747 verifyFormat("Constructor()\n"
748 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
749 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000750 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000751
752 verifyGoogleFormat("MyClass::MyClass(int var)\n"
753 " : some_var_(var), // 4 space indent\n"
754 " some_other_var_(var + 1) { // lined up\n"
755 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000756
757 // This test takes VERY long when memoization is broken.
758 verifyGoogleFormat(
759 "Constructor()\n"
760 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
761 " a, a, a,\n"
762 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
763 " a, a, a,\n"
764 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
765 " a, a, a,\n"
766 " a, a, a, a, a, a, a, a, a, a, a)\n"
767 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
768 " a, a, a,\n"
769 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
770 " a, a, a,\n"
771 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
772 " a, a, a,\n"
773 " a, a, a, a, a, a, a, a, a, a, a) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000774}
775
Alexander Kornienko15757312012-12-06 18:03:27 +0000776TEST_F(FormatTest, BreaksAsHighAsPossible) {
777 verifyFormat(
778 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
779 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
780 " f();");
781}
782
Daniel Jasperbac016b2012-12-03 18:12:45 +0000783TEST_F(FormatTest, BreaksDesireably) {
784 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
785 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000786 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000787
788 verifyFormat(
789 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000791
792 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
794 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000795
796 verifyFormat(
797 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
799 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000801
802 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
803 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
804
Daniel Jasper723f0302013-01-02 14:40:02 +0000805 verifyFormat(
806 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
807 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
808
Daniel Jasper33182dd2012-12-05 14:57:28 +0000809 // This test case breaks on an incorrect memoization, i.e. an optimization not
810 // taking into account the StopAt value.
811 verifyFormat(
812 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000813 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
814 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
815 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000816
Daniel Jaspercd162382013-01-07 13:26:07 +0000817 verifyFormat("{\n {\n {\n"
818 " Annotation.SpaceRequiredBefore =\n"
819 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
820 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
821 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000822}
823
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000824TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
825 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
826 " GUARDED_BY(aaaaaaaaaaaaa);");
827}
828
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000829TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
830 verifyFormat(
831 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000832 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000833 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000834 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000835 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000836 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000837 verifyFormat(
838 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000839 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000840}
841
Daniel Jasper9cda8002013-01-07 13:08:40 +0000842TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
843 verifyFormat(
844 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
845 " SI->getAlignment(),\n"
846 " SI->getPointerAddressSpaceee());\n");
847 verifyFormat(
848 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
849 " Line.Tokens.front().Tok.getLocation(),\n"
850 " Line.Tokens.back().Tok.getLocation());");
851}
852
Daniel Jaspercf225b62012-12-24 13:43:52 +0000853TEST_F(FormatTest, AlignsAfterAssignments) {
854 verifyFormat(
855 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000856 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000857 verifyFormat(
858 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000859 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000860 verifyFormat(
861 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000862 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000863 verifyFormat(
864 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000865 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000866 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000867 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
868 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
869 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000870}
871
872TEST_F(FormatTest, AlignsAfterReturn) {
873 verifyFormat(
874 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
875 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
876 verifyFormat(
877 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
878 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
879}
880
Daniel Jasper9c837d02013-01-09 07:06:56 +0000881TEST_F(FormatTest, BreaksConditionalExpressions) {
882 verifyFormat(
883 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
884 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
886 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
887 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
888}
889
Nico Weber7d37b8b2013-01-12 01:28:06 +0000890TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
891 verifyFormat("arr[foo ? bar : baz];");
892 verifyFormat("f()[foo ? bar : baz];");
893 verifyFormat("(a + b)[foo ? bar : baz];");
894 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
895}
896
Daniel Jasperbac016b2012-12-03 18:12:45 +0000897TEST_F(FormatTest, AlignsStringLiterals) {
898 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
899 " \"short literal\");");
900 verifyFormat(
901 "looooooooooooooooooooooooongFunction(\n"
902 " \"short literal\"\n"
903 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
904}
905
Alexander Kornienko15757312012-12-06 18:03:27 +0000906TEST_F(FormatTest, AlignsPipes) {
907 verifyFormat(
908 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
909 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
910 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
911 verifyFormat(
912 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
913 " << aaaaaaaaaaaaaaaaaaaa;");
914 verifyFormat(
915 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
916 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
917 verifyFormat(
918 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
919 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
920 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
921 verifyFormat(
922 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
923 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
924 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
925}
926
Daniel Jasperbac016b2012-12-03 18:12:45 +0000927TEST_F(FormatTest, UnderstandsEquals) {
928 verifyFormat(
929 "aaaaaaaaaaaaaaaaa =\n"
930 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
931 verifyFormat(
932 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000934 verifyFormat(
935 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000936 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000937 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000939
Daniel Jasper9cda8002013-01-07 13:08:40 +0000940 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000941 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000942 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000943 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000944}
945
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000946TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000947 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
948 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000949
Daniel Jasper1321eb52012-12-18 21:05:13 +0000950 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
951 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000952
953 verifyFormat(
954 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
955 " Parameter2);");
956
957 verifyFormat(
958 "ShortObject->shortFunction(\n"
959 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
960 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
961
962 verifyFormat("loooooooooooooongFunction(\n"
963 " LoooooooooooooongObject->looooooooooooooooongFunction());");
964
965 verifyFormat(
966 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
967 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
968
Daniel Jasper46a46a22013-01-07 07:13:20 +0000969 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000970 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000971 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000972 verifyFormat(
973 "aaaaaaaaaaa->aaaaaaaaa(\n"
974 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
975 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000976}
977
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000978TEST_F(FormatTest, WrapsTemplateDeclarations) {
979 verifyFormat("template <typename T>\n"
980 "virtual void loooooooooooongFunction(int Param1, int Param2);");
981 verifyFormat(
982 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
983 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
984 verifyFormat(
985 "template <typename T>\n"
986 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
987 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000988 verifyFormat(
989 "template <typename T>\n"
990 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
991 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
992 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000993 verifyFormat("template <typename T>\n"
994 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
995 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000996 verifyFormat(
997 "template <typename T1, typename T2 = char, typename T3 = char,\n"
998 " typename T4 = char>\n"
999 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001000}
1001
Daniel Jasperbac016b2012-12-03 18:12:45 +00001002TEST_F(FormatTest, UnderstandsTemplateParameters) {
1003 verifyFormat("A<int> a;");
1004 verifyFormat("A<A<A<int> > > a;");
1005 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1006 verifyFormat("bool x = a < 1 || 2 > a;");
1007 verifyFormat("bool x = 5 < f<int>();");
1008 verifyFormat("bool x = f<int>() > 5;");
1009 verifyFormat("bool x = 5 < a<int>::x;");
1010 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1011 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1012
1013 verifyGoogleFormat("A<A<int>> a;");
1014 verifyGoogleFormat("A<A<A<int>>> a;");
1015 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1016
1017 verifyFormat("test >> a >> b;");
1018 verifyFormat("test << a >> b;");
1019
1020 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001021 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001022}
1023
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001024TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001025 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001026 verifyFormat("f(-1, -2, -3);");
1027 verifyFormat("a[-1] = 5;");
1028 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001029 verifyFormat("if (i == -1) {}");
1030 verifyFormat("if (i != -1) {}");
1031 verifyFormat("if (i > -1) {}");
1032 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001033 verifyFormat("++(a->f());");
1034 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001035 verifyFormat("(a->f())++;");
1036 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001037 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001038
1039 verifyFormat("a-- > b;");
1040 verifyFormat("b ? -a : c;");
1041 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001042 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001043 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001044 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001045
1046 verifyFormat("return -1;");
1047 verifyFormat("switch (a) {\n"
1048 "case -1:\n"
1049 " break;\n"
1050 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001051
1052 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1053 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001054
1055 verifyFormat("int a = /* confusing comment */ -1;");
1056 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1057 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001058}
1059
1060TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001061 verifyFormat("bool operator<();");
1062 verifyFormat("bool operator>();");
1063 verifyFormat("bool operator=();");
1064 verifyFormat("bool operator==();");
1065 verifyFormat("bool operator!=();");
1066 verifyFormat("int operator+();");
1067 verifyFormat("int operator++();");
1068 verifyFormat("bool operator();");
1069 verifyFormat("bool operator()();");
1070 verifyFormat("bool operator[]();");
1071 verifyFormat("operator bool();");
1072 verifyFormat("operator SomeType<int>();");
1073 verifyFormat("void *operator new(std::size_t size);");
1074 verifyFormat("void *operator new[](std::size_t size);");
1075 verifyFormat("void operator delete(void *ptr);");
1076 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001077}
1078
Daniel Jasper088dab52013-01-11 16:09:04 +00001079TEST_F(FormatTest, UnderstandsNewAndDelete) {
1080 verifyFormat("A *a = new A;");
1081 verifyFormat("A *a = new (placement) A;");
1082 verifyFormat("delete a;");
1083 verifyFormat("delete (A *)a;");
1084}
1085
Daniel Jasper5d334402013-01-02 08:57:10 +00001086TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001087 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001088 verifyFormat("f(a, *a);");
1089 verifyFormat("f(*a);");
1090 verifyFormat("int a = b * 10;");
1091 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001092 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001093 verifyFormat("int a += b * c;");
1094 verifyFormat("int a -= b * c;");
1095 verifyFormat("int a *= b * c;");
1096 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001097 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001098 verifyFormat("int a = *b * c;");
1099 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001100 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001101 verifyFormat("return 10 * b;");
1102 verifyFormat("return *b * *c;");
1103 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001104 verifyFormat("f(b ? *c : *d);");
1105 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001106 verifyFormat("*b = a;");
1107 verifyFormat("a * ~b;");
1108 verifyFormat("a * !b;");
1109 verifyFormat("a * +b;");
1110 verifyFormat("a * -b;");
1111 verifyFormat("a * ++b;");
1112 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001113 verifyFormat("a[4] * b;");
1114 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001115 verifyFormat("a * [self dostuff];");
1116 verifyFormat("a * (a + b);");
1117 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001118 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001119
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001120 verifyFormat("InvalidRegions[*R] = 0;");
1121
Daniel Jasper8b39c662012-12-10 18:59:13 +00001122 verifyFormat("A<int *> a;");
1123 verifyFormat("A<int **> a;");
1124 verifyFormat("A<int *, int *> a;");
1125 verifyFormat("A<int **, int **> a;");
1126
Daniel Jasper2db356d2013-01-08 20:03:18 +00001127 verifyFormat(
1128 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1130
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001131 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001132 verifyGoogleFormat("A<int*> a;");
1133 verifyGoogleFormat("A<int**> a;");
1134 verifyGoogleFormat("A<int*, int*> a;");
1135 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001136 verifyGoogleFormat("f(b ? *c : *d);");
1137 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001138
1139 verifyFormat("a = *(x + y);");
1140 verifyFormat("a = &(x + y);");
1141 verifyFormat("*(x + y).call();");
1142 verifyFormat("&(x + y)->call();");
1143 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001144
1145 verifyFormat("f(b * /* confusing comment */ ++c);");
1146 verifyFormat(
1147 "int *MyValues = {\n"
1148 " *A, // Operator detection might be confused by the '{'\n"
1149 " *BB // Operator detection might be confused by previous comment\n"
1150 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001151}
1152
Daniel Jasper4981bd02013-01-13 08:01:36 +00001153TEST_F(FormatTest, FormatsCasts) {
1154 verifyFormat("Type *A = static_cast<Type *>(P);");
1155 verifyFormat("Type *A = (Type *)P;");
1156 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1157 verifyFormat("int a = (int)(2.0f);");
1158
1159 // FIXME: These also need to be identified.
1160 verifyFormat("int a = (int) 2.0f;");
1161 verifyFormat("int a = (int) * b;");
1162
1163 // These are not casts.
1164 verifyFormat("void f(int *) {}");
1165 verifyFormat("void f(int *);");
1166 verifyFormat("void f(int *) = 0;");
1167 verifyFormat("void f(SmallVector<int>) {}");
1168 verifyFormat("void f(SmallVector<int>);");
1169 verifyFormat("void f(SmallVector<int>) = 0;");
1170}
1171
Daniel Jasper46ef8522013-01-10 13:08:12 +00001172TEST_F(FormatTest, FormatsFunctionTypes) {
1173 // FIXME: Determine the cases that need a space after the return type and fix.
1174 verifyFormat("A<bool()> a;");
1175 verifyFormat("A<SomeType()> a;");
1176 verifyFormat("A<void(*)(int, std::string)> a;");
1177
1178 verifyFormat("int(*func)(void *);");
1179}
1180
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001181TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001182 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001183 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001184 verifyFormat(
1185 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1186 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001187 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001188}
1189
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001190TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1191 verifyFormat("(a)->b();");
1192 verifyFormat("--a;");
1193}
1194
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001195TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001196 verifyFormat("#include <string>\n"
1197 "#include <a/b/c.h>\n"
1198 "#include \"a/b/string\"\n"
1199 "#include \"string.h\"\n"
1200 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001201 "#include <a-a>\n"
1202 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001203
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001204 verifyFormat("#import <string>");
1205 verifyFormat("#import <a/b/c.h>");
1206 verifyFormat("#import \"a/b/string\"");
1207 verifyFormat("#import \"string.h\"");
1208 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001209}
1210
Alexander Kornienko15757312012-12-06 18:03:27 +00001211//===----------------------------------------------------------------------===//
1212// Error recovery tests.
1213//===----------------------------------------------------------------------===//
1214
Daniel Jasper700e7102013-01-10 09:26:47 +00001215TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001216 verifyFormat("void f() { return; }\n42");
1217 verifyFormat("void f() {\n"
1218 " if (0)\n"
1219 " return;\n"
1220 "}\n"
1221 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001222 verifyFormat("void f() { return }\n42");
1223 verifyFormat("void f() {\n"
1224 " if (0)\n"
1225 " return\n"
1226 "}\n"
1227 "42");
1228}
1229
1230TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1231 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1232 EXPECT_EQ("void f() {\n"
1233 " if (a)\n"
1234 " return\n"
1235 "}", format("void f ( ) { if ( a ) return }"));
1236 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1237 EXPECT_EQ("namespace N {\n"
1238 "void f() {}\n"
1239 "void g()\n"
1240 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001241}
1242
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001243TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1244 verifyFormat("int aaaaaaaa =\n"
1245 " // Overly long comment\n"
1246 " b;", getLLVMStyleWithColumns(20));
1247 verifyFormat("function(\n"
1248 " ShortArgument,\n"
1249 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1250}
1251
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001252TEST_F(FormatTest, IncorrectAccessSpecifier) {
1253 verifyFormat("public:");
1254 verifyFormat("class A {\n"
1255 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001256 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001257 "};");
1258 verifyFormat("public\n"
1259 "int qwerty;");
1260 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001261 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001262 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001263 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001264 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001265 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001266}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001267
Alexander Kornienko393b0082012-12-04 15:40:36 +00001268TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1269 verifyFormat("{");
1270}
1271
1272TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001273 verifyFormat("do {}");
1274 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001275 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001276 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001277 "wheeee(fun);");
1278 verifyFormat("do {\n"
1279 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001280 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001281}
1282
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001283TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001284 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001285 verifyFormat("switch {\n foo;\n foo();\n}");
1286 verifyFormat("for {\n foo;\n foo();\n}");
1287 verifyFormat("while {\n foo;\n foo();\n}");
1288 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001289}
1290
Daniel Jasper1f42f112013-01-04 18:52:56 +00001291TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1292 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001293 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001294}
1295
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001296TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001297 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1298 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1299 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1300 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001301
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001302 EXPECT_EQ("{\n"
1303 " {\n"
1304 " breakme(\n"
1305 " qwe);\n"
1306 "}\n", format("{\n"
1307 " {\n"
1308 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001309 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001310}
1311
Manuel Klimek2851c162013-01-10 14:36:46 +00001312TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1313 verifyFormat(
1314 "int x = {\n"
1315 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001316 " b(alongervariable)\n"
1317 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001318}
1319
1320TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1321 verifyFormat(
1322 "Aaa({\n"
1323 " int i;\n"
1324 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1325 " ccccccccccccccccc));");
1326}
1327
Manuel Klimek517e8942013-01-11 17:54:10 +00001328TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1329 verifyFormat("void f() { return 42; }");
1330 verifyFormat("void f() {\n"
1331 " // Comment\n"
1332 "}");
1333 verifyFormat("{\n"
1334 "#error {\n"
1335 " int a;\n"
1336 "}");
1337 verifyFormat("{\n"
1338 " int a;\n"
1339 "#error {\n"
1340 "}");
1341}
1342
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001343TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1344 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001345 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001346 verifyFormat("class foo a = { bar };\nint n;");
1347 verifyFormat("union foo a = { bar };\nint n;");
1348
1349 // Elaborate types inside function definitions.
1350 verifyFormat("struct foo f() {}\nint n;");
1351 verifyFormat("class foo f() {}\nint n;");
1352 verifyFormat("union foo f() {}\nint n;");
1353
1354 // Templates.
1355 verifyFormat("template <class X> void f() {}\nint n;");
1356 verifyFormat("template <struct X> void f() {}\nint n;");
1357 verifyFormat("template <union X> void f() {}\nint n;");
1358
1359 // Actual definitions...
1360 verifyFormat("struct {} n;");
1361 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1362 verifyFormat("union Z {\n int n;\n} x;");
1363 verifyFormat("class MACRO Z {} n;");
1364 verifyFormat("class MACRO(X) Z {} n;");
1365 verifyFormat("class __attribute__(X) Z {} n;");
1366 verifyFormat("class __declspec(X) Z {} n;");
1367
1368 // Elaborate types where incorrectly parsing the structural element would
1369 // break the indent.
1370 verifyFormat("if (true)\n"
1371 " class X x;\n"
1372 "else\n"
1373 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001374}
1375
Manuel Klimek407a31a2013-01-15 15:50:27 +00001376TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1377 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1378 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1379 EXPECT_EQ("#error 1", format(" # error 1"));
1380 EXPECT_EQ("#warning 1", format(" # warning 1"));
1381}
1382
Manuel Klimek517e8942013-01-11 17:54:10 +00001383// FIXME: This breaks the order of the unwrapped lines:
1384// TEST_F(FormatTest, OrderUnwrappedLines) {
1385// verifyFormat("{\n"
1386// " bool a; //\n"
1387// "#error {\n"
1388// " int a;\n"
1389// "}");
1390// }
1391
Nico Webercf4a79c2013-01-08 17:56:31 +00001392//===----------------------------------------------------------------------===//
1393// Objective-C tests.
1394//===----------------------------------------------------------------------===//
1395
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001396TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1397 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1398 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1399 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001400 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001401 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1402 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1403 format("-(NSInteger)Method3:(id)anObject;"));
1404 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1405 format("-(NSInteger)Method4:(id)anObject;"));
1406 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1407 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1408 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1409 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001410 EXPECT_EQ(
1411 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1412 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001413
1414 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001415 EXPECT_EQ(
1416 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1417 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1418 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1419 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1420 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1421 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1422 format(
1423 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1424 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1425 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1426 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1427 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1428 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001429
1430 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001431 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001432 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1433 // protocol lists (but not for template classes):
1434 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001435
1436 verifyFormat("- (int(*)())foo:(int(*)())f;");
1437 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1438
1439 // If there's no return type (very rare in practice!), LLVM and Google style
1440 // agree.
1441 verifyFormat("- foo:(int)f;");
1442 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001443}
1444
Daniel Jasper886568d2013-01-09 08:36:49 +00001445TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001446 verifyFormat("int (^Block)(int, int);");
1447 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001448}
1449
Nico Weber27d13672013-01-09 20:25:35 +00001450TEST_F(FormatTest, FormatObjCInterface) {
1451 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001452 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001453 "@public\n"
1454 " int field1;\n"
1455 "@protected\n"
1456 " int field2;\n"
1457 "@private\n"
1458 " int field3;\n"
1459 "@package\n"
1460 " int field4;\n"
1461 "}\n"
1462 "+ (id)init;\n"
1463 "@end");
1464
Nico Weber27d13672013-01-09 20:25:35 +00001465 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1466 " @public\n"
1467 " int field1;\n"
1468 " @protected\n"
1469 " int field2;\n"
1470 " @private\n"
1471 " int field3;\n"
1472 " @package\n"
1473 " int field4;\n"
1474 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001475 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001476 "@end");
1477
1478 verifyFormat("@interface Foo\n"
1479 "+ (id)init;\n"
1480 "// Look, a comment!\n"
1481 "- (int)answerWith:(int)i;\n"
1482 "@end");
1483
1484 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001485 "@end\n"
1486 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001487 "@end");
1488
1489 verifyFormat("@interface Foo : Bar\n"
1490 "+ (id)init;\n"
1491 "@end");
1492
Nico Weber5f500df2013-01-10 20:12:55 +00001493 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001494 "+ (id)init;\n"
1495 "@end");
1496
Nico Weber5f500df2013-01-10 20:12:55 +00001497 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001498 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001499 "@end");
1500
Nico Webered91bba2013-01-10 19:19:14 +00001501 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001502 "+ (id)init;\n"
1503 "@end");
1504
Nico Webered91bba2013-01-10 19:19:14 +00001505 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001506 "+ (id)init;\n"
1507 "@end");
1508
Nico Weber5f500df2013-01-10 20:12:55 +00001509 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001510 "+ (id)init;\n"
1511 "@end");
1512
Nico Weber5f500df2013-01-10 20:12:55 +00001513 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001514 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001515 "@end");
1516
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001517 verifyFormat("@interface Foo {\n"
1518 " int _i;\n"
1519 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001520 "+ (id)init;\n"
1521 "@end");
1522
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001523 verifyFormat("@interface Foo : Bar {\n"
1524 " int _i;\n"
1525 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001526 "+ (id)init;\n"
1527 "@end");
1528
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001529 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1530 " int _i;\n"
1531 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001532 "+ (id)init;\n"
1533 "@end");
1534
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001535 verifyFormat("@interface Foo (HackStuff) {\n"
1536 " int _i;\n"
1537 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001538 "+ (id)init;\n"
1539 "@end");
1540
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001541 verifyFormat("@interface Foo () {\n"
1542 " int _i;\n"
1543 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001544 "+ (id)init;\n"
1545 "@end");
1546
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001547 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1548 " int _i;\n"
1549 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001550 "+ (id)init;\n"
1551 "@end");
1552}
1553
Nico Weber50767d82013-01-09 23:25:37 +00001554TEST_F(FormatTest, FormatObjCImplementation) {
1555 verifyFormat("@implementation Foo : NSObject {\n"
1556 "@public\n"
1557 " int field1;\n"
1558 "@protected\n"
1559 " int field2;\n"
1560 "@private\n"
1561 " int field3;\n"
1562 "@package\n"
1563 " int field4;\n"
1564 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001565 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001566 "@end");
1567
1568 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1569 " @public\n"
1570 " int field1;\n"
1571 " @protected\n"
1572 " int field2;\n"
1573 " @private\n"
1574 " int field3;\n"
1575 " @package\n"
1576 " int field4;\n"
1577 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001578 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001579 "@end");
1580
1581 verifyFormat("@implementation Foo\n"
1582 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001583 " if (true)\n"
1584 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001585 "}\n"
1586 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001587 "- (int)answerWith:(int)i {\n"
1588 " return i;\n"
1589 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001590 "+ (int)answerWith:(int)i {\n"
1591 " return i;\n"
1592 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001593 "@end");
1594
1595 verifyFormat("@implementation Foo\n"
1596 "@end\n"
1597 "@implementation Bar\n"
1598 "@end");
1599
1600 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001601 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001602 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001603 "@end");
1604
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001605 verifyFormat("@implementation Foo {\n"
1606 " int _i;\n"
1607 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001608 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001609 "@end");
1610
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001611 verifyFormat("@implementation Foo : Bar {\n"
1612 " int _i;\n"
1613 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001614 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001615 "@end");
1616
Nico Webered91bba2013-01-10 19:19:14 +00001617 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001618 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001619 "@end");
1620}
1621
Nico Weber1abe6ea2013-01-09 21:15:03 +00001622TEST_F(FormatTest, FormatObjCProtocol) {
1623 verifyFormat("@protocol Foo\n"
1624 "@property(weak) id delegate;\n"
1625 "- (NSUInteger)numberOfThings;\n"
1626 "@end");
1627
Nico Weber5f500df2013-01-10 20:12:55 +00001628 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001629 "- (NSUInteger)numberOfThings;\n"
1630 "@end");
1631
Nico Weber5f500df2013-01-10 20:12:55 +00001632 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001633 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001634 "@end");
1635
Nico Weber1abe6ea2013-01-09 21:15:03 +00001636 verifyFormat("@protocol Foo;\n"
1637 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001638
1639 verifyFormat("@protocol Foo\n"
1640 "@end\n"
1641 "@protocol Bar\n"
1642 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001643
1644 verifyFormat("@protocol myProtocol\n"
1645 "- (void)mandatoryWithInt:(int)i;\n"
1646 "@optional\n"
1647 "- (void)optional;\n"
1648 "@required\n"
1649 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001650 "@optional\n"
1651 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001652 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001653}
1654
Nico Weberbcfdd262013-01-12 06:18:40 +00001655TEST_F(FormatTest, FormatObjCMethodExpr) {
1656 verifyFormat("[foo bar:baz];");
1657 verifyFormat("return [foo bar:baz];");
1658 verifyFormat("f([foo bar:baz]);");
1659 verifyFormat("f(2, [foo bar:baz]);");
1660 verifyFormat("f(2, a ? b : c);");
1661 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1662
1663 verifyFormat("[foo bar:baz], [foo bar:baz];");
1664 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1665 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1666 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1667 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1668 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1669 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1670 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1671 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1672 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1673 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1674 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1675 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1676 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1677 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1678 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1679 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1680 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1681 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1682 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1683 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1684 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1685 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1686 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1687 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1688 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1689 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1690 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1691 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1692 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1693 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1694 // Whew!
1695
1696 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1697 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1698 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1699 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1700 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001701 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001702 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001703
Nico Weberbcfdd262013-01-12 06:18:40 +00001704 verifyFormat("arr[[self indexForFoo:a]];");
1705 verifyFormat("throw [self errorFor:a];");
1706 verifyFormat("@throw [self errorFor:a];");
1707
Nico Webere8ccc812013-01-12 22:48:47 +00001708 // This tests that the formatter doesn't break after "backing" but before ":",
1709 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001710 verifyFormat(
1711 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001712 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1713 " backing:NSBackingStoreBuffered defer:YES]))");
1714
Nico Webere8ccc812013-01-12 22:48:47 +00001715 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1716 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001717
1718}
1719
Nico Weber581f5572013-01-07 15:56:25 +00001720TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001721 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001722 verifyFormat("@catch");
1723 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001724 verifyFormat("@compatibility_alias");
1725 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001726 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001727 verifyFormat("@encode");
1728 verifyFormat("@end");
1729 verifyFormat("@finally");
1730 verifyFormat("@implementation");
1731 verifyFormat("@import");
1732 verifyFormat("@interface");
1733 verifyFormat("@optional");
1734 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001735 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001736 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001737 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001738 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001739 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001740 verifyFormat("@required");
1741 verifyFormat("@selector");
1742 verifyFormat("@synchronized");
1743 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001744 verifyFormat("@throw");
1745 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001746
Nico Webercb4d6902013-01-08 19:40:21 +00001747 verifyFormat("@\"String\"");
1748 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001749 verifyFormat("@+4.8");
1750 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001751 verifyFormat("@1LL");
1752 verifyFormat("@.5");
1753 verifyFormat("@'c'");
1754 verifyFormat("@true");
1755 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001756 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001757 verifyFormat("@[");
1758 verifyFormat("@{");
1759
Nico Weber581f5572013-01-07 15:56:25 +00001760 EXPECT_EQ("@interface", format("@ interface"));
1761
1762 // The precise formatting of this doesn't matter, nobody writes code like
1763 // this.
1764 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001765}
1766
Nico Weberc31689a2013-01-08 19:15:23 +00001767TEST_F(FormatTest, ObjCSnippets) {
1768 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001769 verifyFormat("@autoreleasepool {\n"
1770 " foo();\n"
1771 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001772 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001773 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001774 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001775 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001776 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001777 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001778 verifyFormat("@synchronized(self) {\n"
1779 " f();\n"
1780 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001781
Nico Weber70848232013-01-10 21:30:42 +00001782 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1783 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1784
Nico Webercf4a79c2013-01-08 17:56:31 +00001785 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001786 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1787 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001788}
1789
Daniel Jaspercd162382013-01-07 13:26:07 +00001790} // end namespace tooling
1791} // end namespace clang