blob: 7013eb14172ad593c76ec99b6bb634e158c1cf24 [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
389
Alexander Kornienko15757312012-12-06 18:03:27 +0000390
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000391 verifyFormat("enum E {\n"
392 " // comment\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000393 " VAL_A, // comment\n"
Daniel Jaspere35ec2b2012-12-18 19:56:56 +0000394 " VAL_B\n"
395 "};");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000396
397 verifyFormat(
398 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000399 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
Daniel Jasper043835a2013-01-09 09:33:39 +0000400 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
401 " // Comment inside a statement.\n"
402 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
Manuel Klimek6cf58142013-01-07 08:54:53 +0000403
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000404 EXPECT_EQ("int i; // single line trailing comment",
Manuel Klimek6cf58142013-01-07 08:54:53 +0000405 format("int i;\\\n// single line trailing comment"));
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000406
407 verifyGoogleFormat("int a; // Trailing comment.");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000408}
409
410TEST_F(FormatTest, UnderstandsMultiLineComments) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000411 verifyFormat("f(/*test=*/ true);");
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000412 EXPECT_EQ(
413 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
414 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
415 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n"
416 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
417 EXPECT_EQ(
418 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
419 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
420 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
421 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
422}
423
424TEST_F(FormatTest, CommentsInStaticInitializers) {
425 EXPECT_EQ(
426 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
427 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
428 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
429 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
430 " aaaaaaaaaaaaaaaaaaaa };",
431 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
432 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
433 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
434 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
435 " aaaaaaaaaaaaaaaaaaaa };"));
436 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
437 " bbbbbbbbbbb, ccccccccccc };");
438 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
439 " // comment for bb....\n"
440 " bbbbbbbbbbb, ccccccccccc };");
441 verifyGoogleFormat(
442 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
443 " bbbbbbbbbbb,\n"
444 " ccccccccccc };");
445 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
446 " // comment for bb....\n"
447 " bbbbbbbbbbb,\n"
448 " ccccccccccc };");
449
Alexander Kornienko15757312012-12-06 18:03:27 +0000450}
451
Alexander Kornienko15757312012-12-06 18:03:27 +0000452//===----------------------------------------------------------------------===//
453// Tests for classes, namespaces, etc.
454//===----------------------------------------------------------------------===//
455
456TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000457 verifyFormat("class A {};");
Alexander Kornienko15757312012-12-06 18:03:27 +0000458}
459
460TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
461 verifyFormat("class A {\n"
462 "public:\n"
463 "protected:\n"
464 "private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000465 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000466 "};");
467 verifyGoogleFormat("class A {\n"
468 " public:\n"
469 " protected:\n"
470 " private:\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000471 " void f() {}\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000472 "};");
473}
474
475TEST_F(FormatTest, FormatsDerivedClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000476 verifyFormat("class A : public B {};");
477 verifyFormat("class A : public ::B {};");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000478}
479
Manuel Klimekde768542013-01-07 18:10:23 +0000480TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000481 verifyFormat("class A {} a, b;");
482 verifyFormat("struct A {} a, b;");
Daniel Jasper17746032013-01-13 14:39:04 +0000483 verifyFormat("union A {} a;");
Manuel Klimekde768542013-01-07 18:10:23 +0000484}
485
Alexander Kornienko15757312012-12-06 18:03:27 +0000486TEST_F(FormatTest, FormatsEnum) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000487 verifyFormat("enum {\n"
488 " Zero,\n"
489 " One = 1,\n"
490 " Two = One + 1,\n"
491 " Three = (One + Two),\n"
492 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
493 " Five = (One, Two, Three, Four, 5)\n"
494 "};");
495 verifyFormat("enum Enum {\n"
496 "};");
497 verifyFormat("enum {\n"
498 "};");
499}
500
Nico Weberefaddc02013-01-14 05:49:49 +0000501TEST_F(FormatTest, FormatsBitfields) {
502 verifyFormat("struct Bitfields {\n"
503 " unsigned sClass : 8;\n"
504 " unsigned ValueKind : 2;\n"
505 "};");
506}
507
Alexander Kornienko15757312012-12-06 18:03:27 +0000508TEST_F(FormatTest, FormatsNamespaces) {
509 verifyFormat("namespace some_namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000510 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000511 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000512 "}");
513 verifyFormat("namespace {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000514 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000515 "void f() { f(); }\n"
Alexander Kornienko15757312012-12-06 18:03:27 +0000516 "}");
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000517 verifyFormat("inline namespace X {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000518 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000519 "void f() { f(); }\n"
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000520 "}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000521 verifyFormat("using namespace some_namespace;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000522 "class A {};\n"
Manuel Klimek517e8942013-01-11 17:54:10 +0000523 "void f() { f(); }");
Alexander Kornienko15757312012-12-06 18:03:27 +0000524}
525
Nico Webera9ccdd12013-01-07 16:36:17 +0000526TEST_F(FormatTest, FormatTryCatch) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000527 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
528 // also not create single-line-blocks.
Nico Webera9ccdd12013-01-07 16:36:17 +0000529 verifyFormat("try {\n"
530 " throw a * b;\n"
531 "}\n"
532 "catch (int a) {\n"
533 " // Do nothing.\n"
534 "}\n"
535 "catch (...) {\n"
536 " exit(42);\n"
537 "}");
538
539 // Function-level try statements.
Manuel Klimek517e8942013-01-11 17:54:10 +0000540 verifyFormat("int f() try { return 4; }\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000541 "catch (...) {\n"
542 " return 5;\n"
543 "}");
544 verifyFormat("class A {\n"
545 " int a;\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000546 " A() try : a(0) {}\n"
Nico Webera9ccdd12013-01-07 16:36:17 +0000547 " catch (...) {\n"
548 " throw;\n"
549 " }\n"
550 "};\n");
551}
552
553TEST_F(FormatTest, FormatObjCTryCatch) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +0000554 verifyFormat("@try {\n"
555 " f();\n"
556 "}\n"
557 "@catch (NSException e) {\n"
558 " @throw;\n"
559 "}\n"
560 "@finally {\n"
561 " exit(42);\n"
562 "}");
Nico Webera9ccdd12013-01-07 16:36:17 +0000563}
564
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000565TEST_F(FormatTest, StaticInitializers) {
566 verifyFormat("static SomeClass SC = { 1, 'a' };");
567
568 // FIXME: Format like enums if the static initializer does not fit on a line.
569 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000570 "static SomeClass WithALoooooooooooooooooooongName = {\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000571 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
572 "};");
573
574 verifyFormat(
575 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
576 " looooooooooooooooooooooooooooooooooongname,\n"
577 " looooooooooooooooooooooooooooooong };");
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000578}
579
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000580TEST_F(FormatTest, NestedStaticInitializers) {
581 verifyFormat("static A x = { { {} } };\n");
582 verifyFormat(
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000583 "static A x = { { { init1, init2, init3, init4 },\n"
584 " { init1, init2, init3, init4 } } };");
585
Nico Weber6a21a552013-01-18 02:43:57 +0000586 // FIXME: Fix this in general and verify that it works in LLVM style again.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000587 verifyGoogleFormat(
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000588 "somes Status::global_reps[3] = {\n"
589 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
590 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
591 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
592 "};");
593 verifyFormat(
594 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
595 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
596 " } };");
597
Nico Weber6a21a552013-01-18 02:43:57 +0000598 // FIXME: We might at some point want to handle this similar to parameter
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000599 // lists, where we have an option to put each on a single line.
600 verifyFormat("struct {\n"
601 " unsigned bit;\n"
602 " const char *const name;\n"
603 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
604 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
605}
606
Manuel Klimeka080a182013-01-02 16:30:12 +0000607TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
608 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
609 " \\\n"
610 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
611}
612
Daniel Jasper71607512013-01-07 10:48:50 +0000613TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000614 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
615 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
Daniel Jasper71607512013-01-07 10:48:50 +0000616}
617
Manuel Klimeka080a182013-01-02 16:30:12 +0000618TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
619 EXPECT_EQ("#\n;", format("#;"));
Manuel Klimekdd5b1012013-01-07 10:03:37 +0000620 verifyFormat("#\n;\n;\n;");
Manuel Klimeka080a182013-01-02 16:30:12 +0000621}
622
623TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
624 EXPECT_EQ("#line 42 \"test\"\n",
625 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000626 EXPECT_EQ("#define A B\n",
Manuel Klimekd4397b92013-01-04 23:34:14 +0000627 format("# \\\n define \\\n A \\\n B\n",
628 getLLVMStyleWithColumns(12)));
Manuel Klimeka080a182013-01-02 16:30:12 +0000629}
630
631TEST_F(FormatTest, EndOfFileEndsPPDirective) {
632 EXPECT_EQ("#line 42 \"test\"",
633 format("# \\\n line \\\n 42 \\\n \"test\""));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000634 EXPECT_EQ("#define A B",
635 format("# \\\n define \\\n A \\\n B"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000636}
637
Manuel Klimek060143e2013-01-02 18:33:23 +0000638TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
Manuel Klimekd544c572013-01-07 09:24:17 +0000639 // If the macro fits in one line, we still do not get the full
640 // line, as only the next line decides whether we need an escaped newline and
641 // thus use the last column.
642 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
Manuel Klimek060143e2013-01-02 18:33:23 +0000643
Manuel Klimekd544c572013-01-07 09:24:17 +0000644 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12));
645 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12));
Manuel Klimek060143e2013-01-02 18:33:23 +0000646 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000647
648 verifyFormat("#define A A\n#define A A");
649 verifyFormat("#define A(X) A\n#define A A");
650
651 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
652 verifyFormat("#define Something \\\n"
653 " Other", getLLVMStyleWithColumns(23));
Manuel Klimek060143e2013-01-02 18:33:23 +0000654}
655
656TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000657 EXPECT_EQ("// some comment\n"
658 "#include \"a.h\"\n"
659 "#define A(A,\\\n"
660 " B)\n"
661 "#include \"b.h\"\n"
662 "// some comment\n",
663 format(" // some comment\n"
664 " #include \"a.h\"\n"
665 "#define A(A,\\\n"
666 " B)\n"
667 " #include \"b.h\"\n"
668 " // some comment\n", getLLVMStyleWithColumns(13)));
Manuel Klimek060143e2013-01-02 18:33:23 +0000669}
670
Manuel Klimekd4397b92013-01-04 23:34:14 +0000671TEST_F(FormatTest, LayoutSingleHash) {
672 EXPECT_EQ("#\na;", format("#\na;"));
673}
674
675TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
676 EXPECT_EQ("#define A \\\n"
677 " c; \\\n"
678 " e;\n"
679 "f;", format("#define A c; e;\n"
680 "f;", getLLVMStyleWithColumns(14)));
681}
682
683TEST_F(FormatTest, LayoutRemainingTokens) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000684 EXPECT_EQ("{}", format("{}"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000685}
686
687TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000688 EXPECT_EQ("# define A\\\n b;",
689 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000690}
691
692TEST_F(FormatTest, MacroDefinitionInsideStatement) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000693 EXPECT_EQ("int x,\n"
694 "#define A\n"
695 " y;", format("int x,\n#define A\ny;"));
Manuel Klimekd4397b92013-01-04 23:34:14 +0000696}
697
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000698TEST_F(FormatTest, HashInMacroDefinition) {
699 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
700 verifyFormat("#define A \\\n"
701 " { \\\n"
702 " f(#c);\\\n"
703 " }", getLLVMStyleWithColumns(11));
Daniel Jasper765561f2013-01-08 16:17:54 +0000704
705 verifyFormat("#define A(X) \\\n"
706 " void function##X()", getLLVMStyleWithColumns(22));
707
708 verifyFormat("#define A(a, b, c) \\\n"
709 " void a##b##c()", getLLVMStyleWithColumns(22));
710
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000711 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
Manuel Klimek6f8424b2013-01-05 21:34:55 +0000712}
713
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000714TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
715 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
716}
717
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000718TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000719 verifyFormat("{\n { a #c; }\n}");
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000720}
721
Manuel Klimeka5342db2013-01-06 20:07:31 +0000722TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
723 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
724 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
725 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
726 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
727}
Manuel Klimekd4397b92013-01-04 23:34:14 +0000728
Manuel Klimek95419382013-01-07 07:56:50 +0000729TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
Daniel Jaspercd162382013-01-07 13:26:07 +0000730 EXPECT_EQ(
731 "#define A \\\n int i; \\\n int j;",
732 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
Manuel Klimek95419382013-01-07 07:56:50 +0000733}
734
Manuel Klimekd544c572013-01-07 09:24:17 +0000735TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
736 verifyFormat("#define A \\\n"
737 " int v( \\\n"
738 " a); \\\n"
739 " int i;", getLLVMStyleWithColumns(11));
740}
741
Manuel Klimeka080a182013-01-02 16:30:12 +0000742TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000743 EXPECT_EQ(
744 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
745 " \\\n"
746 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
747 "\n"
748 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
749 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
750 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
751 "\\\n"
752 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
753 " \n"
754 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
755 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
Manuel Klimeka080a182013-01-02 16:30:12 +0000756}
757
Manuel Klimek526ed112013-01-09 15:25:02 +0000758TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
759 EXPECT_EQ("int\n"
760 "#define A\n"
761 " a;",
762 format("int\n#define A\na;"));
763 verifyFormat(
764 "functionCallTo(someOtherFunction(\n"
765 " withSomeParameters, whichInSequence,\n"
766 " areLongerThanALine(andAnotherCall,\n"
Daniel Jaspere0b15ea2013-01-14 15:40:57 +0000767 "#define A B\n"
Manuel Klimek526ed112013-01-09 15:25:02 +0000768 " withMoreParamters,\n"
769 " whichStronglyInfluenceTheLayout),\n"
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000770 " andMoreParameters), trailing);", getLLVMStyleWithColumns(69));
Manuel Klimek526ed112013-01-09 15:25:02 +0000771}
772
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000773TEST_F(FormatTest, LayoutBlockInsideParens) {
774 EXPECT_EQ("functionCall({\n"
775 " int i;\n"
776 "});", format(" functionCall ( {int i;} );"));
777}
778
779TEST_F(FormatTest, LayoutBlockInsideStatement) {
Manuel Klimek517e8942013-01-11 17:54:10 +0000780 EXPECT_EQ("SOME_MACRO { int i; }\n"
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000781 "int i;", format(" SOME_MACRO {int i;} int i;"));
782}
783
784TEST_F(FormatTest, LayoutNestedBlocks) {
785 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
786 " struct s {\n"
787 " int i;\n"
788 " };\n"
789 " s kBitsToOs[] = { { 10 } };\n"
790 " for (int i = 0; i < 10; ++i)\n"
791 " return;\n"
792 "}");
793}
794
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000795TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
796 EXPECT_EQ("{}", format("{}"));
797}
798
Alexander Kornienko15757312012-12-06 18:03:27 +0000799//===----------------------------------------------------------------------===//
800// Line break tests.
801//===----------------------------------------------------------------------===//
802
803TEST_F(FormatTest, FormatsFunctionDefinition) {
804 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
805 " int h, int j, int f,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000806 " int c, int ddddddddddddd) {}");
Alexander Kornienko15757312012-12-06 18:03:27 +0000807}
808
809TEST_F(FormatTest, FormatsAwesomeMethodCall) {
810 verifyFormat(
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000811 "SomeLongMethodName(SomeReallyLongMethod(\n"
812 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
813 " SecondLongCall(parameter));");
Alexander Kornienko15757312012-12-06 18:03:27 +0000814}
815
Daniel Jasper1321eb52012-12-18 21:05:13 +0000816TEST_F(FormatTest, ConstructorInitializers) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000817 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000818 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
819 getLLVMStyleWithColumns(45));
820 verifyFormat("Constructor()\n"
821 " : Inttializer(FitsOnTheLine) {}",
822 getLLVMStyleWithColumns(44));
Daniel Jasper1321eb52012-12-18 21:05:13 +0000823
824 verifyFormat(
825 "SomeClass::Constructor()\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000826 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000827
828 verifyFormat(
829 "SomeClass::Constructor()\n"
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000830 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
831 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
832 verifyGoogleFormat(
833 "SomeClass::Constructor()\n"
834 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
835 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
836 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper16618242013-01-16 17:00:50 +0000837 verifyGoogleFormat(
838 "SomeClass::Constructor()\n"
839 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
840 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
841 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000842
843 verifyFormat(
844 "SomeClass::Constructor()\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000845 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000846 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000847
848 verifyFormat("Constructor()\n"
849 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
850 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
851 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000852 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000853
854 // Here a line could be saved by splitting the second initializer onto two
855 // lines, but that is not desireable.
856 verifyFormat("Constructor()\n"
857 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
858 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000859 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper1321eb52012-12-18 21:05:13 +0000860
861 verifyGoogleFormat("MyClass::MyClass(int var)\n"
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000862 " : some_var_(var), // 4 space indent\n"
Daniel Jasper1321eb52012-12-18 21:05:13 +0000863 " some_other_var_(var + 1) { // lined up\n"
864 "}");
Daniel Jasperb3123142013-01-12 07:36:22 +0000865
866 // This test takes VERY long when memoization is broken.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000867 std::string input = "Constructor()\n"
868 " : aaaa(a,\n";
869 for (unsigned i = 0, e = 80; i != e; ++i) {
870 input += " a,\n";
871 }
872 input += " a) {}";
873 verifyGoogleFormat(input);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000874}
875
Alexander Kornienko15757312012-12-06 18:03:27 +0000876TEST_F(FormatTest, BreaksAsHighAsPossible) {
877 verifyFormat(
878 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
879 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
880 " f();");
881}
882
Daniel Jasperbac016b2012-12-03 18:12:45 +0000883TEST_F(FormatTest, BreaksDesireably) {
884 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
885 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000886 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000887
888 verifyFormat(
889 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000891
892 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
894 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
Daniel Jaspera88bb452012-12-04 10:50:12 +0000895
896 verifyFormat(
897 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
899 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
900 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
Daniel Jasper33182dd2012-12-05 14:57:28 +0000901
902 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
903 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
904
Daniel Jasper723f0302013-01-02 14:40:02 +0000905 verifyFormat(
906 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
908
Daniel Jasper33182dd2012-12-05 14:57:28 +0000909 // This test case breaks on an incorrect memoization, i.e. an optimization not
910 // taking into account the StopAt value.
911 verifyFormat(
912 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jaspercf225b62012-12-24 13:43:52 +0000913 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
914 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
915 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000916
Daniel Jaspercd162382013-01-07 13:26:07 +0000917 verifyFormat("{\n {\n {\n"
918 " Annotation.SpaceRequiredBefore =\n"
919 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
920 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
921 " }\n }\n}");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000922}
923
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000924TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
925 verifyGoogleFormat(
926 "aaaaaaaa(aaaaaaaaaaaaa,\n"
927 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
929 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
930 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
931 verifyGoogleFormat(
932 "aaaaaaaaaaaaaaa(aaaaaaaaa,\n"
933 " aaaaaaaaa,\n"
934 " aaaaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();");
935 verifyGoogleFormat(
936 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
937 " ddddddddddddddddddddddddddddd),\n"
938 " test);");
939
940 verifyGoogleFormat(
941 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
942 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
943 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
944 verifyGoogleFormat("a(\"a\"\n"
945 " \"a\",\n"
946 " a);");
947}
948
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000949TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
950 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
951 " GUARDED_BY(aaaaaaaaaaaaa);");
Daniel Jasper60ca75d2013-01-17 13:31:52 +0000952 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
953 " GUARDED_BY(aaaaaaaaaaaaa);");
954 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
955 " GUARDED_BY(aaaaaaaaaaaaa) {}");
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000956}
957
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000958TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
959 verifyFormat(
960 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000961 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000962 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000963 " ccccccccccccccccccccccccc) {}");
Daniel Jaspercd162382013-01-07 13:26:07 +0000964 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000965 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000966 verifyFormat(
967 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000968 " ccccccccccccccccccccccccc) {}");
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000969}
970
Daniel Jasper9cda8002013-01-07 13:08:40 +0000971TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
972 verifyFormat(
973 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
974 " SI->getAlignment(),\n"
975 " SI->getPointerAddressSpaceee());\n");
976 verifyFormat(
977 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
978 " Line.Tokens.front().Tok.getLocation(),\n"
979 " Line.Tokens.back().Tok.getLocation());");
980}
981
Daniel Jaspercf225b62012-12-24 13:43:52 +0000982TEST_F(FormatTest, AlignsAfterAssignments) {
983 verifyFormat(
984 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000985 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000986 verifyFormat(
987 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000988 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000989 verifyFormat(
990 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000991 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000992 verifyFormat(
993 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
Daniel Jasper9cda8002013-01-07 13:08:40 +0000994 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000995 verifyFormat(
Daniel Jasper9cda8002013-01-07 13:08:40 +0000996 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
997 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
998 " aaaaaaaaaaaaaaaaaaaaaaaa;");
Daniel Jaspercf225b62012-12-24 13:43:52 +0000999}
1000
1001TEST_F(FormatTest, AlignsAfterReturn) {
1002 verifyFormat(
1003 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1004 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
1005 verifyFormat(
1006 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1007 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
1008}
1009
Daniel Jasper9c837d02013-01-09 07:06:56 +00001010TEST_F(FormatTest, BreaksConditionalExpressions) {
1011 verifyFormat(
1012 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1013 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1015 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
1016 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasper1f2b0782013-01-16 16:23:19 +00001017 verifyFormat(
1018 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
1019 " aaaaaaaaaaaaa);");
Daniel Jasper9c837d02013-01-09 07:06:56 +00001020}
1021
Nico Weber7d37b8b2013-01-12 01:28:06 +00001022TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1023 verifyFormat("arr[foo ? bar : baz];");
1024 verifyFormat("f()[foo ? bar : baz];");
1025 verifyFormat("(a + b)[foo ? bar : baz];");
1026 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1027}
1028
Daniel Jasperbac016b2012-12-03 18:12:45 +00001029TEST_F(FormatTest, AlignsStringLiterals) {
1030 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1031 " \"short literal\");");
1032 verifyFormat(
1033 "looooooooooooooooooooooooongFunction(\n"
1034 " \"short literal\"\n"
1035 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1036}
1037
Alexander Kornienko15757312012-12-06 18:03:27 +00001038TEST_F(FormatTest, AlignsPipes) {
1039 verifyFormat(
1040 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1041 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1042 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1043 verifyFormat(
1044 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1045 " << aaaaaaaaaaaaaaaaaaaa;");
1046 verifyFormat(
1047 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1048 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1049 verifyFormat(
1050 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1051 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1052 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1053 verifyFormat(
1054 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1055 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1056 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1057}
1058
Daniel Jasperbac016b2012-12-03 18:12:45 +00001059TEST_F(FormatTest, UnderstandsEquals) {
1060 verifyFormat(
1061 "aaaaaaaaaaaaaaaaa =\n"
1062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1063 verifyFormat(
1064 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001066 verifyFormat(
1067 "if (a) {\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001068 " f();\n"
Daniel Jasperbac016b2012-12-03 18:12:45 +00001069 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001071
Daniel Jasper9cda8002013-01-07 13:08:40 +00001072 verifyFormat(
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001073 // FIXME: Does an expression like this ever make sense? If yes, fix.
Daniel Jasper9cda8002013-01-07 13:08:40 +00001074 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001075 " 10000000) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001076}
1077
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001078TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
Daniel Jasper1321eb52012-12-18 21:05:13 +00001079 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1080 " .looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001081
Daniel Jasper1321eb52012-12-18 21:05:13 +00001082 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1083 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001084
1085 verifyFormat(
1086 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1087 " Parameter2);");
1088
1089 verifyFormat(
1090 "ShortObject->shortFunction(\n"
1091 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1092 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1093
1094 verifyFormat("loooooooooooooongFunction(\n"
1095 " LoooooooooooooongObject->looooooooooooooooongFunction());");
1096
1097 verifyFormat(
1098 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1099 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1100
Daniel Jasper46a46a22013-01-07 07:13:20 +00001101 // Here, it is not necessary to wrap at "." or "->".
Daniel Jasper1321eb52012-12-18 21:05:13 +00001102 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001103 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
Daniel Jasper46a46a22013-01-07 07:13:20 +00001104 verifyFormat(
1105 "aaaaaaaaaaa->aaaaaaaaa(\n"
1106 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1107 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
Daniel Jasper9a0b4942012-12-17 14:34:14 +00001108}
1109
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001110TEST_F(FormatTest, WrapsTemplateDeclarations) {
1111 verifyFormat("template <typename T>\n"
1112 "virtual void loooooooooooongFunction(int Param1, int Param2);");
1113 verifyFormat(
1114 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1115 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
1116 verifyFormat(
1117 "template <typename T>\n"
1118 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1119 " int Paaaaaaaaaaaaaaaaaaaaram2);");
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001120 verifyFormat(
1121 "template <typename T>\n"
1122 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1123 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1124 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperd64f7382013-01-09 09:50:48 +00001125 verifyFormat("template <typename T>\n"
1126 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1127 " int aaaaaaaaaaaaaaaaa);");
Daniel Jasper3fc0bb72013-01-09 10:40:23 +00001128 verifyFormat(
1129 "template <typename T1, typename T2 = char, typename T3 = char,\n"
1130 " typename T4 = char>\n"
1131 "void f();");
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001132}
1133
Daniel Jasperbac016b2012-12-03 18:12:45 +00001134TEST_F(FormatTest, UnderstandsTemplateParameters) {
1135 verifyFormat("A<int> a;");
1136 verifyFormat("A<A<A<int> > > a;");
1137 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1138 verifyFormat("bool x = a < 1 || 2 > a;");
1139 verifyFormat("bool x = 5 < f<int>();");
1140 verifyFormat("bool x = f<int>() > 5;");
1141 verifyFormat("bool x = 5 < a<int>::x;");
1142 verifyFormat("bool x = a < 4 ? a > 2 : false;");
1143 verifyFormat("bool x = f() ? a < 2 : a > 2;");
1144
1145 verifyGoogleFormat("A<A<int>> a;");
1146 verifyGoogleFormat("A<A<A<int>>> a;");
1147 verifyGoogleFormat("A<A<A<A<int>>>> a;");
1148
1149 verifyFormat("test >> a >> b;");
1150 verifyFormat("test << a >> b;");
1151
1152 verifyFormat("f<int>();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001153 verifyFormat("template <typename T> void f() {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001154}
1155
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001156TEST_F(FormatTest, UnderstandsUnaryOperators) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001157 verifyFormat("int a = -2;");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001158 verifyFormat("f(-1, -2, -3);");
1159 verifyFormat("a[-1] = 5;");
1160 verifyFormat("int a = 5 + -2;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001161 verifyFormat("if (i == -1) {}");
1162 verifyFormat("if (i != -1) {}");
1163 verifyFormat("if (i > -1) {}");
1164 verifyFormat("if (i < -1) {}");
Daniel Jasperd56a7372012-12-06 13:16:39 +00001165 verifyFormat("++(a->f());");
1166 verifyFormat("--(a->f());");
Daniel Jasper4abbb532013-01-14 12:18:19 +00001167 verifyFormat("(a->f())++;");
1168 verifyFormat("a[42]++;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001169 verifyFormat("if (!(a->f())) {}");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001170
1171 verifyFormat("a-- > b;");
1172 verifyFormat("b ? -a : c;");
1173 verifyFormat("n * sizeof char16;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001174 verifyFormat("n * alignof char16;");
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001175 verifyFormat("sizeof(char);");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001176 verifyFormat("alignof(char);");
Daniel Jasper1f0754b2013-01-02 15:26:16 +00001177
1178 verifyFormat("return -1;");
1179 verifyFormat("switch (a) {\n"
1180 "case -1:\n"
1181 " break;\n"
1182 "}");
Nico Webercc191d12013-01-12 05:41:23 +00001183
1184 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1185 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001186
1187 verifyFormat("int a = /* confusing comment */ -1;");
1188 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1189 verifyFormat("int a = i /* confusing comment */++;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001190}
1191
1192TEST_F(FormatTest, UndestandsOverloadedOperators) {
Daniel Jasperf6aef6a2012-12-24 10:56:04 +00001193 verifyFormat("bool operator<();");
1194 verifyFormat("bool operator>();");
1195 verifyFormat("bool operator=();");
1196 verifyFormat("bool operator==();");
1197 verifyFormat("bool operator!=();");
1198 verifyFormat("int operator+();");
1199 verifyFormat("int operator++();");
1200 verifyFormat("bool operator();");
1201 verifyFormat("bool operator()();");
1202 verifyFormat("bool operator[]();");
1203 verifyFormat("operator bool();");
1204 verifyFormat("operator SomeType<int>();");
1205 verifyFormat("void *operator new(std::size_t size);");
1206 verifyFormat("void *operator new[](std::size_t size);");
1207 verifyFormat("void operator delete(void *ptr);");
1208 verifyFormat("void operator delete[](void *ptr);");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001209}
1210
Daniel Jasper088dab52013-01-11 16:09:04 +00001211TEST_F(FormatTest, UnderstandsNewAndDelete) {
1212 verifyFormat("A *a = new A;");
1213 verifyFormat("A *a = new (placement) A;");
1214 verifyFormat("delete a;");
1215 verifyFormat("delete (A *)a;");
1216}
1217
Daniel Jasper5d334402013-01-02 08:57:10 +00001218TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001219 verifyFormat("int *f(int *a) {}");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001220 verifyFormat("f(a, *a);");
1221 verifyFormat("f(*a);");
1222 verifyFormat("int a = b * 10;");
1223 verifyFormat("int a = 10 * b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001224 verifyFormat("int a = b * c;");
Daniel Jasper33182dd2012-12-05 14:57:28 +00001225 verifyFormat("int a += b * c;");
1226 verifyFormat("int a -= b * c;");
1227 verifyFormat("int a *= b * c;");
1228 verifyFormat("int a /= b * c;");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001229 verifyFormat("int a = *b;");
Daniel Jasper112fb272012-12-05 07:51:39 +00001230 verifyFormat("int a = *b * c;");
1231 verifyFormat("int a = b * *c;");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001232 verifyFormat("int main(int argc, char **argv) {}");
Nico Weber00d5a042012-12-23 01:07:46 +00001233 verifyFormat("return 10 * b;");
1234 verifyFormat("return *b * *c;");
1235 verifyFormat("return a & ~b;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001236 verifyFormat("f(b ? *c : *d);");
1237 verifyFormat("int a = b ? *c : *d;");
Daniel Jasperba3d3072013-01-02 17:21:36 +00001238 verifyFormat("*b = a;");
1239 verifyFormat("a * ~b;");
1240 verifyFormat("a * !b;");
1241 verifyFormat("a * +b;");
1242 verifyFormat("a * -b;");
1243 verifyFormat("a * ++b;");
1244 verifyFormat("a * --b;");
Nico Weber2355ceb2013-01-12 05:47:16 +00001245 verifyFormat("a[4] * b;");
1246 verifyFormat("f() * b;");
Nico Weber5e9f91c2013-01-12 05:50:48 +00001247 verifyFormat("a * [self dostuff];");
1248 verifyFormat("a * (a + b);");
1249 verifyFormat("(a *)(a + b);");
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001250 verifyFormat("int *pa = (int *)&a;");
Daniel Jasperc74e2792012-12-07 09:52:15 +00001251
Daniel Jasper9bb0d282013-01-04 20:46:38 +00001252 verifyFormat("InvalidRegions[*R] = 0;");
1253
Daniel Jasper8b39c662012-12-10 18:59:13 +00001254 verifyFormat("A<int *> a;");
1255 verifyFormat("A<int **> a;");
1256 verifyFormat("A<int *, int *> a;");
1257 verifyFormat("A<int **, int **> a;");
1258
Daniel Jasper2db356d2013-01-08 20:03:18 +00001259 verifyFormat(
1260 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1262
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001263 verifyGoogleFormat("int main(int argc, char** argv) {}");
Daniel Jasper8b39c662012-12-10 18:59:13 +00001264 verifyGoogleFormat("A<int*> a;");
1265 verifyGoogleFormat("A<int**> a;");
1266 verifyGoogleFormat("A<int*, int*> a;");
1267 verifyGoogleFormat("A<int**, int**> a;");
Daniel Jasper5d334402013-01-02 08:57:10 +00001268 verifyGoogleFormat("f(b ? *c : *d);");
1269 verifyGoogleFormat("int a = b ? *c : *d;");
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001270 verifyGoogleFormat("Type* t = **x;");
1271 verifyGoogleFormat("Type* t = *++*x;");
1272 verifyGoogleFormat("*++*x;");
1273 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1274 verifyGoogleFormat("Type* t = x++ * y;");
Manuel Klimek1b6f4bd2013-01-14 10:58:01 +00001275
1276 verifyFormat("a = *(x + y);");
1277 verifyFormat("a = &(x + y);");
1278 verifyFormat("*(x + y).call();");
1279 verifyFormat("&(x + y)->call();");
1280 verifyFormat("&(*I).first");
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001281
1282 verifyFormat("f(b * /* confusing comment */ ++c);");
1283 verifyFormat(
1284 "int *MyValues = {\n"
1285 " *A, // Operator detection might be confused by the '{'\n"
1286 " *BB // Operator detection might be confused by previous comment\n"
1287 "};");
Nico Weber5096a442013-01-17 17:17:19 +00001288
1289 verifyFormat("if (int *a = &b)");
1290 verifyFormat("if (int &a = *b)");
1291 verifyFormat("if (a & b[i])");
1292 verifyFormat("if (a::b::c::d & b[i])");
1293 verifyFormat("if (*b[i])");
1294 verifyFormat("if (int *a = (&b))");
1295 verifyFormat("while (int *a = &b)");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001296}
1297
Daniel Jasper4981bd02013-01-13 08:01:36 +00001298TEST_F(FormatTest, FormatsCasts) {
1299 verifyFormat("Type *A = static_cast<Type *>(P);");
1300 verifyFormat("Type *A = (Type *)P;");
1301 verifyFormat("Type *A = (vector<Type *, int *>)P;");
1302 verifyFormat("int a = (int)(2.0f);");
1303
1304 // FIXME: These also need to be identified.
1305 verifyFormat("int a = (int) 2.0f;");
1306 verifyFormat("int a = (int) * b;");
1307
1308 // These are not casts.
1309 verifyFormat("void f(int *) {}");
1310 verifyFormat("void f(int *);");
1311 verifyFormat("void f(int *) = 0;");
1312 verifyFormat("void f(SmallVector<int>) {}");
1313 verifyFormat("void f(SmallVector<int>);");
1314 verifyFormat("void f(SmallVector<int>) = 0;");
1315}
1316
Daniel Jasper46ef8522013-01-10 13:08:12 +00001317TEST_F(FormatTest, FormatsFunctionTypes) {
1318 // FIXME: Determine the cases that need a space after the return type and fix.
1319 verifyFormat("A<bool()> a;");
1320 verifyFormat("A<SomeType()> a;");
1321 verifyFormat("A<void(*)(int, std::string)> a;");
1322
1323 verifyFormat("int(*func)(void *);");
1324}
1325
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001326TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
Daniel Jaspercd162382013-01-07 13:26:07 +00001327 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001328 " int LoooooooooooooooongParam2) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001329 verifyFormat(
1330 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1331 " SourceLocation L, IdentifierIn *II,\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001332 " Type *T) {}");
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001333}
1334
Daniel Jasper3b5943f2012-12-06 09:56:08 +00001335TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1336 verifyFormat("(a)->b();");
1337 verifyFormat("--a;");
1338}
1339
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001340TEST_F(FormatTest, HandlesIncludeDirectives) {
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001341 verifyFormat("#include <string>\n"
1342 "#include <a/b/c.h>\n"
1343 "#include \"a/b/string\"\n"
1344 "#include \"string.h\"\n"
1345 "#include \"string.h\"\n"
Manuel Klimek407a31a2013-01-15 15:50:27 +00001346 "#include <a-a>\n"
1347 "#include < path with space >\n");
Nico Weberb23ae0c2012-12-21 18:21:56 +00001348
Daniel Jasper8134e1e2013-01-13 08:12:18 +00001349 verifyFormat("#import <string>");
1350 verifyFormat("#import <a/b/c.h>");
1351 verifyFormat("#import \"a/b/string\"");
1352 verifyFormat("#import \"string.h\"");
1353 verifyFormat("#import \"string.h\"");
Daniel Jasper8822d3a2012-12-04 13:02:32 +00001354}
1355
Alexander Kornienko15757312012-12-06 18:03:27 +00001356//===----------------------------------------------------------------------===//
1357// Error recovery tests.
1358//===----------------------------------------------------------------------===//
1359
Daniel Jasper700e7102013-01-10 09:26:47 +00001360TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
Alexander Kornienko51c23fa2013-01-16 11:45:16 +00001361 verifyFormat("void f() { return; }\n42");
1362 verifyFormat("void f() {\n"
1363 " if (0)\n"
1364 " return;\n"
1365 "}\n"
1366 "42");
Alexander Kornienkod8818752013-01-16 11:43:46 +00001367 verifyFormat("void f() { return }\n42");
1368 verifyFormat("void f() {\n"
1369 " if (0)\n"
1370 " return\n"
1371 "}\n"
1372 "42");
1373}
1374
1375TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1376 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
1377 EXPECT_EQ("void f() {\n"
1378 " if (a)\n"
1379 " return\n"
1380 "}", format("void f ( ) { if ( a ) return }"));
1381 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
1382 EXPECT_EQ("namespace N {\n"
1383 "void f() {}\n"
1384 "void g()\n"
1385 "}", format("namespace N { void f( ) { } void g( ) }"));
Daniel Jasper700e7102013-01-10 09:26:47 +00001386}
1387
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001388TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1389 verifyFormat("int aaaaaaaa =\n"
1390 " // Overly long comment\n"
1391 " b;", getLLVMStyleWithColumns(20));
1392 verifyFormat("function(\n"
1393 " ShortArgument,\n"
1394 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1395}
1396
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001397TEST_F(FormatTest, IncorrectAccessSpecifier) {
1398 verifyFormat("public:");
1399 verifyFormat("class A {\n"
1400 "public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001401 " void f() {}\n"
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001402 "};");
1403 verifyFormat("public\n"
1404 "int qwerty;");
1405 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001406 "B {}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001407 verifyFormat("public\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001408 "{}");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001409 verifyFormat("public\n"
Manuel Klimek517e8942013-01-11 17:54:10 +00001410 "B { int x; }");
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001411}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001412
Alexander Kornienko393b0082012-12-04 15:40:36 +00001413TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1414 verifyFormat("{");
1415}
1416
1417TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001418 verifyFormat("do {}");
1419 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001420 "f();");
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001421 verifyFormat("do {}\n"
Alexander Kornienko393b0082012-12-04 15:40:36 +00001422 "wheeee(fun);");
1423 verifyFormat("do {\n"
1424 " f();\n"
Manuel Klimekde768542013-01-07 18:10:23 +00001425 "}");
Alexander Kornienko393b0082012-12-04 15:40:36 +00001426}
1427
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001428TEST_F(FormatTest, IncorrectCodeMissingParens) {
Manuel Klimekd4658432013-01-11 18:28:36 +00001429 verifyFormat("if {\n foo;\n foo();\n}");
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001430 verifyFormat("switch {\n foo;\n foo();\n}");
1431 verifyFormat("for {\n foo;\n foo();\n}");
1432 verifyFormat("while {\n foo;\n foo();\n}");
1433 verifyFormat("do {\n foo;\n foo();\n} while;");
Manuel Klimekd4658432013-01-11 18:28:36 +00001434}
1435
Daniel Jasper1f42f112013-01-04 18:52:56 +00001436TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1437 verifyFormat("namespace {\n"
Manuel Klimekd4397b92013-01-04 23:34:14 +00001438 "class Foo { Foo ( }; } // comment");
Daniel Jasper1f42f112013-01-04 18:52:56 +00001439}
1440
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001441TEST_F(FormatTest, IncorrectCodeErrorDetection) {
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001442 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
1443 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
1444 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
1445 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001446
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001447 EXPECT_EQ("{\n"
1448 " {\n"
1449 " breakme(\n"
1450 " qwe);\n"
1451 "}\n", format("{\n"
1452 " {\n"
1453 " breakme(qwe);\n"
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001454 "}\n", getLLVMStyleWithColumns(10)));
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001455}
1456
Manuel Klimek2851c162013-01-10 14:36:46 +00001457TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
1458 verifyFormat(
1459 "int x = {\n"
1460 " avariable,\n"
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001461 " b(alongervariable)\n"
1462 "};", getLLVMStyleWithColumns(25));
Manuel Klimek2851c162013-01-10 14:36:46 +00001463}
1464
1465TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
1466 verifyFormat(
1467 "Aaa({\n"
1468 " int i;\n"
1469 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1470 " ccccccccccccccccc));");
1471}
1472
Manuel Klimek517e8942013-01-11 17:54:10 +00001473TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
1474 verifyFormat("void f() { return 42; }");
1475 verifyFormat("void f() {\n"
1476 " // Comment\n"
1477 "}");
1478 verifyFormat("{\n"
1479 "#error {\n"
1480 " int a;\n"
1481 "}");
1482 verifyFormat("{\n"
1483 " int a;\n"
1484 "#error {\n"
1485 "}");
1486}
1487
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001488TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
1489 // Elaborate type variable declarations.
Manuel Klimek606e07e2013-01-11 18:13:04 +00001490 verifyFormat("struct foo a = { bar };\nint n;");
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001491 verifyFormat("class foo a = { bar };\nint n;");
1492 verifyFormat("union foo a = { bar };\nint n;");
1493
1494 // Elaborate types inside function definitions.
1495 verifyFormat("struct foo f() {}\nint n;");
1496 verifyFormat("class foo f() {}\nint n;");
1497 verifyFormat("union foo f() {}\nint n;");
1498
1499 // Templates.
1500 verifyFormat("template <class X> void f() {}\nint n;");
1501 verifyFormat("template <struct X> void f() {}\nint n;");
1502 verifyFormat("template <union X> void f() {}\nint n;");
1503
1504 // Actual definitions...
1505 verifyFormat("struct {} n;");
1506 verifyFormat("template <template <class T, class Y>, class Z > class X {} n;");
1507 verifyFormat("union Z {\n int n;\n} x;");
1508 verifyFormat("class MACRO Z {} n;");
1509 verifyFormat("class MACRO(X) Z {} n;");
1510 verifyFormat("class __attribute__(X) Z {} n;");
1511 verifyFormat("class __declspec(X) Z {} n;");
1512
1513 // Elaborate types where incorrectly parsing the structural element would
1514 // break the indent.
1515 verifyFormat("if (true)\n"
1516 " class X x;\n"
1517 "else\n"
1518 " f();\n");
Manuel Klimek606e07e2013-01-11 18:13:04 +00001519}
1520
Manuel Klimek407a31a2013-01-15 15:50:27 +00001521TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
1522 verifyFormat("#error Leave all white!!!!! space* alone!\n");
1523 verifyFormat("#warning Leave all white!!!!! space* alone!\n");
1524 EXPECT_EQ("#error 1", format(" # error 1"));
1525 EXPECT_EQ("#warning 1", format(" # warning 1"));
1526}
1527
Manuel Klimek517e8942013-01-11 17:54:10 +00001528// FIXME: This breaks the order of the unwrapped lines:
1529// TEST_F(FormatTest, OrderUnwrappedLines) {
1530// verifyFormat("{\n"
1531// " bool a; //\n"
1532// "#error {\n"
1533// " int a;\n"
1534// "}");
1535// }
1536
Nico Webercf4a79c2013-01-08 17:56:31 +00001537//===----------------------------------------------------------------------===//
1538// Objective-C tests.
1539//===----------------------------------------------------------------------===//
1540
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001541TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1542 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1543 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1544 format("-(NSUInteger)indexOfObject:(id)anObject;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001545 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001546 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1547 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1548 format("-(NSInteger)Method3:(id)anObject;"));
1549 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1550 format("-(NSInteger)Method4:(id)anObject;"));
1551 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1552 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1553 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1554 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
Daniel Jaspercd162382013-01-07 13:26:07 +00001555 EXPECT_EQ(
1556 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1557 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
Fariborz Jahanian9b3f02c2012-12-21 22:51:18 +00001558
1559 // Very long objectiveC method declaration.
Daniel Jaspercd162382013-01-07 13:26:07 +00001560 EXPECT_EQ(
1561 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n "
1562 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n "
1563 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n "
1564 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n "
1565 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n "
1566 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1567 format(
1568 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1569 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1570 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 "
1571 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 "
1572 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 "
1573 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;"));
Nico Webere0fd4292013-01-10 20:18:33 +00001574
1575 verifyFormat("- (int)sum:(vector<int>)numbers;");
Nico Weberaab60052013-01-17 06:14:50 +00001576 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
Nico Webere0fd4292013-01-10 20:18:33 +00001577 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
1578 // protocol lists (but not for template classes):
1579 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
Nico Webercd52bda2013-01-10 23:11:41 +00001580
1581 verifyFormat("- (int(*)())foo:(int(*)())f;");
Nico Weberaab60052013-01-17 06:14:50 +00001582 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
Nico Webercd52bda2013-01-10 23:11:41 +00001583
1584 // If there's no return type (very rare in practice!), LLVM and Google style
1585 // agree.
1586 verifyFormat("- foo:(int)f;");
1587 verifyGoogleFormat("- foo:(int)foo;");
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001588}
1589
Daniel Jasper886568d2013-01-09 08:36:49 +00001590TEST_F(FormatTest, FormatObjCBlocks) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001591 verifyFormat("int (^Block)(int, int);");
1592 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
Daniel Jasper886568d2013-01-09 08:36:49 +00001593}
1594
Nico Weber27d13672013-01-09 20:25:35 +00001595TEST_F(FormatTest, FormatObjCInterface) {
1596 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
Nico Weber5f500df2013-01-10 20:12:55 +00001597 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Nico Weber27d13672013-01-09 20:25:35 +00001598 "@public\n"
1599 " int field1;\n"
1600 "@protected\n"
1601 " int field2;\n"
1602 "@private\n"
1603 " int field3;\n"
1604 "@package\n"
1605 " int field4;\n"
1606 "}\n"
1607 "+ (id)init;\n"
1608 "@end");
1609
Nico Weber27d13672013-01-09 20:25:35 +00001610 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1611 " @public\n"
1612 " int field1;\n"
1613 " @protected\n"
1614 " int field2;\n"
1615 " @private\n"
1616 " int field3;\n"
1617 " @package\n"
1618 " int field4;\n"
1619 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001620 "+ (id)init;\n"
Nico Weber27d13672013-01-09 20:25:35 +00001621 "@end");
1622
1623 verifyFormat("@interface Foo\n"
1624 "+ (id)init;\n"
1625 "// Look, a comment!\n"
1626 "- (int)answerWith:(int)i;\n"
1627 "@end");
1628
1629 verifyFormat("@interface Foo\n"
Nico Weber049c4472013-01-09 21:42:32 +00001630 "@end\n"
1631 "@interface Bar\n"
Nico Weber27d13672013-01-09 20:25:35 +00001632 "@end");
1633
1634 verifyFormat("@interface Foo : Bar\n"
1635 "+ (id)init;\n"
1636 "@end");
1637
Nico Weber5f500df2013-01-10 20:12:55 +00001638 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001639 "+ (id)init;\n"
1640 "@end");
1641
Nico Weber5f500df2013-01-10 20:12:55 +00001642 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001643 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001644 "@end");
1645
Nico Webered91bba2013-01-10 19:19:14 +00001646 verifyFormat("@interface Foo (HackStuff)\n"
Nico Weber27d13672013-01-09 20:25:35 +00001647 "+ (id)init;\n"
1648 "@end");
1649
Nico Webered91bba2013-01-10 19:19:14 +00001650 verifyFormat("@interface Foo ()\n"
Nico Weber27d13672013-01-09 20:25:35 +00001651 "+ (id)init;\n"
1652 "@end");
1653
Nico Weber5f500df2013-01-10 20:12:55 +00001654 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Nico Weber27d13672013-01-09 20:25:35 +00001655 "+ (id)init;\n"
1656 "@end");
1657
Nico Weber5f500df2013-01-10 20:12:55 +00001658 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001659 "+ (id)init;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001660 "@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 : Bar {\n"
1669 " int _i;\n"
1670 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001671 "+ (id)init;\n"
1672 "@end");
1673
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001674 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
1675 " int _i;\n"
1676 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001677 "+ (id)init;\n"
1678 "@end");
1679
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001680 verifyFormat("@interface Foo (HackStuff) {\n"
1681 " int _i;\n"
1682 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001683 "+ (id)init;\n"
1684 "@end");
1685
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001686 verifyFormat("@interface Foo () {\n"
1687 " int _i;\n"
1688 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001689 "+ (id)init;\n"
1690 "@end");
1691
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001692 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
1693 " int _i;\n"
1694 "}\n"
Nico Weber27d13672013-01-09 20:25:35 +00001695 "+ (id)init;\n"
1696 "@end");
1697}
1698
Nico Weber50767d82013-01-09 23:25:37 +00001699TEST_F(FormatTest, FormatObjCImplementation) {
1700 verifyFormat("@implementation Foo : NSObject {\n"
1701 "@public\n"
1702 " int field1;\n"
1703 "@protected\n"
1704 " int field2;\n"
1705 "@private\n"
1706 " int field3;\n"
1707 "@package\n"
1708 " int field4;\n"
1709 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001710 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001711 "@end");
1712
1713 verifyGoogleFormat("@implementation Foo : NSObject {\n"
1714 " @public\n"
1715 " int field1;\n"
1716 " @protected\n"
1717 " int field2;\n"
1718 " @private\n"
1719 " int field3;\n"
1720 " @package\n"
1721 " int field4;\n"
1722 "}\n"
Nico Weberaab60052013-01-17 06:14:50 +00001723 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001724 "@end");
1725
1726 verifyFormat("@implementation Foo\n"
1727 "+ (id)init {\n"
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001728 " if (true)\n"
1729 " return nil;\n"
Nico Weber50767d82013-01-09 23:25:37 +00001730 "}\n"
1731 "// Look, a comment!\n"
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001732 "- (int)answerWith:(int)i {\n"
1733 " return i;\n"
1734 "}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001735 "+ (int)answerWith:(int)i {\n"
1736 " return i;\n"
1737 "}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001738 "@end");
1739
1740 verifyFormat("@implementation Foo\n"
1741 "@end\n"
1742 "@implementation Bar\n"
1743 "@end");
1744
1745 verifyFormat("@implementation Foo : Bar\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001746 "+ (id)init {}\n"
Nico Weber67015ed2013-01-11 21:14:08 +00001747 "- (void)foo {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001748 "@end");
1749
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001750 verifyFormat("@implementation Foo {\n"
1751 " int _i;\n"
1752 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001753 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001754 "@end");
1755
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001756 verifyFormat("@implementation Foo : Bar {\n"
1757 " int _i;\n"
1758 "}\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001759 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001760 "@end");
1761
Nico Webered91bba2013-01-10 19:19:14 +00001762 verifyFormat("@implementation Foo (HackStuff)\n"
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001763 "+ (id)init {}\n"
Nico Weber50767d82013-01-09 23:25:37 +00001764 "@end");
1765}
1766
Nico Weber1abe6ea2013-01-09 21:15:03 +00001767TEST_F(FormatTest, FormatObjCProtocol) {
1768 verifyFormat("@protocol Foo\n"
1769 "@property(weak) id delegate;\n"
1770 "- (NSUInteger)numberOfThings;\n"
1771 "@end");
1772
Nico Weber5f500df2013-01-10 20:12:55 +00001773 verifyFormat("@protocol MyProtocol <NSObject>\n"
Nico Weber1abe6ea2013-01-09 21:15:03 +00001774 "- (NSUInteger)numberOfThings;\n"
1775 "@end");
1776
Nico Weber5f500df2013-01-10 20:12:55 +00001777 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
Nico Weberaab60052013-01-17 06:14:50 +00001778 "- (NSUInteger)numberOfThings;\n"
Nico Weber5f500df2013-01-10 20:12:55 +00001779 "@end");
1780
Nico Weber1abe6ea2013-01-09 21:15:03 +00001781 verifyFormat("@protocol Foo;\n"
1782 "@protocol Bar;\n");
Nico Weber049c4472013-01-09 21:42:32 +00001783
1784 verifyFormat("@protocol Foo\n"
1785 "@end\n"
1786 "@protocol Bar\n"
1787 "@end");
Nico Weberb530fa32013-01-10 00:25:19 +00001788
1789 verifyFormat("@protocol myProtocol\n"
1790 "- (void)mandatoryWithInt:(int)i;\n"
1791 "@optional\n"
1792 "- (void)optional;\n"
1793 "@required\n"
1794 "- (void)required;\n"
Nico Weber880e5382013-01-10 00:42:07 +00001795 "@optional\n"
1796 "@property(assign) int madProp;\n"
Nico Weberb530fa32013-01-10 00:25:19 +00001797 "@end\n");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001798}
1799
Nico Weberbcfdd262013-01-12 06:18:40 +00001800TEST_F(FormatTest, FormatObjCMethodExpr) {
1801 verifyFormat("[foo bar:baz];");
1802 verifyFormat("return [foo bar:baz];");
1803 verifyFormat("f([foo bar:baz]);");
1804 verifyFormat("f(2, [foo bar:baz]);");
1805 verifyFormat("f(2, a ? b : c);");
1806 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
1807
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 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
1816 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
1817 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
1818 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
1819 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
1820 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
1821 verifyFormat("[foo bar:baz] || [foo bar:baz];");
1822 verifyFormat("[foo bar:baz] && [foo bar:baz];");
1823 verifyFormat("[foo bar:baz] | [foo bar:baz];");
1824 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
1825 verifyFormat("[foo bar:baz] & [foo bar:baz];");
1826 verifyFormat("[foo bar:baz] == [foo bar:baz];");
1827 verifyFormat("[foo bar:baz] != [foo bar:baz];");
1828 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
1829 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
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 // Whew!
1840
1841 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
1842 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
1843 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
1844 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
1845 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
Nico Weber6292dd42013-01-12 23:41:33 +00001846 verifyFormat("[button setAction:@selector(zoomOut:)];");
Nico Webercd458332013-01-12 23:48:49 +00001847 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001848
Nico Weberbcfdd262013-01-12 06:18:40 +00001849 verifyFormat("arr[[self indexForFoo:a]];");
1850 verifyFormat("throw [self errorFor:a];");
1851 verifyFormat("@throw [self errorFor:a];");
1852
Nico Webere8ccc812013-01-12 22:48:47 +00001853 // This tests that the formatter doesn't break after "backing" but before ":",
1854 // which would be at 80 columns.
Nico Weberbcfdd262013-01-12 06:18:40 +00001855 verifyFormat(
1856 "void f() {\n"
Nico Webere8ccc812013-01-12 22:48:47 +00001857 " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
1858 " backing:NSBackingStoreBuffered defer:YES]))");
1859
Nico Webere8ccc812013-01-12 22:48:47 +00001860 verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
1861 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
Nico Weberbcfdd262013-01-12 06:18:40 +00001862
1863}
1864
Nico Weber581f5572013-01-07 15:56:25 +00001865TEST_F(FormatTest, ObjCAt) {
Nico Weber4a293a22013-01-07 16:07:07 +00001866 verifyFormat("@autoreleasepool");
Nico Weberd0af4b42013-01-07 16:14:28 +00001867 verifyFormat("@catch");
1868 verifyFormat("@class");
Nico Weber4a293a22013-01-07 16:07:07 +00001869 verifyFormat("@compatibility_alias");
1870 verifyFormat("@defs");
Nico Weberefcfe732013-01-07 15:17:23 +00001871 verifyFormat("@dynamic");
Nico Weber4a293a22013-01-07 16:07:07 +00001872 verifyFormat("@encode");
1873 verifyFormat("@end");
1874 verifyFormat("@finally");
1875 verifyFormat("@implementation");
1876 verifyFormat("@import");
1877 verifyFormat("@interface");
1878 verifyFormat("@optional");
1879 verifyFormat("@package");
Nico Weberd0af4b42013-01-07 16:14:28 +00001880 verifyFormat("@private");
Nico Weber4a293a22013-01-07 16:07:07 +00001881 verifyFormat("@property");
Nico Weberd0af4b42013-01-07 16:14:28 +00001882 verifyFormat("@protected");
Nico Weber4a293a22013-01-07 16:07:07 +00001883 verifyFormat("@protocol");
Nico Weberd0af4b42013-01-07 16:14:28 +00001884 verifyFormat("@public");
Nico Weber4a293a22013-01-07 16:07:07 +00001885 verifyFormat("@required");
1886 verifyFormat("@selector");
1887 verifyFormat("@synchronized");
1888 verifyFormat("@synthesize");
Nico Weberd0af4b42013-01-07 16:14:28 +00001889 verifyFormat("@throw");
1890 verifyFormat("@try");
Nico Weber4a293a22013-01-07 16:07:07 +00001891
Nico Webercb4d6902013-01-08 19:40:21 +00001892 verifyFormat("@\"String\"");
1893 verifyFormat("@1");
Nico Weber81ed2f12013-01-10 19:36:35 +00001894 verifyFormat("@+4.8");
1895 verifyFormat("@-4");
Nico Webercb4d6902013-01-08 19:40:21 +00001896 verifyFormat("@1LL");
1897 verifyFormat("@.5");
1898 verifyFormat("@'c'");
1899 verifyFormat("@true");
1900 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
Nico Webere8ccc812013-01-12 22:48:47 +00001901 // FIXME: Array and dictionary literals need more work.
Nico Webercb4d6902013-01-08 19:40:21 +00001902 verifyFormat("@[");
1903 verifyFormat("@{");
1904
Nico Weber581f5572013-01-07 15:56:25 +00001905 EXPECT_EQ("@interface", format("@ interface"));
1906
1907 // The precise formatting of this doesn't matter, nobody writes code like
1908 // this.
1909 verifyFormat("@ /*foo*/ interface");
Nico Weberefcfe732013-01-07 15:17:23 +00001910}
1911
Nico Weberc31689a2013-01-08 19:15:23 +00001912TEST_F(FormatTest, ObjCSnippets) {
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001913 verifyFormat("@autoreleasepool {\n"
1914 " foo();\n"
1915 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001916 verifyFormat("@class Foo, Bar;");
Nico Weberc31689a2013-01-08 19:15:23 +00001917 verifyFormat("@compatibility_alias AliasName ExistingClass;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001918 verifyFormat("@dynamic textColor;");
Nico Weber5dfe9b42013-01-18 05:11:47 +00001919 verifyFormat("char *buf1 = @encode(int *);");
1920 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1921 // FIXME: Enable once PR14884 is fixed:
Nico Weberc31689a2013-01-08 19:15:23 +00001922 //verifyFormat("char *buf1 = @encode(int **);");
Nico Weber3a2673e2013-01-08 20:16:23 +00001923 verifyFormat("Protocol *proto = @protocol(p1);");
Nico Weber6a21a552013-01-18 02:43:57 +00001924 verifyFormat("SEL s = @selector(foo:);");
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001925 verifyFormat("@synchronized(self) {\n"
1926 " f();\n"
1927 "}");
Nico Webercf4a79c2013-01-08 17:56:31 +00001928
Nico Weber70848232013-01-10 21:30:42 +00001929 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1930 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1931
Nico Webercf4a79c2013-01-08 17:56:31 +00001932 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
Nico Weber70848232013-01-10 21:30:42 +00001933 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1934 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
Nico Webercf4a79c2013-01-08 17:56:31 +00001935}
1936
Daniel Jaspercd162382013-01-07 13:26:07 +00001937} // end namespace tooling
1938} // end namespace clang