FormatTestSelective.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. std::string Result = applyAllReplacements(Code, Replaces);
  28. EXPECT_NE("", 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;\nint a;", format("int b;\n int a;", 7, 0));
  42. EXPECT_EQ("int b;\n int a;", format("int b;\n int a;", 6, 0));
  43. Style.ColumnLimit = 12;
  44. EXPECT_EQ("#define A \\\n"
  45. " int a; \\\n"
  46. " int b;",
  47. format("#define A \\\n"
  48. " int a; \\\n"
  49. " int b;",
  50. 26, 0));
  51. EXPECT_EQ("#define A \\\n"
  52. " int a; \\\n"
  53. " int b;",
  54. format("#define A \\\n"
  55. " int a; \\\n"
  56. " int b;",
  57. 25, 0));
  58. }
  59. TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) {
  60. EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0));
  61. EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0));
  62. // This might not strictly be correct, but is likely good in all practical
  63. // cases.
  64. EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0));
  65. }
  66. TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
  67. EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0));
  68. EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0));
  69. }
  70. TEST_F(FormatTestSelective, ReformatsMovedLines) {
  71. EXPECT_EQ(
  72. "template <typename T> T *getFETokenInfo() const {\n"
  73. " return static_cast<T *>(FETokenInfo);\n"
  74. "}\n"
  75. " int a; // <- Should not be formatted",
  76. format(
  77. "template<typename T>\n"
  78. "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
  79. " int a; // <- Should not be formatted",
  80. 9, 5));
  81. }
  82. TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) {
  83. Style.AllowShortIfStatementsOnASingleLine = true;
  84. EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
  85. EXPECT_EQ("if (a) return; // comment",
  86. format("if(a)\nreturn; // comment", 20, 1));
  87. }
  88. TEST_F(FormatTestSelective, FormatsCommentsLocally) {
  89. EXPECT_EQ("int a; // comment\n"
  90. "int b; // comment",
  91. format("int a; // comment\n"
  92. "int b; // comment",
  93. 0, 0));
  94. EXPECT_EQ("int a; // comment\n"
  95. " // line 2\n"
  96. "int b;",
  97. format("int a; // comment\n"
  98. " // line 2\n"
  99. "int b;",
  100. 28, 0));
  101. EXPECT_EQ("int aaaaaa; // comment\n"
  102. "int b;\n"
  103. "int c; // unrelated comment",
  104. format("int aaaaaa; // comment\n"
  105. "int b;\n"
  106. "int c; // unrelated comment",
  107. 31, 0));
  108. EXPECT_EQ("int a; // This\n"
  109. " // is\n"
  110. " // a",
  111. format("int a; // This\n"
  112. " // is\n"
  113. " // a",
  114. 0, 0));
  115. EXPECT_EQ("int a; // This\n"
  116. " // is\n"
  117. " // a\n"
  118. "// This is b\n"
  119. "int b;",
  120. format("int a; // This\n"
  121. " // is\n"
  122. " // a\n"
  123. "// This is b\n"
  124. "int b;",
  125. 0, 0));
  126. EXPECT_EQ("int a; // This\n"
  127. " // is\n"
  128. " // a\n"
  129. "\n"
  130. " // This is unrelated",
  131. format("int a; // This\n"
  132. " // is\n"
  133. " // a\n"
  134. "\n"
  135. " // This is unrelated",
  136. 0, 0));
  137. EXPECT_EQ("int a;\n"
  138. "// This is\n"
  139. "// not formatted. ",
  140. format("int a;\n"
  141. "// This is\n"
  142. "// not formatted. ",
  143. 0, 0));
  144. }
  145. TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
  146. EXPECT_EQ("DEBUG({\n"
  147. " int i;\n"
  148. " int j;\n"
  149. "});",
  150. format("DEBUG( {\n"
  151. " int i;\n"
  152. " int j;\n"
  153. "} ) ;",
  154. 20, 1));
  155. EXPECT_EQ("DEBUG( {\n"
  156. " int i;\n"
  157. " int j;\n"
  158. "} ) ;",
  159. format("DEBUG( {\n"
  160. " int i;\n"
  161. " int j;\n"
  162. "} ) ;",
  163. 41, 1));
  164. EXPECT_EQ("DEBUG( {\n"
  165. " int i;\n"
  166. " int j;\n"
  167. "} ) ;",
  168. format("DEBUG( {\n"
  169. " int i;\n"
  170. " int j;\n"
  171. "} ) ;",
  172. 41, 1));
  173. EXPECT_EQ("DEBUG({\n"
  174. " int i;\n"
  175. " int j;\n"
  176. "});",
  177. format("DEBUG( {\n"
  178. " int i;\n"
  179. " int j;\n"
  180. "} ) ;",
  181. 20, 1));
  182. EXPECT_EQ("Debug({\n"
  183. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  184. " return;\n"
  185. " },\n"
  186. " a);",
  187. format("Debug({\n"
  188. " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
  189. " return;\n"
  190. " },\n"
  191. " a);",
  192. 50, 1));
  193. EXPECT_EQ("DEBUG({\n"
  194. " DEBUG({\n"
  195. " int a;\n"
  196. " int b;\n"
  197. " }) ;\n"
  198. "});",
  199. format("DEBUG({\n"
  200. " DEBUG({\n"
  201. " int a;\n"
  202. " int b;\n" // Format this line only.
  203. " }) ;\n" // Don't touch this line.
  204. "});",
  205. 35, 0));
  206. EXPECT_EQ("DEBUG({\n"
  207. " int a; //\n"
  208. "});",
  209. format("DEBUG({\n"
  210. " int a; //\n"
  211. "});",
  212. 0, 0));
  213. EXPECT_EQ("someFunction(\n"
  214. " [] {\n"
  215. " // Only with this comment.\n"
  216. " int i; // invoke formatting here.\n"
  217. " }, // force line break\n"
  218. " aaa);",
  219. format("someFunction(\n"
  220. " [] {\n"
  221. " // Only with this comment.\n"
  222. " int i; // invoke formatting here.\n"
  223. " }, // force line break\n"
  224. " aaa);",
  225. 63, 1));
  226. EXPECT_EQ("int longlongname; // comment\n"
  227. "int x = f({\n"
  228. " int x; // comment\n"
  229. " int y; // comment\n"
  230. "});",
  231. format("int longlongname; // comment\n"
  232. "int x = f({\n"
  233. " int x; // comment\n"
  234. " int y; // comment\n"
  235. "});",
  236. 65, 0));
  237. EXPECT_EQ("int s = f({\n"
  238. " class X {\n"
  239. " public:\n"
  240. " void f();\n"
  241. " };\n"
  242. "});",
  243. format("int s = f({\n"
  244. " class X {\n"
  245. " public:\n"
  246. " void f();\n"
  247. " };\n"
  248. "});",
  249. 0, 0));
  250. }
  251. TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
  252. Style.AlignEscapedNewlinesLeft = true;
  253. EXPECT_EQ("int i;\n"
  254. "#define A \\\n"
  255. " int i; \\\n"
  256. " int j\n"
  257. "int k;",
  258. format("int i;\n"
  259. "#define A \\\n"
  260. " int i ; \\\n"
  261. " int j\n"
  262. "int k;",
  263. 8, 0)); // 8: position of "#define".
  264. EXPECT_EQ("int i;\n"
  265. "#define A \\\n"
  266. " int i; \\\n"
  267. " int j\n"
  268. "int k;",
  269. format("int i;\n"
  270. "#define A \\\n"
  271. " int i ; \\\n"
  272. " int j\n"
  273. "int k;",
  274. 45, 0)); // 45: position of "j".
  275. }
  276. TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
  277. EXPECT_EQ("{\n"
  278. "{\n"
  279. "a;\n"
  280. "b;\n"
  281. "}\n"
  282. "}",
  283. format("{\n"
  284. "{\n"
  285. "a;\n"
  286. " b;\n"
  287. "}\n"
  288. "}",
  289. 13, 2));
  290. EXPECT_EQ("{\n"
  291. "{\n"
  292. " a;\n"
  293. "b;\n"
  294. "}\n"
  295. "}",
  296. format("{\n"
  297. "{\n"
  298. " a;\n"
  299. "b;\n"
  300. "}\n"
  301. "}",
  302. 9, 2));
  303. EXPECT_EQ("{\n"
  304. "{\n"
  305. "public:\n"
  306. " b;\n"
  307. "}\n"
  308. "}",
  309. format("{\n"
  310. "{\n"
  311. "public:\n"
  312. " b;\n"
  313. "}\n"
  314. "}",
  315. 17, 2));
  316. EXPECT_EQ("{\n"
  317. "{\n"
  318. "a;\n"
  319. "}\n"
  320. "{\n"
  321. " b; //\n"
  322. "}\n"
  323. "}",
  324. format("{\n"
  325. "{\n"
  326. "a;\n"
  327. "}\n"
  328. "{\n"
  329. " b; //\n"
  330. "}\n"
  331. "}",
  332. 22, 2));
  333. EXPECT_EQ(" {\n"
  334. " a; //\n"
  335. " }",
  336. format(" {\n"
  337. "a; //\n"
  338. " }",
  339. 4, 2));
  340. EXPECT_EQ("void f() {}\n"
  341. "void g() {}",
  342. format("void f() {}\n"
  343. "void g() {}",
  344. 13, 0));
  345. EXPECT_EQ("int a; // comment\n"
  346. " // line 2\n"
  347. "int b;",
  348. format("int a; // comment\n"
  349. " // line 2\n"
  350. " int b;",
  351. 35, 0));
  352. EXPECT_EQ(" void f() {\n"
  353. "#define A 1\n"
  354. " }",
  355. format(" void f() {\n"
  356. " #define A 1\n" // Format this line.
  357. " }",
  358. 20, 0));
  359. EXPECT_EQ(" void f() {\n"
  360. " int i;\n"
  361. "#define A \\\n"
  362. " int i; \\\n"
  363. " int j;\n"
  364. " int k;\n"
  365. " }",
  366. format(" void f() {\n"
  367. " int i;\n"
  368. "#define A \\\n"
  369. " int i; \\\n"
  370. " int j;\n"
  371. " int k;\n" // Format this line.
  372. " }",
  373. 67, 0));
  374. Style.ColumnLimit = 11;
  375. EXPECT_EQ(" int a;\n"
  376. " void\n"
  377. " ffffff() {\n"
  378. " }",
  379. format(" int a;\n"
  380. "void ffffff() {}",
  381. 11, 0));
  382. }
  383. TEST_F(FormatTestSelective, UnderstandsTabs) {
  384. Style.IndentWidth = 8;
  385. Style.UseTab = FormatStyle::UT_Always;
  386. Style.AlignEscapedNewlinesLeft = true;
  387. EXPECT_EQ("void f() {\n"
  388. "\tf();\n"
  389. "\tg();\n"
  390. "}",
  391. format("void f() {\n"
  392. "\tf();\n"
  393. "\tg();\n"
  394. "}",
  395. 0, 0));
  396. EXPECT_EQ("void f() {\n"
  397. "\tf();\n"
  398. "\tg();\n"
  399. "}",
  400. format("void f() {\n"
  401. "\tf();\n"
  402. "\tg();\n"
  403. "}",
  404. 16, 0));
  405. EXPECT_EQ("void f() {\n"
  406. " \tf();\n"
  407. "\tg();\n"
  408. "}",
  409. format("void f() {\n"
  410. " \tf();\n"
  411. " \tg();\n"
  412. "}",
  413. 21, 0));
  414. }
  415. } // end namespace
  416. } // end namespace format
  417. } // end namespace clang