HeaderIncludesTest.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. //===- unittest/Tooling/CleanupTest.cpp - Include insertion/deletion tests ===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/Tooling/Inclusions/HeaderIncludes.h"
  9. #include "../Tooling/ReplacementTest.h"
  10. #include "../Tooling/RewriterTestContext.h"
  11. #include "clang/Format/Format.h"
  12. #include "clang/Tooling/Core/Replacement.h"
  13. #include "gtest/gtest.h"
  14. namespace clang {
  15. namespace tooling {
  16. namespace {
  17. class HeaderIncludesTest : public ::testing::Test {
  18. protected:
  19. std::string insert(llvm::StringRef Code, llvm::StringRef Header) {
  20. HeaderIncludes Includes(FileName, Code, Style);
  21. assert(Header.startswith("\"") || Header.startswith("<"));
  22. auto R = Includes.insert(Header.trim("\"<>"), Header.startswith("<"));
  23. if (!R)
  24. return Code;
  25. auto Result = applyAllReplacements(Code, Replacements(*R));
  26. EXPECT_TRUE(static_cast<bool>(Result));
  27. return *Result;
  28. }
  29. std::string remove(llvm::StringRef Code, llvm::StringRef Header) {
  30. HeaderIncludes Includes(FileName, Code, Style);
  31. assert(Header.startswith("\"") || Header.startswith("<"));
  32. auto Replaces = Includes.remove(Header.trim("\"<>"), Header.startswith("<"));
  33. auto Result = applyAllReplacements(Code, Replaces);
  34. EXPECT_TRUE(static_cast<bool>(Result));
  35. return *Result;
  36. }
  37. const std::string FileName = "fix.cpp";
  38. IncludeStyle Style = format::getLLVMStyle().IncludeStyle;
  39. };
  40. TEST_F(HeaderIncludesTest, NoExistingIncludeWithoutDefine) {
  41. std::string Code = "int main() {}";
  42. std::string Expected = "#include \"a.h\"\n"
  43. "int main() {}";
  44. EXPECT_EQ(Expected, insert(Code, "\"a.h\""));
  45. }
  46. TEST_F(HeaderIncludesTest, NoExistingIncludeWithDefine) {
  47. std::string Code = "#ifndef A_H\n"
  48. "#define A_H\n"
  49. "class A {};\n"
  50. "#define MMM 123\n"
  51. "#endif";
  52. std::string Expected = "#ifndef A_H\n"
  53. "#define A_H\n"
  54. "#include \"b.h\"\n"
  55. "class A {};\n"
  56. "#define MMM 123\n"
  57. "#endif";
  58. EXPECT_EQ(Expected, insert(Code, "\"b.h\""));
  59. }
  60. TEST_F(HeaderIncludesTest, InsertBeforeCategoryWithLowerPriority) {
  61. std::string Code = "#ifndef A_H\n"
  62. "#define A_H\n"
  63. "\n"
  64. "\n"
  65. "\n"
  66. "#include <vector>\n"
  67. "class A {};\n"
  68. "#define MMM 123\n"
  69. "#endif";
  70. std::string Expected = "#ifndef A_H\n"
  71. "#define A_H\n"
  72. "\n"
  73. "\n"
  74. "\n"
  75. "#include \"a.h\"\n"
  76. "#include <vector>\n"
  77. "class A {};\n"
  78. "#define MMM 123\n"
  79. "#endif";
  80. EXPECT_EQ(Expected, insert(Code, "\"a.h\""));
  81. }
  82. TEST_F(HeaderIncludesTest, InsertAfterMainHeader) {
  83. std::string Code = "#include \"fix.h\"\n"
  84. "\n"
  85. "int main() {}";
  86. std::string Expected = "#include \"fix.h\"\n"
  87. "#include <a>\n"
  88. "\n"
  89. "int main() {}";
  90. Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
  91. .IncludeStyle;
  92. EXPECT_EQ(Expected, insert(Code, "<a>"));
  93. }
  94. TEST_F(HeaderIncludesTest, InsertBeforeSystemHeaderLLVM) {
  95. std::string Code = "#include <memory>\n"
  96. "\n"
  97. "int main() {}";
  98. std::string Expected = "#include \"z.h\"\n"
  99. "#include <memory>\n"
  100. "\n"
  101. "int main() {}";
  102. EXPECT_EQ(Expected, insert(Code, "\"z.h\""));
  103. }
  104. TEST_F(HeaderIncludesTest, InsertAfterSystemHeaderGoogle) {
  105. std::string Code = "#include <memory>\n"
  106. "\n"
  107. "int main() {}";
  108. std::string Expected = "#include <memory>\n"
  109. "#include \"z.h\"\n"
  110. "\n"
  111. "int main() {}";
  112. Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
  113. .IncludeStyle;
  114. EXPECT_EQ(Expected, insert(Code, "\"z.h\""));
  115. }
  116. TEST_F(HeaderIncludesTest, InsertOneIncludeLLVMStyle) {
  117. std::string Code = "#include \"x/fix.h\"\n"
  118. "#include \"a.h\"\n"
  119. "#include \"b.h\"\n"
  120. "#include \"clang/Format/Format.h\"\n"
  121. "#include <memory>\n";
  122. std::string Expected = "#include \"x/fix.h\"\n"
  123. "#include \"a.h\"\n"
  124. "#include \"b.h\"\n"
  125. "#include \"clang/Format/Format.h\"\n"
  126. "#include \"llvm/x/y.h\"\n"
  127. "#include <memory>\n";
  128. EXPECT_EQ(Expected, insert(Code, "\"llvm/x/y.h\""));
  129. }
  130. TEST_F(HeaderIncludesTest, InsertIntoBlockSorted) {
  131. std::string Code = "#include \"x/fix.h\"\n"
  132. "#include \"a.h\"\n"
  133. "#include \"c.h\"\n"
  134. "#include <memory>\n";
  135. std::string Expected = "#include \"x/fix.h\"\n"
  136. "#include \"a.h\"\n"
  137. "#include \"b.h\"\n"
  138. "#include \"c.h\"\n"
  139. "#include <memory>\n";
  140. EXPECT_EQ(Expected, insert(Code, "\"b.h\""));
  141. }
  142. TEST_F(HeaderIncludesTest, InsertIntoFirstBlockOfSameKind) {
  143. std::string Code = "#include \"x/fix.h\"\n"
  144. "#include \"c.h\"\n"
  145. "#include \"e.h\"\n"
  146. "#include \"f.h\"\n"
  147. "#include <memory>\n"
  148. "#include <vector>\n"
  149. "#include \"m.h\"\n"
  150. "#include \"n.h\"\n";
  151. std::string Expected = "#include \"x/fix.h\"\n"
  152. "#include \"c.h\"\n"
  153. "#include \"d.h\"\n"
  154. "#include \"e.h\"\n"
  155. "#include \"f.h\"\n"
  156. "#include <memory>\n"
  157. "#include <vector>\n"
  158. "#include \"m.h\"\n"
  159. "#include \"n.h\"\n";
  160. EXPECT_EQ(Expected, insert(Code, "\"d.h\""));
  161. }
  162. TEST_F(HeaderIncludesTest, InsertIntoSystemBlockSorted) {
  163. std::string Code = "#include \"x/fix.h\"\n"
  164. "#include \"a.h\"\n"
  165. "#include \"c.h\"\n"
  166. "#include <a>\n"
  167. "#include <z>\n";
  168. std::string Expected = "#include \"x/fix.h\"\n"
  169. "#include \"a.h\"\n"
  170. "#include \"c.h\"\n"
  171. "#include <a>\n"
  172. "#include <vector>\n"
  173. "#include <z>\n";
  174. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  175. }
  176. TEST_F(HeaderIncludesTest, InsertNewSystemIncludeGoogleStyle) {
  177. std::string Code = "#include \"x/fix.h\"\n"
  178. "\n"
  179. "#include \"y/a.h\"\n"
  180. "#include \"z/b.h\"\n";
  181. // FIXME: inserting after the empty line following the main header might be
  182. // preferred.
  183. std::string Expected = "#include \"x/fix.h\"\n"
  184. "#include <vector>\n"
  185. "\n"
  186. "#include \"y/a.h\"\n"
  187. "#include \"z/b.h\"\n";
  188. Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
  189. .IncludeStyle;
  190. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  191. }
  192. TEST_F(HeaderIncludesTest, NotConfusedByDefine) {
  193. std::string Code = "void f() {}\n"
  194. "#define A \\\n"
  195. " int i;";
  196. std::string Expected = "#include <vector>\n"
  197. "void f() {}\n"
  198. "#define A \\\n"
  199. " int i;";
  200. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  201. }
  202. TEST_F(HeaderIncludesTest, SkippedTopComment) {
  203. std::string Code = "// comment\n"
  204. "\n"
  205. " // comment\n";
  206. std::string Expected = "// comment\n"
  207. "\n"
  208. " // comment\n"
  209. "#include <vector>\n";
  210. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  211. }
  212. TEST_F(HeaderIncludesTest, SkippedMixedComments) {
  213. std::string Code = "// comment\n"
  214. "// comment \\\n"
  215. " comment continued\n"
  216. "/*\n"
  217. "* comment\n"
  218. "*/\n";
  219. std::string Expected = "// comment\n"
  220. "// comment \\\n"
  221. " comment continued\n"
  222. "/*\n"
  223. "* comment\n"
  224. "*/\n"
  225. "#include <vector>\n";
  226. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  227. }
  228. TEST_F(HeaderIncludesTest, MultipleBlockCommentsInOneLine) {
  229. std::string Code = "/*\n"
  230. "* comment\n"
  231. "*/ /* comment\n"
  232. "*/\n"
  233. "\n\n"
  234. "/* c1 */ /*c2 */\n";
  235. std::string Expected = "/*\n"
  236. "* comment\n"
  237. "*/ /* comment\n"
  238. "*/\n"
  239. "\n\n"
  240. "/* c1 */ /*c2 */\n"
  241. "#include <vector>\n";
  242. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  243. }
  244. TEST_F(HeaderIncludesTest, CodeAfterComments) {
  245. std::string Code = "/*\n"
  246. "* comment\n"
  247. "*/ /* comment\n"
  248. "*/\n"
  249. "\n\n"
  250. "/* c1 */ /*c2 */\n"
  251. "\n"
  252. "int x;\n";
  253. std::string Expected = "/*\n"
  254. "* comment\n"
  255. "*/ /* comment\n"
  256. "*/\n"
  257. "\n\n"
  258. "/* c1 */ /*c2 */\n"
  259. "\n"
  260. "#include <vector>\n"
  261. "int x;\n";
  262. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  263. }
  264. TEST_F(HeaderIncludesTest, FakeHeaderGuardIfDef) {
  265. std::string Code = "// comment \n"
  266. "#ifdef X\n"
  267. "#define X\n";
  268. std::string Expected = "// comment \n"
  269. "#include <vector>\n"
  270. "#ifdef X\n"
  271. "#define X\n";
  272. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  273. }
  274. TEST_F(HeaderIncludesTest, RealHeaderGuardAfterComments) {
  275. std::string Code = "// comment \n"
  276. "#ifndef X\n"
  277. "#define X\n"
  278. "int x;\n"
  279. "#define Y 1\n";
  280. std::string Expected = "// comment \n"
  281. "#ifndef X\n"
  282. "#define X\n"
  283. "#include <vector>\n"
  284. "int x;\n"
  285. "#define Y 1\n";
  286. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  287. }
  288. TEST_F(HeaderIncludesTest, PragmaOnce) {
  289. std::string Code = "// comment \n"
  290. "#pragma once\n"
  291. "int x;\n";
  292. std::string Expected = "// comment \n"
  293. "#pragma once\n"
  294. "#include <vector>\n"
  295. "int x;\n";
  296. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  297. }
  298. TEST_F(HeaderIncludesTest, IfNDefWithNoDefine) {
  299. std::string Code = "// comment \n"
  300. "#ifndef X\n"
  301. "int x;\n"
  302. "#define Y 1\n";
  303. std::string Expected = "// comment \n"
  304. "#include <vector>\n"
  305. "#ifndef X\n"
  306. "int x;\n"
  307. "#define Y 1\n";
  308. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  309. }
  310. TEST_F(HeaderIncludesTest, FakeHeaderGuard) {
  311. std::string Code = "// comment \n"
  312. "#ifndef X\n"
  313. "#define 1\n";
  314. std::string Expected = "// comment \n"
  315. "#include <vector>\n"
  316. "#ifndef X\n"
  317. "#define 1\n";
  318. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  319. }
  320. TEST_F(HeaderIncludesTest, HeaderGuardWithComment) {
  321. std::string Code = "// comment \n"
  322. "#ifndef X // comment\n"
  323. "// comment\n"
  324. "/* comment\n"
  325. "*/\n"
  326. "/* comment */ #define X\n"
  327. "int x;\n"
  328. "#define Y 1\n";
  329. std::string Expected = "// comment \n"
  330. "#ifndef X // comment\n"
  331. "// comment\n"
  332. "/* comment\n"
  333. "*/\n"
  334. "/* comment */ #define X\n"
  335. "#include <vector>\n"
  336. "int x;\n"
  337. "#define Y 1\n";
  338. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  339. }
  340. TEST_F(HeaderIncludesTest, EmptyCode) {
  341. std::string Code = "";
  342. std::string Expected = "#include <vector>\n";
  343. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  344. }
  345. TEST_F(HeaderIncludesTest, NoNewLineAtTheEndOfCode) {
  346. std::string Code = "#include <map>";
  347. std::string Expected = "#include <map>\n#include <vector>\n";
  348. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  349. }
  350. TEST_F(HeaderIncludesTest, SkipExistingHeaders) {
  351. std::string Code = "#include \"a.h\"\n"
  352. "#include <vector>\n";
  353. std::string Expected = "#include \"a.h\"\n"
  354. "#include <vector>\n";
  355. EXPECT_EQ(Expected, insert(Code, "<vector>"));
  356. EXPECT_EQ(Expected, insert(Code, "\"a.h\""));
  357. }
  358. TEST_F(HeaderIncludesTest, AddIncludesWithDifferentForms) {
  359. std::string Code = "#include <vector>\n";
  360. // FIXME: this might not be the best behavior.
  361. std::string Expected = "#include \"vector\"\n"
  362. "#include <vector>\n";
  363. EXPECT_EQ(Expected, insert(Code, "\"vector\""));
  364. }
  365. TEST_F(HeaderIncludesTest, NoInsertionAfterCode) {
  366. std::string Code = "#include \"a.h\"\n"
  367. "void f() {}\n"
  368. "#include \"b.h\"\n";
  369. std::string Expected = "#include \"a.h\"\n"
  370. "#include \"c.h\"\n"
  371. "void f() {}\n"
  372. "#include \"b.h\"\n";
  373. EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
  374. }
  375. TEST_F(HeaderIncludesTest, NoInsertionInStringLiteral) {
  376. std::string Code = "#include \"a.h\"\n"
  377. "const char[] = R\"(\n"
  378. "#include \"b.h\"\n"
  379. ")\";\n";
  380. std::string Expected = "#include \"a.h\"\n"
  381. "#include \"c.h\"\n"
  382. "const char[] = R\"(\n"
  383. "#include \"b.h\"\n"
  384. ")\";\n";
  385. EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
  386. }
  387. TEST_F(HeaderIncludesTest, NoInsertionAfterOtherDirective) {
  388. std::string Code = "#include \"a.h\"\n"
  389. "#ifdef X\n"
  390. "#include \"b.h\"\n"
  391. "#endif\n";
  392. std::string Expected = "#include \"a.h\"\n"
  393. "#include \"c.h\"\n"
  394. "#ifdef X\n"
  395. "#include \"b.h\"\n"
  396. "#endif\n";
  397. EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
  398. }
  399. TEST_F(HeaderIncludesTest, CanInsertAfterLongSystemInclude) {
  400. std::string Code = "#include \"a.h\"\n"
  401. "// comment\n\n"
  402. "#include <a/b/c/d/e.h>\n";
  403. std::string Expected = "#include \"a.h\"\n"
  404. "// comment\n\n"
  405. "#include <a/b/c/d/e.h>\n"
  406. "#include <x.h>\n";
  407. EXPECT_EQ(Expected, insert(Code, "<x.h>"));
  408. }
  409. TEST_F(HeaderIncludesTest, CanInsertAfterComment) {
  410. std::string Code = "#include \"a.h\"\n"
  411. "// Comment\n"
  412. "\n"
  413. "/* Comment */\n"
  414. "// Comment\n"
  415. "\n"
  416. "#include \"b.h\"\n";
  417. std::string Expected = "#include \"a.h\"\n"
  418. "// Comment\n"
  419. "\n"
  420. "/* Comment */\n"
  421. "// Comment\n"
  422. "\n"
  423. "#include \"b.h\"\n"
  424. "#include \"c.h\"\n";
  425. EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
  426. }
  427. TEST_F(HeaderIncludesTest, LongCommentsInTheBeginningOfFile) {
  428. std::string Code = "// Loooooooooooooooooooooooooong comment\n"
  429. "// Loooooooooooooooooooooooooong comment\n"
  430. "// Loooooooooooooooooooooooooong comment\n"
  431. "#include <string>\n"
  432. "#include <vector>\n"
  433. "\n"
  434. "#include \"a.h\"\n"
  435. "#include \"b.h\"\n";
  436. std::string Expected = "// Loooooooooooooooooooooooooong comment\n"
  437. "// Loooooooooooooooooooooooooong comment\n"
  438. "// Loooooooooooooooooooooooooong comment\n"
  439. "#include <string>\n"
  440. "#include <vector>\n"
  441. "\n"
  442. "#include \"a.h\"\n"
  443. "#include \"b.h\"\n"
  444. "#include \"third.h\"\n";
  445. Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
  446. .IncludeStyle;
  447. EXPECT_EQ(Expected, insert(Code, "\"third.h\""));
  448. }
  449. TEST_F(HeaderIncludesTest, SimpleDeleteInclude) {
  450. std::string Code = "#include \"abc.h\"\n"
  451. "#include \"xyz.h\" // comment\n"
  452. "int x;\n";
  453. std::string Expected = "#include \"abc.h\"\n"
  454. "int x;\n";
  455. EXPECT_EQ(Expected, remove(Code, "\"xyz.h\""));
  456. }
  457. TEST_F(HeaderIncludesTest, DeleteQuotedOnly) {
  458. std::string Code = "#include \"abc.h\"\n"
  459. "#include <abc.h>\n"
  460. "int x;\n";
  461. std::string Expected = "#include <abc.h>\n"
  462. "int x;\n";
  463. EXPECT_EQ(Expected, remove(Code, "\"abc.h\""));
  464. }
  465. TEST_F(HeaderIncludesTest, DeleteAllCode) {
  466. std::string Code = "#include \"xyz.h\"\n";
  467. std::string Expected = "";
  468. EXPECT_EQ(Expected, remove(Code, "\"xyz.h\""));
  469. }
  470. TEST_F(HeaderIncludesTest, DeleteOnlyIncludesWithSameQuote) {
  471. std::string Code = "#include \"xyz.h\"\n"
  472. "#include \"xyz\"\n"
  473. "#include <xyz.h>\n";
  474. std::string Expected = "#include \"xyz.h\"\n"
  475. "#include \"xyz\"\n";
  476. EXPECT_EQ(Expected, remove(Code, "<xyz.h>"));
  477. }
  478. TEST_F(HeaderIncludesTest, CanDeleteAfterCode) {
  479. std::string Code = "#include \"a.h\"\n"
  480. "void f() {}\n"
  481. "#include \"b.h\"\n";
  482. std::string Expected = "#include \"a.h\"\n"
  483. "void f() {}\n";
  484. EXPECT_EQ(Expected, remove(Code, "\"b.h\""));
  485. }
  486. } // namespace
  487. } // namespace tooling
  488. } // namespace clang