blob: 8e55ddb5d0fc6ed489e8b3a965213b66ddb98db8 [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
81 void verifyFormat(llvm::StringRef Code,
82 const FormatStyle &Style = getLLVMStyle()) {
83 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000084 }
85
86 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +000087 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +000088 }
89};
90
Manuel Klimek526ed112013-01-09 15:25:02 +000091TEST_F(FormatTest, MessUp) {
92 EXPECT_EQ("1 2 3", messUp("1 2 3"));
93 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
94 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
95 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
96 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
97}
98
Alexander Kornienko15757312012-12-06 18:03:27 +000099//===----------------------------------------------------------------------===//
100// Basic function tests.
101//===----------------------------------------------------------------------===//
102
Daniel Jasperbac016b2012-12-03 18:12:45 +0000103TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
104 EXPECT_EQ(";", format(";"));
105}
106
107TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
108 EXPECT_EQ("int i;", format(" int i;"));
109 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
110 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
111 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
112}
113
114TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
115 EXPECT_EQ("int i;", format("int\ni;"));
116}
117
118TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000119 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000120}
121
Alexander Kornienko15757312012-12-06 18:03:27 +0000122TEST_F(FormatTest, FormatsNestedCall) {
123 verifyFormat("Method(f1, f2(f3));");
124 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper4e9008a2013-01-13 08:19:51 +0000125 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000126}
127
Alexander Kornienko15757312012-12-06 18:03:27 +0000128//===----------------------------------------------------------------------===//
129// Tests for control statements.
130//===----------------------------------------------------------------------===//
131
132TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
Daniel Jasperfeb18f52013-01-14 14:14:23 +0000133 verifyFormat("if (true) f();\ng();");
134 verifyFormat("if (a)\n if (b)\n if (c) g();\nh();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000135 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000136 verifyFormat("if (a)\n"
137 " // comment\n"
138 " f();");
Daniel Jasperfeb18f52013-01-14 14:14:23 +0000139 verifyFormat("if (a) return;", getLLVMStyleWithColumns(14));
140 verifyFormat("if (a)\n return;", getLLVMStyleWithColumns(13));
Daniel Jasperfd0ca972013-01-14 16:02:06 +0000141 verifyFormat("if (aaaaaaaaa)\n"
142 " return;", getLLVMStyleWithColumns(14));
Alexander Kornienko15757312012-12-06 18:03:27 +0000143}
144
145TEST_F(FormatTest, ParseIfElse) {
146 verifyFormat("if (true)\n"
147 " if (true)\n"
148 " if (true)\n"
149 " f();\n"
150 " else\n"
151 " g();\n"
152 " else\n"
153 " h();\n"
154 "else\n"
155 " i();");
156 verifyFormat("if (true)\n"
157 " if (true)\n"
158 " if (true) {\n"
Daniel Jasperfeb18f52013-01-14 14:14:23 +0000159 " if (true) f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000160 " } else {\n"
161 " g();\n"
162 " }\n"
163 " else\n"
164 " h();\n"
165 "else {\n"
166 " i();\n"
167 "}");
168}
169
170TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000171 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000172 verifyFormat("if (a)\n"
173 " f();\n"
174 "else if (b)\n"
175 " g();\n"
176 "else\n"
177 " h();");
178}
179
Daniel Jasperbac016b2012-12-03 18:12:45 +0000180TEST_F(FormatTest, FormatsForLoop) {
181 verifyFormat(
182 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000183 " ++VeryVeryLongLoopVariable)\n"
184 " ;");
185 verifyFormat("for (;;)\n"
186 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000187 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000188 verifyFormat("for (;;) {\n"
189 " f();\n"
190 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000191
192 verifyFormat(
193 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
194 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000195 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000196
197 verifyFormat(
198 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000199 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000200}
201
202TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000203 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000204 verifyFormat("while (true)\n"
205 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000206 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000207 verifyFormat("while () {\n"
208 " f();\n"
209 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000210}
211
Alexander Kornienko15757312012-12-06 18:03:27 +0000212TEST_F(FormatTest, FormatsDoWhile) {
213 verifyFormat("do {\n"
214 " do_something();\n"
215 "} while (something());");
216 verifyFormat("do\n"
217 " do_something();\n"
218 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000219}
220
Alexander Kornienko15757312012-12-06 18:03:27 +0000221TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000222 verifyFormat("switch (x) {\n"
223 "case 1:\n"
224 " f();\n"
225 " break;\n"
226 "case kFoo:\n"
227 "case ns::kBar:\n"
228 "case kBaz:\n"
229 " break;\n"
230 "default:\n"
231 " g();\n"
232 " break;\n"
233 "}");
234 verifyFormat("switch (x) {\n"
235 "case 1: {\n"
236 " f();\n"
237 " break;\n"
238 "}\n"
239 "}");
240 verifyFormat("switch (test)\n"
241 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000242 verifyGoogleFormat("switch (x) {\n"
243 " case 1:\n"
244 " f();\n"
245 " break;\n"
246 " case kFoo:\n"
247 " case ns::kBar:\n"
248 " case kBaz:\n"
249 " break;\n"
250 " default:\n"
251 " g();\n"
252 " break;\n"
253 "}");
254 verifyGoogleFormat("switch (x) {\n"
255 " case 1: {\n"
256 " f();\n"
257 " break;\n"
258 " }\n"
259 "}");
260 verifyGoogleFormat("switch (test)\n"
261 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000262}
263
Alexander Kornienko15757312012-12-06 18:03:27 +0000264TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000265 verifyFormat("void f() {\n"
266 " some_code();\n"
267 "test_label:\n"
268 " some_other_code();\n"
269 " {\n"
270 " some_more_code();\n"
271 " another_label:\n"
272 " some_more_code();\n"
273 " }\n"
274 "}");
275 verifyFormat("some_code();\n"
276 "test_label:\n"
277 "some_other_code();");
278}
279
Alexander Kornienko15757312012-12-06 18:03:27 +0000280//===----------------------------------------------------------------------===//
281// Tests for comments.
282//===----------------------------------------------------------------------===//
283
284TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000285 verifyFormat("// line 1\n"
286 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000287 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000288
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000289 verifyFormat("void f() {\n"
290 " // Doesn't do anything\n"
291 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000292 verifyFormat("void f(int i, // some comment (probably for i)\n"
293 " int j, // some comment (probably for j)\n"
294 " int k); // some comment (probably for k)");
295 verifyFormat("void f(int i,\n"
296 " // some comment (probably for j)\n"
297 " int j,\n"
298 " // some comment (probably for k)\n"
299 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000300
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000301 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000302 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000303
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000304 verifyFormat("enum E {\n"
305 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000306 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000307 " VAL_B\n"
308 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000309
310 verifyFormat(
311 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000312 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000313 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
314 " // Comment inside a statement.\n"
315 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000316
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000317 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000318 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000319
320 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000321}
322
323TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000324 verifyFormat("f(/*test=*/ true);");
325}
326
Alexander Kornienko15757312012-12-06 18:03:27 +0000327//===----------------------------------------------------------------------===//
328// Tests for classes, namespaces, etc.
329//===----------------------------------------------------------------------===//
330
331TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000332 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000333}
334
335TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
336 verifyFormat("class A {\n"
337 "public:\n"
338 "protected:\n"
339 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000340 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000341 "};");
342 verifyGoogleFormat("class A {\n"
343 " public:\n"
344 " protected:\n"
345 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000346 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000347 "};");
348}
349
350TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000351 verifyFormat("class A : public B {};");
352 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000353}
354
Manuel Klimekde768542013-01-07 18:10:23 +0000355TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000356 verifyFormat("class A {} a, b;");
357 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000358 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000359}
360
Alexander Kornienko15757312012-12-06 18:03:27 +0000361TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000362 verifyFormat("enum {\n"
363 " Zero,\n"
364 " One = 1,\n"
365 " Two = One + 1,\n"
366 " Three = (One + Two),\n"
367 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
368 " Five = (One, Two, Three, Four, 5)\n"
369 "};");
370 verifyFormat("enum Enum {\n"
371 "};");
372 verifyFormat("enum {\n"
373 "};");
374}
375
Nico Weberefaddc02013-01-14 05:49:49 +0000376TEST_F(FormatTest, FormatsBitfields) {
377 verifyFormat("struct Bitfields {\n"
378 " unsigned sClass : 8;\n"
379 " unsigned ValueKind : 2;\n"
380 "};");
381}
382
Alexander Kornienko15757312012-12-06 18:03:27 +0000383TEST_F(FormatTest, FormatsNamespaces) {
384 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000385 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000386 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000387 "}");
388 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000389 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000390 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000391 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000392 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000393 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000394 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000395 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000396 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000397 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000398 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000399}
400
Nico Webera9ccdd12013-01-07 16:36:17 +0000401TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000402 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
403 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000404 verifyFormat("try {\n"
405 " throw a * b;\n"
406 "}\n"
407 "catch (int a) {\n"
408 " // Do nothing.\n"
409 "}\n"
410 "catch (...) {\n"
411 " exit(42);\n"
412 "}");
413
414 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000415 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000416 "catch (...) {\n"
417 " return 5;\n"
418 "}");
419 verifyFormat("class A {\n"
420 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000421 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000422 " catch (...) {\n"
423 " throw;\n"
424 " }\n"
425 "};\n");
426}
427
428TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000429 verifyFormat("@try {\n"
430 " f();\n"
431 "}\n"
432 "@catch (NSException e) {\n"
433 " @throw;\n"
434 "}\n"
435 "@finally {\n"
436 " exit(42);\n"
437 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000438}
439
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000440TEST_F(FormatTest, StaticInitializers) {
441 verifyFormat("static SomeClass SC = { 1, 'a' };");
442
443 // FIXME: Format like enums if the static initializer does not fit on a line.
444 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000445 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000446 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
447 "};");
448
449 verifyFormat(
450 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
451 " looooooooooooooooooooooooooooooooooongname,\n"
452 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000453}
454
Manuel Klimeka080a182013-01-02 16:30:12 +0000455TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
456 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
457 " \\\n"
458 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
459}
460
Daniel Jasper71607512013-01-07 10:48:50 +0000461TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000462 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
463 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000464}
465
Manuel Klimeka080a182013-01-02 16:30:12 +0000466TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
467 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000468 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000469}
470
471TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
472 EXPECT_EQ("#line 42 \"test\"\n",
473 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000474 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000475 format("# \\\n define \\\n A \\\n B\n",
476 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000477}
478
479TEST_F(FormatTest, EndOfFileEndsPPDirective) {
480 EXPECT_EQ("#line 42 \"test\"",
481 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000482 EXPECT_EQ("#define A B",
483 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000484}
485
Manuel Klimek060143e2013-01-02 18:33:23 +0000486TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000487 // If the macro fits in one line, we still do not get the full
488 // line, as only the next line decides whether we need an escaped newline and
489 // thus use the last column.
490 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000491
Manuel Klimekd544c572013-01-07 09:24:17 +0000492 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
493 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000494 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000495
496 verifyFormat("#define A A\n#define A A");
497 verifyFormat("#define A(X) A\n#define A A");
498
499 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
500 verifyFormat("#define Something \\\n"
501 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000502}
503
504TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000505 EXPECT_EQ("// some comment\n"
506 "#include \"a.h\"\n"
507 "#define A(A,\\\n"
508 " B)\n"
509 "#include \"b.h\"\n"
510 "// some comment\n",
511 format(" // some comment\n"
512 " #include \"a.h\"\n"
513 "#define A(A,\\\n"
514 " B)\n"
515 " #include \"b.h\"\n"
516 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000517}
518
Manuel Klimekd4397b92013-01-04 23:34:14 +0000519TEST_F(FormatTest, LayoutSingleHash) {
520 EXPECT_EQ("#\na;", format("#\na;"));
521}
522
523TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
524 EXPECT_EQ("#define A \\\n"
525 " c; \\\n"
526 " e;\n"
527 "f;", format("#define A c; e;\n"
528 "f;", getLLVMStyleWithColumns(14)));
529}
530
531TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000532 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000533}
534
535TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000536 EXPECT_EQ("# define A\\\n b;",
537 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000538}
539
540TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000541 EXPECT_EQ("int x,\n"
542 "#define A\n"
543 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000544}
545
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000546TEST_F(FormatTest, HashInMacroDefinition) {
547 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
548 verifyFormat("#define A \\\n"
549 " { \\\n"
550 " f(#c);\\\n"
551 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000552
553 verifyFormat("#define A(X) \\\n"
554 " void function##X()", getLLVMStyleWithColumns(22));
555
556 verifyFormat("#define A(a, b, c) \\\n"
557 " void a##b##c()", getLLVMStyleWithColumns(22));
558
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000559 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000560}
561
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000562TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
563 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
564}
565
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000566TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000567 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000568}
569
Manuel Klimeka5342db2013-01-06 20:07:31 +0000570TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
571 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
572 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
573 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
574 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
575}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000576
Manuel Klimek95419382013-01-07 07:56:50 +0000577TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000578 EXPECT_EQ(
579 "#define A \\\n int i; \\\n int j;",
580 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000581}
582
Manuel Klimekd544c572013-01-07 09:24:17 +0000583TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
584 verifyFormat("#define A \\\n"
585 " int v( \\\n"
586 " a); \\\n"
587 " int i;", getLLVMStyleWithColumns(11));
588}
589
Manuel Klimeka080a182013-01-02 16:30:12 +0000590TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000591 EXPECT_EQ(
592 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
593 " \\\n"
594 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
595 "\n"
596 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
597 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
598 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
599 "\\\n"
600 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
601 " \n"
602 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
603 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000604}
605
Manuel Klimek526ed112013-01-09 15:25:02 +0000606TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
607 EXPECT_EQ("int\n"
608 "#define A\n"
609 " a;",
610 format("int\n#define A\na;"));
611 verifyFormat(
612 "functionCallTo(someOtherFunction(\n"
613 " withSomeParameters, whichInSequence,\n"
614 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000615 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000616 " withMoreParamters,\n"
617 " whichStronglyInfluenceTheLayout),\n"
618 " andMoreParameters),\n"
619 " trailing);", getLLVMStyleWithColumns(69));
620}
621
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000622TEST_F(FormatTest, LayoutBlockInsideParens) {
623 EXPECT_EQ("functionCall({\n"
624 " int i;\n"
625 "});", format(" functionCall ( {int i;} );"));
626}
627
628TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000629 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000630 "int i;", format(" SOME_MACRO {int i;} int i;"));
631}
632
633TEST_F(FormatTest, LayoutNestedBlocks) {
634 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
635 " struct s {\n"
636 " int i;\n"
637 " };\n"
638 " s kBitsToOs[] = { { 10 } };\n"
639 " for (int i = 0; i < 10; ++i)\n"
640 " return;\n"
641 "}");
642}
643
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000644TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
645 EXPECT_EQ("{}", format("{}"));
646}
647
Alexander Kornienko15757312012-12-06 18:03:27 +0000648//===----------------------------------------------------------------------===//
649// Line break tests.
650//===----------------------------------------------------------------------===//
651
652TEST_F(FormatTest, FormatsFunctionDefinition) {
653 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
654 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000655 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000656}
657
658TEST_F(FormatTest, FormatsAwesomeMethodCall) {
659 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000660 "SomeLongMethodName(SomeReallyLongMethod(\n"
661 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
662 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000663}
664
Daniel Jasper1321eb52012-12-18 21:05:13 +0000665TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000666 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000667 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
668 getLLVMStyleWithColumns(45));
669 verifyFormat("Constructor()\n"
670 " : Inttializer(FitsOnTheLine) {}",
671 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000672
673 verifyFormat(
674 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000675 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000676
677 verifyFormat(
678 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000679 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
680 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
681 verifyGoogleFormat(
682 "SomeClass::Constructor()\n"
683 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
684 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
685 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
686
687 verifyFormat(
688 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000689 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000690 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000691
692 verifyFormat("Constructor()\n"
693 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
694 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
695 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000696 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000697
698 // Here a line could be saved by splitting the second initializer onto two
699 // lines, but that is not desireable.
700 verifyFormat("Constructor()\n"
701 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
702 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000703 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000704
705 verifyGoogleFormat("MyClass::MyClass(int var)\n"
706 " : some_var_(var), // 4 space indent\n"
707 " some_other_var_(var + 1) { // lined up\n"
708 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000709
710 // This test takes VERY long when memoization is broken.
711 verifyGoogleFormat(
712 "Constructor()\n"
713 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
714 " a, a, a,\n"
715 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
716 " a, a, a,\n"
717 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
718 " a, a, a,\n"
719 " a, a, a, a, a, a, a, a, a, a, a)\n"
720 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
721 " a, a, a,\n"
722 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
723 " a, a, a,\n"
724 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
725 " a, a, a,\n"
726 " a, a, a, a, a, a, a, a, a, a, a) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000727}
728
Alexander Kornienko15757312012-12-06 18:03:27 +0000729TEST_F(FormatTest, BreaksAsHighAsPossible) {
730 verifyFormat(
731 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
732 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
733 " f();");
734}
735
Daniel Jasperbac016b2012-12-03 18:12:45 +0000736TEST_F(FormatTest, BreaksDesireably) {
737 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
738 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000739 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000740
741 verifyFormat(
742 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000744
745 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
746 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
747 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000748
749 verifyFormat(
750 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
752 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000754
755 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
756 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
757
Daniel Jasper723f0302013-01-02 14:40:02 +0000758 verifyFormat(
759 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
761
Daniel Jasper33182dd2012-12-05 14:57:28 +0000762 // This test case breaks on an incorrect memoization, i.e. an optimization not
763 // taking into account the StopAt value.
764 verifyFormat(
765 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000766 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
767 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
768 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000769
Daniel Jaspercd162382013-01-07 13:26:07 +0000770 verifyFormat("{\n {\n {\n"
771 " Annotation.SpaceRequiredBefore =\n"
772 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
773 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
774 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000775}
776
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000777TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
778 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
779 " GUARDED_BY(aaaaaaaaaaaaa);");
780}
781
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000782TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
783 verifyFormat(
784 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000785 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000786 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000787 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000788 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000789 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000790 verifyFormat(
791 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000792 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000793}
794
Daniel Jasper9cda8002013-01-07 13:08:40 +0000795TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
796 verifyFormat(
797 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
798 " SI->getAlignment(),\n"
799 " SI->getPointerAddressSpaceee());\n");
800 verifyFormat(
801 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
802 " Line.Tokens.front().Tok.getLocation(),\n"
803 " Line.Tokens.back().Tok.getLocation());");
804}
805
Daniel Jaspercf225b62012-12-24 13:43:52 +0000806TEST_F(FormatTest, AlignsAfterAssignments) {
807 verifyFormat(
808 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000809 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000810 verifyFormat(
811 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000812 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000813 verifyFormat(
814 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000815 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000816 verifyFormat(
817 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000818 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000819 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000820 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
821 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
822 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000823}
824
825TEST_F(FormatTest, AlignsAfterReturn) {
826 verifyFormat(
827 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
828 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
829 verifyFormat(
830 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
831 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
832}
833
Daniel Jasper9c837d02013-01-09 07:06:56 +0000834TEST_F(FormatTest, BreaksConditionalExpressions) {
835 verifyFormat(
836 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
837 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
839 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
840 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
841}
842
Nico Weber7d37b8b2013-01-12 01:28:06 +0000843TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
844 verifyFormat("arr[foo ? bar : baz];");
845 verifyFormat("f()[foo ? bar : baz];");
846 verifyFormat("(a + b)[foo ? bar : baz];");
847 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
848}
849
Daniel Jasperbac016b2012-12-03 18:12:45 +0000850TEST_F(FormatTest, AlignsStringLiterals) {
851 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
852 " \"short literal\");");
853 verifyFormat(
854 "looooooooooooooooooooooooongFunction(\n"
855 " \"short literal\"\n"
856 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
857}
858
Alexander Kornienko15757312012-12-06 18:03:27 +0000859TEST_F(FormatTest, AlignsPipes) {
860 verifyFormat(
861 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
862 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
863 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
864 verifyFormat(
865 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
866 " << aaaaaaaaaaaaaaaaaaaa;");
867 verifyFormat(
868 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
869 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
870 verifyFormat(
871 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
872 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
873 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
874 verifyFormat(
875 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
876 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
877 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
878}
879
Daniel Jasperbac016b2012-12-03 18:12:45 +0000880TEST_F(FormatTest, UnderstandsEquals) {
881 verifyFormat(
882 "aaaaaaaaaaaaaaaaa =\n"
883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
884 verifyFormat(
885 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000886 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000887 verifyFormat(
888 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000889 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000890 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000892
Daniel Jasper9cda8002013-01-07 13:08:40 +0000893 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000894 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000895 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000896 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000897}
898
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000899TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000900 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
901 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000902
Daniel Jasper1321eb52012-12-18 21:05:13 +0000903 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
904 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000905
906 verifyFormat(
907 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
908 " Parameter2);");
909
910 verifyFormat(
911 "ShortObject->shortFunction(\n"
912 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
913 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
914
915 verifyFormat("loooooooooooooongFunction(\n"
916 " LoooooooooooooongObject->looooooooooooooooongFunction());");
917
918 verifyFormat(
919 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
920 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
921
Daniel Jasper46a46a22013-01-07 07:13:20 +0000922 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000923 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000924 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000925 verifyFormat(
926 "aaaaaaaaaaa->aaaaaaaaa(\n"
927 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
928 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000929}
930
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000931TEST_F(FormatTest, WrapsTemplateDeclarations) {
932 verifyFormat("template <typename T>\n"
933 "virtual void loooooooooooongFunction(int Param1, int Param2);");
934 verifyFormat(
935 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
936 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
937 verifyFormat(
938 "template <typename T>\n"
939 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
940 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000941 verifyFormat(
942 "template <typename T>\n"
943 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
944 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000946 verifyFormat("template <typename T>\n"
947 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
948 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000949 verifyFormat(
950 "template <typename T1, typename T2 = char, typename T3 = char,\n"
951 " typename T4 = char>\n"
952 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000953}
954
Daniel Jasperbac016b2012-12-03 18:12:45 +0000955TEST_F(FormatTest, UnderstandsTemplateParameters) {
956 verifyFormat("A<int> a;");
957 verifyFormat("A<A<A<int> > > a;");
958 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
959 verifyFormat("bool x = a < 1 || 2 > a;");
960 verifyFormat("bool x = 5 < f<int>();");
961 verifyFormat("bool x = f<int>() > 5;");
962 verifyFormat("bool x = 5 < a<int>::x;");
963 verifyFormat("bool x = a < 4 ? a > 2 : false;");
964 verifyFormat("bool x = f() ? a < 2 : a > 2;");
965
966 verifyGoogleFormat("A<A<int>> a;");
967 verifyGoogleFormat("A<A<A<int>>> a;");
968 verifyGoogleFormat("A<A<A<A<int>>>> a;");
969
970 verifyFormat("test >> a >> b;");
971 verifyFormat("test << a >> b;");
972
973 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000974 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000975}
976
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000977TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000978 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000979 verifyFormat("f(-1, -2, -3);");
980 verifyFormat("a[-1] = 5;");
981 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000982 verifyFormat("if (i == -1) {}");
983 verifyFormat("if (i != -1) {}");
984 verifyFormat("if (i > -1) {}");
985 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +0000986 verifyFormat("++(a->f());");
987 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +0000988 verifyFormat("(a->f())++;");
989 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000990 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000991
992 verifyFormat("a-- > b;");
993 verifyFormat("b ? -a : c;");
994 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000995 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000996 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000997 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +0000998
999 verifyFormat("return -1;");
1000 verifyFormat("switch (a) {\n"
1001 "case -1:\n"
1002 " break;\n"
1003 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001004
1005 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1006 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001007}
1008
1009TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001010 verifyFormat("bool operator<();");
1011 verifyFormat("bool operator>();");
1012 verifyFormat("bool operator=();");
1013 verifyFormat("bool operator==();");
1014 verifyFormat("bool operator!=();");
1015 verifyFormat("int operator+();");
1016 verifyFormat("int operator++();");
1017 verifyFormat("bool operator();");
1018 verifyFormat("bool operator()();");
1019 verifyFormat("bool operator[]();");
1020 verifyFormat("operator bool();");
1021 verifyFormat("operator SomeType<int>();");
1022 verifyFormat("void *operator new(std::size_t size);");
1023 verifyFormat("void *operator new[](std::size_t size);");
1024 verifyFormat("void operator delete(void *ptr);");
1025 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001026}
1027
Daniel Jasper088dab52013-01-11 16:09:04 +00001028TEST_F(FormatTest, UnderstandsNewAndDelete) {
1029 verifyFormat("A *a = new A;");
1030 verifyFormat("A *a = new (placement) A;");
1031 verifyFormat("delete a;");
1032 verifyFormat("delete (A *)a;");
1033}
1034
Daniel Jasper5d334402013-01-02 08:57:10 +00001035TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001036 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001037 verifyFormat("f(a, *a);");
1038 verifyFormat("f(*a);");
1039 verifyFormat("int a = b * 10;");
1040 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001041 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001042 verifyFormat("int a += b * c;");
1043 verifyFormat("int a -= b * c;");
1044 verifyFormat("int a *= b * c;");
1045 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001046 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001047 verifyFormat("int a = *b * c;");
1048 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001049 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001050 verifyFormat("return 10 * b;");
1051 verifyFormat("return *b * *c;");
1052 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001053 verifyFormat("f(b ? *c : *d);");
1054 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001055 verifyFormat("*b = a;");
1056 verifyFormat("a * ~b;");
1057 verifyFormat("a * !b;");
1058 verifyFormat("a * +b;");
1059 verifyFormat("a * -b;");
1060 verifyFormat("a * ++b;");
1061 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001062 verifyFormat("a[4] * b;");
1063 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001064 verifyFormat("a * [self dostuff];");
1065 verifyFormat("a * (a + b);");
1066 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001067 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001068
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001069 verifyFormat("InvalidRegions[*R] = 0;");
1070
Daniel Jasper8b39c662012-12-10 18:59:13 +00001071 verifyFormat("A<int *> a;");
1072 verifyFormat("A<int **> a;");
1073 verifyFormat("A<int *, int *> a;");
1074 verifyFormat("A<int **, int **> a;");
1075
Daniel Jasper2db356d2013-01-08 20:03:18 +00001076 verifyFormat(
1077 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1079
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001080 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001081 verifyGoogleFormat("A<int*> a;");
1082 verifyGoogleFormat("A<int**> a;");
1083 verifyGoogleFormat("A<int*, int*> a;");
1084 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001085 verifyGoogleFormat("f(b ? *c : *d);");
1086 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001087
1088 verifyFormat("a = *(x + y);");
1089 verifyFormat("a = &(x + y);");
1090 verifyFormat("*(x + y).call();");
1091 verifyFormat("&(x + y)->call();");
1092 verifyFormat("&(*I).first");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001093}
1094
Daniel Jasper4981bd02013-01-13 08:01:36 +00001095TEST_F(FormatTest, FormatsCasts) {
1096 verifyFormat("Type *A = static_cast<Type *>(P);");
1097 verifyFormat("Type *A = (Type *)P;");
1098 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1099 verifyFormat("int a = (int)(2.0f);");
1100
1101 // FIXME: These also need to be identified.
1102 verifyFormat("int a = (int) 2.0f;");
1103 verifyFormat("int a = (int) * b;");
1104
1105 // These are not casts.
1106 verifyFormat("void f(int *) {}");
1107 verifyFormat("void f(int *);");
1108 verifyFormat("void f(int *) = 0;");
1109 verifyFormat("void f(SmallVector<int>) {}");
1110 verifyFormat("void f(SmallVector<int>);");
1111 verifyFormat("void f(SmallVector<int>) = 0;");
1112}
1113
Daniel Jasper46ef8522013-01-10 13:08:12 +00001114TEST_F(FormatTest, FormatsFunctionTypes) {
1115 // FIXME: Determine the cases that need a space after the return type and fix.
1116 verifyFormat("A<bool()> a;");
1117 verifyFormat("A<SomeType()> a;");
1118 verifyFormat("A<void(*)(int, std::string)> a;");
1119
1120 verifyFormat("int(*func)(void *);");
1121}
1122
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001123TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001124 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001125 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001126 verifyFormat(
1127 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1128 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001129 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001130}
1131
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001132TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1133 verifyFormat("(a)->b();");
1134 verifyFormat("--a;");
1135}
1136
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001137TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001138 verifyFormat("#include <string>\n"
1139 "#include <a/b/c.h>\n"
1140 "#include \"a/b/string\"\n"
1141 "#include \"string.h\"\n"
1142 "#include \"string.h\"\n"
1143 "#include <a-a>");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001144
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001145 verifyFormat("#import <string>");
1146 verifyFormat("#import <a/b/c.h>");
1147 verifyFormat("#import \"a/b/string\"");
1148 verifyFormat("#import \"string.h\"");
1149 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001150}
1151
Alexander Kornienko15757312012-12-06 18:03:27 +00001152//===----------------------------------------------------------------------===//
1153// Error recovery tests.
1154//===----------------------------------------------------------------------===//
1155
Daniel Jasper700e7102013-01-10 09:26:47 +00001156TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1157 verifyFormat("void f() { return } 42");
1158}
1159
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001160TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1161 verifyFormat("int aaaaaaaa =\n"
1162 " // Overly long comment\n"
1163 " b;", getLLVMStyleWithColumns(20));
1164 verifyFormat("function(\n"
1165 " ShortArgument,\n"
1166 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1167}
1168
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001169TEST_F(FormatTest, IncorrectAccessSpecifier) {
1170 verifyFormat("public:");
1171 verifyFormat("class A {\n"
1172 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001173 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001174 "};");
1175 verifyFormat("public\n"
1176 "int qwerty;");
1177 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001178 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001179 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001180 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001181 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001182 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001183}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001184
Alexander Kornienko393b0082012-12-04 15:40:36 +00001185TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1186 verifyFormat("{");
1187}
1188
1189TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001190 verifyFormat("do {}");
1191 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001192 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001193 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001194 "wheeee(fun);");
1195 verifyFormat("do {\n"
1196 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001197 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001198}
1199
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001200TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001201 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001202 verifyFormat("switch {\n foo;\n foo();\n}");
1203 verifyFormat("for {\n foo;\n foo();\n}");
1204 verifyFormat("while {\n foo;\n foo();\n}");
1205 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001206}
1207
Daniel Jasper1f42f112013-01-04 18:52:56 +00001208TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1209 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001210 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001211}
1212
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001213TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001214 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1215 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1216 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1217 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001218
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001219 EXPECT_EQ("{\n"
1220 " {\n"
1221 " breakme(\n"
1222 " qwe);\n"
1223 "}\n", format("{\n"
1224 " {\n"
1225 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001226 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001227}
1228
Manuel Klimek2851c162013-01-10 14:36:46 +00001229TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1230 verifyFormat(
1231 "int x = {\n"
1232 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001233 " b(alongervariable)\n"
1234 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001235}
1236
1237TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1238 verifyFormat(
1239 "Aaa({\n"
1240 " int i;\n"
1241 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1242 " ccccccccccccccccc));");
1243}
1244
Manuel Klimek517e8942013-01-11 17:54:10 +00001245TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1246 verifyFormat("void f() { return 42; }");
1247 verifyFormat("void f() {\n"
1248 " // Comment\n"
1249 "}");
1250 verifyFormat("{\n"
1251 "#error {\n"
1252 " int a;\n"
1253 "}");
1254 verifyFormat("{\n"
1255 " int a;\n"
1256 "#error {\n"
1257 "}");
1258}
1259
Manuel Klimek606e07e2013-01-11 18:13:04 +00001260TEST_F(FormatTest, BracedInitListWithElaboratedTypeSpecifier) {
1261 verifyFormat("struct foo a = { bar };\nint n;");
1262}
1263
Manuel Klimek517e8942013-01-11 17:54:10 +00001264// FIXME: This breaks the order of the unwrapped lines:
1265// TEST_F(FormatTest, OrderUnwrappedLines) {
1266// verifyFormat("{\n"
1267// " bool a; //\n"
1268// "#error {\n"
1269// " int a;\n"
1270// "}");
1271// }
1272
Nico Webercf4a79c2013-01-08 17:56:31 +00001273//===----------------------------------------------------------------------===//
1274// Objective-C tests.
1275//===----------------------------------------------------------------------===//
1276
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001277TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1278 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1279 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1280 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001281 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001282 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1283 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1284 format("-(NSInteger)Method3:(id)anObject;"));
1285 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1286 format("-(NSInteger)Method4:(id)anObject;"));
1287 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1288 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1289 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1290 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001291 EXPECT_EQ(
1292 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1293 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001294
1295 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001296 EXPECT_EQ(
1297 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1298 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1299 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1300 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1301 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1302 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1303 format(
1304 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1305 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1306 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1307 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1308 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1309 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001310
1311 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001312 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001313 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1314 // protocol lists (but not for template classes):
1315 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001316
1317 verifyFormat("- (int(*)())foo:(int(*)())f;");
1318 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1319
1320 // If there's no return type (very rare in practice!), LLVM and Google style
1321 // agree.
1322 verifyFormat("- foo:(int)f;");
1323 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001324}
1325
Daniel Jasper886568d2013-01-09 08:36:49 +00001326TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001327 verifyFormat("int (^Block)(int, int);");
1328 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001329}
1330
Nico Weber27d13672013-01-09 20:25:35 +00001331TEST_F(FormatTest, FormatObjCInterface) {
1332 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001333 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001334 "@public\n"
1335 " int field1;\n"
1336 "@protected\n"
1337 " int field2;\n"
1338 "@private\n"
1339 " int field3;\n"
1340 "@package\n"
1341 " int field4;\n"
1342 "}\n"
1343 "+ (id)init;\n"
1344 "@end");
1345
Nico Weber27d13672013-01-09 20:25:35 +00001346 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1347 " @public\n"
1348 " int field1;\n"
1349 " @protected\n"
1350 " int field2;\n"
1351 " @private\n"
1352 " int field3;\n"
1353 " @package\n"
1354 " int field4;\n"
1355 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001356 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001357 "@end");
1358
1359 verifyFormat("@interface Foo\n"
1360 "+ (id)init;\n"
1361 "// Look, a comment!\n"
1362 "- (int)answerWith:(int)i;\n"
1363 "@end");
1364
1365 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001366 "@end\n"
1367 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001368 "@end");
1369
1370 verifyFormat("@interface Foo : Bar\n"
1371 "+ (id)init;\n"
1372 "@end");
1373
Nico Weber5f500df2013-01-10 20:12:55 +00001374 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001375 "+ (id)init;\n"
1376 "@end");
1377
Nico Weber5f500df2013-01-10 20:12:55 +00001378 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001379 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001380 "@end");
1381
Nico Webered91bba2013-01-10 19:19:14 +00001382 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001383 "+ (id)init;\n"
1384 "@end");
1385
Nico Webered91bba2013-01-10 19:19:14 +00001386 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001387 "+ (id)init;\n"
1388 "@end");
1389
Nico Weber5f500df2013-01-10 20:12:55 +00001390 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001391 "+ (id)init;\n"
1392 "@end");
1393
Nico Weber5f500df2013-01-10 20:12:55 +00001394 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001395 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001396 "@end");
1397
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001398 verifyFormat("@interface Foo {\n"
1399 " int _i;\n"
1400 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001401 "+ (id)init;\n"
1402 "@end");
1403
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001404 verifyFormat("@interface Foo : Bar {\n"
1405 " int _i;\n"
1406 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001407 "+ (id)init;\n"
1408 "@end");
1409
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001410 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1411 " int _i;\n"
1412 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001413 "+ (id)init;\n"
1414 "@end");
1415
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001416 verifyFormat("@interface Foo (HackStuff) {\n"
1417 " int _i;\n"
1418 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001419 "+ (id)init;\n"
1420 "@end");
1421
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001422 verifyFormat("@interface Foo () {\n"
1423 " int _i;\n"
1424 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001425 "+ (id)init;\n"
1426 "@end");
1427
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001428 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1429 " int _i;\n"
1430 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001431 "+ (id)init;\n"
1432 "@end");
1433}
1434
Nico Weber50767d82013-01-09 23:25:37 +00001435TEST_F(FormatTest, FormatObjCImplementation) {
1436 verifyFormat("@implementation Foo : NSObject {\n"
1437 "@public\n"
1438 " int field1;\n"
1439 "@protected\n"
1440 " int field2;\n"
1441 "@private\n"
1442 " int field3;\n"
1443 "@package\n"
1444 " int field4;\n"
1445 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001446 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001447 "@end");
1448
1449 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1450 " @public\n"
1451 " int field1;\n"
1452 " @protected\n"
1453 " int field2;\n"
1454 " @private\n"
1455 " int field3;\n"
1456 " @package\n"
1457 " int field4;\n"
1458 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001459 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001460 "@end");
1461
1462 verifyFormat("@implementation Foo\n"
1463 "+ (id)init {\n"
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001464 " if (true) return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001465 "}\n"
1466 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001467 "- (int)answerWith:(int)i {\n"
1468 " return i;\n"
1469 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001470 "+ (int)answerWith:(int)i {\n"
1471 " return i;\n"
1472 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001473 "@end");
1474
1475 verifyFormat("@implementation Foo\n"
1476 "@end\n"
1477 "@implementation Bar\n"
1478 "@end");
1479
1480 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001481 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001482 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001483 "@end");
1484
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001485 verifyFormat("@implementation Foo {\n"
1486 " int _i;\n"
1487 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001488 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001489 "@end");
1490
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001491 verifyFormat("@implementation Foo : Bar {\n"
1492 " int _i;\n"
1493 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001494 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001495 "@end");
1496
Nico Webered91bba2013-01-10 19:19:14 +00001497 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001498 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001499 "@end");
1500}
1501
Nico Weber1abe6ea2013-01-09 21:15:03 +00001502TEST_F(FormatTest, FormatObjCProtocol) {
1503 verifyFormat("@protocol Foo\n"
1504 "@property(weak) id delegate;\n"
1505 "- (NSUInteger)numberOfThings;\n"
1506 "@end");
1507
Nico Weber5f500df2013-01-10 20:12:55 +00001508 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001509 "- (NSUInteger)numberOfThings;\n"
1510 "@end");
1511
Nico Weber5f500df2013-01-10 20:12:55 +00001512 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001513 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001514 "@end");
1515
Nico Weber1abe6ea2013-01-09 21:15:03 +00001516 verifyFormat("@protocol Foo;\n"
1517 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001518
1519 verifyFormat("@protocol Foo\n"
1520 "@end\n"
1521 "@protocol Bar\n"
1522 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001523
1524 verifyFormat("@protocol myProtocol\n"
1525 "- (void)mandatoryWithInt:(int)i;\n"
1526 "@optional\n"
1527 "- (void)optional;\n"
1528 "@required\n"
1529 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001530 "@optional\n"
1531 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001532 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001533}
1534
Nico Weberbcfdd262013-01-12 06:18:40 +00001535TEST_F(FormatTest, FormatObjCMethodExpr) {
1536 verifyFormat("[foo bar:baz];");
1537 verifyFormat("return [foo bar:baz];");
1538 verifyFormat("f([foo bar:baz]);");
1539 verifyFormat("f(2, [foo bar:baz]);");
1540 verifyFormat("f(2, a ? b : c);");
1541 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1542
1543 verifyFormat("[foo bar:baz], [foo bar:baz];");
1544 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1545 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1546 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1547 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1548 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1549 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1550 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1551 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1552 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1553 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1554 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1555 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1556 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1557 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1558 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1559 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1560 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1561 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1562 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1563 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1564 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1565 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1566 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1567 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1568 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1569 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1570 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1571 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1572 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1573 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1574 // Whew!
1575
1576 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1577 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1578 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1579 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1580 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001581 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001582 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001583
Nico Weberbcfdd262013-01-12 06:18:40 +00001584 verifyFormat("arr[[self indexForFoo:a]];");
1585 verifyFormat("throw [self errorFor:a];");
1586 verifyFormat("@throw [self errorFor:a];");
1587
Nico Webere8ccc812013-01-12 22:48:47 +00001588 // This tests that the formatter doesn't break after "backing" but before ":",
1589 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001590 verifyFormat(
1591 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001592 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1593 " backing:NSBackingStoreBuffered defer:YES]))");
1594
Nico Webere8ccc812013-01-12 22:48:47 +00001595 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1596 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001597
1598}
1599
Nico Weber581f5572013-01-07 15:56:25 +00001600TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001601 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001602 verifyFormat("@catch");
1603 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001604 verifyFormat("@compatibility_alias");
1605 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001606 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001607 verifyFormat("@encode");
1608 verifyFormat("@end");
1609 verifyFormat("@finally");
1610 verifyFormat("@implementation");
1611 verifyFormat("@import");
1612 verifyFormat("@interface");
1613 verifyFormat("@optional");
1614 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001615 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001616 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001617 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001618 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001619 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001620 verifyFormat("@required");
1621 verifyFormat("@selector");
1622 verifyFormat("@synchronized");
1623 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001624 verifyFormat("@throw");
1625 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001626
Nico Webercb4d6902013-01-08 19:40:21 +00001627 verifyFormat("@\"String\"");
1628 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001629 verifyFormat("@+4.8");
1630 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001631 verifyFormat("@1LL");
1632 verifyFormat("@.5");
1633 verifyFormat("@'c'");
1634 verifyFormat("@true");
1635 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001636 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001637 verifyFormat("@[");
1638 verifyFormat("@{");
1639
Nico Weber581f5572013-01-07 15:56:25 +00001640 EXPECT_EQ("@interface", format("@ interface"));
1641
1642 // The precise formatting of this doesn't matter, nobody writes code like
1643 // this.
1644 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001645}
1646
Nico Weberc31689a2013-01-08 19:15:23 +00001647TEST_F(FormatTest, ObjCSnippets) {
1648 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001649 verifyFormat("@autoreleasepool {\n"
1650 " foo();\n"
1651 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001652 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001653 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001654 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001655 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001656 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001657 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001658 verifyFormat("@synchronized(self) {\n"
1659 " f();\n"
1660 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001661
Nico Weber70848232013-01-10 21:30:42 +00001662 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1663 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1664
Nico Webercf4a79c2013-01-08 17:56:31 +00001665 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001666 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1667 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001668}
1669
Daniel Jaspercd162382013-01-07 13:26:07 +00001670} // end namespace tooling
1671} // end namespace clang