blob: 7d8ed14191f1802b5f64a016117f97451d7283b7 [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
Manuel Klimekca547db2013-01-16 14:55:28 +000010#define DEBUG_TYPE "format-test"
11
Chandler Carruth1050e8b2012-12-04 09:45:34 +000012#include "clang/Format/Format.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000013#include "clang/Lex/Lexer.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000014#include "gtest/gtest.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000015#include "llvm/Support/Debug.h"
16#include "../Tooling/RewriterTestContext.h"
17
18// Uncomment to get debug output from tests:
19// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
Daniel Jasperbac016b2012-12-03 18:12:45 +000020
21namespace clang {
22namespace format {
23
24class FormatTest : public ::testing::Test {
25protected:
26 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
27 const FormatStyle &Style) {
Manuel Klimekca547db2013-01-16 14:55:28 +000028 DEBUG(llvm::errs() << "---\n");
Daniel Jasperbac016b2012-12-03 18:12:45 +000029 RewriterTestContext Context;
30 FileID ID = Context.createInMemoryFile("input.cc", Code);
31 SourceLocation Start =
32 Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
33 std::vector<CharSourceRange> Ranges(
34 1,
35 CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
Daniel Jasper46ef8522013-01-10 13:08:12 +000036 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
37 getFormattingLangOpts());
Daniel Jaspercd162382013-01-07 13:26:07 +000038 tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +000039 Ranges,
40 new IgnoringDiagConsumer());
Daniel Jasperbac016b2012-12-03 18:12:45 +000041 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
Manuel Klimekca547db2013-01-16 14:55:28 +000042 DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n");
Daniel Jasperbac016b2012-12-03 18:12:45 +000043 return Context.getRewrittenText(ID);
44 }
45
46 std::string format(llvm::StringRef Code,
47 const FormatStyle &Style = getLLVMStyle()) {
48 return format(Code, 0, Code.size(), Style);
49 }
50
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000051 std::string messUp(llvm::StringRef Code) {
52 std::string MessedUp(Code.str());
53 bool InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000054 bool InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000055 bool JustReplacedNewline = false;
56 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
57 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
58 if (JustReplacedNewline)
59 MessedUp[i - 1] = '\n';
60 InComment = true;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +000061 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
62 if (i != 0) MessedUp[i - 1] = '\n';
Manuel Klimek526ed112013-01-09 15:25:02 +000063 InPreprocessorDirective = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +000064 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
65 MessedUp[i] = ' ';
Manuel Klimek526ed112013-01-09 15:25:02 +000066 MessedUp[i + 1] = ' ';
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000067 } else if (MessedUp[i] == '\n') {
68 if (InComment) {
69 InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000070 } else if (InPreprocessorDirective) {
71 InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000072 } else {
73 JustReplacedNewline = true;
74 MessedUp[i] = ' ';
75 }
Manuel Klimekd4397b92013-01-04 23:34:14 +000076 } else if (MessedUp[i] != ' ') {
77 JustReplacedNewline = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000078 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000079 }
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000080 return MessedUp;
81 }
82
Manuel Klimek060143e2013-01-02 18:33:23 +000083 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
84 FormatStyle Style = getLLVMStyle();
85 Style.ColumnLimit = ColumnLimit;
86 return Style;
87 }
88
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000089 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
90 FormatStyle Style = getGoogleStyle();
91 Style.ColumnLimit = ColumnLimit;
92 return Style;
93 }
94
Manuel Klimek060143e2013-01-02 18:33:23 +000095 void verifyFormat(llvm::StringRef Code,
96 const FormatStyle &Style = getLLVMStyle()) {
97 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000098 }
99
100 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +0000101 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102 }
103};
104
Manuel Klimek526ed112013-01-09 15:25:02 +0000105TEST_F(FormatTest, MessUp) {
106 EXPECT_EQ("1 2 3", messUp("1 2 3"));
107 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
108 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
109 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
110 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
111}
112
Alexander Kornienko15757312012-12-06 18:03:27 +0000113//===----------------------------------------------------------------------===//
114// Basic function tests.
115//===----------------------------------------------------------------------===//
116
Daniel Jasperbac016b2012-12-03 18:12:45 +0000117TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
118 EXPECT_EQ(";", format(";"));
119}
120
121TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
122 EXPECT_EQ("int i;", format(" int i;"));
123 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
124 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
125 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
126}
127
128TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
129 EXPECT_EQ("int i;", format("int\ni;"));
130}
131
132TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000133 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000134}
135
Alexander Kornienko15757312012-12-06 18:03:27 +0000136TEST_F(FormatTest, FormatsNestedCall) {
137 verifyFormat("Method(f1, f2(f3));");
138 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper4e9008a2013-01-13 08:19:51 +0000139 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000140}
141
Daniel Jasper6b825c22013-01-16 07:19:28 +0000142TEST_F(FormatTest, ImportantSpaces) {
143 verifyFormat("vector< ::Type> v;");
144}
145
Alexander Kornienko15757312012-12-06 18:03:27 +0000146//===----------------------------------------------------------------------===//
147// Tests for control statements.
148//===----------------------------------------------------------------------===//
149
150TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000151 verifyFormat("if (true)\n f();\ng();");
152 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000153 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jasperdf3736a2013-01-16 15:44:34 +0000154
155 FormatStyle AllowsMergedIf = getGoogleStyle();
156 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
157 verifyFormat("if (a)\n"
158 " // comment\n"
159 " f();", AllowsMergedIf);
160
161 verifyFormat("if (a) // Can't merge this\n"
162 " f();\n", AllowsMergedIf);
163 verifyFormat("if (a) /* still don't merge */\n"
164 " f();", AllowsMergedIf);
165 verifyFormat("if (a) { // Never merge this\n"
166 " f();\n"
167 "}", AllowsMergedIf);
168 verifyFormat("if (a) { /* Never merge this */\n"
169 " f();\n"
170 "}", AllowsMergedIf);
171
172 AllowsMergedIf.ColumnLimit = 14;
173 verifyFormat("if (a) return;", AllowsMergedIf);
Daniel Jasperfd0ca972013-01-14 16:02:06 +0000174 verifyFormat("if (aaaaaaaaa)\n"
Daniel Jasperdf3736a2013-01-16 15:44:34 +0000175 " return;", AllowsMergedIf);
176
177 AllowsMergedIf.ColumnLimit = 13;
178 verifyFormat("if (a)\n return;", AllowsMergedIf);
Alexander Kornienko15757312012-12-06 18:03:27 +0000179}
180
181TEST_F(FormatTest, ParseIfElse) {
182 verifyFormat("if (true)\n"
183 " if (true)\n"
184 " if (true)\n"
185 " f();\n"
186 " else\n"
187 " g();\n"
188 " else\n"
189 " h();\n"
190 "else\n"
191 " i();");
192 verifyFormat("if (true)\n"
193 " if (true)\n"
194 " if (true) {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000195 " if (true)\n"
196 " f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000197 " } else {\n"
198 " g();\n"
199 " }\n"
200 " else\n"
201 " h();\n"
202 "else {\n"
203 " i();\n"
204 "}");
205}
206
207TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000208 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000209 verifyFormat("if (a)\n"
210 " f();\n"
211 "else if (b)\n"
212 " g();\n"
213 "else\n"
214 " h();");
215}
216
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217TEST_F(FormatTest, FormatsForLoop) {
218 verifyFormat(
219 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000220 " ++VeryVeryLongLoopVariable)\n"
221 " ;");
222 verifyFormat("for (;;)\n"
223 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000224 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000225 verifyFormat("for (;;) {\n"
226 " f();\n"
227 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000228
229 verifyFormat(
230 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
231 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000232 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000233
234 verifyFormat(
235 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000236 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000237}
238
239TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000240 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000241 verifyFormat("while (true)\n"
242 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000243 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000244 verifyFormat("while () {\n"
245 " f();\n"
246 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000247}
248
Alexander Kornienko15757312012-12-06 18:03:27 +0000249TEST_F(FormatTest, FormatsDoWhile) {
250 verifyFormat("do {\n"
251 " do_something();\n"
252 "} while (something());");
253 verifyFormat("do\n"
254 " do_something();\n"
255 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000256}
257
Alexander Kornienko15757312012-12-06 18:03:27 +0000258TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000259 verifyFormat("switch (x) {\n"
260 "case 1:\n"
261 " f();\n"
262 " break;\n"
263 "case kFoo:\n"
264 "case ns::kBar:\n"
265 "case kBaz:\n"
266 " break;\n"
267 "default:\n"
268 " g();\n"
269 " break;\n"
270 "}");
271 verifyFormat("switch (x) {\n"
272 "case 1: {\n"
273 " f();\n"
274 " break;\n"
275 "}\n"
276 "}");
277 verifyFormat("switch (test)\n"
278 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000279 verifyGoogleFormat("switch (x) {\n"
280 " case 1:\n"
281 " f();\n"
282 " break;\n"
283 " case kFoo:\n"
284 " case ns::kBar:\n"
285 " case kBaz:\n"
286 " break;\n"
287 " default:\n"
288 " g();\n"
289 " break;\n"
290 "}");
291 verifyGoogleFormat("switch (x) {\n"
292 " case 1: {\n"
293 " f();\n"
294 " break;\n"
295 " }\n"
296 "}");
297 verifyGoogleFormat("switch (test)\n"
298 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000299}
300
Alexander Kornienko15757312012-12-06 18:03:27 +0000301TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000302 verifyFormat("void f() {\n"
303 " some_code();\n"
304 "test_label:\n"
305 " some_other_code();\n"
306 " {\n"
307 " some_more_code();\n"
308 " another_label:\n"
309 " some_more_code();\n"
310 " }\n"
311 "}");
312 verifyFormat("some_code();\n"
313 "test_label:\n"
314 "some_other_code();");
315}
316
Alexander Kornienko15757312012-12-06 18:03:27 +0000317//===----------------------------------------------------------------------===//
318// Tests for comments.
319//===----------------------------------------------------------------------===//
320
321TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000322 verifyFormat("// line 1\n"
323 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000324 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000325
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000326 verifyFormat("void f() {\n"
327 " // Doesn't do anything\n"
328 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000329 verifyFormat("void f(int i, // some comment (probably for i)\n"
330 " int j, // some comment (probably for j)\n"
331 " int k); // some comment (probably for k)");
332 verifyFormat("void f(int i,\n"
333 " // some comment (probably for j)\n"
334 " int j,\n"
335 " // some comment (probably for k)\n"
336 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000337
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000338 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000339 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000340
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000341 verifyFormat("enum E {\n"
342 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000343 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000344 " VAL_B\n"
345 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000346
347 verifyFormat(
348 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000349 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000350 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
351 " // Comment inside a statement.\n"
352 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000353
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000354 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000355 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000356
357 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000358}
359
360TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000361 verifyFormat("f(/*test=*/ true);");
362}
363
Alexander Kornienko15757312012-12-06 18:03:27 +0000364//===----------------------------------------------------------------------===//
365// Tests for classes, namespaces, etc.
366//===----------------------------------------------------------------------===//
367
368TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000369 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000370}
371
372TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
373 verifyFormat("class A {\n"
374 "public:\n"
375 "protected:\n"
376 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000377 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000378 "};");
379 verifyGoogleFormat("class A {\n"
380 " public:\n"
381 " protected:\n"
382 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000383 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000384 "};");
385}
386
387TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000388 verifyFormat("class A : public B {};");
389 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000390}
391
Manuel Klimekde768542013-01-07 18:10:23 +0000392TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000393 verifyFormat("class A {} a, b;");
394 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000395 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000396}
397
Alexander Kornienko15757312012-12-06 18:03:27 +0000398TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000399 verifyFormat("enum {\n"
400 " Zero,\n"
401 " One = 1,\n"
402 " Two = One + 1,\n"
403 " Three = (One + Two),\n"
404 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
405 " Five = (One, Two, Three, Four, 5)\n"
406 "};");
407 verifyFormat("enum Enum {\n"
408 "};");
409 verifyFormat("enum {\n"
410 "};");
411}
412
Nico Weberefaddc02013-01-14 05:49:49 +0000413TEST_F(FormatTest, FormatsBitfields) {
414 verifyFormat("struct Bitfields {\n"
415 " unsigned sClass : 8;\n"
416 " unsigned ValueKind : 2;\n"
417 "};");
418}
419
Alexander Kornienko15757312012-12-06 18:03:27 +0000420TEST_F(FormatTest, FormatsNamespaces) {
421 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000422 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000423 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000424 "}");
425 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000426 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000427 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000428 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000429 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000430 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000431 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000432 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000433 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000434 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000435 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000436}
437
Nico Webera9ccdd12013-01-07 16:36:17 +0000438TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000439 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
440 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000441 verifyFormat("try {\n"
442 " throw a * b;\n"
443 "}\n"
444 "catch (int a) {\n"
445 " // Do nothing.\n"
446 "}\n"
447 "catch (...) {\n"
448 " exit(42);\n"
449 "}");
450
451 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000452 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000453 "catch (...) {\n"
454 " return 5;\n"
455 "}");
456 verifyFormat("class A {\n"
457 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000458 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000459 " catch (...) {\n"
460 " throw;\n"
461 " }\n"
462 "};\n");
463}
464
465TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000466 verifyFormat("@try {\n"
467 " f();\n"
468 "}\n"
469 "@catch (NSException e) {\n"
470 " @throw;\n"
471 "}\n"
472 "@finally {\n"
473 " exit(42);\n"
474 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000475}
476
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000477TEST_F(FormatTest, StaticInitializers) {
478 verifyFormat("static SomeClass SC = { 1, 'a' };");
479
480 // FIXME: Format like enums if the static initializer does not fit on a line.
481 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000482 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000483 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
484 "};");
485
486 verifyFormat(
487 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
488 " looooooooooooooooooooooooooooooooooongname,\n"
489 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000490}
491
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000492TEST_F(FormatTest, NestedStaticInitializers) {
493 verifyFormat("static A x = { { {} } };\n");
494 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000495 "static A x = { { { init1, init2, init3, init4 },\n"
496 " { init1, init2, init3, init4 } } };");
497
498 // FIXME: Fix this in general an verify that it works in LLVM style again.
499 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000500 "somes Status::global_reps[3] = {\n"
501 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
502 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
503 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
504 "};");
505 verifyFormat(
506 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
507 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
508 " } };");
509
510 // FIXME: We might at some point want to handle this similar to parameters
511 // lists, where we have an option to put each on a single line.
512 verifyFormat("struct {\n"
513 " unsigned bit;\n"
514 " const char *const name;\n"
515 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
516 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
517}
518
Manuel Klimeka080a182013-01-02 16:30:12 +0000519TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
520 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
521 " \\\n"
522 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
523}
524
Daniel Jasper71607512013-01-07 10:48:50 +0000525TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000526 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
527 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000528}
529
Manuel Klimeka080a182013-01-02 16:30:12 +0000530TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
531 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000532 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000533}
534
535TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
536 EXPECT_EQ("#line 42 \"test\"\n",
537 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000538 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000539 format("# \\\n define \\\n A \\\n B\n",
540 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000541}
542
543TEST_F(FormatTest, EndOfFileEndsPPDirective) {
544 EXPECT_EQ("#line 42 \"test\"",
545 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000546 EXPECT_EQ("#define A B",
547 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000548}
549
Manuel Klimek060143e2013-01-02 18:33:23 +0000550TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000551 // If the macro fits in one line, we still do not get the full
552 // line, as only the next line decides whether we need an escaped newline and
553 // thus use the last column.
554 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000555
Manuel Klimekd544c572013-01-07 09:24:17 +0000556 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
557 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000558 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000559
560 verifyFormat("#define A A\n#define A A");
561 verifyFormat("#define A(X) A\n#define A A");
562
563 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
564 verifyFormat("#define Something \\\n"
565 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000566}
567
568TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000569 EXPECT_EQ("// some comment\n"
570 "#include \"a.h\"\n"
571 "#define A(A,\\\n"
572 " B)\n"
573 "#include \"b.h\"\n"
574 "// some comment\n",
575 format(" // some comment\n"
576 " #include \"a.h\"\n"
577 "#define A(A,\\\n"
578 " B)\n"
579 " #include \"b.h\"\n"
580 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000581}
582
Manuel Klimekd4397b92013-01-04 23:34:14 +0000583TEST_F(FormatTest, LayoutSingleHash) {
584 EXPECT_EQ("#\na;", format("#\na;"));
585}
586
587TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
588 EXPECT_EQ("#define A \\\n"
589 " c; \\\n"
590 " e;\n"
591 "f;", format("#define A c; e;\n"
592 "f;", getLLVMStyleWithColumns(14)));
593}
594
595TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000596 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000597}
598
599TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000600 EXPECT_EQ("# define A\\\n b;",
601 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000602}
603
604TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000605 EXPECT_EQ("int x,\n"
606 "#define A\n"
607 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000608}
609
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000610TEST_F(FormatTest, HashInMacroDefinition) {
611 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
612 verifyFormat("#define A \\\n"
613 " { \\\n"
614 " f(#c);\\\n"
615 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000616
617 verifyFormat("#define A(X) \\\n"
618 " void function##X()", getLLVMStyleWithColumns(22));
619
620 verifyFormat("#define A(a, b, c) \\\n"
621 " void a##b##c()", getLLVMStyleWithColumns(22));
622
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000623 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000624}
625
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000626TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
627 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
628}
629
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000630TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000631 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000632}
633
Manuel Klimeka5342db2013-01-06 20:07:31 +0000634TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
635 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
636 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
637 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
638 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
639}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000640
Manuel Klimek95419382013-01-07 07:56:50 +0000641TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000642 EXPECT_EQ(
643 "#define A \\\n int i; \\\n int j;",
644 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000645}
646
Manuel Klimekd544c572013-01-07 09:24:17 +0000647TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
648 verifyFormat("#define A \\\n"
649 " int v( \\\n"
650 " a); \\\n"
651 " int i;", getLLVMStyleWithColumns(11));
652}
653
Manuel Klimeka080a182013-01-02 16:30:12 +0000654TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000655 EXPECT_EQ(
656 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
657 " \\\n"
658 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
659 "\n"
660 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
661 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
662 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
663 "\\\n"
664 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
665 " \n"
666 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
667 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000668}
669
Manuel Klimek526ed112013-01-09 15:25:02 +0000670TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
671 EXPECT_EQ("int\n"
672 "#define A\n"
673 " a;",
674 format("int\n#define A\na;"));
675 verifyFormat(
676 "functionCallTo(someOtherFunction(\n"
677 " withSomeParameters, whichInSequence,\n"
678 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000679 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000680 " withMoreParamters,\n"
681 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000682 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000683}
684
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000685TEST_F(FormatTest, LayoutBlockInsideParens) {
686 EXPECT_EQ("functionCall({\n"
687 " int i;\n"
688 "});", format(" functionCall ( {int i;} );"));
689}
690
691TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000692 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000693 "int i;", format(" SOME_MACRO {int i;} int i;"));
694}
695
696TEST_F(FormatTest, LayoutNestedBlocks) {
697 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
698 " struct s {\n"
699 " int i;\n"
700 " };\n"
701 " s kBitsToOs[] = { { 10 } };\n"
702 " for (int i = 0; i < 10; ++i)\n"
703 " return;\n"
704 "}");
705}
706
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000707TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
708 EXPECT_EQ("{}", format("{}"));
709}
710
Alexander Kornienko15757312012-12-06 18:03:27 +0000711//===----------------------------------------------------------------------===//
712// Line break tests.
713//===----------------------------------------------------------------------===//
714
715TEST_F(FormatTest, FormatsFunctionDefinition) {
716 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
717 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000718 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000719}
720
721TEST_F(FormatTest, FormatsAwesomeMethodCall) {
722 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000723 "SomeLongMethodName(SomeReallyLongMethod(\n"
724 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
725 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000726}
727
Daniel Jasper1321eb52012-12-18 21:05:13 +0000728TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000729 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000730 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
731 getLLVMStyleWithColumns(45));
732 verifyFormat("Constructor()\n"
733 " : Inttializer(FitsOnTheLine) {}",
734 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000735
736 verifyFormat(
737 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000738 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000739
740 verifyFormat(
741 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000742 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
743 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
744 verifyGoogleFormat(
745 "SomeClass::Constructor()\n"
746 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
747 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
748 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
749
750 verifyFormat(
751 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000752 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000753 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000754
755 verifyFormat("Constructor()\n"
756 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
757 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
758 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000759 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000760
761 // Here a line could be saved by splitting the second initializer onto two
762 // lines, but that is not desireable.
763 verifyFormat("Constructor()\n"
764 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
765 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000766 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000767
768 verifyGoogleFormat("MyClass::MyClass(int var)\n"
769 " : some_var_(var), // 4 space indent\n"
770 " some_other_var_(var + 1) { // lined up\n"
771 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000772
773 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000774 std::string input = "Constructor()\n"
775 " : aaaa(a,\n";
776 for (unsigned i = 0, e = 80; i != e; ++i) {
777 input += " a,\n";
778 }
779 input += " a) {}";
780 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000781}
782
Alexander Kornienko15757312012-12-06 18:03:27 +0000783TEST_F(FormatTest, BreaksAsHighAsPossible) {
784 verifyFormat(
785 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
786 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
787 " f();");
788}
789
Daniel Jasperbac016b2012-12-03 18:12:45 +0000790TEST_F(FormatTest, BreaksDesireably) {
791 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
792 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000793 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000794
795 verifyFormat(
796 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000797 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000798
799 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000802
803 verifyFormat(
804 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
806 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
807 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000808
809 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
810 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
811
Daniel Jasper723f0302013-01-02 14:40:02 +0000812 verifyFormat(
813 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
815
Daniel Jasper33182dd2012-12-05 14:57:28 +0000816 // This test case breaks on an incorrect memoization, i.e. an optimization not
817 // taking into account the StopAt value.
818 verifyFormat(
819 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000820 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
821 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
822 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000823
Daniel Jaspercd162382013-01-07 13:26:07 +0000824 verifyFormat("{\n {\n {\n"
825 " Annotation.SpaceRequiredBefore =\n"
826 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
827 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
828 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000829}
830
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000831TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
832 verifyGoogleFormat(
833 "aaaaaaaa(aaaaaaaaaaaaa,\n"
834 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
835 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
836 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
838 verifyGoogleFormat(
839 "aaaaaaaaaaaaaaa(aaaaaaaaa,\n"
840 " aaaaaaaaa,\n"
841 " aaaaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();");
842 verifyGoogleFormat(
843 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
844 " ddddddddddddddddddddddddddddd),\n"
845 " test);");
846
847 verifyGoogleFormat(
848 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
849 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
850 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
851 verifyGoogleFormat("a(\"a\"\n"
852 " \"a\",\n"
853 " a);");
854}
855
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000856TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
857 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
858 " GUARDED_BY(aaaaaaaaaaaaa);");
859}
860
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000861TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
862 verifyFormat(
863 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000864 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000865 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000866 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000867 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000868 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000869 verifyFormat(
870 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000871 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000872}
873
Daniel Jasper9cda8002013-01-07 13:08:40 +0000874TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
875 verifyFormat(
876 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
877 " SI->getAlignment(),\n"
878 " SI->getPointerAddressSpaceee());\n");
879 verifyFormat(
880 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
881 " Line.Tokens.front().Tok.getLocation(),\n"
882 " Line.Tokens.back().Tok.getLocation());");
883}
884
Daniel Jaspercf225b62012-12-24 13:43:52 +0000885TEST_F(FormatTest, AlignsAfterAssignments) {
886 verifyFormat(
887 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000888 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000889 verifyFormat(
890 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000891 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000892 verifyFormat(
893 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000894 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000895 verifyFormat(
896 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000897 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000898 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000899 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
900 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
901 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000902}
903
904TEST_F(FormatTest, AlignsAfterReturn) {
905 verifyFormat(
906 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
907 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
908 verifyFormat(
909 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
910 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
911}
912
Daniel Jasper9c837d02013-01-09 07:06:56 +0000913TEST_F(FormatTest, BreaksConditionalExpressions) {
914 verifyFormat(
915 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
916 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
918 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
919 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
920}
921
Nico Weber7d37b8b2013-01-12 01:28:06 +0000922TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
923 verifyFormat("arr[foo ? bar : baz];");
924 verifyFormat("f()[foo ? bar : baz];");
925 verifyFormat("(a + b)[foo ? bar : baz];");
926 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
927}
928
Daniel Jasperbac016b2012-12-03 18:12:45 +0000929TEST_F(FormatTest, AlignsStringLiterals) {
930 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
931 " \"short literal\");");
932 verifyFormat(
933 "looooooooooooooooooooooooongFunction(\n"
934 " \"short literal\"\n"
935 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
936}
937
Alexander Kornienko15757312012-12-06 18:03:27 +0000938TEST_F(FormatTest, AlignsPipes) {
939 verifyFormat(
940 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
941 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
942 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
943 verifyFormat(
944 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
945 " << aaaaaaaaaaaaaaaaaaaa;");
946 verifyFormat(
947 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
948 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
949 verifyFormat(
950 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
951 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
952 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
953 verifyFormat(
954 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
955 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
956 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
957}
958
Daniel Jasperbac016b2012-12-03 18:12:45 +0000959TEST_F(FormatTest, UnderstandsEquals) {
960 verifyFormat(
961 "aaaaaaaaaaaaaaaaa =\n"
962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
963 verifyFormat(
964 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000966 verifyFormat(
967 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000968 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000969 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000971
Daniel Jasper9cda8002013-01-07 13:08:40 +0000972 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000973 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000974 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000975 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000976}
977
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000978TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000979 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
980 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000981
Daniel Jasper1321eb52012-12-18 21:05:13 +0000982 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
983 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000984
985 verifyFormat(
986 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
987 " Parameter2);");
988
989 verifyFormat(
990 "ShortObject->shortFunction(\n"
991 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
992 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
993
994 verifyFormat("loooooooooooooongFunction(\n"
995 " LoooooooooooooongObject->looooooooooooooooongFunction());");
996
997 verifyFormat(
998 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
999 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1000
Daniel Jasper46a46a22013-01-07 07:13:20 +00001001 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001002 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001003 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001004 verifyFormat(
1005 "aaaaaaaaaaa->aaaaaaaaa(\n"
1006 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1007 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001008}
1009
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001010TEST_F(FormatTest, WrapsTemplateDeclarations) {
1011 verifyFormat("template <typename T>\n"
1012 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1013 verifyFormat(
1014 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1015 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
1016 verifyFormat(
1017 "template <typename T>\n"
1018 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1019 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001020 verifyFormat(
1021 "template <typename T>\n"
1022 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1023 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1024 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001025 verifyFormat("template <typename T>\n"
1026 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1027 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001028 verifyFormat(
1029 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1030 " typename T4 = char>\n"
1031 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001032}
1033
Daniel Jasperbac016b2012-12-03 18:12:45 +00001034TEST_F(FormatTest, UnderstandsTemplateParameters) {
1035 verifyFormat("A<int> a;");
1036 verifyFormat("A<A<A<int> > > a;");
1037 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1038 verifyFormat("bool x = a < 1 || 2 > a;");
1039 verifyFormat("bool x = 5 < f<int>();");
1040 verifyFormat("bool x = f<int>() > 5;");
1041 verifyFormat("bool x = 5 < a<int>::x;");
1042 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1043 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1044
1045 verifyGoogleFormat("A<A<int>> a;");
1046 verifyGoogleFormat("A<A<A<int>>> a;");
1047 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1048
1049 verifyFormat("test >> a >> b;");
1050 verifyFormat("test << a >> b;");
1051
1052 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001053 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001054}
1055
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001056TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001057 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001058 verifyFormat("f(-1, -2, -3);");
1059 verifyFormat("a[-1] = 5;");
1060 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001061 verifyFormat("if (i == -1) {}");
1062 verifyFormat("if (i != -1) {}");
1063 verifyFormat("if (i > -1) {}");
1064 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001065 verifyFormat("++(a->f());");
1066 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001067 verifyFormat("(a->f())++;");
1068 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001069 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001070
1071 verifyFormat("a-- > b;");
1072 verifyFormat("b ? -a : c;");
1073 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001074 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001075 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001076 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001077
1078 verifyFormat("return -1;");
1079 verifyFormat("switch (a) {\n"
1080 "case -1:\n"
1081 " break;\n"
1082 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001083
1084 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1085 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001086
1087 verifyFormat("int a = /* confusing comment */ -1;");
1088 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1089 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001090}
1091
1092TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001093 verifyFormat("bool operator<();");
1094 verifyFormat("bool operator>();");
1095 verifyFormat("bool operator=();");
1096 verifyFormat("bool operator==();");
1097 verifyFormat("bool operator!=();");
1098 verifyFormat("int operator+();");
1099 verifyFormat("int operator++();");
1100 verifyFormat("bool operator();");
1101 verifyFormat("bool operator()();");
1102 verifyFormat("bool operator[]();");
1103 verifyFormat("operator bool();");
1104 verifyFormat("operator SomeType<int>();");
1105 verifyFormat("void *operator new(std::size_t size);");
1106 verifyFormat("void *operator new[](std::size_t size);");
1107 verifyFormat("void operator delete(void *ptr);");
1108 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001109}
1110
Daniel Jasper088dab52013-01-11 16:09:04 +00001111TEST_F(FormatTest, UnderstandsNewAndDelete) {
1112 verifyFormat("A *a = new A;");
1113 verifyFormat("A *a = new (placement) A;");
1114 verifyFormat("delete a;");
1115 verifyFormat("delete (A *)a;");
1116}
1117
Daniel Jasper5d334402013-01-02 08:57:10 +00001118TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001119 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001120 verifyFormat("f(a, *a);");
1121 verifyFormat("f(*a);");
1122 verifyFormat("int a = b * 10;");
1123 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001124 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001125 verifyFormat("int a += b * c;");
1126 verifyFormat("int a -= b * c;");
1127 verifyFormat("int a *= b * c;");
1128 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001129 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001130 verifyFormat("int a = *b * c;");
1131 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001132 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001133 verifyFormat("return 10 * b;");
1134 verifyFormat("return *b * *c;");
1135 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001136 verifyFormat("f(b ? *c : *d);");
1137 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001138 verifyFormat("*b = a;");
1139 verifyFormat("a * ~b;");
1140 verifyFormat("a * !b;");
1141 verifyFormat("a * +b;");
1142 verifyFormat("a * -b;");
1143 verifyFormat("a * ++b;");
1144 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001145 verifyFormat("a[4] * b;");
1146 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001147 verifyFormat("a * [self dostuff];");
1148 verifyFormat("a * (a + b);");
1149 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001150 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001151
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001152 verifyFormat("InvalidRegions[*R] = 0;");
1153
Daniel Jasper8b39c662012-12-10 18:59:13 +00001154 verifyFormat("A<int *> a;");
1155 verifyFormat("A<int **> a;");
1156 verifyFormat("A<int *, int *> a;");
1157 verifyFormat("A<int **, int **> a;");
1158
Daniel Jasper2db356d2013-01-08 20:03:18 +00001159 verifyFormat(
1160 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1162
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001163 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001164 verifyGoogleFormat("A<int*> a;");
1165 verifyGoogleFormat("A<int**> a;");
1166 verifyGoogleFormat("A<int*, int*> a;");
1167 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001168 verifyGoogleFormat("f(b ? *c : *d);");
1169 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001170
1171 verifyFormat("a = *(x + y);");
1172 verifyFormat("a = &(x + y);");
1173 verifyFormat("*(x + y).call();");
1174 verifyFormat("&(x + y)->call();");
1175 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001176
1177 verifyFormat("f(b * /* confusing comment */ ++c);");
1178 verifyFormat(
1179 "int *MyValues = {\n"
1180 " *A, // Operator detection might be confused by the '{'\n"
1181 " *BB // Operator detection might be confused by previous comment\n"
1182 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001183}
1184
Daniel Jasper4981bd02013-01-13 08:01:36 +00001185TEST_F(FormatTest, FormatsCasts) {
1186 verifyFormat("Type *A = static_cast<Type *>(P);");
1187 verifyFormat("Type *A = (Type *)P;");
1188 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1189 verifyFormat("int a = (int)(2.0f);");
1190
1191 // FIXME: These also need to be identified.
1192 verifyFormat("int a = (int) 2.0f;");
1193 verifyFormat("int a = (int) * b;");
1194
1195 // These are not casts.
1196 verifyFormat("void f(int *) {}");
1197 verifyFormat("void f(int *);");
1198 verifyFormat("void f(int *) = 0;");
1199 verifyFormat("void f(SmallVector<int>) {}");
1200 verifyFormat("void f(SmallVector<int>);");
1201 verifyFormat("void f(SmallVector<int>) = 0;");
1202}
1203
Daniel Jasper46ef8522013-01-10 13:08:12 +00001204TEST_F(FormatTest, FormatsFunctionTypes) {
1205 // FIXME: Determine the cases that need a space after the return type and fix.
1206 verifyFormat("A<bool()> a;");
1207 verifyFormat("A<SomeType()> a;");
1208 verifyFormat("A<void(*)(int, std::string)> a;");
1209
1210 verifyFormat("int(*func)(void *);");
1211}
1212
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001213TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001214 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001215 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001216 verifyFormat(
1217 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1218 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001219 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001220}
1221
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001222TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1223 verifyFormat("(a)->b();");
1224 verifyFormat("--a;");
1225}
1226
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001227TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001228 verifyFormat("#include <string>\n"
1229 "#include <a/b/c.h>\n"
1230 "#include \"a/b/string\"\n"
1231 "#include \"string.h\"\n"
1232 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001233 "#include <a-a>\n"
1234 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001235
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001236 verifyFormat("#import <string>");
1237 verifyFormat("#import <a/b/c.h>");
1238 verifyFormat("#import \"a/b/string\"");
1239 verifyFormat("#import \"string.h\"");
1240 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001241}
1242
Alexander Kornienko15757312012-12-06 18:03:27 +00001243//===----------------------------------------------------------------------===//
1244// Error recovery tests.
1245//===----------------------------------------------------------------------===//
1246
Daniel Jasper700e7102013-01-10 09:26:47 +00001247TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001248 verifyFormat("void f() { return; }\n42");
1249 verifyFormat("void f() {\n"
1250 " if (0)\n"
1251 " return;\n"
1252 "}\n"
1253 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001254 verifyFormat("void f() { return }\n42");
1255 verifyFormat("void f() {\n"
1256 " if (0)\n"
1257 " return\n"
1258 "}\n"
1259 "42");
1260}
1261
1262TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1263 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1264 EXPECT_EQ("void f() {\n"
1265 " if (a)\n"
1266 " return\n"
1267 "}", format("void f ( ) { if ( a ) return }"));
1268 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1269 EXPECT_EQ("namespace N {\n"
1270 "void f() {}\n"
1271 "void g()\n"
1272 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001273}
1274
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001275TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1276 verifyFormat("int aaaaaaaa =\n"
1277 " // Overly long comment\n"
1278 " b;", getLLVMStyleWithColumns(20));
1279 verifyFormat("function(\n"
1280 " ShortArgument,\n"
1281 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1282}
1283
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001284TEST_F(FormatTest, IncorrectAccessSpecifier) {
1285 verifyFormat("public:");
1286 verifyFormat("class A {\n"
1287 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001288 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001289 "};");
1290 verifyFormat("public\n"
1291 "int qwerty;");
1292 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001293 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001294 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001295 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001296 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001297 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001298}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001299
Alexander Kornienko393b0082012-12-04 15:40:36 +00001300TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1301 verifyFormat("{");
1302}
1303
1304TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001305 verifyFormat("do {}");
1306 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001307 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001308 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001309 "wheeee(fun);");
1310 verifyFormat("do {\n"
1311 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001312 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001313}
1314
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001315TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001316 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001317 verifyFormat("switch {\n foo;\n foo();\n}");
1318 verifyFormat("for {\n foo;\n foo();\n}");
1319 verifyFormat("while {\n foo;\n foo();\n}");
1320 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001321}
1322
Daniel Jasper1f42f112013-01-04 18:52:56 +00001323TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1324 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001325 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001326}
1327
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001328TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001329 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1330 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1331 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1332 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001333
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001334 EXPECT_EQ("{\n"
1335 " {\n"
1336 " breakme(\n"
1337 " qwe);\n"
1338 "}\n", format("{\n"
1339 " {\n"
1340 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001341 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001342}
1343
Manuel Klimek2851c162013-01-10 14:36:46 +00001344TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1345 verifyFormat(
1346 "int x = {\n"
1347 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001348 " b(alongervariable)\n"
1349 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001350}
1351
1352TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1353 verifyFormat(
1354 "Aaa({\n"
1355 " int i;\n"
1356 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1357 " ccccccccccccccccc));");
1358}
1359
Manuel Klimek517e8942013-01-11 17:54:10 +00001360TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1361 verifyFormat("void f() { return 42; }");
1362 verifyFormat("void f() {\n"
1363 " // Comment\n"
1364 "}");
1365 verifyFormat("{\n"
1366 "#error {\n"
1367 " int a;\n"
1368 "}");
1369 verifyFormat("{\n"
1370 " int a;\n"
1371 "#error {\n"
1372 "}");
1373}
1374
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001375TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1376 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001377 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001378 verifyFormat("class foo a = { bar };\nint n;");
1379 verifyFormat("union foo a = { bar };\nint n;");
1380
1381 // Elaborate types inside function definitions.
1382 verifyFormat("struct foo f() {}\nint n;");
1383 verifyFormat("class foo f() {}\nint n;");
1384 verifyFormat("union foo f() {}\nint n;");
1385
1386 // Templates.
1387 verifyFormat("template <class X> void f() {}\nint n;");
1388 verifyFormat("template <struct X> void f() {}\nint n;");
1389 verifyFormat("template <union X> void f() {}\nint n;");
1390
1391 // Actual definitions...
1392 verifyFormat("struct {} n;");
1393 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1394 verifyFormat("union Z {\n int n;\n} x;");
1395 verifyFormat("class MACRO Z {} n;");
1396 verifyFormat("class MACRO(X) Z {} n;");
1397 verifyFormat("class __attribute__(X) Z {} n;");
1398 verifyFormat("class __declspec(X) Z {} n;");
1399
1400 // Elaborate types where incorrectly parsing the structural element would
1401 // break the indent.
1402 verifyFormat("if (true)\n"
1403 " class X x;\n"
1404 "else\n"
1405 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001406}
1407
Manuel Klimek407a31a2013-01-15 15:50:27 +00001408TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1409 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1410 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1411 EXPECT_EQ("#error 1", format(" # error 1"));
1412 EXPECT_EQ("#warning 1", format(" # warning 1"));
1413}
1414
Manuel Klimek517e8942013-01-11 17:54:10 +00001415// FIXME: This breaks the order of the unwrapped lines:
1416// TEST_F(FormatTest, OrderUnwrappedLines) {
1417// verifyFormat("{\n"
1418// " bool a; //\n"
1419// "#error {\n"
1420// " int a;\n"
1421// "}");
1422// }
1423
Nico Webercf4a79c2013-01-08 17:56:31 +00001424//===----------------------------------------------------------------------===//
1425// Objective-C tests.
1426//===----------------------------------------------------------------------===//
1427
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001428TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1429 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1430 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1431 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001432 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001433 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1434 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1435 format("-(NSInteger)Method3:(id)anObject;"));
1436 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1437 format("-(NSInteger)Method4:(id)anObject;"));
1438 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1439 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1440 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1441 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001442 EXPECT_EQ(
1443 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1444 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001445
1446 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001447 EXPECT_EQ(
1448 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1449 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1450 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1451 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1452 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1453 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1454 format(
1455 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1456 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1457 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1458 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1459 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1460 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001461
1462 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001463 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001464 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1465 // protocol lists (but not for template classes):
1466 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001467
1468 verifyFormat("- (int(*)())foo:(int(*)())f;");
1469 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1470
1471 // If there's no return type (very rare in practice!), LLVM and Google style
1472 // agree.
1473 verifyFormat("- foo:(int)f;");
1474 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001475}
1476
Daniel Jasper886568d2013-01-09 08:36:49 +00001477TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001478 verifyFormat("int (^Block)(int, int);");
1479 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001480}
1481
Nico Weber27d13672013-01-09 20:25:35 +00001482TEST_F(FormatTest, FormatObjCInterface) {
1483 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001484 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001485 "@public\n"
1486 " int field1;\n"
1487 "@protected\n"
1488 " int field2;\n"
1489 "@private\n"
1490 " int field3;\n"
1491 "@package\n"
1492 " int field4;\n"
1493 "}\n"
1494 "+ (id)init;\n"
1495 "@end");
1496
Nico Weber27d13672013-01-09 20:25:35 +00001497 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1498 " @public\n"
1499 " int field1;\n"
1500 " @protected\n"
1501 " int field2;\n"
1502 " @private\n"
1503 " int field3;\n"
1504 " @package\n"
1505 " int field4;\n"
1506 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001507 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001508 "@end");
1509
1510 verifyFormat("@interface Foo\n"
1511 "+ (id)init;\n"
1512 "// Look, a comment!\n"
1513 "- (int)answerWith:(int)i;\n"
1514 "@end");
1515
1516 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001517 "@end\n"
1518 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001519 "@end");
1520
1521 verifyFormat("@interface Foo : Bar\n"
1522 "+ (id)init;\n"
1523 "@end");
1524
Nico Weber5f500df2013-01-10 20:12:55 +00001525 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001526 "+ (id)init;\n"
1527 "@end");
1528
Nico Weber5f500df2013-01-10 20:12:55 +00001529 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001530 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001531 "@end");
1532
Nico Webered91bba2013-01-10 19:19:14 +00001533 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001534 "+ (id)init;\n"
1535 "@end");
1536
Nico Webered91bba2013-01-10 19:19:14 +00001537 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001538 "+ (id)init;\n"
1539 "@end");
1540
Nico Weber5f500df2013-01-10 20:12:55 +00001541 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001542 "+ (id)init;\n"
1543 "@end");
1544
Nico Weber5f500df2013-01-10 20:12:55 +00001545 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001546 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001547 "@end");
1548
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001549 verifyFormat("@interface Foo {\n"
1550 " int _i;\n"
1551 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001552 "+ (id)init;\n"
1553 "@end");
1554
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001555 verifyFormat("@interface Foo : Bar {\n"
1556 " int _i;\n"
1557 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001558 "+ (id)init;\n"
1559 "@end");
1560
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001561 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1562 " int _i;\n"
1563 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001564 "+ (id)init;\n"
1565 "@end");
1566
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001567 verifyFormat("@interface Foo (HackStuff) {\n"
1568 " int _i;\n"
1569 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001570 "+ (id)init;\n"
1571 "@end");
1572
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001573 verifyFormat("@interface Foo () {\n"
1574 " int _i;\n"
1575 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001576 "+ (id)init;\n"
1577 "@end");
1578
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001579 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1580 " int _i;\n"
1581 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001582 "+ (id)init;\n"
1583 "@end");
1584}
1585
Nico Weber50767d82013-01-09 23:25:37 +00001586TEST_F(FormatTest, FormatObjCImplementation) {
1587 verifyFormat("@implementation Foo : NSObject {\n"
1588 "@public\n"
1589 " int field1;\n"
1590 "@protected\n"
1591 " int field2;\n"
1592 "@private\n"
1593 " int field3;\n"
1594 "@package\n"
1595 " int field4;\n"
1596 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001597 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001598 "@end");
1599
1600 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1601 " @public\n"
1602 " int field1;\n"
1603 " @protected\n"
1604 " int field2;\n"
1605 " @private\n"
1606 " int field3;\n"
1607 " @package\n"
1608 " int field4;\n"
1609 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001610 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001611 "@end");
1612
1613 verifyFormat("@implementation Foo\n"
1614 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001615 " if (true)\n"
1616 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001617 "}\n"
1618 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001619 "- (int)answerWith:(int)i {\n"
1620 " return i;\n"
1621 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001622 "+ (int)answerWith:(int)i {\n"
1623 " return i;\n"
1624 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001625 "@end");
1626
1627 verifyFormat("@implementation Foo\n"
1628 "@end\n"
1629 "@implementation Bar\n"
1630 "@end");
1631
1632 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001633 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001634 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001635 "@end");
1636
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001637 verifyFormat("@implementation Foo {\n"
1638 " int _i;\n"
1639 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001640 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001641 "@end");
1642
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001643 verifyFormat("@implementation Foo : Bar {\n"
1644 " int _i;\n"
1645 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001646 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001647 "@end");
1648
Nico Webered91bba2013-01-10 19:19:14 +00001649 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001650 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001651 "@end");
1652}
1653
Nico Weber1abe6ea2013-01-09 21:15:03 +00001654TEST_F(FormatTest, FormatObjCProtocol) {
1655 verifyFormat("@protocol Foo\n"
1656 "@property(weak) id delegate;\n"
1657 "- (NSUInteger)numberOfThings;\n"
1658 "@end");
1659
Nico Weber5f500df2013-01-10 20:12:55 +00001660 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001661 "- (NSUInteger)numberOfThings;\n"
1662 "@end");
1663
Nico Weber5f500df2013-01-10 20:12:55 +00001664 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001665 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001666 "@end");
1667
Nico Weber1abe6ea2013-01-09 21:15:03 +00001668 verifyFormat("@protocol Foo;\n"
1669 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001670
1671 verifyFormat("@protocol Foo\n"
1672 "@end\n"
1673 "@protocol Bar\n"
1674 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001675
1676 verifyFormat("@protocol myProtocol\n"
1677 "- (void)mandatoryWithInt:(int)i;\n"
1678 "@optional\n"
1679 "- (void)optional;\n"
1680 "@required\n"
1681 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001682 "@optional\n"
1683 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001684 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001685}
1686
Nico Weberbcfdd262013-01-12 06:18:40 +00001687TEST_F(FormatTest, FormatObjCMethodExpr) {
1688 verifyFormat("[foo bar:baz];");
1689 verifyFormat("return [foo bar:baz];");
1690 verifyFormat("f([foo bar:baz]);");
1691 verifyFormat("f(2, [foo bar:baz]);");
1692 verifyFormat("f(2, a ? b : c);");
1693 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1694
1695 verifyFormat("[foo bar:baz], [foo bar:baz];");
1696 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1697 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1698 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1699 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1700 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1701 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1702 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1703 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1704 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1705 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1706 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1707 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1708 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1709 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1710 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1711 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1712 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1713 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1714 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1715 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1716 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1717 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1718 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1719 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1720 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1721 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1722 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1723 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1724 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1725 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1726 // Whew!
1727
1728 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1729 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1730 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1731 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1732 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001733 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001734 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001735
Nico Weberbcfdd262013-01-12 06:18:40 +00001736 verifyFormat("arr[[self indexForFoo:a]];");
1737 verifyFormat("throw [self errorFor:a];");
1738 verifyFormat("@throw [self errorFor:a];");
1739
Nico Webere8ccc812013-01-12 22:48:47 +00001740 // This tests that the formatter doesn't break after "backing" but before ":",
1741 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001742 verifyFormat(
1743 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001744 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1745 " backing:NSBackingStoreBuffered defer:YES]))");
1746
Nico Webere8ccc812013-01-12 22:48:47 +00001747 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1748 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001749
1750}
1751
Nico Weber581f5572013-01-07 15:56:25 +00001752TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001753 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001754 verifyFormat("@catch");
1755 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001756 verifyFormat("@compatibility_alias");
1757 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001758 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001759 verifyFormat("@encode");
1760 verifyFormat("@end");
1761 verifyFormat("@finally");
1762 verifyFormat("@implementation");
1763 verifyFormat("@import");
1764 verifyFormat("@interface");
1765 verifyFormat("@optional");
1766 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001767 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001768 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001769 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001770 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001771 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001772 verifyFormat("@required");
1773 verifyFormat("@selector");
1774 verifyFormat("@synchronized");
1775 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001776 verifyFormat("@throw");
1777 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001778
Nico Webercb4d6902013-01-08 19:40:21 +00001779 verifyFormat("@\"String\"");
1780 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001781 verifyFormat("@+4.8");
1782 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001783 verifyFormat("@1LL");
1784 verifyFormat("@.5");
1785 verifyFormat("@'c'");
1786 verifyFormat("@true");
1787 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001788 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001789 verifyFormat("@[");
1790 verifyFormat("@{");
1791
Nico Weber581f5572013-01-07 15:56:25 +00001792 EXPECT_EQ("@interface", format("@ interface"));
1793
1794 // The precise formatting of this doesn't matter, nobody writes code like
1795 // this.
1796 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001797}
1798
Nico Weberc31689a2013-01-08 19:15:23 +00001799TEST_F(FormatTest, ObjCSnippets) {
1800 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001801 verifyFormat("@autoreleasepool {\n"
1802 " foo();\n"
1803 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001804 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001805 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001806 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001807 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001808 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001809 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001810 verifyFormat("@synchronized(self) {\n"
1811 " f();\n"
1812 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001813
Nico Weber70848232013-01-10 21:30:42 +00001814 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1815 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1816
Nico Webercf4a79c2013-01-08 17:56:31 +00001817 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001818 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1819 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001820}
1821
Daniel Jaspercd162382013-01-07 13:26:07 +00001822} // end namespace tooling
1823} // end namespace clang