blob: 066e8881eb2d38016906001377f9f753ba18a753 [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 Jasper6f5bb2c2013-01-14 16:24:39 +0000154 verifyGoogleFormat("if (a)\n"
155 " // comment\n"
156 " f();");
157 verifyFormat("if (a) return;", getGoogleStyleWithColumns(14));
158 verifyFormat("if (a)\n return;", getGoogleStyleWithColumns(13));
Daniel Jasperfd0ca972013-01-14 16:02:06 +0000159 verifyFormat("if (aaaaaaaaa)\n"
Daniel Jasper55b08e72013-01-16 07:02:34 +0000160 " return;", getGoogleStyleWithColumns(14));
161 verifyGoogleFormat("if (a) // Can't merge this\n"
162 " f();\n");
163 verifyGoogleFormat("if (a) /* still don't merge */\n"
164 " f();");
165 verifyGoogleFormat("if (a) { // Never merge this\n"
166 " f();\n"
167 "}");
168 verifyGoogleFormat("if (a) { /* Never merge this */\n"
169 " f();\n"
170 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000171}
172
173TEST_F(FormatTest, ParseIfElse) {
174 verifyFormat("if (true)\n"
175 " if (true)\n"
176 " if (true)\n"
177 " f();\n"
178 " else\n"
179 " g();\n"
180 " else\n"
181 " h();\n"
182 "else\n"
183 " i();");
184 verifyFormat("if (true)\n"
185 " if (true)\n"
186 " if (true) {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000187 " if (true)\n"
188 " f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000189 " } else {\n"
190 " g();\n"
191 " }\n"
192 " else\n"
193 " h();\n"
194 "else {\n"
195 " i();\n"
196 "}");
197}
198
199TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000200 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000201 verifyFormat("if (a)\n"
202 " f();\n"
203 "else if (b)\n"
204 " g();\n"
205 "else\n"
206 " h();");
207}
208
Daniel Jasperbac016b2012-12-03 18:12:45 +0000209TEST_F(FormatTest, FormatsForLoop) {
210 verifyFormat(
211 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000212 " ++VeryVeryLongLoopVariable)\n"
213 " ;");
214 verifyFormat("for (;;)\n"
215 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000216 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000217 verifyFormat("for (;;) {\n"
218 " f();\n"
219 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000220
221 verifyFormat(
222 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
223 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000224 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000225
226 verifyFormat(
227 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000228 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000229}
230
231TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000232 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000233 verifyFormat("while (true)\n"
234 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000235 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000236 verifyFormat("while () {\n"
237 " f();\n"
238 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000239}
240
Alexander Kornienko15757312012-12-06 18:03:27 +0000241TEST_F(FormatTest, FormatsDoWhile) {
242 verifyFormat("do {\n"
243 " do_something();\n"
244 "} while (something());");
245 verifyFormat("do\n"
246 " do_something();\n"
247 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000248}
249
Alexander Kornienko15757312012-12-06 18:03:27 +0000250TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000251 verifyFormat("switch (x) {\n"
252 "case 1:\n"
253 " f();\n"
254 " break;\n"
255 "case kFoo:\n"
256 "case ns::kBar:\n"
257 "case kBaz:\n"
258 " break;\n"
259 "default:\n"
260 " g();\n"
261 " break;\n"
262 "}");
263 verifyFormat("switch (x) {\n"
264 "case 1: {\n"
265 " f();\n"
266 " break;\n"
267 "}\n"
268 "}");
269 verifyFormat("switch (test)\n"
270 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000271 verifyGoogleFormat("switch (x) {\n"
272 " case 1:\n"
273 " f();\n"
274 " break;\n"
275 " case kFoo:\n"
276 " case ns::kBar:\n"
277 " case kBaz:\n"
278 " break;\n"
279 " default:\n"
280 " g();\n"
281 " break;\n"
282 "}");
283 verifyGoogleFormat("switch (x) {\n"
284 " case 1: {\n"
285 " f();\n"
286 " break;\n"
287 " }\n"
288 "}");
289 verifyGoogleFormat("switch (test)\n"
290 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000291}
292
Alexander Kornienko15757312012-12-06 18:03:27 +0000293TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000294 verifyFormat("void f() {\n"
295 " some_code();\n"
296 "test_label:\n"
297 " some_other_code();\n"
298 " {\n"
299 " some_more_code();\n"
300 " another_label:\n"
301 " some_more_code();\n"
302 " }\n"
303 "}");
304 verifyFormat("some_code();\n"
305 "test_label:\n"
306 "some_other_code();");
307}
308
Alexander Kornienko15757312012-12-06 18:03:27 +0000309//===----------------------------------------------------------------------===//
310// Tests for comments.
311//===----------------------------------------------------------------------===//
312
313TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000314 verifyFormat("// line 1\n"
315 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000316 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000317
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000318 verifyFormat("void f() {\n"
319 " // Doesn't do anything\n"
320 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000321 verifyFormat("void f(int i, // some comment (probably for i)\n"
322 " int j, // some comment (probably for j)\n"
323 " int k); // some comment (probably for k)");
324 verifyFormat("void f(int i,\n"
325 " // some comment (probably for j)\n"
326 " int j,\n"
327 " // some comment (probably for k)\n"
328 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000329
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000330 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000331 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000332
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000333 verifyFormat("enum E {\n"
334 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000335 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000336 " VAL_B\n"
337 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000338
339 verifyFormat(
340 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000341 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000342 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
343 " // Comment inside a statement.\n"
344 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000345
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000346 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000347 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000348
349 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000350}
351
352TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000353 verifyFormat("f(/*test=*/ true);");
354}
355
Alexander Kornienko15757312012-12-06 18:03:27 +0000356//===----------------------------------------------------------------------===//
357// Tests for classes, namespaces, etc.
358//===----------------------------------------------------------------------===//
359
360TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000361 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000362}
363
364TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
365 verifyFormat("class A {\n"
366 "public:\n"
367 "protected:\n"
368 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000369 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000370 "};");
371 verifyGoogleFormat("class A {\n"
372 " public:\n"
373 " protected:\n"
374 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000375 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000376 "};");
377}
378
379TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000380 verifyFormat("class A : public B {};");
381 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000382}
383
Manuel Klimekde768542013-01-07 18:10:23 +0000384TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000385 verifyFormat("class A {} a, b;");
386 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000387 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000388}
389
Alexander Kornienko15757312012-12-06 18:03:27 +0000390TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000391 verifyFormat("enum {\n"
392 " Zero,\n"
393 " One = 1,\n"
394 " Two = One + 1,\n"
395 " Three = (One + Two),\n"
396 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
397 " Five = (One, Two, Three, Four, 5)\n"
398 "};");
399 verifyFormat("enum Enum {\n"
400 "};");
401 verifyFormat("enum {\n"
402 "};");
403}
404
Nico Weberefaddc02013-01-14 05:49:49 +0000405TEST_F(FormatTest, FormatsBitfields) {
406 verifyFormat("struct Bitfields {\n"
407 " unsigned sClass : 8;\n"
408 " unsigned ValueKind : 2;\n"
409 "};");
410}
411
Alexander Kornienko15757312012-12-06 18:03:27 +0000412TEST_F(FormatTest, FormatsNamespaces) {
413 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000414 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000415 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000416 "}");
417 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000418 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000419 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000420 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000421 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000422 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000423 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000424 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000425 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000426 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000427 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000428}
429
Nico Webera9ccdd12013-01-07 16:36:17 +0000430TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000431 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
432 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000433 verifyFormat("try {\n"
434 " throw a * b;\n"
435 "}\n"
436 "catch (int a) {\n"
437 " // Do nothing.\n"
438 "}\n"
439 "catch (...) {\n"
440 " exit(42);\n"
441 "}");
442
443 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000444 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000445 "catch (...) {\n"
446 " return 5;\n"
447 "}");
448 verifyFormat("class A {\n"
449 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000450 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000451 " catch (...) {\n"
452 " throw;\n"
453 " }\n"
454 "};\n");
455}
456
457TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000458 verifyFormat("@try {\n"
459 " f();\n"
460 "}\n"
461 "@catch (NSException e) {\n"
462 " @throw;\n"
463 "}\n"
464 "@finally {\n"
465 " exit(42);\n"
466 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000467}
468
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000469TEST_F(FormatTest, StaticInitializers) {
470 verifyFormat("static SomeClass SC = { 1, 'a' };");
471
472 // FIXME: Format like enums if the static initializer does not fit on a line.
473 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000474 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000475 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
476 "};");
477
478 verifyFormat(
479 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
480 " looooooooooooooooooooooooooooooooooongname,\n"
481 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000482}
483
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000484TEST_F(FormatTest, NestedStaticInitializers) {
485 verifyFormat("static A x = { { {} } };\n");
486 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000487 "static A x = { { { init1, init2, init3, init4 },\n"
488 " { init1, init2, init3, init4 } } };");
489
490 // FIXME: Fix this in general an verify that it works in LLVM style again.
491 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000492 "somes Status::global_reps[3] = {\n"
493 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
494 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
495 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
496 "};");
497 verifyFormat(
498 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
499 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
500 " } };");
501
502 // FIXME: We might at some point want to handle this similar to parameters
503 // lists, where we have an option to put each on a single line.
504 verifyFormat("struct {\n"
505 " unsigned bit;\n"
506 " const char *const name;\n"
507 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
508 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
509}
510
Manuel Klimeka080a182013-01-02 16:30:12 +0000511TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
512 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
513 " \\\n"
514 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
515}
516
Daniel Jasper71607512013-01-07 10:48:50 +0000517TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000518 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
519 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000520}
521
Manuel Klimeka080a182013-01-02 16:30:12 +0000522TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
523 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000524 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000525}
526
527TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
528 EXPECT_EQ("#line 42 \"test\"\n",
529 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000530 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000531 format("# \\\n define \\\n A \\\n B\n",
532 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000533}
534
535TEST_F(FormatTest, EndOfFileEndsPPDirective) {
536 EXPECT_EQ("#line 42 \"test\"",
537 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000538 EXPECT_EQ("#define A B",
539 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000540}
541
Manuel Klimek060143e2013-01-02 18:33:23 +0000542TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000543 // If the macro fits in one line, we still do not get the full
544 // line, as only the next line decides whether we need an escaped newline and
545 // thus use the last column.
546 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000547
Manuel Klimekd544c572013-01-07 09:24:17 +0000548 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
549 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000550 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000551
552 verifyFormat("#define A A\n#define A A");
553 verifyFormat("#define A(X) A\n#define A A");
554
555 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
556 verifyFormat("#define Something \\\n"
557 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000558}
559
560TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000561 EXPECT_EQ("// some comment\n"
562 "#include \"a.h\"\n"
563 "#define A(A,\\\n"
564 " B)\n"
565 "#include \"b.h\"\n"
566 "// some comment\n",
567 format(" // some comment\n"
568 " #include \"a.h\"\n"
569 "#define A(A,\\\n"
570 " B)\n"
571 " #include \"b.h\"\n"
572 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000573}
574
Manuel Klimekd4397b92013-01-04 23:34:14 +0000575TEST_F(FormatTest, LayoutSingleHash) {
576 EXPECT_EQ("#\na;", format("#\na;"));
577}
578
579TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
580 EXPECT_EQ("#define A \\\n"
581 " c; \\\n"
582 " e;\n"
583 "f;", format("#define A c; e;\n"
584 "f;", getLLVMStyleWithColumns(14)));
585}
586
587TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000588 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000589}
590
591TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000592 EXPECT_EQ("# define A\\\n b;",
593 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000594}
595
596TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000597 EXPECT_EQ("int x,\n"
598 "#define A\n"
599 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000600}
601
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000602TEST_F(FormatTest, HashInMacroDefinition) {
603 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
604 verifyFormat("#define A \\\n"
605 " { \\\n"
606 " f(#c);\\\n"
607 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000608
609 verifyFormat("#define A(X) \\\n"
610 " void function##X()", getLLVMStyleWithColumns(22));
611
612 verifyFormat("#define A(a, b, c) \\\n"
613 " void a##b##c()", getLLVMStyleWithColumns(22));
614
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000615 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000616}
617
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000618TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
619 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
620}
621
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000622TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000623 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000624}
625
Manuel Klimeka5342db2013-01-06 20:07:31 +0000626TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
627 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
628 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
629 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
630 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
631}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000632
Manuel Klimek95419382013-01-07 07:56:50 +0000633TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000634 EXPECT_EQ(
635 "#define A \\\n int i; \\\n int j;",
636 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000637}
638
Manuel Klimekd544c572013-01-07 09:24:17 +0000639TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
640 verifyFormat("#define A \\\n"
641 " int v( \\\n"
642 " a); \\\n"
643 " int i;", getLLVMStyleWithColumns(11));
644}
645
Manuel Klimeka080a182013-01-02 16:30:12 +0000646TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000647 EXPECT_EQ(
648 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
649 " \\\n"
650 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
651 "\n"
652 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
653 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
654 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
655 "\\\n"
656 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
657 " \n"
658 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
659 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000660}
661
Manuel Klimek526ed112013-01-09 15:25:02 +0000662TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
663 EXPECT_EQ("int\n"
664 "#define A\n"
665 " a;",
666 format("int\n#define A\na;"));
667 verifyFormat(
668 "functionCallTo(someOtherFunction(\n"
669 " withSomeParameters, whichInSequence,\n"
670 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000671 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000672 " withMoreParamters,\n"
673 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000674 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000675}
676
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000677TEST_F(FormatTest, LayoutBlockInsideParens) {
678 EXPECT_EQ("functionCall({\n"
679 " int i;\n"
680 "});", format(" functionCall ( {int i;} );"));
681}
682
683TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000684 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000685 "int i;", format(" SOME_MACRO {int i;} int i;"));
686}
687
688TEST_F(FormatTest, LayoutNestedBlocks) {
689 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
690 " struct s {\n"
691 " int i;\n"
692 " };\n"
693 " s kBitsToOs[] = { { 10 } };\n"
694 " for (int i = 0; i < 10; ++i)\n"
695 " return;\n"
696 "}");
697}
698
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000699TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
700 EXPECT_EQ("{}", format("{}"));
701}
702
Alexander Kornienko15757312012-12-06 18:03:27 +0000703//===----------------------------------------------------------------------===//
704// Line break tests.
705//===----------------------------------------------------------------------===//
706
707TEST_F(FormatTest, FormatsFunctionDefinition) {
708 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
709 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000710 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000711}
712
713TEST_F(FormatTest, FormatsAwesomeMethodCall) {
714 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000715 "SomeLongMethodName(SomeReallyLongMethod(\n"
716 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
717 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000718}
719
Daniel Jasper1321eb52012-12-18 21:05:13 +0000720TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000721 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000722 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
723 getLLVMStyleWithColumns(45));
724 verifyFormat("Constructor()\n"
725 " : Inttializer(FitsOnTheLine) {}",
726 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000727
728 verifyFormat(
729 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000730 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000731
732 verifyFormat(
733 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000734 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
735 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
736 verifyGoogleFormat(
737 "SomeClass::Constructor()\n"
738 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
739 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
740 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
741
742 verifyFormat(
743 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000744 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000745 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000746
747 verifyFormat("Constructor()\n"
748 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
749 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
750 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000751 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000752
753 // Here a line could be saved by splitting the second initializer onto two
754 // lines, but that is not desireable.
755 verifyFormat("Constructor()\n"
756 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
757 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000758 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000759
760 verifyGoogleFormat("MyClass::MyClass(int var)\n"
761 " : some_var_(var), // 4 space indent\n"
762 " some_other_var_(var + 1) { // lined up\n"
763 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000764
765 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000766 std::string input = "Constructor()\n"
767 " : aaaa(a,\n";
768 for (unsigned i = 0, e = 80; i != e; ++i) {
769 input += " a,\n";
770 }
771 input += " a) {}";
772 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000773}
774
Alexander Kornienko15757312012-12-06 18:03:27 +0000775TEST_F(FormatTest, BreaksAsHighAsPossible) {
776 verifyFormat(
777 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
778 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
779 " f();");
780}
781
Daniel Jasperbac016b2012-12-03 18:12:45 +0000782TEST_F(FormatTest, BreaksDesireably) {
783 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
784 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000785 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000786
787 verifyFormat(
788 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000789 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000790
791 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000794
795 verifyFormat(
796 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
797 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
798 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000800
801 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
802 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
803
Daniel Jasper723f0302013-01-02 14:40:02 +0000804 verifyFormat(
805 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
807
Daniel Jasper33182dd2012-12-05 14:57:28 +0000808 // This test case breaks on an incorrect memoization, i.e. an optimization not
809 // taking into account the StopAt value.
810 verifyFormat(
811 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000812 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
813 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
814 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000815
Daniel Jaspercd162382013-01-07 13:26:07 +0000816 verifyFormat("{\n {\n {\n"
817 " Annotation.SpaceRequiredBefore =\n"
818 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
819 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
820 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000821}
822
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000823TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
824 verifyGoogleFormat(
825 "aaaaaaaa(aaaaaaaaaaaaa,\n"
826 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
827 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
828 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
830 verifyGoogleFormat(
831 "aaaaaaaaaaaaaaa(aaaaaaaaa,\n"
832 " aaaaaaaaa,\n"
833 " aaaaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();");
834 verifyGoogleFormat(
835 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
836 " ddddddddddddddddddddddddddddd),\n"
837 " test);");
838
839 verifyGoogleFormat(
840 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
841 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
842 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
843 verifyGoogleFormat("a(\"a\"\n"
844 " \"a\",\n"
845 " a);");
846}
847
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000848TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
849 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
850 " GUARDED_BY(aaaaaaaaaaaaa);");
851}
852
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000853TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
854 verifyFormat(
855 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000856 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000857 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000858 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000859 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000860 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000861 verifyFormat(
862 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000863 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000864}
865
Daniel Jasper9cda8002013-01-07 13:08:40 +0000866TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
867 verifyFormat(
868 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
869 " SI->getAlignment(),\n"
870 " SI->getPointerAddressSpaceee());\n");
871 verifyFormat(
872 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
873 " Line.Tokens.front().Tok.getLocation(),\n"
874 " Line.Tokens.back().Tok.getLocation());");
875}
876
Daniel Jaspercf225b62012-12-24 13:43:52 +0000877TEST_F(FormatTest, AlignsAfterAssignments) {
878 verifyFormat(
879 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000880 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000881 verifyFormat(
882 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000883 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000884 verifyFormat(
885 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000886 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000887 verifyFormat(
888 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000889 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000890 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000891 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
892 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
893 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000894}
895
896TEST_F(FormatTest, AlignsAfterReturn) {
897 verifyFormat(
898 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
899 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
900 verifyFormat(
901 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
902 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
903}
904
Daniel Jasper9c837d02013-01-09 07:06:56 +0000905TEST_F(FormatTest, BreaksConditionalExpressions) {
906 verifyFormat(
907 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
908 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
910 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
911 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
912}
913
Nico Weber7d37b8b2013-01-12 01:28:06 +0000914TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
915 verifyFormat("arr[foo ? bar : baz];");
916 verifyFormat("f()[foo ? bar : baz];");
917 verifyFormat("(a + b)[foo ? bar : baz];");
918 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
919}
920
Daniel Jasperbac016b2012-12-03 18:12:45 +0000921TEST_F(FormatTest, AlignsStringLiterals) {
922 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
923 " \"short literal\");");
924 verifyFormat(
925 "looooooooooooooooooooooooongFunction(\n"
926 " \"short literal\"\n"
927 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
928}
929
Alexander Kornienko15757312012-12-06 18:03:27 +0000930TEST_F(FormatTest, AlignsPipes) {
931 verifyFormat(
932 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
933 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
934 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
935 verifyFormat(
936 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
937 " << aaaaaaaaaaaaaaaaaaaa;");
938 verifyFormat(
939 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
940 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
941 verifyFormat(
942 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
943 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
944 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
945 verifyFormat(
946 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
947 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
948 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
949}
950
Daniel Jasperbac016b2012-12-03 18:12:45 +0000951TEST_F(FormatTest, UnderstandsEquals) {
952 verifyFormat(
953 "aaaaaaaaaaaaaaaaa =\n"
954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
955 verifyFormat(
956 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000957 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000958 verifyFormat(
959 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000960 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000961 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000963
Daniel Jasper9cda8002013-01-07 13:08:40 +0000964 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000965 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000966 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000967 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000968}
969
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000970TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000971 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
972 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000973
Daniel Jasper1321eb52012-12-18 21:05:13 +0000974 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
975 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000976
977 verifyFormat(
978 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
979 " Parameter2);");
980
981 verifyFormat(
982 "ShortObject->shortFunction(\n"
983 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
984 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
985
986 verifyFormat("loooooooooooooongFunction(\n"
987 " LoooooooooooooongObject->looooooooooooooooongFunction());");
988
989 verifyFormat(
990 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
991 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
992
Daniel Jasper46a46a22013-01-07 07:13:20 +0000993 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000994 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000995 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000996 verifyFormat(
997 "aaaaaaaaaaa->aaaaaaaaa(\n"
998 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
999 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001000}
1001
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001002TEST_F(FormatTest, WrapsTemplateDeclarations) {
1003 verifyFormat("template <typename T>\n"
1004 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1005 verifyFormat(
1006 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1007 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
1008 verifyFormat(
1009 "template <typename T>\n"
1010 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1011 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001012 verifyFormat(
1013 "template <typename T>\n"
1014 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1015 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1016 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001017 verifyFormat("template <typename T>\n"
1018 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1019 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001020 verifyFormat(
1021 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1022 " typename T4 = char>\n"
1023 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001024}
1025
Daniel Jasperbac016b2012-12-03 18:12:45 +00001026TEST_F(FormatTest, UnderstandsTemplateParameters) {
1027 verifyFormat("A<int> a;");
1028 verifyFormat("A<A<A<int> > > a;");
1029 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1030 verifyFormat("bool x = a < 1 || 2 > a;");
1031 verifyFormat("bool x = 5 < f<int>();");
1032 verifyFormat("bool x = f<int>() > 5;");
1033 verifyFormat("bool x = 5 < a<int>::x;");
1034 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1035 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1036
1037 verifyGoogleFormat("A<A<int>> a;");
1038 verifyGoogleFormat("A<A<A<int>>> a;");
1039 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1040
1041 verifyFormat("test >> a >> b;");
1042 verifyFormat("test << a >> b;");
1043
1044 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001045 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001046}
1047
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001048TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001049 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001050 verifyFormat("f(-1, -2, -3);");
1051 verifyFormat("a[-1] = 5;");
1052 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001053 verifyFormat("if (i == -1) {}");
1054 verifyFormat("if (i != -1) {}");
1055 verifyFormat("if (i > -1) {}");
1056 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001057 verifyFormat("++(a->f());");
1058 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001059 verifyFormat("(a->f())++;");
1060 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001061 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001062
1063 verifyFormat("a-- > b;");
1064 verifyFormat("b ? -a : c;");
1065 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001066 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001067 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001068 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001069
1070 verifyFormat("return -1;");
1071 verifyFormat("switch (a) {\n"
1072 "case -1:\n"
1073 " break;\n"
1074 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001075
1076 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1077 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001078
1079 verifyFormat("int a = /* confusing comment */ -1;");
1080 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1081 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001082}
1083
1084TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001085 verifyFormat("bool operator<();");
1086 verifyFormat("bool operator>();");
1087 verifyFormat("bool operator=();");
1088 verifyFormat("bool operator==();");
1089 verifyFormat("bool operator!=();");
1090 verifyFormat("int operator+();");
1091 verifyFormat("int operator++();");
1092 verifyFormat("bool operator();");
1093 verifyFormat("bool operator()();");
1094 verifyFormat("bool operator[]();");
1095 verifyFormat("operator bool();");
1096 verifyFormat("operator SomeType<int>();");
1097 verifyFormat("void *operator new(std::size_t size);");
1098 verifyFormat("void *operator new[](std::size_t size);");
1099 verifyFormat("void operator delete(void *ptr);");
1100 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001101}
1102
Daniel Jasper088dab52013-01-11 16:09:04 +00001103TEST_F(FormatTest, UnderstandsNewAndDelete) {
1104 verifyFormat("A *a = new A;");
1105 verifyFormat("A *a = new (placement) A;");
1106 verifyFormat("delete a;");
1107 verifyFormat("delete (A *)a;");
1108}
1109
Daniel Jasper5d334402013-01-02 08:57:10 +00001110TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001111 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001112 verifyFormat("f(a, *a);");
1113 verifyFormat("f(*a);");
1114 verifyFormat("int a = b * 10;");
1115 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001116 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001117 verifyFormat("int a += b * c;");
1118 verifyFormat("int a -= b * c;");
1119 verifyFormat("int a *= b * c;");
1120 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001121 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001122 verifyFormat("int a = *b * c;");
1123 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001124 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001125 verifyFormat("return 10 * b;");
1126 verifyFormat("return *b * *c;");
1127 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001128 verifyFormat("f(b ? *c : *d);");
1129 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001130 verifyFormat("*b = a;");
1131 verifyFormat("a * ~b;");
1132 verifyFormat("a * !b;");
1133 verifyFormat("a * +b;");
1134 verifyFormat("a * -b;");
1135 verifyFormat("a * ++b;");
1136 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001137 verifyFormat("a[4] * b;");
1138 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001139 verifyFormat("a * [self dostuff];");
1140 verifyFormat("a * (a + b);");
1141 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001142 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001143
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001144 verifyFormat("InvalidRegions[*R] = 0;");
1145
Daniel Jasper8b39c662012-12-10 18:59:13 +00001146 verifyFormat("A<int *> a;");
1147 verifyFormat("A<int **> a;");
1148 verifyFormat("A<int *, int *> a;");
1149 verifyFormat("A<int **, int **> a;");
1150
Daniel Jasper2db356d2013-01-08 20:03:18 +00001151 verifyFormat(
1152 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1154
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001155 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001156 verifyGoogleFormat("A<int*> a;");
1157 verifyGoogleFormat("A<int**> a;");
1158 verifyGoogleFormat("A<int*, int*> a;");
1159 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001160 verifyGoogleFormat("f(b ? *c : *d);");
1161 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001162
1163 verifyFormat("a = *(x + y);");
1164 verifyFormat("a = &(x + y);");
1165 verifyFormat("*(x + y).call();");
1166 verifyFormat("&(x + y)->call();");
1167 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001168
1169 verifyFormat("f(b * /* confusing comment */ ++c);");
1170 verifyFormat(
1171 "int *MyValues = {\n"
1172 " *A, // Operator detection might be confused by the '{'\n"
1173 " *BB // Operator detection might be confused by previous comment\n"
1174 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001175}
1176
Daniel Jasper4981bd02013-01-13 08:01:36 +00001177TEST_F(FormatTest, FormatsCasts) {
1178 verifyFormat("Type *A = static_cast<Type *>(P);");
1179 verifyFormat("Type *A = (Type *)P;");
1180 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1181 verifyFormat("int a = (int)(2.0f);");
1182
1183 // FIXME: These also need to be identified.
1184 verifyFormat("int a = (int) 2.0f;");
1185 verifyFormat("int a = (int) * b;");
1186
1187 // These are not casts.
1188 verifyFormat("void f(int *) {}");
1189 verifyFormat("void f(int *);");
1190 verifyFormat("void f(int *) = 0;");
1191 verifyFormat("void f(SmallVector<int>) {}");
1192 verifyFormat("void f(SmallVector<int>);");
1193 verifyFormat("void f(SmallVector<int>) = 0;");
1194}
1195
Daniel Jasper46ef8522013-01-10 13:08:12 +00001196TEST_F(FormatTest, FormatsFunctionTypes) {
1197 // FIXME: Determine the cases that need a space after the return type and fix.
1198 verifyFormat("A<bool()> a;");
1199 verifyFormat("A<SomeType()> a;");
1200 verifyFormat("A<void(*)(int, std::string)> a;");
1201
1202 verifyFormat("int(*func)(void *);");
1203}
1204
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001205TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001206 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001207 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001208 verifyFormat(
1209 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1210 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001211 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001212}
1213
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001214TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1215 verifyFormat("(a)->b();");
1216 verifyFormat("--a;");
1217}
1218
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001219TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001220 verifyFormat("#include <string>\n"
1221 "#include <a/b/c.h>\n"
1222 "#include \"a/b/string\"\n"
1223 "#include \"string.h\"\n"
1224 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001225 "#include <a-a>\n"
1226 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001227
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001228 verifyFormat("#import <string>");
1229 verifyFormat("#import <a/b/c.h>");
1230 verifyFormat("#import \"a/b/string\"");
1231 verifyFormat("#import \"string.h\"");
1232 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001233}
1234
Alexander Kornienko15757312012-12-06 18:03:27 +00001235//===----------------------------------------------------------------------===//
1236// Error recovery tests.
1237//===----------------------------------------------------------------------===//
1238
Daniel Jasper700e7102013-01-10 09:26:47 +00001239TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001240 verifyFormat("void f() { return; }\n42");
1241 verifyFormat("void f() {\n"
1242 " if (0)\n"
1243 " return;\n"
1244 "}\n"
1245 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001246 verifyFormat("void f() { return }\n42");
1247 verifyFormat("void f() {\n"
1248 " if (0)\n"
1249 " return\n"
1250 "}\n"
1251 "42");
1252}
1253
1254TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1255 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1256 EXPECT_EQ("void f() {\n"
1257 " if (a)\n"
1258 " return\n"
1259 "}", format("void f ( ) { if ( a ) return }"));
1260 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1261 EXPECT_EQ("namespace N {\n"
1262 "void f() {}\n"
1263 "void g()\n"
1264 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001265}
1266
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001267TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1268 verifyFormat("int aaaaaaaa =\n"
1269 " // Overly long comment\n"
1270 " b;", getLLVMStyleWithColumns(20));
1271 verifyFormat("function(\n"
1272 " ShortArgument,\n"
1273 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1274}
1275
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001276TEST_F(FormatTest, IncorrectAccessSpecifier) {
1277 verifyFormat("public:");
1278 verifyFormat("class A {\n"
1279 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001280 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001281 "};");
1282 verifyFormat("public\n"
1283 "int qwerty;");
1284 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001285 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001286 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001287 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001288 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001289 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001290}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001291
Alexander Kornienko393b0082012-12-04 15:40:36 +00001292TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1293 verifyFormat("{");
1294}
1295
1296TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001297 verifyFormat("do {}");
1298 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001299 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001300 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001301 "wheeee(fun);");
1302 verifyFormat("do {\n"
1303 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001304 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001305}
1306
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001307TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001308 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001309 verifyFormat("switch {\n foo;\n foo();\n}");
1310 verifyFormat("for {\n foo;\n foo();\n}");
1311 verifyFormat("while {\n foo;\n foo();\n}");
1312 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001313}
1314
Daniel Jasper1f42f112013-01-04 18:52:56 +00001315TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1316 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001317 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001318}
1319
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001320TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001321 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1322 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1323 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1324 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001325
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001326 EXPECT_EQ("{\n"
1327 " {\n"
1328 " breakme(\n"
1329 " qwe);\n"
1330 "}\n", format("{\n"
1331 " {\n"
1332 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001333 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001334}
1335
Manuel Klimek2851c162013-01-10 14:36:46 +00001336TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1337 verifyFormat(
1338 "int x = {\n"
1339 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001340 " b(alongervariable)\n"
1341 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001342}
1343
1344TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1345 verifyFormat(
1346 "Aaa({\n"
1347 " int i;\n"
1348 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1349 " ccccccccccccccccc));");
1350}
1351
Manuel Klimek517e8942013-01-11 17:54:10 +00001352TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1353 verifyFormat("void f() { return 42; }");
1354 verifyFormat("void f() {\n"
1355 " // Comment\n"
1356 "}");
1357 verifyFormat("{\n"
1358 "#error {\n"
1359 " int a;\n"
1360 "}");
1361 verifyFormat("{\n"
1362 " int a;\n"
1363 "#error {\n"
1364 "}");
1365}
1366
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001367TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1368 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001369 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001370 verifyFormat("class foo a = { bar };\nint n;");
1371 verifyFormat("union foo a = { bar };\nint n;");
1372
1373 // Elaborate types inside function definitions.
1374 verifyFormat("struct foo f() {}\nint n;");
1375 verifyFormat("class foo f() {}\nint n;");
1376 verifyFormat("union foo f() {}\nint n;");
1377
1378 // Templates.
1379 verifyFormat("template <class X> void f() {}\nint n;");
1380 verifyFormat("template <struct X> void f() {}\nint n;");
1381 verifyFormat("template <union X> void f() {}\nint n;");
1382
1383 // Actual definitions...
1384 verifyFormat("struct {} n;");
1385 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1386 verifyFormat("union Z {\n int n;\n} x;");
1387 verifyFormat("class MACRO Z {} n;");
1388 verifyFormat("class MACRO(X) Z {} n;");
1389 verifyFormat("class __attribute__(X) Z {} n;");
1390 verifyFormat("class __declspec(X) Z {} n;");
1391
1392 // Elaborate types where incorrectly parsing the structural element would
1393 // break the indent.
1394 verifyFormat("if (true)\n"
1395 " class X x;\n"
1396 "else\n"
1397 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001398}
1399
Manuel Klimek407a31a2013-01-15 15:50:27 +00001400TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1401 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1402 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1403 EXPECT_EQ("#error 1", format(" # error 1"));
1404 EXPECT_EQ("#warning 1", format(" # warning 1"));
1405}
1406
Manuel Klimek517e8942013-01-11 17:54:10 +00001407// FIXME: This breaks the order of the unwrapped lines:
1408// TEST_F(FormatTest, OrderUnwrappedLines) {
1409// verifyFormat("{\n"
1410// " bool a; //\n"
1411// "#error {\n"
1412// " int a;\n"
1413// "}");
1414// }
1415
Nico Webercf4a79c2013-01-08 17:56:31 +00001416//===----------------------------------------------------------------------===//
1417// Objective-C tests.
1418//===----------------------------------------------------------------------===//
1419
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001420TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1421 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1422 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1423 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001424 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001425 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1426 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1427 format("-(NSInteger)Method3:(id)anObject;"));
1428 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1429 format("-(NSInteger)Method4:(id)anObject;"));
1430 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1431 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1432 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1433 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001434 EXPECT_EQ(
1435 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1436 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001437
1438 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001439 EXPECT_EQ(
1440 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1441 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1442 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1443 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1444 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1445 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1446 format(
1447 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1448 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1449 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1450 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1451 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1452 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001453
1454 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001455 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001456 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1457 // protocol lists (but not for template classes):
1458 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001459
1460 verifyFormat("- (int(*)())foo:(int(*)())f;");
1461 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1462
1463 // If there's no return type (very rare in practice!), LLVM and Google style
1464 // agree.
1465 verifyFormat("- foo:(int)f;");
1466 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001467}
1468
Daniel Jasper886568d2013-01-09 08:36:49 +00001469TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001470 verifyFormat("int (^Block)(int, int);");
1471 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001472}
1473
Nico Weber27d13672013-01-09 20:25:35 +00001474TEST_F(FormatTest, FormatObjCInterface) {
1475 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001476 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001477 "@public\n"
1478 " int field1;\n"
1479 "@protected\n"
1480 " int field2;\n"
1481 "@private\n"
1482 " int field3;\n"
1483 "@package\n"
1484 " int field4;\n"
1485 "}\n"
1486 "+ (id)init;\n"
1487 "@end");
1488
Nico Weber27d13672013-01-09 20:25:35 +00001489 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1490 " @public\n"
1491 " int field1;\n"
1492 " @protected\n"
1493 " int field2;\n"
1494 " @private\n"
1495 " int field3;\n"
1496 " @package\n"
1497 " int field4;\n"
1498 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001499 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001500 "@end");
1501
1502 verifyFormat("@interface Foo\n"
1503 "+ (id)init;\n"
1504 "// Look, a comment!\n"
1505 "- (int)answerWith:(int)i;\n"
1506 "@end");
1507
1508 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001509 "@end\n"
1510 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001511 "@end");
1512
1513 verifyFormat("@interface Foo : Bar\n"
1514 "+ (id)init;\n"
1515 "@end");
1516
Nico Weber5f500df2013-01-10 20:12:55 +00001517 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001518 "+ (id)init;\n"
1519 "@end");
1520
Nico Weber5f500df2013-01-10 20:12:55 +00001521 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001522 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001523 "@end");
1524
Nico Webered91bba2013-01-10 19:19:14 +00001525 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001526 "+ (id)init;\n"
1527 "@end");
1528
Nico Webered91bba2013-01-10 19:19:14 +00001529 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001530 "+ (id)init;\n"
1531 "@end");
1532
Nico Weber5f500df2013-01-10 20:12:55 +00001533 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001534 "+ (id)init;\n"
1535 "@end");
1536
Nico Weber5f500df2013-01-10 20:12:55 +00001537 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001538 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001539 "@end");
1540
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001541 verifyFormat("@interface Foo {\n"
1542 " int _i;\n"
1543 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001544 "+ (id)init;\n"
1545 "@end");
1546
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001547 verifyFormat("@interface Foo : Bar {\n"
1548 " int _i;\n"
1549 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001550 "+ (id)init;\n"
1551 "@end");
1552
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001553 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1554 " int _i;\n"
1555 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001556 "+ (id)init;\n"
1557 "@end");
1558
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001559 verifyFormat("@interface Foo (HackStuff) {\n"
1560 " int _i;\n"
1561 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001562 "+ (id)init;\n"
1563 "@end");
1564
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001565 verifyFormat("@interface Foo () {\n"
1566 " int _i;\n"
1567 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001568 "+ (id)init;\n"
1569 "@end");
1570
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001571 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1572 " int _i;\n"
1573 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001574 "+ (id)init;\n"
1575 "@end");
1576}
1577
Nico Weber50767d82013-01-09 23:25:37 +00001578TEST_F(FormatTest, FormatObjCImplementation) {
1579 verifyFormat("@implementation Foo : NSObject {\n"
1580 "@public\n"
1581 " int field1;\n"
1582 "@protected\n"
1583 " int field2;\n"
1584 "@private\n"
1585 " int field3;\n"
1586 "@package\n"
1587 " int field4;\n"
1588 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001589 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001590 "@end");
1591
1592 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1593 " @public\n"
1594 " int field1;\n"
1595 " @protected\n"
1596 " int field2;\n"
1597 " @private\n"
1598 " int field3;\n"
1599 " @package\n"
1600 " int field4;\n"
1601 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001602 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001603 "@end");
1604
1605 verifyFormat("@implementation Foo\n"
1606 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001607 " if (true)\n"
1608 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001609 "}\n"
1610 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001611 "- (int)answerWith:(int)i {\n"
1612 " return i;\n"
1613 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001614 "+ (int)answerWith:(int)i {\n"
1615 " return i;\n"
1616 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001617 "@end");
1618
1619 verifyFormat("@implementation Foo\n"
1620 "@end\n"
1621 "@implementation Bar\n"
1622 "@end");
1623
1624 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001625 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001626 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001627 "@end");
1628
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001629 verifyFormat("@implementation Foo {\n"
1630 " int _i;\n"
1631 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001632 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001633 "@end");
1634
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001635 verifyFormat("@implementation Foo : Bar {\n"
1636 " int _i;\n"
1637 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001638 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001639 "@end");
1640
Nico Webered91bba2013-01-10 19:19:14 +00001641 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001642 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001643 "@end");
1644}
1645
Nico Weber1abe6ea2013-01-09 21:15:03 +00001646TEST_F(FormatTest, FormatObjCProtocol) {
1647 verifyFormat("@protocol Foo\n"
1648 "@property(weak) id delegate;\n"
1649 "- (NSUInteger)numberOfThings;\n"
1650 "@end");
1651
Nico Weber5f500df2013-01-10 20:12:55 +00001652 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001653 "- (NSUInteger)numberOfThings;\n"
1654 "@end");
1655
Nico Weber5f500df2013-01-10 20:12:55 +00001656 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001657 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001658 "@end");
1659
Nico Weber1abe6ea2013-01-09 21:15:03 +00001660 verifyFormat("@protocol Foo;\n"
1661 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001662
1663 verifyFormat("@protocol Foo\n"
1664 "@end\n"
1665 "@protocol Bar\n"
1666 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001667
1668 verifyFormat("@protocol myProtocol\n"
1669 "- (void)mandatoryWithInt:(int)i;\n"
1670 "@optional\n"
1671 "- (void)optional;\n"
1672 "@required\n"
1673 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001674 "@optional\n"
1675 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001676 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001677}
1678
Nico Weberbcfdd262013-01-12 06:18:40 +00001679TEST_F(FormatTest, FormatObjCMethodExpr) {
1680 verifyFormat("[foo bar:baz];");
1681 verifyFormat("return [foo bar:baz];");
1682 verifyFormat("f([foo bar:baz]);");
1683 verifyFormat("f(2, [foo bar:baz]);");
1684 verifyFormat("f(2, a ? b : c);");
1685 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1686
1687 verifyFormat("[foo bar:baz], [foo bar:baz];");
1688 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1689 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1690 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1691 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1692 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1693 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1694 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1695 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1696 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1697 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1698 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1699 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1700 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1701 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1702 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1703 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1704 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1705 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1706 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1707 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
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 // Whew!
1719
1720 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1721 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1722 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1723 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1724 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001725 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001726 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001727
Nico Weberbcfdd262013-01-12 06:18:40 +00001728 verifyFormat("arr[[self indexForFoo:a]];");
1729 verifyFormat("throw [self errorFor:a];");
1730 verifyFormat("@throw [self errorFor:a];");
1731
Nico Webere8ccc812013-01-12 22:48:47 +00001732 // This tests that the formatter doesn't break after "backing" but before ":",
1733 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001734 verifyFormat(
1735 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001736 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1737 " backing:NSBackingStoreBuffered defer:YES]))");
1738
Nico Webere8ccc812013-01-12 22:48:47 +00001739 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1740 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001741
1742}
1743
Nico Weber581f5572013-01-07 15:56:25 +00001744TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001745 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001746 verifyFormat("@catch");
1747 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001748 verifyFormat("@compatibility_alias");
1749 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001750 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001751 verifyFormat("@encode");
1752 verifyFormat("@end");
1753 verifyFormat("@finally");
1754 verifyFormat("@implementation");
1755 verifyFormat("@import");
1756 verifyFormat("@interface");
1757 verifyFormat("@optional");
1758 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001759 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001760 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001761 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001762 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001763 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001764 verifyFormat("@required");
1765 verifyFormat("@selector");
1766 verifyFormat("@synchronized");
1767 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001768 verifyFormat("@throw");
1769 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001770
Nico Webercb4d6902013-01-08 19:40:21 +00001771 verifyFormat("@\"String\"");
1772 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001773 verifyFormat("@+4.8");
1774 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001775 verifyFormat("@1LL");
1776 verifyFormat("@.5");
1777 verifyFormat("@'c'");
1778 verifyFormat("@true");
1779 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001780 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001781 verifyFormat("@[");
1782 verifyFormat("@{");
1783
Nico Weber581f5572013-01-07 15:56:25 +00001784 EXPECT_EQ("@interface", format("@ interface"));
1785
1786 // The precise formatting of this doesn't matter, nobody writes code like
1787 // this.
1788 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001789}
1790
Nico Weberc31689a2013-01-08 19:15:23 +00001791TEST_F(FormatTest, ObjCSnippets) {
1792 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001793 verifyFormat("@autoreleasepool {\n"
1794 " foo();\n"
1795 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001796 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001797 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001798 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001799 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001800 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001801 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001802 verifyFormat("@synchronized(self) {\n"
1803 " f();\n"
1804 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001805
Nico Weber70848232013-01-10 21:30:42 +00001806 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1807 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1808
Nico Webercf4a79c2013-01-08 17:56:31 +00001809 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001810 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1811 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001812}
1813
Daniel Jaspercd162382013-01-07 13:26:07 +00001814} // end namespace tooling
1815} // end namespace clang