blob: 7b57e858a44a16d06d4bdb3ae1965f3d1c0742f2 [file] [log] [blame]
Daniel Jasperbac016b2012-12-03 18:12:45 +00001//===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Manuel Klimekca547db2013-01-16 14:55:28 +000010#define DEBUG_TYPE "format-test"
11
Chandler Carruth1050e8b2012-12-04 09:45:34 +000012#include "clang/Format/Format.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000013#include "clang/Lex/Lexer.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000014#include "gtest/gtest.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000015#include "llvm/Support/Debug.h"
16#include "../Tooling/RewriterTestContext.h"
17
18// Uncomment to get debug output from tests:
19// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
Daniel Jasperbac016b2012-12-03 18:12:45 +000020
21namespace clang {
22namespace format {
23
24class FormatTest : public ::testing::Test {
25protected:
26 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
27 const FormatStyle &Style) {
Manuel Klimekca547db2013-01-16 14:55:28 +000028 DEBUG(llvm::errs() << "---\n");
Daniel Jasperbac016b2012-12-03 18:12:45 +000029 RewriterTestContext Context;
30 FileID ID = Context.createInMemoryFile("input.cc", Code);
31 SourceLocation Start =
32 Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
33 std::vector<CharSourceRange> Ranges(
34 1,
35 CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
Daniel Jasper46ef8522013-01-10 13:08:12 +000036 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
37 getFormattingLangOpts());
Daniel Jaspercd162382013-01-07 13:26:07 +000038 tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +000039 Ranges,
40 new IgnoringDiagConsumer());
Daniel Jasperbac016b2012-12-03 18:12:45 +000041 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
Manuel Klimekca547db2013-01-16 14:55:28 +000042 DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n");
Daniel Jasperbac016b2012-12-03 18:12:45 +000043 return Context.getRewrittenText(ID);
44 }
45
46 std::string format(llvm::StringRef Code,
47 const FormatStyle &Style = getLLVMStyle()) {
48 return format(Code, 0, Code.size(), Style);
49 }
50
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000051 std::string messUp(llvm::StringRef Code) {
52 std::string MessedUp(Code.str());
53 bool InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000054 bool InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000055 bool JustReplacedNewline = false;
56 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
57 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
58 if (JustReplacedNewline)
59 MessedUp[i - 1] = '\n';
60 InComment = true;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +000061 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
62 if (i != 0) MessedUp[i - 1] = '\n';
Manuel Klimek526ed112013-01-09 15:25:02 +000063 InPreprocessorDirective = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +000064 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
65 MessedUp[i] = ' ';
Manuel Klimek526ed112013-01-09 15:25:02 +000066 MessedUp[i + 1] = ' ';
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000067 } else if (MessedUp[i] == '\n') {
68 if (InComment) {
69 InComment = false;
Manuel Klimek526ed112013-01-09 15:25:02 +000070 } else if (InPreprocessorDirective) {
71 InPreprocessorDirective = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000072 } else {
73 JustReplacedNewline = true;
74 MessedUp[i] = ' ';
75 }
Manuel Klimekd4397b92013-01-04 23:34:14 +000076 } else if (MessedUp[i] != ' ') {
77 JustReplacedNewline = false;
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000078 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000079 }
Daniel Jaspere35ec2b2012-12-18 19:56:56 +000080 return MessedUp;
81 }
82
Manuel Klimek060143e2013-01-02 18:33:23 +000083 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
84 FormatStyle Style = getLLVMStyle();
85 Style.ColumnLimit = ColumnLimit;
86 return Style;
87 }
88
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000089 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
90 FormatStyle Style = getGoogleStyle();
91 Style.ColumnLimit = ColumnLimit;
92 return Style;
93 }
94
Manuel Klimek060143e2013-01-02 18:33:23 +000095 void verifyFormat(llvm::StringRef Code,
96 const FormatStyle &Style = getLLVMStyle()) {
97 EXPECT_EQ(Code.str(), format(messUp(Code), Style));
Daniel Jasperbac016b2012-12-03 18:12:45 +000098 }
99
100 void verifyGoogleFormat(llvm::StringRef Code) {
Manuel Klimek060143e2013-01-02 18:33:23 +0000101 verifyFormat(Code, getGoogleStyle());
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102 }
103};
104
Manuel Klimek526ed112013-01-09 15:25:02 +0000105TEST_F(FormatTest, MessUp) {
106 EXPECT_EQ("1 2 3", messUp("1 2 3"));
107 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
108 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
109 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
110 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
111}
112
Alexander Kornienko15757312012-12-06 18:03:27 +0000113//===----------------------------------------------------------------------===//
114// Basic function tests.
115//===----------------------------------------------------------------------===//
116
Daniel Jasperbac016b2012-12-03 18:12:45 +0000117TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
118 EXPECT_EQ(";", format(";"));
119}
120
121TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
122 EXPECT_EQ("int i;", format(" int i;"));
123 EXPECT_EQ("\nint i;", format(" \n\t \r int i;"));
124 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
125 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
126}
127
128TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
129 EXPECT_EQ("int i;", format("int\ni;"));
130}
131
132TEST_F(FormatTest, FormatsNestedBlockStatements) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000133 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000134}
135
Alexander Kornienko15757312012-12-06 18:03:27 +0000136TEST_F(FormatTest, FormatsNestedCall) {
137 verifyFormat("Method(f1, f2(f3));");
138 verifyFormat("Method(f1(f2, f3()));");
Daniel Jasper4e9008a2013-01-13 08:19:51 +0000139 verifyFormat("Method(f1(f2, (f3())));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000140}
141
Daniel Jasper6b825c22013-01-16 07:19:28 +0000142TEST_F(FormatTest, ImportantSpaces) {
143 verifyFormat("vector< ::Type> v;");
144}
145
Alexander Kornienko15757312012-12-06 18:03:27 +0000146//===----------------------------------------------------------------------===//
147// Tests for control statements.
148//===----------------------------------------------------------------------===//
149
150TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000151 verifyFormat("if (true)\n f();\ng();");
152 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
Alexander Kornienko15757312012-12-06 18:03:27 +0000153 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
Daniel Jasperdf3736a2013-01-16 15:44:34 +0000154
155 FormatStyle AllowsMergedIf = getGoogleStyle();
156 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
157 verifyFormat("if (a)\n"
158 " // comment\n"
159 " f();", AllowsMergedIf);
160
161 verifyFormat("if (a) // Can't merge this\n"
162 " f();\n", AllowsMergedIf);
163 verifyFormat("if (a) /* still don't merge */\n"
164 " f();", AllowsMergedIf);
165 verifyFormat("if (a) { // Never merge this\n"
166 " f();\n"
167 "}", AllowsMergedIf);
168 verifyFormat("if (a) { /* Never merge this */\n"
169 " f();\n"
170 "}", AllowsMergedIf);
171
172 AllowsMergedIf.ColumnLimit = 14;
173 verifyFormat("if (a) return;", AllowsMergedIf);
Daniel Jasperfd0ca972013-01-14 16:02:06 +0000174 verifyFormat("if (aaaaaaaaa)\n"
Daniel Jasperdf3736a2013-01-16 15:44:34 +0000175 " return;", AllowsMergedIf);
176
177 AllowsMergedIf.ColumnLimit = 13;
178 verifyFormat("if (a)\n return;", AllowsMergedIf);
Alexander Kornienko15757312012-12-06 18:03:27 +0000179}
180
181TEST_F(FormatTest, ParseIfElse) {
182 verifyFormat("if (true)\n"
183 " if (true)\n"
184 " if (true)\n"
185 " f();\n"
186 " else\n"
187 " g();\n"
188 " else\n"
189 " h();\n"
190 "else\n"
191 " i();");
192 verifyFormat("if (true)\n"
193 " if (true)\n"
194 " if (true) {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000195 " if (true)\n"
196 " f();\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000197 " } else {\n"
198 " g();\n"
199 " }\n"
200 " else\n"
201 " h();\n"
202 "else {\n"
203 " i();\n"
204 "}");
205}
206
207TEST_F(FormatTest, ElseIf) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000208 verifyFormat("if (a) {} else if (b) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000209 verifyFormat("if (a)\n"
210 " f();\n"
211 "else if (b)\n"
212 " g();\n"
213 "else\n"
214 " h();");
215}
216
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217TEST_F(FormatTest, FormatsForLoop) {
218 verifyFormat(
219 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000220 " ++VeryVeryLongLoopVariable)\n"
221 " ;");
222 verifyFormat("for (;;)\n"
223 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000224 verifyFormat("for (;;) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000225 verifyFormat("for (;;) {\n"
226 " f();\n"
227 "}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000228
229 verifyFormat(
230 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
231 " E = UnwrappedLines.end();\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000232 " I != E; ++I) {}");
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000233
234 verifyFormat(
235 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000236 " ++IIIII) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000237}
238
239TEST_F(FormatTest, FormatsWhileLoop) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000240 verifyFormat("while (true) {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000241 verifyFormat("while (true)\n"
242 " f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000243 verifyFormat("while () {}");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000244 verifyFormat("while () {\n"
245 " f();\n"
246 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000247}
248
Alexander Kornienko15757312012-12-06 18:03:27 +0000249TEST_F(FormatTest, FormatsDoWhile) {
250 verifyFormat("do {\n"
251 " do_something();\n"
252 "} while (something());");
253 verifyFormat("do\n"
254 " do_something();\n"
255 "while (something());");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000256}
257
Alexander Kornienko15757312012-12-06 18:03:27 +0000258TEST_F(FormatTest, FormatsSwitchStatement) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000259 verifyFormat("switch (x) {\n"
260 "case 1:\n"
261 " f();\n"
262 " break;\n"
263 "case kFoo:\n"
264 "case ns::kBar:\n"
265 "case kBaz:\n"
266 " break;\n"
267 "default:\n"
268 " g();\n"
269 " break;\n"
270 "}");
271 verifyFormat("switch (x) {\n"
272 "case 1: {\n"
273 " f();\n"
274 " break;\n"
275 "}\n"
276 "}");
277 verifyFormat("switch (test)\n"
278 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000279 verifyGoogleFormat("switch (x) {\n"
280 " case 1:\n"
281 " f();\n"
282 " break;\n"
283 " case kFoo:\n"
284 " case ns::kBar:\n"
285 " case kBaz:\n"
286 " break;\n"
287 " default:\n"
288 " g();\n"
289 " break;\n"
290 "}");
291 verifyGoogleFormat("switch (x) {\n"
292 " case 1: {\n"
293 " f();\n"
294 " break;\n"
295 " }\n"
296 "}");
297 verifyGoogleFormat("switch (test)\n"
298 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000299}
300
Alexander Kornienko15757312012-12-06 18:03:27 +0000301TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000302 verifyFormat("void f() {\n"
303 " some_code();\n"
304 "test_label:\n"
305 " some_other_code();\n"
306 " {\n"
307 " some_more_code();\n"
308 " another_label:\n"
309 " some_more_code();\n"
310 " }\n"
311 "}");
312 verifyFormat("some_code();\n"
313 "test_label:\n"
314 "some_other_code();");
315}
316
Alexander Kornienko15757312012-12-06 18:03:27 +0000317//===----------------------------------------------------------------------===//
318// Tests for comments.
319//===----------------------------------------------------------------------===//
320
321TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000322 verifyFormat("// line 1\n"
323 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000324 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000325
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000326 verifyFormat("void f() {\n"
327 " // Doesn't do anything\n"
328 "}");
Daniel Jasper487f64b2013-01-13 16:10:20 +0000329 verifyFormat("void f(int i, // some comment (probably for i)\n"
330 " int j, // some comment (probably for j)\n"
331 " int k); // some comment (probably for k)");
332 verifyFormat("void f(int i,\n"
333 " // some comment (probably for j)\n"
334 " int j,\n"
335 " // some comment (probably for k)\n"
336 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000337
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000338 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000339 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000340
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000341 verifyFormat("enum E {\n"
342 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000343 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000344 " VAL_B\n"
345 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000346
347 verifyFormat(
348 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000349 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000350 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
351 " // Comment inside a statement.\n"
352 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000353
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000354 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000355 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000356
357 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000358}
359
360TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000361 verifyFormat("f(/*test=*/ true);");
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000362 EXPECT_EQ(
363 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
364 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
365 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n"
366 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
367 EXPECT_EQ(
368 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
369 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
370 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
371 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
372}
373
374TEST_F(FormatTest, CommentsInStaticInitializers) {
375 EXPECT_EQ(
376 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
377 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
378 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
379 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
380 " aaaaaaaaaaaaaaaaaaaa };",
381 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
382 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
383 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
384 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
385 " aaaaaaaaaaaaaaaaaaaa };"));
386 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
387 " bbbbbbbbbbb, ccccccccccc };");
388 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
389 " // comment for bb....\n"
390 " bbbbbbbbbbb, ccccccccccc };");
391 verifyGoogleFormat(
392 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
393 " bbbbbbbbbbb,\n"
394 " ccccccccccc };");
395 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
396 " // comment for bb....\n"
397 " bbbbbbbbbbb,\n"
398 " ccccccccccc };");
399
Alexander Kornienko15757312012-12-06 18:03:27 +0000400}
401
Alexander Kornienko15757312012-12-06 18:03:27 +0000402//===----------------------------------------------------------------------===//
403// Tests for classes, namespaces, etc.
404//===----------------------------------------------------------------------===//
405
406TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000407 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000408}
409
410TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
411 verifyFormat("class A {\n"
412 "public:\n"
413 "protected:\n"
414 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000415 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000416 "};");
417 verifyGoogleFormat("class A {\n"
418 " public:\n"
419 " protected:\n"
420 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000421 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000422 "};");
423}
424
425TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000426 verifyFormat("class A : public B {};");
427 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000428}
429
Manuel Klimekde768542013-01-07 18:10:23 +0000430TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000431 verifyFormat("class A {} a, b;");
432 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000433 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000434}
435
Alexander Kornienko15757312012-12-06 18:03:27 +0000436TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000437 verifyFormat("enum {\n"
438 " Zero,\n"
439 " One = 1,\n"
440 " Two = One + 1,\n"
441 " Three = (One + Two),\n"
442 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
443 " Five = (One, Two, Three, Four, 5)\n"
444 "};");
445 verifyFormat("enum Enum {\n"
446 "};");
447 verifyFormat("enum {\n"
448 "};");
449}
450
Nico Weberefaddc02013-01-14 05:49:49 +0000451TEST_F(FormatTest, FormatsBitfields) {
452 verifyFormat("struct Bitfields {\n"
453 " unsigned sClass : 8;\n"
454 " unsigned ValueKind : 2;\n"
455 "};");
456}
457
Alexander Kornienko15757312012-12-06 18:03:27 +0000458TEST_F(FormatTest, FormatsNamespaces) {
459 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000460 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000461 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000462 "}");
463 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000464 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000465 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000466 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000467 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000468 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000469 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000470 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000471 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000472 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000473 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000474}
475
Nico Webera9ccdd12013-01-07 16:36:17 +0000476TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000477 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
478 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000479 verifyFormat("try {\n"
480 " throw a * b;\n"
481 "}\n"
482 "catch (int a) {\n"
483 " // Do nothing.\n"
484 "}\n"
485 "catch (...) {\n"
486 " exit(42);\n"
487 "}");
488
489 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000490 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000491 "catch (...) {\n"
492 " return 5;\n"
493 "}");
494 verifyFormat("class A {\n"
495 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000496 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000497 " catch (...) {\n"
498 " throw;\n"
499 " }\n"
500 "};\n");
501}
502
503TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000504 verifyFormat("@try {\n"
505 " f();\n"
506 "}\n"
507 "@catch (NSException e) {\n"
508 " @throw;\n"
509 "}\n"
510 "@finally {\n"
511 " exit(42);\n"
512 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000513}
514
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000515TEST_F(FormatTest, StaticInitializers) {
516 verifyFormat("static SomeClass SC = { 1, 'a' };");
517
518 // FIXME: Format like enums if the static initializer does not fit on a line.
519 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000520 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000521 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
522 "};");
523
524 verifyFormat(
525 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
526 " looooooooooooooooooooooooooooooooooongname,\n"
527 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000528}
529
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000530TEST_F(FormatTest, NestedStaticInitializers) {
531 verifyFormat("static A x = { { {} } };\n");
532 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000533 "static A x = { { { init1, init2, init3, init4 },\n"
534 " { init1, init2, init3, init4 } } };");
535
536 // FIXME: Fix this in general an verify that it works in LLVM style again.
537 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000538 "somes Status::global_reps[3] = {\n"
539 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
540 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
541 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
542 "};");
543 verifyFormat(
544 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
545 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
546 " } };");
547
548 // FIXME: We might at some point want to handle this similar to parameters
549 // lists, where we have an option to put each on a single line.
550 verifyFormat("struct {\n"
551 " unsigned bit;\n"
552 " const char *const name;\n"
553 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
554 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
555}
556
Manuel Klimeka080a182013-01-02 16:30:12 +0000557TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
558 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
559 " \\\n"
560 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
561}
562
Daniel Jasper71607512013-01-07 10:48:50 +0000563TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000564 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
565 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000566}
567
Manuel Klimeka080a182013-01-02 16:30:12 +0000568TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
569 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000570 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000571}
572
573TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
574 EXPECT_EQ("#line 42 \"test\"\n",
575 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000576 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000577 format("# \\\n define \\\n A \\\n B\n",
578 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000579}
580
581TEST_F(FormatTest, EndOfFileEndsPPDirective) {
582 EXPECT_EQ("#line 42 \"test\"",
583 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000584 EXPECT_EQ("#define A B",
585 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000586}
587
Manuel Klimek060143e2013-01-02 18:33:23 +0000588TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000589 // If the macro fits in one line, we still do not get the full
590 // line, as only the next line decides whether we need an escaped newline and
591 // thus use the last column.
592 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000593
Manuel Klimekd544c572013-01-07 09:24:17 +0000594 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
595 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000596 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000597
598 verifyFormat("#define A A\n#define A A");
599 verifyFormat("#define A(X) A\n#define A A");
600
601 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
602 verifyFormat("#define Something \\\n"
603 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000604}
605
606TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000607 EXPECT_EQ("// some comment\n"
608 "#include \"a.h\"\n"
609 "#define A(A,\\\n"
610 " B)\n"
611 "#include \"b.h\"\n"
612 "// some comment\n",
613 format(" // some comment\n"
614 " #include \"a.h\"\n"
615 "#define A(A,\\\n"
616 " B)\n"
617 " #include \"b.h\"\n"
618 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000619}
620
Manuel Klimekd4397b92013-01-04 23:34:14 +0000621TEST_F(FormatTest, LayoutSingleHash) {
622 EXPECT_EQ("#\na;", format("#\na;"));
623}
624
625TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
626 EXPECT_EQ("#define A \\\n"
627 " c; \\\n"
628 " e;\n"
629 "f;", format("#define A c; e;\n"
630 "f;", getLLVMStyleWithColumns(14)));
631}
632
633TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000634 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000635}
636
637TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000638 EXPECT_EQ("# define A\\\n b;",
639 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000640}
641
642TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000643 EXPECT_EQ("int x,\n"
644 "#define A\n"
645 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000646}
647
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000648TEST_F(FormatTest, HashInMacroDefinition) {
649 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
650 verifyFormat("#define A \\\n"
651 " { \\\n"
652 " f(#c);\\\n"
653 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000654
655 verifyFormat("#define A(X) \\\n"
656 " void function##X()", getLLVMStyleWithColumns(22));
657
658 verifyFormat("#define A(a, b, c) \\\n"
659 " void a##b##c()", getLLVMStyleWithColumns(22));
660
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000661 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000662}
663
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000664TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
665 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
666}
667
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000668TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000669 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000670}
671
Manuel Klimeka5342db2013-01-06 20:07:31 +0000672TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
673 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
674 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
675 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
676 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
677}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000678
Manuel Klimek95419382013-01-07 07:56:50 +0000679TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000680 EXPECT_EQ(
681 "#define A \\\n int i; \\\n int j;",
682 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000683}
684
Manuel Klimekd544c572013-01-07 09:24:17 +0000685TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
686 verifyFormat("#define A \\\n"
687 " int v( \\\n"
688 " a); \\\n"
689 " int i;", getLLVMStyleWithColumns(11));
690}
691
Manuel Klimeka080a182013-01-02 16:30:12 +0000692TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000693 EXPECT_EQ(
694 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
695 " \\\n"
696 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
697 "\n"
698 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
699 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
700 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
701 "\\\n"
702 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
703 " \n"
704 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
705 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000706}
707
Manuel Klimek526ed112013-01-09 15:25:02 +0000708TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
709 EXPECT_EQ("int\n"
710 "#define A\n"
711 " a;",
712 format("int\n#define A\na;"));
713 verifyFormat(
714 "functionCallTo(someOtherFunction(\n"
715 " withSomeParameters, whichInSequence,\n"
716 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000717 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000718 " withMoreParamters,\n"
719 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000720 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000721}
722
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000723TEST_F(FormatTest, LayoutBlockInsideParens) {
724 EXPECT_EQ("functionCall({\n"
725 " int i;\n"
726 "});", format(" functionCall ( {int i;} );"));
727}
728
729TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000730 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000731 "int i;", format(" SOME_MACRO {int i;} int i;"));
732}
733
734TEST_F(FormatTest, LayoutNestedBlocks) {
735 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
736 " struct s {\n"
737 " int i;\n"
738 " };\n"
739 " s kBitsToOs[] = { { 10 } };\n"
740 " for (int i = 0; i < 10; ++i)\n"
741 " return;\n"
742 "}");
743}
744
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000745TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
746 EXPECT_EQ("{}", format("{}"));
747}
748
Alexander Kornienko15757312012-12-06 18:03:27 +0000749//===----------------------------------------------------------------------===//
750// Line break tests.
751//===----------------------------------------------------------------------===//
752
753TEST_F(FormatTest, FormatsFunctionDefinition) {
754 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
755 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000756 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000757}
758
759TEST_F(FormatTest, FormatsAwesomeMethodCall) {
760 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000761 "SomeLongMethodName(SomeReallyLongMethod(\n"
762 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
763 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000764}
765
Daniel Jasper1321eb52012-12-18 21:05:13 +0000766TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000767 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000768 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
769 getLLVMStyleWithColumns(45));
770 verifyFormat("Constructor()\n"
771 " : Inttializer(FitsOnTheLine) {}",
772 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000773
774 verifyFormat(
775 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000776 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000777
778 verifyFormat(
779 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000780 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
781 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
782 verifyGoogleFormat(
783 "SomeClass::Constructor()\n"
784 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
785 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
786 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper16618242013-01-16 17:00:50 +0000787 verifyGoogleFormat(
788 "SomeClass::Constructor()\n"
789 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
790 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
791 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000792
793 verifyFormat(
794 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000795 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000796 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000797
798 verifyFormat("Constructor()\n"
799 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
800 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
801 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000802 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000803
804 // Here a line could be saved by splitting the second initializer onto two
805 // lines, but that is not desireable.
806 verifyFormat("Constructor()\n"
807 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
808 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000809 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000810
811 verifyGoogleFormat("MyClass::MyClass(int var)\n"
812 " : some_var_(var), // 4 space indent\n"
813 " some_other_var_(var + 1) { // lined up\n"
814 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000815
816 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000817 std::string input = "Constructor()\n"
818 " : aaaa(a,\n";
819 for (unsigned i = 0, e = 80; i != e; ++i) {
820 input += " a,\n";
821 }
822 input += " a) {}";
823 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000824}
825
Alexander Kornienko15757312012-12-06 18:03:27 +0000826TEST_F(FormatTest, BreaksAsHighAsPossible) {
827 verifyFormat(
828 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
829 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
830 " f();");
831}
832
Daniel Jasperbac016b2012-12-03 18:12:45 +0000833TEST_F(FormatTest, BreaksDesireably) {
834 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
835 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000836 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000837
838 verifyFormat(
839 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000841
842 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
843 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
844 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000845
846 verifyFormat(
847 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
849 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000851
852 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
853 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
854
Daniel Jasper723f0302013-01-02 14:40:02 +0000855 verifyFormat(
856 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
858
Daniel Jasper33182dd2012-12-05 14:57:28 +0000859 // This test case breaks on an incorrect memoization, i.e. an optimization not
860 // taking into account the StopAt value.
861 verifyFormat(
862 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000863 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
864 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
865 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000866
Daniel Jaspercd162382013-01-07 13:26:07 +0000867 verifyFormat("{\n {\n {\n"
868 " Annotation.SpaceRequiredBefore =\n"
869 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
870 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
871 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000872}
873
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000874TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
875 verifyGoogleFormat(
876 "aaaaaaaa(aaaaaaaaaaaaa,\n"
877 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
879 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
881 verifyGoogleFormat(
882 "aaaaaaaaaaaaaaa(aaaaaaaaa,\n"
883 " aaaaaaaaa,\n"
884 " aaaaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();");
885 verifyGoogleFormat(
886 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
887 " ddddddddddddddddddddddddddddd),\n"
888 " test);");
889
890 verifyGoogleFormat(
891 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
892 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
893 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
894 verifyGoogleFormat("a(\"a\"\n"
895 " \"a\",\n"
896 " a);");
897}
898
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000899TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
900 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
901 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +0000902 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
903 " GUARDED_BY(aaaaaaaaaaaaa);");
904 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
905 " GUARDED_BY(aaaaaaaaaaaaa) {}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000906}
907
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000908TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
909 verifyFormat(
910 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000911 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000912 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000913 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000914 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000915 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000916 verifyFormat(
917 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000918 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000919}
920
Daniel Jasper9cda8002013-01-07 13:08:40 +0000921TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
922 verifyFormat(
923 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
924 " SI->getAlignment(),\n"
925 " SI->getPointerAddressSpaceee());\n");
926 verifyFormat(
927 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
928 " Line.Tokens.front().Tok.getLocation(),\n"
929 " Line.Tokens.back().Tok.getLocation());");
930}
931
Daniel Jaspercf225b62012-12-24 13:43:52 +0000932TEST_F(FormatTest, AlignsAfterAssignments) {
933 verifyFormat(
934 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000935 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000936 verifyFormat(
937 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000938 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000939 verifyFormat(
940 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000941 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000942 verifyFormat(
943 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000944 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000945 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000946 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
947 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
948 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000949}
950
951TEST_F(FormatTest, AlignsAfterReturn) {
952 verifyFormat(
953 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
954 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
955 verifyFormat(
956 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
957 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
958}
959
Daniel Jasper9c837d02013-01-09 07:06:56 +0000960TEST_F(FormatTest, BreaksConditionalExpressions) {
961 verifyFormat(
962 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
963 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
964 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
965 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
966 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +0000967 verifyFormat(
968 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
969 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +0000970}
971
Nico Weber7d37b8b2013-01-12 01:28:06 +0000972TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
973 verifyFormat("arr[foo ? bar : baz];");
974 verifyFormat("f()[foo ? bar : baz];");
975 verifyFormat("(a + b)[foo ? bar : baz];");
976 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
977}
978
Daniel Jasperbac016b2012-12-03 18:12:45 +0000979TEST_F(FormatTest, AlignsStringLiterals) {
980 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
981 " \"short literal\");");
982 verifyFormat(
983 "looooooooooooooooooooooooongFunction(\n"
984 " \"short literal\"\n"
985 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
986}
987
Alexander Kornienko15757312012-12-06 18:03:27 +0000988TEST_F(FormatTest, AlignsPipes) {
989 verifyFormat(
990 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
991 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
992 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
993 verifyFormat(
994 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
995 " << aaaaaaaaaaaaaaaaaaaa;");
996 verifyFormat(
997 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
998 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
999 verifyFormat(
1000 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1001 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1002 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1003 verifyFormat(
1004 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1005 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1006 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1007}
1008
Daniel Jasperbac016b2012-12-03 18:12:45 +00001009TEST_F(FormatTest, UnderstandsEquals) {
1010 verifyFormat(
1011 "aaaaaaaaaaaaaaaaa =\n"
1012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1013 verifyFormat(
1014 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001016 verifyFormat(
1017 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001018 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001019 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001020 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001021
Daniel Jasper9cda8002013-01-07 13:08:40 +00001022 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001023 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001024 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001025 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001026}
1027
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001028TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001029 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1030 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001031
Daniel Jasper1321eb52012-12-18 21:05:13 +00001032 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1033 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001034
1035 verifyFormat(
1036 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1037 " Parameter2);");
1038
1039 verifyFormat(
1040 "ShortObject->shortFunction(\n"
1041 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1042 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1043
1044 verifyFormat("loooooooooooooongFunction(\n"
1045 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1046
1047 verifyFormat(
1048 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1049 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1050
Daniel Jasper46a46a22013-01-07 07:13:20 +00001051 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001052 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001053 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001054 verifyFormat(
1055 "aaaaaaaaaaa->aaaaaaaaa(\n"
1056 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1057 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001058}
1059
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001060TEST_F(FormatTest, WrapsTemplateDeclarations) {
1061 verifyFormat("template <typename T>\n"
1062 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1063 verifyFormat(
1064 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1065 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
1066 verifyFormat(
1067 "template <typename T>\n"
1068 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1069 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001070 verifyFormat(
1071 "template <typename T>\n"
1072 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1073 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001075 verifyFormat("template <typename T>\n"
1076 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1077 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001078 verifyFormat(
1079 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1080 " typename T4 = char>\n"
1081 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001082}
1083
Daniel Jasperbac016b2012-12-03 18:12:45 +00001084TEST_F(FormatTest, UnderstandsTemplateParameters) {
1085 verifyFormat("A<int> a;");
1086 verifyFormat("A<A<A<int> > > a;");
1087 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1088 verifyFormat("bool x = a < 1 || 2 > a;");
1089 verifyFormat("bool x = 5 < f<int>();");
1090 verifyFormat("bool x = f<int>() > 5;");
1091 verifyFormat("bool x = 5 < a<int>::x;");
1092 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1093 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1094
1095 verifyGoogleFormat("A<A<int>> a;");
1096 verifyGoogleFormat("A<A<A<int>>> a;");
1097 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1098
1099 verifyFormat("test >> a >> b;");
1100 verifyFormat("test << a >> b;");
1101
1102 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001103 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001104}
1105
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001106TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001107 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001108 verifyFormat("f(-1, -2, -3);");
1109 verifyFormat("a[-1] = 5;");
1110 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001111 verifyFormat("if (i == -1) {}");
1112 verifyFormat("if (i != -1) {}");
1113 verifyFormat("if (i > -1) {}");
1114 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001115 verifyFormat("++(a->f());");
1116 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001117 verifyFormat("(a->f())++;");
1118 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001119 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001120
1121 verifyFormat("a-- > b;");
1122 verifyFormat("b ? -a : c;");
1123 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001124 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001125 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001126 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001127
1128 verifyFormat("return -1;");
1129 verifyFormat("switch (a) {\n"
1130 "case -1:\n"
1131 " break;\n"
1132 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001133
1134 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1135 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001136
1137 verifyFormat("int a = /* confusing comment */ -1;");
1138 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1139 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001140}
1141
1142TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001143 verifyFormat("bool operator<();");
1144 verifyFormat("bool operator>();");
1145 verifyFormat("bool operator=();");
1146 verifyFormat("bool operator==();");
1147 verifyFormat("bool operator!=();");
1148 verifyFormat("int operator+();");
1149 verifyFormat("int operator++();");
1150 verifyFormat("bool operator();");
1151 verifyFormat("bool operator()();");
1152 verifyFormat("bool operator[]();");
1153 verifyFormat("operator bool();");
1154 verifyFormat("operator SomeType<int>();");
1155 verifyFormat("void *operator new(std::size_t size);");
1156 verifyFormat("void *operator new[](std::size_t size);");
1157 verifyFormat("void operator delete(void *ptr);");
1158 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001159}
1160
Daniel Jasper088dab52013-01-11 16:09:04 +00001161TEST_F(FormatTest, UnderstandsNewAndDelete) {
1162 verifyFormat("A *a = new A;");
1163 verifyFormat("A *a = new (placement) A;");
1164 verifyFormat("delete a;");
1165 verifyFormat("delete (A *)a;");
1166}
1167
Daniel Jasper5d334402013-01-02 08:57:10 +00001168TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001169 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001170 verifyFormat("f(a, *a);");
1171 verifyFormat("f(*a);");
1172 verifyFormat("int a = b * 10;");
1173 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001174 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001175 verifyFormat("int a += b * c;");
1176 verifyFormat("int a -= b * c;");
1177 verifyFormat("int a *= b * c;");
1178 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001179 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001180 verifyFormat("int a = *b * c;");
1181 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001182 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001183 verifyFormat("return 10 * b;");
1184 verifyFormat("return *b * *c;");
1185 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001186 verifyFormat("f(b ? *c : *d);");
1187 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001188 verifyFormat("*b = a;");
1189 verifyFormat("a * ~b;");
1190 verifyFormat("a * !b;");
1191 verifyFormat("a * +b;");
1192 verifyFormat("a * -b;");
1193 verifyFormat("a * ++b;");
1194 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001195 verifyFormat("a[4] * b;");
1196 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001197 verifyFormat("a * [self dostuff];");
1198 verifyFormat("a * (a + b);");
1199 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001200 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001201
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001202 verifyFormat("InvalidRegions[*R] = 0;");
1203
Daniel Jasper8b39c662012-12-10 18:59:13 +00001204 verifyFormat("A<int *> a;");
1205 verifyFormat("A<int **> a;");
1206 verifyFormat("A<int *, int *> a;");
1207 verifyFormat("A<int **, int **> a;");
1208
Daniel Jasper2db356d2013-01-08 20:03:18 +00001209 verifyFormat(
1210 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1212
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001213 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001214 verifyGoogleFormat("A<int*> a;");
1215 verifyGoogleFormat("A<int**> a;");
1216 verifyGoogleFormat("A<int*, int*> a;");
1217 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001218 verifyGoogleFormat("f(b ? *c : *d);");
1219 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001220 verifyGoogleFormat("Type* t = **x;");
1221 verifyGoogleFormat("Type* t = *++*x;");
1222 verifyGoogleFormat("*++*x;");
1223 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1224 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001225
1226 verifyFormat("a = *(x + y);");
1227 verifyFormat("a = &(x + y);");
1228 verifyFormat("*(x + y).call();");
1229 verifyFormat("&(x + y)->call();");
1230 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001231
1232 verifyFormat("f(b * /* confusing comment */ ++c);");
1233 verifyFormat(
1234 "int *MyValues = {\n"
1235 " *A, // Operator detection might be confused by the '{'\n"
1236 " *BB // Operator detection might be confused by previous comment\n"
1237 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001238
1239 verifyFormat("if (int *a = &b)");
1240 verifyFormat("if (int &a = *b)");
1241 verifyFormat("if (a & b[i])");
1242 verifyFormat("if (a::b::c::d & b[i])");
1243 verifyFormat("if (*b[i])");
1244 verifyFormat("if (int *a = (&b))");
1245 verifyFormat("while (int *a = &b)");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001246}
1247
Daniel Jasper4981bd02013-01-13 08:01:36 +00001248TEST_F(FormatTest, FormatsCasts) {
1249 verifyFormat("Type *A = static_cast<Type *>(P);");
1250 verifyFormat("Type *A = (Type *)P;");
1251 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1252 verifyFormat("int a = (int)(2.0f);");
1253
1254 // FIXME: These also need to be identified.
1255 verifyFormat("int a = (int) 2.0f;");
1256 verifyFormat("int a = (int) * b;");
1257
1258 // These are not casts.
1259 verifyFormat("void f(int *) {}");
1260 verifyFormat("void f(int *);");
1261 verifyFormat("void f(int *) = 0;");
1262 verifyFormat("void f(SmallVector<int>) {}");
1263 verifyFormat("void f(SmallVector<int>);");
1264 verifyFormat("void f(SmallVector<int>) = 0;");
1265}
1266
Daniel Jasper46ef8522013-01-10 13:08:12 +00001267TEST_F(FormatTest, FormatsFunctionTypes) {
1268 // FIXME: Determine the cases that need a space after the return type and fix.
1269 verifyFormat("A<bool()> a;");
1270 verifyFormat("A<SomeType()> a;");
1271 verifyFormat("A<void(*)(int, std::string)> a;");
1272
1273 verifyFormat("int(*func)(void *);");
1274}
1275
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001276TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001277 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001278 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001279 verifyFormat(
1280 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1281 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001282 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001283}
1284
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001285TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1286 verifyFormat("(a)->b();");
1287 verifyFormat("--a;");
1288}
1289
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001290TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001291 verifyFormat("#include <string>\n"
1292 "#include <a/b/c.h>\n"
1293 "#include \"a/b/string\"\n"
1294 "#include \"string.h\"\n"
1295 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001296 "#include <a-a>\n"
1297 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001298
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001299 verifyFormat("#import <string>");
1300 verifyFormat("#import <a/b/c.h>");
1301 verifyFormat("#import \"a/b/string\"");
1302 verifyFormat("#import \"string.h\"");
1303 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001304}
1305
Alexander Kornienko15757312012-12-06 18:03:27 +00001306//===----------------------------------------------------------------------===//
1307// Error recovery tests.
1308//===----------------------------------------------------------------------===//
1309
Daniel Jasper700e7102013-01-10 09:26:47 +00001310TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001311 verifyFormat("void f() { return; }\n42");
1312 verifyFormat("void f() {\n"
1313 " if (0)\n"
1314 " return;\n"
1315 "}\n"
1316 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001317 verifyFormat("void f() { return }\n42");
1318 verifyFormat("void f() {\n"
1319 " if (0)\n"
1320 " return\n"
1321 "}\n"
1322 "42");
1323}
1324
1325TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1326 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1327 EXPECT_EQ("void f() {\n"
1328 " if (a)\n"
1329 " return\n"
1330 "}", format("void f ( ) { if ( a ) return }"));
1331 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1332 EXPECT_EQ("namespace N {\n"
1333 "void f() {}\n"
1334 "void g()\n"
1335 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001336}
1337
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001338TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1339 verifyFormat("int aaaaaaaa =\n"
1340 " // Overly long comment\n"
1341 " b;", getLLVMStyleWithColumns(20));
1342 verifyFormat("function(\n"
1343 " ShortArgument,\n"
1344 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1345}
1346
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001347TEST_F(FormatTest, IncorrectAccessSpecifier) {
1348 verifyFormat("public:");
1349 verifyFormat("class A {\n"
1350 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001351 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001352 "};");
1353 verifyFormat("public\n"
1354 "int qwerty;");
1355 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001356 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001357 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001358 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001359 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001360 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001361}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001362
Alexander Kornienko393b0082012-12-04 15:40:36 +00001363TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1364 verifyFormat("{");
1365}
1366
1367TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001368 verifyFormat("do {}");
1369 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001370 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001371 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001372 "wheeee(fun);");
1373 verifyFormat("do {\n"
1374 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001375 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001376}
1377
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001378TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001379 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001380 verifyFormat("switch {\n foo;\n foo();\n}");
1381 verifyFormat("for {\n foo;\n foo();\n}");
1382 verifyFormat("while {\n foo;\n foo();\n}");
1383 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001384}
1385
Daniel Jasper1f42f112013-01-04 18:52:56 +00001386TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1387 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001388 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001389}
1390
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001391TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001392 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1393 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1394 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1395 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001396
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001397 EXPECT_EQ("{\n"
1398 " {\n"
1399 " breakme(\n"
1400 " qwe);\n"
1401 "}\n", format("{\n"
1402 " {\n"
1403 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001404 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001405}
1406
Manuel Klimek2851c162013-01-10 14:36:46 +00001407TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1408 verifyFormat(
1409 "int x = {\n"
1410 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001411 " b(alongervariable)\n"
1412 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001413}
1414
1415TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1416 verifyFormat(
1417 "Aaa({\n"
1418 " int i;\n"
1419 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1420 " ccccccccccccccccc));");
1421}
1422
Manuel Klimek517e8942013-01-11 17:54:10 +00001423TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1424 verifyFormat("void f() { return 42; }");
1425 verifyFormat("void f() {\n"
1426 " // Comment\n"
1427 "}");
1428 verifyFormat("{\n"
1429 "#error {\n"
1430 " int a;\n"
1431 "}");
1432 verifyFormat("{\n"
1433 " int a;\n"
1434 "#error {\n"
1435 "}");
1436}
1437
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001438TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1439 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001440 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001441 verifyFormat("class foo a = { bar };\nint n;");
1442 verifyFormat("union foo a = { bar };\nint n;");
1443
1444 // Elaborate types inside function definitions.
1445 verifyFormat("struct foo f() {}\nint n;");
1446 verifyFormat("class foo f() {}\nint n;");
1447 verifyFormat("union foo f() {}\nint n;");
1448
1449 // Templates.
1450 verifyFormat("template <class X> void f() {}\nint n;");
1451 verifyFormat("template <struct X> void f() {}\nint n;");
1452 verifyFormat("template <union X> void f() {}\nint n;");
1453
1454 // Actual definitions...
1455 verifyFormat("struct {} n;");
1456 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1457 verifyFormat("union Z {\n int n;\n} x;");
1458 verifyFormat("class MACRO Z {} n;");
1459 verifyFormat("class MACRO(X) Z {} n;");
1460 verifyFormat("class __attribute__(X) Z {} n;");
1461 verifyFormat("class __declspec(X) Z {} n;");
1462
1463 // Elaborate types where incorrectly parsing the structural element would
1464 // break the indent.
1465 verifyFormat("if (true)\n"
1466 " class X x;\n"
1467 "else\n"
1468 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001469}
1470
Manuel Klimek407a31a2013-01-15 15:50:27 +00001471TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1472 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1473 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1474 EXPECT_EQ("#error 1", format(" # error 1"));
1475 EXPECT_EQ("#warning 1", format(" # warning 1"));
1476}
1477
Manuel Klimek517e8942013-01-11 17:54:10 +00001478// FIXME: This breaks the order of the unwrapped lines:
1479// TEST_F(FormatTest, OrderUnwrappedLines) {
1480// verifyFormat("{\n"
1481// " bool a; //\n"
1482// "#error {\n"
1483// " int a;\n"
1484// "}");
1485// }
1486
Nico Webercf4a79c2013-01-08 17:56:31 +00001487//===----------------------------------------------------------------------===//
1488// Objective-C tests.
1489//===----------------------------------------------------------------------===//
1490
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001491TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1492 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1493 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1494 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001495 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001496 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1497 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1498 format("-(NSInteger)Method3:(id)anObject;"));
1499 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1500 format("-(NSInteger)Method4:(id)anObject;"));
1501 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1502 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1503 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1504 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001505 EXPECT_EQ(
1506 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1507 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001508
1509 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001510 EXPECT_EQ(
1511 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1512 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1513 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1514 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1515 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1516 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1517 format(
1518 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1519 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1520 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1521 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1522 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1523 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001524
1525 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001526 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001527 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1528 // protocol lists (but not for template classes):
1529 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001530
1531 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001532 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001533
1534 // If there's no return type (very rare in practice!), LLVM and Google style
1535 // agree.
1536 verifyFormat("- foo:(int)f;");
1537 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001538}
1539
Daniel Jasper886568d2013-01-09 08:36:49 +00001540TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001541 verifyFormat("int (^Block)(int, int);");
1542 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001543}
1544
Nico Weber27d13672013-01-09 20:25:35 +00001545TEST_F(FormatTest, FormatObjCInterface) {
1546 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001547 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001548 "@public\n"
1549 " int field1;\n"
1550 "@protected\n"
1551 " int field2;\n"
1552 "@private\n"
1553 " int field3;\n"
1554 "@package\n"
1555 " int field4;\n"
1556 "}\n"
1557 "+ (id)init;\n"
1558 "@end");
1559
Nico Weber27d13672013-01-09 20:25:35 +00001560 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1561 " @public\n"
1562 " int field1;\n"
1563 " @protected\n"
1564 " int field2;\n"
1565 " @private\n"
1566 " int field3;\n"
1567 " @package\n"
1568 " int field4;\n"
1569 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001570 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001571 "@end");
1572
1573 verifyFormat("@interface Foo\n"
1574 "+ (id)init;\n"
1575 "// Look, a comment!\n"
1576 "- (int)answerWith:(int)i;\n"
1577 "@end");
1578
1579 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001580 "@end\n"
1581 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001582 "@end");
1583
1584 verifyFormat("@interface Foo : Bar\n"
1585 "+ (id)init;\n"
1586 "@end");
1587
Nico Weber5f500df2013-01-10 20:12:55 +00001588 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001589 "+ (id)init;\n"
1590 "@end");
1591
Nico Weber5f500df2013-01-10 20:12:55 +00001592 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001593 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001594 "@end");
1595
Nico Webered91bba2013-01-10 19:19:14 +00001596 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001597 "+ (id)init;\n"
1598 "@end");
1599
Nico Webered91bba2013-01-10 19:19:14 +00001600 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001601 "+ (id)init;\n"
1602 "@end");
1603
Nico Weber5f500df2013-01-10 20:12:55 +00001604 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001605 "+ (id)init;\n"
1606 "@end");
1607
Nico Weber5f500df2013-01-10 20:12:55 +00001608 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001609 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001610 "@end");
1611
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001612 verifyFormat("@interface Foo {\n"
1613 " int _i;\n"
1614 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001615 "+ (id)init;\n"
1616 "@end");
1617
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001618 verifyFormat("@interface Foo : Bar {\n"
1619 " int _i;\n"
1620 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001621 "+ (id)init;\n"
1622 "@end");
1623
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001624 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1625 " int _i;\n"
1626 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001627 "+ (id)init;\n"
1628 "@end");
1629
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001630 verifyFormat("@interface Foo (HackStuff) {\n"
1631 " int _i;\n"
1632 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001633 "+ (id)init;\n"
1634 "@end");
1635
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001636 verifyFormat("@interface Foo () {\n"
1637 " int _i;\n"
1638 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001639 "+ (id)init;\n"
1640 "@end");
1641
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001642 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1643 " int _i;\n"
1644 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001645 "+ (id)init;\n"
1646 "@end");
1647}
1648
Nico Weber50767d82013-01-09 23:25:37 +00001649TEST_F(FormatTest, FormatObjCImplementation) {
1650 verifyFormat("@implementation Foo : NSObject {\n"
1651 "@public\n"
1652 " int field1;\n"
1653 "@protected\n"
1654 " int field2;\n"
1655 "@private\n"
1656 " int field3;\n"
1657 "@package\n"
1658 " int field4;\n"
1659 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001660 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001661 "@end");
1662
1663 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1664 " @public\n"
1665 " int field1;\n"
1666 " @protected\n"
1667 " int field2;\n"
1668 " @private\n"
1669 " int field3;\n"
1670 " @package\n"
1671 " int field4;\n"
1672 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001673 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001674 "@end");
1675
1676 verifyFormat("@implementation Foo\n"
1677 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001678 " if (true)\n"
1679 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001680 "}\n"
1681 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001682 "- (int)answerWith:(int)i {\n"
1683 " return i;\n"
1684 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001685 "+ (int)answerWith:(int)i {\n"
1686 " return i;\n"
1687 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001688 "@end");
1689
1690 verifyFormat("@implementation Foo\n"
1691 "@end\n"
1692 "@implementation Bar\n"
1693 "@end");
1694
1695 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001696 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001697 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001698 "@end");
1699
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001700 verifyFormat("@implementation Foo {\n"
1701 " int _i;\n"
1702 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001703 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001704 "@end");
1705
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001706 verifyFormat("@implementation Foo : Bar {\n"
1707 " int _i;\n"
1708 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001709 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001710 "@end");
1711
Nico Webered91bba2013-01-10 19:19:14 +00001712 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001713 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001714 "@end");
1715}
1716
Nico Weber1abe6ea2013-01-09 21:15:03 +00001717TEST_F(FormatTest, FormatObjCProtocol) {
1718 verifyFormat("@protocol Foo\n"
1719 "@property(weak) id delegate;\n"
1720 "- (NSUInteger)numberOfThings;\n"
1721 "@end");
1722
Nico Weber5f500df2013-01-10 20:12:55 +00001723 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001724 "- (NSUInteger)numberOfThings;\n"
1725 "@end");
1726
Nico Weber5f500df2013-01-10 20:12:55 +00001727 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001728 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001729 "@end");
1730
Nico Weber1abe6ea2013-01-09 21:15:03 +00001731 verifyFormat("@protocol Foo;\n"
1732 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001733
1734 verifyFormat("@protocol Foo\n"
1735 "@end\n"
1736 "@protocol Bar\n"
1737 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001738
1739 verifyFormat("@protocol myProtocol\n"
1740 "- (void)mandatoryWithInt:(int)i;\n"
1741 "@optional\n"
1742 "- (void)optional;\n"
1743 "@required\n"
1744 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001745 "@optional\n"
1746 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001747 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001748}
1749
Nico Weberbcfdd262013-01-12 06:18:40 +00001750TEST_F(FormatTest, FormatObjCMethodExpr) {
1751 verifyFormat("[foo bar:baz];");
1752 verifyFormat("return [foo bar:baz];");
1753 verifyFormat("f([foo bar:baz]);");
1754 verifyFormat("f(2, [foo bar:baz]);");
1755 verifyFormat("f(2, a ? b : c);");
1756 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1757
1758 verifyFormat("[foo bar:baz], [foo bar:baz];");
1759 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1760 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1761 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1762 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1763 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1764 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1765 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1766 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1767 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1768 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1769 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1770 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1771 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1772 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1773 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1774 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1775 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1776 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1777 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1778 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1779 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1780 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1781 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1782 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1783 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1784 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1785 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1786 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1787 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1788 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1789 // Whew!
1790
1791 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1792 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1793 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1794 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1795 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001796 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001797 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001798
Nico Weberbcfdd262013-01-12 06:18:40 +00001799 verifyFormat("arr[[self indexForFoo:a]];");
1800 verifyFormat("throw [self errorFor:a];");
1801 verifyFormat("@throw [self errorFor:a];");
1802
Nico Webere8ccc812013-01-12 22:48:47 +00001803 // This tests that the formatter doesn't break after "backing" but before ":",
1804 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001805 verifyFormat(
1806 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001807 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1808 " backing:NSBackingStoreBuffered defer:YES]))");
1809
Nico Webere8ccc812013-01-12 22:48:47 +00001810 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1811 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001812
1813}
1814
Nico Weber581f5572013-01-07 15:56:25 +00001815TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001816 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001817 verifyFormat("@catch");
1818 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001819 verifyFormat("@compatibility_alias");
1820 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001821 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001822 verifyFormat("@encode");
1823 verifyFormat("@end");
1824 verifyFormat("@finally");
1825 verifyFormat("@implementation");
1826 verifyFormat("@import");
1827 verifyFormat("@interface");
1828 verifyFormat("@optional");
1829 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001830 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001831 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001832 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001833 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001834 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001835 verifyFormat("@required");
1836 verifyFormat("@selector");
1837 verifyFormat("@synchronized");
1838 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001839 verifyFormat("@throw");
1840 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001841
Nico Webercb4d6902013-01-08 19:40:21 +00001842 verifyFormat("@\"String\"");
1843 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001844 verifyFormat("@+4.8");
1845 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001846 verifyFormat("@1LL");
1847 verifyFormat("@.5");
1848 verifyFormat("@'c'");
1849 verifyFormat("@true");
1850 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001851 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001852 verifyFormat("@[");
1853 verifyFormat("@{");
1854
Nico Weber581f5572013-01-07 15:56:25 +00001855 EXPECT_EQ("@interface", format("@ interface"));
1856
1857 // The precise formatting of this doesn't matter, nobody writes code like
1858 // this.
1859 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001860}
1861
Nico Weberc31689a2013-01-08 19:15:23 +00001862TEST_F(FormatTest, ObjCSnippets) {
1863 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001864 verifyFormat("@autoreleasepool {\n"
1865 " foo();\n"
1866 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001867 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001868 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001869 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001870 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001871 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001872 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001873 verifyFormat("@synchronized(self) {\n"
1874 " f();\n"
1875 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001876
Nico Weber70848232013-01-10 21:30:42 +00001877 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1878 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1879
Nico Webercf4a79c2013-01-08 17:56:31 +00001880 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001881 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1882 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001883}
1884
Daniel Jaspercd162382013-01-07 13:26:07 +00001885} // end namespace tooling
1886} // end namespace clang