FormatTestSelective.cpp 16 KB

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