FormatTestSelective.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. bool IncompleteFormat = false;
  24. tooling::Replacements Replaces =
  25. reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
  26. EXPECT_FALSE(IncompleteFormat) << 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 aaaaaa; // comment\n"
  108. "int b;\n"
  109. "int c; // unrelated comment",
  110. format("int aaaaaa; // comment\n"
  111. "int b;\n"
  112. "int c; // unrelated comment",
  113. 31, 0));
  114. EXPECT_EQ("int a; // This\n"
  115. " // is\n"
  116. " // a",
  117. format("int a; // This\n"
  118. " // is\n"
  119. " // a",
  120. 0, 0));
  121. EXPECT_EQ("int a; // This\n"
  122. " // is\n"
  123. " // a\n"
  124. "// This is b\n"
  125. "int b;",
  126. format("int a; // This\n"
  127. " // is\n"
  128. " // a\n"
  129. "// This is b\n"
  130. "int b;",
  131. 0, 0));
  132. EXPECT_EQ("int a; // This\n"
  133. " // is\n"
  134. " // a\n"
  135. "\n"
  136. "//This is unrelated",
  137. format("int a; // This\n"
  138. " // is\n"
  139. " // a\n"
  140. "\n"
  141. "//This is unrelated",
  142. 0, 0));
  143. EXPECT_EQ("int a;\n"
  144. "// This is\n"
  145. "// not formatted. ",
  146. format("int a;\n"
  147. "// This is\n"
  148. "// not formatted. ",
  149. 0, 0));
  150. EXPECT_EQ("int x; // Format this line.\n"
  151. "int xx; //\n"
  152. "int xxxxx; //",
  153. format("int x; // Format this line.\n"
  154. "int xx; //\n"
  155. "int xxxxx; //",
  156. 0, 0));
  157. }
  158. TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
  159. EXPECT_EQ("DEBUG({\n"
  160. " int i;\n"
  161. " int j;\n"
  162. "});",
  163. format("DEBUG( {\n"
  164. " int i;\n"
  165. " int j;\n"
  166. "} ) ;",
  167. 20, 1));
  168. EXPECT_EQ("DEBUG( {\n"
  169. " int i;\n"
  170. " int j;\n"
  171. "} ) ;",
  172. format("DEBUG( {\n"
  173. " int i;\n"
  174. " int j;\n"
  175. "} ) ;",
  176. 41, 1));
  177. EXPECT_EQ("DEBUG( {\n"
  178. " int i;\n"
  179. " int j;\n"
  180. "} ) ;",
  181. format("DEBUG( {\n"
  182. " int i;\n"
  183. " int j;\n"
  184. "} ) ;",
  185. 41, 1));
  186. EXPECT_EQ("DEBUG({\n"
  187. " int i;\n"
  188. " int j;\n"
  189. "});",
  190. format("DEBUG( {\n"
  191. " int i;\n"
  192. " int j;\n"
  193. "} ) ;",
  194. 20, 1));
  195. EXPECT_EQ("Debug({\n"
  196. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  197. " return;\n"
  198. " },\n"
  199. " a);",
  200. format("Debug({\n"
  201. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  202. " return;\n"
  203. " },\n"
  204. " a);",
  205. 50, 1));
  206. EXPECT_EQ("DEBUG({\n"
  207. " DEBUG({\n"
  208. " int a;\n"
  209. " int b;\n"
  210. " }) ;\n"
  211. "});",
  212. format("DEBUG({\n"
  213. " DEBUG({\n"
  214. " int a;\n"
  215. " int b;\n" // Format this line only.
  216. " }) ;\n" // Don't touch this line.
  217. "});",
  218. 35, 0));
  219. EXPECT_EQ("DEBUG({\n"
  220. " int a; //\n"
  221. "});",
  222. format("DEBUG({\n"
  223. " int a; //\n"
  224. "});",
  225. 0, 0));
  226. EXPECT_EQ("someFunction(\n"
  227. " [] {\n"
  228. " // Only with this comment.\n"
  229. " int i; // invoke formatting here.\n"
  230. " }, // force line break\n"
  231. " aaa);",
  232. format("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. 63, 1));
  239. EXPECT_EQ("int longlongname; // comment\n"
  240. "int x = f({\n"
  241. " int x; // comment\n"
  242. " int y; // comment\n"
  243. "});",
  244. format("int longlongname; // comment\n"
  245. "int x = f({\n"
  246. " int x; // comment\n"
  247. " int y; // comment\n"
  248. "});",
  249. 65, 0));
  250. EXPECT_EQ("int s = f({\n"
  251. " class X {\n"
  252. " public:\n"
  253. " void f();\n"
  254. " };\n"
  255. "});",
  256. format("int s = f({\n"
  257. " class X {\n"
  258. " public:\n"
  259. " void f();\n"
  260. " };\n"
  261. "});",
  262. 0, 0));
  263. EXPECT_EQ("SomeFunction(\n"
  264. " [] {\n"
  265. " int i;\n"
  266. " return i;\n" // Format this line.
  267. " },\n"
  268. " [] {\n"
  269. " return 2;\n" // Don't fix this.
  270. " });",
  271. format("SomeFunction(\n"
  272. " [] {\n"
  273. " int i;\n"
  274. " return i;\n" // Format this line.
  275. " },\n"
  276. " [] {\n"
  277. " return 2;\n" // Don't fix this.
  278. " });",
  279. 40, 0));
  280. }
  281. TEST_F(FormatTestSelective, WrongIndent) {
  282. EXPECT_EQ("namespace {\n"
  283. "int i;\n"
  284. "int j;\n"
  285. "}",
  286. format("namespace {\n"
  287. " int i;\n" // Format here.
  288. " int j;\n"
  289. "}",
  290. 15, 0));
  291. EXPECT_EQ("namespace {\n"
  292. " int i;\n"
  293. " int j;\n"
  294. "}",
  295. format("namespace {\n"
  296. " int i;\n"
  297. " int j;\n" // Format here.
  298. "}",
  299. 24, 0));
  300. }
  301. TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
  302. Style.AlignEscapedNewlinesLeft = true;
  303. EXPECT_EQ("int i;\n"
  304. "#define A \\\n"
  305. " int i; \\\n"
  306. " int j\n"
  307. "int k;",
  308. format("int i;\n"
  309. "#define A \\\n"
  310. " int i ; \\\n"
  311. " int j\n"
  312. "int k;",
  313. 8, 0)); // 8: position of "#define".
  314. EXPECT_EQ("int i;\n"
  315. "#define A \\\n"
  316. " int i; \\\n"
  317. " int j\n"
  318. "int k;",
  319. format("int i;\n"
  320. "#define A \\\n"
  321. " int i ; \\\n"
  322. " int j\n"
  323. "int k;",
  324. 45, 0)); // 45: position of "j".
  325. }
  326. TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
  327. EXPECT_EQ("{\n"
  328. "{\n"
  329. "a;\n"
  330. "b;\n"
  331. "}\n"
  332. "}",
  333. format("{\n"
  334. "{\n"
  335. "a;\n"
  336. " b;\n"
  337. "}\n"
  338. "}",
  339. 13, 2));
  340. EXPECT_EQ("{\n"
  341. "{\n"
  342. " a;\n"
  343. " b;\n"
  344. " c;\n"
  345. " d;\n"
  346. "}\n"
  347. "}",
  348. format("{\n"
  349. "{\n"
  350. " a;\n"
  351. " b;\n"
  352. " c;\n"
  353. " d;\n"
  354. "}\n"
  355. "}",
  356. 9, 2));
  357. EXPECT_EQ("{\n"
  358. "{\n"
  359. "public:\n"
  360. " b;\n"
  361. "}\n"
  362. "}",
  363. format("{\n"
  364. "{\n"
  365. "public:\n"
  366. " b;\n"
  367. "}\n"
  368. "}",
  369. 17, 2));
  370. EXPECT_EQ("{\n"
  371. "{\n"
  372. "a;\n"
  373. "}\n"
  374. "{\n"
  375. " b; //\n"
  376. "}\n"
  377. "}",
  378. format("{\n"
  379. "{\n"
  380. "a;\n"
  381. "}\n"
  382. "{\n"
  383. " b; //\n"
  384. "}\n"
  385. "}",
  386. 22, 2));
  387. EXPECT_EQ(" {\n"
  388. " a; //\n"
  389. " }",
  390. format(" {\n"
  391. "a; //\n"
  392. " }",
  393. 4, 2));
  394. EXPECT_EQ("void f() {}\n"
  395. "void g() {}",
  396. format("void f() {}\n"
  397. "void g() {}",
  398. 13, 0));
  399. EXPECT_EQ("int a; // comment\n"
  400. " // line 2\n"
  401. "int b;",
  402. format("int a; // comment\n"
  403. " // line 2\n"
  404. " int b;",
  405. 35, 0));
  406. EXPECT_EQ(" void f() {\n"
  407. "#define A 1\n"
  408. " }",
  409. format(" void f() {\n"
  410. " #define A 1\n" // Format this line.
  411. " }",
  412. 20, 0));
  413. EXPECT_EQ(" void f() {\n"
  414. " int i;\n"
  415. "#define A \\\n"
  416. " int i; \\\n"
  417. " int j;\n"
  418. " int k;\n"
  419. " }",
  420. format(" void f() {\n"
  421. " int i;\n"
  422. "#define A \\\n"
  423. " int i; \\\n"
  424. " int j;\n"
  425. " int k;\n" // Format this line.
  426. " }",
  427. 67, 0));
  428. Style.ColumnLimit = 11;
  429. EXPECT_EQ(" int a;\n"
  430. " void\n"
  431. " ffffff() {\n"
  432. " }",
  433. format(" int a;\n"
  434. "void ffffff() {}",
  435. 11, 0));
  436. }
  437. TEST_F(FormatTestSelective, UnderstandsTabs) {
  438. Style.IndentWidth = 8;
  439. Style.UseTab = FormatStyle::UT_Always;
  440. Style.AlignEscapedNewlinesLeft = true;
  441. EXPECT_EQ("void f() {\n"
  442. "\tf();\n"
  443. "\tg();\n"
  444. "}",
  445. format("void f() {\n"
  446. "\tf();\n"
  447. "\tg();\n"
  448. "}",
  449. 0, 0));
  450. EXPECT_EQ("void f() {\n"
  451. "\tf();\n"
  452. "\tg();\n"
  453. "}",
  454. format("void f() {\n"
  455. "\tf();\n"
  456. "\tg();\n"
  457. "}",
  458. 16, 0));
  459. EXPECT_EQ("void f() {\n"
  460. " \tf();\n"
  461. "\tg();\n"
  462. "}",
  463. format("void f() {\n"
  464. " \tf();\n"
  465. " \tg();\n"
  466. "}",
  467. 21, 0));
  468. }
  469. TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
  470. EXPECT_EQ(
  471. "void f() {\n"
  472. " if (a) {\n"
  473. " g();\n"
  474. " h();\n"
  475. "}\n"
  476. "\n"
  477. "void g() {\n"
  478. "}",
  479. format("void f() {\n"
  480. " if (a) {\n" // Assume this was added without the closing brace.
  481. " g();\n"
  482. " h();\n"
  483. "}\n"
  484. "\n"
  485. "void g() {\n" // Make sure not to format this.
  486. "}",
  487. 15, 0));
  488. }
  489. TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) {
  490. Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  491. EXPECT_EQ(
  492. "var x = \"a\";\n"
  493. "var x = 'a';\n"
  494. "var x = \"a\";",
  495. format("var x = \"a\";\n"
  496. "var x = \"a\";\n"
  497. "var x = \"a\";",
  498. 20, 0));
  499. }
  500. } // end namespace
  501. } // end namespace format
  502. } // end namespace clang