blob: bd0fcbcf2cca5c4f4badb0d5c4758053182258f9 [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) {}");
Daniel Jasper16618242013-01-16 17:00:50 +0000749 verifyGoogleFormat(
750 "SomeClass::Constructor()\n"
751 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
752 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
753 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000754
755 verifyFormat(
756 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000757 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000758 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000759
760 verifyFormat("Constructor()\n"
761 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
762 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
763 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000764 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000765
766 // Here a line could be saved by splitting the second initializer onto two
767 // lines, but that is not desireable.
768 verifyFormat("Constructor()\n"
769 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
770 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000771 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000772
773 verifyGoogleFormat("MyClass::MyClass(int var)\n"
774 " : some_var_(var), // 4 space indent\n"
775 " some_other_var_(var + 1) { // lined up\n"
776 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000777
778 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000779 std::string input = "Constructor()\n"
780 " : aaaa(a,\n";
781 for (unsigned i = 0, e = 80; i != e; ++i) {
782 input += " a,\n";
783 }
784 input += " a) {}";
785 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000786}
787
Alexander Kornienko15757312012-12-06 18:03:27 +0000788TEST_F(FormatTest, BreaksAsHighAsPossible) {
789 verifyFormat(
790 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
791 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
792 " f();");
793}
794
Daniel Jasperbac016b2012-12-03 18:12:45 +0000795TEST_F(FormatTest, BreaksDesireably) {
796 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
797 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000798 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000799
800 verifyFormat(
801 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000803
804 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000807
808 verifyFormat(
809 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
810 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
811 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
812 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000813
814 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
815 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
816
Daniel Jasper723f0302013-01-02 14:40:02 +0000817 verifyFormat(
818 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
820
Daniel Jasper33182dd2012-12-05 14:57:28 +0000821 // This test case breaks on an incorrect memoization, i.e. an optimization not
822 // taking into account the StopAt value.
823 verifyFormat(
824 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000825 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
826 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
827 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000828
Daniel Jaspercd162382013-01-07 13:26:07 +0000829 verifyFormat("{\n {\n {\n"
830 " Annotation.SpaceRequiredBefore =\n"
831 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
832 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
833 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000834}
835
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000836TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
837 verifyGoogleFormat(
838 "aaaaaaaa(aaaaaaaaaaaaa,\n"
839 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
841 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
843 verifyGoogleFormat(
844 "aaaaaaaaaaaaaaa(aaaaaaaaa,\n"
845 " aaaaaaaaa,\n"
846 " aaaaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();");
847 verifyGoogleFormat(
848 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
849 " ddddddddddddddddddddddddddddd),\n"
850 " test);");
851
852 verifyGoogleFormat(
853 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
854 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
855 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
856 verifyGoogleFormat("a(\"a\"\n"
857 " \"a\",\n"
858 " a);");
859}
860
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000861TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
862 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
863 " GUARDED_BY(aaaaaaaaaaaaa);");
864}
865
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000866TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
867 verifyFormat(
868 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000869 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000870 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000871 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000872 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000873 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000874 verifyFormat(
875 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000876 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000877}
878
Daniel Jasper9cda8002013-01-07 13:08:40 +0000879TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
880 verifyFormat(
881 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
882 " SI->getAlignment(),\n"
883 " SI->getPointerAddressSpaceee());\n");
884 verifyFormat(
885 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
886 " Line.Tokens.front().Tok.getLocation(),\n"
887 " Line.Tokens.back().Tok.getLocation());");
888}
889
Daniel Jaspercf225b62012-12-24 13:43:52 +0000890TEST_F(FormatTest, AlignsAfterAssignments) {
891 verifyFormat(
892 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000893 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000894 verifyFormat(
895 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000896 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000897 verifyFormat(
898 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000899 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000900 verifyFormat(
901 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000902 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000903 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000904 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
905 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
906 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000907}
908
909TEST_F(FormatTest, AlignsAfterReturn) {
910 verifyFormat(
911 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
912 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
913 verifyFormat(
914 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
915 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
916}
917
Daniel Jasper9c837d02013-01-09 07:06:56 +0000918TEST_F(FormatTest, BreaksConditionalExpressions) {
919 verifyFormat(
920 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
921 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
923 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
924 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +0000925 verifyFormat(
926 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
927 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +0000928}
929
Nico Weber7d37b8b2013-01-12 01:28:06 +0000930TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
931 verifyFormat("arr[foo ? bar : baz];");
932 verifyFormat("f()[foo ? bar : baz];");
933 verifyFormat("(a + b)[foo ? bar : baz];");
934 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
935}
936
Daniel Jasperbac016b2012-12-03 18:12:45 +0000937TEST_F(FormatTest, AlignsStringLiterals) {
938 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
939 " \"short literal\");");
940 verifyFormat(
941 "looooooooooooooooooooooooongFunction(\n"
942 " \"short literal\"\n"
943 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
944}
945
Alexander Kornienko15757312012-12-06 18:03:27 +0000946TEST_F(FormatTest, AlignsPipes) {
947 verifyFormat(
948 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
949 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
950 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
951 verifyFormat(
952 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
953 " << aaaaaaaaaaaaaaaaaaaa;");
954 verifyFormat(
955 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
956 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
957 verifyFormat(
958 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
959 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
960 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
961 verifyFormat(
962 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
963 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
964 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
965}
966
Daniel Jasperbac016b2012-12-03 18:12:45 +0000967TEST_F(FormatTest, UnderstandsEquals) {
968 verifyFormat(
969 "aaaaaaaaaaaaaaaaa =\n"
970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
971 verifyFormat(
972 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000974 verifyFormat(
975 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000976 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000977 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000978 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000979
Daniel Jasper9cda8002013-01-07 13:08:40 +0000980 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000981 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000982 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000983 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000984}
985
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000986TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000987 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
988 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000989
Daniel Jasper1321eb52012-12-18 21:05:13 +0000990 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
991 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000992
993 verifyFormat(
994 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
995 " Parameter2);");
996
997 verifyFormat(
998 "ShortObject->shortFunction(\n"
999 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1000 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1001
1002 verifyFormat("loooooooooooooongFunction(\n"
1003 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1004
1005 verifyFormat(
1006 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1007 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1008
Daniel Jasper46a46a22013-01-07 07:13:20 +00001009 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001010 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001011 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001012 verifyFormat(
1013 "aaaaaaaaaaa->aaaaaaaaa(\n"
1014 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1015 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001016}
1017
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001018TEST_F(FormatTest, WrapsTemplateDeclarations) {
1019 verifyFormat("template <typename T>\n"
1020 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1021 verifyFormat(
1022 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1023 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
1024 verifyFormat(
1025 "template <typename T>\n"
1026 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1027 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001028 verifyFormat(
1029 "template <typename T>\n"
1030 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1031 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001033 verifyFormat("template <typename T>\n"
1034 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1035 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001036 verifyFormat(
1037 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1038 " typename T4 = char>\n"
1039 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001040}
1041
Daniel Jasperbac016b2012-12-03 18:12:45 +00001042TEST_F(FormatTest, UnderstandsTemplateParameters) {
1043 verifyFormat("A<int> a;");
1044 verifyFormat("A<A<A<int> > > a;");
1045 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1046 verifyFormat("bool x = a < 1 || 2 > a;");
1047 verifyFormat("bool x = 5 < f<int>();");
1048 verifyFormat("bool x = f<int>() > 5;");
1049 verifyFormat("bool x = 5 < a<int>::x;");
1050 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1051 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1052
1053 verifyGoogleFormat("A<A<int>> a;");
1054 verifyGoogleFormat("A<A<A<int>>> a;");
1055 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1056
1057 verifyFormat("test >> a >> b;");
1058 verifyFormat("test << a >> b;");
1059
1060 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001061 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001062}
1063
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001064TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001065 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001066 verifyFormat("f(-1, -2, -3);");
1067 verifyFormat("a[-1] = 5;");
1068 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001069 verifyFormat("if (i == -1) {}");
1070 verifyFormat("if (i != -1) {}");
1071 verifyFormat("if (i > -1) {}");
1072 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001073 verifyFormat("++(a->f());");
1074 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001075 verifyFormat("(a->f())++;");
1076 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001077 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001078
1079 verifyFormat("a-- > b;");
1080 verifyFormat("b ? -a : c;");
1081 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001082 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001083 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001084 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001085
1086 verifyFormat("return -1;");
1087 verifyFormat("switch (a) {\n"
1088 "case -1:\n"
1089 " break;\n"
1090 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001091
1092 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1093 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001094
1095 verifyFormat("int a = /* confusing comment */ -1;");
1096 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1097 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001098}
1099
1100TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001101 verifyFormat("bool operator<();");
1102 verifyFormat("bool operator>();");
1103 verifyFormat("bool operator=();");
1104 verifyFormat("bool operator==();");
1105 verifyFormat("bool operator!=();");
1106 verifyFormat("int operator+();");
1107 verifyFormat("int operator++();");
1108 verifyFormat("bool operator();");
1109 verifyFormat("bool operator()();");
1110 verifyFormat("bool operator[]();");
1111 verifyFormat("operator bool();");
1112 verifyFormat("operator SomeType<int>();");
1113 verifyFormat("void *operator new(std::size_t size);");
1114 verifyFormat("void *operator new[](std::size_t size);");
1115 verifyFormat("void operator delete(void *ptr);");
1116 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001117}
1118
Daniel Jasper088dab52013-01-11 16:09:04 +00001119TEST_F(FormatTest, UnderstandsNewAndDelete) {
1120 verifyFormat("A *a = new A;");
1121 verifyFormat("A *a = new (placement) A;");
1122 verifyFormat("delete a;");
1123 verifyFormat("delete (A *)a;");
1124}
1125
Daniel Jasper5d334402013-01-02 08:57:10 +00001126TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001127 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001128 verifyFormat("f(a, *a);");
1129 verifyFormat("f(*a);");
1130 verifyFormat("int a = b * 10;");
1131 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001132 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001133 verifyFormat("int a += b * c;");
1134 verifyFormat("int a -= b * c;");
1135 verifyFormat("int a *= b * c;");
1136 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001137 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001138 verifyFormat("int a = *b * c;");
1139 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001140 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001141 verifyFormat("return 10 * b;");
1142 verifyFormat("return *b * *c;");
1143 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001144 verifyFormat("f(b ? *c : *d);");
1145 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001146 verifyFormat("*b = a;");
1147 verifyFormat("a * ~b;");
1148 verifyFormat("a * !b;");
1149 verifyFormat("a * +b;");
1150 verifyFormat("a * -b;");
1151 verifyFormat("a * ++b;");
1152 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001153 verifyFormat("a[4] * b;");
1154 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001155 verifyFormat("a * [self dostuff];");
1156 verifyFormat("a * (a + b);");
1157 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001158 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001159
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001160 verifyFormat("InvalidRegions[*R] = 0;");
1161
Daniel Jasper8b39c662012-12-10 18:59:13 +00001162 verifyFormat("A<int *> a;");
1163 verifyFormat("A<int **> a;");
1164 verifyFormat("A<int *, int *> a;");
1165 verifyFormat("A<int **, int **> a;");
1166
Daniel Jasper2db356d2013-01-08 20:03:18 +00001167 verifyFormat(
1168 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1169 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1170
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001171 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001172 verifyGoogleFormat("A<int*> a;");
1173 verifyGoogleFormat("A<int**> a;");
1174 verifyGoogleFormat("A<int*, int*> a;");
1175 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001176 verifyGoogleFormat("f(b ? *c : *d);");
1177 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001178 verifyGoogleFormat("Type* t = **x;");
1179 verifyGoogleFormat("Type* t = *++*x;");
1180 verifyGoogleFormat("*++*x;");
1181 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1182 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001183
1184 verifyFormat("a = *(x + y);");
1185 verifyFormat("a = &(x + y);");
1186 verifyFormat("*(x + y).call();");
1187 verifyFormat("&(x + y)->call();");
1188 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001189
1190 verifyFormat("f(b * /* confusing comment */ ++c);");
1191 verifyFormat(
1192 "int *MyValues = {\n"
1193 " *A, // Operator detection might be confused by the '{'\n"
1194 " *BB // Operator detection might be confused by previous comment\n"
1195 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001196}
1197
Daniel Jasper4981bd02013-01-13 08:01:36 +00001198TEST_F(FormatTest, FormatsCasts) {
1199 verifyFormat("Type *A = static_cast<Type *>(P);");
1200 verifyFormat("Type *A = (Type *)P;");
1201 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1202 verifyFormat("int a = (int)(2.0f);");
1203
1204 // FIXME: These also need to be identified.
1205 verifyFormat("int a = (int) 2.0f;");
1206 verifyFormat("int a = (int) * b;");
1207
1208 // These are not casts.
1209 verifyFormat("void f(int *) {}");
1210 verifyFormat("void f(int *);");
1211 verifyFormat("void f(int *) = 0;");
1212 verifyFormat("void f(SmallVector<int>) {}");
1213 verifyFormat("void f(SmallVector<int>);");
1214 verifyFormat("void f(SmallVector<int>) = 0;");
1215}
1216
Daniel Jasper46ef8522013-01-10 13:08:12 +00001217TEST_F(FormatTest, FormatsFunctionTypes) {
1218 // FIXME: Determine the cases that need a space after the return type and fix.
1219 verifyFormat("A<bool()> a;");
1220 verifyFormat("A<SomeType()> a;");
1221 verifyFormat("A<void(*)(int, std::string)> a;");
1222
1223 verifyFormat("int(*func)(void *);");
1224}
1225
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001226TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001227 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001228 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001229 verifyFormat(
1230 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1231 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001232 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001233}
1234
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001235TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1236 verifyFormat("(a)->b();");
1237 verifyFormat("--a;");
1238}
1239
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001240TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001241 verifyFormat("#include <string>\n"
1242 "#include <a/b/c.h>\n"
1243 "#include \"a/b/string\"\n"
1244 "#include \"string.h\"\n"
1245 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001246 "#include <a-a>\n"
1247 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001248
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001249 verifyFormat("#import <string>");
1250 verifyFormat("#import <a/b/c.h>");
1251 verifyFormat("#import \"a/b/string\"");
1252 verifyFormat("#import \"string.h\"");
1253 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001254}
1255
Alexander Kornienko15757312012-12-06 18:03:27 +00001256//===----------------------------------------------------------------------===//
1257// Error recovery tests.
1258//===----------------------------------------------------------------------===//
1259
Daniel Jasper700e7102013-01-10 09:26:47 +00001260TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001261 verifyFormat("void f() { return; }\n42");
1262 verifyFormat("void f() {\n"
1263 " if (0)\n"
1264 " return;\n"
1265 "}\n"
1266 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001267 verifyFormat("void f() { return }\n42");
1268 verifyFormat("void f() {\n"
1269 " if (0)\n"
1270 " return\n"
1271 "}\n"
1272 "42");
1273}
1274
1275TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1276 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1277 EXPECT_EQ("void f() {\n"
1278 " if (a)\n"
1279 " return\n"
1280 "}", format("void f ( ) { if ( a ) return }"));
1281 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1282 EXPECT_EQ("namespace N {\n"
1283 "void f() {}\n"
1284 "void g()\n"
1285 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001286}
1287
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001288TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1289 verifyFormat("int aaaaaaaa =\n"
1290 " // Overly long comment\n"
1291 " b;", getLLVMStyleWithColumns(20));
1292 verifyFormat("function(\n"
1293 " ShortArgument,\n"
1294 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1295}
1296
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001297TEST_F(FormatTest, IncorrectAccessSpecifier) {
1298 verifyFormat("public:");
1299 verifyFormat("class A {\n"
1300 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001301 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001302 "};");
1303 verifyFormat("public\n"
1304 "int qwerty;");
1305 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001306 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001307 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001308 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001309 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001310 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001311}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001312
Alexander Kornienko393b0082012-12-04 15:40:36 +00001313TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1314 verifyFormat("{");
1315}
1316
1317TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001318 verifyFormat("do {}");
1319 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001320 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001321 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001322 "wheeee(fun);");
1323 verifyFormat("do {\n"
1324 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001325 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001326}
1327
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001328TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001329 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001330 verifyFormat("switch {\n foo;\n foo();\n}");
1331 verifyFormat("for {\n foo;\n foo();\n}");
1332 verifyFormat("while {\n foo;\n foo();\n}");
1333 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001334}
1335
Daniel Jasper1f42f112013-01-04 18:52:56 +00001336TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1337 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001338 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001339}
1340
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001341TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001342 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1343 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1344 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1345 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001346
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001347 EXPECT_EQ("{\n"
1348 " {\n"
1349 " breakme(\n"
1350 " qwe);\n"
1351 "}\n", format("{\n"
1352 " {\n"
1353 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001354 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001355}
1356
Manuel Klimek2851c162013-01-10 14:36:46 +00001357TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1358 verifyFormat(
1359 "int x = {\n"
1360 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001361 " b(alongervariable)\n"
1362 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001363}
1364
1365TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1366 verifyFormat(
1367 "Aaa({\n"
1368 " int i;\n"
1369 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1370 " ccccccccccccccccc));");
1371}
1372
Manuel Klimek517e8942013-01-11 17:54:10 +00001373TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1374 verifyFormat("void f() { return 42; }");
1375 verifyFormat("void f() {\n"
1376 " // Comment\n"
1377 "}");
1378 verifyFormat("{\n"
1379 "#error {\n"
1380 " int a;\n"
1381 "}");
1382 verifyFormat("{\n"
1383 " int a;\n"
1384 "#error {\n"
1385 "}");
1386}
1387
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001388TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1389 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001390 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001391 verifyFormat("class foo a = { bar };\nint n;");
1392 verifyFormat("union foo a = { bar };\nint n;");
1393
1394 // Elaborate types inside function definitions.
1395 verifyFormat("struct foo f() {}\nint n;");
1396 verifyFormat("class foo f() {}\nint n;");
1397 verifyFormat("union foo f() {}\nint n;");
1398
1399 // Templates.
1400 verifyFormat("template <class X> void f() {}\nint n;");
1401 verifyFormat("template <struct X> void f() {}\nint n;");
1402 verifyFormat("template <union X> void f() {}\nint n;");
1403
1404 // Actual definitions...
1405 verifyFormat("struct {} n;");
1406 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1407 verifyFormat("union Z {\n int n;\n} x;");
1408 verifyFormat("class MACRO Z {} n;");
1409 verifyFormat("class MACRO(X) Z {} n;");
1410 verifyFormat("class __attribute__(X) Z {} n;");
1411 verifyFormat("class __declspec(X) Z {} n;");
1412
1413 // Elaborate types where incorrectly parsing the structural element would
1414 // break the indent.
1415 verifyFormat("if (true)\n"
1416 " class X x;\n"
1417 "else\n"
1418 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001419}
1420
Manuel Klimek407a31a2013-01-15 15:50:27 +00001421TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1422 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1423 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1424 EXPECT_EQ("#error 1", format(" # error 1"));
1425 EXPECT_EQ("#warning 1", format(" # warning 1"));
1426}
1427
Manuel Klimek517e8942013-01-11 17:54:10 +00001428// FIXME: This breaks the order of the unwrapped lines:
1429// TEST_F(FormatTest, OrderUnwrappedLines) {
1430// verifyFormat("{\n"
1431// " bool a; //\n"
1432// "#error {\n"
1433// " int a;\n"
1434// "}");
1435// }
1436
Nico Webercf4a79c2013-01-08 17:56:31 +00001437//===----------------------------------------------------------------------===//
1438// Objective-C tests.
1439//===----------------------------------------------------------------------===//
1440
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001441TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1442 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1443 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1444 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001445 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001446 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1447 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1448 format("-(NSInteger)Method3:(id)anObject;"));
1449 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1450 format("-(NSInteger)Method4:(id)anObject;"));
1451 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1452 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1453 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1454 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001455 EXPECT_EQ(
1456 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1457 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001458
1459 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001460 EXPECT_EQ(
1461 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1462 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1463 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1464 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1465 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1466 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1467 format(
1468 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1469 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1470 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1471 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1472 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1473 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001474
1475 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001476 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001477 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1478 // protocol lists (but not for template classes):
1479 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001480
1481 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001482 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001483
1484 // If there's no return type (very rare in practice!), LLVM and Google style
1485 // agree.
1486 verifyFormat("- foo:(int)f;");
1487 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001488}
1489
Daniel Jasper886568d2013-01-09 08:36:49 +00001490TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001491 verifyFormat("int (^Block)(int, int);");
1492 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001493}
1494
Nico Weber27d13672013-01-09 20:25:35 +00001495TEST_F(FormatTest, FormatObjCInterface) {
1496 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001497 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001498 "@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"
1507 "+ (id)init;\n"
1508 "@end");
1509
Nico Weber27d13672013-01-09 20:25:35 +00001510 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1511 " @public\n"
1512 " int field1;\n"
1513 " @protected\n"
1514 " int field2;\n"
1515 " @private\n"
1516 " int field3;\n"
1517 " @package\n"
1518 " int field4;\n"
1519 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001520 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001521 "@end");
1522
1523 verifyFormat("@interface Foo\n"
1524 "+ (id)init;\n"
1525 "// Look, a comment!\n"
1526 "- (int)answerWith:(int)i;\n"
1527 "@end");
1528
1529 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001530 "@end\n"
1531 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001532 "@end");
1533
1534 verifyFormat("@interface Foo : Bar\n"
1535 "+ (id)init;\n"
1536 "@end");
1537
Nico Weber5f500df2013-01-10 20:12:55 +00001538 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001539 "+ (id)init;\n"
1540 "@end");
1541
Nico Weber5f500df2013-01-10 20:12:55 +00001542 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001543 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001544 "@end");
1545
Nico Webered91bba2013-01-10 19:19:14 +00001546 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001547 "+ (id)init;\n"
1548 "@end");
1549
Nico Webered91bba2013-01-10 19:19:14 +00001550 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001551 "+ (id)init;\n"
1552 "@end");
1553
Nico Weber5f500df2013-01-10 20:12:55 +00001554 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001555 "+ (id)init;\n"
1556 "@end");
1557
Nico Weber5f500df2013-01-10 20:12:55 +00001558 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001559 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001560 "@end");
1561
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001562 verifyFormat("@interface Foo {\n"
1563 " int _i;\n"
1564 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001565 "+ (id)init;\n"
1566 "@end");
1567
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001568 verifyFormat("@interface Foo : Bar {\n"
1569 " int _i;\n"
1570 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001571 "+ (id)init;\n"
1572 "@end");
1573
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001574 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1575 " int _i;\n"
1576 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001577 "+ (id)init;\n"
1578 "@end");
1579
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001580 verifyFormat("@interface Foo (HackStuff) {\n"
1581 " int _i;\n"
1582 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001583 "+ (id)init;\n"
1584 "@end");
1585
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001586 verifyFormat("@interface Foo () {\n"
1587 " int _i;\n"
1588 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001589 "+ (id)init;\n"
1590 "@end");
1591
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001592 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1593 " int _i;\n"
1594 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001595 "+ (id)init;\n"
1596 "@end");
1597}
1598
Nico Weber50767d82013-01-09 23:25:37 +00001599TEST_F(FormatTest, FormatObjCImplementation) {
1600 verifyFormat("@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"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001610 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001611 "@end");
1612
1613 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1614 " @public\n"
1615 " int field1;\n"
1616 " @protected\n"
1617 " int field2;\n"
1618 " @private\n"
1619 " int field3;\n"
1620 " @package\n"
1621 " int field4;\n"
1622 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001623 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001624 "@end");
1625
1626 verifyFormat("@implementation Foo\n"
1627 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001628 " if (true)\n"
1629 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001630 "}\n"
1631 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001632 "- (int)answerWith:(int)i {\n"
1633 " return i;\n"
1634 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001635 "+ (int)answerWith:(int)i {\n"
1636 " return i;\n"
1637 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001638 "@end");
1639
1640 verifyFormat("@implementation Foo\n"
1641 "@end\n"
1642 "@implementation Bar\n"
1643 "@end");
1644
1645 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001646 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001647 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001648 "@end");
1649
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001650 verifyFormat("@implementation Foo {\n"
1651 " int _i;\n"
1652 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001653 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001654 "@end");
1655
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001656 verifyFormat("@implementation Foo : Bar {\n"
1657 " int _i;\n"
1658 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001659 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001660 "@end");
1661
Nico Webered91bba2013-01-10 19:19:14 +00001662 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001663 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001664 "@end");
1665}
1666
Nico Weber1abe6ea2013-01-09 21:15:03 +00001667TEST_F(FormatTest, FormatObjCProtocol) {
1668 verifyFormat("@protocol Foo\n"
1669 "@property(weak) id delegate;\n"
1670 "- (NSUInteger)numberOfThings;\n"
1671 "@end");
1672
Nico Weber5f500df2013-01-10 20:12:55 +00001673 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001674 "- (NSUInteger)numberOfThings;\n"
1675 "@end");
1676
Nico Weber5f500df2013-01-10 20:12:55 +00001677 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001678 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001679 "@end");
1680
Nico Weber1abe6ea2013-01-09 21:15:03 +00001681 verifyFormat("@protocol Foo;\n"
1682 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001683
1684 verifyFormat("@protocol Foo\n"
1685 "@end\n"
1686 "@protocol Bar\n"
1687 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001688
1689 verifyFormat("@protocol myProtocol\n"
1690 "- (void)mandatoryWithInt:(int)i;\n"
1691 "@optional\n"
1692 "- (void)optional;\n"
1693 "@required\n"
1694 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001695 "@optional\n"
1696 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001697 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001698}
1699
Nico Weberbcfdd262013-01-12 06:18:40 +00001700TEST_F(FormatTest, FormatObjCMethodExpr) {
1701 verifyFormat("[foo bar:baz];");
1702 verifyFormat("return [foo bar:baz];");
1703 verifyFormat("f([foo bar:baz]);");
1704 verifyFormat("f(2, [foo bar:baz]);");
1705 verifyFormat("f(2, a ? b : c);");
1706 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1707
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] : [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 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1732 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1733 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1734 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1735 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1736 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1737 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1738 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1739 // Whew!
1740
1741 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1742 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1743 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1744 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1745 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001746 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001747 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001748
Nico Weberbcfdd262013-01-12 06:18:40 +00001749 verifyFormat("arr[[self indexForFoo:a]];");
1750 verifyFormat("throw [self errorFor:a];");
1751 verifyFormat("@throw [self errorFor:a];");
1752
Nico Webere8ccc812013-01-12 22:48:47 +00001753 // This tests that the formatter doesn't break after "backing" but before ":",
1754 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001755 verifyFormat(
1756 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001757 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1758 " backing:NSBackingStoreBuffered defer:YES]))");
1759
Nico Webere8ccc812013-01-12 22:48:47 +00001760 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1761 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001762
1763}
1764
Nico Weber581f5572013-01-07 15:56:25 +00001765TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001766 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001767 verifyFormat("@catch");
1768 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001769 verifyFormat("@compatibility_alias");
1770 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001771 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001772 verifyFormat("@encode");
1773 verifyFormat("@end");
1774 verifyFormat("@finally");
1775 verifyFormat("@implementation");
1776 verifyFormat("@import");
1777 verifyFormat("@interface");
1778 verifyFormat("@optional");
1779 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001780 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001781 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001782 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001783 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001784 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001785 verifyFormat("@required");
1786 verifyFormat("@selector");
1787 verifyFormat("@synchronized");
1788 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001789 verifyFormat("@throw");
1790 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001791
Nico Webercb4d6902013-01-08 19:40:21 +00001792 verifyFormat("@\"String\"");
1793 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001794 verifyFormat("@+4.8");
1795 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001796 verifyFormat("@1LL");
1797 verifyFormat("@.5");
1798 verifyFormat("@'c'");
1799 verifyFormat("@true");
1800 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001801 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001802 verifyFormat("@[");
1803 verifyFormat("@{");
1804
Nico Weber581f5572013-01-07 15:56:25 +00001805 EXPECT_EQ("@interface", format("@ interface"));
1806
1807 // The precise formatting of this doesn't matter, nobody writes code like
1808 // this.
1809 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001810}
1811
Nico Weberc31689a2013-01-08 19:15:23 +00001812TEST_F(FormatTest, ObjCSnippets) {
1813 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001814 verifyFormat("@autoreleasepool {\n"
1815 " foo();\n"
1816 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001817 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001818 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001819 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001820 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001821 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001822 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001823 verifyFormat("@synchronized(self) {\n"
1824 " f();\n"
1825 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001826
Nico Weber70848232013-01-10 21:30:42 +00001827 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1828 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1829
Nico Webercf4a79c2013-01-08 17:56:31 +00001830 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001831 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1832 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001833}
1834
Daniel Jaspercd162382013-01-07 13:26:07 +00001835} // end namespace tooling
1836} // end namespace clang