HeaderIncludesTest.cpp 19 KB

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