blob: cba25fcf8f427253ba5afe0848e744a9ba065415 [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,
32 Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +000033 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
34 return Context.getRewrittenText(ID);
35 }
36
37 std::string format(llvm::StringRef Code,
38 const FormatStyle &Style = getLLVMStyle()) {
39 return format(Code, 0, Code.size(), Style);
40 }
41
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000042 std::string messUp(llvm::StringRef Code) {
43 std::string MessedUp(Code.str());
44 bool InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000045 bool InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000046 bool JustReplacedNewline = false;
47 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
48 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
49 if (JustReplacedNewline)
50 MessedUp[i - 1] = '\n';
51 InComment = true;
Manuel Klimek526ed112013-01-09 15:25:02 +000052 } else if (MessedUp[i] == '#' && JustReplacedNewline) {
53 MessedUp[i - 1] = '\n';
54 InPreprocessorDirective = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +000055 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
56 MessedUp[i] = ' ';
Manuel Klimek526ed112013-01-09 15:25:02 +000057 MessedUp[i + 1] = ' ';
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000058 } else if (MessedUp[i] == '\n') {
59 if (InComment) {
60 InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000061 } else if (InPreprocessorDirective) {
62 InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000063 } else {
64 JustReplacedNewline = true;
65 MessedUp[i] = ' ';
66 }
Manuel Klimekd4397b92013-01-04 23:34:14 +000067 } else if (MessedUp[i] != ' ') {
68 JustReplacedNewline = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000069 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000070 }
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000071 return MessedUp;
72 }
73
Manuel Klimek060143e2013-01-02 18:33:23 +000074 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
75 FormatStyle Style = getLLVMStyle();
76 Style.ColumnLimit = ColumnLimit;
77 return Style;
78 }
79
80 void verifyFormat(llvm::StringRef Code,
81 const FormatStyle &Style = getLLVMStyle()) {
82 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000083 }
84
85 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +000086 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +000087 }
88};
89
Manuel Klimek526ed112013-01-09 15:25:02 +000090TEST_F(FormatTest, MessUp) {
91 EXPECT_EQ("1 2 3", messUp("1 2 3"));
92 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
93 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
94 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
95 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
96}
97
Alexander Kornienko15757312012-12-06 18:03:27 +000098//===----------------------------------------------------------------------===//
99// Basic function tests.
100//===----------------------------------------------------------------------===//
101
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
103 EXPECT_EQ(";", format(";"));
104}
105
106TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
107 EXPECT_EQ("int i;", format(" int i;"));
108 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
109 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
110 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
111}
112
113TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
114 EXPECT_EQ("int i;", format("int\ni;"));
115}
116
117TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000118 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000119}
120
Alexander Kornienko15757312012-12-06 18:03:27 +0000121TEST_F(FormatTest, FormatsNestedCall) {
122 verifyFormat("Method(f1, f2(f3));");
123 verifyFormat("Method(f1(f2, f3()));");
124}
125
Alexander Kornienko15757312012-12-06 18:03:27 +0000126//===----------------------------------------------------------------------===//
127// Tests for control statements.
128//===----------------------------------------------------------------------===//
129
130TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
131 verifyFormat("if (true)\n f();\ng();");
132 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
133 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000134 verifyFormat("if (a)\n"
135 " // comment\n"
136 " f();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000137}
138
139TEST_F(FormatTest, ParseIfElse) {
140 verifyFormat("if (true)\n"
141 " if (true)\n"
142 " if (true)\n"
143 " f();\n"
144 " else\n"
145 " g();\n"
146 " else\n"
147 " h();\n"
148 "else\n"
149 " i();");
150 verifyFormat("if (true)\n"
151 " if (true)\n"
152 " if (true) {\n"
153 " if (true)\n"
154 " f();\n"
155 " } else {\n"
156 " g();\n"
157 " }\n"
158 " else\n"
159 " h();\n"
160 "else {\n"
161 " i();\n"
162 "}");
163}
164
165TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000166 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000167 verifyFormat("if (a)\n"
168 " f();\n"
169 "else if (b)\n"
170 " g();\n"
171 "else\n"
172 " h();");
173}
174
Daniel Jasperbac016b2012-12-03 18:12:45 +0000175TEST_F(FormatTest, FormatsForLoop) {
176 verifyFormat(
177 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000178 " ++VeryVeryLongLoopVariable)\n"
179 " ;");
180 verifyFormat("for (;;)\n"
181 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000182 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000183 verifyFormat("for (;;) {\n"
184 " f();\n"
185 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000186
187 verifyFormat(
188 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
189 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000190 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000191
192 verifyFormat(
193 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000194 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000195}
196
197TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000198 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000199 verifyFormat("while (true)\n"
200 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000201 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000202 verifyFormat("while () {\n"
203 " f();\n"
204 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000205}
206
Alexander Kornienko15757312012-12-06 18:03:27 +0000207TEST_F(FormatTest, FormatsDoWhile) {
208 verifyFormat("do {\n"
209 " do_something();\n"
210 "} while (something());");
211 verifyFormat("do\n"
212 " do_something();\n"
213 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000214}
215
Alexander Kornienko15757312012-12-06 18:03:27 +0000216TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217 verifyFormat("switch (x) {\n"
218 "case 1:\n"
219 " f();\n"
220 " break;\n"
221 "case kFoo:\n"
222 "case ns::kBar:\n"
223 "case kBaz:\n"
224 " break;\n"
225 "default:\n"
226 " g();\n"
227 " break;\n"
228 "}");
229 verifyFormat("switch (x) {\n"
230 "case 1: {\n"
231 " f();\n"
232 " break;\n"
233 "}\n"
234 "}");
235 verifyFormat("switch (test)\n"
236 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000237 verifyGoogleFormat("switch (x) {\n"
238 " case 1:\n"
239 " f();\n"
240 " break;\n"
241 " case kFoo:\n"
242 " case ns::kBar:\n"
243 " case kBaz:\n"
244 " break;\n"
245 " default:\n"
246 " g();\n"
247 " break;\n"
248 "}");
249 verifyGoogleFormat("switch (x) {\n"
250 " case 1: {\n"
251 " f();\n"
252 " break;\n"
253 " }\n"
254 "}");
255 verifyGoogleFormat("switch (test)\n"
256 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000257}
258
Alexander Kornienko15757312012-12-06 18:03:27 +0000259TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000260 verifyFormat("void f() {\n"
261 " some_code();\n"
262 "test_label:\n"
263 " some_other_code();\n"
264 " {\n"
265 " some_more_code();\n"
266 " another_label:\n"
267 " some_more_code();\n"
268 " }\n"
269 "}");
270 verifyFormat("some_code();\n"
271 "test_label:\n"
272 "some_other_code();");
273}
274
Alexander Kornienko15757312012-12-06 18:03:27 +0000275//===----------------------------------------------------------------------===//
276// Tests for comments.
277//===----------------------------------------------------------------------===//
278
279TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000280 verifyFormat("// line 1\n"
281 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000282 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000283
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000284 verifyFormat("void f() {\n"
285 " // Doesn't do anything\n"
286 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000287
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000288 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000289 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000290
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000291 verifyFormat("enum E {\n"
292 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000293 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000294 " VAL_B\n"
295 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000296
297 verifyFormat(
298 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000299 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000300 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
301 " // Comment inside a statement.\n"
302 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000303
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000304 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000305 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000306
307 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000308}
309
310TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000311 verifyFormat("f(/*test=*/ true);");
312}
313
Alexander Kornienko15757312012-12-06 18:03:27 +0000314//===----------------------------------------------------------------------===//
315// Tests for classes, namespaces, etc.
316//===----------------------------------------------------------------------===//
317
318TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000319 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000320}
321
322TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
323 verifyFormat("class A {\n"
324 "public:\n"
325 "protected:\n"
326 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000327 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000328 "};");
329 verifyGoogleFormat("class A {\n"
330 " public:\n"
331 " protected:\n"
332 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000333 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000334 "};");
335}
336
337TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000338 verifyFormat("class A : public B {};");
339 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000340}
341
Manuel Klimekde768542013-01-07 18:10:23 +0000342TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000343 verifyFormat("class A {} a, b;");
344 verifyFormat("struct A {} a, b;");
Manuel Klimekde768542013-01-07 18:10:23 +0000345}
346
Alexander Kornienko15757312012-12-06 18:03:27 +0000347TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000348 verifyFormat("enum {\n"
349 " Zero,\n"
350 " One = 1,\n"
351 " Two = One + 1,\n"
352 " Three = (One + Two),\n"
353 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
354 " Five = (One, Two, Three, Four, 5)\n"
355 "};");
356 verifyFormat("enum Enum {\n"
357 "};");
358 verifyFormat("enum {\n"
359 "};");
360}
361
Alexander Kornienko15757312012-12-06 18:03:27 +0000362TEST_F(FormatTest, FormatsNamespaces) {
363 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000364 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000365 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000366 "}");
367 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000368 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000369 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000370 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000371 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000372 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000373 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000374 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000375 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000376 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000377 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000378}
379
Nico Webera9ccdd12013-01-07 16:36:17 +0000380TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000381 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
382 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000383 verifyFormat("try {\n"
384 " throw a * b;\n"
385 "}\n"
386 "catch (int a) {\n"
387 " // Do nothing.\n"
388 "}\n"
389 "catch (...) {\n"
390 " exit(42);\n"
391 "}");
392
393 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000394 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000395 "catch (...) {\n"
396 " return 5;\n"
397 "}");
398 verifyFormat("class A {\n"
399 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000400 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000401 " catch (...) {\n"
402 " throw;\n"
403 " }\n"
404 "};\n");
405}
406
407TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000408 verifyFormat("@try {\n"
409 " f();\n"
410 "}\n"
411 "@catch (NSException e) {\n"
412 " @throw;\n"
413 "}\n"
414 "@finally {\n"
415 " exit(42);\n"
416 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000417}
418
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000419TEST_F(FormatTest, StaticInitializers) {
420 verifyFormat("static SomeClass SC = { 1, 'a' };");
421
422 // FIXME: Format like enums if the static initializer does not fit on a line.
423 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000424 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000425 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
426 "};");
427
428 verifyFormat(
429 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
430 " looooooooooooooooooooooooooooooooooongname,\n"
431 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000432}
433
Manuel Klimeka080a182013-01-02 16:30:12 +0000434TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
435 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
436 " \\\n"
437 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
438}
439
Daniel Jasper71607512013-01-07 10:48:50 +0000440TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000441 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
442 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000443}
444
Manuel Klimeka080a182013-01-02 16:30:12 +0000445TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
446 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000447 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000448}
449
450TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
451 EXPECT_EQ("#line 42 \"test\"\n",
452 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000453 EXPECT_EQ("#define A \\\n B\n",
454 format("# \\\n define \\\n A \\\n B\n",
455 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000456}
457
458TEST_F(FormatTest, EndOfFileEndsPPDirective) {
459 EXPECT_EQ("#line 42 \"test\"",
460 format("# \\\n line \\\n 42 \\\n \"test\""));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000461 EXPECT_EQ("#define A \\\n B",
462 format("# \\\n define \\\n A \\\n B",
463 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000464}
465
Manuel Klimek060143e2013-01-02 18:33:23 +0000466TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000467 // If the macro fits in one line, we still do not get the full
468 // line, as only the next line decides whether we need an escaped newline and
469 // thus use the last column.
470 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000471
Manuel Klimekd544c572013-01-07 09:24:17 +0000472 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
473 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000474 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
475}
476
477TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000478 EXPECT_EQ("// some comment\n"
479 "#include \"a.h\"\n"
480 "#define A(A,\\\n"
481 " B)\n"
482 "#include \"b.h\"\n"
483 "// some comment\n",
484 format(" // some comment\n"
485 " #include \"a.h\"\n"
486 "#define A(A,\\\n"
487 " B)\n"
488 " #include \"b.h\"\n"
489 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000490}
491
Manuel Klimekd4397b92013-01-04 23:34:14 +0000492TEST_F(FormatTest, LayoutSingleHash) {
493 EXPECT_EQ("#\na;", format("#\na;"));
494}
495
496TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
497 EXPECT_EQ("#define A \\\n"
498 " c; \\\n"
499 " e;\n"
500 "f;", format("#define A c; e;\n"
501 "f;", getLLVMStyleWithColumns(14)));
502}
503
504TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000505 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000506}
507
508TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000509 EXPECT_EQ("# define A\\\n b;",
510 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000511}
512
513TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000514 EXPECT_EQ("int x,\n"
515 "#define A\n"
516 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000517}
518
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000519TEST_F(FormatTest, HashInMacroDefinition) {
520 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
521 verifyFormat("#define A \\\n"
522 " { \\\n"
523 " f(#c);\\\n"
524 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000525
526 verifyFormat("#define A(X) \\\n"
527 " void function##X()", getLLVMStyleWithColumns(22));
528
529 verifyFormat("#define A(a, b, c) \\\n"
530 " void a##b##c()", getLLVMStyleWithColumns(22));
531
532 verifyFormat("#define A \\\n"
533 " void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000534}
535
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000536TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
537 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
538}
539
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000540TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000541 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000542}
543
Manuel Klimeka5342db2013-01-06 20:07:31 +0000544TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
545 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
546 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
547 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
548 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
549}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000550
Manuel Klimek95419382013-01-07 07:56:50 +0000551TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000552 EXPECT_EQ(
553 "#define A \\\n int i; \\\n int j;",
554 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000555}
556
Manuel Klimekd544c572013-01-07 09:24:17 +0000557TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
558 verifyFormat("#define A \\\n"
559 " int v( \\\n"
560 " a); \\\n"
561 " int i;", getLLVMStyleWithColumns(11));
562}
563
Manuel Klimeka080a182013-01-02 16:30:12 +0000564TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000565 EXPECT_EQ(
566 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
567 " \\\n"
568 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
569 "\n"
570 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
571 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
572 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
573 "\\\n"
574 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
575 " \n"
576 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
577 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000578}
579
Manuel Klimek526ed112013-01-09 15:25:02 +0000580TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
581 EXPECT_EQ("int\n"
582 "#define A\n"
583 " a;",
584 format("int\n#define A\na;"));
585 verifyFormat(
586 "functionCallTo(someOtherFunction(\n"
587 " withSomeParameters, whichInSequence,\n"
588 " areLongerThanALine(andAnotherCall,\n"
589 "#define A \\\n"
590 " B\n"
591 " withMoreParamters,\n"
592 " whichStronglyInfluenceTheLayout),\n"
593 " andMoreParameters),\n"
594 " trailing);", getLLVMStyleWithColumns(69));
595}
596
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000597TEST_F(FormatTest, LayoutBlockInsideParens) {
598 EXPECT_EQ("functionCall({\n"
599 " int i;\n"
600 "});", format(" functionCall ( {int i;} );"));
601}
602
603TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000604 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000605 "int i;", format(" SOME_MACRO {int i;} int i;"));
606}
607
608TEST_F(FormatTest, LayoutNestedBlocks) {
609 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
610 " struct s {\n"
611 " int i;\n"
612 " };\n"
613 " s kBitsToOs[] = { { 10 } };\n"
614 " for (int i = 0; i < 10; ++i)\n"
615 " return;\n"
616 "}");
617}
618
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000619TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
620 EXPECT_EQ("{}", format("{}"));
621}
622
Alexander Kornienko15757312012-12-06 18:03:27 +0000623//===----------------------------------------------------------------------===//
624// Line break tests.
625//===----------------------------------------------------------------------===//
626
627TEST_F(FormatTest, FormatsFunctionDefinition) {
628 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
629 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000630 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000631}
632
633TEST_F(FormatTest, FormatsAwesomeMethodCall) {
634 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000635 "SomeLongMethodName(SomeReallyLongMethod(\n"
636 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
637 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000638}
639
Daniel Jasper1321eb52012-12-18 21:05:13 +0000640TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000641 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000642 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
643 getLLVMStyleWithColumns(45));
644 verifyFormat("Constructor()\n"
645 " : Inttializer(FitsOnTheLine) {}",
646 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000647
648 verifyFormat(
649 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000650 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000651
652 verifyFormat(
653 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000654 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
655 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
656 verifyGoogleFormat(
657 "SomeClass::Constructor()\n"
658 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
659 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
660 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
661
662 verifyFormat(
663 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000664 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000665 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000666
667 verifyFormat("Constructor()\n"
668 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
669 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
670 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000671 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000672
673 // Here a line could be saved by splitting the second initializer onto two
674 // lines, but that is not desireable.
675 verifyFormat("Constructor()\n"
676 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
677 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000678 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000679
680 verifyGoogleFormat("MyClass::MyClass(int var)\n"
681 " : some_var_(var), // 4 space indent\n"
682 " some_other_var_(var + 1) { // lined up\n"
683 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000684
685 // This test takes VERY long when memoization is broken.
686 verifyGoogleFormat(
687 "Constructor()\n"
688 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
689 " a, a, a,\n"
690 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
691 " a, a, a,\n"
692 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
693 " a, a, a,\n"
694 " a, a, a, a, a, a, a, a, a, a, a)\n"
695 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
696 " a, a, a,\n"
697 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
698 " a, a, a,\n"
699 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
700 " a, a, a,\n"
701 " a, a, a, a, a, a, a, a, a, a, a) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000702}
703
Alexander Kornienko15757312012-12-06 18:03:27 +0000704TEST_F(FormatTest, BreaksAsHighAsPossible) {
705 verifyFormat(
706 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
707 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
708 " f();");
709}
710
Daniel Jasperbac016b2012-12-03 18:12:45 +0000711TEST_F(FormatTest, BreaksDesireably) {
712 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
713 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000714 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000715
716 verifyFormat(
717 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000719
720 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000723
724 verifyFormat(
725 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
727 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000729
730 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
731 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
732
Daniel Jasper723f0302013-01-02 14:40:02 +0000733 verifyFormat(
734 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
736
Daniel Jasper33182dd2012-12-05 14:57:28 +0000737 // This test case breaks on an incorrect memoization, i.e. an optimization not
738 // taking into account the StopAt value.
739 verifyFormat(
740 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000741 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
742 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
743 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000744
Daniel Jaspercd162382013-01-07 13:26:07 +0000745 verifyFormat("{\n {\n {\n"
746 " Annotation.SpaceRequiredBefore =\n"
747 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
748 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
749 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000750}
751
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000752TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
753 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
754 " GUARDED_BY(aaaaaaaaaaaaa);");
755}
756
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000757TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
758 verifyFormat(
759 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000760 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000761 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000762 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000763 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000764 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000765 verifyFormat(
766 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000767 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000768}
769
Daniel Jasper9cda8002013-01-07 13:08:40 +0000770TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
771 verifyFormat(
772 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
773 " SI->getAlignment(),\n"
774 " SI->getPointerAddressSpaceee());\n");
775 verifyFormat(
776 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
777 " Line.Tokens.front().Tok.getLocation(),\n"
778 " Line.Tokens.back().Tok.getLocation());");
779}
780
Daniel Jaspercf225b62012-12-24 13:43:52 +0000781TEST_F(FormatTest, AlignsAfterAssignments) {
782 verifyFormat(
783 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000784 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000785 verifyFormat(
786 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000787 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000788 verifyFormat(
789 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000790 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000791 verifyFormat(
792 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000793 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000794 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000795 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
796 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
797 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000798}
799
800TEST_F(FormatTest, AlignsAfterReturn) {
801 verifyFormat(
802 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
803 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
804 verifyFormat(
805 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
806 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
807}
808
Daniel Jasper9c837d02013-01-09 07:06:56 +0000809TEST_F(FormatTest, BreaksConditionalExpressions) {
810 verifyFormat(
811 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
812 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
814 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
815 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
816}
817
Nico Weber7d37b8b2013-01-12 01:28:06 +0000818TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
819 verifyFormat("arr[foo ? bar : baz];");
820 verifyFormat("f()[foo ? bar : baz];");
821 verifyFormat("(a + b)[foo ? bar : baz];");
822 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
823}
824
Daniel Jasperbac016b2012-12-03 18:12:45 +0000825TEST_F(FormatTest, AlignsStringLiterals) {
826 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
827 " \"short literal\");");
828 verifyFormat(
829 "looooooooooooooooooooooooongFunction(\n"
830 " \"short literal\"\n"
831 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
832}
833
Alexander Kornienko15757312012-12-06 18:03:27 +0000834TEST_F(FormatTest, AlignsPipes) {
835 verifyFormat(
836 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
837 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
838 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
839 verifyFormat(
840 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
841 " << aaaaaaaaaaaaaaaaaaaa;");
842 verifyFormat(
843 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
844 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
845 verifyFormat(
846 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
847 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
848 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
849 verifyFormat(
850 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
851 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
852 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
853}
854
Daniel Jasperbac016b2012-12-03 18:12:45 +0000855TEST_F(FormatTest, UnderstandsEquals) {
856 verifyFormat(
857 "aaaaaaaaaaaaaaaaa =\n"
858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
859 verifyFormat(
860 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000862 verifyFormat(
863 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000864 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000865 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000867
Daniel Jasper9cda8002013-01-07 13:08:40 +0000868 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000869 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000870 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000871 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000872}
873
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000874TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000875 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
876 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000877
Daniel Jasper1321eb52012-12-18 21:05:13 +0000878 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
879 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000880
881 verifyFormat(
882 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
883 " Parameter2);");
884
885 verifyFormat(
886 "ShortObject->shortFunction(\n"
887 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
888 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
889
890 verifyFormat("loooooooooooooongFunction(\n"
891 " LoooooooooooooongObject->looooooooooooooooongFunction());");
892
893 verifyFormat(
894 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
895 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
896
Daniel Jasper46a46a22013-01-07 07:13:20 +0000897 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000898 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000899 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000900 verifyFormat(
901 "aaaaaaaaaaa->aaaaaaaaa(\n"
902 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
903 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000904}
905
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000906TEST_F(FormatTest, WrapsTemplateDeclarations) {
907 verifyFormat("template <typename T>\n"
908 "virtual void loooooooooooongFunction(int Param1, int Param2);");
909 verifyFormat(
910 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
911 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
912 verifyFormat(
913 "template <typename T>\n"
914 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
915 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000916 verifyFormat(
917 "template <typename T>\n"
918 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
919 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
920 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +0000921 verifyFormat("template <typename T>\n"
922 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
923 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000924 verifyFormat(
925 "template <typename T1, typename T2 = char, typename T3 = char,\n"
926 " typename T4 = char>\n"
927 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000928}
929
Daniel Jasperbac016b2012-12-03 18:12:45 +0000930TEST_F(FormatTest, UnderstandsTemplateParameters) {
931 verifyFormat("A<int> a;");
932 verifyFormat("A<A<A<int> > > a;");
933 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
934 verifyFormat("bool x = a < 1 || 2 > a;");
935 verifyFormat("bool x = 5 < f<int>();");
936 verifyFormat("bool x = f<int>() > 5;");
937 verifyFormat("bool x = 5 < a<int>::x;");
938 verifyFormat("bool x = a < 4 ? a > 2 : false;");
939 verifyFormat("bool x = f() ? a < 2 : a > 2;");
940
941 verifyGoogleFormat("A<A<int>> a;");
942 verifyGoogleFormat("A<A<A<int>>> a;");
943 verifyGoogleFormat("A<A<A<A<int>>>> a;");
944
945 verifyFormat("test >> a >> b;");
946 verifyFormat("test << a >> b;");
947
948 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000949 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000950}
951
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000952TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000953 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +0000954 verifyFormat("f(-1, -2, -3);");
955 verifyFormat("a[-1] = 5;");
956 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000957 verifyFormat("if (i == -1) {}");
958 verifyFormat("if (i != -1) {}");
959 verifyFormat("if (i > -1) {}");
960 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +0000961 verifyFormat("++(a->f());");
962 verifyFormat("--(a->f());");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000963 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000964
965 verifyFormat("a-- > b;");
966 verifyFormat("b ? -a : c;");
967 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000968 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +0000969 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +0000970 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +0000971
972 verifyFormat("return -1;");
973 verifyFormat("switch (a) {\n"
974 "case -1:\n"
975 " break;\n"
976 "}");
Nico Webercc191d12013-01-12 05:41:23 +0000977
978 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
979 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000980}
981
982TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +0000983 verifyFormat("bool operator<();");
984 verifyFormat("bool operator>();");
985 verifyFormat("bool operator=();");
986 verifyFormat("bool operator==();");
987 verifyFormat("bool operator!=();");
988 verifyFormat("int operator+();");
989 verifyFormat("int operator++();");
990 verifyFormat("bool operator();");
991 verifyFormat("bool operator()();");
992 verifyFormat("bool operator[]();");
993 verifyFormat("operator bool();");
994 verifyFormat("operator SomeType<int>();");
995 verifyFormat("void *operator new(std::size_t size);");
996 verifyFormat("void *operator new[](std::size_t size);");
997 verifyFormat("void operator delete(void *ptr);");
998 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000999}
1000
Daniel Jasper088dab52013-01-11 16:09:04 +00001001TEST_F(FormatTest, UnderstandsNewAndDelete) {
1002 verifyFormat("A *a = new A;");
1003 verifyFormat("A *a = new (placement) A;");
1004 verifyFormat("delete a;");
1005 verifyFormat("delete (A *)a;");
1006}
1007
Daniel Jasper5d334402013-01-02 08:57:10 +00001008TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001009 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001010 verifyFormat("f(a, *a);");
1011 verifyFormat("f(*a);");
1012 verifyFormat("int a = b * 10;");
1013 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001014 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001015 verifyFormat("int a += b * c;");
1016 verifyFormat("int a -= b * c;");
1017 verifyFormat("int a *= b * c;");
1018 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001019 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001020 verifyFormat("int a = *b * c;");
1021 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001022 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001023 verifyFormat("return 10 * b;");
1024 verifyFormat("return *b * *c;");
1025 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001026 verifyFormat("f(b ? *c : *d);");
1027 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001028 verifyFormat("*b = a;");
1029 verifyFormat("a * ~b;");
1030 verifyFormat("a * !b;");
1031 verifyFormat("a * +b;");
1032 verifyFormat("a * -b;");
1033 verifyFormat("a * ++b;");
1034 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001035 verifyFormat("a[4] * b;");
1036 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001037 verifyFormat("a * [self dostuff];");
1038 verifyFormat("a * (a + b);");
1039 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001040 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001041
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001042 verifyFormat("InvalidRegions[*R] = 0;");
1043
Daniel Jasper8b39c662012-12-10 18:59:13 +00001044 verifyFormat("A<int *> a;");
1045 verifyFormat("A<int **> a;");
1046 verifyFormat("A<int *, int *> a;");
1047 verifyFormat("A<int **, int **> a;");
Daniel Jasperef5b9c32013-01-02 15:46:59 +00001048 verifyFormat("Type *A = static_cast<Type *>(P);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001049 verifyFormat("Type *A = (Type *)P;");
1050 verifyFormat("Type *A = (vector<Type *, int *>)P;");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001051
Daniel Jasper2db356d2013-01-08 20:03:18 +00001052 verifyFormat(
1053 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1055
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001056 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001057 verifyGoogleFormat("A<int*> a;");
1058 verifyGoogleFormat("A<int**> a;");
1059 verifyGoogleFormat("A<int*, int*> a;");
1060 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001061 verifyGoogleFormat("f(b ? *c : *d);");
1062 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001063}
1064
Daniel Jasper46ef8522013-01-10 13:08:12 +00001065TEST_F(FormatTest, FormatsFunctionTypes) {
1066 // FIXME: Determine the cases that need a space after the return type and fix.
1067 verifyFormat("A<bool()> a;");
1068 verifyFormat("A<SomeType()> a;");
1069 verifyFormat("A<void(*)(int, std::string)> a;");
1070
1071 verifyFormat("int(*func)(void *);");
1072}
1073
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001074TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001075 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001076 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001077 verifyFormat(
1078 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1079 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001080 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001081}
1082
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001083TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1084 verifyFormat("(a)->b();");
1085 verifyFormat("--a;");
1086}
1087
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001088TEST_F(FormatTest, HandlesIncludeDirectives) {
1089 EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001090 EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n"));
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001091 EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
1092 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
1093 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
Nico Weberb23ae0c2012-12-21 18:21:56 +00001094
1095 EXPECT_EQ("#import <string>\n", format("#import <string>\n"));
1096 EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n"));
1097 EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n"));
1098 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
1099 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001100}
1101
Alexander Kornienko15757312012-12-06 18:03:27 +00001102//===----------------------------------------------------------------------===//
1103// Error recovery tests.
1104//===----------------------------------------------------------------------===//
1105
Daniel Jasper700e7102013-01-10 09:26:47 +00001106TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1107 verifyFormat("void f() { return } 42");
1108}
1109
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001110TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1111 verifyFormat("int aaaaaaaa =\n"
1112 " // Overly long comment\n"
1113 " b;", getLLVMStyleWithColumns(20));
1114 verifyFormat("function(\n"
1115 " ShortArgument,\n"
1116 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1117}
1118
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001119TEST_F(FormatTest, IncorrectAccessSpecifier) {
1120 verifyFormat("public:");
1121 verifyFormat("class A {\n"
1122 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001123 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001124 "};");
1125 verifyFormat("public\n"
1126 "int qwerty;");
1127 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001128 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001129 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001130 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001131 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001132 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001133}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001134
Alexander Kornienko393b0082012-12-04 15:40:36 +00001135TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1136 verifyFormat("{");
1137}
1138
1139TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001140 verifyFormat("do {}");
1141 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001142 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001143 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001144 "wheeee(fun);");
1145 verifyFormat("do {\n"
1146 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001147 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001148}
1149
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001150TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001151 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001152 verifyFormat("switch {\n foo;\n foo();\n}");
1153 verifyFormat("for {\n foo;\n foo();\n}");
1154 verifyFormat("while {\n foo;\n foo();\n}");
1155 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001156}
1157
Daniel Jasper1f42f112013-01-04 18:52:56 +00001158TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1159 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001160 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001161}
1162
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001163TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001164 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1165 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1166 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1167 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001168
1169 FormatStyle Style = getLLVMStyle();
1170 Style.ColumnLimit = 10;
1171 EXPECT_EQ("{\n"
1172 " {\n"
1173 " breakme(\n"
1174 " qwe);\n"
1175 "}\n", format("{\n"
1176 " {\n"
1177 " breakme(qwe);\n"
1178 "}\n", Style));
1179
1180}
1181
Manuel Klimek2851c162013-01-10 14:36:46 +00001182TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1183 verifyFormat(
1184 "int x = {\n"
1185 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001186 " b(alongervariable)\n"
1187 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001188}
1189
1190TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1191 verifyFormat(
1192 "Aaa({\n"
1193 " int i;\n"
1194 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1195 " ccccccccccccccccc));");
1196}
1197
Manuel Klimek517e8942013-01-11 17:54:10 +00001198TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1199 verifyFormat("void f() { return 42; }");
1200 verifyFormat("void f() {\n"
1201 " // Comment\n"
1202 "}");
1203 verifyFormat("{\n"
1204 "#error {\n"
1205 " int a;\n"
1206 "}");
1207 verifyFormat("{\n"
1208 " int a;\n"
1209 "#error {\n"
1210 "}");
1211}
1212
Manuel Klimek606e07e2013-01-11 18:13:04 +00001213TEST_F(FormatTest, BracedInitListWithElaboratedTypeSpecifier) {
1214 verifyFormat("struct foo a = { bar };\nint n;");
1215}
1216
Manuel Klimek517e8942013-01-11 17:54:10 +00001217// FIXME: This breaks the order of the unwrapped lines:
1218// TEST_F(FormatTest, OrderUnwrappedLines) {
1219// verifyFormat("{\n"
1220// " bool a; //\n"
1221// "#error {\n"
1222// " int a;\n"
1223// "}");
1224// }
1225
Nico Webercf4a79c2013-01-08 17:56:31 +00001226//===----------------------------------------------------------------------===//
1227// Objective-C tests.
1228//===----------------------------------------------------------------------===//
1229
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001230TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1231 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1232 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1233 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001234 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001235 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1236 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1237 format("-(NSInteger)Method3:(id)anObject;"));
1238 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1239 format("-(NSInteger)Method4:(id)anObject;"));
1240 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1241 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1242 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1243 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001244 EXPECT_EQ(
1245 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1246 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001247
1248 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001249 EXPECT_EQ(
1250 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1251 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1252 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1253 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1254 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1255 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1256 format(
1257 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1258 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1259 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1260 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1261 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1262 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001263
1264 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001265 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001266 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1267 // protocol lists (but not for template classes):
1268 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001269
1270 verifyFormat("- (int(*)())foo:(int(*)())f;");
1271 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1272
1273 // If there's no return type (very rare in practice!), LLVM and Google style
1274 // agree.
1275 verifyFormat("- foo:(int)f;");
1276 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001277}
1278
Daniel Jasper886568d2013-01-09 08:36:49 +00001279TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001280 verifyFormat("int (^Block)(int, int);");
1281 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001282}
1283
Nico Weber27d13672013-01-09 20:25:35 +00001284TEST_F(FormatTest, FormatObjCInterface) {
1285 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001286 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001287 "@public\n"
1288 " int field1;\n"
1289 "@protected\n"
1290 " int field2;\n"
1291 "@private\n"
1292 " int field3;\n"
1293 "@package\n"
1294 " int field4;\n"
1295 "}\n"
1296 "+ (id)init;\n"
1297 "@end");
1298
Nico Weber27d13672013-01-09 20:25:35 +00001299 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1300 " @public\n"
1301 " int field1;\n"
1302 " @protected\n"
1303 " int field2;\n"
1304 " @private\n"
1305 " int field3;\n"
1306 " @package\n"
1307 " int field4;\n"
1308 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001309 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001310 "@end");
1311
1312 verifyFormat("@interface Foo\n"
1313 "+ (id)init;\n"
1314 "// Look, a comment!\n"
1315 "- (int)answerWith:(int)i;\n"
1316 "@end");
1317
1318 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001319 "@end\n"
1320 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001321 "@end");
1322
1323 verifyFormat("@interface Foo : Bar\n"
1324 "+ (id)init;\n"
1325 "@end");
1326
Nico Weber5f500df2013-01-10 20:12:55 +00001327 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001328 "+ (id)init;\n"
1329 "@end");
1330
Nico Weber5f500df2013-01-10 20:12:55 +00001331 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001332 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001333 "@end");
1334
Nico Webered91bba2013-01-10 19:19:14 +00001335 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001336 "+ (id)init;\n"
1337 "@end");
1338
Nico Webered91bba2013-01-10 19:19:14 +00001339 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001340 "+ (id)init;\n"
1341 "@end");
1342
Nico Weber5f500df2013-01-10 20:12:55 +00001343 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001344 "+ (id)init;\n"
1345 "@end");
1346
Nico Weber5f500df2013-01-10 20:12:55 +00001347 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001348 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001349 "@end");
1350
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001351 verifyFormat("@interface Foo {\n"
1352 " int _i;\n"
1353 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001354 "+ (id)init;\n"
1355 "@end");
1356
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001357 verifyFormat("@interface Foo : Bar {\n"
1358 " int _i;\n"
1359 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001360 "+ (id)init;\n"
1361 "@end");
1362
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001363 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1364 " int _i;\n"
1365 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001366 "+ (id)init;\n"
1367 "@end");
1368
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001369 verifyFormat("@interface Foo (HackStuff) {\n"
1370 " int _i;\n"
1371 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001372 "+ (id)init;\n"
1373 "@end");
1374
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001375 verifyFormat("@interface Foo () {\n"
1376 " int _i;\n"
1377 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001378 "+ (id)init;\n"
1379 "@end");
1380
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001381 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1382 " int _i;\n"
1383 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001384 "+ (id)init;\n"
1385 "@end");
1386}
1387
Nico Weber50767d82013-01-09 23:25:37 +00001388TEST_F(FormatTest, FormatObjCImplementation) {
1389 verifyFormat("@implementation Foo : NSObject {\n"
1390 "@public\n"
1391 " int field1;\n"
1392 "@protected\n"
1393 " int field2;\n"
1394 "@private\n"
1395 " int field3;\n"
1396 "@package\n"
1397 " int field4;\n"
1398 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001399 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001400 "@end");
1401
1402 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1403 " @public\n"
1404 " int field1;\n"
1405 " @protected\n"
1406 " int field2;\n"
1407 " @private\n"
1408 " int field3;\n"
1409 " @package\n"
1410 " int field4;\n"
1411 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001412 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001413 "@end");
1414
1415 verifyFormat("@implementation Foo\n"
1416 "+ (id)init {\n"
1417 " if (true)\n"
1418 " return nil;\n"
1419 "}\n"
1420 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001421 "- (int)answerWith:(int)i {\n"
1422 " return i;\n"
1423 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001424 "+ (int)answerWith:(int)i {\n"
1425 " return i;\n"
1426 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001427 "@end");
1428
1429 verifyFormat("@implementation Foo\n"
1430 "@end\n"
1431 "@implementation Bar\n"
1432 "@end");
1433
1434 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001435 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001436 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001437 "@end");
1438
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001439 verifyFormat("@implementation Foo {\n"
1440 " int _i;\n"
1441 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001442 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001443 "@end");
1444
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001445 verifyFormat("@implementation Foo : Bar {\n"
1446 " int _i;\n"
1447 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001448 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001449 "@end");
1450
Nico Webered91bba2013-01-10 19:19:14 +00001451 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001452 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001453 "@end");
1454}
1455
Nico Weber1abe6ea2013-01-09 21:15:03 +00001456TEST_F(FormatTest, FormatObjCProtocol) {
1457 verifyFormat("@protocol Foo\n"
1458 "@property(weak) id delegate;\n"
1459 "- (NSUInteger)numberOfThings;\n"
1460 "@end");
1461
Nico Weber5f500df2013-01-10 20:12:55 +00001462 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001463 "- (NSUInteger)numberOfThings;\n"
1464 "@end");
1465
Nico Weber5f500df2013-01-10 20:12:55 +00001466 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001467 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001468 "@end");
1469
Nico Weber1abe6ea2013-01-09 21:15:03 +00001470 verifyFormat("@protocol Foo;\n"
1471 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001472
1473 verifyFormat("@protocol Foo\n"
1474 "@end\n"
1475 "@protocol Bar\n"
1476 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001477
1478 verifyFormat("@protocol myProtocol\n"
1479 "- (void)mandatoryWithInt:(int)i;\n"
1480 "@optional\n"
1481 "- (void)optional;\n"
1482 "@required\n"
1483 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001484 "@optional\n"
1485 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001486 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001487}
1488
Nico Weberbcfdd262013-01-12 06:18:40 +00001489TEST_F(FormatTest, FormatObjCMethodExpr) {
1490 verifyFormat("[foo bar:baz];");
1491 verifyFormat("return [foo bar:baz];");
1492 verifyFormat("f([foo bar:baz]);");
1493 verifyFormat("f(2, [foo bar:baz]);");
1494 verifyFormat("f(2, a ? b : c);");
1495 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1496
1497 verifyFormat("[foo bar:baz], [foo bar:baz];");
1498 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1499 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1500 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1501 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1502 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1503 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1504 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1505 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1506 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1507 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1508 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1509 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1510 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1511 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1512 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1513 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1514 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1515 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1516 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1517 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1518 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1519 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1520 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1521 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1522 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1523 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1524 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1525 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1526 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1527 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1528 // Whew!
1529
1530 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1531 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1532 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1533 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1534 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
1535
Nico Weberbcfdd262013-01-12 06:18:40 +00001536 verifyFormat("arr[[self indexForFoo:a]];");
1537 verifyFormat("throw [self errorFor:a];");
1538 verifyFormat("@throw [self errorFor:a];");
1539
Nico Webere8ccc812013-01-12 22:48:47 +00001540 // This tests that the formatter doesn't break after "backing" but before ":",
1541 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001542 verifyFormat(
1543 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001544 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1545 " backing:NSBackingStoreBuffered defer:YES]))");
1546
1547 verifyFormat("[foo setasdfasdffffffffffffadfasdfasdf:\n"
1548 " [bar dowith:asdfdsfasdfasdfasfasfasfsafasdfsfad]];");
1549
1550 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1551 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001552
1553}
1554
Nico Weber581f5572013-01-07 15:56:25 +00001555TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001556 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001557 verifyFormat("@catch");
1558 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001559 verifyFormat("@compatibility_alias");
1560 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001561 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001562 verifyFormat("@encode");
1563 verifyFormat("@end");
1564 verifyFormat("@finally");
1565 verifyFormat("@implementation");
1566 verifyFormat("@import");
1567 verifyFormat("@interface");
1568 verifyFormat("@optional");
1569 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001570 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001571 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001572 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001573 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001574 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001575 verifyFormat("@required");
1576 verifyFormat("@selector");
1577 verifyFormat("@synchronized");
1578 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001579 verifyFormat("@throw");
1580 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001581
Nico Webercb4d6902013-01-08 19:40:21 +00001582 verifyFormat("@\"String\"");
1583 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001584 verifyFormat("@+4.8");
1585 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001586 verifyFormat("@1LL");
1587 verifyFormat("@.5");
1588 verifyFormat("@'c'");
1589 verifyFormat("@true");
1590 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001591 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001592 verifyFormat("@[");
1593 verifyFormat("@{");
1594
Nico Weber581f5572013-01-07 15:56:25 +00001595 EXPECT_EQ("@interface", format("@ interface"));
1596
1597 // The precise formatting of this doesn't matter, nobody writes code like
1598 // this.
1599 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001600}
1601
Nico Weberc31689a2013-01-08 19:15:23 +00001602TEST_F(FormatTest, ObjCSnippets) {
1603 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001604 verifyFormat("@autoreleasepool {\n"
1605 " foo();\n"
1606 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001607 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001608 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001609 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001610 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001611 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001612 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001613 verifyFormat("@synchronized(self) {\n"
1614 " f();\n"
1615 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001616
Nico Weber70848232013-01-10 21:30:42 +00001617 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1618 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1619
Nico Webercf4a79c2013-01-08 17:56:31 +00001620 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001621 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1622 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001623}
1624
Daniel Jaspercd162382013-01-07 13:26:07 +00001625} // end namespace tooling
1626} // end namespace clang