blob: f67cd8c222d246a6ec23fcb7c36f0589d0e88d3d [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"
Manuel Klimekca547db2013-01-16 14:55:28 +000013#include "../Tooling/RewriterTestContext.h"
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000014#include "clang/Lex/Lexer.h"
15#include "llvm/Support/Debug.h"
16#include "gtest/gtest.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000017
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 "}");
Nico Weber94fb7292013-01-18 05:50:57 +0000277 verifyFormat("switch (x) {\n"
278 "case 1: {\n"
279 " f();\n"
280 " {\n"
281 " g();\n"
282 " h();\n"
283 " }\n"
284 " break;\n"
285 "}\n"
286 "}");
287 verifyFormat("switch (x) {\n"
288 "case 1: {\n"
289 " f();\n"
290 " if (foo) {\n"
291 " g();\n"
292 " h();\n"
293 " }\n"
294 " break;\n"
295 "}\n"
296 "}");
297 verifyFormat("switch (x) {\n"
298 "case 1: {\n"
299 " f();\n"
300 " g();\n"
301 "} break;\n"
302 "}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000303 verifyFormat("switch (test)\n"
304 " ;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000305 verifyGoogleFormat("switch (x) {\n"
306 " case 1:\n"
307 " f();\n"
308 " break;\n"
309 " case kFoo:\n"
310 " case ns::kBar:\n"
311 " case kBaz:\n"
312 " break;\n"
313 " default:\n"
314 " g();\n"
315 " break;\n"
316 "}");
317 verifyGoogleFormat("switch (x) {\n"
318 " case 1: {\n"
319 " f();\n"
320 " break;\n"
321 " }\n"
322 "}");
323 verifyGoogleFormat("switch (test)\n"
324 " ;");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000325}
326
Alexander Kornienko15757312012-12-06 18:03:27 +0000327TEST_F(FormatTest, FormatsLabels) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000328 verifyFormat("void f() {\n"
329 " some_code();\n"
330 "test_label:\n"
331 " some_other_code();\n"
332 " {\n"
333 " some_more_code();\n"
334 " another_label:\n"
335 " some_more_code();\n"
336 " }\n"
337 "}");
338 verifyFormat("some_code();\n"
339 "test_label:\n"
340 "some_other_code();");
341}
342
Alexander Kornienko15757312012-12-06 18:03:27 +0000343//===----------------------------------------------------------------------===//
344// Tests for comments.
345//===----------------------------------------------------------------------===//
346
347TEST_F(FormatTest, UnderstandsSingleLineComments) {
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000348 verifyFormat("// line 1\n"
349 "// line 2\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000350 "void f() {}\n");
Alexander Kornienko15757312012-12-06 18:03:27 +0000351
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000352 verifyFormat("void f() {\n"
353 " // Doesn't do anything\n"
354 "}");
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000355 verifyFormat("void f(int i, // some comment (probably for i)\n"
356 " int j, // some comment (probably for j)\n"
Daniel Jasper487f64b2013-01-13 16:10:20 +0000357 " int k); // some comment (probably for k)");
358 verifyFormat("void f(int i,\n"
359 " // some comment (probably for j)\n"
360 " int j,\n"
361 " // some comment (probably for k)\n"
362 " int k);");
Alexander Kornienko15757312012-12-06 18:03:27 +0000363
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000364 verifyFormat("int i // This is a fancy variable\n"
365 " = 5; // with nicely aligned comment.");
366
367 verifyFormat("// Leading comment.\n"
368 "int a; // Trailing comment.");
369 verifyFormat("int a; // Trailing comment\n"
370 " // on 2\n"
371 " // or 3 lines.\n"
372 "int b;");
373 verifyFormat("int a; // Trailing comment\n"
374 "\n"
375 "// Leading comment.\n"
376 "int b;");
377 verifyFormat("int a; // Comment.\n"
378 " // More details.\n"
379 "int bbbb; // Another comment.");
380 verifyFormat(
381 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
382 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
383 "int cccccccccccccccccccccccccccccc; // comment\n"
384 "int ddd; // looooooooooooooooooooooooong comment\n"
385 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
386 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
387 "int ccccccccccccccccccc; // comment");
388
Daniel Jasper7d1185d2013-01-18 09:19:33 +0000389 verifyFormat("#include \"a\" // comment\n"
390 "#include \"a/b/c\" // comment");
391 verifyFormat("#include <a> // comment\n"
392 "#include <a/b/c> // comment");
Alexander Kornienko15757312012-12-06 18:03:27 +0000393
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000394 verifyFormat("enum E {\n"
395 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000396 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000397 " VAL_B\n"
398 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000399
400 verifyFormat(
401 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000402 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000403 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
404 " // Comment inside a statement.\n"
405 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000406
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000407 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000408 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000409
410 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000411}
412
413TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000414 verifyFormat("f(/*test=*/ true);");
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000415 EXPECT_EQ(
416 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
417 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
418 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n"
419 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
420 EXPECT_EQ(
421 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
422 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
423 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
424 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
425}
426
427TEST_F(FormatTest, CommentsInStaticInitializers) {
428 EXPECT_EQ(
429 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
430 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
431 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
432 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
433 " aaaaaaaaaaaaaaaaaaaa };",
434 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
435 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
436 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
437 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
438 " aaaaaaaaaaaaaaaaaaaa };"));
439 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
440 " bbbbbbbbbbb, ccccccccccc };");
441 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
442 " // comment for bb....\n"
443 " bbbbbbbbbbb, ccccccccccc };");
444 verifyGoogleFormat(
445 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
446 " bbbbbbbbbbb,\n"
447 " ccccccccccc };");
448 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
449 " // comment for bb....\n"
450 " bbbbbbbbbbb,\n"
451 " ccccccccccc };");
452
Alexander Kornienko15757312012-12-06 18:03:27 +0000453}
454
Alexander Kornienko15757312012-12-06 18:03:27 +0000455//===----------------------------------------------------------------------===//
456// Tests for classes, namespaces, etc.
457//===----------------------------------------------------------------------===//
458
459TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000460 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000461}
462
463TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
464 verifyFormat("class A {\n"
465 "public:\n"
466 "protected:\n"
467 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000468 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000469 "};");
470 verifyGoogleFormat("class A {\n"
471 " public:\n"
472 " protected:\n"
473 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000474 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000475 "};");
476}
477
478TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000479 verifyFormat("class A : public B {};");
480 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000481}
482
Manuel Klimekde768542013-01-07 18:10:23 +0000483TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000484 verifyFormat("class A {} a, b;");
485 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000486 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000487}
488
Alexander Kornienko15757312012-12-06 18:03:27 +0000489TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000490 verifyFormat("enum {\n"
491 " Zero,\n"
492 " One = 1,\n"
493 " Two = One + 1,\n"
494 " Three = (One + Two),\n"
495 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
496 " Five = (One, Two, Three, Four, 5)\n"
497 "};");
498 verifyFormat("enum Enum {\n"
499 "};");
500 verifyFormat("enum {\n"
501 "};");
502}
503
Nico Weberefaddc02013-01-14 05:49:49 +0000504TEST_F(FormatTest, FormatsBitfields) {
505 verifyFormat("struct Bitfields {\n"
506 " unsigned sClass : 8;\n"
507 " unsigned ValueKind : 2;\n"
508 "};");
509}
510
Alexander Kornienko15757312012-12-06 18:03:27 +0000511TEST_F(FormatTest, FormatsNamespaces) {
512 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000513 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000514 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000515 "}");
516 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000517 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000518 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000519 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000520 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000521 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000522 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000523 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000524 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000525 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000526 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000527}
528
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000529TEST_F(FormatTest, FormatsExternC) {
530 verifyFormat("extern \"C\" {\nint a;");
531}
532
Nico Webera9ccdd12013-01-07 16:36:17 +0000533TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000534 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
535 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000536 verifyFormat("try {\n"
537 " throw a * b;\n"
538 "}\n"
539 "catch (int a) {\n"
540 " // Do nothing.\n"
541 "}\n"
542 "catch (...) {\n"
543 " exit(42);\n"
544 "}");
545
546 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000547 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000548 "catch (...) {\n"
549 " return 5;\n"
550 "}");
551 verifyFormat("class A {\n"
552 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000553 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000554 " catch (...) {\n"
555 " throw;\n"
556 " }\n"
557 "};\n");
558}
559
560TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000561 verifyFormat("@try {\n"
562 " f();\n"
563 "}\n"
564 "@catch (NSException e) {\n"
565 " @throw;\n"
566 "}\n"
567 "@finally {\n"
568 " exit(42);\n"
569 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000570}
571
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000572TEST_F(FormatTest, StaticInitializers) {
573 verifyFormat("static SomeClass SC = { 1, 'a' };");
574
575 // FIXME: Format like enums if the static initializer does not fit on a line.
576 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000577 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000578 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
579 "};");
580
581 verifyFormat(
582 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
583 " looooooooooooooooooooooooooooooooooongname,\n"
584 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000585}
586
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000587TEST_F(FormatTest, NestedStaticInitializers) {
588 verifyFormat("static A x = { { {} } };\n");
589 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000590 "static A x = { { { init1, init2, init3, init4 },\n"
591 " { init1, init2, init3, init4 } } };");
592
Nico Weber6a21a552013-01-18 02:43:57 +0000593 // FIXME: Fix this in general and verify that it works in LLVM style again.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000594 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000595 "somes Status::global_reps[3] = {\n"
596 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
597 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
598 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
599 "};");
600 verifyFormat(
601 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
602 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
603 " } };");
604
Nico Weber6a21a552013-01-18 02:43:57 +0000605 // FIXME: We might at some point want to handle this similar to parameter
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000606 // lists, where we have an option to put each on a single line.
607 verifyFormat("struct {\n"
608 " unsigned bit;\n"
609 " const char *const name;\n"
610 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
611 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
612}
613
Manuel Klimeka080a182013-01-02 16:30:12 +0000614TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
615 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
616 " \\\n"
617 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
618}
619
Daniel Jasper71607512013-01-07 10:48:50 +0000620TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000621 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
622 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000623}
624
Manuel Klimeka080a182013-01-02 16:30:12 +0000625TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
626 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000627 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000628}
629
630TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
631 EXPECT_EQ("#line 42 \"test\"\n",
632 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000633 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000634 format("# \\\n define \\\n A \\\n B\n",
635 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000636}
637
638TEST_F(FormatTest, EndOfFileEndsPPDirective) {
639 EXPECT_EQ("#line 42 \"test\"",
640 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000641 EXPECT_EQ("#define A B",
642 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000643}
644
Manuel Klimek060143e2013-01-02 18:33:23 +0000645TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000646 // If the macro fits in one line, we still do not get the full
647 // line, as only the next line decides whether we need an escaped newline and
648 // thus use the last column.
649 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000650
Manuel Klimekd544c572013-01-07 09:24:17 +0000651 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
652 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000653 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000654
655 verifyFormat("#define A A\n#define A A");
656 verifyFormat("#define A(X) A\n#define A A");
657
658 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
659 verifyFormat("#define Something \\\n"
660 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000661}
662
663TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000664 EXPECT_EQ("// some comment\n"
665 "#include \"a.h\"\n"
666 "#define A(A,\\\n"
667 " B)\n"
668 "#include \"b.h\"\n"
669 "// some comment\n",
670 format(" // some comment\n"
671 " #include \"a.h\"\n"
672 "#define A(A,\\\n"
673 " B)\n"
674 " #include \"b.h\"\n"
675 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000676}
677
Manuel Klimekd4397b92013-01-04 23:34:14 +0000678TEST_F(FormatTest, LayoutSingleHash) {
679 EXPECT_EQ("#\na;", format("#\na;"));
680}
681
682TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
683 EXPECT_EQ("#define A \\\n"
684 " c; \\\n"
685 " e;\n"
686 "f;", format("#define A c; e;\n"
687 "f;", getLLVMStyleWithColumns(14)));
688}
689
690TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000691 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000692}
693
694TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000695 EXPECT_EQ("# define A\\\n b;",
696 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000697}
698
699TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000700 EXPECT_EQ("int x,\n"
701 "#define A\n"
702 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000703}
704
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000705TEST_F(FormatTest, HashInMacroDefinition) {
706 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
707 verifyFormat("#define A \\\n"
708 " { \\\n"
709 " f(#c);\\\n"
710 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000711
712 verifyFormat("#define A(X) \\\n"
713 " void function##X()", getLLVMStyleWithColumns(22));
714
715 verifyFormat("#define A(a, b, c) \\\n"
716 " void a##b##c()", getLLVMStyleWithColumns(22));
717
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000718 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000719}
720
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000721TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
722 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
723}
724
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000725TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000726 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000727}
728
Manuel Klimeka5342db2013-01-06 20:07:31 +0000729TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
730 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
731 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
732 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
733 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
734}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000735
Manuel Klimek95419382013-01-07 07:56:50 +0000736TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000737 EXPECT_EQ(
738 "#define A \\\n int i; \\\n int j;",
739 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000740}
741
Manuel Klimekd544c572013-01-07 09:24:17 +0000742TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
743 verifyFormat("#define A \\\n"
744 " int v( \\\n"
745 " a); \\\n"
746 " int i;", getLLVMStyleWithColumns(11));
747}
748
Manuel Klimeka080a182013-01-02 16:30:12 +0000749TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000750 EXPECT_EQ(
751 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
752 " \\\n"
753 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
754 "\n"
755 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
756 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
757 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
758 "\\\n"
759 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
760 " \n"
761 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
762 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000763}
764
Manuel Klimek526ed112013-01-09 15:25:02 +0000765TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
766 EXPECT_EQ("int\n"
767 "#define A\n"
768 " a;",
769 format("int\n#define A\na;"));
770 verifyFormat(
771 "functionCallTo(someOtherFunction(\n"
772 " withSomeParameters, whichInSequence,\n"
773 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000774 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000775 " withMoreParamters,\n"
776 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000777 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000778}
779
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000780TEST_F(FormatTest, LayoutBlockInsideParens) {
781 EXPECT_EQ("functionCall({\n"
782 " int i;\n"
783 "});", format(" functionCall ( {int i;} );"));
784}
785
786TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000787 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000788 "int i;", format(" SOME_MACRO {int i;} int i;"));
789}
790
791TEST_F(FormatTest, LayoutNestedBlocks) {
792 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
793 " struct s {\n"
794 " int i;\n"
795 " };\n"
796 " s kBitsToOs[] = { { 10 } };\n"
797 " for (int i = 0; i < 10; ++i)\n"
798 " return;\n"
799 "}");
800}
801
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000802TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
803 EXPECT_EQ("{}", format("{}"));
804}
805
Alexander Kornienko15757312012-12-06 18:03:27 +0000806//===----------------------------------------------------------------------===//
807// Line break tests.
808//===----------------------------------------------------------------------===//
809
810TEST_F(FormatTest, FormatsFunctionDefinition) {
811 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
812 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000813 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000814}
815
816TEST_F(FormatTest, FormatsAwesomeMethodCall) {
817 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000818 "SomeLongMethodName(SomeReallyLongMethod(\n"
819 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
820 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000821}
822
Daniel Jasper1321eb52012-12-18 21:05:13 +0000823TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000824 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000825 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
826 getLLVMStyleWithColumns(45));
827 verifyFormat("Constructor()\n"
828 " : Inttializer(FitsOnTheLine) {}",
829 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000830
831 verifyFormat(
832 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000833 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000834
835 verifyFormat(
836 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000837 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
838 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
839 verifyGoogleFormat(
840 "SomeClass::Constructor()\n"
841 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
842 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
843 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper16618242013-01-16 17:00:50 +0000844 verifyGoogleFormat(
845 "SomeClass::Constructor()\n"
846 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
847 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
848 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000849
850 verifyFormat(
851 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000852 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000853 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000854
855 verifyFormat("Constructor()\n"
856 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
857 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
858 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000859 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000860
861 // Here a line could be saved by splitting the second initializer onto two
862 // lines, but that is not desireable.
863 verifyFormat("Constructor()\n"
864 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
865 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000866 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000867
868 verifyGoogleFormat("MyClass::MyClass(int var)\n"
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000869 " : some_var_(var), // 4 space indent\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000870 " some_other_var_(var + 1) { // lined up\n"
871 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000872
873 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000874 std::string input = "Constructor()\n"
875 " : aaaa(a,\n";
876 for (unsigned i = 0, e = 80; i != e; ++i) {
877 input += " a,\n";
878 }
879 input += " a) {}";
880 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000881}
882
Alexander Kornienko15757312012-12-06 18:03:27 +0000883TEST_F(FormatTest, BreaksAsHighAsPossible) {
884 verifyFormat(
885 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
886 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
887 " f();");
888}
889
Daniel Jasperbac016b2012-12-03 18:12:45 +0000890TEST_F(FormatTest, BreaksDesireably) {
891 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
892 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000893 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000894
895 verifyFormat(
896 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000898
899 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
900 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
901 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000902
903 verifyFormat(
904 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
906 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000908
909 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
910 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
911
Daniel Jasper723f0302013-01-02 14:40:02 +0000912 verifyFormat(
913 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
915
Daniel Jasper33182dd2012-12-05 14:57:28 +0000916 // This test case breaks on an incorrect memoization, i.e. an optimization not
917 // taking into account the StopAt value.
918 verifyFormat(
919 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000920 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
921 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
922 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000923
Daniel Jaspercd162382013-01-07 13:26:07 +0000924 verifyFormat("{\n {\n {\n"
925 " Annotation.SpaceRequiredBefore =\n"
926 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
927 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
928 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000929}
930
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000931TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
932 verifyGoogleFormat(
933 "aaaaaaaa(aaaaaaaaaaaaa,\n"
934 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
936 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
938 verifyGoogleFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +0000939 "aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa)\n"
940 " .aaaaaaaaaaaaaaaaaa();");
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000941 verifyGoogleFormat(
942 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
943 " ddddddddddddddddddddddddddddd),\n"
944 " test);");
945
946 verifyGoogleFormat(
947 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
948 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
949 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
950 verifyGoogleFormat("a(\"a\"\n"
951 " \"a\",\n"
952 " a);");
953}
954
Daniel Jasperc79afda2013-01-18 10:56:38 +0000955TEST_F(FormatTest, FormatsBuilderPattern) {
956 verifyFormat(
957 "return llvm::StringSwitch<Reference::Kind>(name)\n"
958 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
959 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
960 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
961 " .Default(ORDER_TEXT);\n");
962}
963
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000964TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
965 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
966 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +0000967 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
968 " GUARDED_BY(aaaaaaaaaaaaa);");
969 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
970 " GUARDED_BY(aaaaaaaaaaaaa) {}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000971}
972
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000973TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
974 verifyFormat(
975 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000976 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000977 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000978 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000979 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000980 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000981 verifyFormat(
982 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000983 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000984}
985
Daniel Jasper9cda8002013-01-07 13:08:40 +0000986TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
987 verifyFormat(
988 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
989 " SI->getAlignment(),\n"
990 " SI->getPointerAddressSpaceee());\n");
991 verifyFormat(
992 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
993 " Line.Tokens.front().Tok.getLocation(),\n"
994 " Line.Tokens.back().Tok.getLocation());");
995}
996
Daniel Jaspercf225b62012-12-24 13:43:52 +0000997TEST_F(FormatTest, AlignsAfterAssignments) {
998 verifyFormat(
999 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001000 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001001 verifyFormat(
1002 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001003 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001004 verifyFormat(
1005 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001006 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001007 verifyFormat(
1008 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001009 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001010 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +00001011 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1012 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1013 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001014}
1015
1016TEST_F(FormatTest, AlignsAfterReturn) {
1017 verifyFormat(
1018 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1019 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
1020 verifyFormat(
1021 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1022 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
1023}
1024
Daniel Jasper9c837d02013-01-09 07:06:56 +00001025TEST_F(FormatTest, BreaksConditionalExpressions) {
1026 verifyFormat(
1027 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1028 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1030 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
1031 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +00001032 verifyFormat(
1033 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
1034 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +00001035}
1036
Nico Weber7d37b8b2013-01-12 01:28:06 +00001037TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1038 verifyFormat("arr[foo ? bar : baz];");
1039 verifyFormat("f()[foo ? bar : baz];");
1040 verifyFormat("(a + b)[foo ? bar : baz];");
1041 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1042}
1043
Daniel Jasperbac016b2012-12-03 18:12:45 +00001044TEST_F(FormatTest, AlignsStringLiterals) {
1045 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1046 " \"short literal\");");
1047 verifyFormat(
1048 "looooooooooooooooooooooooongFunction(\n"
1049 " \"short literal\"\n"
1050 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1051}
1052
Alexander Kornienko15757312012-12-06 18:03:27 +00001053TEST_F(FormatTest, AlignsPipes) {
1054 verifyFormat(
1055 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1056 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1057 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1058 verifyFormat(
1059 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1060 " << aaaaaaaaaaaaaaaaaaaa;");
1061 verifyFormat(
1062 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1063 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1064 verifyFormat(
1065 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1066 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1067 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1068 verifyFormat(
1069 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1070 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1071 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1072}
1073
Daniel Jasperbac016b2012-12-03 18:12:45 +00001074TEST_F(FormatTest, UnderstandsEquals) {
1075 verifyFormat(
1076 "aaaaaaaaaaaaaaaaa =\n"
1077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1078 verifyFormat(
1079 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001081 verifyFormat(
1082 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001083 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001084 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001086
Daniel Jasper9cda8002013-01-07 13:08:40 +00001087 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001088 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001089 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001090 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001091}
1092
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001093TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001094 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1095 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001096
Daniel Jasper1321eb52012-12-18 21:05:13 +00001097 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1098 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001099
1100 verifyFormat(
1101 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1102 " Parameter2);");
1103
1104 verifyFormat(
1105 "ShortObject->shortFunction(\n"
1106 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1107 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1108
1109 verifyFormat("loooooooooooooongFunction(\n"
1110 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1111
1112 verifyFormat(
1113 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1114 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1115
Daniel Jasper46a46a22013-01-07 07:13:20 +00001116 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001117 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001118 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001119 verifyFormat(
1120 "aaaaaaaaaaa->aaaaaaaaa(\n"
1121 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1122 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001123}
1124
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001125TEST_F(FormatTest, WrapsTemplateDeclarations) {
1126 verifyFormat("template <typename T>\n"
1127 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1128 verifyFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +00001129 "template <typename T>\n"
1130 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1131 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001132 verifyFormat(
1133 "template <typename T>\n"
1134 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1135 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001136 verifyFormat(
1137 "template <typename T>\n"
1138 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1139 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1140 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001141 verifyFormat("template <typename T>\n"
1142 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1143 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001144 verifyFormat(
1145 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1146 " typename T4 = char>\n"
1147 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001148}
1149
Daniel Jasperbac016b2012-12-03 18:12:45 +00001150TEST_F(FormatTest, UnderstandsTemplateParameters) {
1151 verifyFormat("A<int> a;");
1152 verifyFormat("A<A<A<int> > > a;");
1153 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1154 verifyFormat("bool x = a < 1 || 2 > a;");
1155 verifyFormat("bool x = 5 < f<int>();");
1156 verifyFormat("bool x = f<int>() > 5;");
1157 verifyFormat("bool x = 5 < a<int>::x;");
1158 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1159 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1160
1161 verifyGoogleFormat("A<A<int>> a;");
1162 verifyGoogleFormat("A<A<A<int>>> a;");
1163 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1164
1165 verifyFormat("test >> a >> b;");
1166 verifyFormat("test << a >> b;");
1167
1168 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001169 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001170}
1171
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001172TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001173 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001174 verifyFormat("f(-1, -2, -3);");
1175 verifyFormat("a[-1] = 5;");
1176 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001177 verifyFormat("if (i == -1) {}");
1178 verifyFormat("if (i != -1) {}");
1179 verifyFormat("if (i > -1) {}");
1180 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001181 verifyFormat("++(a->f());");
1182 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001183 verifyFormat("(a->f())++;");
1184 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001185 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001186
1187 verifyFormat("a-- > b;");
1188 verifyFormat("b ? -a : c;");
1189 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001190 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001191 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001192 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001193
1194 verifyFormat("return -1;");
1195 verifyFormat("switch (a) {\n"
1196 "case -1:\n"
1197 " break;\n"
1198 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001199
1200 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1201 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001202
1203 verifyFormat("int a = /* confusing comment */ -1;");
1204 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1205 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001206}
1207
1208TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001209 verifyFormat("bool operator<();");
1210 verifyFormat("bool operator>();");
1211 verifyFormat("bool operator=();");
1212 verifyFormat("bool operator==();");
1213 verifyFormat("bool operator!=();");
1214 verifyFormat("int operator+();");
1215 verifyFormat("int operator++();");
1216 verifyFormat("bool operator();");
1217 verifyFormat("bool operator()();");
1218 verifyFormat("bool operator[]();");
1219 verifyFormat("operator bool();");
1220 verifyFormat("operator SomeType<int>();");
1221 verifyFormat("void *operator new(std::size_t size);");
1222 verifyFormat("void *operator new[](std::size_t size);");
1223 verifyFormat("void operator delete(void *ptr);");
1224 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001225}
1226
Daniel Jasper088dab52013-01-11 16:09:04 +00001227TEST_F(FormatTest, UnderstandsNewAndDelete) {
1228 verifyFormat("A *a = new A;");
1229 verifyFormat("A *a = new (placement) A;");
1230 verifyFormat("delete a;");
1231 verifyFormat("delete (A *)a;");
1232}
1233
Daniel Jasper5d334402013-01-02 08:57:10 +00001234TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001235 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001236 verifyFormat("f(a, *a);");
1237 verifyFormat("f(*a);");
1238 verifyFormat("int a = b * 10;");
1239 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001240 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001241 verifyFormat("int a += b * c;");
1242 verifyFormat("int a -= b * c;");
1243 verifyFormat("int a *= b * c;");
1244 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001245 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001246 verifyFormat("int a = *b * c;");
1247 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001248 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001249 verifyFormat("return 10 * b;");
1250 verifyFormat("return *b * *c;");
1251 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001252 verifyFormat("f(b ? *c : *d);");
1253 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001254 verifyFormat("*b = a;");
1255 verifyFormat("a * ~b;");
1256 verifyFormat("a * !b;");
1257 verifyFormat("a * +b;");
1258 verifyFormat("a * -b;");
1259 verifyFormat("a * ++b;");
1260 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001261 verifyFormat("a[4] * b;");
1262 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001263 verifyFormat("a * [self dostuff];");
1264 verifyFormat("a * (a + b);");
1265 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001266 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001267
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001268 verifyFormat("InvalidRegions[*R] = 0;");
1269
Daniel Jasper8b39c662012-12-10 18:59:13 +00001270 verifyFormat("A<int *> a;");
1271 verifyFormat("A<int **> a;");
1272 verifyFormat("A<int *, int *> a;");
1273 verifyFormat("A<int **, int **> a;");
1274
Daniel Jasper2db356d2013-01-08 20:03:18 +00001275 verifyFormat(
1276 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1277 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1278
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001279 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001280 verifyGoogleFormat("A<int*> a;");
1281 verifyGoogleFormat("A<int**> a;");
1282 verifyGoogleFormat("A<int*, int*> a;");
1283 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001284 verifyGoogleFormat("f(b ? *c : *d);");
1285 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001286 verifyGoogleFormat("Type* t = **x;");
1287 verifyGoogleFormat("Type* t = *++*x;");
1288 verifyGoogleFormat("*++*x;");
1289 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1290 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001291
1292 verifyFormat("a = *(x + y);");
1293 verifyFormat("a = &(x + y);");
1294 verifyFormat("*(x + y).call();");
1295 verifyFormat("&(x + y)->call();");
1296 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001297
1298 verifyFormat("f(b * /* confusing comment */ ++c);");
1299 verifyFormat(
1300 "int *MyValues = {\n"
1301 " *A, // Operator detection might be confused by the '{'\n"
1302 " *BB // Operator detection might be confused by previous comment\n"
1303 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001304
1305 verifyFormat("if (int *a = &b)");
1306 verifyFormat("if (int &a = *b)");
1307 verifyFormat("if (a & b[i])");
1308 verifyFormat("if (a::b::c::d & b[i])");
1309 verifyFormat("if (*b[i])");
1310 verifyFormat("if (int *a = (&b))");
1311 verifyFormat("while (int *a = &b)");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001312}
1313
Daniel Jasper4981bd02013-01-13 08:01:36 +00001314TEST_F(FormatTest, FormatsCasts) {
1315 verifyFormat("Type *A = static_cast<Type *>(P);");
1316 verifyFormat("Type *A = (Type *)P;");
1317 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1318 verifyFormat("int a = (int)(2.0f);");
1319
1320 // FIXME: These also need to be identified.
1321 verifyFormat("int a = (int) 2.0f;");
1322 verifyFormat("int a = (int) * b;");
1323
1324 // These are not casts.
1325 verifyFormat("void f(int *) {}");
1326 verifyFormat("void f(int *);");
1327 verifyFormat("void f(int *) = 0;");
1328 verifyFormat("void f(SmallVector<int>) {}");
1329 verifyFormat("void f(SmallVector<int>);");
1330 verifyFormat("void f(SmallVector<int>) = 0;");
1331}
1332
Daniel Jasper46ef8522013-01-10 13:08:12 +00001333TEST_F(FormatTest, FormatsFunctionTypes) {
1334 // FIXME: Determine the cases that need a space after the return type and fix.
1335 verifyFormat("A<bool()> a;");
1336 verifyFormat("A<SomeType()> a;");
1337 verifyFormat("A<void(*)(int, std::string)> a;");
1338
1339 verifyFormat("int(*func)(void *);");
1340}
1341
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001342TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001343 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001344 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001345 verifyFormat(
1346 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1347 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001348 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001349}
1350
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001351TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1352 verifyFormat("(a)->b();");
1353 verifyFormat("--a;");
1354}
1355
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001356TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001357 verifyFormat("#include <string>\n"
1358 "#include <a/b/c.h>\n"
1359 "#include \"a/b/string\"\n"
1360 "#include \"string.h\"\n"
1361 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001362 "#include <a-a>\n"
1363 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001364
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001365 verifyFormat("#import <string>");
1366 verifyFormat("#import <a/b/c.h>");
1367 verifyFormat("#import \"a/b/string\"");
1368 verifyFormat("#import \"string.h\"");
1369 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001370}
1371
Alexander Kornienko15757312012-12-06 18:03:27 +00001372//===----------------------------------------------------------------------===//
1373// Error recovery tests.
1374//===----------------------------------------------------------------------===//
1375
Daniel Jasper700e7102013-01-10 09:26:47 +00001376TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001377 verifyFormat("void f() { return; }\n42");
1378 verifyFormat("void f() {\n"
1379 " if (0)\n"
1380 " return;\n"
1381 "}\n"
1382 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001383 verifyFormat("void f() { return }\n42");
1384 verifyFormat("void f() {\n"
1385 " if (0)\n"
1386 " return\n"
1387 "}\n"
1388 "42");
1389}
1390
1391TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1392 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1393 EXPECT_EQ("void f() {\n"
1394 " if (a)\n"
1395 " return\n"
1396 "}", format("void f ( ) { if ( a ) return }"));
1397 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1398 EXPECT_EQ("namespace N {\n"
1399 "void f() {}\n"
1400 "void g()\n"
1401 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001402}
1403
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001404TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1405 verifyFormat("int aaaaaaaa =\n"
1406 " // Overly long comment\n"
1407 " b;", getLLVMStyleWithColumns(20));
1408 verifyFormat("function(\n"
1409 " ShortArgument,\n"
1410 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1411}
1412
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001413TEST_F(FormatTest, IncorrectAccessSpecifier) {
1414 verifyFormat("public:");
1415 verifyFormat("class A {\n"
1416 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001417 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001418 "};");
1419 verifyFormat("public\n"
1420 "int qwerty;");
1421 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001422 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001423 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001424 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001425 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001426 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001427}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001428
Alexander Kornienko393b0082012-12-04 15:40:36 +00001429TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1430 verifyFormat("{");
1431}
1432
1433TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001434 verifyFormat("do {}");
1435 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001436 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001437 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001438 "wheeee(fun);");
1439 verifyFormat("do {\n"
1440 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001441 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001442}
1443
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001444TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001445 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001446 verifyFormat("switch {\n foo;\n foo();\n}");
1447 verifyFormat("for {\n foo;\n foo();\n}");
1448 verifyFormat("while {\n foo;\n foo();\n}");
1449 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001450}
1451
Daniel Jasper1f42f112013-01-04 18:52:56 +00001452TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1453 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001454 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001455}
1456
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001457TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001458 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1459 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1460 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1461 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001462
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001463 EXPECT_EQ("{\n"
1464 " {\n"
1465 " breakme(\n"
1466 " qwe);\n"
1467 "}\n", format("{\n"
1468 " {\n"
1469 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001470 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001471}
1472
Manuel Klimek2851c162013-01-10 14:36:46 +00001473TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1474 verifyFormat(
1475 "int x = {\n"
1476 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001477 " b(alongervariable)\n"
1478 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001479}
1480
Manuel Klimekc44ee892013-01-21 10:07:49 +00001481TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
1482 verifyFormat("return (a)(b) { 1, 2, 3 };");
1483}
1484
Manuel Klimek2851c162013-01-10 14:36:46 +00001485TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1486 verifyFormat(
1487 "Aaa({\n"
1488 " int i;\n"
1489 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1490 " ccccccccccccccccc));");
1491}
1492
Manuel Klimek517e8942013-01-11 17:54:10 +00001493TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1494 verifyFormat("void f() { return 42; }");
1495 verifyFormat("void f() {\n"
1496 " // Comment\n"
1497 "}");
1498 verifyFormat("{\n"
1499 "#error {\n"
1500 " int a;\n"
1501 "}");
1502 verifyFormat("{\n"
1503 " int a;\n"
1504 "#error {\n"
1505 "}");
1506}
1507
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001508TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1509 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001510 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001511 verifyFormat("class foo a = { bar };\nint n;");
1512 verifyFormat("union foo a = { bar };\nint n;");
1513
1514 // Elaborate types inside function definitions.
1515 verifyFormat("struct foo f() {}\nint n;");
1516 verifyFormat("class foo f() {}\nint n;");
1517 verifyFormat("union foo f() {}\nint n;");
1518
1519 // Templates.
1520 verifyFormat("template <class X> void f() {}\nint n;");
1521 verifyFormat("template <struct X> void f() {}\nint n;");
1522 verifyFormat("template <union X> void f() {}\nint n;");
1523
1524 // Actual definitions...
1525 verifyFormat("struct {} n;");
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001526 verifyFormat(
1527 "template <template <class T, class Y>, class Z > class X {} n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001528 verifyFormat("union Z {\n int n;\n} x;");
1529 verifyFormat("class MACRO Z {} n;");
1530 verifyFormat("class MACRO(X) Z {} n;");
1531 verifyFormat("class __attribute__(X) Z {} n;");
1532 verifyFormat("class __declspec(X) Z {} n;");
1533
Manuel Klimek7f5b0252013-01-21 10:17:14 +00001534 // Redefinition from nested context:
1535 verifyFormat("class A::B::C {} n;");
1536
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001537 // Template definitions.
1538 // FIXME: This is still incorrectly handled at the formatter side.
1539 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {};");
1540
1541 // FIXME:
1542 // This now gets parsed incorrectly as class definition.
1543 // verifyFormat("class A<int> f() {}\nint n;");
1544
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001545 // Elaborate types where incorrectly parsing the structural element would
1546 // break the indent.
1547 verifyFormat("if (true)\n"
1548 " class X x;\n"
1549 "else\n"
1550 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001551}
1552
Manuel Klimek407a31a2013-01-15 15:50:27 +00001553TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1554 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1555 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1556 EXPECT_EQ("#error 1", format(" # error 1"));
1557 EXPECT_EQ("#warning 1", format(" # warning 1"));
1558}
1559
Manuel Klimek525fe162013-01-18 14:04:34 +00001560TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
1561 FormatStyle AllowsMergedIf = getGoogleStyle();
1562 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
1563 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
1564 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
Manuel Klimek4c128122013-01-18 14:46:43 +00001565 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
1566 EXPECT_EQ("if (true) return 42;",
1567 format("if (true)\nreturn 42;", AllowsMergedIf));
1568 FormatStyle ShortMergedIf = AllowsMergedIf;
1569 ShortMergedIf.ColumnLimit = 25;
1570 verifyFormat("#define A \\\n"
1571 " if (true) return 42;", ShortMergedIf);
1572 verifyFormat("#define A \\\n"
1573 " f(); \\\n"
1574 " if (true)\n"
1575 "#define B", ShortMergedIf);
1576 verifyFormat("#define A \\\n"
1577 " f(); \\\n"
1578 " if (true)\n"
1579 "g();", ShortMergedIf);
Manuel Klimek0fbe0082013-01-21 14:16:56 +00001580 verifyFormat("{\n"
1581 "#ifdef A\n"
1582 " // Comment\n"
1583 " if (true) continue;\n"
1584 "#endif\n"
1585 " // Comment\n"
1586 " if (true) continue;", ShortMergedIf);
Manuel Klimek525fe162013-01-18 14:04:34 +00001587}
1588
Nico Webercf4a79c2013-01-08 17:56:31 +00001589//===----------------------------------------------------------------------===//
1590// Objective-C tests.
1591//===----------------------------------------------------------------------===//
1592
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001593TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1594 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1595 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1596 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001597 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001598 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1599 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1600 format("-(NSInteger)Method3:(id)anObject;"));
1601 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1602 format("-(NSInteger)Method4:(id)anObject;"));
1603 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1604 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1605 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1606 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001607 EXPECT_EQ(
1608 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1609 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001610
1611 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001612 EXPECT_EQ(
1613 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1614 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1615 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1616 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1617 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1618 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1619 format(
1620 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1621 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1622 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1623 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1624 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1625 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001626
1627 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001628 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001629 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1630 // protocol lists (but not for template classes):
1631 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001632
1633 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001634 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001635
1636 // If there's no return type (very rare in practice!), LLVM and Google style
1637 // agree.
1638 verifyFormat("- foo:(int)f;");
1639 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001640}
1641
Daniel Jasper886568d2013-01-09 08:36:49 +00001642TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001643 verifyFormat("int (^Block)(int, int);");
1644 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001645}
1646
Nico Weber27d13672013-01-09 20:25:35 +00001647TEST_F(FormatTest, FormatObjCInterface) {
1648 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001649 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001650 "@public\n"
1651 " int field1;\n"
1652 "@protected\n"
1653 " int field2;\n"
1654 "@private\n"
1655 " int field3;\n"
1656 "@package\n"
1657 " int field4;\n"
1658 "}\n"
1659 "+ (id)init;\n"
1660 "@end");
1661
Nico Weber27d13672013-01-09 20:25:35 +00001662 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1663 " @public\n"
1664 " int field1;\n"
1665 " @protected\n"
1666 " int field2;\n"
1667 " @private\n"
1668 " int field3;\n"
1669 " @package\n"
1670 " int field4;\n"
1671 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001672 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001673 "@end");
1674
1675 verifyFormat("@interface Foo\n"
1676 "+ (id)init;\n"
1677 "// Look, a comment!\n"
1678 "- (int)answerWith:(int)i;\n"
1679 "@end");
1680
1681 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001682 "@end\n"
1683 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001684 "@end");
1685
1686 verifyFormat("@interface Foo : Bar\n"
1687 "+ (id)init;\n"
1688 "@end");
1689
Nico Weber5f500df2013-01-10 20:12:55 +00001690 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001691 "+ (id)init;\n"
1692 "@end");
1693
Nico Weber5f500df2013-01-10 20:12:55 +00001694 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001695 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001696 "@end");
1697
Nico Webered91bba2013-01-10 19:19:14 +00001698 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001699 "+ (id)init;\n"
1700 "@end");
1701
Nico Webered91bba2013-01-10 19:19:14 +00001702 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001703 "+ (id)init;\n"
1704 "@end");
1705
Nico Weber5f500df2013-01-10 20:12:55 +00001706 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001707 "+ (id)init;\n"
1708 "@end");
1709
Nico Weber5f500df2013-01-10 20:12:55 +00001710 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001711 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001712 "@end");
1713
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001714 verifyFormat("@interface Foo {\n"
1715 " int _i;\n"
1716 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001717 "+ (id)init;\n"
1718 "@end");
1719
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001720 verifyFormat("@interface Foo : Bar {\n"
1721 " int _i;\n"
1722 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001723 "+ (id)init;\n"
1724 "@end");
1725
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001726 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1727 " int _i;\n"
1728 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001729 "+ (id)init;\n"
1730 "@end");
1731
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001732 verifyFormat("@interface Foo (HackStuff) {\n"
1733 " int _i;\n"
1734 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001735 "+ (id)init;\n"
1736 "@end");
1737
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001738 verifyFormat("@interface Foo () {\n"
1739 " int _i;\n"
1740 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001741 "+ (id)init;\n"
1742 "@end");
1743
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001744 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1745 " int _i;\n"
1746 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001747 "+ (id)init;\n"
1748 "@end");
1749}
1750
Nico Weber50767d82013-01-09 23:25:37 +00001751TEST_F(FormatTest, FormatObjCImplementation) {
1752 verifyFormat("@implementation Foo : NSObject {\n"
1753 "@public\n"
1754 " int field1;\n"
1755 "@protected\n"
1756 " int field2;\n"
1757 "@private\n"
1758 " int field3;\n"
1759 "@package\n"
1760 " int field4;\n"
1761 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001762 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001763 "@end");
1764
1765 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1766 " @public\n"
1767 " int field1;\n"
1768 " @protected\n"
1769 " int field2;\n"
1770 " @private\n"
1771 " int field3;\n"
1772 " @package\n"
1773 " int field4;\n"
1774 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001775 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001776 "@end");
1777
1778 verifyFormat("@implementation Foo\n"
1779 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001780 " if (true)\n"
1781 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001782 "}\n"
1783 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001784 "- (int)answerWith:(int)i {\n"
1785 " return i;\n"
1786 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001787 "+ (int)answerWith:(int)i {\n"
1788 " return i;\n"
1789 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001790 "@end");
1791
1792 verifyFormat("@implementation Foo\n"
1793 "@end\n"
1794 "@implementation Bar\n"
1795 "@end");
1796
1797 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001798 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001799 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001800 "@end");
1801
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001802 verifyFormat("@implementation Foo {\n"
1803 " int _i;\n"
1804 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001805 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001806 "@end");
1807
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001808 verifyFormat("@implementation Foo : Bar {\n"
1809 " int _i;\n"
1810 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001811 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001812 "@end");
1813
Nico Webered91bba2013-01-10 19:19:14 +00001814 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001815 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001816 "@end");
1817}
1818
Nico Weber1abe6ea2013-01-09 21:15:03 +00001819TEST_F(FormatTest, FormatObjCProtocol) {
1820 verifyFormat("@protocol Foo\n"
1821 "@property(weak) id delegate;\n"
1822 "- (NSUInteger)numberOfThings;\n"
1823 "@end");
1824
Nico Weber5f500df2013-01-10 20:12:55 +00001825 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001826 "- (NSUInteger)numberOfThings;\n"
1827 "@end");
1828
Nico Weber5f500df2013-01-10 20:12:55 +00001829 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001830 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001831 "@end");
1832
Nico Weber1abe6ea2013-01-09 21:15:03 +00001833 verifyFormat("@protocol Foo;\n"
1834 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001835
1836 verifyFormat("@protocol Foo\n"
1837 "@end\n"
1838 "@protocol Bar\n"
1839 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001840
1841 verifyFormat("@protocol myProtocol\n"
1842 "- (void)mandatoryWithInt:(int)i;\n"
1843 "@optional\n"
1844 "- (void)optional;\n"
1845 "@required\n"
1846 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001847 "@optional\n"
1848 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001849 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001850}
1851
Nico Weberbcfdd262013-01-12 06:18:40 +00001852TEST_F(FormatTest, FormatObjCMethodExpr) {
1853 verifyFormat("[foo bar:baz];");
1854 verifyFormat("return [foo bar:baz];");
1855 verifyFormat("f([foo bar:baz]);");
1856 verifyFormat("f(2, [foo bar:baz]);");
1857 verifyFormat("f(2, a ? b : c);");
1858 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1859
1860 verifyFormat("[foo bar:baz], [foo bar:baz];");
1861 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1862 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1863 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1864 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1865 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1866 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1867 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1868 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1869 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1870 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1871 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1872 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1873 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1874 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1875 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1876 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1877 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1878 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1879 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1880 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1881 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1882 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1883 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1884 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1885 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1886 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1887 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1888 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1889 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1890 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1891 // Whew!
1892
1893 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1894 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1895 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1896 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1897 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001898 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001899 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001900
Nico Weberbcfdd262013-01-12 06:18:40 +00001901 verifyFormat("arr[[self indexForFoo:a]];");
1902 verifyFormat("throw [self errorFor:a];");
1903 verifyFormat("@throw [self errorFor:a];");
1904
Nico Webere8ccc812013-01-12 22:48:47 +00001905 // This tests that the formatter doesn't break after "backing" but before ":",
1906 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001907 verifyFormat(
1908 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001909 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1910 " backing:NSBackingStoreBuffered defer:YES]))");
1911
Nico Webere8ccc812013-01-12 22:48:47 +00001912 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1913 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001914
1915}
1916
Nico Weber581f5572013-01-07 15:56:25 +00001917TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001918 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001919 verifyFormat("@catch");
1920 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001921 verifyFormat("@compatibility_alias");
1922 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001923 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001924 verifyFormat("@encode");
1925 verifyFormat("@end");
1926 verifyFormat("@finally");
1927 verifyFormat("@implementation");
1928 verifyFormat("@import");
1929 verifyFormat("@interface");
1930 verifyFormat("@optional");
1931 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001932 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001933 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001934 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001935 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001936 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001937 verifyFormat("@required");
1938 verifyFormat("@selector");
1939 verifyFormat("@synchronized");
1940 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001941 verifyFormat("@throw");
1942 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001943
Nico Webercb4d6902013-01-08 19:40:21 +00001944 verifyFormat("@\"String\"");
1945 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001946 verifyFormat("@+4.8");
1947 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001948 verifyFormat("@1LL");
1949 verifyFormat("@.5");
1950 verifyFormat("@'c'");
1951 verifyFormat("@true");
1952 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001953 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001954 verifyFormat("@[");
1955 verifyFormat("@{");
1956
Nico Weber581f5572013-01-07 15:56:25 +00001957 EXPECT_EQ("@interface", format("@ interface"));
1958
1959 // The precise formatting of this doesn't matter, nobody writes code like
1960 // this.
1961 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001962}
1963
Nico Weberc31689a2013-01-08 19:15:23 +00001964TEST_F(FormatTest, ObjCSnippets) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001965 verifyFormat("@autoreleasepool {\n"
1966 " foo();\n"
1967 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001968 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001969 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001970 verifyFormat("@dynamic textColor;");
Nico Weber5dfe9b42013-01-18 05:11:47 +00001971 verifyFormat("char *buf1 = @encode(int *);");
1972 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1973 // FIXME: Enable once PR14884 is fixed:
Nico Weberc31689a2013-01-08 19:15:23 +00001974 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001975 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weber6a21a552013-01-18 02:43:57 +00001976 verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001977 verifyFormat("@synchronized(self) {\n"
1978 " f();\n"
1979 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001980
Nico Weber70848232013-01-10 21:30:42 +00001981 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1982 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1983
Nico Webercf4a79c2013-01-08 17:56:31 +00001984 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001985 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1986 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001987}
1988
Daniel Jaspercd162382013-01-07 13:26:07 +00001989} // end namespace tooling
1990} // end namespace clang