Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===- 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 | |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 10 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 11 | #include "../Tooling/RewriterTestContext.h" |
| 12 | #include "clang/Lex/Lexer.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace format { |
| 17 | |
| 18 | class FormatTest : public ::testing::Test { |
| 19 | protected: |
| 20 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, |
| 21 | const FormatStyle &Style) { |
| 22 | RewriterTestContext Context; |
| 23 | FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 24 | SourceLocation Start = |
| 25 | Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 26 | std::vector<CharSourceRange> Ranges( |
| 27 | 1, |
| 28 | CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); |
| 29 | LangOptions LangOpts; |
| 30 | LangOpts.CPlusPlus = 1; |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 31 | LangOpts.CPlusPlus11 = 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 32 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts); |
| 33 | tooling::Replacements Replace = |
| 34 | reformat(Style, Lex, Context.Sources, Ranges); |
| 35 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
| 36 | return Context.getRewrittenText(ID); |
| 37 | } |
| 38 | |
| 39 | std::string format(llvm::StringRef Code, |
| 40 | const FormatStyle &Style = getLLVMStyle()) { |
| 41 | return format(Code, 0, Code.size(), Style); |
| 42 | } |
| 43 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 44 | std::string messUp(llvm::StringRef Code) { |
| 45 | std::string MessedUp(Code.str()); |
| 46 | bool InComment = false; |
| 47 | bool JustReplacedNewline = false; |
| 48 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 49 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 50 | if (JustReplacedNewline) |
| 51 | MessedUp[i - 1] = '\n'; |
| 52 | InComment = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 53 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 54 | MessedUp[i] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 55 | } else if (MessedUp[i] == '\n') { |
| 56 | if (InComment) { |
| 57 | InComment = false; |
| 58 | } else { |
| 59 | JustReplacedNewline = true; |
| 60 | MessedUp[i] = ' '; |
| 61 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 62 | } else if (MessedUp[i] != ' ') { |
| 63 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 64 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 65 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 66 | return MessedUp; |
| 67 | } |
| 68 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 69 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 70 | FormatStyle Style = getLLVMStyle(); |
| 71 | Style.ColumnLimit = ColumnLimit; |
| 72 | return Style; |
| 73 | } |
| 74 | |
| 75 | void verifyFormat(llvm::StringRef Code, |
| 76 | const FormatStyle &Style = getLLVMStyle()) { |
| 77 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 81 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 85 | //===----------------------------------------------------------------------===// |
| 86 | // Basic function tests. |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 89 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 90 | EXPECT_EQ(";", format(";")); |
| 91 | } |
| 92 | |
| 93 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 94 | EXPECT_EQ("int i;", format(" int i;")); |
| 95 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 96 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 97 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 98 | } |
| 99 | |
| 100 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 101 | EXPECT_EQ("int i;", format("int\ni;")); |
| 102 | } |
| 103 | |
| 104 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
| 105 | EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); |
| 106 | } |
| 107 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 108 | TEST_F(FormatTest, FormatsNestedCall) { |
| 109 | verifyFormat("Method(f1, f2(f3));"); |
| 110 | verifyFormat("Method(f1(f2, f3()));"); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | // Tests for control statements. |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
| 118 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 119 | verifyFormat("if (true)\n f();\ng();"); |
| 120 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 121 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 122 | verifyFormat("if (a)\n" |
| 123 | " // comment\n" |
| 124 | " f();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | TEST_F(FormatTest, ParseIfElse) { |
| 128 | verifyFormat("if (true)\n" |
| 129 | " if (true)\n" |
| 130 | " if (true)\n" |
| 131 | " f();\n" |
| 132 | " else\n" |
| 133 | " g();\n" |
| 134 | " else\n" |
| 135 | " h();\n" |
| 136 | "else\n" |
| 137 | " i();"); |
| 138 | verifyFormat("if (true)\n" |
| 139 | " if (true)\n" |
| 140 | " if (true) {\n" |
| 141 | " if (true)\n" |
| 142 | " f();\n" |
| 143 | " } else {\n" |
| 144 | " g();\n" |
| 145 | " }\n" |
| 146 | " else\n" |
| 147 | " h();\n" |
| 148 | "else {\n" |
| 149 | " i();\n" |
| 150 | "}"); |
| 151 | } |
| 152 | |
| 153 | TEST_F(FormatTest, ElseIf) { |
| 154 | verifyFormat("if (a) {\n" |
| 155 | "} else if (b) {\n" |
| 156 | "}"); |
| 157 | verifyFormat("if (a)\n" |
| 158 | " f();\n" |
| 159 | "else if (b)\n" |
| 160 | " g();\n" |
| 161 | "else\n" |
| 162 | " h();"); |
| 163 | } |
| 164 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 165 | TEST_F(FormatTest, FormatsForLoop) { |
| 166 | verifyFormat( |
| 167 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 168 | " ++VeryVeryLongLoopVariable)\n" |
| 169 | " ;"); |
| 170 | verifyFormat("for (;;)\n" |
| 171 | " f();"); |
| 172 | verifyFormat("for (;;) {\n" |
| 173 | "}"); |
| 174 | verifyFormat("for (;;) {\n" |
| 175 | " f();\n" |
| 176 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 177 | |
| 178 | verifyFormat( |
| 179 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 180 | " E = UnwrappedLines.end();\n" |
| 181 | " I != E; ++I) {\n}"); |
| 182 | |
| 183 | verifyFormat( |
| 184 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
| 185 | " ++IIIII) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 189 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 190 | verifyFormat("while (true)\n" |
| 191 | " f();"); |
| 192 | verifyFormat("while () {\n" |
| 193 | "}"); |
| 194 | verifyFormat("while () {\n" |
| 195 | " f();\n" |
| 196 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 199 | TEST_F(FormatTest, FormatsDoWhile) { |
| 200 | verifyFormat("do {\n" |
| 201 | " do_something();\n" |
| 202 | "} while (something());"); |
| 203 | verifyFormat("do\n" |
| 204 | " do_something();\n" |
| 205 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 208 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 209 | verifyFormat("switch (x) {\n" |
| 210 | "case 1:\n" |
| 211 | " f();\n" |
| 212 | " break;\n" |
| 213 | "case kFoo:\n" |
| 214 | "case ns::kBar:\n" |
| 215 | "case kBaz:\n" |
| 216 | " break;\n" |
| 217 | "default:\n" |
| 218 | " g();\n" |
| 219 | " break;\n" |
| 220 | "}"); |
| 221 | verifyFormat("switch (x) {\n" |
| 222 | "case 1: {\n" |
| 223 | " f();\n" |
| 224 | " break;\n" |
| 225 | "}\n" |
| 226 | "}"); |
| 227 | verifyFormat("switch (test)\n" |
| 228 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 229 | verifyGoogleFormat("switch (x) {\n" |
| 230 | " case 1:\n" |
| 231 | " f();\n" |
| 232 | " break;\n" |
| 233 | " case kFoo:\n" |
| 234 | " case ns::kBar:\n" |
| 235 | " case kBaz:\n" |
| 236 | " break;\n" |
| 237 | " default:\n" |
| 238 | " g();\n" |
| 239 | " break;\n" |
| 240 | "}"); |
| 241 | verifyGoogleFormat("switch (x) {\n" |
| 242 | " case 1: {\n" |
| 243 | " f();\n" |
| 244 | " break;\n" |
| 245 | " }\n" |
| 246 | "}"); |
| 247 | verifyGoogleFormat("switch (test)\n" |
| 248 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 251 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 252 | verifyFormat("void f() {\n" |
| 253 | " some_code();\n" |
| 254 | "test_label:\n" |
| 255 | " some_other_code();\n" |
| 256 | " {\n" |
| 257 | " some_more_code();\n" |
| 258 | " another_label:\n" |
| 259 | " some_more_code();\n" |
| 260 | " }\n" |
| 261 | "}"); |
| 262 | verifyFormat("some_code();\n" |
| 263 | "test_label:\n" |
| 264 | "some_other_code();"); |
| 265 | } |
| 266 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 267 | |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | // Tests for comments. |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
| 272 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 273 | verifyFormat("// line 1\n" |
| 274 | "// line 2\n" |
| 275 | "void f() {\n}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 276 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 277 | verifyFormat("void f() {\n" |
| 278 | " // Doesn't do anything\n" |
| 279 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 280 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 281 | verifyFormat("int i // This is a fancy variable\n" |
| 282 | " = 5;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 283 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 284 | verifyFormat("enum E {\n" |
| 285 | " // comment\n" |
| 286 | " VAL_A, // comment\n" |
| 287 | " VAL_B\n" |
| 288 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 289 | |
| 290 | verifyFormat( |
| 291 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 292 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
| 293 | } |
| 294 | |
| 295 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 296 | verifyFormat("f(/*test=*/ true);"); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | //===----------------------------------------------------------------------===// |
| 301 | // Tests for classes, namespaces, etc. |
| 302 | //===----------------------------------------------------------------------===// |
| 303 | |
| 304 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 305 | verifyFormat("class A {\n};"); |
| 306 | } |
| 307 | |
| 308 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 309 | verifyFormat("class A {\n" |
| 310 | "public:\n" |
| 311 | "protected:\n" |
| 312 | "private:\n" |
| 313 | " void f() {\n" |
| 314 | " }\n" |
| 315 | "};"); |
| 316 | verifyGoogleFormat("class A {\n" |
| 317 | " public:\n" |
| 318 | " protected:\n" |
| 319 | " private:\n" |
| 320 | " void f() {\n" |
| 321 | " }\n" |
| 322 | "};"); |
| 323 | } |
| 324 | |
| 325 | TEST_F(FormatTest, FormatsDerivedClass) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 326 | verifyFormat("class A : public B {\n" |
| 327 | "};"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 328 | verifyFormat("class A : public ::B {\n" |
| 329 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 332 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 333 | verifyFormat("enum {\n" |
| 334 | " Zero,\n" |
| 335 | " One = 1,\n" |
| 336 | " Two = One + 1,\n" |
| 337 | " Three = (One + Two),\n" |
| 338 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 339 | " Five = (One, Two, Three, Four, 5)\n" |
| 340 | "};"); |
| 341 | verifyFormat("enum Enum {\n" |
| 342 | "};"); |
| 343 | verifyFormat("enum {\n" |
| 344 | "};"); |
| 345 | } |
| 346 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 347 | TEST_F(FormatTest, FormatsNamespaces) { |
| 348 | verifyFormat("namespace some_namespace {\n" |
| 349 | "class A {\n" |
| 350 | "};\n" |
| 351 | "void f() {\n" |
| 352 | " f();\n" |
| 353 | "}\n" |
| 354 | "}"); |
| 355 | verifyFormat("namespace {\n" |
| 356 | "class A {\n" |
| 357 | "};\n" |
| 358 | "void f() {\n" |
| 359 | " f();\n" |
| 360 | "}\n" |
| 361 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 362 | verifyFormat("inline namespace X {\n" |
| 363 | "class A {\n" |
| 364 | "};\n" |
| 365 | "void f() {\n" |
| 366 | " f();\n" |
| 367 | "}\n" |
| 368 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 369 | verifyFormat("using namespace some_namespace;\n" |
| 370 | "class A {\n" |
| 371 | "};\n" |
| 372 | "void f() {\n" |
| 373 | " f();\n" |
| 374 | "}"); |
| 375 | } |
| 376 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 377 | TEST_F(FormatTest, StaticInitializers) { |
| 378 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 379 | |
| 380 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 381 | verifyFormat( |
| 382 | "static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n" |
| 383 | " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); |
| 384 | } |
| 385 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 386 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 387 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 388 | " \\\n" |
| 389 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 390 | } |
| 391 | |
| 392 | TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { |
| 393 | EXPECT_EQ("#\n;", format("#;")); |
| 394 | } |
| 395 | |
| 396 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 397 | EXPECT_EQ("#line 42 \"test\"\n", |
| 398 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 399 | EXPECT_EQ("#define A \\\n B\n", |
| 400 | format("# \\\n define \\\n A \\\n B\n", |
| 401 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 405 | EXPECT_EQ("#line 42 \"test\"", |
| 406 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 407 | EXPECT_EQ("#define A \\\n B", |
| 408 | format("# \\\n define \\\n A \\\n B", |
| 409 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 412 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
| 413 | // If the macro fits in one line, we have the full width. |
| 414 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(12)); |
| 415 | |
| 416 | verifyFormat("#define A(\\\n B)", getLLVMStyleWithColumns(11)); |
| 417 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(11)); |
| 418 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
| 419 | } |
| 420 | |
| 421 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 422 | EXPECT_EQ("// some comment\n" |
| 423 | "#include \"a.h\"\n" |
| 424 | "#define A(A,\\\n" |
| 425 | " B)\n" |
| 426 | "#include \"b.h\"\n" |
| 427 | "// some comment\n", |
| 428 | format(" // some comment\n" |
| 429 | " #include \"a.h\"\n" |
| 430 | "#define A(A,\\\n" |
| 431 | " B)\n" |
| 432 | " #include \"b.h\"\n" |
| 433 | " // some comment\n", getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 436 | TEST_F(FormatTest, LayoutSingleHash) { |
| 437 | EXPECT_EQ("#\na;", format("#\na;")); |
| 438 | } |
| 439 | |
| 440 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 441 | EXPECT_EQ("#define A \\\n" |
| 442 | " c; \\\n" |
| 443 | " e;\n" |
| 444 | "f;", format("#define A c; e;\n" |
| 445 | "f;", getLLVMStyleWithColumns(14))); |
| 446 | } |
| 447 | |
| 448 | TEST_F(FormatTest, LayoutRemainingTokens) { |
| 449 | EXPECT_EQ("{\n}", format("{}")); |
| 450 | } |
| 451 | |
| 452 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
| 453 | EXPECT_EQ("#define A \\\n b;", |
| 454 | format("#define A b;", 10, 2, getLLVMStyleWithColumns(11))); |
| 455 | } |
| 456 | |
| 457 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
| 458 | EXPECT_EQ("int x,\n#define A\ny;", format("int x,\n#define A\ny;")); |
| 459 | } |
| 460 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 461 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 462 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 463 | verifyFormat("#define A \\\n" |
| 464 | " { \\\n" |
| 465 | " f(#c);\\\n" |
| 466 | " }", getLLVMStyleWithColumns(11)); |
| 467 | } |
| 468 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 469 | // FIXME: write test for unbalanced braces in macros... |
| 470 | // FIXME: test { { #include "a.h" } } |
| 471 | // FIXME: test # in the middle of a statement without \n before it |
| 472 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 473 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 474 | EXPECT_EQ( |
| 475 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 476 | " \\\n" |
| 477 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 478 | "\n" |
| 479 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 480 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 481 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 482 | "\\\n" |
| 483 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 484 | " \n" |
| 485 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 486 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 489 | //===----------------------------------------------------------------------===// |
| 490 | // Line break tests. |
| 491 | //===----------------------------------------------------------------------===// |
| 492 | |
| 493 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 494 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 495 | " int h, int j, int f,\n" |
| 496 | " int c, int ddddddddddddd) {\n" |
| 497 | "}"); |
| 498 | } |
| 499 | |
| 500 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 501 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 502 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 503 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 504 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 507 | TEST_F(FormatTest, ConstructorInitializers) { |
| 508 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}"); |
| 509 | |
| 510 | verifyFormat( |
| 511 | "SomeClass::Constructor()\n" |
| 512 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 513 | "}"); |
| 514 | |
| 515 | verifyFormat( |
| 516 | "SomeClass::Constructor()\n" |
| 517 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 518 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 519 | "}"); |
| 520 | |
| 521 | verifyFormat("Constructor()\n" |
| 522 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 523 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 524 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 525 | " aaaaaaaaaaaaaaaaaaaaaaa() {\n" |
| 526 | "}"); |
| 527 | |
| 528 | // Here a line could be saved by splitting the second initializer onto two |
| 529 | // lines, but that is not desireable. |
| 530 | verifyFormat("Constructor()\n" |
| 531 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 532 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 533 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 534 | "}"); |
| 535 | |
| 536 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
| 537 | " : some_var_(var), // 4 space indent\n" |
| 538 | " some_other_var_(var + 1) { // lined up\n" |
| 539 | "}"); |
| 540 | } |
| 541 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 542 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 543 | verifyFormat( |
| 544 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 545 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 546 | " f();"); |
| 547 | } |
| 548 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 549 | TEST_F(FormatTest, BreaksDesireably) { |
| 550 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 551 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 552 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};"); |
| 553 | |
| 554 | verifyFormat( |
| 555 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 556 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 557 | |
| 558 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 559 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 560 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 561 | |
| 562 | verifyFormat( |
| 563 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 564 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 565 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 566 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 567 | |
| 568 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 569 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 570 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 571 | verifyFormat( |
| 572 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 573 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 574 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 575 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 576 | // taking into account the StopAt value. |
| 577 | verifyFormat( |
| 578 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 579 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 580 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 581 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 582 | |
| 583 | verifyFormat( |
| 584 | "{\n {\n {\n" |
| 585 | " Annotation.SpaceRequiredBefore =\n" |
| 586 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 587 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 588 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 591 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 592 | verifyFormat( |
| 593 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 594 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
| 595 | verifyFormat( |
| 596 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 597 | " ccccccccccccccccccccccccc) {\n}"); |
| 598 | verifyFormat( |
| 599 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 600 | " ccccccccccccccccccccccccc) {\n}"); |
| 601 | verifyFormat( |
| 602 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
| 603 | " ccccccccccccccccccccccccc) {\n}"); |
| 604 | } |
| 605 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 606 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 607 | verifyFormat( |
| 608 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 609 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 610 | verifyFormat( |
| 611 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 612 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 613 | verifyFormat( |
| 614 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 615 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 616 | verifyFormat( |
| 617 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 618 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 619 | verifyFormat( |
| 620 | "double LooooooooooooooooooooooooongResult =\n" |
| 621 | " aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 622 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 623 | } |
| 624 | |
| 625 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 626 | verifyFormat( |
| 627 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 628 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 629 | verifyFormat( |
| 630 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 631 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 632 | } |
| 633 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 634 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 635 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 636 | " \"short literal\");"); |
| 637 | verifyFormat( |
| 638 | "looooooooooooooooooooooooongFunction(\n" |
| 639 | " \"short literal\"\n" |
| 640 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 641 | } |
| 642 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 643 | TEST_F(FormatTest, AlignsPipes) { |
| 644 | verifyFormat( |
| 645 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 646 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 647 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 648 | verifyFormat( |
| 649 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 650 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 651 | verifyFormat( |
| 652 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 653 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 654 | verifyFormat( |
| 655 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 656 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 657 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 658 | verifyFormat( |
| 659 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 660 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 661 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 662 | } |
| 663 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 664 | TEST_F(FormatTest, UnderstandsEquals) { |
| 665 | verifyFormat( |
| 666 | "aaaaaaaaaaaaaaaaa =\n" |
| 667 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 668 | verifyFormat( |
| 669 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 670 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 671 | "}"); |
| 672 | verifyFormat( |
| 673 | "if (a) {\n" |
| 674 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 675 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 676 | "}"); |
| 677 | |
| 678 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 679 | " 100000000 + 100000000) {\n}"); |
| 680 | } |
| 681 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 682 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 683 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 684 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 685 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 686 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 687 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 688 | |
| 689 | verifyFormat( |
| 690 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 691 | " Parameter2);"); |
| 692 | |
| 693 | verifyFormat( |
| 694 | "ShortObject->shortFunction(\n" |
| 695 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 696 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 697 | |
| 698 | verifyFormat("loooooooooooooongFunction(\n" |
| 699 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 700 | |
| 701 | verifyFormat( |
| 702 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 703 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 704 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 705 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
| 706 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 707 | "}"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 710 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 711 | verifyFormat("template <typename T>\n" |
| 712 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 713 | verifyFormat( |
| 714 | "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 715 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
| 716 | verifyFormat( |
| 717 | "template <typename T>\n" |
| 718 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 719 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 720 | verifyFormat( |
| 721 | "template <typename T>\n" |
| 722 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 723 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 724 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 725 | |
| 726 | } |
| 727 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 728 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 729 | verifyFormat("A<int> a;"); |
| 730 | verifyFormat("A<A<A<int> > > a;"); |
| 731 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 732 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 733 | verifyFormat("bool x = 5 < f<int>();"); |
| 734 | verifyFormat("bool x = f<int>() > 5;"); |
| 735 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 736 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 737 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 738 | |
| 739 | verifyGoogleFormat("A<A<int>> a;"); |
| 740 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 741 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 742 | |
| 743 | verifyFormat("test >> a >> b;"); |
| 744 | verifyFormat("test << a >> b;"); |
| 745 | |
| 746 | verifyFormat("f<int>();"); |
| 747 | verifyFormat("template <typename T> void f() {\n}"); |
| 748 | } |
| 749 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 750 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 751 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 752 | verifyFormat("f(-1, -2, -3);"); |
| 753 | verifyFormat("a[-1] = 5;"); |
| 754 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 755 | verifyFormat("if (i == -1) {\n}"); |
| 756 | verifyFormat("if (i != -1) {\n}"); |
| 757 | verifyFormat("if (i > -1) {\n}"); |
| 758 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 759 | verifyFormat("++(a->f());"); |
| 760 | verifyFormat("--(a->f());"); |
| 761 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 762 | |
| 763 | verifyFormat("a-- > b;"); |
| 764 | verifyFormat("b ? -a : c;"); |
| 765 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 766 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 767 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 768 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 769 | |
| 770 | verifyFormat("return -1;"); |
| 771 | verifyFormat("switch (a) {\n" |
| 772 | "case -1:\n" |
| 773 | " break;\n" |
| 774 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 778 | verifyFormat("bool operator<();"); |
| 779 | verifyFormat("bool operator>();"); |
| 780 | verifyFormat("bool operator=();"); |
| 781 | verifyFormat("bool operator==();"); |
| 782 | verifyFormat("bool operator!=();"); |
| 783 | verifyFormat("int operator+();"); |
| 784 | verifyFormat("int operator++();"); |
| 785 | verifyFormat("bool operator();"); |
| 786 | verifyFormat("bool operator()();"); |
| 787 | verifyFormat("bool operator[]();"); |
| 788 | verifyFormat("operator bool();"); |
| 789 | verifyFormat("operator SomeType<int>();"); |
| 790 | verifyFormat("void *operator new(std::size_t size);"); |
| 791 | verifyFormat("void *operator new[](std::size_t size);"); |
| 792 | verifyFormat("void operator delete(void *ptr);"); |
| 793 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 796 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 797 | verifyFormat("int *f(int *a) {\n}"); |
| 798 | verifyFormat("f(a, *a);"); |
| 799 | verifyFormat("f(*a);"); |
| 800 | verifyFormat("int a = b * 10;"); |
| 801 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 802 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 803 | verifyFormat("int a += b * c;"); |
| 804 | verifyFormat("int a -= b * c;"); |
| 805 | verifyFormat("int a *= b * c;"); |
| 806 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 807 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 808 | verifyFormat("int a = *b * c;"); |
| 809 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 810 | verifyFormat("int main(int argc, char **argv) {\n}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 811 | verifyFormat("return 10 * b;"); |
| 812 | verifyFormat("return *b * *c;"); |
| 813 | verifyFormat("return a & ~b;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 814 | verifyFormat("f(b ? *c : *d);"); |
| 815 | verifyFormat("int a = b ? *c : *d;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 816 | verifyFormat("*b = a;"); |
| 817 | verifyFormat("a * ~b;"); |
| 818 | verifyFormat("a * !b;"); |
| 819 | verifyFormat("a * +b;"); |
| 820 | verifyFormat("a * -b;"); |
| 821 | verifyFormat("a * ++b;"); |
| 822 | verifyFormat("a * --b;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 823 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 824 | verifyFormat("InvalidRegions[*R] = 0;"); |
| 825 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 826 | // FIXME: Is this desired for LLVM? Fix if not. |
| 827 | verifyFormat("A<int *> a;"); |
| 828 | verifyFormat("A<int **> a;"); |
| 829 | verifyFormat("A<int *, int *> a;"); |
| 830 | verifyFormat("A<int **, int **> a;"); |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 831 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 832 | verifyFormat("Type *A = (Type *) P;"); |
| 833 | verifyFormat("Type *A = (vector<Type *, int *>) P;"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 834 | |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 835 | verifyGoogleFormat("int main(int argc, char** argv) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 836 | verifyGoogleFormat("A<int*> a;"); |
| 837 | verifyGoogleFormat("A<int**> a;"); |
| 838 | verifyGoogleFormat("A<int*, int*> a;"); |
| 839 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 840 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 841 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 844 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
| 845 | verifyFormat( |
| 846 | "int *someFunction(int LoooooooooooooooongParam1,\n" |
| 847 | " int LoooooooooooooooongParam2) {\n}"); |
| 848 | verifyFormat( |
| 849 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 850 | " SourceLocation L, IdentifierIn *II,\n" |
| 851 | " Type *T) {\n}"); |
| 852 | } |
| 853 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 854 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 855 | verifyFormat("(a)->b();"); |
| 856 | verifyFormat("--a;"); |
| 857 | } |
| 858 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 859 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 860 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 861 | EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 862 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 863 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 864 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 865 | |
| 866 | EXPECT_EQ("#import <string>\n", format("#import <string>\n")); |
| 867 | EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); |
| 868 | EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); |
| 869 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
| 870 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 873 | |
| 874 | //===----------------------------------------------------------------------===// |
| 875 | // Error recovery tests. |
| 876 | //===----------------------------------------------------------------------===// |
| 877 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 878 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 879 | verifyFormat("public:"); |
| 880 | verifyFormat("class A {\n" |
| 881 | "public\n" |
| 882 | " void f() {\n" |
| 883 | " }\n" |
| 884 | "};"); |
| 885 | verifyFormat("public\n" |
| 886 | "int qwerty;"); |
| 887 | verifyFormat("public\n" |
| 888 | "B {\n" |
| 889 | "};"); |
| 890 | verifyFormat("public\n" |
| 891 | "{\n" |
| 892 | "};"); |
| 893 | verifyFormat("public\n" |
| 894 | "B {\n" |
| 895 | " int x;\n" |
| 896 | "};"); |
| 897 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 898 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 899 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 900 | verifyFormat("{"); |
| 901 | } |
| 902 | |
| 903 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 904 | verifyFormat("do {\n" |
| 905 | "};"); |
| 906 | verifyFormat("do {\n" |
| 907 | "};\n" |
| 908 | "f();"); |
| 909 | verifyFormat("do {\n" |
| 910 | "}\n" |
| 911 | "wheeee(fun);"); |
| 912 | verifyFormat("do {\n" |
| 913 | " f();\n" |
| 914 | "};"); |
| 915 | } |
| 916 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 917 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 918 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 919 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 922 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 923 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 924 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 925 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 926 | EXPECT_EQ("{\n {\n }\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 927 | |
| 928 | FormatStyle Style = getLLVMStyle(); |
| 929 | Style.ColumnLimit = 10; |
| 930 | EXPECT_EQ("{\n" |
| 931 | " {\n" |
| 932 | " breakme(\n" |
| 933 | " qwe);\n" |
| 934 | "}\n", format("{\n" |
| 935 | " {\n" |
| 936 | " breakme(qwe);\n" |
| 937 | "}\n", Style)); |
| 938 | |
| 939 | } |
| 940 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 941 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 942 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 943 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 944 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
| 945 | EXPECT_EQ("- (NSInteger)Mthod1;", |
| 946 | format("-(NSInteger)Mthod1;")); |
| 947 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 948 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 949 | format("-(NSInteger)Method3:(id)anObject;")); |
| 950 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 951 | format("-(NSInteger)Method4:(id)anObject;")); |
| 952 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 953 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 954 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 955 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
| 956 | EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 957 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 958 | |
| 959 | // Very long objectiveC method declaration. |
| 960 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 961 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 962 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 963 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 964 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 965 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 966 | |
| 967 | format("- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 968 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 969 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 970 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 971 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 972 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 975 | } // end namespace tooling |
| 976 | } // end namespace clang |