blob: 2a2bb3fe6d95f38c4e63ff488d7c92e3fa14c716 [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 Jasper487f64b2013-01-13 16:10:20 +0000355 verifyFormat("void f(int i, // some comment (probably for i)\n"
356 " int j, // some comment (probably for j)\n"
357 " 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 Jasper7ad4eff2013-01-07 11:09:06 +0000364 verifyFormat("int i // This is a fancy variable\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000365 " = 5;");
Alexander Kornienko15757312012-12-06 18:03:27 +0000366
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000367 verifyFormat("enum E {\n"
368 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000369 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000370 " VAL_B\n"
371 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000372
373 verifyFormat(
374 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000375 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000376 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
377 " // Comment inside a statement.\n"
378 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000379
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000380 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000381 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000382
383 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000384}
385
386TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000387 verifyFormat("f(/*test=*/ true);");
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000388 EXPECT_EQ(
389 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
390 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
391 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n"
392 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
393 EXPECT_EQ(
394 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
395 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
396 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
397 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
398}
399
400TEST_F(FormatTest, CommentsInStaticInitializers) {
401 EXPECT_EQ(
402 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
403 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
404 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
405 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
406 " aaaaaaaaaaaaaaaaaaaa };",
407 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
408 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
409 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
410 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
411 " aaaaaaaaaaaaaaaaaaaa };"));
412 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
413 " bbbbbbbbbbb, ccccccccccc };");
414 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
415 " // comment for bb....\n"
416 " bbbbbbbbbbb, ccccccccccc };");
417 verifyGoogleFormat(
418 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
419 " bbbbbbbbbbb,\n"
420 " ccccccccccc };");
421 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
422 " // comment for bb....\n"
423 " bbbbbbbbbbb,\n"
424 " ccccccccccc };");
425
Alexander Kornienko15757312012-12-06 18:03:27 +0000426}
427
Alexander Kornienko15757312012-12-06 18:03:27 +0000428//===----------------------------------------------------------------------===//
429// Tests for classes, namespaces, etc.
430//===----------------------------------------------------------------------===//
431
432TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000433 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000434}
435
436TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
437 verifyFormat("class A {\n"
438 "public:\n"
439 "protected:\n"
440 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000441 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000442 "};");
443 verifyGoogleFormat("class A {\n"
444 " public:\n"
445 " protected:\n"
446 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000447 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000448 "};");
449}
450
451TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000452 verifyFormat("class A : public B {};");
453 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000454}
455
Manuel Klimekde768542013-01-07 18:10:23 +0000456TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000457 verifyFormat("class A {} a, b;");
458 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000459 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000460}
461
Alexander Kornienko15757312012-12-06 18:03:27 +0000462TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000463 verifyFormat("enum {\n"
464 " Zero,\n"
465 " One = 1,\n"
466 " Two = One + 1,\n"
467 " Three = (One + Two),\n"
468 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
469 " Five = (One, Two, Three, Four, 5)\n"
470 "};");
471 verifyFormat("enum Enum {\n"
472 "};");
473 verifyFormat("enum {\n"
474 "};");
475}
476
Nico Weberefaddc02013-01-14 05:49:49 +0000477TEST_F(FormatTest, FormatsBitfields) {
478 verifyFormat("struct Bitfields {\n"
479 " unsigned sClass : 8;\n"
480 " unsigned ValueKind : 2;\n"
481 "};");
482}
483
Alexander Kornienko15757312012-12-06 18:03:27 +0000484TEST_F(FormatTest, FormatsNamespaces) {
485 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000486 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000487 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000488 "}");
489 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000490 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000491 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000492 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000493 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000494 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000495 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000496 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000497 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000498 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000499 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000500}
501
Nico Webera9ccdd12013-01-07 16:36:17 +0000502TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000503 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
504 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000505 verifyFormat("try {\n"
506 " throw a * b;\n"
507 "}\n"
508 "catch (int a) {\n"
509 " // Do nothing.\n"
510 "}\n"
511 "catch (...) {\n"
512 " exit(42);\n"
513 "}");
514
515 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000516 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000517 "catch (...) {\n"
518 " return 5;\n"
519 "}");
520 verifyFormat("class A {\n"
521 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000522 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000523 " catch (...) {\n"
524 " throw;\n"
525 " }\n"
526 "};\n");
527}
528
529TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000530 verifyFormat("@try {\n"
531 " f();\n"
532 "}\n"
533 "@catch (NSException e) {\n"
534 " @throw;\n"
535 "}\n"
536 "@finally {\n"
537 " exit(42);\n"
538 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000539}
540
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000541TEST_F(FormatTest, StaticInitializers) {
542 verifyFormat("static SomeClass SC = { 1, 'a' };");
543
544 // FIXME: Format like enums if the static initializer does not fit on a line.
545 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000546 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000547 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
548 "};");
549
550 verifyFormat(
551 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
552 " looooooooooooooooooooooooooooooooooongname,\n"
553 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000554}
555
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000556TEST_F(FormatTest, NestedStaticInitializers) {
557 verifyFormat("static A x = { { {} } };\n");
558 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000559 "static A x = { { { init1, init2, init3, init4 },\n"
560 " { init1, init2, init3, init4 } } };");
561
Nico Weber6a21a552013-01-18 02:43:57 +0000562 // FIXME: Fix this in general and verify that it works in LLVM style again.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000563 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000564 "somes Status::global_reps[3] = {\n"
565 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
566 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
567 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
568 "};");
569 verifyFormat(
570 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
571 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
572 " } };");
573
Nico Weber6a21a552013-01-18 02:43:57 +0000574 // FIXME: We might at some point want to handle this similar to parameter
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000575 // lists, where we have an option to put each on a single line.
576 verifyFormat("struct {\n"
577 " unsigned bit;\n"
578 " const char *const name;\n"
579 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
580 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
581}
582
Manuel Klimeka080a182013-01-02 16:30:12 +0000583TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
584 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
585 " \\\n"
586 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
587}
588
Daniel Jasper71607512013-01-07 10:48:50 +0000589TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000590 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
591 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000592}
593
Manuel Klimeka080a182013-01-02 16:30:12 +0000594TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
595 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000596 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000597}
598
599TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
600 EXPECT_EQ("#line 42 \"test\"\n",
601 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000602 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000603 format("# \\\n define \\\n A \\\n B\n",
604 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000605}
606
607TEST_F(FormatTest, EndOfFileEndsPPDirective) {
608 EXPECT_EQ("#line 42 \"test\"",
609 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000610 EXPECT_EQ("#define A B",
611 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000612}
613
Manuel Klimek060143e2013-01-02 18:33:23 +0000614TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000615 // If the macro fits in one line, we still do not get the full
616 // line, as only the next line decides whether we need an escaped newline and
617 // thus use the last column.
618 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000619
Manuel Klimekd544c572013-01-07 09:24:17 +0000620 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
621 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000622 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000623
624 verifyFormat("#define A A\n#define A A");
625 verifyFormat("#define A(X) A\n#define A A");
626
627 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
628 verifyFormat("#define Something \\\n"
629 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000630}
631
632TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000633 EXPECT_EQ("// some comment\n"
634 "#include \"a.h\"\n"
635 "#define A(A,\\\n"
636 " B)\n"
637 "#include \"b.h\"\n"
638 "// some comment\n",
639 format(" // some comment\n"
640 " #include \"a.h\"\n"
641 "#define A(A,\\\n"
642 " B)\n"
643 " #include \"b.h\"\n"
644 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000645}
646
Manuel Klimekd4397b92013-01-04 23:34:14 +0000647TEST_F(FormatTest, LayoutSingleHash) {
648 EXPECT_EQ("#\na;", format("#\na;"));
649}
650
651TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
652 EXPECT_EQ("#define A \\\n"
653 " c; \\\n"
654 " e;\n"
655 "f;", format("#define A c; e;\n"
656 "f;", getLLVMStyleWithColumns(14)));
657}
658
659TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000660 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000661}
662
663TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000664 EXPECT_EQ("# define A\\\n b;",
665 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000666}
667
668TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000669 EXPECT_EQ("int x,\n"
670 "#define A\n"
671 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000672}
673
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000674TEST_F(FormatTest, HashInMacroDefinition) {
675 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
676 verifyFormat("#define A \\\n"
677 " { \\\n"
678 " f(#c);\\\n"
679 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000680
681 verifyFormat("#define A(X) \\\n"
682 " void function##X()", getLLVMStyleWithColumns(22));
683
684 verifyFormat("#define A(a, b, c) \\\n"
685 " void a##b##c()", getLLVMStyleWithColumns(22));
686
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000687 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000688}
689
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000690TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
691 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
692}
693
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000694TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000695 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000696}
697
Manuel Klimeka5342db2013-01-06 20:07:31 +0000698TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
699 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
700 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
701 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
702 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
703}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000704
Manuel Klimek95419382013-01-07 07:56:50 +0000705TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000706 EXPECT_EQ(
707 "#define A \\\n int i; \\\n int j;",
708 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000709}
710
Manuel Klimekd544c572013-01-07 09:24:17 +0000711TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
712 verifyFormat("#define A \\\n"
713 " int v( \\\n"
714 " a); \\\n"
715 " int i;", getLLVMStyleWithColumns(11));
716}
717
Manuel Klimeka080a182013-01-02 16:30:12 +0000718TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000719 EXPECT_EQ(
720 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
721 " \\\n"
722 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
723 "\n"
724 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
725 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
726 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
727 "\\\n"
728 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
729 " \n"
730 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
731 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000732}
733
Manuel Klimek526ed112013-01-09 15:25:02 +0000734TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
735 EXPECT_EQ("int\n"
736 "#define A\n"
737 " a;",
738 format("int\n#define A\na;"));
739 verifyFormat(
740 "functionCallTo(someOtherFunction(\n"
741 " withSomeParameters, whichInSequence,\n"
742 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000743 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000744 " withMoreParamters,\n"
745 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000746 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000747}
748
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000749TEST_F(FormatTest, LayoutBlockInsideParens) {
750 EXPECT_EQ("functionCall({\n"
751 " int i;\n"
752 "});", format(" functionCall ( {int i;} );"));
753}
754
755TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000756 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000757 "int i;", format(" SOME_MACRO {int i;} int i;"));
758}
759
760TEST_F(FormatTest, LayoutNestedBlocks) {
761 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
762 " struct s {\n"
763 " int i;\n"
764 " };\n"
765 " s kBitsToOs[] = { { 10 } };\n"
766 " for (int i = 0; i < 10; ++i)\n"
767 " return;\n"
768 "}");
769}
770
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000771TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
772 EXPECT_EQ("{}", format("{}"));
773}
774
Alexander Kornienko15757312012-12-06 18:03:27 +0000775//===----------------------------------------------------------------------===//
776// Line break tests.
777//===----------------------------------------------------------------------===//
778
779TEST_F(FormatTest, FormatsFunctionDefinition) {
780 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
781 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000782 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000783}
784
785TEST_F(FormatTest, FormatsAwesomeMethodCall) {
786 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000787 "SomeLongMethodName(SomeReallyLongMethod(\n"
788 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
789 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000790}
791
Daniel Jasper1321eb52012-12-18 21:05:13 +0000792TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000793 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000794 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
795 getLLVMStyleWithColumns(45));
796 verifyFormat("Constructor()\n"
797 " : Inttializer(FitsOnTheLine) {}",
798 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000799
800 verifyFormat(
801 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000802 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000803
804 verifyFormat(
805 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000806 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
807 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
808 verifyGoogleFormat(
809 "SomeClass::Constructor()\n"
810 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
811 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
812 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper16618242013-01-16 17:00:50 +0000813 verifyGoogleFormat(
814 "SomeClass::Constructor()\n"
815 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
816 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
817 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000818
819 verifyFormat(
820 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000821 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000822 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000823
824 verifyFormat("Constructor()\n"
825 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
826 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
827 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000828 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000829
830 // Here a line could be saved by splitting the second initializer onto two
831 // lines, but that is not desireable.
832 verifyFormat("Constructor()\n"
833 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
834 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000835 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000836
837 verifyGoogleFormat("MyClass::MyClass(int var)\n"
838 " : some_var_(var), // 4 space indent\n"
839 " some_other_var_(var + 1) { // lined up\n"
840 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000841
842 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000843 std::string input = "Constructor()\n"
844 " : aaaa(a,\n";
845 for (unsigned i = 0, e = 80; i != e; ++i) {
846 input += " a,\n";
847 }
848 input += " a) {}";
849 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000850}
851
Alexander Kornienko15757312012-12-06 18:03:27 +0000852TEST_F(FormatTest, BreaksAsHighAsPossible) {
853 verifyFormat(
854 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
855 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
856 " f();");
857}
858
Daniel Jasperbac016b2012-12-03 18:12:45 +0000859TEST_F(FormatTest, BreaksDesireably) {
860 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
861 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000862 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000863
864 verifyFormat(
865 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000867
868 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
869 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
870 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000871
872 verifyFormat(
873 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
874 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
875 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000877
878 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
879 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
880
Daniel Jasper723f0302013-01-02 14:40:02 +0000881 verifyFormat(
882 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
884
Daniel Jasper33182dd2012-12-05 14:57:28 +0000885 // This test case breaks on an incorrect memoization, i.e. an optimization not
886 // taking into account the StopAt value.
887 verifyFormat(
888 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000889 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
890 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
891 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000892
Daniel Jaspercd162382013-01-07 13:26:07 +0000893 verifyFormat("{\n {\n {\n"
894 " Annotation.SpaceRequiredBefore =\n"
895 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
896 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
897 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000898}
899
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000900TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
901 verifyGoogleFormat(
902 "aaaaaaaa(aaaaaaaaaaaaa,\n"
903 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
905 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
907 verifyGoogleFormat(
908 "aaaaaaaaaaaaaaa(aaaaaaaaa,\n"
909 " aaaaaaaaa,\n"
910 " aaaaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();");
911 verifyGoogleFormat(
912 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
913 " ddddddddddddddddddddddddddddd),\n"
914 " test);");
915
916 verifyGoogleFormat(
917 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
918 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
919 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
920 verifyGoogleFormat("a(\"a\"\n"
921 " \"a\",\n"
922 " a);");
923}
924
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000925TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
926 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
927 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +0000928 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
929 " GUARDED_BY(aaaaaaaaaaaaa);");
930 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
931 " GUARDED_BY(aaaaaaaaaaaaa) {}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000932}
933
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000934TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
935 verifyFormat(
936 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000937 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000938 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000939 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000940 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000941 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000942 verifyFormat(
943 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000944 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000945}
946
Daniel Jasper9cda8002013-01-07 13:08:40 +0000947TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
948 verifyFormat(
949 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
950 " SI->getAlignment(),\n"
951 " SI->getPointerAddressSpaceee());\n");
952 verifyFormat(
953 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
954 " Line.Tokens.front().Tok.getLocation(),\n"
955 " Line.Tokens.back().Tok.getLocation());");
956}
957
Daniel Jaspercf225b62012-12-24 13:43:52 +0000958TEST_F(FormatTest, AlignsAfterAssignments) {
959 verifyFormat(
960 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000961 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000962 verifyFormat(
963 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000964 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000965 verifyFormat(
966 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000967 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000968 verifyFormat(
969 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000970 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000971 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000972 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
973 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
974 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000975}
976
977TEST_F(FormatTest, AlignsAfterReturn) {
978 verifyFormat(
979 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
980 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
981 verifyFormat(
982 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
983 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
984}
985
Daniel Jasper9c837d02013-01-09 07:06:56 +0000986TEST_F(FormatTest, BreaksConditionalExpressions) {
987 verifyFormat(
988 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
989 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
990 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
991 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
992 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +0000993 verifyFormat(
994 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
995 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +0000996}
997
Nico Weber7d37b8b2013-01-12 01:28:06 +0000998TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
999 verifyFormat("arr[foo ? bar : baz];");
1000 verifyFormat("f()[foo ? bar : baz];");
1001 verifyFormat("(a + b)[foo ? bar : baz];");
1002 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1003}
1004
Daniel Jasperbac016b2012-12-03 18:12:45 +00001005TEST_F(FormatTest, AlignsStringLiterals) {
1006 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1007 " \"short literal\");");
1008 verifyFormat(
1009 "looooooooooooooooooooooooongFunction(\n"
1010 " \"short literal\"\n"
1011 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1012}
1013
Alexander Kornienko15757312012-12-06 18:03:27 +00001014TEST_F(FormatTest, AlignsPipes) {
1015 verifyFormat(
1016 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1017 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1018 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1019 verifyFormat(
1020 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1021 " << aaaaaaaaaaaaaaaaaaaa;");
1022 verifyFormat(
1023 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1024 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1025 verifyFormat(
1026 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1027 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1028 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1029 verifyFormat(
1030 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1031 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1032 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1033}
1034
Daniel Jasperbac016b2012-12-03 18:12:45 +00001035TEST_F(FormatTest, UnderstandsEquals) {
1036 verifyFormat(
1037 "aaaaaaaaaaaaaaaaa =\n"
1038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1039 verifyFormat(
1040 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001042 verifyFormat(
1043 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001044 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001045 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001046 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001047
Daniel Jasper9cda8002013-01-07 13:08:40 +00001048 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001049 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001050 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001051 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001052}
1053
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001054TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001055 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1056 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001057
Daniel Jasper1321eb52012-12-18 21:05:13 +00001058 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1059 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001060
1061 verifyFormat(
1062 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1063 " Parameter2);");
1064
1065 verifyFormat(
1066 "ShortObject->shortFunction(\n"
1067 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1068 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1069
1070 verifyFormat("loooooooooooooongFunction(\n"
1071 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1072
1073 verifyFormat(
1074 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1075 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1076
Daniel Jasper46a46a22013-01-07 07:13:20 +00001077 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001078 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001079 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001080 verifyFormat(
1081 "aaaaaaaaaaa->aaaaaaaaa(\n"
1082 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1083 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001084}
1085
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001086TEST_F(FormatTest, WrapsTemplateDeclarations) {
1087 verifyFormat("template <typename T>\n"
1088 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1089 verifyFormat(
1090 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1091 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
1092 verifyFormat(
1093 "template <typename T>\n"
1094 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1095 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001096 verifyFormat(
1097 "template <typename T>\n"
1098 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1099 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001101 verifyFormat("template <typename T>\n"
1102 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1103 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001104 verifyFormat(
1105 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1106 " typename T4 = char>\n"
1107 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001108}
1109
Daniel Jasperbac016b2012-12-03 18:12:45 +00001110TEST_F(FormatTest, UnderstandsTemplateParameters) {
1111 verifyFormat("A<int> a;");
1112 verifyFormat("A<A<A<int> > > a;");
1113 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1114 verifyFormat("bool x = a < 1 || 2 > a;");
1115 verifyFormat("bool x = 5 < f<int>();");
1116 verifyFormat("bool x = f<int>() > 5;");
1117 verifyFormat("bool x = 5 < a<int>::x;");
1118 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1119 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1120
1121 verifyGoogleFormat("A<A<int>> a;");
1122 verifyGoogleFormat("A<A<A<int>>> a;");
1123 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1124
1125 verifyFormat("test >> a >> b;");
1126 verifyFormat("test << a >> b;");
1127
1128 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001129 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001130}
1131
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001132TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001133 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001134 verifyFormat("f(-1, -2, -3);");
1135 verifyFormat("a[-1] = 5;");
1136 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001137 verifyFormat("if (i == -1) {}");
1138 verifyFormat("if (i != -1) {}");
1139 verifyFormat("if (i > -1) {}");
1140 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001141 verifyFormat("++(a->f());");
1142 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001143 verifyFormat("(a->f())++;");
1144 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001145 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001146
1147 verifyFormat("a-- > b;");
1148 verifyFormat("b ? -a : c;");
1149 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001150 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001151 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001152 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001153
1154 verifyFormat("return -1;");
1155 verifyFormat("switch (a) {\n"
1156 "case -1:\n"
1157 " break;\n"
1158 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001159
1160 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1161 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001162
1163 verifyFormat("int a = /* confusing comment */ -1;");
1164 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1165 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001166}
1167
1168TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001169 verifyFormat("bool operator<();");
1170 verifyFormat("bool operator>();");
1171 verifyFormat("bool operator=();");
1172 verifyFormat("bool operator==();");
1173 verifyFormat("bool operator!=();");
1174 verifyFormat("int operator+();");
1175 verifyFormat("int operator++();");
1176 verifyFormat("bool operator();");
1177 verifyFormat("bool operator()();");
1178 verifyFormat("bool operator[]();");
1179 verifyFormat("operator bool();");
1180 verifyFormat("operator SomeType<int>();");
1181 verifyFormat("void *operator new(std::size_t size);");
1182 verifyFormat("void *operator new[](std::size_t size);");
1183 verifyFormat("void operator delete(void *ptr);");
1184 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001185}
1186
Daniel Jasper088dab52013-01-11 16:09:04 +00001187TEST_F(FormatTest, UnderstandsNewAndDelete) {
1188 verifyFormat("A *a = new A;");
1189 verifyFormat("A *a = new (placement) A;");
1190 verifyFormat("delete a;");
1191 verifyFormat("delete (A *)a;");
1192}
1193
Daniel Jasper5d334402013-01-02 08:57:10 +00001194TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001195 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001196 verifyFormat("f(a, *a);");
1197 verifyFormat("f(*a);");
1198 verifyFormat("int a = b * 10;");
1199 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001200 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001201 verifyFormat("int a += b * c;");
1202 verifyFormat("int a -= b * c;");
1203 verifyFormat("int a *= b * c;");
1204 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001205 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001206 verifyFormat("int a = *b * c;");
1207 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001208 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001209 verifyFormat("return 10 * b;");
1210 verifyFormat("return *b * *c;");
1211 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001212 verifyFormat("f(b ? *c : *d);");
1213 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001214 verifyFormat("*b = a;");
1215 verifyFormat("a * ~b;");
1216 verifyFormat("a * !b;");
1217 verifyFormat("a * +b;");
1218 verifyFormat("a * -b;");
1219 verifyFormat("a * ++b;");
1220 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001221 verifyFormat("a[4] * b;");
1222 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001223 verifyFormat("a * [self dostuff];");
1224 verifyFormat("a * (a + b);");
1225 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001226 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001227
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001228 verifyFormat("InvalidRegions[*R] = 0;");
1229
Daniel Jasper8b39c662012-12-10 18:59:13 +00001230 verifyFormat("A<int *> a;");
1231 verifyFormat("A<int **> a;");
1232 verifyFormat("A<int *, int *> a;");
1233 verifyFormat("A<int **, int **> a;");
1234
Daniel Jasper2db356d2013-01-08 20:03:18 +00001235 verifyFormat(
1236 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1237 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1238
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001239 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001240 verifyGoogleFormat("A<int*> a;");
1241 verifyGoogleFormat("A<int**> a;");
1242 verifyGoogleFormat("A<int*, int*> a;");
1243 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001244 verifyGoogleFormat("f(b ? *c : *d);");
1245 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001246 verifyGoogleFormat("Type* t = **x;");
1247 verifyGoogleFormat("Type* t = *++*x;");
1248 verifyGoogleFormat("*++*x;");
1249 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1250 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001251
1252 verifyFormat("a = *(x + y);");
1253 verifyFormat("a = &(x + y);");
1254 verifyFormat("*(x + y).call();");
1255 verifyFormat("&(x + y)->call();");
1256 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001257
1258 verifyFormat("f(b * /* confusing comment */ ++c);");
1259 verifyFormat(
1260 "int *MyValues = {\n"
1261 " *A, // Operator detection might be confused by the '{'\n"
1262 " *BB // Operator detection might be confused by previous comment\n"
1263 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001264
1265 verifyFormat("if (int *a = &b)");
1266 verifyFormat("if (int &a = *b)");
1267 verifyFormat("if (a & b[i])");
1268 verifyFormat("if (a::b::c::d & b[i])");
1269 verifyFormat("if (*b[i])");
1270 verifyFormat("if (int *a = (&b))");
1271 verifyFormat("while (int *a = &b)");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001272}
1273
Daniel Jasper4981bd02013-01-13 08:01:36 +00001274TEST_F(FormatTest, FormatsCasts) {
1275 verifyFormat("Type *A = static_cast<Type *>(P);");
1276 verifyFormat("Type *A = (Type *)P;");
1277 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1278 verifyFormat("int a = (int)(2.0f);");
1279
1280 // FIXME: These also need to be identified.
1281 verifyFormat("int a = (int) 2.0f;");
1282 verifyFormat("int a = (int) * b;");
1283
1284 // These are not casts.
1285 verifyFormat("void f(int *) {}");
1286 verifyFormat("void f(int *);");
1287 verifyFormat("void f(int *) = 0;");
1288 verifyFormat("void f(SmallVector<int>) {}");
1289 verifyFormat("void f(SmallVector<int>);");
1290 verifyFormat("void f(SmallVector<int>) = 0;");
1291}
1292
Daniel Jasper46ef8522013-01-10 13:08:12 +00001293TEST_F(FormatTest, FormatsFunctionTypes) {
1294 // FIXME: Determine the cases that need a space after the return type and fix.
1295 verifyFormat("A<bool()> a;");
1296 verifyFormat("A<SomeType()> a;");
1297 verifyFormat("A<void(*)(int, std::string)> a;");
1298
1299 verifyFormat("int(*func)(void *);");
1300}
1301
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001302TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001303 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001304 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001305 verifyFormat(
1306 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1307 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001308 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001309}
1310
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001311TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1312 verifyFormat("(a)->b();");
1313 verifyFormat("--a;");
1314}
1315
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001316TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001317 verifyFormat("#include <string>\n"
1318 "#include <a/b/c.h>\n"
1319 "#include \"a/b/string\"\n"
1320 "#include \"string.h\"\n"
1321 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001322 "#include <a-a>\n"
1323 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001324
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001325 verifyFormat("#import <string>");
1326 verifyFormat("#import <a/b/c.h>");
1327 verifyFormat("#import \"a/b/string\"");
1328 verifyFormat("#import \"string.h\"");
1329 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001330}
1331
Alexander Kornienko15757312012-12-06 18:03:27 +00001332//===----------------------------------------------------------------------===//
1333// Error recovery tests.
1334//===----------------------------------------------------------------------===//
1335
Daniel Jasper700e7102013-01-10 09:26:47 +00001336TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001337 verifyFormat("void f() { return; }\n42");
1338 verifyFormat("void f() {\n"
1339 " if (0)\n"
1340 " return;\n"
1341 "}\n"
1342 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001343 verifyFormat("void f() { return }\n42");
1344 verifyFormat("void f() {\n"
1345 " if (0)\n"
1346 " return\n"
1347 "}\n"
1348 "42");
1349}
1350
1351TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1352 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1353 EXPECT_EQ("void f() {\n"
1354 " if (a)\n"
1355 " return\n"
1356 "}", format("void f ( ) { if ( a ) return }"));
1357 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1358 EXPECT_EQ("namespace N {\n"
1359 "void f() {}\n"
1360 "void g()\n"
1361 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001362}
1363
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001364TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1365 verifyFormat("int aaaaaaaa =\n"
1366 " // Overly long comment\n"
1367 " b;", getLLVMStyleWithColumns(20));
1368 verifyFormat("function(\n"
1369 " ShortArgument,\n"
1370 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1371}
1372
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001373TEST_F(FormatTest, IncorrectAccessSpecifier) {
1374 verifyFormat("public:");
1375 verifyFormat("class A {\n"
1376 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001377 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001378 "};");
1379 verifyFormat("public\n"
1380 "int qwerty;");
1381 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001382 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001383 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001384 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001385 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001386 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001387}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001388
Alexander Kornienko393b0082012-12-04 15:40:36 +00001389TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1390 verifyFormat("{");
1391}
1392
1393TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001394 verifyFormat("do {}");
1395 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001396 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001397 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001398 "wheeee(fun);");
1399 verifyFormat("do {\n"
1400 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001401 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001402}
1403
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001404TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001405 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001406 verifyFormat("switch {\n foo;\n foo();\n}");
1407 verifyFormat("for {\n foo;\n foo();\n}");
1408 verifyFormat("while {\n foo;\n foo();\n}");
1409 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001410}
1411
Daniel Jasper1f42f112013-01-04 18:52:56 +00001412TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1413 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001414 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001415}
1416
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001417TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001418 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1419 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1420 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1421 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001422
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001423 EXPECT_EQ("{\n"
1424 " {\n"
1425 " breakme(\n"
1426 " qwe);\n"
1427 "}\n", format("{\n"
1428 " {\n"
1429 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001430 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001431}
1432
Manuel Klimek2851c162013-01-10 14:36:46 +00001433TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1434 verifyFormat(
1435 "int x = {\n"
1436 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001437 " b(alongervariable)\n"
1438 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001439}
1440
1441TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1442 verifyFormat(
1443 "Aaa({\n"
1444 " int i;\n"
1445 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1446 " ccccccccccccccccc));");
1447}
1448
Manuel Klimek517e8942013-01-11 17:54:10 +00001449TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1450 verifyFormat("void f() { return 42; }");
1451 verifyFormat("void f() {\n"
1452 " // Comment\n"
1453 "}");
1454 verifyFormat("{\n"
1455 "#error {\n"
1456 " int a;\n"
1457 "}");
1458 verifyFormat("{\n"
1459 " int a;\n"
1460 "#error {\n"
1461 "}");
1462}
1463
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001464TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1465 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001466 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001467 verifyFormat("class foo a = { bar };\nint n;");
1468 verifyFormat("union foo a = { bar };\nint n;");
1469
1470 // Elaborate types inside function definitions.
1471 verifyFormat("struct foo f() {}\nint n;");
1472 verifyFormat("class foo f() {}\nint n;");
1473 verifyFormat("union foo f() {}\nint n;");
1474
1475 // Templates.
1476 verifyFormat("template <class X> void f() {}\nint n;");
1477 verifyFormat("template <struct X> void f() {}\nint n;");
1478 verifyFormat("template <union X> void f() {}\nint n;");
1479
1480 // Actual definitions...
1481 verifyFormat("struct {} n;");
1482 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1483 verifyFormat("union Z {\n int n;\n} x;");
1484 verifyFormat("class MACRO Z {} n;");
1485 verifyFormat("class MACRO(X) Z {} n;");
1486 verifyFormat("class __attribute__(X) Z {} n;");
1487 verifyFormat("class __declspec(X) Z {} n;");
1488
1489 // Elaborate types where incorrectly parsing the structural element would
1490 // break the indent.
1491 verifyFormat("if (true)\n"
1492 " class X x;\n"
1493 "else\n"
1494 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001495}
1496
Manuel Klimek407a31a2013-01-15 15:50:27 +00001497TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1498 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1499 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1500 EXPECT_EQ("#error 1", format(" # error 1"));
1501 EXPECT_EQ("#warning 1", format(" # warning 1"));
1502}
1503
Manuel Klimek517e8942013-01-11 17:54:10 +00001504// FIXME: This breaks the order of the unwrapped lines:
1505// TEST_F(FormatTest, OrderUnwrappedLines) {
1506// verifyFormat("{\n"
1507// " bool a; //\n"
1508// "#error {\n"
1509// " int a;\n"
1510// "}");
1511// }
1512
Nico Webercf4a79c2013-01-08 17:56:31 +00001513//===----------------------------------------------------------------------===//
1514// Objective-C tests.
1515//===----------------------------------------------------------------------===//
1516
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001517TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1518 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1519 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1520 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001521 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001522 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1523 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1524 format("-(NSInteger)Method3:(id)anObject;"));
1525 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1526 format("-(NSInteger)Method4:(id)anObject;"));
1527 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1528 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1529 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1530 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001531 EXPECT_EQ(
1532 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1533 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001534
1535 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001536 EXPECT_EQ(
1537 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1538 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1539 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1540 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1541 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1542 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1543 format(
1544 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1545 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1546 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1547 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1548 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1549 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001550
1551 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001552 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001553 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1554 // protocol lists (but not for template classes):
1555 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001556
1557 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001558 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001559
1560 // If there's no return type (very rare in practice!), LLVM and Google style
1561 // agree.
1562 verifyFormat("- foo:(int)f;");
1563 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001564}
1565
Daniel Jasper886568d2013-01-09 08:36:49 +00001566TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001567 verifyFormat("int (^Block)(int, int);");
1568 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001569}
1570
Nico Weber27d13672013-01-09 20:25:35 +00001571TEST_F(FormatTest, FormatObjCInterface) {
1572 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001573 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001574 "@public\n"
1575 " int field1;\n"
1576 "@protected\n"
1577 " int field2;\n"
1578 "@private\n"
1579 " int field3;\n"
1580 "@package\n"
1581 " int field4;\n"
1582 "}\n"
1583 "+ (id)init;\n"
1584 "@end");
1585
Nico Weber27d13672013-01-09 20:25:35 +00001586 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1587 " @public\n"
1588 " int field1;\n"
1589 " @protected\n"
1590 " int field2;\n"
1591 " @private\n"
1592 " int field3;\n"
1593 " @package\n"
1594 " int field4;\n"
1595 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001596 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001597 "@end");
1598
1599 verifyFormat("@interface Foo\n"
1600 "+ (id)init;\n"
1601 "// Look, a comment!\n"
1602 "- (int)answerWith:(int)i;\n"
1603 "@end");
1604
1605 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001606 "@end\n"
1607 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001608 "@end");
1609
1610 verifyFormat("@interface Foo : Bar\n"
1611 "+ (id)init;\n"
1612 "@end");
1613
Nico Weber5f500df2013-01-10 20:12:55 +00001614 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001615 "+ (id)init;\n"
1616 "@end");
1617
Nico Weber5f500df2013-01-10 20:12:55 +00001618 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001619 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001620 "@end");
1621
Nico Webered91bba2013-01-10 19:19:14 +00001622 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001623 "+ (id)init;\n"
1624 "@end");
1625
Nico Webered91bba2013-01-10 19:19:14 +00001626 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001627 "+ (id)init;\n"
1628 "@end");
1629
Nico Weber5f500df2013-01-10 20:12:55 +00001630 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001631 "+ (id)init;\n"
1632 "@end");
1633
Nico Weber5f500df2013-01-10 20:12:55 +00001634 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001635 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001636 "@end");
1637
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001638 verifyFormat("@interface Foo {\n"
1639 " int _i;\n"
1640 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001641 "+ (id)init;\n"
1642 "@end");
1643
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001644 verifyFormat("@interface Foo : Bar {\n"
1645 " int _i;\n"
1646 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001647 "+ (id)init;\n"
1648 "@end");
1649
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001650 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1651 " int _i;\n"
1652 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001653 "+ (id)init;\n"
1654 "@end");
1655
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001656 verifyFormat("@interface Foo (HackStuff) {\n"
1657 " int _i;\n"
1658 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001659 "+ (id)init;\n"
1660 "@end");
1661
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001662 verifyFormat("@interface Foo () {\n"
1663 " int _i;\n"
1664 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001665 "+ (id)init;\n"
1666 "@end");
1667
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001668 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1669 " int _i;\n"
1670 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001671 "+ (id)init;\n"
1672 "@end");
1673}
1674
Nico Weber50767d82013-01-09 23:25:37 +00001675TEST_F(FormatTest, FormatObjCImplementation) {
1676 verifyFormat("@implementation Foo : NSObject {\n"
1677 "@public\n"
1678 " int field1;\n"
1679 "@protected\n"
1680 " int field2;\n"
1681 "@private\n"
1682 " int field3;\n"
1683 "@package\n"
1684 " int field4;\n"
1685 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001686 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001687 "@end");
1688
1689 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1690 " @public\n"
1691 " int field1;\n"
1692 " @protected\n"
1693 " int field2;\n"
1694 " @private\n"
1695 " int field3;\n"
1696 " @package\n"
1697 " int field4;\n"
1698 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001699 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001700 "@end");
1701
1702 verifyFormat("@implementation Foo\n"
1703 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001704 " if (true)\n"
1705 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001706 "}\n"
1707 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001708 "- (int)answerWith:(int)i {\n"
1709 " return i;\n"
1710 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001711 "+ (int)answerWith:(int)i {\n"
1712 " return i;\n"
1713 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001714 "@end");
1715
1716 verifyFormat("@implementation Foo\n"
1717 "@end\n"
1718 "@implementation Bar\n"
1719 "@end");
1720
1721 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001722 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001723 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001724 "@end");
1725
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001726 verifyFormat("@implementation Foo {\n"
1727 " int _i;\n"
1728 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001729 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001730 "@end");
1731
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001732 verifyFormat("@implementation Foo : Bar {\n"
1733 " int _i;\n"
1734 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001735 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001736 "@end");
1737
Nico Webered91bba2013-01-10 19:19:14 +00001738 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001739 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001740 "@end");
1741}
1742
Nico Weber1abe6ea2013-01-09 21:15:03 +00001743TEST_F(FormatTest, FormatObjCProtocol) {
1744 verifyFormat("@protocol Foo\n"
1745 "@property(weak) id delegate;\n"
1746 "- (NSUInteger)numberOfThings;\n"
1747 "@end");
1748
Nico Weber5f500df2013-01-10 20:12:55 +00001749 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001750 "- (NSUInteger)numberOfThings;\n"
1751 "@end");
1752
Nico Weber5f500df2013-01-10 20:12:55 +00001753 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001754 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001755 "@end");
1756
Nico Weber1abe6ea2013-01-09 21:15:03 +00001757 verifyFormat("@protocol Foo;\n"
1758 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001759
1760 verifyFormat("@protocol Foo\n"
1761 "@end\n"
1762 "@protocol Bar\n"
1763 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001764
1765 verifyFormat("@protocol myProtocol\n"
1766 "- (void)mandatoryWithInt:(int)i;\n"
1767 "@optional\n"
1768 "- (void)optional;\n"
1769 "@required\n"
1770 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001771 "@optional\n"
1772 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001773 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001774}
1775
Nico Weberbcfdd262013-01-12 06:18:40 +00001776TEST_F(FormatTest, FormatObjCMethodExpr) {
1777 verifyFormat("[foo bar:baz];");
1778 verifyFormat("return [foo bar:baz];");
1779 verifyFormat("f([foo bar:baz]);");
1780 verifyFormat("f(2, [foo bar:baz]);");
1781 verifyFormat("f(2, a ? b : c);");
1782 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1783
1784 verifyFormat("[foo bar:baz], [foo bar:baz];");
1785 verifyFormat("[foo bar:baz] = [foo bar:baz];");
1786 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
1787 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
1788 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
1789 verifyFormat("[foo bar:baz] += [foo bar:baz];");
1790 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
1791 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1792 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1793 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1794 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1795 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1796 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1797 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1798 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1799 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1800 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1801 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1802 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1803 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1804 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1805 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
1806 verifyFormat("[foo bar:baz] > [foo bar:baz];");
1807 verifyFormat("[foo bar:baz] < [foo bar:baz];");
1808 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
1809 verifyFormat("[foo bar:baz] << [foo bar:baz];");
1810 verifyFormat("[foo bar:baz] - [foo bar:baz];");
1811 verifyFormat("[foo bar:baz] + [foo bar:baz];");
1812 verifyFormat("[foo bar:baz] * [foo bar:baz];");
1813 verifyFormat("[foo bar:baz] / [foo bar:baz];");
1814 verifyFormat("[foo bar:baz] % [foo bar:baz];");
1815 // Whew!
1816
1817 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1818 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1819 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1820 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1821 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001822 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001823 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001824
Nico Weberbcfdd262013-01-12 06:18:40 +00001825 verifyFormat("arr[[self indexForFoo:a]];");
1826 verifyFormat("throw [self errorFor:a];");
1827 verifyFormat("@throw [self errorFor:a];");
1828
Nico Webere8ccc812013-01-12 22:48:47 +00001829 // This tests that the formatter doesn't break after "backing" but before ":",
1830 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001831 verifyFormat(
1832 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001833 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1834 " backing:NSBackingStoreBuffered defer:YES]))");
1835
Nico Webere8ccc812013-01-12 22:48:47 +00001836 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1837 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001838
1839}
1840
Nico Weber581f5572013-01-07 15:56:25 +00001841TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001842 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001843 verifyFormat("@catch");
1844 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001845 verifyFormat("@compatibility_alias");
1846 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001847 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001848 verifyFormat("@encode");
1849 verifyFormat("@end");
1850 verifyFormat("@finally");
1851 verifyFormat("@implementation");
1852 verifyFormat("@import");
1853 verifyFormat("@interface");
1854 verifyFormat("@optional");
1855 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001856 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001857 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001858 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001859 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001860 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001861 verifyFormat("@required");
1862 verifyFormat("@selector");
1863 verifyFormat("@synchronized");
1864 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001865 verifyFormat("@throw");
1866 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001867
Nico Webercb4d6902013-01-08 19:40:21 +00001868 verifyFormat("@\"String\"");
1869 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001870 verifyFormat("@+4.8");
1871 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001872 verifyFormat("@1LL");
1873 verifyFormat("@.5");
1874 verifyFormat("@'c'");
1875 verifyFormat("@true");
1876 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001877 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001878 verifyFormat("@[");
1879 verifyFormat("@{");
1880
Nico Weber581f5572013-01-07 15:56:25 +00001881 EXPECT_EQ("@interface", format("@ interface"));
1882
1883 // The precise formatting of this doesn't matter, nobody writes code like
1884 // this.
1885 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001886}
1887
Nico Weberc31689a2013-01-08 19:15:23 +00001888TEST_F(FormatTest, ObjCSnippets) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001889 verifyFormat("@autoreleasepool {\n"
1890 " foo();\n"
1891 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001892 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001893 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001894 verifyFormat("@dynamic textColor;");
Nico Weber5dfe9b42013-01-18 05:11:47 +00001895 verifyFormat("char *buf1 = @encode(int *);");
1896 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1897 // FIXME: Enable once PR14884 is fixed:
Nico Weberc31689a2013-01-08 19:15:23 +00001898 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001899 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weber6a21a552013-01-18 02:43:57 +00001900 verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001901 verifyFormat("@synchronized(self) {\n"
1902 " f();\n"
1903 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001904
Nico Weber70848232013-01-10 21:30:42 +00001905 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1906 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1907
Nico Webercf4a79c2013-01-08 17:56:31 +00001908 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001909 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1910 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001911}
1912
Daniel Jaspercd162382013-01-07 13:26:07 +00001913} // end namespace tooling
1914} // end namespace clang