blob: 6dd7cad13800ff100515f800902d26822b7f6de8 [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;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001170 verifyGoogleFormat("Type* t = **x;");
1171 verifyGoogleFormat("Type* t = *++*x;");
1172 verifyGoogleFormat("*++*x;");
1173 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1174 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001175
1176 verifyFormat("a = *(x + y);");
1177 verifyFormat("a = &(x + y);");
1178 verifyFormat("*(x + y).call();");
1179 verifyFormat("&(x + y)->call();");
1180 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001181
1182 verifyFormat("f(b * /* confusing comment */ ++c);");
1183 verifyFormat(
1184 "int *MyValues = {\n"
1185 " *A, // Operator detection might be confused by the '{'\n"
1186 " *BB // Operator detection might be confused by previous comment\n"
1187 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001188}
1189
Daniel Jasper4981bd02013-01-13 08:01:36 +00001190TEST_F(FormatTest, FormatsCasts) {
1191 verifyFormat("Type *A = static_cast<Type *>(P);");
1192 verifyFormat("Type *A = (Type *)P;");
1193 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1194 verifyFormat("int a = (int)(2.0f);");
1195
1196 // FIXME: These also need to be identified.
1197 verifyFormat("int a = (int) 2.0f;");
1198 verifyFormat("int a = (int) * b;");
1199
1200 // These are not casts.
1201 verifyFormat("void f(int *) {}");
1202 verifyFormat("void f(int *);");
1203 verifyFormat("void f(int *) = 0;");
1204 verifyFormat("void f(SmallVector<int>) {}");
1205 verifyFormat("void f(SmallVector<int>);");
1206 verifyFormat("void f(SmallVector<int>) = 0;");
1207}
1208
Daniel Jasper46ef8522013-01-10 13:08:12 +00001209TEST_F(FormatTest, FormatsFunctionTypes) {
1210 // FIXME: Determine the cases that need a space after the return type and fix.
1211 verifyFormat("A<bool()> a;");
1212 verifyFormat("A<SomeType()> a;");
1213 verifyFormat("A<void(*)(int, std::string)> a;");
1214
1215 verifyFormat("int(*func)(void *);");
1216}
1217
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001218TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001219 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001220 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001221 verifyFormat(
1222 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1223 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001224 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001225}
1226
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001227TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1228 verifyFormat("(a)->b();");
1229 verifyFormat("--a;");
1230}
1231
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001232TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001233 verifyFormat("#include <string>\n"
1234 "#include <a/b/c.h>\n"
1235 "#include \"a/b/string\"\n"
1236 "#include \"string.h\"\n"
1237 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001238 "#include <a-a>\n"
1239 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001240
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001241 verifyFormat("#import <string>");
1242 verifyFormat("#import <a/b/c.h>");
1243 verifyFormat("#import \"a/b/string\"");
1244 verifyFormat("#import \"string.h\"");
1245 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001246}
1247
Alexander Kornienko15757312012-12-06 18:03:27 +00001248//===----------------------------------------------------------------------===//
1249// Error recovery tests.
1250//===----------------------------------------------------------------------===//
1251
Daniel Jasper700e7102013-01-10 09:26:47 +00001252TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001253 verifyFormat("void f() { return; }\n42");
1254 verifyFormat("void f() {\n"
1255 " if (0)\n"
1256 " return;\n"
1257 "}\n"
1258 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001259 verifyFormat("void f() { return }\n42");
1260 verifyFormat("void f() {\n"
1261 " if (0)\n"
1262 " return\n"
1263 "}\n"
1264 "42");
1265}
1266
1267TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1268 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1269 EXPECT_EQ("void f() {\n"
1270 " if (a)\n"
1271 " return\n"
1272 "}", format("void f ( ) { if ( a ) return }"));
1273 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1274 EXPECT_EQ("namespace N {\n"
1275 "void f() {}\n"
1276 "void g()\n"
1277 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001278}
1279
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001280TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1281 verifyFormat("int aaaaaaaa =\n"
1282 " // Overly long comment\n"
1283 " b;", getLLVMStyleWithColumns(20));
1284 verifyFormat("function(\n"
1285 " ShortArgument,\n"
1286 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1287}
1288
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001289TEST_F(FormatTest, IncorrectAccessSpecifier) {
1290 verifyFormat("public:");
1291 verifyFormat("class A {\n"
1292 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001293 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001294 "};");
1295 verifyFormat("public\n"
1296 "int qwerty;");
1297 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001298 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001299 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001300 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001301 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001302 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001303}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001304
Alexander Kornienko393b0082012-12-04 15:40:36 +00001305TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1306 verifyFormat("{");
1307}
1308
1309TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001310 verifyFormat("do {}");
1311 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001312 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001313 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001314 "wheeee(fun);");
1315 verifyFormat("do {\n"
1316 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001317 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001318}
1319
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001320TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001321 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001322 verifyFormat("switch {\n foo;\n foo();\n}");
1323 verifyFormat("for {\n foo;\n foo();\n}");
1324 verifyFormat("while {\n foo;\n foo();\n}");
1325 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001326}
1327
Daniel Jasper1f42f112013-01-04 18:52:56 +00001328TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1329 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001330 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001331}
1332
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001333TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001334 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1335 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1336 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1337 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001338
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001339 EXPECT_EQ("{\n"
1340 " {\n"
1341 " breakme(\n"
1342 " qwe);\n"
1343 "}\n", format("{\n"
1344 " {\n"
1345 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001346 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001347}
1348
Manuel Klimek2851c162013-01-10 14:36:46 +00001349TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1350 verifyFormat(
1351 "int x = {\n"
1352 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001353 " b(alongervariable)\n"
1354 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001355}
1356
1357TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1358 verifyFormat(
1359 "Aaa({\n"
1360 " int i;\n"
1361 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1362 " ccccccccccccccccc));");
1363}
1364
Manuel Klimek517e8942013-01-11 17:54:10 +00001365TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1366 verifyFormat("void f() { return 42; }");
1367 verifyFormat("void f() {\n"
1368 " // Comment\n"
1369 "}");
1370 verifyFormat("{\n"
1371 "#error {\n"
1372 " int a;\n"
1373 "}");
1374 verifyFormat("{\n"
1375 " int a;\n"
1376 "#error {\n"
1377 "}");
1378}
1379
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001380TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1381 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001382 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001383 verifyFormat("class foo a = { bar };\nint n;");
1384 verifyFormat("union foo a = { bar };\nint n;");
1385
1386 // Elaborate types inside function definitions.
1387 verifyFormat("struct foo f() {}\nint n;");
1388 verifyFormat("class foo f() {}\nint n;");
1389 verifyFormat("union foo f() {}\nint n;");
1390
1391 // Templates.
1392 verifyFormat("template <class X> void f() {}\nint n;");
1393 verifyFormat("template <struct X> void f() {}\nint n;");
1394 verifyFormat("template <union X> void f() {}\nint n;");
1395
1396 // Actual definitions...
1397 verifyFormat("struct {} n;");
1398 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1399 verifyFormat("union Z {\n int n;\n} x;");
1400 verifyFormat("class MACRO Z {} n;");
1401 verifyFormat("class MACRO(X) Z {} n;");
1402 verifyFormat("class __attribute__(X) Z {} n;");
1403 verifyFormat("class __declspec(X) Z {} n;");
1404
1405 // Elaborate types where incorrectly parsing the structural element would
1406 // break the indent.
1407 verifyFormat("if (true)\n"
1408 " class X x;\n"
1409 "else\n"
1410 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001411}
1412
Manuel Klimek407a31a2013-01-15 15:50:27 +00001413TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1414 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1415 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1416 EXPECT_EQ("#error 1", format(" # error 1"));
1417 EXPECT_EQ("#warning 1", format(" # warning 1"));
1418}
1419
Manuel Klimek517e8942013-01-11 17:54:10 +00001420// FIXME: This breaks the order of the unwrapped lines:
1421// TEST_F(FormatTest, OrderUnwrappedLines) {
1422// verifyFormat("{\n"
1423// " bool a; //\n"
1424// "#error {\n"
1425// " int a;\n"
1426// "}");
1427// }
1428
Nico Webercf4a79c2013-01-08 17:56:31 +00001429//===----------------------------------------------------------------------===//
1430// Objective-C tests.
1431//===----------------------------------------------------------------------===//
1432
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001433TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1434 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1435 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1436 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001437 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001438 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1439 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1440 format("-(NSInteger)Method3:(id)anObject;"));
1441 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1442 format("-(NSInteger)Method4:(id)anObject;"));
1443 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1444 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1445 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1446 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001447 EXPECT_EQ(
1448 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1449 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001450
1451 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001452 EXPECT_EQ(
1453 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1454 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1455 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1456 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1457 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1458 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1459 format(
1460 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1461 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1462 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1463 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1464 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1465 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001466
1467 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001468 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001469 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1470 // protocol lists (but not for template classes):
1471 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001472
1473 verifyFormat("- (int(*)())foo:(int(*)())f;");
1474 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1475
1476 // If there's no return type (very rare in practice!), LLVM and Google style
1477 // agree.
1478 verifyFormat("- foo:(int)f;");
1479 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001480}
1481
Daniel Jasper886568d2013-01-09 08:36:49 +00001482TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001483 verifyFormat("int (^Block)(int, int);");
1484 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001485}
1486
Nico Weber27d13672013-01-09 20:25:35 +00001487TEST_F(FormatTest, FormatObjCInterface) {
1488 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001489 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001490 "@public\n"
1491 " int field1;\n"
1492 "@protected\n"
1493 " int field2;\n"
1494 "@private\n"
1495 " int field3;\n"
1496 "@package\n"
1497 " int field4;\n"
1498 "}\n"
1499 "+ (id)init;\n"
1500 "@end");
1501
Nico Weber27d13672013-01-09 20:25:35 +00001502 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1503 " @public\n"
1504 " int field1;\n"
1505 " @protected\n"
1506 " int field2;\n"
1507 " @private\n"
1508 " int field3;\n"
1509 " @package\n"
1510 " int field4;\n"
1511 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001512 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001513 "@end");
1514
1515 verifyFormat("@interface Foo\n"
1516 "+ (id)init;\n"
1517 "// Look, a comment!\n"
1518 "- (int)answerWith:(int)i;\n"
1519 "@end");
1520
1521 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001522 "@end\n"
1523 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001524 "@end");
1525
1526 verifyFormat("@interface Foo : Bar\n"
1527 "+ (id)init;\n"
1528 "@end");
1529
Nico Weber5f500df2013-01-10 20:12:55 +00001530 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001531 "+ (id)init;\n"
1532 "@end");
1533
Nico Weber5f500df2013-01-10 20:12:55 +00001534 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001535 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001536 "@end");
1537
Nico Webered91bba2013-01-10 19:19:14 +00001538 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001539 "+ (id)init;\n"
1540 "@end");
1541
Nico Webered91bba2013-01-10 19:19:14 +00001542 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001543 "+ (id)init;\n"
1544 "@end");
1545
Nico Weber5f500df2013-01-10 20:12:55 +00001546 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001547 "+ (id)init;\n"
1548 "@end");
1549
Nico Weber5f500df2013-01-10 20:12:55 +00001550 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001551 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001552 "@end");
1553
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001554 verifyFormat("@interface Foo {\n"
1555 " int _i;\n"
1556 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001557 "+ (id)init;\n"
1558 "@end");
1559
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001560 verifyFormat("@interface Foo : Bar {\n"
1561 " int _i;\n"
1562 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001563 "+ (id)init;\n"
1564 "@end");
1565
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001566 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1567 " int _i;\n"
1568 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001569 "+ (id)init;\n"
1570 "@end");
1571
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001572 verifyFormat("@interface Foo (HackStuff) {\n"
1573 " int _i;\n"
1574 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001575 "+ (id)init;\n"
1576 "@end");
1577
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001578 verifyFormat("@interface Foo () {\n"
1579 " int _i;\n"
1580 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001581 "+ (id)init;\n"
1582 "@end");
1583
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001584 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1585 " int _i;\n"
1586 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001587 "+ (id)init;\n"
1588 "@end");
1589}
1590
Nico Weber50767d82013-01-09 23:25:37 +00001591TEST_F(FormatTest, FormatObjCImplementation) {
1592 verifyFormat("@implementation Foo : NSObject {\n"
1593 "@public\n"
1594 " int field1;\n"
1595 "@protected\n"
1596 " int field2;\n"
1597 "@private\n"
1598 " int field3;\n"
1599 "@package\n"
1600 " int field4;\n"
1601 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001602 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001603 "@end");
1604
1605 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1606 " @public\n"
1607 " int field1;\n"
1608 " @protected\n"
1609 " int field2;\n"
1610 " @private\n"
1611 " int field3;\n"
1612 " @package\n"
1613 " int field4;\n"
1614 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001615 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001616 "@end");
1617
1618 verifyFormat("@implementation Foo\n"
1619 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001620 " if (true)\n"
1621 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001622 "}\n"
1623 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001624 "- (int)answerWith:(int)i {\n"
1625 " return i;\n"
1626 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001627 "+ (int)answerWith:(int)i {\n"
1628 " return i;\n"
1629 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001630 "@end");
1631
1632 verifyFormat("@implementation Foo\n"
1633 "@end\n"
1634 "@implementation Bar\n"
1635 "@end");
1636
1637 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001638 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001639 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001640 "@end");
1641
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001642 verifyFormat("@implementation Foo {\n"
1643 " int _i;\n"
1644 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001645 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001646 "@end");
1647
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001648 verifyFormat("@implementation Foo : Bar {\n"
1649 " int _i;\n"
1650 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001651 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001652 "@end");
1653
Nico Webered91bba2013-01-10 19:19:14 +00001654 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001655 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001656 "@end");
1657}
1658
Nico Weber1abe6ea2013-01-09 21:15:03 +00001659TEST_F(FormatTest, FormatObjCProtocol) {
1660 verifyFormat("@protocol Foo\n"
1661 "@property(weak) id delegate;\n"
1662 "- (NSUInteger)numberOfThings;\n"
1663 "@end");
1664
Nico Weber5f500df2013-01-10 20:12:55 +00001665 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001666 "- (NSUInteger)numberOfThings;\n"
1667 "@end");
1668
Nico Weber5f500df2013-01-10 20:12:55 +00001669 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001670 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001671 "@end");
1672
Nico Weber1abe6ea2013-01-09 21:15:03 +00001673 verifyFormat("@protocol Foo;\n"
1674 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001675
1676 verifyFormat("@protocol Foo\n"
1677 "@end\n"
1678 "@protocol Bar\n"
1679 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001680
1681 verifyFormat("@protocol myProtocol\n"
1682 "- (void)mandatoryWithInt:(int)i;\n"
1683 "@optional\n"
1684 "- (void)optional;\n"
1685 "@required\n"
1686 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001687 "@optional\n"
1688 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001689 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001690}
1691
Nico Weberbcfdd262013-01-12 06:18:40 +00001692TEST_F(FormatTest, FormatObjCMethodExpr) {
1693 verifyFormat("[foo bar:baz];");
1694 verifyFormat("return [foo bar:baz];");
1695 verifyFormat("f([foo bar:baz]);");
1696 verifyFormat("f(2, [foo bar:baz]);");
1697 verifyFormat("f(2, a ? b : c);");
1698 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1699
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];");
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] : [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 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1727 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1728 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1729 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1730 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1731 // Whew!
1732
1733 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1734 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1735 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1736 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1737 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001738 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001739 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001740
Nico Weberbcfdd262013-01-12 06:18:40 +00001741 verifyFormat("arr[[self indexForFoo:a]];");
1742 verifyFormat("throw [self errorFor:a];");
1743 verifyFormat("@throw [self errorFor:a];");
1744
Nico Webere8ccc812013-01-12 22:48:47 +00001745 // This tests that the formatter doesn't break after "backing" but before ":",
1746 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001747 verifyFormat(
1748 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001749 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1750 " backing:NSBackingStoreBuffered defer:YES]))");
1751
Nico Webere8ccc812013-01-12 22:48:47 +00001752 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1753 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001754
1755}
1756
Nico Weber581f5572013-01-07 15:56:25 +00001757TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001758 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001759 verifyFormat("@catch");
1760 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001761 verifyFormat("@compatibility_alias");
1762 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001763 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001764 verifyFormat("@encode");
1765 verifyFormat("@end");
1766 verifyFormat("@finally");
1767 verifyFormat("@implementation");
1768 verifyFormat("@import");
1769 verifyFormat("@interface");
1770 verifyFormat("@optional");
1771 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001772 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001773 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001774 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001775 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001776 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001777 verifyFormat("@required");
1778 verifyFormat("@selector");
1779 verifyFormat("@synchronized");
1780 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001781 verifyFormat("@throw");
1782 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001783
Nico Webercb4d6902013-01-08 19:40:21 +00001784 verifyFormat("@\"String\"");
1785 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001786 verifyFormat("@+4.8");
1787 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001788 verifyFormat("@1LL");
1789 verifyFormat("@.5");
1790 verifyFormat("@'c'");
1791 verifyFormat("@true");
1792 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001793 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001794 verifyFormat("@[");
1795 verifyFormat("@{");
1796
Nico Weber581f5572013-01-07 15:56:25 +00001797 EXPECT_EQ("@interface", format("@ interface"));
1798
1799 // The precise formatting of this doesn't matter, nobody writes code like
1800 // this.
1801 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001802}
1803
Nico Weberc31689a2013-01-08 19:15:23 +00001804TEST_F(FormatTest, ObjCSnippets) {
1805 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001806 verifyFormat("@autoreleasepool {\n"
1807 " foo();\n"
1808 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001809 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001810 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001811 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001812 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001813 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001814 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001815 verifyFormat("@synchronized(self) {\n"
1816 " f();\n"
1817 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001818
Nico Weber70848232013-01-10 21:30:42 +00001819 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1820 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1821
Nico Webercf4a79c2013-01-08 17:56:31 +00001822 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001823 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1824 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001825}
1826
Daniel Jaspercd162382013-01-07 13:26:07 +00001827} // end namespace tooling
1828} // end namespace clang