blob: 190eabab38f98b07d1282db9fe1bffffc146204c [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);");
902}
903
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000904TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
905 verifyFormat(
906 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000907 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000908 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000909 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000910 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000911 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000912 verifyFormat(
913 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000914 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000915}
916
Daniel Jasper9cda8002013-01-07 13:08:40 +0000917TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
918 verifyFormat(
919 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
920 " SI->getAlignment(),\n"
921 " SI->getPointerAddressSpaceee());\n");
922 verifyFormat(
923 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
924 " Line.Tokens.front().Tok.getLocation(),\n"
925 " Line.Tokens.back().Tok.getLocation());");
926}
927
Daniel Jaspercf225b62012-12-24 13:43:52 +0000928TEST_F(FormatTest, AlignsAfterAssignments) {
929 verifyFormat(
930 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000931 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000932 verifyFormat(
933 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000934 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000935 verifyFormat(
936 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000937 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000938 verifyFormat(
939 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000940 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000941 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000942 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
943 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
944 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000945}
946
947TEST_F(FormatTest, AlignsAfterReturn) {
948 verifyFormat(
949 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
950 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
951 verifyFormat(
952 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
953 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
954}
955
Daniel Jasper9c837d02013-01-09 07:06:56 +0000956TEST_F(FormatTest, BreaksConditionalExpressions) {
957 verifyFormat(
958 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
959 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
961 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
962 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +0000963 verifyFormat(
964 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
965 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +0000966}
967
Nico Weber7d37b8b2013-01-12 01:28:06 +0000968TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
969 verifyFormat("arr[foo ? bar : baz];");
970 verifyFormat("f()[foo ? bar : baz];");
971 verifyFormat("(a + b)[foo ? bar : baz];");
972 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
973}
974
Daniel Jasperbac016b2012-12-03 18:12:45 +0000975TEST_F(FormatTest, AlignsStringLiterals) {
976 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
977 " \"short literal\");");
978 verifyFormat(
979 "looooooooooooooooooooooooongFunction(\n"
980 " \"short literal\"\n"
981 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
982}
983
Alexander Kornienko15757312012-12-06 18:03:27 +0000984TEST_F(FormatTest, AlignsPipes) {
985 verifyFormat(
986 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
987 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
988 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
989 verifyFormat(
990 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
991 " << aaaaaaaaaaaaaaaaaaaa;");
992 verifyFormat(
993 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
994 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
995 verifyFormat(
996 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
997 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
998 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
999 verifyFormat(
1000 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1001 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1002 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1003}
1004
Daniel Jasperbac016b2012-12-03 18:12:45 +00001005TEST_F(FormatTest, UnderstandsEquals) {
1006 verifyFormat(
1007 "aaaaaaaaaaaaaaaaa =\n"
1008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1009 verifyFormat(
1010 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001012 verifyFormat(
1013 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001014 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001015 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001016 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001017
Daniel Jasper9cda8002013-01-07 13:08:40 +00001018 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001019 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001020 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001021 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001022}
1023
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001024TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001025 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1026 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001027
Daniel Jasper1321eb52012-12-18 21:05:13 +00001028 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1029 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001030
1031 verifyFormat(
1032 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1033 " Parameter2);");
1034
1035 verifyFormat(
1036 "ShortObject->shortFunction(\n"
1037 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1038 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1039
1040 verifyFormat("loooooooooooooongFunction(\n"
1041 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1042
1043 verifyFormat(
1044 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1045 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1046
Daniel Jasper46a46a22013-01-07 07:13:20 +00001047 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001048 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001049 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001050 verifyFormat(
1051 "aaaaaaaaaaa->aaaaaaaaa(\n"
1052 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1053 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001054}
1055
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001056TEST_F(FormatTest, WrapsTemplateDeclarations) {
1057 verifyFormat("template <typename T>\n"
1058 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1059 verifyFormat(
1060 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1061 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
1062 verifyFormat(
1063 "template <typename T>\n"
1064 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1065 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001066 verifyFormat(
1067 "template <typename T>\n"
1068 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1069 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001071 verifyFormat("template <typename T>\n"
1072 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1073 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001074 verifyFormat(
1075 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1076 " typename T4 = char>\n"
1077 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001078}
1079
Daniel Jasperbac016b2012-12-03 18:12:45 +00001080TEST_F(FormatTest, UnderstandsTemplateParameters) {
1081 verifyFormat("A<int> a;");
1082 verifyFormat("A<A<A<int> > > a;");
1083 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1084 verifyFormat("bool x = a < 1 || 2 > a;");
1085 verifyFormat("bool x = 5 < f<int>();");
1086 verifyFormat("bool x = f<int>() > 5;");
1087 verifyFormat("bool x = 5 < a<int>::x;");
1088 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1089 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1090
1091 verifyGoogleFormat("A<A<int>> a;");
1092 verifyGoogleFormat("A<A<A<int>>> a;");
1093 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1094
1095 verifyFormat("test >> a >> b;");
1096 verifyFormat("test << a >> b;");
1097
1098 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001099 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001100}
1101
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001102TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001103 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001104 verifyFormat("f(-1, -2, -3);");
1105 verifyFormat("a[-1] = 5;");
1106 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001107 verifyFormat("if (i == -1) {}");
1108 verifyFormat("if (i != -1) {}");
1109 verifyFormat("if (i > -1) {}");
1110 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001111 verifyFormat("++(a->f());");
1112 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001113 verifyFormat("(a->f())++;");
1114 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001115 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001116
1117 verifyFormat("a-- > b;");
1118 verifyFormat("b ? -a : c;");
1119 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001120 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001121 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001122 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001123
1124 verifyFormat("return -1;");
1125 verifyFormat("switch (a) {\n"
1126 "case -1:\n"
1127 " break;\n"
1128 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001129
1130 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1131 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001132
1133 verifyFormat("int a = /* confusing comment */ -1;");
1134 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1135 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001136}
1137
1138TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001139 verifyFormat("bool operator<();");
1140 verifyFormat("bool operator>();");
1141 verifyFormat("bool operator=();");
1142 verifyFormat("bool operator==();");
1143 verifyFormat("bool operator!=();");
1144 verifyFormat("int operator+();");
1145 verifyFormat("int operator++();");
1146 verifyFormat("bool operator();");
1147 verifyFormat("bool operator()();");
1148 verifyFormat("bool operator[]();");
1149 verifyFormat("operator bool();");
1150 verifyFormat("operator SomeType<int>();");
1151 verifyFormat("void *operator new(std::size_t size);");
1152 verifyFormat("void *operator new[](std::size_t size);");
1153 verifyFormat("void operator delete(void *ptr);");
1154 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001155}
1156
Daniel Jasper088dab52013-01-11 16:09:04 +00001157TEST_F(FormatTest, UnderstandsNewAndDelete) {
1158 verifyFormat("A *a = new A;");
1159 verifyFormat("A *a = new (placement) A;");
1160 verifyFormat("delete a;");
1161 verifyFormat("delete (A *)a;");
1162}
1163
Daniel Jasper5d334402013-01-02 08:57:10 +00001164TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001165 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001166 verifyFormat("f(a, *a);");
1167 verifyFormat("f(*a);");
1168 verifyFormat("int a = b * 10;");
1169 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001170 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001171 verifyFormat("int a += b * c;");
1172 verifyFormat("int a -= b * c;");
1173 verifyFormat("int a *= b * c;");
1174 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001175 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001176 verifyFormat("int a = *b * c;");
1177 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001178 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001179 verifyFormat("return 10 * b;");
1180 verifyFormat("return *b * *c;");
1181 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001182 verifyFormat("f(b ? *c : *d);");
1183 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001184 verifyFormat("*b = a;");
1185 verifyFormat("a * ~b;");
1186 verifyFormat("a * !b;");
1187 verifyFormat("a * +b;");
1188 verifyFormat("a * -b;");
1189 verifyFormat("a * ++b;");
1190 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001191 verifyFormat("a[4] * b;");
1192 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001193 verifyFormat("a * [self dostuff];");
1194 verifyFormat("a * (a + b);");
1195 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001196 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001197
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001198 verifyFormat("InvalidRegions[*R] = 0;");
1199
Daniel Jasper8b39c662012-12-10 18:59:13 +00001200 verifyFormat("A<int *> a;");
1201 verifyFormat("A<int **> a;");
1202 verifyFormat("A<int *, int *> a;");
1203 verifyFormat("A<int **, int **> a;");
1204
Daniel Jasper2db356d2013-01-08 20:03:18 +00001205 verifyFormat(
1206 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1208
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001209 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001210 verifyGoogleFormat("A<int*> a;");
1211 verifyGoogleFormat("A<int**> a;");
1212 verifyGoogleFormat("A<int*, int*> a;");
1213 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001214 verifyGoogleFormat("f(b ? *c : *d);");
1215 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001216 verifyGoogleFormat("Type* t = **x;");
1217 verifyGoogleFormat("Type* t = *++*x;");
1218 verifyGoogleFormat("*++*x;");
1219 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1220 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001221
1222 verifyFormat("a = *(x + y);");
1223 verifyFormat("a = &(x + y);");
1224 verifyFormat("*(x + y).call();");
1225 verifyFormat("&(x + y)->call();");
1226 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001227
1228 verifyFormat("f(b * /* confusing comment */ ++c);");
1229 verifyFormat(
1230 "int *MyValues = {\n"
1231 " *A, // Operator detection might be confused by the '{'\n"
1232 " *BB // Operator detection might be confused by previous comment\n"
1233 "};");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001234}
1235
Daniel Jasper4981bd02013-01-13 08:01:36 +00001236TEST_F(FormatTest, FormatsCasts) {
1237 verifyFormat("Type *A = static_cast<Type *>(P);");
1238 verifyFormat("Type *A = (Type *)P;");
1239 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1240 verifyFormat("int a = (int)(2.0f);");
1241
1242 // FIXME: These also need to be identified.
1243 verifyFormat("int a = (int) 2.0f;");
1244 verifyFormat("int a = (int) * b;");
1245
1246 // These are not casts.
1247 verifyFormat("void f(int *) {}");
1248 verifyFormat("void f(int *);");
1249 verifyFormat("void f(int *) = 0;");
1250 verifyFormat("void f(SmallVector<int>) {}");
1251 verifyFormat("void f(SmallVector<int>);");
1252 verifyFormat("void f(SmallVector<int>) = 0;");
1253}
1254
Daniel Jasper46ef8522013-01-10 13:08:12 +00001255TEST_F(FormatTest, FormatsFunctionTypes) {
1256 // FIXME: Determine the cases that need a space after the return type and fix.
1257 verifyFormat("A<bool()> a;");
1258 verifyFormat("A<SomeType()> a;");
1259 verifyFormat("A<void(*)(int, std::string)> a;");
1260
1261 verifyFormat("int(*func)(void *);");
1262}
1263
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001264TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001265 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001266 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001267 verifyFormat(
1268 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1269 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001270 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001271}
1272
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001273TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1274 verifyFormat("(a)->b();");
1275 verifyFormat("--a;");
1276}
1277
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001278TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001279 verifyFormat("#include <string>\n"
1280 "#include <a/b/c.h>\n"
1281 "#include \"a/b/string\"\n"
1282 "#include \"string.h\"\n"
1283 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001284 "#include <a-a>\n"
1285 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001286
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001287 verifyFormat("#import <string>");
1288 verifyFormat("#import <a/b/c.h>");
1289 verifyFormat("#import \"a/b/string\"");
1290 verifyFormat("#import \"string.h\"");
1291 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001292}
1293
Alexander Kornienko15757312012-12-06 18:03:27 +00001294//===----------------------------------------------------------------------===//
1295// Error recovery tests.
1296//===----------------------------------------------------------------------===//
1297
Daniel Jasper700e7102013-01-10 09:26:47 +00001298TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001299 verifyFormat("void f() { return; }\n42");
1300 verifyFormat("void f() {\n"
1301 " if (0)\n"
1302 " return;\n"
1303 "}\n"
1304 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001305 verifyFormat("void f() { return }\n42");
1306 verifyFormat("void f() {\n"
1307 " if (0)\n"
1308 " return\n"
1309 "}\n"
1310 "42");
1311}
1312
1313TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1314 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1315 EXPECT_EQ("void f() {\n"
1316 " if (a)\n"
1317 " return\n"
1318 "}", format("void f ( ) { if ( a ) return }"));
1319 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1320 EXPECT_EQ("namespace N {\n"
1321 "void f() {}\n"
1322 "void g()\n"
1323 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001324}
1325
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001326TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1327 verifyFormat("int aaaaaaaa =\n"
1328 " // Overly long comment\n"
1329 " b;", getLLVMStyleWithColumns(20));
1330 verifyFormat("function(\n"
1331 " ShortArgument,\n"
1332 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1333}
1334
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001335TEST_F(FormatTest, IncorrectAccessSpecifier) {
1336 verifyFormat("public:");
1337 verifyFormat("class A {\n"
1338 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001339 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001340 "};");
1341 verifyFormat("public\n"
1342 "int qwerty;");
1343 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001344 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001345 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001346 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001347 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001348 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001349}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001350
Alexander Kornienko393b0082012-12-04 15:40:36 +00001351TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1352 verifyFormat("{");
1353}
1354
1355TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001356 verifyFormat("do {}");
1357 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001358 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001359 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001360 "wheeee(fun);");
1361 verifyFormat("do {\n"
1362 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001363 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001364}
1365
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001366TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001367 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001368 verifyFormat("switch {\n foo;\n foo();\n}");
1369 verifyFormat("for {\n foo;\n foo();\n}");
1370 verifyFormat("while {\n foo;\n foo();\n}");
1371 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001372}
1373
Daniel Jasper1f42f112013-01-04 18:52:56 +00001374TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1375 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001376 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001377}
1378
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001379TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001380 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1381 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1382 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1383 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001384
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001385 EXPECT_EQ("{\n"
1386 " {\n"
1387 " breakme(\n"
1388 " qwe);\n"
1389 "}\n", format("{\n"
1390 " {\n"
1391 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001392 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001393}
1394
Manuel Klimek2851c162013-01-10 14:36:46 +00001395TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1396 verifyFormat(
1397 "int x = {\n"
1398 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001399 " b(alongervariable)\n"
1400 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001401}
1402
1403TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1404 verifyFormat(
1405 "Aaa({\n"
1406 " int i;\n"
1407 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1408 " ccccccccccccccccc));");
1409}
1410
Manuel Klimek517e8942013-01-11 17:54:10 +00001411TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1412 verifyFormat("void f() { return 42; }");
1413 verifyFormat("void f() {\n"
1414 " // Comment\n"
1415 "}");
1416 verifyFormat("{\n"
1417 "#error {\n"
1418 " int a;\n"
1419 "}");
1420 verifyFormat("{\n"
1421 " int a;\n"
1422 "#error {\n"
1423 "}");
1424}
1425
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001426TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1427 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001428 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001429 verifyFormat("class foo a = { bar };\nint n;");
1430 verifyFormat("union foo a = { bar };\nint n;");
1431
1432 // Elaborate types inside function definitions.
1433 verifyFormat("struct foo f() {}\nint n;");
1434 verifyFormat("class foo f() {}\nint n;");
1435 verifyFormat("union foo f() {}\nint n;");
1436
1437 // Templates.
1438 verifyFormat("template <class X> void f() {}\nint n;");
1439 verifyFormat("template <struct X> void f() {}\nint n;");
1440 verifyFormat("template <union X> void f() {}\nint n;");
1441
1442 // Actual definitions...
1443 verifyFormat("struct {} n;");
1444 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1445 verifyFormat("union Z {\n int n;\n} x;");
1446 verifyFormat("class MACRO Z {} n;");
1447 verifyFormat("class MACRO(X) Z {} n;");
1448 verifyFormat("class __attribute__(X) Z {} n;");
1449 verifyFormat("class __declspec(X) Z {} n;");
1450
1451 // Elaborate types where incorrectly parsing the structural element would
1452 // break the indent.
1453 verifyFormat("if (true)\n"
1454 " class X x;\n"
1455 "else\n"
1456 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001457}
1458
Manuel Klimek407a31a2013-01-15 15:50:27 +00001459TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1460 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1461 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1462 EXPECT_EQ("#error 1", format(" # error 1"));
1463 EXPECT_EQ("#warning 1", format(" # warning 1"));
1464}
1465
Manuel Klimek517e8942013-01-11 17:54:10 +00001466// FIXME: This breaks the order of the unwrapped lines:
1467// TEST_F(FormatTest, OrderUnwrappedLines) {
1468// verifyFormat("{\n"
1469// " bool a; //\n"
1470// "#error {\n"
1471// " int a;\n"
1472// "}");
1473// }
1474
Nico Webercf4a79c2013-01-08 17:56:31 +00001475//===----------------------------------------------------------------------===//
1476// Objective-C tests.
1477//===----------------------------------------------------------------------===//
1478
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001479TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1480 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1481 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1482 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001483 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001484 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1485 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1486 format("-(NSInteger)Method3:(id)anObject;"));
1487 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1488 format("-(NSInteger)Method4:(id)anObject;"));
1489 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1490 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1491 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1492 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001493 EXPECT_EQ(
1494 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1495 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001496
1497 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001498 EXPECT_EQ(
1499 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1500 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1501 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1502 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1503 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1504 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1505 format(
1506 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1507 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1508 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1509 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1510 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1511 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001512
1513 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001514 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001515 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1516 // protocol lists (but not for template classes):
1517 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001518
1519 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001520 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001521
1522 // If there's no return type (very rare in practice!), LLVM and Google style
1523 // agree.
1524 verifyFormat("- foo:(int)f;");
1525 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001526}
1527
Daniel Jasper886568d2013-01-09 08:36:49 +00001528TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001529 verifyFormat("int (^Block)(int, int);");
1530 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001531}
1532
Nico Weber27d13672013-01-09 20:25:35 +00001533TEST_F(FormatTest, FormatObjCInterface) {
1534 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001535 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001536 "@public\n"
1537 " int field1;\n"
1538 "@protected\n"
1539 " int field2;\n"
1540 "@private\n"
1541 " int field3;\n"
1542 "@package\n"
1543 " int field4;\n"
1544 "}\n"
1545 "+ (id)init;\n"
1546 "@end");
1547
Nico Weber27d13672013-01-09 20:25:35 +00001548 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1549 " @public\n"
1550 " int field1;\n"
1551 " @protected\n"
1552 " int field2;\n"
1553 " @private\n"
1554 " int field3;\n"
1555 " @package\n"
1556 " int field4;\n"
1557 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001558 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001559 "@end");
1560
1561 verifyFormat("@interface Foo\n"
1562 "+ (id)init;\n"
1563 "// Look, a comment!\n"
1564 "- (int)answerWith:(int)i;\n"
1565 "@end");
1566
1567 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001568 "@end\n"
1569 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001570 "@end");
1571
1572 verifyFormat("@interface Foo : Bar\n"
1573 "+ (id)init;\n"
1574 "@end");
1575
Nico Weber5f500df2013-01-10 20:12:55 +00001576 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001577 "+ (id)init;\n"
1578 "@end");
1579
Nico Weber5f500df2013-01-10 20:12:55 +00001580 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001581 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001582 "@end");
1583
Nico Webered91bba2013-01-10 19:19:14 +00001584 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001585 "+ (id)init;\n"
1586 "@end");
1587
Nico Webered91bba2013-01-10 19:19:14 +00001588 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001589 "+ (id)init;\n"
1590 "@end");
1591
Nico Weber5f500df2013-01-10 20:12:55 +00001592 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001593 "+ (id)init;\n"
1594 "@end");
1595
Nico Weber5f500df2013-01-10 20:12:55 +00001596 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001597 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001598 "@end");
1599
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001600 verifyFormat("@interface Foo {\n"
1601 " int _i;\n"
1602 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001603 "+ (id)init;\n"
1604 "@end");
1605
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001606 verifyFormat("@interface Foo : Bar {\n"
1607 " int _i;\n"
1608 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001609 "+ (id)init;\n"
1610 "@end");
1611
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001612 verifyFormat("@interface Foo : Bar <Baz, Quux> {\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 (HackStuff) {\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 () {\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) <MyProtocol> {\n"
1631 " int _i;\n"
1632 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001633 "+ (id)init;\n"
1634 "@end");
1635}
1636
Nico Weber50767d82013-01-09 23:25:37 +00001637TEST_F(FormatTest, FormatObjCImplementation) {
1638 verifyFormat("@implementation Foo : NSObject {\n"
1639 "@public\n"
1640 " int field1;\n"
1641 "@protected\n"
1642 " int field2;\n"
1643 "@private\n"
1644 " int field3;\n"
1645 "@package\n"
1646 " int field4;\n"
1647 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001648 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001649 "@end");
1650
1651 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1652 " @public\n"
1653 " int field1;\n"
1654 " @protected\n"
1655 " int field2;\n"
1656 " @private\n"
1657 " int field3;\n"
1658 " @package\n"
1659 " int field4;\n"
1660 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001661 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001662 "@end");
1663
1664 verifyFormat("@implementation Foo\n"
1665 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001666 " if (true)\n"
1667 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001668 "}\n"
1669 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001670 "- (int)answerWith:(int)i {\n"
1671 " return i;\n"
1672 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001673 "+ (int)answerWith:(int)i {\n"
1674 " return i;\n"
1675 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001676 "@end");
1677
1678 verifyFormat("@implementation Foo\n"
1679 "@end\n"
1680 "@implementation Bar\n"
1681 "@end");
1682
1683 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001684 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001685 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001686 "@end");
1687
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001688 verifyFormat("@implementation Foo {\n"
1689 " int _i;\n"
1690 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001691 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001692 "@end");
1693
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001694 verifyFormat("@implementation Foo : Bar {\n"
1695 " int _i;\n"
1696 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001697 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001698 "@end");
1699
Nico Webered91bba2013-01-10 19:19:14 +00001700 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001701 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001702 "@end");
1703}
1704
Nico Weber1abe6ea2013-01-09 21:15:03 +00001705TEST_F(FormatTest, FormatObjCProtocol) {
1706 verifyFormat("@protocol Foo\n"
1707 "@property(weak) id delegate;\n"
1708 "- (NSUInteger)numberOfThings;\n"
1709 "@end");
1710
Nico Weber5f500df2013-01-10 20:12:55 +00001711 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001712 "- (NSUInteger)numberOfThings;\n"
1713 "@end");
1714
Nico Weber5f500df2013-01-10 20:12:55 +00001715 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001716 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001717 "@end");
1718
Nico Weber1abe6ea2013-01-09 21:15:03 +00001719 verifyFormat("@protocol Foo;\n"
1720 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001721
1722 verifyFormat("@protocol Foo\n"
1723 "@end\n"
1724 "@protocol Bar\n"
1725 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001726
1727 verifyFormat("@protocol myProtocol\n"
1728 "- (void)mandatoryWithInt:(int)i;\n"
1729 "@optional\n"
1730 "- (void)optional;\n"
1731 "@required\n"
1732 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001733 "@optional\n"
1734 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001735 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001736}
1737
Nico Weberbcfdd262013-01-12 06:18:40 +00001738TEST_F(FormatTest, FormatObjCMethodExpr) {
1739 verifyFormat("[foo bar:baz];");
1740 verifyFormat("return [foo bar:baz];");
1741 verifyFormat("f([foo bar:baz]);");
1742 verifyFormat("f(2, [foo bar:baz]);");
1743 verifyFormat("f(2, a ? b : c);");
1744 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1745
1746 verifyFormat("[foo bar:baz], [foo bar:baz];");
1747 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1748 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1749 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1750 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1751 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1752 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1753 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1754 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1755 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1756 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1757 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1758 verifyFormat("[foo bar:baz] ? [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];");
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 // Whew!
1778
1779 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1780 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1781 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1782 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1783 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001784 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001785 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001786
Nico Weberbcfdd262013-01-12 06:18:40 +00001787 verifyFormat("arr[[self indexForFoo:a]];");
1788 verifyFormat("throw [self errorFor:a];");
1789 verifyFormat("@throw [self errorFor:a];");
1790
Nico Webere8ccc812013-01-12 22:48:47 +00001791 // This tests that the formatter doesn't break after "backing" but before ":",
1792 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001793 verifyFormat(
1794 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001795 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1796 " backing:NSBackingStoreBuffered defer:YES]))");
1797
Nico Webere8ccc812013-01-12 22:48:47 +00001798 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1799 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001800
1801}
1802
Nico Weber581f5572013-01-07 15:56:25 +00001803TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001804 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001805 verifyFormat("@catch");
1806 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001807 verifyFormat("@compatibility_alias");
1808 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001809 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001810 verifyFormat("@encode");
1811 verifyFormat("@end");
1812 verifyFormat("@finally");
1813 verifyFormat("@implementation");
1814 verifyFormat("@import");
1815 verifyFormat("@interface");
1816 verifyFormat("@optional");
1817 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001818 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001819 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001820 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001821 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001822 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001823 verifyFormat("@required");
1824 verifyFormat("@selector");
1825 verifyFormat("@synchronized");
1826 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001827 verifyFormat("@throw");
1828 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001829
Nico Webercb4d6902013-01-08 19:40:21 +00001830 verifyFormat("@\"String\"");
1831 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001832 verifyFormat("@+4.8");
1833 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001834 verifyFormat("@1LL");
1835 verifyFormat("@.5");
1836 verifyFormat("@'c'");
1837 verifyFormat("@true");
1838 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001839 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001840 verifyFormat("@[");
1841 verifyFormat("@{");
1842
Nico Weber581f5572013-01-07 15:56:25 +00001843 EXPECT_EQ("@interface", format("@ interface"));
1844
1845 // The precise formatting of this doesn't matter, nobody writes code like
1846 // this.
1847 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001848}
1849
Nico Weberc31689a2013-01-08 19:15:23 +00001850TEST_F(FormatTest, ObjCSnippets) {
1851 // FIXME: Make the uncommented lines below pass.
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001852 verifyFormat("@autoreleasepool {\n"
1853 " foo();\n"
1854 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001855 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001856 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001857 verifyFormat("@dynamic textColor;");
Nico Weberc31689a2013-01-08 19:15:23 +00001858 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001859 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weberc31689a2013-01-08 19:15:23 +00001860 //verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001861 verifyFormat("@synchronized(self) {\n"
1862 " f();\n"
1863 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001864
Nico Weber70848232013-01-10 21:30:42 +00001865 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1866 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1867
Nico Webercf4a79c2013-01-08 17:56:31 +00001868 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001869 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1870 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001871}
1872
Daniel Jaspercd162382013-01-07 13:26:07 +00001873} // end namespace tooling
1874} // end namespace clang