1 //===- unittest/Format/FormatTestSelective.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 10 #include "FormatTestUtils.h" 11 #include "clang/Format/Format.h" 12 #include "llvm/Support/Debug.h" 13 #include "gtest/gtest.h" 14 15 #define DEBUG_TYPE "format-test" 16 17 namespace clang { 18 namespace format { 19 namespace { 20 21 class FormatTestSelective : public ::testing::Test { 22 protected: 23 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) { 24 DEBUG(llvm::errs() << "---\n"); 25 DEBUG(llvm::errs() << Code << "\n\n"); 26 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); 27 bool IncompleteFormat = false; 28 tooling::Replacements Replaces = 29 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 30 EXPECT_FALSE(IncompleteFormat) << Code << "\n\n"; 31 std::string Result = applyAllReplacements(Code, Replaces); 32 EXPECT_NE("", Result); 33 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 34 return Result; 35 } 36 37 FormatStyle Style = getLLVMStyle(); 38 }; 39 40 TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) { 41 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0)); 42 EXPECT_EQ("int a;", format("int a; ", 0, 0)); 43 EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0)); 44 EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0)); 45 } 46 47 TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) { 48 EXPECT_EQ("{int b;\n" 49 " int a;\n" 50 "}", 51 format("{int b;\n int a;}", 8, 0)); 52 EXPECT_EQ("{\n" 53 " int b;\n" 54 " int a;}", 55 format("{int b;\n int a;}", 7, 0)); 56 57 Style.ColumnLimit = 12; 58 EXPECT_EQ("#define A \\\n" 59 " int a; \\\n" 60 " int b;", 61 format("#define A \\\n" 62 " int a; \\\n" 63 " int b;", 64 26, 0)); 65 EXPECT_EQ("#define A \\\n" 66 " int a; \\\n" 67 " int b;", 68 format("#define A \\\n" 69 " int a; \\\n" 70 " int b;", 71 25, 0)); 72 } 73 74 TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) { 75 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0)); 76 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0)); 77 78 // This might not strictly be correct, but is likely good in all practical 79 // cases. 80 EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0)); 81 } 82 83 TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) { 84 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0)); 85 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0)); 86 } 87 88 TEST_F(FormatTestSelective, ReformatsMovedLines) { 89 EXPECT_EQ( 90 "template <typename T> T *getFETokenInfo() const {\n" 91 " return static_cast<T *>(FETokenInfo);\n" 92 "}\n" 93 "int a; // <- Should not be formatted", 94 format( 95 "template<typename T>\n" 96 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" 97 "int a; // <- Should not be formatted", 98 9, 5)); 99 } 100 101 TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) { 102 Style.AllowShortIfStatementsOnASingleLine = true; 103 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1)); 104 EXPECT_EQ("if (a) return; // comment", 105 format("if(a)\nreturn; // comment", 20, 1)); 106 } 107 108 TEST_F(FormatTestSelective, FormatsCommentsLocally) { 109 EXPECT_EQ("int a; // comment\n" 110 "int b; // comment", 111 format("int a; // comment\n" 112 "int b; // comment", 113 0, 0)); 114 EXPECT_EQ("int a; // comment\n" 115 " // line 2\n" 116 "int b;", 117 format("int a; // comment\n" 118 " // line 2\n" 119 "int b;", 120 28, 0)); 121 EXPECT_EQ("int aaaaaa; // comment\n" 122 "int b;\n" 123 "int c; // unrelated comment", 124 format("int aaaaaa; // comment\n" 125 "int b;\n" 126 "int c; // unrelated comment", 127 31, 0)); 128 129 EXPECT_EQ("int a; // This\n" 130 " // is\n" 131 " // a", 132 format("int a; // This\n" 133 " // is\n" 134 " // a", 135 0, 0)); 136 EXPECT_EQ("int a; // This\n" 137 " // is\n" 138 " // a\n" 139 "// This is b\n" 140 "int b;", 141 format("int a; // This\n" 142 " // is\n" 143 " // a\n" 144 "// This is b\n" 145 "int b;", 146 0, 0)); 147 EXPECT_EQ("int a; // This\n" 148 " // is\n" 149 " // a\n" 150 "\n" 151 "//This is unrelated", 152 format("int a; // This\n" 153 " // is\n" 154 " // a\n" 155 "\n" 156 "//This is unrelated", 157 0, 0)); 158 EXPECT_EQ("int a;\n" 159 "// This is\n" 160 "// not formatted. ", 161 format("int a;\n" 162 "// This is\n" 163 "// not formatted. ", 164 0, 0)); 165 EXPECT_EQ("int x; // Format this line.\n" 166 "int xx; //\n" 167 "int xxxxx; //", 168 format("int x; // Format this line.\n" 169 "int xx; //\n" 170 "int xxxxx; //", 171 0, 0)); 172 } 173 174 TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) { 175 EXPECT_EQ("DEBUG({\n" 176 " int i;\n" 177 " int j;\n" 178 "});", 179 format("DEBUG( {\n" 180 " int i;\n" 181 " int j;\n" 182 "} ) ;", 183 20, 1)); 184 EXPECT_EQ("DEBUG( {\n" 185 " int i;\n" 186 " int j;\n" 187 "} ) ;", 188 format("DEBUG( {\n" 189 " int i;\n" 190 " int j;\n" 191 "} ) ;", 192 41, 1)); 193 EXPECT_EQ("DEBUG( {\n" 194 " int i;\n" 195 " int j;\n" 196 "} ) ;", 197 format("DEBUG( {\n" 198 " int i;\n" 199 " int j;\n" 200 "} ) ;", 201 41, 1)); 202 EXPECT_EQ("DEBUG({\n" 203 " int i;\n" 204 " int j;\n" 205 "});", 206 format("DEBUG( {\n" 207 " int i;\n" 208 " int j;\n" 209 "} ) ;", 210 20, 1)); 211 212 EXPECT_EQ("Debug({\n" 213 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 214 " return;\n" 215 " },\n" 216 " a);", 217 format("Debug({\n" 218 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 219 " return;\n" 220 " },\n" 221 " a);", 222 50, 1)); 223 EXPECT_EQ("DEBUG({\n" 224 " DEBUG({\n" 225 " int a;\n" 226 " int b;\n" 227 " }) ;\n" 228 "});", 229 format("DEBUG({\n" 230 " DEBUG({\n" 231 " int a;\n" 232 " int b;\n" // Format this line only. 233 " }) ;\n" // Don't touch this line. 234 "});", 235 35, 0)); 236 EXPECT_EQ("DEBUG({\n" 237 " int a; //\n" 238 "});", 239 format("DEBUG({\n" 240 " int a; //\n" 241 "});", 242 0, 0)); 243 EXPECT_EQ("someFunction(\n" 244 " [] {\n" 245 " // Only with this comment.\n" 246 " int i; // invoke formatting here.\n" 247 " }, // force line break\n" 248 " aaa);", 249 format("someFunction(\n" 250 " [] {\n" 251 " // Only with this comment.\n" 252 " int i; // invoke formatting here.\n" 253 " }, // force line break\n" 254 " aaa);", 255 63, 1)); 256 257 EXPECT_EQ("int longlongname; // comment\n" 258 "int x = f({\n" 259 " int x; // comment\n" 260 " int y; // comment\n" 261 "});", 262 format("int longlongname; // comment\n" 263 "int x = f({\n" 264 " int x; // comment\n" 265 " int y; // comment\n" 266 "});", 267 65, 0)); 268 EXPECT_EQ("int s = f({\n" 269 " class X {\n" 270 " public:\n" 271 " void f();\n" 272 " };\n" 273 "});", 274 format("int s = f({\n" 275 " class X {\n" 276 " public:\n" 277 " void f();\n" 278 " };\n" 279 "});", 280 0, 0)); 281 } 282 283 TEST_F(FormatTestSelective, WrongIndent) { 284 EXPECT_EQ("namespace {\n" 285 "int i;\n" 286 "int j;\n" 287 "}", 288 format("namespace {\n" 289 " int i;\n" // Format here. 290 " int j;\n" 291 "}", 292 15, 0)); 293 EXPECT_EQ("namespace {\n" 294 " int i;\n" 295 " int j;\n" 296 "}", 297 format("namespace {\n" 298 " int i;\n" 299 " int j;\n" // Format here. 300 "}", 301 24, 0)); 302 } 303 304 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) { 305 Style.AlignEscapedNewlinesLeft = true; 306 EXPECT_EQ("int i;\n" 307 "#define A \\\n" 308 " int i; \\\n" 309 " int j\n" 310 "int k;", 311 format("int i;\n" 312 "#define A \\\n" 313 " int i ; \\\n" 314 " int j\n" 315 "int k;", 316 8, 0)); // 8: position of "#define". 317 EXPECT_EQ("int i;\n" 318 "#define A \\\n" 319 " int i; \\\n" 320 " int j\n" 321 "int k;", 322 format("int i;\n" 323 "#define A \\\n" 324 " int i ; \\\n" 325 " int j\n" 326 "int k;", 327 45, 0)); // 45: position of "j". 328 } 329 330 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) { 331 EXPECT_EQ("{\n" 332 "{\n" 333 "a;\n" 334 "b;\n" 335 "}\n" 336 "}", 337 format("{\n" 338 "{\n" 339 "a;\n" 340 " b;\n" 341 "}\n" 342 "}", 343 13, 2)); 344 EXPECT_EQ("{\n" 345 "{\n" 346 " a;\n" 347 " b;\n" 348 " c;\n" 349 " d;\n" 350 "}\n" 351 "}", 352 format("{\n" 353 "{\n" 354 " a;\n" 355 " b;\n" 356 " c;\n" 357 " d;\n" 358 "}\n" 359 "}", 360 9, 2)); 361 EXPECT_EQ("{\n" 362 "{\n" 363 "public:\n" 364 " b;\n" 365 "}\n" 366 "}", 367 format("{\n" 368 "{\n" 369 "public:\n" 370 " b;\n" 371 "}\n" 372 "}", 373 17, 2)); 374 EXPECT_EQ("{\n" 375 "{\n" 376 "a;\n" 377 "}\n" 378 "{\n" 379 " b; //\n" 380 "}\n" 381 "}", 382 format("{\n" 383 "{\n" 384 "a;\n" 385 "}\n" 386 "{\n" 387 " b; //\n" 388 "}\n" 389 "}", 390 22, 2)); 391 EXPECT_EQ(" {\n" 392 " a; //\n" 393 " }", 394 format(" {\n" 395 "a; //\n" 396 " }", 397 4, 2)); 398 EXPECT_EQ("void f() {}\n" 399 "void g() {}", 400 format("void f() {}\n" 401 "void g() {}", 402 13, 0)); 403 EXPECT_EQ("int a; // comment\n" 404 " // line 2\n" 405 "int b;", 406 format("int a; // comment\n" 407 " // line 2\n" 408 " int b;", 409 35, 0)); 410 411 EXPECT_EQ(" void f() {\n" 412 "#define A 1\n" 413 " }", 414 format(" void f() {\n" 415 " #define A 1\n" // Format this line. 416 " }", 417 20, 0)); 418 EXPECT_EQ(" void f() {\n" 419 " int i;\n" 420 "#define A \\\n" 421 " int i; \\\n" 422 " int j;\n" 423 " int k;\n" 424 " }", 425 format(" void f() {\n" 426 " int i;\n" 427 "#define A \\\n" 428 " int i; \\\n" 429 " int j;\n" 430 " int k;\n" // Format this line. 431 " }", 432 67, 0)); 433 434 Style.ColumnLimit = 11; 435 EXPECT_EQ(" int a;\n" 436 " void\n" 437 " ffffff() {\n" 438 " }", 439 format(" int a;\n" 440 "void ffffff() {}", 441 11, 0)); 442 } 443 444 TEST_F(FormatTestSelective, UnderstandsTabs) { 445 Style.IndentWidth = 8; 446 Style.UseTab = FormatStyle::UT_Always; 447 Style.AlignEscapedNewlinesLeft = true; 448 EXPECT_EQ("void f() {\n" 449 "\tf();\n" 450 "\tg();\n" 451 "}", 452 format("void f() {\n" 453 "\tf();\n" 454 "\tg();\n" 455 "}", 456 0, 0)); 457 EXPECT_EQ("void f() {\n" 458 "\tf();\n" 459 "\tg();\n" 460 "}", 461 format("void f() {\n" 462 "\tf();\n" 463 "\tg();\n" 464 "}", 465 16, 0)); 466 EXPECT_EQ("void f() {\n" 467 " \tf();\n" 468 "\tg();\n" 469 "}", 470 format("void f() {\n" 471 " \tf();\n" 472 " \tg();\n" 473 "}", 474 21, 0)); 475 } 476 477 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) { 478 EXPECT_EQ( 479 "void f() {\n" 480 " if (a) {\n" 481 " g();\n" 482 " h();\n" 483 "}\n" 484 "\n" 485 "void g() {\n" 486 "}", 487 format("void f() {\n" 488 " if (a) {\n" // Assume this was added without the closing brace. 489 " g();\n" 490 " h();\n" 491 "}\n" 492 "\n" 493 "void g() {\n" // Make sure not to format this. 494 "}", 495 15, 0)); 496 } 497 498 } // end namespace 499 } // end namespace format 500 } // end namespace clang 501