blob: c088089211cec222dc34acfa74fc08546b0ed533 [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));
Alexander Kornienko15757312012-12-06 18:03:27 +0000141}
142
143TEST_F(FormatTest, ParseIfElse) {
144 verifyFormat("if (true)\n"
145 " if (true)\n"
146 " if (true)\n"
147 " f();\n"
148 " else\n"
149 " g();\n"
150 " else\n"
151 " h();\n"
152 "else\n"
153 " i();");
154 verifyFormat("if (true)\n"
155 " if (true)\n"
156 " if (true) {\n"
Daniel Jasperfeb18f52013-01-14 14:14:23 +0000157 " if (true) f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000158 " } else {\n"
159 " g();\n"
160 " }\n"
161 " else\n"
162 " h();\n"
163 "else {\n"
164 " i();\n"
165 "}");
166}
167
168TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000169 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000170 verifyFormat("if (a)\n"
171 " f();\n"
172 "else if (b)\n"
173 " g();\n"
174 "else\n"
175 " h();");
176}
177
Daniel Jasperbac016b2012-12-03 18:12:45 +0000178TEST_F(FormatTest, FormatsForLoop) {
179 verifyFormat(
180 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000181 " ++VeryVeryLongLoopVariable)\n"
182 " ;");
183 verifyFormat("for (;;)\n"
184 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000185 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000186 verifyFormat("for (;;) {\n"
187 " f();\n"
188 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000189
190 verifyFormat(
191 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
192 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000193 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000194
195 verifyFormat(
196 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000197 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000198}
199
200TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000201 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000202 verifyFormat("while (true)\n"
203 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000204 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000205 verifyFormat("while () {\n"
206 " f();\n"
207 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000208}
209
Alexander Kornienko15757312012-12-06 18:03:27 +0000210TEST_F(FormatTest, FormatsDoWhile) {
211 verifyFormat("do {\n"
212 " do_something();\n"
213 "} while (something());");
214 verifyFormat("do\n"
215 " do_something();\n"
216 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217}
218
Alexander Kornienko15757312012-12-06 18:03:27 +0000219TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000220 verifyFormat("switch (x) {\n"
221 "case 1:\n"
222 " f();\n"
223 " break;\n"
224 "case kFoo:\n"
225 "case ns::kBar:\n"
226 "case kBaz:\n"
227 " break;\n"
228 "default:\n"
229 " g();\n"
230 " break;\n"
231 "}");
232 verifyFormat("switch (x) {\n"
233 "case 1: {\n"
234 " f();\n"
235 " break;\n"
236 "}\n"
237 "}");
238 verifyFormat("switch (test)\n"
239 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000240 verifyGoogleFormat("switch (x) {\n"
241 " case 1:\n"
242 " f();\n"
243 " break;\n"
244 " case kFoo:\n"
245 " case ns::kBar:\n"
246 " case kBaz:\n"
247 " break;\n"
248 " default:\n"
249 " g();\n"
250 " break;\n"
251 "}");
252 verifyGoogleFormat("switch (x) {\n"
253 " case 1: {\n"
254 " f();\n"
255 " break;\n"
256 " }\n"
257 "}");
258 verifyGoogleFormat("switch (test)\n"
259 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000260}
261
Alexander Kornienko15757312012-12-06 18:03:27 +0000262TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000263 verifyFormat("void f() {\n"
264 " some_code();\n"
265 "test_label:\n"
266 " some_other_code();\n"
267 " {\n"
268 " some_more_code();\n"
269 " another_label:\n"
270 " some_more_code();\n"
271 " }\n"
272 "}");
273 verifyFormat("some_code();\n"
274 "test_label:\n"
275 "some_other_code();");
276}
277
Alexander Kornienko15757312012-12-06 18:03:27 +0000278//===----------------------------------------------------------------------===//
279// Tests for comments.
280//===----------------------------------------------------------------------===//
281
282TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000283 verifyFormat("// line 1\n"
284 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000285 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000286
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000287 verifyFormat("void f() {\n"
288 " // Doesn't do anything\n"
289 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000290 verifyFormat("void f(int i, // some comment (probably for i)\n"
291 " int j, // some comment (probably for j)\n"
292 " int k); // some comment (probably for k)");
293 verifyFormat("void f(int i,\n"
294 " // some comment (probably for j)\n"
295 " int j,\n"
296 " // some comment (probably for k)\n"
297 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000298
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000299 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000300 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000301
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000302 verifyFormat("enum E {\n"
303 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000304 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000305 " VAL_B\n"
306 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000307
308 verifyFormat(
309 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000310 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000311 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
312 " // Comment inside a statement.\n"
313 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000314
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000315 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000316 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000317
318 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000319}
320
321TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000322 verifyFormat("f(/*test=*/ true);");
323}
324
Alexander Kornienko15757312012-12-06 18:03:27 +0000325//===----------------------------------------------------------------------===//
326// Tests for classes, namespaces, etc.
327//===----------------------------------------------------------------------===//
328
329TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000330 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000331}
332
333TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
334 verifyFormat("class A {\n"
335 "public:\n"
336 "protected:\n"
337 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000338 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000339 "};");
340 verifyGoogleFormat("class A {\n"
341 " public:\n"
342 " protected:\n"
343 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000344 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000345 "};");
346}
347
348TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000349 verifyFormat("class A : public B {};");
350 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000351}
352
Manuel Klimekde768542013-01-07 18:10:23 +0000353TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000354 verifyFormat("class A {} a, b;");
355 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000356 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000357}
358
Alexander Kornienko15757312012-12-06 18:03:27 +0000359TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000360 verifyFormat("enum {\n"
361 " Zero,\n"
362 " One = 1,\n"
363 " Two = One + 1,\n"
364 " Three = (One + Two),\n"
365 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
366 " Five = (One, Two, Three, Four, 5)\n"
367 "};");
368 verifyFormat("enum Enum {\n"
369 "};");
370 verifyFormat("enum {\n"
371 "};");
372}
373
Nico Weberefaddc02013-01-14 05:49:49 +0000374TEST_F(FormatTest, FormatsBitfields) {
375 verifyFormat("struct Bitfields {\n"
376 " unsigned sClass : 8;\n"
377 " unsigned ValueKind : 2;\n"
378 "};");
379}
380
Alexander Kornienko15757312012-12-06 18:03:27 +0000381TEST_F(FormatTest, FormatsNamespaces) {
382 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000383 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000384 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000385 "}");
386 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000387 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000388 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000389 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000390 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000391 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000392 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000393 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000394 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000395 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000396 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000397}
398
Nico Webera9ccdd12013-01-07 16:36:17 +0000399TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000400 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
401 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000402 verifyFormat("try {\n"
403 " throw a * b;\n"
404 "}\n"
405 "catch (int a) {\n"
406 " // Do nothing.\n"
407 "}\n"
408 "catch (...) {\n"
409 " exit(42);\n"
410 "}");
411
412 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000413 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000414 "catch (...) {\n"
415 " return 5;\n"
416 "}");
417 verifyFormat("class A {\n"
418 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000419 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000420 " catch (...) {\n"
421 " throw;\n"
422 " }\n"
423 "};\n");
424}
425
426TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000427 verifyFormat("@try {\n"
428 " f();\n"
429 "}\n"
430 "@catch (NSException e) {\n"
431 " @throw;\n"
432 "}\n"
433 "@finally {\n"
434 " exit(42);\n"
435 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000436}
437
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000438TEST_F(FormatTest, StaticInitializers) {
439 verifyFormat("static SomeClass SC = { 1, 'a' };");
440
441 // FIXME: Format like enums if the static initializer does not fit on a line.
442 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000443 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000444 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
445 "};");
446
447 verifyFormat(
448 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
449 " looooooooooooooooooooooooooooooooooongname,\n"
450 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000451}
452
Manuel Klimeka080a182013-01-02 16:30:12 +0000453TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
454 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
455 " \\\n"
456 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
457}
458
Daniel Jasper71607512013-01-07 10:48:50 +0000459TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000460 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
461 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000462}
463
Manuel Klimeka080a182013-01-02 16:30:12 +0000464TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
465 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000466 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000467}
468
469TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
470 EXPECT_EQ("#line 42 \"test\"\n",
471 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000472 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000473 format("# \\\n define \\\n A \\\n B\n",
474 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000475}
476
477TEST_F(FormatTest, EndOfFileEndsPPDirective) {
478 EXPECT_EQ("#line 42 \"test\"",
479 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000480 EXPECT_EQ("#define A B",
481 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000482}
483
Manuel Klimek060143e2013-01-02 18:33:23 +0000484TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000485 // If the macro fits in one line, we still do not get the full
486 // line, as only the next line decides whether we need an escaped newline and
487 // thus use the last column.
488 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000489
Manuel Klimekd544c572013-01-07 09:24:17 +0000490 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
491 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000492 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000493
494 verifyFormat("#define A A\n#define A A");
495 verifyFormat("#define A(X) A\n#define A A");
496
497 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
498 verifyFormat("#define Something \\\n"
499 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000500}
501
502TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000503 EXPECT_EQ("// some comment\n"
504 "#include \"a.h\"\n"
505 "#define A(A,\\\n"
506 " B)\n"
507 "#include \"b.h\"\n"
508 "// some comment\n",
509 format(" // some comment\n"
510 " #include \"a.h\"\n"
511 "#define A(A,\\\n"
512 " B)\n"
513 " #include \"b.h\"\n"
514 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000515}
516
Manuel Klimekd4397b92013-01-04 23:34:14 +0000517TEST_F(FormatTest, LayoutSingleHash) {
518 EXPECT_EQ("#\na;", format("#\na;"));
519}
520
521TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
522 EXPECT_EQ("#define A \\\n"
523 " c; \\\n"
524 " e;\n"
525 "f;", format("#define A c; e;\n"
526 "f;", getLLVMStyleWithColumns(14)));
527}
528
529TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000530 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000531}
532
533TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000534 EXPECT_EQ("# define A\\\n b;",
535 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000536}
537
538TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000539 EXPECT_EQ("int x,\n"
540 "#define A\n"
541 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000542}
543
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000544TEST_F(FormatTest, HashInMacroDefinition) {
545 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
546 verifyFormat("#define A \\\n"
547 " { \\\n"
548 " f(#c);\\\n"
549 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000550
551 verifyFormat("#define A(X) \\\n"
552 " void function##X()", getLLVMStyleWithColumns(22));
553
554 verifyFormat("#define A(a, b, c) \\\n"
555 " void a##b##c()", getLLVMStyleWithColumns(22));
556
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000557 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000558}
559
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000560TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
561 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
562}
563
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000564TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000565 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000566}
567
Manuel Klimeka5342db2013-01-06 20:07:31 +0000568TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
569 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
570 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
571 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
572 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
573}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000574
Manuel Klimek95419382013-01-07 07:56:50 +0000575TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000576 EXPECT_EQ(
577 "#define A \\\n int i; \\\n int j;",
578 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000579}
580
Manuel Klimekd544c572013-01-07 09:24:17 +0000581TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
582 verifyFormat("#define A \\\n"
583 " int v( \\\n"
584 " a); \\\n"
585 " int i;", getLLVMStyleWithColumns(11));
586}
587
Manuel Klimeka080a182013-01-02 16:30:12 +0000588TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000589 EXPECT_EQ(
590 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
591 " \\\n"
592 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
593 "\n"
594 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
595 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
596 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
597 "\\\n"
598 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
599 " \n"
600 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
601 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000602}
603
Manuel Klimek526ed112013-01-09 15:25:02 +0000604TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
605 EXPECT_EQ("int\n"
606 "#define A\n"
607 " a;",
608 format("int\n#define A\na;"));
609 verifyFormat(
610 "functionCallTo(someOtherFunction(\n"
611 " withSomeParameters, whichInSequence,\n"
612 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000613 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000614 " withMoreParamters,\n"
615 " whichStronglyInfluenceTheLayout),\n"
616 " andMoreParameters),\n"
617 " trailing);", getLLVMStyleWithColumns(69));
618}
619
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000620TEST_F(FormatTest, LayoutBlockInsideParens) {
621 EXPECT_EQ("functionCall({\n"
622 " int i;\n"
623 "});", format(" functionCall ( {int i;} );"));
624}
625
626TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000627 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000628 "int i;", format(" SOME_MACRO {int i;} int i;"));
629}
630
631TEST_F(FormatTest, LayoutNestedBlocks) {
632 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
633 " struct s {\n"
634 " int i;\n"
635 " };\n"
636 " s kBitsToOs[] = { { 10 } };\n"
637 " for (int i = 0; i < 10; ++i)\n"
638 " return;\n"
639 "}");
640}
641
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000642TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
643 EXPECT_EQ("{}", format("{}"));
644}
645
Alexander Kornienko15757312012-12-06 18:03:27 +0000646//===----------------------------------------------------------------------===//
647// Line break tests.
648//===----------------------------------------------------------------------===//
649
650TEST_F(FormatTest, FormatsFunctionDefinition) {
651 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
652 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000653 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000654}
655
656TEST_F(FormatTest, FormatsAwesomeMethodCall) {
657 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000658 "SomeLongMethodName(SomeReallyLongMethod(\n"
659 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
660 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000661}
662
Daniel Jasper1321eb52012-12-18 21:05:13 +0000663TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000664 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000665 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
666 getLLVMStyleWithColumns(45));
667 verifyFormat("Constructor()\n"
668 " : Inttializer(FitsOnTheLine) {}",
669 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000670
671 verifyFormat(
672 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000673 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000674
675 verifyFormat(
676 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000677 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
678 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
679 verifyGoogleFormat(
680 "SomeClass::Constructor()\n"
681 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
682 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
683 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
684
685 verifyFormat(
686 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000687 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000688 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000689
690 verifyFormat("Constructor()\n"
691 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
692 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
693 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000694 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000695
696 // Here a line could be saved by splitting the second initializer onto two
697 // lines, but that is not desireable.
698 verifyFormat("Constructor()\n"
699 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
700 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000701 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000702
703 verifyGoogleFormat("MyClass::MyClass(int var)\n"
704 " : some_var_(var), // 4 space indent\n"
705 " some_other_var_(var + 1) { // lined up\n"
706 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000707
708 // This test takes VERY long when memoization is broken.
709 verifyGoogleFormat(
710 "Constructor()\n"
711 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
712 " a, a, a,\n"
713 " 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)\n"
718 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
719 " a, a, a,\n"
720 " 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) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000725}
726
Alexander Kornienko15757312012-12-06 18:03:27 +0000727TEST_F(FormatTest, BreaksAsHighAsPossible) {
728 verifyFormat(
729 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
730 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
731 " f();");
732}
733
Daniel Jasperbac016b2012-12-03 18:12:45 +0000734TEST_F(FormatTest, BreaksDesireably) {
735 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
736 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000737 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000738
739 verifyFormat(
740 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000742
743 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000746
747 verifyFormat(
748 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
750 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000752
753 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
754 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
755
Daniel Jasper723f0302013-01-02 14:40:02 +0000756 verifyFormat(
757 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
759
Daniel Jasper33182dd2012-12-05 14:57:28 +0000760 // This test case breaks on an incorrect memoization, i.e. an optimization not
761 // taking into account the StopAt value.
762 verifyFormat(
763 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000764 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
765 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
766 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000767
Daniel Jaspercd162382013-01-07 13:26:07 +0000768 verifyFormat("{\n {\n {\n"
769 " Annotation.SpaceRequiredBefore =\n"
770 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
771 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
772 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000773}
774
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000775TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
776 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
777 " GUARDED_BY(aaaaaaaaaaaaa);");
778}
779
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000780TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
781 verifyFormat(
782 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000783 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000784 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000785 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000786 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000787 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000788 verifyFormat(
789 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000790 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000791}
792
Daniel Jasper9cda8002013-01-07 13:08:40 +0000793TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
794 verifyFormat(
795 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
796 " SI->getAlignment(),\n"
797 " SI->getPointerAddressSpaceee());\n");
798 verifyFormat(
799 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
800 " Line.Tokens.front().Tok.getLocation(),\n"
801 " Line.Tokens.back().Tok.getLocation());");
802}
803
Daniel Jaspercf225b62012-12-24 13:43:52 +0000804TEST_F(FormatTest, AlignsAfterAssignments) {
805 verifyFormat(
806 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000807 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000808 verifyFormat(
809 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000810 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000811 verifyFormat(
812 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000813 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000814 verifyFormat(
815 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000816 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000817 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000818 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
819 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
820 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000821}
822
823TEST_F(FormatTest, AlignsAfterReturn) {
824 verifyFormat(
825 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
826 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
827 verifyFormat(
828 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
829 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
830}
831
Daniel Jasper9c837d02013-01-09 07:06:56 +0000832TEST_F(FormatTest, BreaksConditionalExpressions) {
833 verifyFormat(
834 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
835 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
837 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
838 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
839}
840
Nico Weber7d37b8b2013-01-12 01:28:06 +0000841TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
842 verifyFormat("arr[foo ? bar : baz];");
843 verifyFormat("f()[foo ? bar : baz];");
844 verifyFormat("(a + b)[foo ? bar : baz];");
845 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
846}
847
Daniel Jasperbac016b2012-12-03 18:12:45 +0000848TEST_F(FormatTest, AlignsStringLiterals) {
849 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
850 " \"short literal\");");
851 verifyFormat(
852 "looooooooooooooooooooooooongFunction(\n"
853 " \"short literal\"\n"
854 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
855}
856
Alexander Kornienko15757312012-12-06 18:03:27 +0000857TEST_F(FormatTest, AlignsPipes) {
858 verifyFormat(
859 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
860 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
861 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
862 verifyFormat(
863 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
864 " << aaaaaaaaaaaaaaaaaaaa;");
865 verifyFormat(
866 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
867 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
868 verifyFormat(
869 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
870 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
871 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
872 verifyFormat(
873 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
874 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
875 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
876}
877
Daniel Jasperbac016b2012-12-03 18:12:45 +0000878TEST_F(FormatTest, UnderstandsEquals) {
879 verifyFormat(
880 "aaaaaaaaaaaaaaaaa =\n"
881 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
882 verifyFormat(
883 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000884 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000885 verifyFormat(
886 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000887 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000888 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000890
Daniel Jasper9cda8002013-01-07 13:08:40 +0000891 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000892 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000893 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000894 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000895}
896
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000897TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000898 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
899 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000900
Daniel Jasper1321eb52012-12-18 21:05:13 +0000901 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
902 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000903
904 verifyFormat(
905 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
906 " Parameter2);");
907
908 verifyFormat(
909 "ShortObject->shortFunction(\n"
910 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
911 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
912
913 verifyFormat("loooooooooooooongFunction(\n"
914 " LoooooooooooooongObject->looooooooooooooooongFunction());");
915
916 verifyFormat(
917 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
918 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
919
Daniel Jasper46a46a22013-01-07 07:13:20 +0000920 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000921 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000922 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000923 verifyFormat(
924 "aaaaaaaaaaa->aaaaaaaaa(\n"
925 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
926 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000927}
928
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000929TEST_F(FormatTest, WrapsTemplateDeclarations) {
930 verifyFormat("template <typename T>\n"
931 "virtual void loooooooooooongFunction(int Param1, int Param2);");
932 verifyFormat(
933 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
934 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
935 verifyFormat(
936 "template <typename T>\n"
937 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
938 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000939 verifyFormat(
940 "template <typename T>\n"
941 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
942 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
943 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000944 verifyFormat("template <typename T>\n"
945 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
946 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000947 verifyFormat(
948 "template <typename T1, typename T2 = char, typename T3 = char,\n"
949 " typename T4 = char>\n"
950 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000951}
952
Daniel Jasperbac016b2012-12-03 18:12:45 +0000953TEST_F(FormatTest, UnderstandsTemplateParameters) {
954 verifyFormat("A<int> a;");
955 verifyFormat("A<A<A<int> > > a;");
956 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
957 verifyFormat("bool x = a < 1 || 2 > a;");
958 verifyFormat("bool x = 5 < f<int>();");
959 verifyFormat("bool x = f<int>() > 5;");
960 verifyFormat("bool x = 5 < a<int>::x;");
961 verifyFormat("bool x = a < 4 ? a > 2 : false;");
962 verifyFormat("bool x = f() ? a < 2 : a > 2;");
963
964 verifyGoogleFormat("A<A<int>> a;");
965 verifyGoogleFormat("A<A<A<int>>> a;");
966 verifyGoogleFormat("A<A<A<A<int>>>> a;");
967
968 verifyFormat("test >> a >> b;");
969 verifyFormat("test << a >> b;");
970
971 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000972 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000973}
974
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000975TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000976 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000977 verifyFormat("f(-1, -2, -3);");
978 verifyFormat("a[-1] = 5;");
979 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000980 verifyFormat("if (i == -1) {}");
981 verifyFormat("if (i != -1) {}");
982 verifyFormat("if (i > -1) {}");
983 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +0000984 verifyFormat("++(a->f());");
985 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +0000986 verifyFormat("(a->f())++;");
987 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000988 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000989
990 verifyFormat("a-- > b;");
991 verifyFormat("b ? -a : c;");
992 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000993 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000994 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000995 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +0000996
997 verifyFormat("return -1;");
998 verifyFormat("switch (a) {\n"
999 "case -1:\n"
1000 " break;\n"
1001 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001002
1003 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1004 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001005}
1006
1007TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001008 verifyFormat("bool operator<();");
1009 verifyFormat("bool operator>();");
1010 verifyFormat("bool operator=();");
1011 verifyFormat("bool operator==();");
1012 verifyFormat("bool operator!=();");
1013 verifyFormat("int operator+();");
1014 verifyFormat("int operator++();");
1015 verifyFormat("bool operator();");
1016 verifyFormat("bool operator()();");
1017 verifyFormat("bool operator[]();");
1018 verifyFormat("operator bool();");
1019 verifyFormat("operator SomeType<int>();");
1020 verifyFormat("void *operator new(std::size_t size);");
1021 verifyFormat("void *operator new[](std::size_t size);");
1022 verifyFormat("void operator delete(void *ptr);");
1023 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001024}
1025
Daniel Jasper088dab52013-01-11 16:09:04 +00001026TEST_F(FormatTest, UnderstandsNewAndDelete) {
1027 verifyFormat("A *a = new A;");
1028 verifyFormat("A *a = new (placement) A;");
1029 verifyFormat("delete a;");
1030 verifyFormat("delete (A *)a;");
1031}
1032
Daniel Jasper5d334402013-01-02 08:57:10 +00001033TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001034 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001035 verifyFormat("f(a, *a);");
1036 verifyFormat("f(*a);");
1037 verifyFormat("int a = b * 10;");
1038 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001039 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001040 verifyFormat("int a += b * c;");
1041 verifyFormat("int a -= b * c;");
1042 verifyFormat("int a *= b * c;");
1043 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001044 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001045 verifyFormat("int a = *b * c;");
1046 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001047 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001048 verifyFormat("return 10 * b;");
1049 verifyFormat("return *b * *c;");
1050 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001051 verifyFormat("f(b ? *c : *d);");
1052 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001053 verifyFormat("*b = a;");
1054 verifyFormat("a * ~b;");
1055 verifyFormat("a * !b;");
1056 verifyFormat("a * +b;");
1057 verifyFormat("a * -b;");
1058 verifyFormat("a * ++b;");
1059 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001060 verifyFormat("a[4] * b;");
1061 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001062 verifyFormat("a * [self dostuff];");
1063 verifyFormat("a * (a + b);");
1064 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001065 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001066
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001067 verifyFormat("InvalidRegions[*R] = 0;");
1068
Daniel Jasper8b39c662012-12-10 18:59:13 +00001069 verifyFormat("A<int *> a;");
1070 verifyFormat("A<int **> a;");
1071 verifyFormat("A<int *, int *> a;");
1072 verifyFormat("A<int **, int **> a;");
1073
Daniel Jasper2db356d2013-01-08 20:03:18 +00001074 verifyFormat(
1075 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1077
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001078 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001079 verifyGoogleFormat("A<int*> a;");
1080 verifyGoogleFormat("A<int**> a;");
1081 verifyGoogleFormat("A<int*, int*> a;");
1082 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001083 verifyGoogleFormat("f(b ? *c : *d);");
1084 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001085
1086 verifyFormat("a = *(x + y);");
1087 verifyFormat("a = &(x + y);");
1088 verifyFormat("*(x + y).call();");
1089 verifyFormat("&(x + y)->call();");
1090 verifyFormat("&(*I).first");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001091}
1092
Daniel Jasper4981bd02013-01-13 08:01:36 +00001093TEST_F(FormatTest, FormatsCasts) {
1094 verifyFormat("Type *A = static_cast<Type *>(P);");
1095 verifyFormat("Type *A = (Type *)P;");
1096 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1097 verifyFormat("int a = (int)(2.0f);");
1098
1099 // FIXME: These also need to be identified.
1100 verifyFormat("int a = (int) 2.0f;");
1101 verifyFormat("int a = (int) * b;");
1102
1103 // These are not casts.
1104 verifyFormat("void f(int *) {}");
1105 verifyFormat("void f(int *);");
1106 verifyFormat("void f(int *) = 0;");
1107 verifyFormat("void f(SmallVector<int>) {}");
1108 verifyFormat("void f(SmallVector<int>);");
1109 verifyFormat("void f(SmallVector<int>) = 0;");
1110}
1111
Daniel Jasper46ef8522013-01-10 13:08:12 +00001112TEST_F(FormatTest, FormatsFunctionTypes) {
1113 // FIXME: Determine the cases that need a space after the return type and fix.
1114 verifyFormat("A<bool()> a;");
1115 verifyFormat("A<SomeType()> a;");
1116 verifyFormat("A<void(*)(int, std::string)> a;");
1117
1118 verifyFormat("int(*func)(void *);");
1119}
1120
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001121TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001122 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001123 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001124 verifyFormat(
1125 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1126 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001127 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001128}
1129
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001130TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1131 verifyFormat("(a)->b();");
1132 verifyFormat("--a;");
1133}
1134
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001135TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001136 verifyFormat("#include <string>");
1137 verifyFormat("#include <a/b/c.h>");
1138 verifyFormat("#include \"a/b/string\"");
1139 verifyFormat("#include \"string.h\"");
1140 verifyFormat("#include \"string.h\"");
1141 verifyFormat("#include <a-a>");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001142
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001143 verifyFormat("#import <string>");
1144 verifyFormat("#import <a/b/c.h>");
1145 verifyFormat("#import \"a/b/string\"");
1146 verifyFormat("#import \"string.h\"");
1147 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001148}
1149
Alexander Kornienko15757312012-12-06 18:03:27 +00001150//===----------------------------------------------------------------------===//
1151// Error recovery tests.
1152//===----------------------------------------------------------------------===//
1153
Daniel Jasper700e7102013-01-10 09:26:47 +00001154TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1155 verifyFormat("void f() { return } 42");
1156}
1157
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001158TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1159 verifyFormat("int aaaaaaaa =\n"
1160 " // Overly long comment\n"
1161 " b;", getLLVMStyleWithColumns(20));
1162 verifyFormat("function(\n"
1163 " ShortArgument,\n"
1164 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1165}
1166
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001167TEST_F(FormatTest, IncorrectAccessSpecifier) {
1168 verifyFormat("public:");
1169 verifyFormat("class A {\n"
1170 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001171 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001172 "};");
1173 verifyFormat("public\n"
1174 "int qwerty;");
1175 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001176 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001177 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001178 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001179 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001180 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001181}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001182
Alexander Kornienko393b0082012-12-04 15:40:36 +00001183TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1184 verifyFormat("{");
1185}
1186
1187TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001188 verifyFormat("do {}");
1189 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001190 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001191 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001192 "wheeee(fun);");
1193 verifyFormat("do {\n"
1194 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001195 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001196}
1197
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001198TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001199 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001200 verifyFormat("switch {\n foo;\n foo();\n}");
1201 verifyFormat("for {\n foo;\n foo();\n}");
1202 verifyFormat("while {\n foo;\n foo();\n}");
1203 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001204}
1205
Daniel Jasper1f42f112013-01-04 18:52:56 +00001206TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1207 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001208 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001209}
1210
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001211TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001212 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1213 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1214 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1215 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001216
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001217 EXPECT_EQ("{\n"
1218 " {\n"
1219 " breakme(\n"
1220 " qwe);\n"
1221 "}\n", format("{\n"
1222 " {\n"
1223 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001224 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001225}
1226
Manuel Klimek2851c162013-01-10 14:36:46 +00001227TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1228 verifyFormat(
1229 "int x = {\n"
1230 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001231 " b(alongervariable)\n"
1232 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001233}
1234
1235TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1236 verifyFormat(
1237 "Aaa({\n"
1238 " int i;\n"
1239 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1240 " ccccccccccccccccc));");
1241}
1242
Manuel Klimek517e8942013-01-11 17:54:10 +00001243TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1244 verifyFormat("void f() { return 42; }");
1245 verifyFormat("void f() {\n"
1246 " // Comment\n"
1247 "}");
1248 verifyFormat("{\n"
1249 "#error {\n"
1250 " int a;\n"
1251 "}");
1252 verifyFormat("{\n"
1253 " int a;\n"
1254 "#error {\n"
1255 "}");
1256}
1257
Manuel Klimek606e07e2013-01-11 18:13:04 +00001258TEST_F(FormatTest, BracedInitListWithElaboratedTypeSpecifier) {
1259 verifyFormat("struct foo a = { bar };\nint n;");
1260}
1261
Manuel Klimek517e8942013-01-11 17:54:10 +00001262// FIXME: This breaks the order of the unwrapped lines:
1263// TEST_F(FormatTest, OrderUnwrappedLines) {
1264// verifyFormat("{\n"
1265// " bool a; //\n"
1266// "#error {\n"
1267// " int a;\n"
1268// "}");
1269// }
1270
Nico Webercf4a79c2013-01-08 17:56:31 +00001271//===----------------------------------------------------------------------===//
1272// Objective-C tests.
1273//===----------------------------------------------------------------------===//
1274
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001275TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1276 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1277 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1278 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001279 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001280 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1281 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1282 format("-(NSInteger)Method3:(id)anObject;"));
1283 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1284 format("-(NSInteger)Method4:(id)anObject;"));
1285 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1286 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1287 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1288 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001289 EXPECT_EQ(
1290 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1291 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001292
1293 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001294 EXPECT_EQ(
1295 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1296 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1297 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1298 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1299 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1300 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1301 format(
1302 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1303 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1304 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1305 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1306 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1307 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001308
1309 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001310 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001311 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1312 // protocol lists (but not for template classes):
1313 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001314
1315 verifyFormat("- (int(*)())foo:(int(*)())f;");
1316 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1317
1318 // If there's no return type (very rare in practice!), LLVM and Google style
1319 // agree.
1320 verifyFormat("- foo:(int)f;");
1321 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001322}
1323
Daniel Jasper886568d2013-01-09 08:36:49 +00001324TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001325 verifyFormat("int (^Block)(int, int);");
1326 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001327}
1328
Nico Weber27d13672013-01-09 20:25:35 +00001329TEST_F(FormatTest, FormatObjCInterface) {
1330 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001331 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001332 "@public\n"
1333 " int field1;\n"
1334 "@protected\n"
1335 " int field2;\n"
1336 "@private\n"
1337 " int field3;\n"
1338 "@package\n"
1339 " int field4;\n"
1340 "}\n"
1341 "+ (id)init;\n"
1342 "@end");
1343
Nico Weber27d13672013-01-09 20:25:35 +00001344 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1345 " @public\n"
1346 " int field1;\n"
1347 " @protected\n"
1348 " int field2;\n"
1349 " @private\n"
1350 " int field3;\n"
1351 " @package\n"
1352 " int field4;\n"
1353 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001354 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001355 "@end");
1356
1357 verifyFormat("@interface Foo\n"
1358 "+ (id)init;\n"
1359 "// Look, a comment!\n"
1360 "- (int)answerWith:(int)i;\n"
1361 "@end");
1362
1363 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001364 "@end\n"
1365 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001366 "@end");
1367
1368 verifyFormat("@interface Foo : Bar\n"
1369 "+ (id)init;\n"
1370 "@end");
1371
Nico Weber5f500df2013-01-10 20:12:55 +00001372 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001373 "+ (id)init;\n"
1374 "@end");
1375
Nico Weber5f500df2013-01-10 20:12:55 +00001376 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001377 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001378 "@end");
1379
Nico Webered91bba2013-01-10 19:19:14 +00001380 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001381 "+ (id)init;\n"
1382 "@end");
1383
Nico Webered91bba2013-01-10 19:19:14 +00001384 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001385 "+ (id)init;\n"
1386 "@end");
1387
Nico Weber5f500df2013-01-10 20:12:55 +00001388 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001389 "+ (id)init;\n"
1390 "@end");
1391
Nico Weber5f500df2013-01-10 20:12:55 +00001392 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001393 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001394 "@end");
1395
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001396 verifyFormat("@interface Foo {\n"
1397 " int _i;\n"
1398 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001399 "+ (id)init;\n"
1400 "@end");
1401
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001402 verifyFormat("@interface Foo : Bar {\n"
1403 " int _i;\n"
1404 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001405 "+ (id)init;\n"
1406 "@end");
1407
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001408 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1409 " int _i;\n"
1410 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001411 "+ (id)init;\n"
1412 "@end");
1413
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001414 verifyFormat("@interface Foo (HackStuff) {\n"
1415 " int _i;\n"
1416 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001417 "+ (id)init;\n"
1418 "@end");
1419
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001420 verifyFormat("@interface Foo () {\n"
1421 " int _i;\n"
1422 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001423 "+ (id)init;\n"
1424 "@end");
1425
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001426 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1427 " int _i;\n"
1428 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001429 "+ (id)init;\n"
1430 "@end");
1431}
1432
Nico Weber50767d82013-01-09 23:25:37 +00001433TEST_F(FormatTest, FormatObjCImplementation) {
1434 verifyFormat("@implementation Foo : NSObject {\n"
1435 "@public\n"
1436 " int field1;\n"
1437 "@protected\n"
1438 " int field2;\n"
1439 "@private\n"
1440 " int field3;\n"
1441 "@package\n"
1442 " int field4;\n"
1443 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001444 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001445 "@end");
1446
1447 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1448 " @public\n"
1449 " int field1;\n"
1450 " @protected\n"
1451 " int field2;\n"
1452 " @private\n"
1453 " int field3;\n"
1454 " @package\n"
1455 " int field4;\n"
1456 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001457 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001458 "@end");
1459
1460 verifyFormat("@implementation Foo\n"
1461 "+ (id)init {\n"
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001462 " if (true) return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001463 "}\n"
1464 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001465 "- (int)answerWith:(int)i {\n"
1466 " return i;\n"
1467 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001468 "+ (int)answerWith:(int)i {\n"
1469 " return i;\n"
1470 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001471 "@end");
1472
1473 verifyFormat("@implementation Foo\n"
1474 "@end\n"
1475 "@implementation Bar\n"
1476 "@end");
1477
1478 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001479 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001480 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001481 "@end");
1482
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001483 verifyFormat("@implementation Foo {\n"
1484 " int _i;\n"
1485 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001486 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001487 "@end");
1488
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001489 verifyFormat("@implementation Foo : Bar {\n"
1490 " int _i;\n"
1491 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001492 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001493 "@end");
1494
Nico Webered91bba2013-01-10 19:19:14 +00001495 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001496 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001497 "@end");
1498}
1499
Nico Weber1abe6ea2013-01-09 21:15:03 +00001500TEST_F(FormatTest, FormatObjCProtocol) {
1501 verifyFormat("@protocol Foo\n"
1502 "@property(weak) id delegate;\n"
1503 "- (NSUInteger)numberOfThings;\n"
1504 "@end");
1505
Nico Weber5f500df2013-01-10 20:12:55 +00001506 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001507 "- (NSUInteger)numberOfThings;\n"
1508 "@end");
1509
Nico Weber5f500df2013-01-10 20:12:55 +00001510 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001511 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001512 "@end");
1513
Nico Weber1abe6ea2013-01-09 21:15:03 +00001514 verifyFormat("@protocol Foo;\n"
1515 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001516
1517 verifyFormat("@protocol Foo\n"
1518 "@end\n"
1519 "@protocol Bar\n"
1520 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001521
1522 verifyFormat("@protocol myProtocol\n"
1523 "- (void)mandatoryWithInt:(int)i;\n"
1524 "@optional\n"
1525 "- (void)optional;\n"
1526 "@required\n"
1527 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001528 "@optional\n"
1529 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001530 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001531}
1532
Nico Weberbcfdd262013-01-12 06:18:40 +00001533TEST_F(FormatTest, FormatObjCMethodExpr) {
1534 verifyFormat("[foo bar:baz];");
1535 verifyFormat("return [foo bar:baz];");
1536 verifyFormat("f([foo bar:baz]);");
1537 verifyFormat("f(2, [foo bar:baz]);");
1538 verifyFormat("f(2, a ? b : c);");
1539 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1540
1541 verifyFormat("[foo bar:baz], [foo bar:baz];");
1542 verifyFormat("[foo bar:baz] = [foo bar:baz];");
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] : [foo bar:baz];");
1554 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1555 verifyFormat("[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 // Whew!
1573
1574 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1575 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1576 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1577 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1578 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001579 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001580 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001581
Nico Weberbcfdd262013-01-12 06:18:40 +00001582 verifyFormat("arr[[self indexForFoo:a]];");
1583 verifyFormat("throw [self errorFor:a];");
1584 verifyFormat("@throw [self errorFor:a];");
1585
Nico Webere8ccc812013-01-12 22:48:47 +00001586 // This tests that the formatter doesn't break after "backing" but before ":",
1587 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001588 verifyFormat(
1589 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001590 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1591 " backing:NSBackingStoreBuffered defer:YES]))");
1592
Nico Webere8ccc812013-01-12 22:48:47 +00001593 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1594 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001595
1596}
1597
Nico Weber581f5572013-01-07 15:56:25 +00001598TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001599 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001600 verifyFormat("@catch");
1601 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001602 verifyFormat("@compatibility_alias");
1603 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001604 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001605 verifyFormat("@encode");
1606 verifyFormat("@end");
1607 verifyFormat("@finally");
1608 verifyFormat("@implementation");
1609 verifyFormat("@import");
1610 verifyFormat("@interface");
1611 verifyFormat("@optional");
1612 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001613 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001614 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001615 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001616 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001617 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001618 verifyFormat("@required");
1619 verifyFormat("@selector");
1620 verifyFormat("@synchronized");
1621 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001622 verifyFormat("@throw");
1623 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001624
Nico Webercb4d6902013-01-08 19:40:21 +00001625 verifyFormat("@\"String\"");
1626 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001627 verifyFormat("@+4.8");
1628 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001629 verifyFormat("@1LL");
1630 verifyFormat("@.5");
1631 verifyFormat("@'c'");
1632 verifyFormat("@true");
1633 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001634 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001635 verifyFormat("@[");
1636 verifyFormat("@{");
1637
Nico Weber581f5572013-01-07 15:56:25 +00001638 EXPECT_EQ("@interface", format("@ interface"));
1639
1640 // The precise formatting of this doesn't matter, nobody writes code like
1641 // this.
1642 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001643}
1644
Nico Weberc31689a2013-01-08 19:15:23 +00001645TEST_F(FormatTest, ObjCSnippets) {
1646 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001647 verifyFormat("@autoreleasepool {\n"
1648 " foo();\n"
1649 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001650 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001651 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001652 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001653 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001654 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001655 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001656 verifyFormat("@synchronized(self) {\n"
1657 " f();\n"
1658 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001659
Nico Weber70848232013-01-10 21:30:42 +00001660 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1661 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1662
Nico Webercf4a79c2013-01-08 17:56:31 +00001663 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001664 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1665 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001666}
1667
Daniel Jaspercd162382013-01-07 13:26:07 +00001668} // end namespace tooling
1669} // end namespace clang