blob: 6b77868776a73d092f7c04c3ae7d753b4d3815e6 [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 "}");
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
Nico Webera9ccdd12013-01-07 16:36:17 +0000529TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000530 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
531 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000532 verifyFormat("try {\n"
533 " throw a * b;\n"
534 "}\n"
535 "catch (int a) {\n"
536 " // Do nothing.\n"
537 "}\n"
538 "catch (...) {\n"
539 " exit(42);\n"
540 "}");
541
542 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000543 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000544 "catch (...) {\n"
545 " return 5;\n"
546 "}");
547 verifyFormat("class A {\n"
548 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000549 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000550 " catch (...) {\n"
551 " throw;\n"
552 " }\n"
553 "};\n");
554}
555
556TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000557 verifyFormat("@try {\n"
558 " f();\n"
559 "}\n"
560 "@catch (NSException e) {\n"
561 " @throw;\n"
562 "}\n"
563 "@finally {\n"
564 " exit(42);\n"
565 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000566}
567
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000568TEST_F(FormatTest, StaticInitializers) {
569 verifyFormat("static SomeClass SC = { 1, 'a' };");
570
571 // FIXME: Format like enums if the static initializer does not fit on a line.
572 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000573 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000574 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
575 "};");
576
577 verifyFormat(
578 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
579 " looooooooooooooooooooooooooooooooooongname,\n"
580 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000581}
582
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000583TEST_F(FormatTest, NestedStaticInitializers) {
584 verifyFormat("static A x = { { {} } };\n");
585 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000586 "static A x = { { { init1, init2, init3, init4 },\n"
587 " { init1, init2, init3, init4 } } };");
588
Nico Weber6a21a552013-01-18 02:43:57 +0000589 // FIXME: Fix this in general and verify that it works in LLVM style again.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000590 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000591 "somes Status::global_reps[3] = {\n"
592 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
593 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
594 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
595 "};");
596 verifyFormat(
597 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
598 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
599 " } };");
600
Nico Weber6a21a552013-01-18 02:43:57 +0000601 // FIXME: We might at some point want to handle this similar to parameter
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000602 // lists, where we have an option to put each on a single line.
603 verifyFormat("struct {\n"
604 " unsigned bit;\n"
605 " const char *const name;\n"
606 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
607 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
608}
609
Manuel Klimeka080a182013-01-02 16:30:12 +0000610TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
611 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
612 " \\\n"
613 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
614}
615
Daniel Jasper71607512013-01-07 10:48:50 +0000616TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000617 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
618 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000619}
620
Manuel Klimeka080a182013-01-02 16:30:12 +0000621TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
622 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000623 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000624}
625
626TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
627 EXPECT_EQ("#line 42 \"test\"\n",
628 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000629 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000630 format("# \\\n define \\\n A \\\n B\n",
631 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000632}
633
634TEST_F(FormatTest, EndOfFileEndsPPDirective) {
635 EXPECT_EQ("#line 42 \"test\"",
636 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000637 EXPECT_EQ("#define A B",
638 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000639}
640
Manuel Klimek060143e2013-01-02 18:33:23 +0000641TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000642 // If the macro fits in one line, we still do not get the full
643 // line, as only the next line decides whether we need an escaped newline and
644 // thus use the last column.
645 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000646
Manuel Klimekd544c572013-01-07 09:24:17 +0000647 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
648 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000649 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000650
651 verifyFormat("#define A A\n#define A A");
652 verifyFormat("#define A(X) A\n#define A A");
653
654 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
655 verifyFormat("#define Something \\\n"
656 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000657}
658
659TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000660 EXPECT_EQ("// some comment\n"
661 "#include \"a.h\"\n"
662 "#define A(A,\\\n"
663 " B)\n"
664 "#include \"b.h\"\n"
665 "// some comment\n",
666 format(" // some comment\n"
667 " #include \"a.h\"\n"
668 "#define A(A,\\\n"
669 " B)\n"
670 " #include \"b.h\"\n"
671 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000672}
673
Manuel Klimekd4397b92013-01-04 23:34:14 +0000674TEST_F(FormatTest, LayoutSingleHash) {
675 EXPECT_EQ("#\na;", format("#\na;"));
676}
677
678TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
679 EXPECT_EQ("#define A \\\n"
680 " c; \\\n"
681 " e;\n"
682 "f;", format("#define A c; e;\n"
683 "f;", getLLVMStyleWithColumns(14)));
684}
685
686TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000687 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000688}
689
690TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000691 EXPECT_EQ("# define A\\\n b;",
692 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000693}
694
695TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000696 EXPECT_EQ("int x,\n"
697 "#define A\n"
698 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000699}
700
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000701TEST_F(FormatTest, HashInMacroDefinition) {
702 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
703 verifyFormat("#define A \\\n"
704 " { \\\n"
705 " f(#c);\\\n"
706 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000707
708 verifyFormat("#define A(X) \\\n"
709 " void function##X()", getLLVMStyleWithColumns(22));
710
711 verifyFormat("#define A(a, b, c) \\\n"
712 " void a##b##c()", getLLVMStyleWithColumns(22));
713
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000714 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000715}
716
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000717TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
718 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
719}
720
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000721TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000722 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000723}
724
Manuel Klimeka5342db2013-01-06 20:07:31 +0000725TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
726 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
727 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
728 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
729 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
730}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000731
Manuel Klimek95419382013-01-07 07:56:50 +0000732TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000733 EXPECT_EQ(
734 "#define A \\\n int i; \\\n int j;",
735 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000736}
737
Manuel Klimekd544c572013-01-07 09:24:17 +0000738TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
739 verifyFormat("#define A \\\n"
740 " int v( \\\n"
741 " a); \\\n"
742 " int i;", getLLVMStyleWithColumns(11));
743}
744
Manuel Klimeka080a182013-01-02 16:30:12 +0000745TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000746 EXPECT_EQ(
747 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
748 " \\\n"
749 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
750 "\n"
751 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
752 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
753 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
754 "\\\n"
755 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
756 " \n"
757 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
758 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000759}
760
Manuel Klimek526ed112013-01-09 15:25:02 +0000761TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
762 EXPECT_EQ("int\n"
763 "#define A\n"
764 " a;",
765 format("int\n#define A\na;"));
766 verifyFormat(
767 "functionCallTo(someOtherFunction(\n"
768 " withSomeParameters, whichInSequence,\n"
769 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000770 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000771 " withMoreParamters,\n"
772 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000773 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000774}
775
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000776TEST_F(FormatTest, LayoutBlockInsideParens) {
777 EXPECT_EQ("functionCall({\n"
778 " int i;\n"
779 "});", format(" functionCall ( {int i;} );"));
780}
781
782TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000783 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000784 "int i;", format(" SOME_MACRO {int i;} int i;"));
785}
786
787TEST_F(FormatTest, LayoutNestedBlocks) {
788 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
789 " struct s {\n"
790 " int i;\n"
791 " };\n"
792 " s kBitsToOs[] = { { 10 } };\n"
793 " for (int i = 0; i < 10; ++i)\n"
794 " return;\n"
795 "}");
796}
797
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000798TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
799 EXPECT_EQ("{}", format("{}"));
800}
801
Alexander Kornienko15757312012-12-06 18:03:27 +0000802//===----------------------------------------------------------------------===//
803// Line break tests.
804//===----------------------------------------------------------------------===//
805
806TEST_F(FormatTest, FormatsFunctionDefinition) {
807 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
808 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000809 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000810}
811
812TEST_F(FormatTest, FormatsAwesomeMethodCall) {
813 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000814 "SomeLongMethodName(SomeReallyLongMethod(\n"
815 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
816 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000817}
818
Daniel Jasper1321eb52012-12-18 21:05:13 +0000819TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000820 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000821 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
822 getLLVMStyleWithColumns(45));
823 verifyFormat("Constructor()\n"
824 " : Inttializer(FitsOnTheLine) {}",
825 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000826
827 verifyFormat(
828 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000829 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000830
831 verifyFormat(
832 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000833 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
834 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
835 verifyGoogleFormat(
836 "SomeClass::Constructor()\n"
837 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
838 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
839 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper16618242013-01-16 17:00:50 +0000840 verifyGoogleFormat(
841 "SomeClass::Constructor()\n"
842 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
843 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
844 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000845
846 verifyFormat(
847 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000848 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000849 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000850
851 verifyFormat("Constructor()\n"
852 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
853 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
854 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000855 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000856
857 // Here a line could be saved by splitting the second initializer onto two
858 // lines, but that is not desireable.
859 verifyFormat("Constructor()\n"
860 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
861 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000862 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000863
864 verifyGoogleFormat("MyClass::MyClass(int var)\n"
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000865 " : some_var_(var), // 4 space indent\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000866 " some_other_var_(var + 1) { // lined up\n"
867 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000868
869 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000870 std::string input = "Constructor()\n"
871 " : aaaa(a,\n";
872 for (unsigned i = 0, e = 80; i != e; ++i) {
873 input += " a,\n";
874 }
875 input += " a) {}";
876 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000877}
878
Alexander Kornienko15757312012-12-06 18:03:27 +0000879TEST_F(FormatTest, BreaksAsHighAsPossible) {
880 verifyFormat(
881 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
882 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
883 " f();");
884}
885
Daniel Jasperbac016b2012-12-03 18:12:45 +0000886TEST_F(FormatTest, BreaksDesireably) {
887 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
888 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000889 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000890
891 verifyFormat(
892 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000894
895 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000898
899 verifyFormat(
900 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
901 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
902 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
903 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000904
905 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
906 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
907
Daniel Jasper723f0302013-01-02 14:40:02 +0000908 verifyFormat(
909 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
911
Daniel Jasper33182dd2012-12-05 14:57:28 +0000912 // This test case breaks on an incorrect memoization, i.e. an optimization not
913 // taking into account the StopAt value.
914 verifyFormat(
915 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000916 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
917 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
918 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000919
Daniel Jaspercd162382013-01-07 13:26:07 +0000920 verifyFormat("{\n {\n {\n"
921 " Annotation.SpaceRequiredBefore =\n"
922 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
923 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
924 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000925}
926
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000927TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
928 verifyGoogleFormat(
929 "aaaaaaaa(aaaaaaaaaaaaa,\n"
930 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
931 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
932 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
934 verifyGoogleFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +0000935 "aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa)\n"
936 " .aaaaaaaaaaaaaaaaaa();");
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000937 verifyGoogleFormat(
938 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
939 " ddddddddddddddddddddddddddddd),\n"
940 " test);");
941
942 verifyGoogleFormat(
943 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
944 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
945 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
946 verifyGoogleFormat("a(\"a\"\n"
947 " \"a\",\n"
948 " a);");
949}
950
Daniel Jasperc79afda2013-01-18 10:56:38 +0000951TEST_F(FormatTest, FormatsBuilderPattern) {
952 verifyFormat(
953 "return llvm::StringSwitch<Reference::Kind>(name)\n"
954 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
955 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
956 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
957 " .Default(ORDER_TEXT);\n");
958}
959
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000960TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
961 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
962 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +0000963 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
964 " GUARDED_BY(aaaaaaaaaaaaa);");
965 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
966 " GUARDED_BY(aaaaaaaaaaaaa) {}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000967}
968
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000969TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
970 verifyFormat(
971 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000972 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000973 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000974 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000975 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000976 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000977 verifyFormat(
978 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000979 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000980}
981
Daniel Jasper9cda8002013-01-07 13:08:40 +0000982TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
983 verifyFormat(
984 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
985 " SI->getAlignment(),\n"
986 " SI->getPointerAddressSpaceee());\n");
987 verifyFormat(
988 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
989 " Line.Tokens.front().Tok.getLocation(),\n"
990 " Line.Tokens.back().Tok.getLocation());");
991}
992
Daniel Jaspercf225b62012-12-24 13:43:52 +0000993TEST_F(FormatTest, AlignsAfterAssignments) {
994 verifyFormat(
995 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000996 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000997 verifyFormat(
998 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000999 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001000 verifyFormat(
1001 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001002 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001003 verifyFormat(
1004 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +00001005 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001006 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +00001007 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1008 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1009 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +00001010}
1011
1012TEST_F(FormatTest, AlignsAfterReturn) {
1013 verifyFormat(
1014 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1015 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
1016 verifyFormat(
1017 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1018 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
1019}
1020
Daniel Jasper9c837d02013-01-09 07:06:56 +00001021TEST_F(FormatTest, BreaksConditionalExpressions) {
1022 verifyFormat(
1023 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1024 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1025 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1026 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
1027 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +00001028 verifyFormat(
1029 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
1030 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +00001031}
1032
Nico Weber7d37b8b2013-01-12 01:28:06 +00001033TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1034 verifyFormat("arr[foo ? bar : baz];");
1035 verifyFormat("f()[foo ? bar : baz];");
1036 verifyFormat("(a + b)[foo ? bar : baz];");
1037 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1038}
1039
Daniel Jasperbac016b2012-12-03 18:12:45 +00001040TEST_F(FormatTest, AlignsStringLiterals) {
1041 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1042 " \"short literal\");");
1043 verifyFormat(
1044 "looooooooooooooooooooooooongFunction(\n"
1045 " \"short literal\"\n"
1046 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1047}
1048
Alexander Kornienko15757312012-12-06 18:03:27 +00001049TEST_F(FormatTest, AlignsPipes) {
1050 verifyFormat(
1051 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1052 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1053 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1054 verifyFormat(
1055 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1056 " << aaaaaaaaaaaaaaaaaaaa;");
1057 verifyFormat(
1058 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1059 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1060 verifyFormat(
1061 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1062 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1063 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1064 verifyFormat(
1065 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1066 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1067 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1068}
1069
Daniel Jasperbac016b2012-12-03 18:12:45 +00001070TEST_F(FormatTest, UnderstandsEquals) {
1071 verifyFormat(
1072 "aaaaaaaaaaaaaaaaa =\n"
1073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1074 verifyFormat(
1075 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001077 verifyFormat(
1078 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001079 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001080 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001082
Daniel Jasper9cda8002013-01-07 13:08:40 +00001083 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001084 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001085 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001086 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001087}
1088
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001089TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001090 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1091 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001092
Daniel Jasper1321eb52012-12-18 21:05:13 +00001093 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1094 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001095
1096 verifyFormat(
1097 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1098 " Parameter2);");
1099
1100 verifyFormat(
1101 "ShortObject->shortFunction(\n"
1102 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1103 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1104
1105 verifyFormat("loooooooooooooongFunction(\n"
1106 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1107
1108 verifyFormat(
1109 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1110 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1111
Daniel Jasper46a46a22013-01-07 07:13:20 +00001112 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001113 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001114 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001115 verifyFormat(
1116 "aaaaaaaaaaa->aaaaaaaaa(\n"
1117 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1118 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001119}
1120
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001121TEST_F(FormatTest, WrapsTemplateDeclarations) {
1122 verifyFormat("template <typename T>\n"
1123 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1124 verifyFormat(
Daniel Jasperc79afda2013-01-18 10:56:38 +00001125 "template <typename T>\n"
1126 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1127 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001128 verifyFormat(
1129 "template <typename T>\n"
1130 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1131 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001132 verifyFormat(
1133 "template <typename T>\n"
1134 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1135 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001137 verifyFormat("template <typename T>\n"
1138 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1139 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001140 verifyFormat(
1141 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1142 " typename T4 = char>\n"
1143 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001144}
1145
Daniel Jasperbac016b2012-12-03 18:12:45 +00001146TEST_F(FormatTest, UnderstandsTemplateParameters) {
1147 verifyFormat("A<int> a;");
1148 verifyFormat("A<A<A<int> > > a;");
1149 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1150 verifyFormat("bool x = a < 1 || 2 > a;");
1151 verifyFormat("bool x = 5 < f<int>();");
1152 verifyFormat("bool x = f<int>() > 5;");
1153 verifyFormat("bool x = 5 < a<int>::x;");
1154 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1155 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1156
1157 verifyGoogleFormat("A<A<int>> a;");
1158 verifyGoogleFormat("A<A<A<int>>> a;");
1159 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1160
1161 verifyFormat("test >> a >> b;");
1162 verifyFormat("test << a >> b;");
1163
1164 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001165 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001166}
1167
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001168TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001169 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001170 verifyFormat("f(-1, -2, -3);");
1171 verifyFormat("a[-1] = 5;");
1172 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001173 verifyFormat("if (i == -1) {}");
1174 verifyFormat("if (i != -1) {}");
1175 verifyFormat("if (i > -1) {}");
1176 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001177 verifyFormat("++(a->f());");
1178 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001179 verifyFormat("(a->f())++;");
1180 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001181 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001182
1183 verifyFormat("a-- > b;");
1184 verifyFormat("b ? -a : c;");
1185 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001186 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001187 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001188 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001189
1190 verifyFormat("return -1;");
1191 verifyFormat("switch (a) {\n"
1192 "case -1:\n"
1193 " break;\n"
1194 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001195
1196 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1197 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001198
1199 verifyFormat("int a = /* confusing comment */ -1;");
1200 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1201 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001202}
1203
1204TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001205 verifyFormat("bool operator<();");
1206 verifyFormat("bool operator>();");
1207 verifyFormat("bool operator=();");
1208 verifyFormat("bool operator==();");
1209 verifyFormat("bool operator!=();");
1210 verifyFormat("int operator+();");
1211 verifyFormat("int operator++();");
1212 verifyFormat("bool operator();");
1213 verifyFormat("bool operator()();");
1214 verifyFormat("bool operator[]();");
1215 verifyFormat("operator bool();");
1216 verifyFormat("operator SomeType<int>();");
1217 verifyFormat("void *operator new(std::size_t size);");
1218 verifyFormat("void *operator new[](std::size_t size);");
1219 verifyFormat("void operator delete(void *ptr);");
1220 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001221}
1222
Daniel Jasper088dab52013-01-11 16:09:04 +00001223TEST_F(FormatTest, UnderstandsNewAndDelete) {
1224 verifyFormat("A *a = new A;");
1225 verifyFormat("A *a = new (placement) A;");
1226 verifyFormat("delete a;");
1227 verifyFormat("delete (A *)a;");
1228}
1229
Daniel Jasper5d334402013-01-02 08:57:10 +00001230TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001231 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001232 verifyFormat("f(a, *a);");
1233 verifyFormat("f(*a);");
1234 verifyFormat("int a = b * 10;");
1235 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001236 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001237 verifyFormat("int a += b * c;");
1238 verifyFormat("int a -= b * c;");
1239 verifyFormat("int a *= b * c;");
1240 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001241 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001242 verifyFormat("int a = *b * c;");
1243 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001244 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001245 verifyFormat("return 10 * b;");
1246 verifyFormat("return *b * *c;");
1247 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001248 verifyFormat("f(b ? *c : *d);");
1249 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001250 verifyFormat("*b = a;");
1251 verifyFormat("a * ~b;");
1252 verifyFormat("a * !b;");
1253 verifyFormat("a * +b;");
1254 verifyFormat("a * -b;");
1255 verifyFormat("a * ++b;");
1256 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001257 verifyFormat("a[4] * b;");
1258 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001259 verifyFormat("a * [self dostuff];");
1260 verifyFormat("a * (a + b);");
1261 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001262 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001263
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001264 verifyFormat("InvalidRegions[*R] = 0;");
1265
Daniel Jasper8b39c662012-12-10 18:59:13 +00001266 verifyFormat("A<int *> a;");
1267 verifyFormat("A<int **> a;");
1268 verifyFormat("A<int *, int *> a;");
1269 verifyFormat("A<int **, int **> a;");
1270
Daniel Jasper2db356d2013-01-08 20:03:18 +00001271 verifyFormat(
1272 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1274
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001275 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001276 verifyGoogleFormat("A<int*> a;");
1277 verifyGoogleFormat("A<int**> a;");
1278 verifyGoogleFormat("A<int*, int*> a;");
1279 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001280 verifyGoogleFormat("f(b ? *c : *d);");
1281 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001282 verifyGoogleFormat("Type* t = **x;");
1283 verifyGoogleFormat("Type* t = *++*x;");
1284 verifyGoogleFormat("*++*x;");
1285 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1286 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001287
1288 verifyFormat("a = *(x + y);");
1289 verifyFormat("a = &(x + y);");
1290 verifyFormat("*(x + y).call();");
1291 verifyFormat("&(x + y)->call();");
1292 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001293
1294 verifyFormat("f(b * /* confusing comment */ ++c);");
1295 verifyFormat(
1296 "int *MyValues = {\n"
1297 " *A, // Operator detection might be confused by the '{'\n"
1298 " *BB // Operator detection might be confused by previous comment\n"
1299 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001300
1301 verifyFormat("if (int *a = &b)");
1302 verifyFormat("if (int &a = *b)");
1303 verifyFormat("if (a & b[i])");
1304 verifyFormat("if (a::b::c::d & b[i])");
1305 verifyFormat("if (*b[i])");
1306 verifyFormat("if (int *a = (&b))");
1307 verifyFormat("while (int *a = &b)");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001308}
1309
Daniel Jasper4981bd02013-01-13 08:01:36 +00001310TEST_F(FormatTest, FormatsCasts) {
1311 verifyFormat("Type *A = static_cast<Type *>(P);");
1312 verifyFormat("Type *A = (Type *)P;");
1313 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1314 verifyFormat("int a = (int)(2.0f);");
1315
1316 // FIXME: These also need to be identified.
1317 verifyFormat("int a = (int) 2.0f;");
1318 verifyFormat("int a = (int) * b;");
1319
1320 // These are not casts.
1321 verifyFormat("void f(int *) {}");
1322 verifyFormat("void f(int *);");
1323 verifyFormat("void f(int *) = 0;");
1324 verifyFormat("void f(SmallVector<int>) {}");
1325 verifyFormat("void f(SmallVector<int>);");
1326 verifyFormat("void f(SmallVector<int>) = 0;");
1327}
1328
Daniel Jasper46ef8522013-01-10 13:08:12 +00001329TEST_F(FormatTest, FormatsFunctionTypes) {
1330 // FIXME: Determine the cases that need a space after the return type and fix.
1331 verifyFormat("A<bool()> a;");
1332 verifyFormat("A<SomeType()> a;");
1333 verifyFormat("A<void(*)(int, std::string)> a;");
1334
1335 verifyFormat("int(*func)(void *);");
1336}
1337
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001338TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001339 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001340 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001341 verifyFormat(
1342 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1343 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001344 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001345}
1346
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001347TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1348 verifyFormat("(a)->b();");
1349 verifyFormat("--a;");
1350}
1351
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001352TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001353 verifyFormat("#include <string>\n"
1354 "#include <a/b/c.h>\n"
1355 "#include \"a/b/string\"\n"
1356 "#include \"string.h\"\n"
1357 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001358 "#include <a-a>\n"
1359 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001360
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001361 verifyFormat("#import <string>");
1362 verifyFormat("#import <a/b/c.h>");
1363 verifyFormat("#import \"a/b/string\"");
1364 verifyFormat("#import \"string.h\"");
1365 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001366}
1367
Alexander Kornienko15757312012-12-06 18:03:27 +00001368//===----------------------------------------------------------------------===//
1369// Error recovery tests.
1370//===----------------------------------------------------------------------===//
1371
Daniel Jasper700e7102013-01-10 09:26:47 +00001372TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001373 verifyFormat("void f() { return; }\n42");
1374 verifyFormat("void f() {\n"
1375 " if (0)\n"
1376 " return;\n"
1377 "}\n"
1378 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001379 verifyFormat("void f() { return }\n42");
1380 verifyFormat("void f() {\n"
1381 " if (0)\n"
1382 " return\n"
1383 "}\n"
1384 "42");
1385}
1386
1387TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1388 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1389 EXPECT_EQ("void f() {\n"
1390 " if (a)\n"
1391 " return\n"
1392 "}", format("void f ( ) { if ( a ) return }"));
1393 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1394 EXPECT_EQ("namespace N {\n"
1395 "void f() {}\n"
1396 "void g()\n"
1397 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001398}
1399
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001400TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1401 verifyFormat("int aaaaaaaa =\n"
1402 " // Overly long comment\n"
1403 " b;", getLLVMStyleWithColumns(20));
1404 verifyFormat("function(\n"
1405 " ShortArgument,\n"
1406 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1407}
1408
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001409TEST_F(FormatTest, IncorrectAccessSpecifier) {
1410 verifyFormat("public:");
1411 verifyFormat("class A {\n"
1412 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001413 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001414 "};");
1415 verifyFormat("public\n"
1416 "int qwerty;");
1417 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001418 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001419 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001420 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001421 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001422 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001423}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001424
Alexander Kornienko393b0082012-12-04 15:40:36 +00001425TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1426 verifyFormat("{");
1427}
1428
1429TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001430 verifyFormat("do {}");
1431 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001432 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001433 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001434 "wheeee(fun);");
1435 verifyFormat("do {\n"
1436 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001437 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001438}
1439
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001440TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001441 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001442 verifyFormat("switch {\n foo;\n foo();\n}");
1443 verifyFormat("for {\n foo;\n foo();\n}");
1444 verifyFormat("while {\n foo;\n foo();\n}");
1445 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001446}
1447
Daniel Jasper1f42f112013-01-04 18:52:56 +00001448TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1449 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001450 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001451}
1452
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001453TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001454 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1455 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1456 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1457 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001458
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001459 EXPECT_EQ("{\n"
1460 " {\n"
1461 " breakme(\n"
1462 " qwe);\n"
1463 "}\n", format("{\n"
1464 " {\n"
1465 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001466 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001467}
1468
Manuel Klimek2851c162013-01-10 14:36:46 +00001469TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1470 verifyFormat(
1471 "int x = {\n"
1472 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001473 " b(alongervariable)\n"
1474 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001475}
1476
1477TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1478 verifyFormat(
1479 "Aaa({\n"
1480 " int i;\n"
1481 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1482 " ccccccccccccccccc));");
1483}
1484
Manuel Klimek517e8942013-01-11 17:54:10 +00001485TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1486 verifyFormat("void f() { return 42; }");
1487 verifyFormat("void f() {\n"
1488 " // Comment\n"
1489 "}");
1490 verifyFormat("{\n"
1491 "#error {\n"
1492 " int a;\n"
1493 "}");
1494 verifyFormat("{\n"
1495 " int a;\n"
1496 "#error {\n"
1497 "}");
1498}
1499
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001500TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1501 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001502 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001503 verifyFormat("class foo a = { bar };\nint n;");
1504 verifyFormat("union foo a = { bar };\nint n;");
1505
1506 // Elaborate types inside function definitions.
1507 verifyFormat("struct foo f() {}\nint n;");
1508 verifyFormat("class foo f() {}\nint n;");
1509 verifyFormat("union foo f() {}\nint n;");
1510
1511 // Templates.
1512 verifyFormat("template <class X> void f() {}\nint n;");
1513 verifyFormat("template <struct X> void f() {}\nint n;");
1514 verifyFormat("template <union X> void f() {}\nint n;");
1515
1516 // Actual definitions...
1517 verifyFormat("struct {} n;");
1518 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1519 verifyFormat("union Z {\n int n;\n} x;");
1520 verifyFormat("class MACRO Z {} n;");
1521 verifyFormat("class MACRO(X) Z {} n;");
1522 verifyFormat("class __attribute__(X) Z {} n;");
1523 verifyFormat("class __declspec(X) Z {} n;");
1524
1525 // Elaborate types where incorrectly parsing the structural element would
1526 // break the indent.
1527 verifyFormat("if (true)\n"
1528 " class X x;\n"
1529 "else\n"
1530 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001531}
1532
Manuel Klimek407a31a2013-01-15 15:50:27 +00001533TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1534 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1535 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1536 EXPECT_EQ("#error 1", format(" # error 1"));
1537 EXPECT_EQ("#warning 1", format(" # warning 1"));
1538}
1539
Manuel Klimek525fe162013-01-18 14:04:34 +00001540TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
1541 FormatStyle AllowsMergedIf = getGoogleStyle();
1542 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
1543 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
1544 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
1545
1546 // FIXME:
1547 // verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
1548}
1549
Manuel Klimek517e8942013-01-11 17:54:10 +00001550// FIXME: This breaks the order of the unwrapped lines:
1551// TEST_F(FormatTest, OrderUnwrappedLines) {
1552// verifyFormat("{\n"
1553// " bool a; //\n"
1554// "#error {\n"
1555// " int a;\n"
1556// "}");
1557// }
1558
Nico Webercf4a79c2013-01-08 17:56:31 +00001559//===----------------------------------------------------------------------===//
1560// Objective-C tests.
1561//===----------------------------------------------------------------------===//
1562
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001563TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1564 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1565 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1566 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001567 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001568 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1569 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1570 format("-(NSInteger)Method3:(id)anObject;"));
1571 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1572 format("-(NSInteger)Method4:(id)anObject;"));
1573 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1574 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1575 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1576 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001577 EXPECT_EQ(
1578 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1579 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001580
1581 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001582 EXPECT_EQ(
1583 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1584 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1585 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1586 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1587 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1588 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1589 format(
1590 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1591 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1592 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1593 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1594 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1595 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001596
1597 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001598 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001599 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1600 // protocol lists (but not for template classes):
1601 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001602
1603 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001604 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001605
1606 // If there's no return type (very rare in practice!), LLVM and Google style
1607 // agree.
1608 verifyFormat("- foo:(int)f;");
1609 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001610}
1611
Daniel Jasper886568d2013-01-09 08:36:49 +00001612TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001613 verifyFormat("int (^Block)(int, int);");
1614 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001615}
1616
Nico Weber27d13672013-01-09 20:25:35 +00001617TEST_F(FormatTest, FormatObjCInterface) {
1618 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001619 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001620 "@public\n"
1621 " int field1;\n"
1622 "@protected\n"
1623 " int field2;\n"
1624 "@private\n"
1625 " int field3;\n"
1626 "@package\n"
1627 " int field4;\n"
1628 "}\n"
1629 "+ (id)init;\n"
1630 "@end");
1631
Nico Weber27d13672013-01-09 20:25:35 +00001632 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1633 " @public\n"
1634 " int field1;\n"
1635 " @protected\n"
1636 " int field2;\n"
1637 " @private\n"
1638 " int field3;\n"
1639 " @package\n"
1640 " int field4;\n"
1641 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001642 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001643 "@end");
1644
1645 verifyFormat("@interface Foo\n"
1646 "+ (id)init;\n"
1647 "// Look, a comment!\n"
1648 "- (int)answerWith:(int)i;\n"
1649 "@end");
1650
1651 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001652 "@end\n"
1653 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001654 "@end");
1655
1656 verifyFormat("@interface Foo : Bar\n"
1657 "+ (id)init;\n"
1658 "@end");
1659
Nico Weber5f500df2013-01-10 20:12:55 +00001660 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001661 "+ (id)init;\n"
1662 "@end");
1663
Nico Weber5f500df2013-01-10 20:12:55 +00001664 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001665 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001666 "@end");
1667
Nico Webered91bba2013-01-10 19:19:14 +00001668 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001669 "+ (id)init;\n"
1670 "@end");
1671
Nico Webered91bba2013-01-10 19:19:14 +00001672 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001673 "+ (id)init;\n"
1674 "@end");
1675
Nico Weber5f500df2013-01-10 20:12:55 +00001676 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001677 "+ (id)init;\n"
1678 "@end");
1679
Nico Weber5f500df2013-01-10 20:12:55 +00001680 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001681 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001682 "@end");
1683
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001684 verifyFormat("@interface Foo {\n"
1685 " int _i;\n"
1686 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001687 "+ (id)init;\n"
1688 "@end");
1689
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001690 verifyFormat("@interface Foo : Bar {\n"
1691 " int _i;\n"
1692 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001693 "+ (id)init;\n"
1694 "@end");
1695
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001696 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1697 " int _i;\n"
1698 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001699 "+ (id)init;\n"
1700 "@end");
1701
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001702 verifyFormat("@interface Foo (HackStuff) {\n"
1703 " int _i;\n"
1704 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001705 "+ (id)init;\n"
1706 "@end");
1707
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001708 verifyFormat("@interface Foo () {\n"
1709 " int _i;\n"
1710 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001711 "+ (id)init;\n"
1712 "@end");
1713
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001714 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1715 " int _i;\n"
1716 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001717 "+ (id)init;\n"
1718 "@end");
1719}
1720
Nico Weber50767d82013-01-09 23:25:37 +00001721TEST_F(FormatTest, FormatObjCImplementation) {
1722 verifyFormat("@implementation Foo : NSObject {\n"
1723 "@public\n"
1724 " int field1;\n"
1725 "@protected\n"
1726 " int field2;\n"
1727 "@private\n"
1728 " int field3;\n"
1729 "@package\n"
1730 " int field4;\n"
1731 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001732 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001733 "@end");
1734
1735 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1736 " @public\n"
1737 " int field1;\n"
1738 " @protected\n"
1739 " int field2;\n"
1740 " @private\n"
1741 " int field3;\n"
1742 " @package\n"
1743 " int field4;\n"
1744 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001745 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001746 "@end");
1747
1748 verifyFormat("@implementation Foo\n"
1749 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001750 " if (true)\n"
1751 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001752 "}\n"
1753 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001754 "- (int)answerWith:(int)i {\n"
1755 " return i;\n"
1756 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001757 "+ (int)answerWith:(int)i {\n"
1758 " return i;\n"
1759 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001760 "@end");
1761
1762 verifyFormat("@implementation Foo\n"
1763 "@end\n"
1764 "@implementation Bar\n"
1765 "@end");
1766
1767 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001768 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001769 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001770 "@end");
1771
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001772 verifyFormat("@implementation Foo {\n"
1773 " int _i;\n"
1774 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001775 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001776 "@end");
1777
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001778 verifyFormat("@implementation Foo : Bar {\n"
1779 " int _i;\n"
1780 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001781 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001782 "@end");
1783
Nico Webered91bba2013-01-10 19:19:14 +00001784 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001785 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001786 "@end");
1787}
1788
Nico Weber1abe6ea2013-01-09 21:15:03 +00001789TEST_F(FormatTest, FormatObjCProtocol) {
1790 verifyFormat("@protocol Foo\n"
1791 "@property(weak) id delegate;\n"
1792 "- (NSUInteger)numberOfThings;\n"
1793 "@end");
1794
Nico Weber5f500df2013-01-10 20:12:55 +00001795 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001796 "- (NSUInteger)numberOfThings;\n"
1797 "@end");
1798
Nico Weber5f500df2013-01-10 20:12:55 +00001799 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001800 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001801 "@end");
1802
Nico Weber1abe6ea2013-01-09 21:15:03 +00001803 verifyFormat("@protocol Foo;\n"
1804 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001805
1806 verifyFormat("@protocol Foo\n"
1807 "@end\n"
1808 "@protocol Bar\n"
1809 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001810
1811 verifyFormat("@protocol myProtocol\n"
1812 "- (void)mandatoryWithInt:(int)i;\n"
1813 "@optional\n"
1814 "- (void)optional;\n"
1815 "@required\n"
1816 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001817 "@optional\n"
1818 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001819 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001820}
1821
Nico Weberbcfdd262013-01-12 06:18:40 +00001822TEST_F(FormatTest, FormatObjCMethodExpr) {
1823 verifyFormat("[foo bar:baz];");
1824 verifyFormat("return [foo bar:baz];");
1825 verifyFormat("f([foo bar:baz]);");
1826 verifyFormat("f(2, [foo bar:baz]);");
1827 verifyFormat("f(2, a ? b : c);");
1828 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1829
1830 verifyFormat("[foo bar:baz], [foo bar:baz];");
1831 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1832 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1833 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1834 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1835 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1836 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1837 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1838 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1839 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1840 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1841 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1842 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1843 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1844 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1845 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1846 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1847 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1848 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1849 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1850 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1851 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1852 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1853 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1854 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1855 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1856 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1857 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1858 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1859 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1860 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1861 // Whew!
1862
1863 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1864 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1865 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1866 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1867 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001868 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001869 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001870
Nico Weberbcfdd262013-01-12 06:18:40 +00001871 verifyFormat("arr[[self indexForFoo:a]];");
1872 verifyFormat("throw [self errorFor:a];");
1873 verifyFormat("@throw [self errorFor:a];");
1874
Nico Webere8ccc812013-01-12 22:48:47 +00001875 // This tests that the formatter doesn't break after "backing" but before ":",
1876 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001877 verifyFormat(
1878 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001879 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1880 " backing:NSBackingStoreBuffered defer:YES]))");
1881
Nico Webere8ccc812013-01-12 22:48:47 +00001882 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1883 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001884
1885}
1886
Nico Weber581f5572013-01-07 15:56:25 +00001887TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001888 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001889 verifyFormat("@catch");
1890 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001891 verifyFormat("@compatibility_alias");
1892 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001893 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001894 verifyFormat("@encode");
1895 verifyFormat("@end");
1896 verifyFormat("@finally");
1897 verifyFormat("@implementation");
1898 verifyFormat("@import");
1899 verifyFormat("@interface");
1900 verifyFormat("@optional");
1901 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001902 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001903 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001904 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001905 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001906 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001907 verifyFormat("@required");
1908 verifyFormat("@selector");
1909 verifyFormat("@synchronized");
1910 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001911 verifyFormat("@throw");
1912 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001913
Nico Webercb4d6902013-01-08 19:40:21 +00001914 verifyFormat("@\"String\"");
1915 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001916 verifyFormat("@+4.8");
1917 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001918 verifyFormat("@1LL");
1919 verifyFormat("@.5");
1920 verifyFormat("@'c'");
1921 verifyFormat("@true");
1922 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001923 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001924 verifyFormat("@[");
1925 verifyFormat("@{");
1926
Nico Weber581f5572013-01-07 15:56:25 +00001927 EXPECT_EQ("@interface", format("@ interface"));
1928
1929 // The precise formatting of this doesn't matter, nobody writes code like
1930 // this.
1931 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001932}
1933
Nico Weberc31689a2013-01-08 19:15:23 +00001934TEST_F(FormatTest, ObjCSnippets) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001935 verifyFormat("@autoreleasepool {\n"
1936 " foo();\n"
1937 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001938 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001939 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001940 verifyFormat("@dynamic textColor;");
Nico Weber5dfe9b42013-01-18 05:11:47 +00001941 verifyFormat("char *buf1 = @encode(int *);");
1942 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1943 // FIXME: Enable once PR14884 is fixed:
Nico Weberc31689a2013-01-08 19:15:23 +00001944 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001945 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weber6a21a552013-01-18 02:43:57 +00001946 verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001947 verifyFormat("@synchronized(self) {\n"
1948 " f();\n"
1949 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001950
Nico Weber70848232013-01-10 21:30:42 +00001951 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1952 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1953
Nico Webercf4a79c2013-01-08 17:56:31 +00001954 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001955 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1956 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001957}
1958
Daniel Jaspercd162382013-01-07 13:26:07 +00001959} // end namespace tooling
1960} // end namespace clang