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