blob: 3c92d82f8fb528f17c8478ec56700d98df902eeb [file] [log] [blame]
Daniel Jasperf7935112012-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 Klimek24998102013-01-16 14:55:28 +000010#define DEBUG_TYPE "format-test"
11
Chandler Carruth320d9662012-12-04 09:45:34 +000012#include "clang/Format/Format.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000013#include "clang/Lex/Lexer.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000014#include "gtest/gtest.h"
Manuel Klimek24998102013-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 Jasperf7935112012-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 Klimek24998102013-01-16 14:55:28 +000028 DEBUG(llvm::errs() << "---\n");
Daniel Jasperf7935112012-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 Jasperc1fa2812013-01-10 13:08:12 +000036 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
37 getFormattingLangOpts());
Daniel Jasper8d1832e2013-01-07 13:26:07 +000038 tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
Alexander Kornienko116ba682013-01-14 11:34:14 +000039 Ranges,
40 new IgnoringDiagConsumer());
Daniel Jasperf7935112012-12-03 18:12:45 +000041 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
Manuel Klimek24998102013-01-16 14:55:28 +000042 DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n");
Daniel Jasperf7935112012-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 Jasper3f69a1b2012-12-18 19:56:56 +000051 std::string messUp(llvm::StringRef Code) {
52 std::string MessedUp(Code.str());
53 bool InComment = false;
Manuel Klimek52b15152013-01-09 15:25:02 +000054 bool InPreprocessorDirective = false;
Daniel Jasper3f69a1b2012-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 Jasper39825ea2013-01-14 15:40:57 +000061 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
62 if (i != 0) MessedUp[i - 1] = '\n';
Manuel Klimek52b15152013-01-09 15:25:02 +000063 InPreprocessorDirective = true;
Manuel Klimek1abf7892013-01-04 23:34:14 +000064 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
65 MessedUp[i] = ' ';
Manuel Klimek52b15152013-01-09 15:25:02 +000066 MessedUp[i + 1] = ' ';
Daniel Jasper3f69a1b2012-12-18 19:56:56 +000067 } else if (MessedUp[i] == '\n') {
68 if (InComment) {
69 InComment = false;
Manuel Klimek52b15152013-01-09 15:25:02 +000070 } else if (InPreprocessorDirective) {
71 InPreprocessorDirective = false;
Daniel Jasper3f69a1b2012-12-18 19:56:56 +000072 } else {
73 JustReplacedNewline = true;
74 MessedUp[i] = ' ';
75 }
Manuel Klimek1abf7892013-01-04 23:34:14 +000076 } else if (MessedUp[i] != ' ') {
77 JustReplacedNewline = false;
Daniel Jasper3f69a1b2012-12-18 19:56:56 +000078 }
Daniel Jasperf7935112012-12-03 18:12:45 +000079 }
Daniel Jasper3f69a1b2012-12-18 19:56:56 +000080 return MessedUp;
81 }
82
Manuel Klimekb69e3c62013-01-02 18:33:23 +000083 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
84 FormatStyle Style = getLLVMStyle();
85 Style.ColumnLimit = ColumnLimit;
86 return Style;
87 }
88
Daniel Jasper1b750ed2013-01-14 16:24:39 +000089 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
90 FormatStyle Style = getGoogleStyle();
91 Style.ColumnLimit = ColumnLimit;
92 return Style;
93 }
94
Manuel Klimekb69e3c62013-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 Jasperf7935112012-12-03 18:12:45 +000098 }
99
100 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000101 verifyFormat(Code, getGoogleStyle());
Daniel Jasperf7935112012-12-03 18:12:45 +0000102 }
103};
104
Manuel Klimek52b15152013-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 Kornienko578fdd82012-12-06 18:03:27 +0000113//===----------------------------------------------------------------------===//
114// Basic function tests.
115//===----------------------------------------------------------------------===//
116
Daniel Jasperf7935112012-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 Klimeke7d10a12013-01-10 13:24:24 +0000133 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperf7935112012-12-03 18:12:45 +0000134}
135
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000136TEST_F(FormatTest, FormatsNestedCall) {
137 verifyFormat("Method(f1, f2(f3));");
138 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper48cb3b92013-01-13 08:19:51 +0000139 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000140}
141
Daniel Jasper736c14f2013-01-16 07:19:28 +0000142TEST_F(FormatTest, ImportantSpaces) {
143 verifyFormat("vector< ::Type> v;");
144}
145
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000146//===----------------------------------------------------------------------===//
147// Tests for control statements.
148//===----------------------------------------------------------------------===//
149
150TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
Daniel Jasper1b750ed2013-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 Kornienko578fdd82012-12-06 18:03:27 +0000153 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jasperced17f82013-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 Jasper3e9218e2013-01-14 16:02:06 +0000174 verifyFormat("if (aaaaaaaaa)\n"
Daniel Jasperced17f82013-01-16 15:44:34 +0000175 " return;", AllowsMergedIf);
176
177 AllowsMergedIf.ColumnLimit = 13;
178 verifyFormat("if (a)\n return;", AllowsMergedIf);
Alexander Kornienko578fdd82012-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 Jasper1b750ed2013-01-14 16:24:39 +0000195 " if (true)\n"
196 " f();\n"
Alexander Kornienko578fdd82012-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 Klimeke7d10a12013-01-10 13:24:24 +0000208 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko578fdd82012-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 Jasperf7935112012-12-03 18:12:45 +0000217TEST_F(FormatTest, FormatsForLoop) {
218 verifyFormat(
219 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000220 " ++VeryVeryLongLoopVariable)\n"
221 " ;");
222 verifyFormat("for (;;)\n"
223 " f();");
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000224 verifyFormat("for (;;) {}");
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000225 verifyFormat("for (;;) {\n"
226 " f();\n"
227 "}");
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000228
229 verifyFormat(
230 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
231 " E = UnwrappedLines.end();\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000232 " I != E; ++I) {}");
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000233
234 verifyFormat(
235 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000236 " ++IIIII) {}");
Daniel Jasperf7935112012-12-03 18:12:45 +0000237}
238
239TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000240 verifyFormat("while (true) {}");
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000241 verifyFormat("while (true)\n"
242 " f();");
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000243 verifyFormat("while () {}");
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000244 verifyFormat("while () {\n"
245 " f();\n"
246 "}");
Daniel Jasperf7935112012-12-03 18:12:45 +0000247}
248
Alexander Kornienko578fdd82012-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 Jasperf7935112012-12-03 18:12:45 +0000256}
257
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000258TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperf7935112012-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 Kornienko578fdd82012-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 Jasperf7935112012-12-03 18:12:45 +0000299}
300
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000301TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperf7935112012-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 Kornienko578fdd82012-12-06 18:03:27 +0000317//===----------------------------------------------------------------------===//
318// Tests for comments.
319//===----------------------------------------------------------------------===//
320
321TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jasper3f69a1b2012-12-18 19:56:56 +0000322 verifyFormat("// line 1\n"
323 "// line 2\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000324 "void f() {}\n");
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000325
Daniel Jasper3f69a1b2012-12-18 19:56:56 +0000326 verifyFormat("void f() {\n"
327 " // Doesn't do anything\n"
328 "}");
Daniel Jasper942ee722013-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 Kornienko578fdd82012-12-06 18:03:27 +0000337
Daniel Jasper5ad1e192013-01-07 11:09:06 +0000338 verifyFormat("int i // This is a fancy variable\n"
Daniel Jasper3f69a1b2012-12-18 19:56:56 +0000339 " = 5;");
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000340
Daniel Jasper3f69a1b2012-12-18 19:56:56 +0000341 verifyFormat("enum E {\n"
342 " // comment\n"
Daniel Jasper5ad1e192013-01-07 11:09:06 +0000343 " VAL_A, // comment\n"
Daniel Jasper3f69a1b2012-12-18 19:56:56 +0000344 " VAL_B\n"
345 "};");
Daniel Jaspere25509f2012-12-17 11:29:41 +0000346
347 verifyFormat(
348 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper5ad1e192013-01-07 11:09:06 +0000349 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasperd8bb2db2013-01-09 09:33:39 +0000350 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
351 " // Comment inside a statement.\n"
352 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimekc74d2922013-01-07 08:54:53 +0000353
Daniel Jasper5ad1e192013-01-07 11:09:06 +0000354 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimekc74d2922013-01-07 08:54:53 +0000355 format("int i;\\\n// single line trailing comment"));
Daniel Jasper5ad1e192013-01-07 11:09:06 +0000356
357 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jaspere25509f2012-12-17 11:29:41 +0000358}
359
360TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000361 verifyFormat("f(/*test=*/ true);");
362}
363
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000364//===----------------------------------------------------------------------===//
365// Tests for classes, namespaces, etc.
366//===----------------------------------------------------------------------===//
367
368TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000369 verifyFormat("class A {};");
Alexander Kornienko578fdd82012-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 Klimeke7d10a12013-01-10 13:24:24 +0000377 " void f() {}\n"
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000378 "};");
379 verifyGoogleFormat("class A {\n"
380 " public:\n"
381 " protected:\n"
382 " private:\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000383 " void f() {}\n"
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000384 "};");
385}
386
387TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000388 verifyFormat("class A : public B {};");
389 verifyFormat("class A : public ::B {};");
Daniel Jasperf7935112012-12-03 18:12:45 +0000390}
391
Manuel Klimek28cacc72013-01-07 18:10:23 +0000392TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000393 verifyFormat("class A {} a, b;");
394 verifyFormat("struct A {} a, b;");
Daniel Jasper22cf4622013-01-13 14:39:04 +0000395 verifyFormat("union A {} a;");
Manuel Klimek28cacc72013-01-07 18:10:23 +0000396}
397
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000398TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkob7076a22012-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 Weber7769a902013-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 Kornienko578fdd82012-12-06 18:03:27 +0000420TEST_F(FormatTest, FormatsNamespaces) {
421 verifyFormat("namespace some_namespace {\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000422 "class A {};\n"
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +0000423 "void f() { f(); }\n"
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000424 "}");
425 verifyFormat("namespace {\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000426 "class A {};\n"
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +0000427 "void f() { f(); }\n"
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000428 "}");
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000429 verifyFormat("inline namespace X {\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000430 "class A {};\n"
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +0000431 "void f() { f(); }\n"
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000432 "}");
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000433 verifyFormat("using namespace some_namespace;\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000434 "class A {};\n"
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +0000435 "void f() { f(); }");
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000436}
437
Nico Weberd5650bd2013-01-07 16:36:17 +0000438TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimekf4ab9ef2013-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 Weberd5650bd2013-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 Klimekf4ab9ef2013-01-11 17:54:10 +0000452 verifyFormat("int f() try { return 4; }\n"
Nico Weberd5650bd2013-01-07 16:36:17 +0000453 "catch (...) {\n"
454 " return 5;\n"
455 "}");
456 verifyFormat("class A {\n"
457 " int a;\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000458 " A() try : a(0) {}\n"
Nico Weberd5650bd2013-01-07 16:36:17 +0000459 " catch (...) {\n"
460 " throw;\n"
461 " }\n"
462 "};\n");
463}
464
465TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimek2acb7b72013-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 Weberd5650bd2013-01-07 16:36:17 +0000475}
476
Daniel Jaspere25509f2012-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 Jasper206df732013-01-07 13:08:40 +0000482 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimek0ddd57a2013-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 Jaspere25509f2012-12-17 11:29:41 +0000490}
491
Manuel Klimeka54d1a92013-01-14 16:41:43 +0000492TEST_F(FormatTest, NestedStaticInitializers) {
493 verifyFormat("static A x = { { {} } };\n");
494 verifyFormat(
Daniel Jasper9278eb92013-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 Klimeka54d1a92013-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 Klimeka71e5d82013-01-02 16:30:12 +0000519TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
520 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
521 " \\\n"
522 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
523}
524
Daniel Jasperda16db32013-01-07 10:48:50 +0000525TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000526 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
527 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasperda16db32013-01-07 10:48:50 +0000528}
529
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000530TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
531 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimek78725712013-01-07 10:03:37 +0000532 verifyFormat("#\n;\n;\n;");
Manuel Klimeka71e5d82013-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 Jasper39825ea2013-01-14 15:40:57 +0000538 EXPECT_EQ("#define A B\n",
Manuel Klimek1abf7892013-01-04 23:34:14 +0000539 format("# \\\n define \\\n A \\\n B\n",
540 getLLVMStyleWithColumns(12)));
Manuel Klimeka71e5d82013-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 Jasper39825ea2013-01-14 15:40:57 +0000546 EXPECT_EQ("#define A B",
547 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000548}
549
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000550TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimek38ba11e2013-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 Klimekb69e3c62013-01-02 18:33:23 +0000555
Manuel Klimek38ba11e2013-01-07 09:24:17 +0000556 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
557 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000558 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jasper39825ea2013-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 Klimekb69e3c62013-01-02 18:33:23 +0000566}
567
568TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimek1abf7892013-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 Klimekb69e3c62013-01-02 18:33:23 +0000581}
582
Manuel Klimek1abf7892013-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 Klimeke7d10a12013-01-10 13:24:24 +0000596 EXPECT_EQ("{}", format("{}"));
Manuel Klimek1abf7892013-01-04 23:34:14 +0000597}
598
599TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000600 EXPECT_EQ("# define A\\\n b;",
601 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimek1abf7892013-01-04 23:34:14 +0000602}
603
604TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek52b15152013-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 Klimek1abf7892013-01-04 23:34:14 +0000608}
609
Manuel Klimek09e07972013-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 Jasper4f397152013-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 Jasper39825ea2013-01-14 15:40:57 +0000623 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek09e07972013-01-05 21:34:55 +0000624}
625
Manuel Klimekef2cfb12013-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 Klimek52d0fd82013-01-05 22:56:06 +0000630TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +0000631 verifyFormat("{\n { a #c; }\n}");
Manuel Klimek52d0fd82013-01-05 22:56:06 +0000632}
633
Manuel Klimek1058d982013-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 Klimek1abf7892013-01-04 23:34:14 +0000640
Manuel Klimekef920692013-01-07 07:56:50 +0000641TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jasper8d1832e2013-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 Klimekef920692013-01-07 07:56:50 +0000645}
646
Manuel Klimek38ba11e2013-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 Klimeka71e5d82013-01-02 16:30:12 +0000654TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimek1abf7892013-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 Klimeka71e5d82013-01-02 16:30:12 +0000668}
669
Manuel Klimek52b15152013-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 Jasper39825ea2013-01-14 15:40:57 +0000679 "#define A B\n"
Manuel Klimek52b15152013-01-09 15:25:02 +0000680 " withMoreParamters,\n"
681 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper9278eb92013-01-16 14:59:02 +0000682 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek52b15152013-01-09 15:25:02 +0000683}
684
Manuel Klimek8e07a1b2013-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 Klimekf4ab9ef2013-01-11 17:54:10 +0000692 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimek8e07a1b2013-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 Klimeke7d10a12013-01-10 13:24:24 +0000707TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
708 EXPECT_EQ("{}", format("{}"));
709}
710
Alexander Kornienko578fdd82012-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 Klimeke7d10a12013-01-10 13:24:24 +0000718 " int c, int ddddddddddddd) {}");
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000719}
720
721TEST_F(FormatTest, FormatsAwesomeMethodCall) {
722 verifyFormat(
Daniel Jasper6d822722012-12-24 16:43:00 +0000723 "SomeLongMethodName(SomeReallyLongMethod(\n"
724 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
725 " SecondLongCall(parameter));");
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000726}
727
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000728TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000729 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper2408a8c2013-01-11 11:37:55 +0000730 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
731 getLLVMStyleWithColumns(45));
732 verifyFormat("Constructor()\n"
733 " : Inttializer(FitsOnTheLine) {}",
734 getLLVMStyleWithColumns(44));
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000735
736 verifyFormat(
737 "SomeClass::Constructor()\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000738 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000739
740 verifyFormat(
741 "SomeClass::Constructor()\n"
Daniel Jasper2408a8c2013-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 Jasper2af6bbe2012-12-18 21:05:13 +0000752 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000753 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000754
755 verifyFormat("Constructor()\n"
756 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
757 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
758 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000759 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper2af6bbe2012-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 Klimeke7d10a12013-01-10 13:24:24 +0000766 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper2af6bbe2012-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 Jasper7b7877a2013-01-12 07:36:22 +0000772
773 // This test takes VERY long when memoization is broken.
Daniel Jasper9278eb92013-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 Jasper2af6bbe2012-12-18 21:05:13 +0000781}
782
Alexander Kornienko578fdd82012-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 Jasperf7935112012-12-03 18:12:45 +0000790TEST_F(FormatTest, BreaksDesireably) {
791 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
792 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000793 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperf7935112012-12-03 18:12:45 +0000794
795 verifyFormat(
796 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000797 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperf7935112012-12-03 18:12:45 +0000798
799 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jasper9b155472012-12-04 10:50:12 +0000802
803 verifyFormat(
804 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
806 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
807 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000808
809 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
810 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
811
Daniel Jasper3d0c75c2013-01-02 14:40:02 +0000812 verifyFormat(
813 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
815
Daniel Jasperaa1c9202012-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 Jasper2eda23e2012-12-24 13:43:52 +0000820 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
821 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
822 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper6d822722012-12-24 16:43:00 +0000823
Daniel Jasper8d1832e2013-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 Jasperf7935112012-12-03 18:12:45 +0000829}
830
Daniel Jasper9278eb92013-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 Jasperfd8c4b12013-01-11 14:23:32 +0000856TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
857 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
858 " GUARDED_BY(aaaaaaaaaaaaa);");
859}
860
Daniel Jasperde5c2072012-12-24 00:13:23 +0000861TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
862 verifyFormat(
863 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000864 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000865 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000866 " ccccccccccccccccccccccccc) {}");
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000867 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000868 " ccccccccccccccccccccccccc) {}");
Daniel Jasperde5c2072012-12-24 00:13:23 +0000869 verifyFormat(
870 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000871 " ccccccccccccccccccccccccc) {}");
Daniel Jasperde5c2072012-12-24 00:13:23 +0000872}
873
Daniel Jasper206df732013-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 Jasper2eda23e2012-12-24 13:43:52 +0000885TEST_F(FormatTest, AlignsAfterAssignments) {
886 verifyFormat(
887 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper206df732013-01-07 13:08:40 +0000888 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000889 verifyFormat(
890 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper206df732013-01-07 13:08:40 +0000891 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000892 verifyFormat(
893 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper206df732013-01-07 13:08:40 +0000894 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000895 verifyFormat(
896 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper206df732013-01-07 13:08:40 +0000897 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000898 verifyFormat(
Daniel Jasper206df732013-01-07 13:08:40 +0000899 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
900 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
901 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jasper2eda23e2012-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 Jasper399d24b2013-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);");
Daniel Jasper8c5fba92013-01-16 16:23:19 +0000920 verifyFormat(
921 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
922 " aaaaaaaaaaaaa);");
Daniel Jasper399d24b2013-01-09 07:06:56 +0000923}
924
Nico Weber4a5030c2013-01-12 01:28:06 +0000925TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
926 verifyFormat("arr[foo ? bar : baz];");
927 verifyFormat("f()[foo ? bar : baz];");
928 verifyFormat("(a + b)[foo ? bar : baz];");
929 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
930}
931
Daniel Jasperf7935112012-12-03 18:12:45 +0000932TEST_F(FormatTest, AlignsStringLiterals) {
933 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
934 " \"short literal\");");
935 verifyFormat(
936 "looooooooooooooooooooooooongFunction(\n"
937 " \"short literal\"\n"
938 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
939}
940
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000941TEST_F(FormatTest, AlignsPipes) {
942 verifyFormat(
943 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
944 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
945 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
946 verifyFormat(
947 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
948 " << aaaaaaaaaaaaaaaaaaaa;");
949 verifyFormat(
950 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
951 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
952 verifyFormat(
953 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
954 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
955 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
956 verifyFormat(
957 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
958 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
959 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
960}
961
Daniel Jasperf7935112012-12-03 18:12:45 +0000962TEST_F(FormatTest, UnderstandsEquals) {
963 verifyFormat(
964 "aaaaaaaaaaaaaaaaa =\n"
965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
966 verifyFormat(
967 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperf7935112012-12-03 18:12:45 +0000969 verifyFormat(
970 "if (a) {\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000971 " f();\n"
Daniel Jasperf7935112012-12-03 18:12:45 +0000972 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperf7935112012-12-03 18:12:45 +0000974
Daniel Jasper206df732013-01-07 13:08:40 +0000975 verifyFormat(
Daniel Jasperbcab4302013-01-09 10:40:23 +0000976 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper206df732013-01-07 13:08:40 +0000977 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +0000978 " 10000000) {}");
Daniel Jasperf7935112012-12-03 18:12:45 +0000979}
980
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000981TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000982 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
983 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000984
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000985 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
986 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000987
988 verifyFormat(
989 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
990 " Parameter2);");
991
992 verifyFormat(
993 "ShortObject->shortFunction(\n"
994 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
995 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
996
997 verifyFormat("loooooooooooooongFunction(\n"
998 " LoooooooooooooongObject->looooooooooooooooongFunction());");
999
1000 verifyFormat(
1001 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1002 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1003
Daniel Jasperc7345cc2013-01-07 07:13:20 +00001004 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper2af6bbe2012-12-18 21:05:13 +00001005 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001006 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperc7345cc2013-01-07 07:13:20 +00001007 verifyFormat(
1008 "aaaaaaaaaaa->aaaaaaaaa(\n"
1009 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1010 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper5485d0c2012-12-17 14:34:14 +00001011}
1012
Daniel Jasperac5c1c22013-01-02 15:08:56 +00001013TEST_F(FormatTest, WrapsTemplateDeclarations) {
1014 verifyFormat("template <typename T>\n"
1015 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1016 verifyFormat(
1017 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1018 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
1019 verifyFormat(
1020 "template <typename T>\n"
1021 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1022 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper90e51fd2013-01-02 18:30:06 +00001023 verifyFormat(
1024 "template <typename T>\n"
1025 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1026 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper89058942013-01-09 09:50:48 +00001028 verifyFormat("template <typename T>\n"
1029 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1030 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasperbcab4302013-01-09 10:40:23 +00001031 verifyFormat(
1032 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1033 " typename T4 = char>\n"
1034 "void f();");
Daniel Jasperac5c1c22013-01-02 15:08:56 +00001035}
1036
Daniel Jasperf7935112012-12-03 18:12:45 +00001037TEST_F(FormatTest, UnderstandsTemplateParameters) {
1038 verifyFormat("A<int> a;");
1039 verifyFormat("A<A<A<int> > > a;");
1040 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1041 verifyFormat("bool x = a < 1 || 2 > a;");
1042 verifyFormat("bool x = 5 < f<int>();");
1043 verifyFormat("bool x = f<int>() > 5;");
1044 verifyFormat("bool x = 5 < a<int>::x;");
1045 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1046 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1047
1048 verifyGoogleFormat("A<A<int>> a;");
1049 verifyGoogleFormat("A<A<A<int>>> a;");
1050 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1051
1052 verifyFormat("test >> a >> b;");
1053 verifyFormat("test << a >> b;");
1054
1055 verifyFormat("f<int>();");
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001056 verifyFormat("template <typename T> void f() {}");
Daniel Jasperf7935112012-12-03 18:12:45 +00001057}
1058
Daniel Jasper8dd40472012-12-21 09:41:31 +00001059TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001060 verifyFormat("int a = -2;");
Daniel Jasper8b529712012-12-04 13:02:32 +00001061 verifyFormat("f(-1, -2, -3);");
1062 verifyFormat("a[-1] = 5;");
1063 verifyFormat("int a = 5 + -2;");
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001064 verifyFormat("if (i == -1) {}");
1065 verifyFormat("if (i != -1) {}");
1066 verifyFormat("if (i > -1) {}");
1067 verifyFormat("if (i < -1) {}");
Daniel Jasper26333c32012-12-06 13:16:39 +00001068 verifyFormat("++(a->f());");
1069 verifyFormat("--(a->f());");
Daniel Jasper13f23e12013-01-14 12:18:19 +00001070 verifyFormat("(a->f())++;");
1071 verifyFormat("a[42]++;");
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001072 verifyFormat("if (!(a->f())) {}");
Daniel Jasper8dd40472012-12-21 09:41:31 +00001073
1074 verifyFormat("a-- > b;");
1075 verifyFormat("b ? -a : c;");
1076 verifyFormat("n * sizeof char16;");
Daniel Jasper3c0431c2013-01-02 17:21:36 +00001077 verifyFormat("n * alignof char16;");
Daniel Jasper8dd40472012-12-21 09:41:31 +00001078 verifyFormat("sizeof(char);");
Daniel Jasper3c0431c2013-01-02 17:21:36 +00001079 verifyFormat("alignof(char);");
Daniel Jasperda1c68a2013-01-02 15:26:16 +00001080
1081 verifyFormat("return -1;");
1082 verifyFormat("switch (a) {\n"
1083 "case -1:\n"
1084 " break;\n"
1085 "}");
Nico Weber63a54eb2013-01-12 05:41:23 +00001086
1087 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1088 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasper71945272013-01-15 14:27:39 +00001089
1090 verifyFormat("int a = /* confusing comment */ -1;");
1091 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1092 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperf7935112012-12-03 18:12:45 +00001093}
1094
1095TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasper537a2962012-12-24 10:56:04 +00001096 verifyFormat("bool operator<();");
1097 verifyFormat("bool operator>();");
1098 verifyFormat("bool operator=();");
1099 verifyFormat("bool operator==();");
1100 verifyFormat("bool operator!=();");
1101 verifyFormat("int operator+();");
1102 verifyFormat("int operator++();");
1103 verifyFormat("bool operator();");
1104 verifyFormat("bool operator()();");
1105 verifyFormat("bool operator[]();");
1106 verifyFormat("operator bool();");
1107 verifyFormat("operator SomeType<int>();");
1108 verifyFormat("void *operator new(std::size_t size);");
1109 verifyFormat("void *operator new[](std::size_t size);");
1110 verifyFormat("void operator delete(void *ptr);");
1111 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperf7935112012-12-03 18:12:45 +00001112}
1113
Daniel Jasperd6a947f2013-01-11 16:09:04 +00001114TEST_F(FormatTest, UnderstandsNewAndDelete) {
1115 verifyFormat("A *a = new A;");
1116 verifyFormat("A *a = new (placement) A;");
1117 verifyFormat("delete a;");
1118 verifyFormat("delete (A *)a;");
1119}
1120
Daniel Jasper22bcf8a2013-01-02 08:57:10 +00001121TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001122 verifyFormat("int *f(int *a) {}");
Daniel Jasperf7935112012-12-03 18:12:45 +00001123 verifyFormat("f(a, *a);");
1124 verifyFormat("f(*a);");
1125 verifyFormat("int a = b * 10;");
1126 verifyFormat("int a = 10 * b;");
Daniel Jasper426702d2012-12-05 07:51:39 +00001127 verifyFormat("int a = b * c;");
Daniel Jasperaa1c9202012-12-05 14:57:28 +00001128 verifyFormat("int a += b * c;");
1129 verifyFormat("int a -= b * c;");
1130 verifyFormat("int a *= b * c;");
1131 verifyFormat("int a /= b * c;");
Daniel Jasperf7935112012-12-03 18:12:45 +00001132 verifyFormat("int a = *b;");
Daniel Jasper426702d2012-12-05 07:51:39 +00001133 verifyFormat("int a = *b * c;");
1134 verifyFormat("int a = b * *c;");
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001135 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber6f372e62012-12-23 01:07:46 +00001136 verifyFormat("return 10 * b;");
1137 verifyFormat("return *b * *c;");
1138 verifyFormat("return a & ~b;");
Daniel Jasper22bcf8a2013-01-02 08:57:10 +00001139 verifyFormat("f(b ? *c : *d);");
1140 verifyFormat("int a = b ? *c : *d;");
Daniel Jasper3c0431c2013-01-02 17:21:36 +00001141 verifyFormat("*b = a;");
1142 verifyFormat("a * ~b;");
1143 verifyFormat("a * !b;");
1144 verifyFormat("a * +b;");
1145 verifyFormat("a * -b;");
1146 verifyFormat("a * ++b;");
1147 verifyFormat("a * --b;");
Nico Weber5dafd4a2013-01-12 05:47:16 +00001148 verifyFormat("a[4] * b;");
1149 verifyFormat("f() * b;");
Nico Webereee7b812013-01-12 05:50:48 +00001150 verifyFormat("a * [self dostuff];");
1151 verifyFormat("a * (a + b);");
1152 verifyFormat("(a *)(a + b);");
Daniel Jasper7194e182013-01-10 11:14:08 +00001153 verifyFormat("int *pa = (int *)&a;");
Daniel Jasper27234032012-12-07 09:52:15 +00001154
Daniel Jasper3c2557d2013-01-04 20:46:38 +00001155 verifyFormat("InvalidRegions[*R] = 0;");
1156
Daniel Jaspera4396862012-12-10 18:59:13 +00001157 verifyFormat("A<int *> a;");
1158 verifyFormat("A<int **> a;");
1159 verifyFormat("A<int *, int *> a;");
1160 verifyFormat("A<int **, int **> a;");
1161
Daniel Jasper66dcb1c2013-01-08 20:03:18 +00001162 verifyFormat(
1163 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1165
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001166 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jaspera4396862012-12-10 18:59:13 +00001167 verifyGoogleFormat("A<int*> a;");
1168 verifyGoogleFormat("A<int**> a;");
1169 verifyGoogleFormat("A<int*, int*> a;");
1170 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper22bcf8a2013-01-02 08:57:10 +00001171 verifyGoogleFormat("f(b ? *c : *d);");
1172 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jaspera1dc93a2013-01-16 16:04:06 +00001173 verifyGoogleFormat("Type* t = **x;");
1174 verifyGoogleFormat("Type* t = *++*x;");
1175 verifyGoogleFormat("*++*x;");
1176 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1177 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek557811f2013-01-14 10:58:01 +00001178
1179 verifyFormat("a = *(x + y);");
1180 verifyFormat("a = &(x + y);");
1181 verifyFormat("*(x + y).call();");
1182 verifyFormat("&(x + y)->call();");
1183 verifyFormat("&(*I).first");
Daniel Jasper71945272013-01-15 14:27:39 +00001184
1185 verifyFormat("f(b * /* confusing comment */ ++c);");
1186 verifyFormat(
1187 "int *MyValues = {\n"
1188 " *A, // Operator detection might be confused by the '{'\n"
1189 " *BB // Operator detection might be confused by previous comment\n"
1190 "};");
Daniel Jasperf7935112012-12-03 18:12:45 +00001191}
1192
Daniel Jasperef906a92013-01-13 08:01:36 +00001193TEST_F(FormatTest, FormatsCasts) {
1194 verifyFormat("Type *A = static_cast<Type *>(P);");
1195 verifyFormat("Type *A = (Type *)P;");
1196 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1197 verifyFormat("int a = (int)(2.0f);");
1198
1199 // FIXME: These also need to be identified.
1200 verifyFormat("int a = (int) 2.0f;");
1201 verifyFormat("int a = (int) * b;");
1202
1203 // These are not casts.
1204 verifyFormat("void f(int *) {}");
1205 verifyFormat("void f(int *);");
1206 verifyFormat("void f(int *) = 0;");
1207 verifyFormat("void f(SmallVector<int>) {}");
1208 verifyFormat("void f(SmallVector<int>);");
1209 verifyFormat("void f(SmallVector<int>) = 0;");
1210}
1211
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001212TEST_F(FormatTest, FormatsFunctionTypes) {
1213 // FIXME: Determine the cases that need a space after the return type and fix.
1214 verifyFormat("A<bool()> a;");
1215 verifyFormat("A<SomeType()> a;");
1216 verifyFormat("A<void(*)(int, std::string)> a;");
1217
1218 verifyFormat("int(*func)(void *);");
1219}
1220
Daniel Jasperd1926a32013-01-02 08:44:14 +00001221TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001222 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001223 " int LoooooooooooooooongParam2) {}");
Daniel Jasperd1926a32013-01-02 08:44:14 +00001224 verifyFormat(
1225 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1226 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001227 " Type *T) {}");
Daniel Jasperd1926a32013-01-02 08:44:14 +00001228}
1229
Daniel Jaspere9de2602012-12-06 09:56:08 +00001230TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1231 verifyFormat("(a)->b();");
1232 verifyFormat("--a;");
1233}
1234
Daniel Jasper8b529712012-12-04 13:02:32 +00001235TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2ab0d012013-01-14 15:52:06 +00001236 verifyFormat("#include <string>\n"
1237 "#include <a/b/c.h>\n"
1238 "#include \"a/b/string\"\n"
1239 "#include \"string.h\"\n"
1240 "#include \"string.h\"\n"
Manuel Klimek99c7baa2013-01-15 15:50:27 +00001241 "#include <a-a>\n"
1242 "#include < path with space >\n");
Nico Weber8f83ee42012-12-21 18:21:56 +00001243
Daniel Jasper5ef433f2013-01-13 08:12:18 +00001244 verifyFormat("#import <string>");
1245 verifyFormat("#import <a/b/c.h>");
1246 verifyFormat("#import \"a/b/string\"");
1247 verifyFormat("#import \"string.h\"");
1248 verifyFormat("#import \"string.h\"");
Daniel Jasper8b529712012-12-04 13:02:32 +00001249}
1250
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001251//===----------------------------------------------------------------------===//
1252// Error recovery tests.
1253//===----------------------------------------------------------------------===//
1254
Daniel Jasper83a54d22013-01-10 09:26:47 +00001255TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienkoae6e53c2013-01-16 11:45:16 +00001256 verifyFormat("void f() { return; }\n42");
1257 verifyFormat("void f() {\n"
1258 " if (0)\n"
1259 " return;\n"
1260 "}\n"
1261 "42");
Alexander Kornienko1231e062013-01-16 11:43:46 +00001262 verifyFormat("void f() { return }\n42");
1263 verifyFormat("void f() {\n"
1264 " if (0)\n"
1265 " return\n"
1266 "}\n"
1267 "42");
1268}
1269
1270TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1271 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1272 EXPECT_EQ("void f() {\n"
1273 " if (a)\n"
1274 " return\n"
1275 "}", format("void f ( ) { if ( a ) return }"));
1276 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1277 EXPECT_EQ("namespace N {\n"
1278 "void f() {}\n"
1279 "void g()\n"
1280 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper83a54d22013-01-10 09:26:47 +00001281}
1282
Daniel Jasper2df93312013-01-09 10:16:05 +00001283TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1284 verifyFormat("int aaaaaaaa =\n"
1285 " // Overly long comment\n"
1286 " b;", getLLVMStyleWithColumns(20));
1287 verifyFormat("function(\n"
1288 " ShortArgument,\n"
1289 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1290}
1291
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001292TEST_F(FormatTest, IncorrectAccessSpecifier) {
1293 verifyFormat("public:");
1294 verifyFormat("class A {\n"
1295 "public\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001296 " void f() {}\n"
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001297 "};");
1298 verifyFormat("public\n"
1299 "int qwerty;");
1300 verifyFormat("public\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001301 "B {}");
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001302 verifyFormat("public\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001303 "{}");
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001304 verifyFormat("public\n"
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001305 "B { int x; }");
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001306}
Daniel Jasperf7935112012-12-03 18:12:45 +00001307
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001308TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1309 verifyFormat("{");
1310}
1311
1312TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001313 verifyFormat("do {}");
1314 verifyFormat("do {}\n"
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001315 "f();");
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001316 verifyFormat("do {}\n"
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001317 "wheeee(fun);");
1318 verifyFormat("do {\n"
1319 " f();\n"
Manuel Klimek28cacc72013-01-07 18:10:23 +00001320 "}");
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001321}
1322
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001323TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekadededf2013-01-11 18:28:36 +00001324 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001325 verifyFormat("switch {\n foo;\n foo();\n}");
1326 verifyFormat("for {\n foo;\n foo();\n}");
1327 verifyFormat("while {\n foo;\n foo();\n}");
1328 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekadededf2013-01-11 18:28:36 +00001329}
1330
Daniel Jasperc0880a92013-01-04 18:52:56 +00001331TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1332 verifyFormat("namespace {\n"
Manuel Klimek1abf7892013-01-04 23:34:14 +00001333 "class Foo { Foo ( }; } // comment");
Daniel Jasperc0880a92013-01-04 18:52:56 +00001334}
1335
Alexander Kornienko870f9eb2012-12-04 17:27:50 +00001336TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001337 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1338 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1339 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1340 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienko870f9eb2012-12-04 17:27:50 +00001341
Alexander Kornienko870f9eb2012-12-04 17:27:50 +00001342 EXPECT_EQ("{\n"
1343 " {\n"
1344 " breakme(\n"
1345 " qwe);\n"
1346 "}\n", format("{\n"
1347 " {\n"
1348 " breakme(qwe);\n"
Alexander Kornienko116ba682013-01-14 11:34:14 +00001349 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienko870f9eb2012-12-04 17:27:50 +00001350}
1351
Manuel Klimek73a2fdf2013-01-10 14:36:46 +00001352TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1353 verifyFormat(
1354 "int x = {\n"
1355 " avariable,\n"
Manuel Klimek0ddd57a2013-01-10 15:58:26 +00001356 " b(alongervariable)\n"
1357 "};", getLLVMStyleWithColumns(25));
Manuel Klimek73a2fdf2013-01-10 14:36:46 +00001358}
1359
1360TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1361 verifyFormat(
1362 "Aaa({\n"
1363 " int i;\n"
1364 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1365 " ccccccccccccccccc));");
1366}
1367
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001368TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1369 verifyFormat("void f() { return 42; }");
1370 verifyFormat("void f() {\n"
1371 " // Comment\n"
1372 "}");
1373 verifyFormat("{\n"
1374 "#error {\n"
1375 " int a;\n"
1376 "}");
1377 verifyFormat("{\n"
1378 " int a;\n"
1379 "#error {\n"
1380 "}");
1381}
1382
Manuel Klimeke01bab52013-01-15 13:38:33 +00001383TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1384 // Elaborate type variable declarations.
Manuel Klimekd5e5f8f2013-01-11 18:13:04 +00001385 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimeke01bab52013-01-15 13:38:33 +00001386 verifyFormat("class foo a = { bar };\nint n;");
1387 verifyFormat("union foo a = { bar };\nint n;");
1388
1389 // Elaborate types inside function definitions.
1390 verifyFormat("struct foo f() {}\nint n;");
1391 verifyFormat("class foo f() {}\nint n;");
1392 verifyFormat("union foo f() {}\nint n;");
1393
1394 // Templates.
1395 verifyFormat("template <class X> void f() {}\nint n;");
1396 verifyFormat("template <struct X> void f() {}\nint n;");
1397 verifyFormat("template <union X> void f() {}\nint n;");
1398
1399 // Actual definitions...
1400 verifyFormat("struct {} n;");
1401 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1402 verifyFormat("union Z {\n int n;\n} x;");
1403 verifyFormat("class MACRO Z {} n;");
1404 verifyFormat("class MACRO(X) Z {} n;");
1405 verifyFormat("class __attribute__(X) Z {} n;");
1406 verifyFormat("class __declspec(X) Z {} n;");
1407
1408 // Elaborate types where incorrectly parsing the structural element would
1409 // break the indent.
1410 verifyFormat("if (true)\n"
1411 " class X x;\n"
1412 "else\n"
1413 " f();\n");
Manuel Klimekd5e5f8f2013-01-11 18:13:04 +00001414}
1415
Manuel Klimek99c7baa2013-01-15 15:50:27 +00001416TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1417 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1418 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1419 EXPECT_EQ("#error 1", format(" # error 1"));
1420 EXPECT_EQ("#warning 1", format(" # warning 1"));
1421}
1422
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001423// FIXME: This breaks the order of the unwrapped lines:
1424// TEST_F(FormatTest, OrderUnwrappedLines) {
1425// verifyFormat("{\n"
1426// " bool a; //\n"
1427// "#error {\n"
1428// " int a;\n"
1429// "}");
1430// }
1431
Nico Weber7e6a7a12013-01-08 17:56:31 +00001432//===----------------------------------------------------------------------===//
1433// Objective-C tests.
1434//===----------------------------------------------------------------------===//
1435
Fariborz Jahanian68a542a2012-12-20 19:54:13 +00001436TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1437 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1438 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1439 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001440 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian68a542a2012-12-20 19:54:13 +00001441 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1442 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1443 format("-(NSInteger)Method3:(id)anObject;"));
1444 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1445 format("-(NSInteger)Method4:(id)anObject;"));
1446 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1447 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1448 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1449 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001450 EXPECT_EQ(
1451 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1452 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9017ec32012-12-21 22:51:18 +00001453
1454 // Very long objectiveC method declaration.
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001455 EXPECT_EQ(
1456 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1457 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1458 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1459 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1460 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1461 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1462 format(
1463 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1464 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1465 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1466 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1467 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1468 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Weberd6f962f2013-01-10 20:18:33 +00001469
1470 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weber9efe2912013-01-10 23:11:41 +00001471 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Weberd6f962f2013-01-10 20:18:33 +00001472 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1473 // protocol lists (but not for template classes):
1474 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Weber9efe2912013-01-10 23:11:41 +00001475
1476 verifyFormat("- (int(*)())foo:(int(*)())f;");
1477 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1478
1479 // If there's no return type (very rare in practice!), LLVM and Google style
1480 // agree.
1481 verifyFormat("- foo:(int)f;");
1482 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian68a542a2012-12-20 19:54:13 +00001483}
1484
Daniel Jasperfb3f2482013-01-09 08:36:49 +00001485TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001486 verifyFormat("int (^Block)(int, int);");
1487 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasperfb3f2482013-01-09 08:36:49 +00001488}
1489
Nico Weber7eecf4b2013-01-09 20:25:35 +00001490TEST_F(FormatTest, FormatObjCInterface) {
1491 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Webera6087752013-01-10 20:12:55 +00001492 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001493 "@public\n"
1494 " int field1;\n"
1495 "@protected\n"
1496 " int field2;\n"
1497 "@private\n"
1498 " int field3;\n"
1499 "@package\n"
1500 " int field4;\n"
1501 "}\n"
1502 "+ (id)init;\n"
1503 "@end");
1504
Nico Weber7eecf4b2013-01-09 20:25:35 +00001505 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1506 " @public\n"
1507 " int field1;\n"
1508 " @protected\n"
1509 " int field2;\n"
1510 " @private\n"
1511 " int field3;\n"
1512 " @package\n"
1513 " int field4;\n"
1514 "}\n"
Nico Weber9efe2912013-01-10 23:11:41 +00001515 "+(id) init;\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001516 "@end");
1517
1518 verifyFormat("@interface Foo\n"
1519 "+ (id)init;\n"
1520 "// Look, a comment!\n"
1521 "- (int)answerWith:(int)i;\n"
1522 "@end");
1523
1524 verifyFormat("@interface Foo\n"
Nico Weberd8ffe752013-01-09 21:42:32 +00001525 "@end\n"
1526 "@interface Bar\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001527 "@end");
1528
1529 verifyFormat("@interface Foo : Bar\n"
1530 "+ (id)init;\n"
1531 "@end");
1532
Nico Webera6087752013-01-10 20:12:55 +00001533 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001534 "+ (id)init;\n"
1535 "@end");
1536
Nico Webera6087752013-01-10 20:12:55 +00001537 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weber9efe2912013-01-10 23:11:41 +00001538 "+(id) init;\n"
Nico Webera6087752013-01-10 20:12:55 +00001539 "@end");
1540
Nico Weber2bb00742013-01-10 19:19:14 +00001541 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001542 "+ (id)init;\n"
1543 "@end");
1544
Nico Weber2bb00742013-01-10 19:19:14 +00001545 verifyFormat("@interface Foo ()\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001546 "+ (id)init;\n"
1547 "@end");
1548
Nico Webera6087752013-01-10 20:12:55 +00001549 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001550 "+ (id)init;\n"
1551 "@end");
1552
Nico Webera6087752013-01-10 20:12:55 +00001553 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weber9efe2912013-01-10 23:11:41 +00001554 "+(id) init;\n"
Nico Webera6087752013-01-10 20:12:55 +00001555 "@end");
1556
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001557 verifyFormat("@interface Foo {\n"
1558 " int _i;\n"
1559 "}\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001560 "+ (id)init;\n"
1561 "@end");
1562
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001563 verifyFormat("@interface Foo : Bar {\n"
1564 " int _i;\n"
1565 "}\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001566 "+ (id)init;\n"
1567 "@end");
1568
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001569 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1570 " int _i;\n"
1571 "}\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001572 "+ (id)init;\n"
1573 "@end");
1574
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001575 verifyFormat("@interface Foo (HackStuff) {\n"
1576 " int _i;\n"
1577 "}\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001578 "+ (id)init;\n"
1579 "@end");
1580
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001581 verifyFormat("@interface Foo () {\n"
1582 " int _i;\n"
1583 "}\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001584 "+ (id)init;\n"
1585 "@end");
1586
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001587 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1588 " int _i;\n"
1589 "}\n"
Nico Weber7eecf4b2013-01-09 20:25:35 +00001590 "+ (id)init;\n"
1591 "@end");
1592}
1593
Nico Weber2ce0ac52013-01-09 23:25:37 +00001594TEST_F(FormatTest, FormatObjCImplementation) {
1595 verifyFormat("@implementation Foo : NSObject {\n"
1596 "@public\n"
1597 " int field1;\n"
1598 "@protected\n"
1599 " int field2;\n"
1600 "@private\n"
1601 " int field3;\n"
1602 "@package\n"
1603 " int field4;\n"
1604 "}\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001605 "+ (id)init {}\n"
Nico Weber2ce0ac52013-01-09 23:25:37 +00001606 "@end");
1607
1608 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1609 " @public\n"
1610 " int field1;\n"
1611 " @protected\n"
1612 " int field2;\n"
1613 " @private\n"
1614 " int field3;\n"
1615 " @package\n"
1616 " int field4;\n"
1617 "}\n"
Nico Weber9efe2912013-01-10 23:11:41 +00001618 "+(id) init {}\n"
Nico Weber2ce0ac52013-01-09 23:25:37 +00001619 "@end");
1620
1621 verifyFormat("@implementation Foo\n"
1622 "+ (id)init {\n"
Daniel Jasper1b750ed2013-01-14 16:24:39 +00001623 " if (true)\n"
1624 " return nil;\n"
Nico Weber2ce0ac52013-01-09 23:25:37 +00001625 "}\n"
1626 "// Look, a comment!\n"
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001627 "- (int)answerWith:(int)i {\n"
1628 " return i;\n"
1629 "}\n"
Nico Webera21aaae2013-01-11 21:14:08 +00001630 "+ (int)answerWith:(int)i {\n"
1631 " return i;\n"
1632 "}\n"
Nico Weber2ce0ac52013-01-09 23:25:37 +00001633 "@end");
1634
1635 verifyFormat("@implementation Foo\n"
1636 "@end\n"
1637 "@implementation Bar\n"
1638 "@end");
1639
1640 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001641 "+ (id)init {}\n"
Nico Webera21aaae2013-01-11 21:14:08 +00001642 "- (void)foo {}\n"
Nico Weber2ce0ac52013-01-09 23:25:37 +00001643 "@end");
1644
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001645 verifyFormat("@implementation Foo {\n"
1646 " int _i;\n"
1647 "}\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001648 "+ (id)init {}\n"
Nico Weber2ce0ac52013-01-09 23:25:37 +00001649 "@end");
1650
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001651 verifyFormat("@implementation Foo : Bar {\n"
1652 " int _i;\n"
1653 "}\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001654 "+ (id)init {}\n"
Nico Weber2ce0ac52013-01-09 23:25:37 +00001655 "@end");
1656
Nico Weber2bb00742013-01-10 19:19:14 +00001657 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimeke7d10a12013-01-10 13:24:24 +00001658 "+ (id)init {}\n"
Nico Weber2ce0ac52013-01-09 23:25:37 +00001659 "@end");
1660}
1661
Nico Weber8696a8d2013-01-09 21:15:03 +00001662TEST_F(FormatTest, FormatObjCProtocol) {
1663 verifyFormat("@protocol Foo\n"
1664 "@property(weak) id delegate;\n"
1665 "- (NSUInteger)numberOfThings;\n"
1666 "@end");
1667
Nico Webera6087752013-01-10 20:12:55 +00001668 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber8696a8d2013-01-09 21:15:03 +00001669 "- (NSUInteger)numberOfThings;\n"
1670 "@end");
1671
Nico Webera6087752013-01-10 20:12:55 +00001672 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weber9efe2912013-01-10 23:11:41 +00001673 "-(NSUInteger) numberOfThings;\n"
Nico Webera6087752013-01-10 20:12:55 +00001674 "@end");
1675
Nico Weber8696a8d2013-01-09 21:15:03 +00001676 verifyFormat("@protocol Foo;\n"
1677 "@protocol Bar;\n");
Nico Weberd8ffe752013-01-09 21:42:32 +00001678
1679 verifyFormat("@protocol Foo\n"
1680 "@end\n"
1681 "@protocol Bar\n"
1682 "@end");
Nico Weber51306d22013-01-10 00:25:19 +00001683
1684 verifyFormat("@protocol myProtocol\n"
1685 "- (void)mandatoryWithInt:(int)i;\n"
1686 "@optional\n"
1687 "- (void)optional;\n"
1688 "@required\n"
1689 "- (void)required;\n"
Nico Weberbbe28b32013-01-10 00:42:07 +00001690 "@optional\n"
1691 "@property(assign) int madProp;\n"
Nico Weber51306d22013-01-10 00:25:19 +00001692 "@end\n");
Nico Weber8696a8d2013-01-09 21:15:03 +00001693}
1694
Nico Webera7252d82013-01-12 06:18:40 +00001695TEST_F(FormatTest, FormatObjCMethodExpr) {
1696 verifyFormat("[foo bar:baz];");
1697 verifyFormat("return [foo bar:baz];");
1698 verifyFormat("f([foo bar:baz]);");
1699 verifyFormat("f(2, [foo bar:baz]);");
1700 verifyFormat("f(2, a ? b : c);");
1701 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1702
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];");
1713 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1714 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1715 verifyFormat("[foo bar:baz] ? [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 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1732 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1733 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1734 // Whew!
1735
1736 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1737 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1738 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1739 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1740 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber5c8709b2013-01-12 23:41:33 +00001741 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Weber2827a7e2013-01-12 23:48:49 +00001742 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Webera7252d82013-01-12 06:18:40 +00001743
Nico Webera7252d82013-01-12 06:18:40 +00001744 verifyFormat("arr[[self indexForFoo:a]];");
1745 verifyFormat("throw [self errorFor:a];");
1746 verifyFormat("@throw [self errorFor:a];");
1747
Nico Weberc9d73612013-01-12 22:48:47 +00001748 // This tests that the formatter doesn't break after "backing" but before ":",
1749 // which would be at 80 columns.
Nico Webera7252d82013-01-12 06:18:40 +00001750 verifyFormat(
1751 "void f() {\n"
Nico Weberc9d73612013-01-12 22:48:47 +00001752 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1753 " backing:NSBackingStoreBuffered defer:YES]))");
1754
Nico Weberc9d73612013-01-12 22:48:47 +00001755 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1756 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Webera7252d82013-01-12 06:18:40 +00001757
1758}
1759
Nico Weber12d5bab2013-01-07 15:56:25 +00001760TEST_F(FormatTest, ObjCAt) {
Nico Weber803d61d2013-01-07 16:07:07 +00001761 verifyFormat("@autoreleasepool");
Nico Webere89c42f2013-01-07 16:14:28 +00001762 verifyFormat("@catch");
1763 verifyFormat("@class");
Nico Weber803d61d2013-01-07 16:07:07 +00001764 verifyFormat("@compatibility_alias");
1765 verifyFormat("@defs");
Nico Webera8876502013-01-07 15:17:23 +00001766 verifyFormat("@dynamic");
Nico Weber803d61d2013-01-07 16:07:07 +00001767 verifyFormat("@encode");
1768 verifyFormat("@end");
1769 verifyFormat("@finally");
1770 verifyFormat("@implementation");
1771 verifyFormat("@import");
1772 verifyFormat("@interface");
1773 verifyFormat("@optional");
1774 verifyFormat("@package");
Nico Webere89c42f2013-01-07 16:14:28 +00001775 verifyFormat("@private");
Nico Weber803d61d2013-01-07 16:07:07 +00001776 verifyFormat("@property");
Nico Webere89c42f2013-01-07 16:14:28 +00001777 verifyFormat("@protected");
Nico Weber803d61d2013-01-07 16:07:07 +00001778 verifyFormat("@protocol");
Nico Webere89c42f2013-01-07 16:14:28 +00001779 verifyFormat("@public");
Nico Weber803d61d2013-01-07 16:07:07 +00001780 verifyFormat("@required");
1781 verifyFormat("@selector");
1782 verifyFormat("@synchronized");
1783 verifyFormat("@synthesize");
Nico Webere89c42f2013-01-07 16:14:28 +00001784 verifyFormat("@throw");
1785 verifyFormat("@try");
Nico Weber803d61d2013-01-07 16:07:07 +00001786
Nico Weber77aa2502013-01-08 19:40:21 +00001787 verifyFormat("@\"String\"");
1788 verifyFormat("@1");
Nico Webera1a5abd2013-01-10 19:36:35 +00001789 verifyFormat("@+4.8");
1790 verifyFormat("@-4");
Nico Weber77aa2502013-01-08 19:40:21 +00001791 verifyFormat("@1LL");
1792 verifyFormat("@.5");
1793 verifyFormat("@'c'");
1794 verifyFormat("@true");
1795 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Weberc9d73612013-01-12 22:48:47 +00001796 // FIXME: Array and dictionary literals need more work.
Nico Weber77aa2502013-01-08 19:40:21 +00001797 verifyFormat("@[");
1798 verifyFormat("@{");
1799
Nico Weber12d5bab2013-01-07 15:56:25 +00001800 EXPECT_EQ("@interface", format("@ interface"));
1801
1802 // The precise formatting of this doesn't matter, nobody writes code like
1803 // this.
1804 verifyFormat("@ /*foo*/ interface");
Nico Webera8876502013-01-07 15:17:23 +00001805}
1806
Nico Weberd8cdb532013-01-08 19:15:23 +00001807TEST_F(FormatTest, ObjCSnippets) {
1808 // FIXME: Make the uncommented lines below pass.
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001809 verifyFormat("@autoreleasepool {\n"
1810 " foo();\n"
1811 "}");
Nico Weber7e6a7a12013-01-08 17:56:31 +00001812 verifyFormat("@class Foo, Bar;");
Nico Weberd8cdb532013-01-08 19:15:23 +00001813 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Weber7e6a7a12013-01-08 17:56:31 +00001814 verifyFormat("@dynamic textColor;");
Nico Weberd8cdb532013-01-08 19:15:23 +00001815 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber4cc98742013-01-08 20:16:23 +00001816 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberd8cdb532013-01-08 19:15:23 +00001817 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimek2acb7b72013-01-11 19:17:44 +00001818 verifyFormat("@synchronized(self) {\n"
1819 " f();\n"
1820 "}");
Nico Weber7e6a7a12013-01-08 17:56:31 +00001821
Nico Webera2a84952013-01-10 21:30:42 +00001822 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1823 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1824
Nico Weber7e6a7a12013-01-08 17:56:31 +00001825 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Webera2a84952013-01-10 21:30:42 +00001826 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1827 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Weber7e6a7a12013-01-08 17:56:31 +00001828}
1829
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001830} // end namespace tooling
1831} // end namespace clang