blob: e5279c442ff7f435a142a0fafc0fcff124efa933 [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;
Manuel Klimek526ed112013-01-09 15:25:02 +000053 } else if (MessedUp[i] == '#' && JustReplacedNewline) {
54 MessedUp[i - 1] = '\n';
55 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"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000472 EXPECT_EQ("#define A \\\n B\n",
473 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\""));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000480 EXPECT_EQ("#define A \\\n B",
481 format("# \\\n define \\\n A \\\n B",
482 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000483}
484
Manuel Klimek060143e2013-01-02 18:33:23 +0000485TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000486 // If the macro fits in one line, we still do not get the full
487 // line, as only the next line decides whether we need an escaped newline and
488 // thus use the last column.
489 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000490
Manuel Klimekd544c572013-01-07 09:24:17 +0000491 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
492 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000493 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
494}
495
496TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000497 EXPECT_EQ("// some comment\n"
498 "#include \"a.h\"\n"
499 "#define A(A,\\\n"
500 " B)\n"
501 "#include \"b.h\"\n"
502 "// some comment\n",
503 format(" // 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", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000509}
510
Manuel Klimekd4397b92013-01-04 23:34:14 +0000511TEST_F(FormatTest, LayoutSingleHash) {
512 EXPECT_EQ("#\na;", format("#\na;"));
513}
514
515TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
516 EXPECT_EQ("#define A \\\n"
517 " c; \\\n"
518 " e;\n"
519 "f;", format("#define A c; e;\n"
520 "f;", getLLVMStyleWithColumns(14)));
521}
522
523TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000524 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000525}
526
527TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000528 EXPECT_EQ("# define A\\\n b;",
529 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000530}
531
532TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000533 EXPECT_EQ("int x,\n"
534 "#define A\n"
535 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000536}
537
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000538TEST_F(FormatTest, HashInMacroDefinition) {
539 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
540 verifyFormat("#define A \\\n"
541 " { \\\n"
542 " f(#c);\\\n"
543 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000544
545 verifyFormat("#define A(X) \\\n"
546 " void function##X()", getLLVMStyleWithColumns(22));
547
548 verifyFormat("#define A(a, b, c) \\\n"
549 " void a##b##c()", getLLVMStyleWithColumns(22));
550
551 verifyFormat("#define A \\\n"
552 " void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000553}
554
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000555TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
556 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
557}
558
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000559TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000560 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000561}
562
Manuel Klimeka5342db2013-01-06 20:07:31 +0000563TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
564 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
565 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
566 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
567 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
568}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000569
Manuel Klimek95419382013-01-07 07:56:50 +0000570TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000571 EXPECT_EQ(
572 "#define A \\\n int i; \\\n int j;",
573 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000574}
575
Manuel Klimekd544c572013-01-07 09:24:17 +0000576TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
577 verifyFormat("#define A \\\n"
578 " int v( \\\n"
579 " a); \\\n"
580 " int i;", getLLVMStyleWithColumns(11));
581}
582
Manuel Klimeka080a182013-01-02 16:30:12 +0000583TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000584 EXPECT_EQ(
585 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
586 " \\\n"
587 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
588 "\n"
589 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
590 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
591 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
592 "\\\n"
593 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
594 " \n"
595 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
596 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000597}
598
Manuel Klimek526ed112013-01-09 15:25:02 +0000599TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
600 EXPECT_EQ("int\n"
601 "#define A\n"
602 " a;",
603 format("int\n#define A\na;"));
604 verifyFormat(
605 "functionCallTo(someOtherFunction(\n"
606 " withSomeParameters, whichInSequence,\n"
607 " areLongerThanALine(andAnotherCall,\n"
608 "#define A \\\n"
609 " B\n"
610 " withMoreParamters,\n"
611 " whichStronglyInfluenceTheLayout),\n"
612 " andMoreParameters),\n"
613 " trailing);", getLLVMStyleWithColumns(69));
614}
615
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000616TEST_F(FormatTest, LayoutBlockInsideParens) {
617 EXPECT_EQ("functionCall({\n"
618 " int i;\n"
619 "});", format(" functionCall ( {int i;} );"));
620}
621
622TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000623 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000624 "int i;", format(" SOME_MACRO {int i;} int i;"));
625}
626
627TEST_F(FormatTest, LayoutNestedBlocks) {
628 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
629 " struct s {\n"
630 " int i;\n"
631 " };\n"
632 " s kBitsToOs[] = { { 10 } };\n"
633 " for (int i = 0; i < 10; ++i)\n"
634 " return;\n"
635 "}");
636}
637
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000638TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
639 EXPECT_EQ("{}", format("{}"));
640}
641
Alexander Kornienko15757312012-12-06 18:03:27 +0000642//===----------------------------------------------------------------------===//
643// Line break tests.
644//===----------------------------------------------------------------------===//
645
646TEST_F(FormatTest, FormatsFunctionDefinition) {
647 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
648 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000649 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000650}
651
652TEST_F(FormatTest, FormatsAwesomeMethodCall) {
653 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000654 "SomeLongMethodName(SomeReallyLongMethod(\n"
655 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
656 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000657}
658
Daniel Jasper1321eb52012-12-18 21:05:13 +0000659TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000660 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000661 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
662 getLLVMStyleWithColumns(45));
663 verifyFormat("Constructor()\n"
664 " : Inttializer(FitsOnTheLine) {}",
665 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000666
667 verifyFormat(
668 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000669 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000670
671 verifyFormat(
672 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000673 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
674 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
675 verifyGoogleFormat(
676 "SomeClass::Constructor()\n"
677 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
678 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
679 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
680
681 verifyFormat(
682 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000683 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000684 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000685
686 verifyFormat("Constructor()\n"
687 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
688 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
689 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000690 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000691
692 // Here a line could be saved by splitting the second initializer onto two
693 // lines, but that is not desireable.
694 verifyFormat("Constructor()\n"
695 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
696 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000697 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000698
699 verifyGoogleFormat("MyClass::MyClass(int var)\n"
700 " : some_var_(var), // 4 space indent\n"
701 " some_other_var_(var + 1) { // lined up\n"
702 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000703
704 // This test takes VERY long when memoization is broken.
705 verifyGoogleFormat(
706 "Constructor()\n"
707 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
708 " a, a, a,\n"
709 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
710 " a, a, a,\n"
711 " 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)\n"
714 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
715 " a, a, a,\n"
716 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
717 " a, a, a,\n"
718 " 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) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000721}
722
Alexander Kornienko15757312012-12-06 18:03:27 +0000723TEST_F(FormatTest, BreaksAsHighAsPossible) {
724 verifyFormat(
725 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
726 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
727 " f();");
728}
729
Daniel Jasperbac016b2012-12-03 18:12:45 +0000730TEST_F(FormatTest, BreaksDesireably) {
731 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
732 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000733 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000734
735 verifyFormat(
736 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000738
739 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000742
743 verifyFormat(
744 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
746 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
747 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000748
749 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
750 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
751
Daniel Jasper723f0302013-01-02 14:40:02 +0000752 verifyFormat(
753 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
755
Daniel Jasper33182dd2012-12-05 14:57:28 +0000756 // This test case breaks on an incorrect memoization, i.e. an optimization not
757 // taking into account the StopAt value.
758 verifyFormat(
759 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000760 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
761 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
762 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000763
Daniel Jaspercd162382013-01-07 13:26:07 +0000764 verifyFormat("{\n {\n {\n"
765 " Annotation.SpaceRequiredBefore =\n"
766 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
767 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
768 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000769}
770
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000771TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
772 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
773 " GUARDED_BY(aaaaaaaaaaaaa);");
774}
775
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000776TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
777 verifyFormat(
778 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000779 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000780 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000781 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000782 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000783 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000784 verifyFormat(
785 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000786 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000787}
788
Daniel Jasper9cda8002013-01-07 13:08:40 +0000789TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
790 verifyFormat(
791 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
792 " SI->getAlignment(),\n"
793 " SI->getPointerAddressSpaceee());\n");
794 verifyFormat(
795 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
796 " Line.Tokens.front().Tok.getLocation(),\n"
797 " Line.Tokens.back().Tok.getLocation());");
798}
799
Daniel Jaspercf225b62012-12-24 13:43:52 +0000800TEST_F(FormatTest, AlignsAfterAssignments) {
801 verifyFormat(
802 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000803 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000804 verifyFormat(
805 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000806 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000807 verifyFormat(
808 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000809 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000810 verifyFormat(
811 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000812 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000813 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000814 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
815 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
816 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000817}
818
819TEST_F(FormatTest, AlignsAfterReturn) {
820 verifyFormat(
821 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
822 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
823 verifyFormat(
824 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
825 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
826}
827
Daniel Jasper9c837d02013-01-09 07:06:56 +0000828TEST_F(FormatTest, BreaksConditionalExpressions) {
829 verifyFormat(
830 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
831 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
833 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
834 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
835}
836
Nico Weber7d37b8b2013-01-12 01:28:06 +0000837TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
838 verifyFormat("arr[foo ? bar : baz];");
839 verifyFormat("f()[foo ? bar : baz];");
840 verifyFormat("(a + b)[foo ? bar : baz];");
841 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
842}
843
Daniel Jasperbac016b2012-12-03 18:12:45 +0000844TEST_F(FormatTest, AlignsStringLiterals) {
845 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
846 " \"short literal\");");
847 verifyFormat(
848 "looooooooooooooooooooooooongFunction(\n"
849 " \"short literal\"\n"
850 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
851}
852
Alexander Kornienko15757312012-12-06 18:03:27 +0000853TEST_F(FormatTest, AlignsPipes) {
854 verifyFormat(
855 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
856 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
857 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
858 verifyFormat(
859 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
860 " << aaaaaaaaaaaaaaaaaaaa;");
861 verifyFormat(
862 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
863 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
864 verifyFormat(
865 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
866 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
867 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
868 verifyFormat(
869 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
870 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
871 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
872}
873
Daniel Jasperbac016b2012-12-03 18:12:45 +0000874TEST_F(FormatTest, UnderstandsEquals) {
875 verifyFormat(
876 "aaaaaaaaaaaaaaaaa =\n"
877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
878 verifyFormat(
879 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000881 verifyFormat(
882 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000883 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000884 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000886
Daniel Jasper9cda8002013-01-07 13:08:40 +0000887 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000888 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000889 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000890 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000891}
892
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000893TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000894 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
895 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000896
Daniel Jasper1321eb52012-12-18 21:05:13 +0000897 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
898 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000899
900 verifyFormat(
901 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
902 " Parameter2);");
903
904 verifyFormat(
905 "ShortObject->shortFunction(\n"
906 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
907 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
908
909 verifyFormat("loooooooooooooongFunction(\n"
910 " LoooooooooooooongObject->looooooooooooooooongFunction());");
911
912 verifyFormat(
913 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
914 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
915
Daniel Jasper46a46a22013-01-07 07:13:20 +0000916 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000917 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000918 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000919 verifyFormat(
920 "aaaaaaaaaaa->aaaaaaaaa(\n"
921 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
922 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000923}
924
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000925TEST_F(FormatTest, WrapsTemplateDeclarations) {
926 verifyFormat("template <typename T>\n"
927 "virtual void loooooooooooongFunction(int Param1, int Param2);");
928 verifyFormat(
929 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
930 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
931 verifyFormat(
932 "template <typename T>\n"
933 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
934 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000935 verifyFormat(
936 "template <typename T>\n"
937 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
938 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000940 verifyFormat("template <typename T>\n"
941 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
942 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000943 verifyFormat(
944 "template <typename T1, typename T2 = char, typename T3 = char,\n"
945 " typename T4 = char>\n"
946 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000947}
948
Daniel Jasperbac016b2012-12-03 18:12:45 +0000949TEST_F(FormatTest, UnderstandsTemplateParameters) {
950 verifyFormat("A<int> a;");
951 verifyFormat("A<A<A<int> > > a;");
952 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
953 verifyFormat("bool x = a < 1 || 2 > a;");
954 verifyFormat("bool x = 5 < f<int>();");
955 verifyFormat("bool x = f<int>() > 5;");
956 verifyFormat("bool x = 5 < a<int>::x;");
957 verifyFormat("bool x = a < 4 ? a > 2 : false;");
958 verifyFormat("bool x = f() ? a < 2 : a > 2;");
959
960 verifyGoogleFormat("A<A<int>> a;");
961 verifyGoogleFormat("A<A<A<int>>> a;");
962 verifyGoogleFormat("A<A<A<A<int>>>> a;");
963
964 verifyFormat("test >> a >> b;");
965 verifyFormat("test << a >> b;");
966
967 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000968 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000969}
970
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000971TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000972 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000973 verifyFormat("f(-1, -2, -3);");
974 verifyFormat("a[-1] = 5;");
975 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000976 verifyFormat("if (i == -1) {}");
977 verifyFormat("if (i != -1) {}");
978 verifyFormat("if (i > -1) {}");
979 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +0000980 verifyFormat("++(a->f());");
981 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +0000982 verifyFormat("(a->f())++;");
983 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000984 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000985
986 verifyFormat("a-- > b;");
987 verifyFormat("b ? -a : c;");
988 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000989 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000990 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000991 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +0000992
993 verifyFormat("return -1;");
994 verifyFormat("switch (a) {\n"
995 "case -1:\n"
996 " break;\n"
997 "}");
Nico Webercc191d12013-01-12 05:41:23 +0000998
999 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1000 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001001}
1002
1003TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001004 verifyFormat("bool operator<();");
1005 verifyFormat("bool operator>();");
1006 verifyFormat("bool operator=();");
1007 verifyFormat("bool operator==();");
1008 verifyFormat("bool operator!=();");
1009 verifyFormat("int operator+();");
1010 verifyFormat("int operator++();");
1011 verifyFormat("bool operator();");
1012 verifyFormat("bool operator()();");
1013 verifyFormat("bool operator[]();");
1014 verifyFormat("operator bool();");
1015 verifyFormat("operator SomeType<int>();");
1016 verifyFormat("void *operator new(std::size_t size);");
1017 verifyFormat("void *operator new[](std::size_t size);");
1018 verifyFormat("void operator delete(void *ptr);");
1019 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001020}
1021
Daniel Jasper088dab52013-01-11 16:09:04 +00001022TEST_F(FormatTest, UnderstandsNewAndDelete) {
1023 verifyFormat("A *a = new A;");
1024 verifyFormat("A *a = new (placement) A;");
1025 verifyFormat("delete a;");
1026 verifyFormat("delete (A *)a;");
1027}
1028
Daniel Jasper5d334402013-01-02 08:57:10 +00001029TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001030 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001031 verifyFormat("f(a, *a);");
1032 verifyFormat("f(*a);");
1033 verifyFormat("int a = b * 10;");
1034 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001035 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001036 verifyFormat("int a += b * c;");
1037 verifyFormat("int a -= b * c;");
1038 verifyFormat("int a *= b * c;");
1039 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001040 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001041 verifyFormat("int a = *b * c;");
1042 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001043 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001044 verifyFormat("return 10 * b;");
1045 verifyFormat("return *b * *c;");
1046 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001047 verifyFormat("f(b ? *c : *d);");
1048 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001049 verifyFormat("*b = a;");
1050 verifyFormat("a * ~b;");
1051 verifyFormat("a * !b;");
1052 verifyFormat("a * +b;");
1053 verifyFormat("a * -b;");
1054 verifyFormat("a * ++b;");
1055 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001056 verifyFormat("a[4] * b;");
1057 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001058 verifyFormat("a * [self dostuff];");
1059 verifyFormat("a * (a + b);");
1060 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001061 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001062
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001063 verifyFormat("InvalidRegions[*R] = 0;");
1064
Daniel Jasper8b39c662012-12-10 18:59:13 +00001065 verifyFormat("A<int *> a;");
1066 verifyFormat("A<int **> a;");
1067 verifyFormat("A<int *, int *> a;");
1068 verifyFormat("A<int **, int **> a;");
1069
Daniel Jasper2db356d2013-01-08 20:03:18 +00001070 verifyFormat(
1071 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1073
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001074 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001075 verifyGoogleFormat("A<int*> a;");
1076 verifyGoogleFormat("A<int**> a;");
1077 verifyGoogleFormat("A<int*, int*> a;");
1078 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001079 verifyGoogleFormat("f(b ? *c : *d);");
1080 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001081
1082 verifyFormat("a = *(x + y);");
1083 verifyFormat("a = &(x + y);");
1084 verifyFormat("*(x + y).call();");
1085 verifyFormat("&(x + y)->call();");
1086 verifyFormat("&(*I).first");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001087}
1088
Daniel Jasper4981bd02013-01-13 08:01:36 +00001089TEST_F(FormatTest, FormatsCasts) {
1090 verifyFormat("Type *A = static_cast<Type *>(P);");
1091 verifyFormat("Type *A = (Type *)P;");
1092 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1093 verifyFormat("int a = (int)(2.0f);");
1094
1095 // FIXME: These also need to be identified.
1096 verifyFormat("int a = (int) 2.0f;");
1097 verifyFormat("int a = (int) * b;");
1098
1099 // These are not casts.
1100 verifyFormat("void f(int *) {}");
1101 verifyFormat("void f(int *);");
1102 verifyFormat("void f(int *) = 0;");
1103 verifyFormat("void f(SmallVector<int>) {}");
1104 verifyFormat("void f(SmallVector<int>);");
1105 verifyFormat("void f(SmallVector<int>) = 0;");
1106}
1107
Daniel Jasper46ef8522013-01-10 13:08:12 +00001108TEST_F(FormatTest, FormatsFunctionTypes) {
1109 // FIXME: Determine the cases that need a space after the return type and fix.
1110 verifyFormat("A<bool()> a;");
1111 verifyFormat("A<SomeType()> a;");
1112 verifyFormat("A<void(*)(int, std::string)> a;");
1113
1114 verifyFormat("int(*func)(void *);");
1115}
1116
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001117TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001118 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001119 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001120 verifyFormat(
1121 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1122 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001123 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001124}
1125
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001126TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1127 verifyFormat("(a)->b();");
1128 verifyFormat("--a;");
1129}
1130
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001131TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001132 verifyFormat("#include <string>");
1133 verifyFormat("#include <a/b/c.h>");
1134 verifyFormat("#include \"a/b/string\"");
1135 verifyFormat("#include \"string.h\"");
1136 verifyFormat("#include \"string.h\"");
1137 verifyFormat("#include <a-a>");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001138
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001139 verifyFormat("#import <string>");
1140 verifyFormat("#import <a/b/c.h>");
1141 verifyFormat("#import \"a/b/string\"");
1142 verifyFormat("#import \"string.h\"");
1143 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001144}
1145
Alexander Kornienko15757312012-12-06 18:03:27 +00001146//===----------------------------------------------------------------------===//
1147// Error recovery tests.
1148//===----------------------------------------------------------------------===//
1149
Daniel Jasper700e7102013-01-10 09:26:47 +00001150TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1151 verifyFormat("void f() { return } 42");
1152}
1153
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001154TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1155 verifyFormat("int aaaaaaaa =\n"
1156 " // Overly long comment\n"
1157 " b;", getLLVMStyleWithColumns(20));
1158 verifyFormat("function(\n"
1159 " ShortArgument,\n"
1160 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1161}
1162
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001163TEST_F(FormatTest, IncorrectAccessSpecifier) {
1164 verifyFormat("public:");
1165 verifyFormat("class A {\n"
1166 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001167 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001168 "};");
1169 verifyFormat("public\n"
1170 "int qwerty;");
1171 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001172 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001173 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001174 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001175 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001176 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001177}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001178
Alexander Kornienko393b0082012-12-04 15:40:36 +00001179TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1180 verifyFormat("{");
1181}
1182
1183TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001184 verifyFormat("do {}");
1185 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001186 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001187 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001188 "wheeee(fun);");
1189 verifyFormat("do {\n"
1190 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001191 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001192}
1193
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001194TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001195 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001196 verifyFormat("switch {\n foo;\n foo();\n}");
1197 verifyFormat("for {\n foo;\n foo();\n}");
1198 verifyFormat("while {\n foo;\n foo();\n}");
1199 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001200}
1201
Daniel Jasper1f42f112013-01-04 18:52:56 +00001202TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1203 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001204 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001205}
1206
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001207TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001208 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1209 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1210 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1211 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001212
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001213 EXPECT_EQ("{\n"
1214 " {\n"
1215 " breakme(\n"
1216 " qwe);\n"
1217 "}\n", format("{\n"
1218 " {\n"
1219 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001220 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001221}
1222
Manuel Klimek2851c162013-01-10 14:36:46 +00001223TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1224 verifyFormat(
1225 "int x = {\n"
1226 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001227 " b(alongervariable)\n"
1228 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001229}
1230
1231TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1232 verifyFormat(
1233 "Aaa({\n"
1234 " int i;\n"
1235 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1236 " ccccccccccccccccc));");
1237}
1238
Manuel Klimek517e8942013-01-11 17:54:10 +00001239TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1240 verifyFormat("void f() { return 42; }");
1241 verifyFormat("void f() {\n"
1242 " // Comment\n"
1243 "}");
1244 verifyFormat("{\n"
1245 "#error {\n"
1246 " int a;\n"
1247 "}");
1248 verifyFormat("{\n"
1249 " int a;\n"
1250 "#error {\n"
1251 "}");
1252}
1253
Manuel Klimek606e07e2013-01-11 18:13:04 +00001254TEST_F(FormatTest, BracedInitListWithElaboratedTypeSpecifier) {
1255 verifyFormat("struct foo a = { bar };\nint n;");
1256}
1257
Manuel Klimek517e8942013-01-11 17:54:10 +00001258// FIXME: This breaks the order of the unwrapped lines:
1259// TEST_F(FormatTest, OrderUnwrappedLines) {
1260// verifyFormat("{\n"
1261// " bool a; //\n"
1262// "#error {\n"
1263// " int a;\n"
1264// "}");
1265// }
1266
Nico Webercf4a79c2013-01-08 17:56:31 +00001267//===----------------------------------------------------------------------===//
1268// Objective-C tests.
1269//===----------------------------------------------------------------------===//
1270
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001271TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1272 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1273 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1274 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001275 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001276 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1277 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1278 format("-(NSInteger)Method3:(id)anObject;"));
1279 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1280 format("-(NSInteger)Method4:(id)anObject;"));
1281 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1282 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1283 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1284 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001285 EXPECT_EQ(
1286 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1287 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001288
1289 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001290 EXPECT_EQ(
1291 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1292 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1293 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1294 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1295 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1296 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1297 format(
1298 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1299 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1300 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1301 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1302 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1303 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001304
1305 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001306 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001307 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1308 // protocol lists (but not for template classes):
1309 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001310
1311 verifyFormat("- (int(*)())foo:(int(*)())f;");
1312 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1313
1314 // If there's no return type (very rare in practice!), LLVM and Google style
1315 // agree.
1316 verifyFormat("- foo:(int)f;");
1317 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001318}
1319
Daniel Jasper886568d2013-01-09 08:36:49 +00001320TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001321 verifyFormat("int (^Block)(int, int);");
1322 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001323}
1324
Nico Weber27d13672013-01-09 20:25:35 +00001325TEST_F(FormatTest, FormatObjCInterface) {
1326 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001327 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001328 "@public\n"
1329 " int field1;\n"
1330 "@protected\n"
1331 " int field2;\n"
1332 "@private\n"
1333 " int field3;\n"
1334 "@package\n"
1335 " int field4;\n"
1336 "}\n"
1337 "+ (id)init;\n"
1338 "@end");
1339
Nico Weber27d13672013-01-09 20:25:35 +00001340 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1341 " @public\n"
1342 " int field1;\n"
1343 " @protected\n"
1344 " int field2;\n"
1345 " @private\n"
1346 " int field3;\n"
1347 " @package\n"
1348 " int field4;\n"
1349 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001350 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001351 "@end");
1352
1353 verifyFormat("@interface Foo\n"
1354 "+ (id)init;\n"
1355 "// Look, a comment!\n"
1356 "- (int)answerWith:(int)i;\n"
1357 "@end");
1358
1359 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001360 "@end\n"
1361 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001362 "@end");
1363
1364 verifyFormat("@interface Foo : Bar\n"
1365 "+ (id)init;\n"
1366 "@end");
1367
Nico Weber5f500df2013-01-10 20:12:55 +00001368 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001369 "+ (id)init;\n"
1370 "@end");
1371
Nico Weber5f500df2013-01-10 20:12:55 +00001372 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001373 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001374 "@end");
1375
Nico Webered91bba2013-01-10 19:19:14 +00001376 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001377 "+ (id)init;\n"
1378 "@end");
1379
Nico Webered91bba2013-01-10 19:19:14 +00001380 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001381 "+ (id)init;\n"
1382 "@end");
1383
Nico Weber5f500df2013-01-10 20:12:55 +00001384 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001385 "+ (id)init;\n"
1386 "@end");
1387
Nico Weber5f500df2013-01-10 20:12:55 +00001388 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001389 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001390 "@end");
1391
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001392 verifyFormat("@interface Foo {\n"
1393 " int _i;\n"
1394 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001395 "+ (id)init;\n"
1396 "@end");
1397
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001398 verifyFormat("@interface Foo : Bar {\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 <Baz, Quux> {\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 (HackStuff) {\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 () {\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 (HackStuff) <MyProtocol> {\n"
1423 " int _i;\n"
1424 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001425 "+ (id)init;\n"
1426 "@end");
1427}
1428
Nico Weber50767d82013-01-09 23:25:37 +00001429TEST_F(FormatTest, FormatObjCImplementation) {
1430 verifyFormat("@implementation Foo : NSObject {\n"
1431 "@public\n"
1432 " int field1;\n"
1433 "@protected\n"
1434 " int field2;\n"
1435 "@private\n"
1436 " int field3;\n"
1437 "@package\n"
1438 " int field4;\n"
1439 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001440 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001441 "@end");
1442
1443 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1444 " @public\n"
1445 " int field1;\n"
1446 " @protected\n"
1447 " int field2;\n"
1448 " @private\n"
1449 " int field3;\n"
1450 " @package\n"
1451 " int field4;\n"
1452 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001453 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001454 "@end");
1455
1456 verifyFormat("@implementation Foo\n"
1457 "+ (id)init {\n"
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001458 " if (true) return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001459 "}\n"
1460 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001461 "- (int)answerWith:(int)i {\n"
1462 " return i;\n"
1463 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001464 "+ (int)answerWith:(int)i {\n"
1465 " return i;\n"
1466 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001467 "@end");
1468
1469 verifyFormat("@implementation Foo\n"
1470 "@end\n"
1471 "@implementation Bar\n"
1472 "@end");
1473
1474 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001475 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001476 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001477 "@end");
1478
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001479 verifyFormat("@implementation Foo {\n"
1480 " int _i;\n"
1481 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001482 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001483 "@end");
1484
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001485 verifyFormat("@implementation Foo : Bar {\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
Nico Webered91bba2013-01-10 19:19:14 +00001491 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001492 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001493 "@end");
1494}
1495
Nico Weber1abe6ea2013-01-09 21:15:03 +00001496TEST_F(FormatTest, FormatObjCProtocol) {
1497 verifyFormat("@protocol Foo\n"
1498 "@property(weak) id delegate;\n"
1499 "- (NSUInteger)numberOfThings;\n"
1500 "@end");
1501
Nico Weber5f500df2013-01-10 20:12:55 +00001502 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001503 "- (NSUInteger)numberOfThings;\n"
1504 "@end");
1505
Nico Weber5f500df2013-01-10 20:12:55 +00001506 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001507 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001508 "@end");
1509
Nico Weber1abe6ea2013-01-09 21:15:03 +00001510 verifyFormat("@protocol Foo;\n"
1511 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001512
1513 verifyFormat("@protocol Foo\n"
1514 "@end\n"
1515 "@protocol Bar\n"
1516 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001517
1518 verifyFormat("@protocol myProtocol\n"
1519 "- (void)mandatoryWithInt:(int)i;\n"
1520 "@optional\n"
1521 "- (void)optional;\n"
1522 "@required\n"
1523 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001524 "@optional\n"
1525 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001526 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001527}
1528
Nico Weberbcfdd262013-01-12 06:18:40 +00001529TEST_F(FormatTest, FormatObjCMethodExpr) {
1530 verifyFormat("[foo bar:baz];");
1531 verifyFormat("return [foo bar:baz];");
1532 verifyFormat("f([foo bar:baz]);");
1533 verifyFormat("f(2, [foo bar:baz]);");
1534 verifyFormat("f(2, a ? b : c);");
1535 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1536
1537 verifyFormat("[foo bar:baz], [foo bar:baz];");
1538 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1539 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1540 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
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] : [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];");
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 // Whew!
1569
1570 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1571 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1572 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1573 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1574 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001575 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001576 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001577
Nico Weberbcfdd262013-01-12 06:18:40 +00001578 verifyFormat("arr[[self indexForFoo:a]];");
1579 verifyFormat("throw [self errorFor:a];");
1580 verifyFormat("@throw [self errorFor:a];");
1581
Nico Webere8ccc812013-01-12 22:48:47 +00001582 // This tests that the formatter doesn't break after "backing" but before ":",
1583 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001584 verifyFormat(
1585 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001586 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1587 " backing:NSBackingStoreBuffered defer:YES]))");
1588
Nico Webere8ccc812013-01-12 22:48:47 +00001589 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1590 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001591
1592}
1593
Nico Weber581f5572013-01-07 15:56:25 +00001594TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001595 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001596 verifyFormat("@catch");
1597 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001598 verifyFormat("@compatibility_alias");
1599 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001600 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001601 verifyFormat("@encode");
1602 verifyFormat("@end");
1603 verifyFormat("@finally");
1604 verifyFormat("@implementation");
1605 verifyFormat("@import");
1606 verifyFormat("@interface");
1607 verifyFormat("@optional");
1608 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001609 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001610 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001611 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001612 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001613 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001614 verifyFormat("@required");
1615 verifyFormat("@selector");
1616 verifyFormat("@synchronized");
1617 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001618 verifyFormat("@throw");
1619 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001620
Nico Webercb4d6902013-01-08 19:40:21 +00001621 verifyFormat("@\"String\"");
1622 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001623 verifyFormat("@+4.8");
1624 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001625 verifyFormat("@1LL");
1626 verifyFormat("@.5");
1627 verifyFormat("@'c'");
1628 verifyFormat("@true");
1629 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001630 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001631 verifyFormat("@[");
1632 verifyFormat("@{");
1633
Nico Weber581f5572013-01-07 15:56:25 +00001634 EXPECT_EQ("@interface", format("@ interface"));
1635
1636 // The precise formatting of this doesn't matter, nobody writes code like
1637 // this.
1638 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001639}
1640
Nico Weberc31689a2013-01-08 19:15:23 +00001641TEST_F(FormatTest, ObjCSnippets) {
1642 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001643 verifyFormat("@autoreleasepool {\n"
1644 " foo();\n"
1645 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001646 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001647 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001648 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001649 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001650 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001651 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001652 verifyFormat("@synchronized(self) {\n"
1653 " f();\n"
1654 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001655
Nico Weber70848232013-01-10 21:30:42 +00001656 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1657 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1658
Nico Webercf4a79c2013-01-08 17:56:31 +00001659 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001660 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1661 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001662}
1663
Daniel Jaspercd162382013-01-07 13:26:07 +00001664} // end namespace tooling
1665} // end namespace clang