blob: efea545d320e18c6995b6534b932c594ef939fcd [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(
487 "static A x = {\n"
488 " { { init1, init2, init3, init4 }, { init1, init2, init3, init4 } }\n"
489 "};\n");
490 verifyFormat(
491 "somes Status::global_reps[3] = {\n"
492 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
493 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
494 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
495 "};");
496 verifyFormat(
497 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
498 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
499 " } };");
500
501 // FIXME: We might at some point want to handle this similar to parameters
502 // lists, where we have an option to put each on a single line.
503 verifyFormat("struct {\n"
504 " unsigned bit;\n"
505 " const char *const name;\n"
506 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
507 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
508}
509
Manuel Klimeka080a182013-01-02 16:30:12 +0000510TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
511 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
512 " \\\n"
513 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
514}
515
Daniel Jasper71607512013-01-07 10:48:50 +0000516TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000517 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
518 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000519}
520
Manuel Klimeka080a182013-01-02 16:30:12 +0000521TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
522 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000523 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000524}
525
526TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
527 EXPECT_EQ("#line 42 \"test\"\n",
528 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000529 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000530 format("# \\\n define \\\n A \\\n B\n",
531 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000532}
533
534TEST_F(FormatTest, EndOfFileEndsPPDirective) {
535 EXPECT_EQ("#line 42 \"test\"",
536 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000537 EXPECT_EQ("#define A B",
538 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000539}
540
Manuel Klimek060143e2013-01-02 18:33:23 +0000541TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000542 // If the macro fits in one line, we still do not get the full
543 // line, as only the next line decides whether we need an escaped newline and
544 // thus use the last column.
545 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000546
Manuel Klimekd544c572013-01-07 09:24:17 +0000547 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
548 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000549 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000550
551 verifyFormat("#define A A\n#define A A");
552 verifyFormat("#define A(X) A\n#define A A");
553
554 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
555 verifyFormat("#define Something \\\n"
556 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000557}
558
559TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000560 EXPECT_EQ("// some comment\n"
561 "#include \"a.h\"\n"
562 "#define A(A,\\\n"
563 " B)\n"
564 "#include \"b.h\"\n"
565 "// some comment\n",
566 format(" // some comment\n"
567 " #include \"a.h\"\n"
568 "#define A(A,\\\n"
569 " B)\n"
570 " #include \"b.h\"\n"
571 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000572}
573
Manuel Klimekd4397b92013-01-04 23:34:14 +0000574TEST_F(FormatTest, LayoutSingleHash) {
575 EXPECT_EQ("#\na;", format("#\na;"));
576}
577
578TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
579 EXPECT_EQ("#define A \\\n"
580 " c; \\\n"
581 " e;\n"
582 "f;", format("#define A c; e;\n"
583 "f;", getLLVMStyleWithColumns(14)));
584}
585
586TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000587 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000588}
589
590TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000591 EXPECT_EQ("# define A\\\n b;",
592 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000593}
594
595TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000596 EXPECT_EQ("int x,\n"
597 "#define A\n"
598 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000599}
600
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000601TEST_F(FormatTest, HashInMacroDefinition) {
602 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
603 verifyFormat("#define A \\\n"
604 " { \\\n"
605 " f(#c);\\\n"
606 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000607
608 verifyFormat("#define A(X) \\\n"
609 " void function##X()", getLLVMStyleWithColumns(22));
610
611 verifyFormat("#define A(a, b, c) \\\n"
612 " void a##b##c()", getLLVMStyleWithColumns(22));
613
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000614 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000615}
616
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000617TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
618 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
619}
620
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000621TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000622 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000623}
624
Manuel Klimeka5342db2013-01-06 20:07:31 +0000625TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
626 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
627 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
628 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
629 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
630}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000631
Manuel Klimek95419382013-01-07 07:56:50 +0000632TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000633 EXPECT_EQ(
634 "#define A \\\n int i; \\\n int j;",
635 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000636}
637
Manuel Klimekd544c572013-01-07 09:24:17 +0000638TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
639 verifyFormat("#define A \\\n"
640 " int v( \\\n"
641 " a); \\\n"
642 " int i;", getLLVMStyleWithColumns(11));
643}
644
Manuel Klimeka080a182013-01-02 16:30:12 +0000645TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000646 EXPECT_EQ(
647 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
648 " \\\n"
649 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
650 "\n"
651 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
652 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
653 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
654 "\\\n"
655 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
656 " \n"
657 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
658 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000659}
660
Manuel Klimek526ed112013-01-09 15:25:02 +0000661TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
662 EXPECT_EQ("int\n"
663 "#define A\n"
664 " a;",
665 format("int\n#define A\na;"));
666 verifyFormat(
667 "functionCallTo(someOtherFunction(\n"
668 " withSomeParameters, whichInSequence,\n"
669 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000670 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000671 " withMoreParamters,\n"
672 " whichStronglyInfluenceTheLayout),\n"
673 " andMoreParameters),\n"
674 " trailing);", getLLVMStyleWithColumns(69));
675}
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.
766 verifyGoogleFormat(
767 "Constructor()\n"
768 " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
769 " a, a, a,\n"
770 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
771 " a, a, a,\n"
772 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
773 " a, a, a,\n"
774 " a, a, a, a, a, a, a, a, a, a, a)\n"
775 " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
776 " a, a, a,\n"
777 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
778 " a, a, a,\n"
779 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,"
780 " a, a, a,\n"
781 " a, a, a, a, a, a, a, a, a, a, a) {}\n");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000782}
783
Alexander Kornienko15757312012-12-06 18:03:27 +0000784TEST_F(FormatTest, BreaksAsHighAsPossible) {
785 verifyFormat(
786 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
787 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
788 " f();");
789}
790
Daniel Jasperbac016b2012-12-03 18:12:45 +0000791TEST_F(FormatTest, BreaksDesireably) {
792 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
793 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000794 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000795
796 verifyFormat(
797 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000799
800 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000803
804 verifyFormat(
805 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
807 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
808 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000809
810 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
811 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
812
Daniel Jasper723f0302013-01-02 14:40:02 +0000813 verifyFormat(
814 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
816
Daniel Jasper33182dd2012-12-05 14:57:28 +0000817 // This test case breaks on an incorrect memoization, i.e. an optimization not
818 // taking into account the StopAt value.
819 verifyFormat(
820 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000821 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
822 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
823 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000824
Daniel Jaspercd162382013-01-07 13:26:07 +0000825 verifyFormat("{\n {\n {\n"
826 " Annotation.SpaceRequiredBefore =\n"
827 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
828 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
829 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000830}
831
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000832TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
833 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
834 " GUARDED_BY(aaaaaaaaaaaaa);");
835}
836
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000837TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
838 verifyFormat(
839 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000840 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000841 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000842 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000843 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000844 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000845 verifyFormat(
846 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000847 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000848}
849
Daniel Jasper9cda8002013-01-07 13:08:40 +0000850TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
851 verifyFormat(
852 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
853 " SI->getAlignment(),\n"
854 " SI->getPointerAddressSpaceee());\n");
855 verifyFormat(
856 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
857 " Line.Tokens.front().Tok.getLocation(),\n"
858 " Line.Tokens.back().Tok.getLocation());");
859}
860
Daniel Jaspercf225b62012-12-24 13:43:52 +0000861TEST_F(FormatTest, AlignsAfterAssignments) {
862 verifyFormat(
863 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000864 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000865 verifyFormat(
866 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000867 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000868 verifyFormat(
869 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000870 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000871 verifyFormat(
872 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000873 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000874 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000875 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
876 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
877 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000878}
879
880TEST_F(FormatTest, AlignsAfterReturn) {
881 verifyFormat(
882 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
883 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
884 verifyFormat(
885 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
886 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
887}
888
Daniel Jasper9c837d02013-01-09 07:06:56 +0000889TEST_F(FormatTest, BreaksConditionalExpressions) {
890 verifyFormat(
891 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
892 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
894 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
895 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
896}
897
Nico Weber7d37b8b2013-01-12 01:28:06 +0000898TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
899 verifyFormat("arr[foo ? bar : baz];");
900 verifyFormat("f()[foo ? bar : baz];");
901 verifyFormat("(a + b)[foo ? bar : baz];");
902 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
903}
904
Daniel Jasperbac016b2012-12-03 18:12:45 +0000905TEST_F(FormatTest, AlignsStringLiterals) {
906 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
907 " \"short literal\");");
908 verifyFormat(
909 "looooooooooooooooooooooooongFunction(\n"
910 " \"short literal\"\n"
911 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
912}
913
Alexander Kornienko15757312012-12-06 18:03:27 +0000914TEST_F(FormatTest, AlignsPipes) {
915 verifyFormat(
916 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
917 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
918 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
919 verifyFormat(
920 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
921 " << aaaaaaaaaaaaaaaaaaaa;");
922 verifyFormat(
923 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
924 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
925 verifyFormat(
926 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
927 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
928 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
929 verifyFormat(
930 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
931 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
932 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
933}
934
Daniel Jasperbac016b2012-12-03 18:12:45 +0000935TEST_F(FormatTest, UnderstandsEquals) {
936 verifyFormat(
937 "aaaaaaaaaaaaaaaaa =\n"
938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
939 verifyFormat(
940 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000942 verifyFormat(
943 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000944 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +0000945 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000947
Daniel Jasper9cda8002013-01-07 13:08:40 +0000948 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000949 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +0000950 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000951 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000952}
953
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000954TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000955 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
956 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000957
Daniel Jasper1321eb52012-12-18 21:05:13 +0000958 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
959 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000960
961 verifyFormat(
962 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
963 " Parameter2);");
964
965 verifyFormat(
966 "ShortObject->shortFunction(\n"
967 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
968 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
969
970 verifyFormat("loooooooooooooongFunction(\n"
971 " LoooooooooooooongObject->looooooooooooooooongFunction());");
972
973 verifyFormat(
974 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
975 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
976
Daniel Jasper46a46a22013-01-07 07:13:20 +0000977 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +0000978 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000979 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +0000980 verifyFormat(
981 "aaaaaaaaaaa->aaaaaaaaa(\n"
982 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
983 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000984}
985
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000986TEST_F(FormatTest, WrapsTemplateDeclarations) {
987 verifyFormat("template <typename T>\n"
988 "virtual void loooooooooooongFunction(int Param1, int Param2);");
989 verifyFormat(
990 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
991 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
992 verifyFormat(
993 "template <typename T>\n"
994 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
995 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +0000996 verifyFormat(
997 "template <typename T>\n"
998 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
999 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001001 verifyFormat("template <typename T>\n"
1002 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1003 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001004 verifyFormat(
1005 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1006 " typename T4 = char>\n"
1007 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001008}
1009
Daniel Jasperbac016b2012-12-03 18:12:45 +00001010TEST_F(FormatTest, UnderstandsTemplateParameters) {
1011 verifyFormat("A<int> a;");
1012 verifyFormat("A<A<A<int> > > a;");
1013 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1014 verifyFormat("bool x = a < 1 || 2 > a;");
1015 verifyFormat("bool x = 5 < f<int>();");
1016 verifyFormat("bool x = f<int>() > 5;");
1017 verifyFormat("bool x = 5 < a<int>::x;");
1018 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1019 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1020
1021 verifyGoogleFormat("A<A<int>> a;");
1022 verifyGoogleFormat("A<A<A<int>>> a;");
1023 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1024
1025 verifyFormat("test >> a >> b;");
1026 verifyFormat("test << a >> b;");
1027
1028 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001029 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001030}
1031
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001032TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001033 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001034 verifyFormat("f(-1, -2, -3);");
1035 verifyFormat("a[-1] = 5;");
1036 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001037 verifyFormat("if (i == -1) {}");
1038 verifyFormat("if (i != -1) {}");
1039 verifyFormat("if (i > -1) {}");
1040 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001041 verifyFormat("++(a->f());");
1042 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001043 verifyFormat("(a->f())++;");
1044 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001045 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001046
1047 verifyFormat("a-- > b;");
1048 verifyFormat("b ? -a : c;");
1049 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001050 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001051 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001052 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001053
1054 verifyFormat("return -1;");
1055 verifyFormat("switch (a) {\n"
1056 "case -1:\n"
1057 " break;\n"
1058 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001059
1060 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1061 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001062
1063 verifyFormat("int a = /* confusing comment */ -1;");
1064 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1065 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001066}
1067
1068TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001069 verifyFormat("bool operator<();");
1070 verifyFormat("bool operator>();");
1071 verifyFormat("bool operator=();");
1072 verifyFormat("bool operator==();");
1073 verifyFormat("bool operator!=();");
1074 verifyFormat("int operator+();");
1075 verifyFormat("int operator++();");
1076 verifyFormat("bool operator();");
1077 verifyFormat("bool operator()();");
1078 verifyFormat("bool operator[]();");
1079 verifyFormat("operator bool();");
1080 verifyFormat("operator SomeType<int>();");
1081 verifyFormat("void *operator new(std::size_t size);");
1082 verifyFormat("void *operator new[](std::size_t size);");
1083 verifyFormat("void operator delete(void *ptr);");
1084 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001085}
1086
Daniel Jasper088dab52013-01-11 16:09:04 +00001087TEST_F(FormatTest, UnderstandsNewAndDelete) {
1088 verifyFormat("A *a = new A;");
1089 verifyFormat("A *a = new (placement) A;");
1090 verifyFormat("delete a;");
1091 verifyFormat("delete (A *)a;");
1092}
1093
Daniel Jasper5d334402013-01-02 08:57:10 +00001094TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001095 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001096 verifyFormat("f(a, *a);");
1097 verifyFormat("f(*a);");
1098 verifyFormat("int a = b * 10;");
1099 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001100 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001101 verifyFormat("int a += b * c;");
1102 verifyFormat("int a -= b * c;");
1103 verifyFormat("int a *= b * c;");
1104 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001105 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001106 verifyFormat("int a = *b * c;");
1107 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001108 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001109 verifyFormat("return 10 * b;");
1110 verifyFormat("return *b * *c;");
1111 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001112 verifyFormat("f(b ? *c : *d);");
1113 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001114 verifyFormat("*b = a;");
1115 verifyFormat("a * ~b;");
1116 verifyFormat("a * !b;");
1117 verifyFormat("a * +b;");
1118 verifyFormat("a * -b;");
1119 verifyFormat("a * ++b;");
1120 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001121 verifyFormat("a[4] * b;");
1122 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001123 verifyFormat("a * [self dostuff];");
1124 verifyFormat("a * (a + b);");
1125 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001126 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001127
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001128 verifyFormat("InvalidRegions[*R] = 0;");
1129
Daniel Jasper8b39c662012-12-10 18:59:13 +00001130 verifyFormat("A<int *> a;");
1131 verifyFormat("A<int **> a;");
1132 verifyFormat("A<int *, int *> a;");
1133 verifyFormat("A<int **, int **> a;");
1134
Daniel Jasper2db356d2013-01-08 20:03:18 +00001135 verifyFormat(
1136 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1138
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001139 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001140 verifyGoogleFormat("A<int*> a;");
1141 verifyGoogleFormat("A<int**> a;");
1142 verifyGoogleFormat("A<int*, int*> a;");
1143 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001144 verifyGoogleFormat("f(b ? *c : *d);");
1145 verifyGoogleFormat("int a = b ? *c : *d;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001146
1147 verifyFormat("a = *(x + y);");
1148 verifyFormat("a = &(x + y);");
1149 verifyFormat("*(x + y).call();");
1150 verifyFormat("&(x + y)->call();");
1151 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001152
1153 verifyFormat("f(b * /* confusing comment */ ++c);");
1154 verifyFormat(
1155 "int *MyValues = {\n"
1156 " *A, // Operator detection might be confused by the '{'\n"
1157 " *BB // Operator detection might be confused by previous comment\n"
1158 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001159}
1160
Daniel Jasper4981bd02013-01-13 08:01:36 +00001161TEST_F(FormatTest, FormatsCasts) {
1162 verifyFormat("Type *A = static_cast<Type *>(P);");
1163 verifyFormat("Type *A = (Type *)P;");
1164 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1165 verifyFormat("int a = (int)(2.0f);");
1166
1167 // FIXME: These also need to be identified.
1168 verifyFormat("int a = (int) 2.0f;");
1169 verifyFormat("int a = (int) * b;");
1170
1171 // These are not casts.
1172 verifyFormat("void f(int *) {}");
1173 verifyFormat("void f(int *);");
1174 verifyFormat("void f(int *) = 0;");
1175 verifyFormat("void f(SmallVector<int>) {}");
1176 verifyFormat("void f(SmallVector<int>);");
1177 verifyFormat("void f(SmallVector<int>) = 0;");
1178}
1179
Daniel Jasper46ef8522013-01-10 13:08:12 +00001180TEST_F(FormatTest, FormatsFunctionTypes) {
1181 // FIXME: Determine the cases that need a space after the return type and fix.
1182 verifyFormat("A<bool()> a;");
1183 verifyFormat("A<SomeType()> a;");
1184 verifyFormat("A<void(*)(int, std::string)> a;");
1185
1186 verifyFormat("int(*func)(void *);");
1187}
1188
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001189TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001190 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001191 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001192 verifyFormat(
1193 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1194 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001195 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001196}
1197
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001198TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1199 verifyFormat("(a)->b();");
1200 verifyFormat("--a;");
1201}
1202
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001203TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001204 verifyFormat("#include <string>\n"
1205 "#include <a/b/c.h>\n"
1206 "#include \"a/b/string\"\n"
1207 "#include \"string.h\"\n"
1208 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001209 "#include <a-a>\n"
1210 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001211
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001212 verifyFormat("#import <string>");
1213 verifyFormat("#import <a/b/c.h>");
1214 verifyFormat("#import \"a/b/string\"");
1215 verifyFormat("#import \"string.h\"");
1216 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001217}
1218
Alexander Kornienko15757312012-12-06 18:03:27 +00001219//===----------------------------------------------------------------------===//
1220// Error recovery tests.
1221//===----------------------------------------------------------------------===//
1222
Daniel Jasper700e7102013-01-10 09:26:47 +00001223TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001224 verifyFormat("void f() { return; }\n42");
1225 verifyFormat("void f() {\n"
1226 " if (0)\n"
1227 " return;\n"
1228 "}\n"
1229 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001230 verifyFormat("void f() { return }\n42");
1231 verifyFormat("void f() {\n"
1232 " if (0)\n"
1233 " return\n"
1234 "}\n"
1235 "42");
1236}
1237
1238TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1239 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1240 EXPECT_EQ("void f() {\n"
1241 " if (a)\n"
1242 " return\n"
1243 "}", format("void f ( ) { if ( a ) return }"));
1244 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1245 EXPECT_EQ("namespace N {\n"
1246 "void f() {}\n"
1247 "void g()\n"
1248 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001249}
1250
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001251TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1252 verifyFormat("int aaaaaaaa =\n"
1253 " // Overly long comment\n"
1254 " b;", getLLVMStyleWithColumns(20));
1255 verifyFormat("function(\n"
1256 " ShortArgument,\n"
1257 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1258}
1259
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001260TEST_F(FormatTest, IncorrectAccessSpecifier) {
1261 verifyFormat("public:");
1262 verifyFormat("class A {\n"
1263 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001264 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001265 "};");
1266 verifyFormat("public\n"
1267 "int qwerty;");
1268 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001269 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001270 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001271 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001272 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001273 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001274}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001275
Alexander Kornienko393b0082012-12-04 15:40:36 +00001276TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1277 verifyFormat("{");
1278}
1279
1280TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001281 verifyFormat("do {}");
1282 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001283 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001284 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001285 "wheeee(fun);");
1286 verifyFormat("do {\n"
1287 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001288 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001289}
1290
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001291TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001292 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001293 verifyFormat("switch {\n foo;\n foo();\n}");
1294 verifyFormat("for {\n foo;\n foo();\n}");
1295 verifyFormat("while {\n foo;\n foo();\n}");
1296 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001297}
1298
Daniel Jasper1f42f112013-01-04 18:52:56 +00001299TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1300 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001301 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001302}
1303
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001304TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001305 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1306 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1307 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1308 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001309
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001310 EXPECT_EQ("{\n"
1311 " {\n"
1312 " breakme(\n"
1313 " qwe);\n"
1314 "}\n", format("{\n"
1315 " {\n"
1316 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001317 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001318}
1319
Manuel Klimek2851c162013-01-10 14:36:46 +00001320TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1321 verifyFormat(
1322 "int x = {\n"
1323 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001324 " b(alongervariable)\n"
1325 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001326}
1327
1328TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1329 verifyFormat(
1330 "Aaa({\n"
1331 " int i;\n"
1332 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1333 " ccccccccccccccccc));");
1334}
1335
Manuel Klimek517e8942013-01-11 17:54:10 +00001336TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1337 verifyFormat("void f() { return 42; }");
1338 verifyFormat("void f() {\n"
1339 " // Comment\n"
1340 "}");
1341 verifyFormat("{\n"
1342 "#error {\n"
1343 " int a;\n"
1344 "}");
1345 verifyFormat("{\n"
1346 " int a;\n"
1347 "#error {\n"
1348 "}");
1349}
1350
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001351TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1352 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001353 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001354 verifyFormat("class foo a = { bar };\nint n;");
1355 verifyFormat("union foo a = { bar };\nint n;");
1356
1357 // Elaborate types inside function definitions.
1358 verifyFormat("struct foo f() {}\nint n;");
1359 verifyFormat("class foo f() {}\nint n;");
1360 verifyFormat("union foo f() {}\nint n;");
1361
1362 // Templates.
1363 verifyFormat("template <class X> void f() {}\nint n;");
1364 verifyFormat("template <struct X> void f() {}\nint n;");
1365 verifyFormat("template <union X> void f() {}\nint n;");
1366
1367 // Actual definitions...
1368 verifyFormat("struct {} n;");
1369 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1370 verifyFormat("union Z {\n int n;\n} x;");
1371 verifyFormat("class MACRO Z {} n;");
1372 verifyFormat("class MACRO(X) Z {} n;");
1373 verifyFormat("class __attribute__(X) Z {} n;");
1374 verifyFormat("class __declspec(X) Z {} n;");
1375
1376 // Elaborate types where incorrectly parsing the structural element would
1377 // break the indent.
1378 verifyFormat("if (true)\n"
1379 " class X x;\n"
1380 "else\n"
1381 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001382}
1383
Manuel Klimek407a31a2013-01-15 15:50:27 +00001384TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1385 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1386 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1387 EXPECT_EQ("#error 1", format(" # error 1"));
1388 EXPECT_EQ("#warning 1", format(" # warning 1"));
1389}
1390
Manuel Klimek517e8942013-01-11 17:54:10 +00001391// FIXME: This breaks the order of the unwrapped lines:
1392// TEST_F(FormatTest, OrderUnwrappedLines) {
1393// verifyFormat("{\n"
1394// " bool a; //\n"
1395// "#error {\n"
1396// " int a;\n"
1397// "}");
1398// }
1399
Nico Webercf4a79c2013-01-08 17:56:31 +00001400//===----------------------------------------------------------------------===//
1401// Objective-C tests.
1402//===----------------------------------------------------------------------===//
1403
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001404TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1405 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1406 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1407 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001408 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001409 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1410 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1411 format("-(NSInteger)Method3:(id)anObject;"));
1412 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1413 format("-(NSInteger)Method4:(id)anObject;"));
1414 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1415 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1416 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1417 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001418 EXPECT_EQ(
1419 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1420 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001421
1422 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001423 EXPECT_EQ(
1424 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1425 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1426 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1427 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1428 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1429 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1430 format(
1431 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1432 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1433 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1434 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1435 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1436 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001437
1438 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Webercd52bda2013-01-10 23:11:41 +00001439 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001440 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1441 // protocol lists (but not for template classes):
1442 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001443
1444 verifyFormat("- (int(*)())foo:(int(*)())f;");
1445 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;");
1446
1447 // If there's no return type (very rare in practice!), LLVM and Google style
1448 // agree.
1449 verifyFormat("- foo:(int)f;");
1450 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001451}
1452
Daniel Jasper886568d2013-01-09 08:36:49 +00001453TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001454 verifyFormat("int (^Block)(int, int);");
1455 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001456}
1457
Nico Weber27d13672013-01-09 20:25:35 +00001458TEST_F(FormatTest, FormatObjCInterface) {
1459 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001460 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001461 "@public\n"
1462 " int field1;\n"
1463 "@protected\n"
1464 " int field2;\n"
1465 "@private\n"
1466 " int field3;\n"
1467 "@package\n"
1468 " int field4;\n"
1469 "}\n"
1470 "+ (id)init;\n"
1471 "@end");
1472
Nico Weber27d13672013-01-09 20:25:35 +00001473 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1474 " @public\n"
1475 " int field1;\n"
1476 " @protected\n"
1477 " int field2;\n"
1478 " @private\n"
1479 " int field3;\n"
1480 " @package\n"
1481 " int field4;\n"
1482 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001483 "+(id) init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001484 "@end");
1485
1486 verifyFormat("@interface Foo\n"
1487 "+ (id)init;\n"
1488 "// Look, a comment!\n"
1489 "- (int)answerWith:(int)i;\n"
1490 "@end");
1491
1492 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001493 "@end\n"
1494 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001495 "@end");
1496
1497 verifyFormat("@interface Foo : Bar\n"
1498 "+ (id)init;\n"
1499 "@end");
1500
Nico Weber5f500df2013-01-10 20:12:55 +00001501 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001502 "+ (id)init;\n"
1503 "@end");
1504
Nico Weber5f500df2013-01-10 20:12:55 +00001505 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001506 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001507 "@end");
1508
Nico Webered91bba2013-01-10 19:19:14 +00001509 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001510 "+ (id)init;\n"
1511 "@end");
1512
Nico Webered91bba2013-01-10 19:19:14 +00001513 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001514 "+ (id)init;\n"
1515 "@end");
1516
Nico Weber5f500df2013-01-10 20:12:55 +00001517 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\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 (HackStuff)<MyProtocol>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001522 "+(id) init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001523 "@end");
1524
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001525 verifyFormat("@interface Foo {\n"
1526 " int _i;\n"
1527 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001528 "+ (id)init;\n"
1529 "@end");
1530
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001531 verifyFormat("@interface Foo : Bar {\n"
1532 " int _i;\n"
1533 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001534 "+ (id)init;\n"
1535 "@end");
1536
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001537 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1538 " int _i;\n"
1539 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001540 "+ (id)init;\n"
1541 "@end");
1542
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001543 verifyFormat("@interface Foo (HackStuff) {\n"
1544 " int _i;\n"
1545 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001546 "+ (id)init;\n"
1547 "@end");
1548
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001549 verifyFormat("@interface Foo () {\n"
1550 " int _i;\n"
1551 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001552 "+ (id)init;\n"
1553 "@end");
1554
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001555 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1556 " int _i;\n"
1557 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001558 "+ (id)init;\n"
1559 "@end");
1560}
1561
Nico Weber50767d82013-01-09 23:25:37 +00001562TEST_F(FormatTest, FormatObjCImplementation) {
1563 verifyFormat("@implementation Foo : NSObject {\n"
1564 "@public\n"
1565 " int field1;\n"
1566 "@protected\n"
1567 " int field2;\n"
1568 "@private\n"
1569 " int field3;\n"
1570 "@package\n"
1571 " int field4;\n"
1572 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001573 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001574 "@end");
1575
1576 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1577 " @public\n"
1578 " int field1;\n"
1579 " @protected\n"
1580 " int field2;\n"
1581 " @private\n"
1582 " int field3;\n"
1583 " @package\n"
1584 " int field4;\n"
1585 "}\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001586 "+(id) init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001587 "@end");
1588
1589 verifyFormat("@implementation Foo\n"
1590 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001591 " if (true)\n"
1592 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001593 "}\n"
1594 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001595 "- (int)answerWith:(int)i {\n"
1596 " return i;\n"
1597 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001598 "+ (int)answerWith:(int)i {\n"
1599 " return i;\n"
1600 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001601 "@end");
1602
1603 verifyFormat("@implementation Foo\n"
1604 "@end\n"
1605 "@implementation Bar\n"
1606 "@end");
1607
1608 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001609 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001610 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001611 "@end");
1612
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001613 verifyFormat("@implementation Foo {\n"
1614 " int _i;\n"
1615 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001616 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001617 "@end");
1618
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001619 verifyFormat("@implementation Foo : Bar {\n"
1620 " int _i;\n"
1621 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001622 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001623 "@end");
1624
Nico Webered91bba2013-01-10 19:19:14 +00001625 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001626 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001627 "@end");
1628}
1629
Nico Weber1abe6ea2013-01-09 21:15:03 +00001630TEST_F(FormatTest, FormatObjCProtocol) {
1631 verifyFormat("@protocol Foo\n"
1632 "@property(weak) id delegate;\n"
1633 "- (NSUInteger)numberOfThings;\n"
1634 "@end");
1635
Nico Weber5f500df2013-01-10 20:12:55 +00001636 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001637 "- (NSUInteger)numberOfThings;\n"
1638 "@end");
1639
Nico Weber5f500df2013-01-10 20:12:55 +00001640 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Webercd52bda2013-01-10 23:11:41 +00001641 "-(NSUInteger) numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001642 "@end");
1643
Nico Weber1abe6ea2013-01-09 21:15:03 +00001644 verifyFormat("@protocol Foo;\n"
1645 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001646
1647 verifyFormat("@protocol Foo\n"
1648 "@end\n"
1649 "@protocol Bar\n"
1650 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001651
1652 verifyFormat("@protocol myProtocol\n"
1653 "- (void)mandatoryWithInt:(int)i;\n"
1654 "@optional\n"
1655 "- (void)optional;\n"
1656 "@required\n"
1657 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001658 "@optional\n"
1659 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001660 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001661}
1662
Nico Weberbcfdd262013-01-12 06:18:40 +00001663TEST_F(FormatTest, FormatObjCMethodExpr) {
1664 verifyFormat("[foo bar:baz];");
1665 verifyFormat("return [foo bar:baz];");
1666 verifyFormat("f([foo bar:baz]);");
1667 verifyFormat("f(2, [foo bar:baz]);");
1668 verifyFormat("f(2, a ? b : c);");
1669 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1670
1671 verifyFormat("[foo bar:baz], [foo bar:baz];");
1672 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1673 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1674 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1675 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1676 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1677 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1678 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1679 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1680 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1681 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1682 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1683 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1684 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1685 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1686 verifyFormat("[foo bar:baz] | [foo bar:baz];");
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];");
1700 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1701 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1702 // Whew!
1703
1704 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1705 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1706 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1707 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1708 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001709 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001710 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001711
Nico Weberbcfdd262013-01-12 06:18:40 +00001712 verifyFormat("arr[[self indexForFoo:a]];");
1713 verifyFormat("throw [self errorFor:a];");
1714 verifyFormat("@throw [self errorFor:a];");
1715
Nico Webere8ccc812013-01-12 22:48:47 +00001716 // This tests that the formatter doesn't break after "backing" but before ":",
1717 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001718 verifyFormat(
1719 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001720 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1721 " backing:NSBackingStoreBuffered defer:YES]))");
1722
Nico Webere8ccc812013-01-12 22:48:47 +00001723 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1724 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001725
1726}
1727
Nico Weber581f5572013-01-07 15:56:25 +00001728TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001729 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001730 verifyFormat("@catch");
1731 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001732 verifyFormat("@compatibility_alias");
1733 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001734 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001735 verifyFormat("@encode");
1736 verifyFormat("@end");
1737 verifyFormat("@finally");
1738 verifyFormat("@implementation");
1739 verifyFormat("@import");
1740 verifyFormat("@interface");
1741 verifyFormat("@optional");
1742 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001743 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001744 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001745 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001746 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001747 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001748 verifyFormat("@required");
1749 verifyFormat("@selector");
1750 verifyFormat("@synchronized");
1751 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001752 verifyFormat("@throw");
1753 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001754
Nico Webercb4d6902013-01-08 19:40:21 +00001755 verifyFormat("@\"String\"");
1756 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001757 verifyFormat("@+4.8");
1758 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001759 verifyFormat("@1LL");
1760 verifyFormat("@.5");
1761 verifyFormat("@'c'");
1762 verifyFormat("@true");
1763 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001764 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001765 verifyFormat("@[");
1766 verifyFormat("@{");
1767
Nico Weber581f5572013-01-07 15:56:25 +00001768 EXPECT_EQ("@interface", format("@ interface"));
1769
1770 // The precise formatting of this doesn't matter, nobody writes code like
1771 // this.
1772 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001773}
1774
Nico Weberc31689a2013-01-08 19:15:23 +00001775TEST_F(FormatTest, ObjCSnippets) {
1776 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001777 verifyFormat("@autoreleasepool {\n"
1778 " foo();\n"
1779 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001780 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001781 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001782 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001783 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001784 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001785 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001786 verifyFormat("@synchronized(self) {\n"
1787 " f();\n"
1788 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001789
Nico Weber70848232013-01-10 21:30:42 +00001790 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1791 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1792
Nico Webercf4a79c2013-01-08 17:56:31 +00001793 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001794 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1795 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001796}
1797
Daniel Jaspercd162382013-01-07 13:26:07 +00001798} // end namespace tooling
1799} // end namespace clang