CleanupTest.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. //===- unittest/Format/CleanupTest.cpp - Code cleanup 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 "clang/Format/Format.h"
  10. #include "../Tooling/ReplacementTest.h"
  11. #include "../Tooling/RewriterTestContext.h"
  12. #include "clang/Tooling/Core/Replacement.h"
  13. #include "gtest/gtest.h"
  14. using clang::tooling::ReplacementTest;
  15. using clang::tooling::toReplacements;
  16. namespace clang {
  17. namespace format {
  18. namespace {
  19. class CleanupTest : public ::testing::Test {
  20. protected:
  21. std::string cleanup(llvm::StringRef Code,
  22. const std::vector<tooling::Range> &Ranges,
  23. const FormatStyle &Style = getLLVMStyle()) {
  24. tooling::Replacements Replaces = format::cleanup(Style, Code, Ranges);
  25. auto Result = applyAllReplacements(Code, Replaces);
  26. EXPECT_TRUE(static_cast<bool>(Result));
  27. return *Result;
  28. }
  29. // Returns code after cleanup around \p Offsets.
  30. std::string cleanupAroundOffsets(llvm::ArrayRef<unsigned> Offsets,
  31. llvm::StringRef Code) {
  32. std::vector<tooling::Range> Ranges;
  33. for (auto Offset : Offsets)
  34. Ranges.push_back(tooling::Range(Offset, 0));
  35. return cleanup(Code, Ranges);
  36. }
  37. };
  38. TEST_F(CleanupTest, DeleteEmptyNamespaces) {
  39. std::string Code = "namespace A {\n"
  40. "namespace B {\n"
  41. "} // namespace B\n"
  42. "} // namespace A\n\n"
  43. "namespace C {\n"
  44. "namespace D { int i; }\n"
  45. "inline namespace E { namespace { } }\n"
  46. "}";
  47. std::string Expected = "\n\n\n\n\nnamespace C {\n"
  48. "namespace D { int i; }\n \n"
  49. "}";
  50. EXPECT_EQ(Expected, cleanupAroundOffsets({28, 91, 132}, Code));
  51. }
  52. TEST_F(CleanupTest, NamespaceWithSyntaxError) {
  53. std::string Code = "namespace A {\n"
  54. "namespace B {\n" // missing r_brace
  55. "} // namespace A\n\n"
  56. "namespace C {\n"
  57. "namespace D int i; }\n"
  58. "inline namespace E { namespace { } }\n"
  59. "}";
  60. std::string Expected = "namespace A {\n"
  61. "\n\n\nnamespace C {\n"
  62. "namespace D int i; }\n \n"
  63. "}";
  64. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  65. EXPECT_EQ(Expected, cleanup(Code, Ranges));
  66. }
  67. TEST_F(CleanupTest, EmptyNamespaceNotAffected) {
  68. std::string Code = "namespace A {\n\n"
  69. "namespace {\n\n}}";
  70. // Even though the namespaces are empty, but the inner most empty namespace
  71. // block is not affected by the changed ranges.
  72. std::string Expected = "namespace A {\n\n"
  73. "namespace {\n\n}}";
  74. // Set the changed range to be the second "\n".
  75. EXPECT_EQ(Expected, cleanupAroundOffsets({14}, Code));
  76. }
  77. TEST_F(CleanupTest, EmptyNamespaceWithCommentsNoBreakBeforeBrace) {
  78. std::string Code = "namespace A {\n"
  79. "namespace B {\n"
  80. "// Yo\n"
  81. "} // namespace B\n"
  82. "} // namespace A\n"
  83. "namespace C { // Yo\n"
  84. "}";
  85. std::string Expected = "\n\n\n\n\n\n";
  86. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  87. std::string Result = cleanup(Code, Ranges);
  88. EXPECT_EQ(Expected, Result);
  89. }
  90. TEST_F(CleanupTest, EmptyNamespaceWithCommentsBreakBeforeBrace) {
  91. std::string Code = "namespace A\n"
  92. "/* Yo */ {\n"
  93. "namespace B\n"
  94. "{\n"
  95. "// Yo\n"
  96. "} // namespace B\n"
  97. "} // namespace A\n"
  98. "namespace C\n"
  99. "{ // Yo\n"
  100. "}\n";
  101. std::string Expected = "\n\n\n\n\n\n\n\n\n\n";
  102. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  103. FormatStyle Style = getLLVMStyle();
  104. Style.BraceWrapping.AfterNamespace = true;
  105. std::string Result = cleanup(Code, Ranges, Style);
  106. EXPECT_EQ(Expected, Result);
  107. }
  108. TEST_F(CleanupTest, CtorInitializationSimpleRedundantComma) {
  109. std::string Code = "class A {\nA() : , {} };";
  110. std::string Expected = "class A {\nA() {} };";
  111. EXPECT_EQ(Expected, cleanupAroundOffsets({17, 19}, Code));
  112. Code = "class A {\nA() : x(1), {} };";
  113. Expected = "class A {\nA() : x(1) {} };";
  114. EXPECT_EQ(Expected, cleanupAroundOffsets({23}, Code));
  115. Code = "class A {\nA() :,,,,{} };";
  116. Expected = "class A {\nA() {} };";
  117. EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
  118. }
  119. TEST_F(CleanupTest, ListRedundantComma) {
  120. std::string Code = "void f() { std::vector<int> v = {1,2,,,3,{4,5}}; }";
  121. std::string Expected = "void f() { std::vector<int> v = {1,2,3,{4,5}}; }";
  122. EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code));
  123. Code = "int main() { f(1,,2,3,,4);}";
  124. Expected = "int main() { f(1,2,3,4);}";
  125. EXPECT_EQ(Expected, cleanupAroundOffsets({17, 22}, Code));
  126. }
  127. TEST_F(CleanupTest, TrailingCommaInParens) {
  128. std::string Code = "int main() { f(,1,,2,3,f(1,2,),4,,);}";
  129. std::string Expected = "int main() { f(1,2,3,f(1,2),4);}";
  130. EXPECT_EQ(Expected, cleanupAroundOffsets({15, 18, 29, 33}, Code));
  131. }
  132. TEST_F(CleanupTest, TrailingCommaInBraces) {
  133. // Trainling comma is allowed in brace list.
  134. // If there was trailing comma in the original code, then trailing comma is
  135. // preserved. In this example, element between the last two commas is deleted
  136. // causing the second-last comma to be redundant.
  137. std::string Code = "void f() { std::vector<int> v = {1,2,3,,}; }";
  138. std::string Expected = "void f() { std::vector<int> v = {1,2,3,}; }";
  139. EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code));
  140. // If there was no trailing comma in the original code, then trainling comma
  141. // introduced by replacements should be cleaned up. In this example, the
  142. // element after the last comma is deleted causing the last comma to be
  143. // redundant.
  144. Code = "void f() { std::vector<int> v = {1,2,3,}; }";
  145. // FIXME: redundant trailing comma should be removed.
  146. Expected = "void f() { std::vector<int> v = {1,2,3,}; }";
  147. EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code));
  148. // Still no trailing comma in the original code, but two elements are deleted,
  149. // which makes it seems like there was trailing comma.
  150. Code = "void f() { std::vector<int> v = {1, 2, 3, , }; }";
  151. // FIXME: redundant trailing comma should also be removed.
  152. Expected = "void f() { std::vector<int> v = {1, 2, 3, }; }";
  153. EXPECT_EQ(Expected, cleanupAroundOffsets({42, 44}, Code));
  154. }
  155. TEST_F(CleanupTest, CtorInitializationBracesInParens) {
  156. std::string Code = "class A {\nA() : x({1}),, {} };";
  157. std::string Expected = "class A {\nA() : x({1}) {} };";
  158. EXPECT_EQ(Expected, cleanupAroundOffsets({24, 26}, Code));
  159. }
  160. TEST_F(CleanupTest, RedundantCommaNotInAffectedRanges) {
  161. std::string Code =
  162. "class A {\nA() : x({1}), /* comment */, { int x = 0; } };";
  163. std::string Expected =
  164. "class A {\nA() : x({1}), /* comment */, { int x = 0; } };";
  165. // Set the affected range to be "int x = 0", which does not intercept the
  166. // constructor initialization list.
  167. std::vector<tooling::Range> Ranges(1, tooling::Range(42, 9));
  168. std::string Result = cleanup(Code, Ranges);
  169. EXPECT_EQ(Expected, Result);
  170. Code = "class A {\nA() : x(1), {} };";
  171. Expected = "class A {\nA() : x(1), {} };";
  172. // No range. Fixer should do nothing.
  173. Ranges.clear();
  174. Result = cleanup(Code, Ranges);
  175. EXPECT_EQ(Expected, Result);
  176. }
  177. TEST_F(CleanupTest, RemoveCommentsAroundDeleteCode) {
  178. std::string Code =
  179. "class A {\nA() : x({1}), /* comment */, /* comment */ {} };";
  180. std::string Expected = "class A {\nA() : x({1}) {} };";
  181. EXPECT_EQ(Expected, cleanupAroundOffsets({25, 40}, Code));
  182. Code = "class A {\nA() : x({1}), // comment\n {} };";
  183. Expected = "class A {\nA() : x({1})\n {} };";
  184. EXPECT_EQ(Expected, cleanupAroundOffsets({25}, Code));
  185. Code = "class A {\nA() : x({1}), // comment\n , y(1),{} };";
  186. Expected = "class A {\nA() : x({1}), y(1){} };";
  187. EXPECT_EQ(Expected, cleanupAroundOffsets({38}, Code));
  188. Code = "class A {\nA() : x({1}), \n/* comment */, y(1),{} };";
  189. Expected = "class A {\nA() : x({1}), \n y(1){} };";
  190. EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code));
  191. Code = "class A {\nA() : , // comment\n y(1),{} };";
  192. Expected = "class A {\nA() : // comment\n y(1){} };";
  193. EXPECT_EQ(Expected, cleanupAroundOffsets({17}, Code));
  194. }
  195. TEST_F(CleanupTest, CtorInitializerInNamespace) {
  196. std::string Code = "namespace A {\n"
  197. "namespace B {\n" // missing r_brace
  198. "} // namespace A\n\n"
  199. "namespace C {\n"
  200. "class A { A() : x(0),, {} };\n"
  201. "inline namespace E { namespace { } }\n"
  202. "}";
  203. std::string Expected = "namespace A {\n"
  204. "\n\n\nnamespace C {\n"
  205. "class A { A() : x(0) {} };\n \n"
  206. "}";
  207. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  208. std::string Result = cleanup(Code, Ranges);
  209. EXPECT_EQ(Expected, Result);
  210. }
  211. class CleanUpReplacementsTest : public ReplacementTest {
  212. protected:
  213. tooling::Replacement createReplacement(unsigned Offset, unsigned Length,
  214. StringRef Text) {
  215. return tooling::Replacement(FileName, Offset, Length, Text);
  216. }
  217. tooling::Replacement createInsertion(StringRef HeaderName) {
  218. return createReplacement(UINT_MAX, 0, HeaderName);
  219. }
  220. inline std::string apply(StringRef Code,
  221. const tooling::Replacements Replaces) {
  222. auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
  223. EXPECT_TRUE(static_cast<bool>(CleanReplaces))
  224. << llvm::toString(CleanReplaces.takeError()) << "\n";
  225. auto Result = applyAllReplacements(Code, *CleanReplaces);
  226. EXPECT_TRUE(static_cast<bool>(Result));
  227. return *Result;
  228. }
  229. inline std::string formatAndApply(StringRef Code,
  230. const tooling::Replacements Replaces) {
  231. auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
  232. EXPECT_TRUE(static_cast<bool>(CleanReplaces))
  233. << llvm::toString(CleanReplaces.takeError()) << "\n";
  234. auto FormattedReplaces = formatReplacements(Code, *CleanReplaces, Style);
  235. EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
  236. << llvm::toString(FormattedReplaces.takeError()) << "\n";
  237. auto Result = applyAllReplacements(Code, *FormattedReplaces);
  238. EXPECT_TRUE(static_cast<bool>(Result));
  239. return *Result;
  240. }
  241. int getOffset(StringRef Code, int Line, int Column) {
  242. RewriterTestContext Context;
  243. FileID ID = Context.createInMemoryFile(FileName, Code);
  244. auto DecomposedLocation =
  245. Context.Sources.getDecomposedLoc(Context.getLocation(ID, Line, Column));
  246. return DecomposedLocation.second;
  247. }
  248. const std::string FileName = "fix.cpp";
  249. FormatStyle Style = getLLVMStyle();
  250. };
  251. TEST_F(CleanUpReplacementsTest, FixOnlyAffectedCodeAfterReplacements) {
  252. std::string Code = "namespace A {\n"
  253. "namespace B {\n"
  254. " int x;\n"
  255. "} // namespace B\n"
  256. "} // namespace A\n"
  257. "\n"
  258. "namespace C {\n"
  259. "namespace D { int i; }\n"
  260. "inline namespace E { namespace { int y; } }\n"
  261. "int x= 0;"
  262. "}";
  263. std::string Expected = "\n\nnamespace C {\n"
  264. "namespace D { int i; }\n\n"
  265. "int x= 0;"
  266. "}";
  267. tooling::Replacements Replaces =
  268. toReplacements({createReplacement(getOffset(Code, 3, 3), 6, ""),
  269. createReplacement(getOffset(Code, 9, 34), 6, "")});
  270. EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
  271. }
  272. TEST_F(CleanUpReplacementsTest, NoExistingIncludeWithoutDefine) {
  273. std::string Code = "int main() {}";
  274. std::string Expected = "#include \"a.h\"\n"
  275. "int main() {}";
  276. tooling::Replacements Replaces =
  277. toReplacements({createInsertion("#include \"a.h\"")});
  278. EXPECT_EQ(Expected, apply(Code, Replaces));
  279. }
  280. TEST_F(CleanUpReplacementsTest, NoExistingIncludeWithDefine) {
  281. std::string Code = "#ifndef A_H\n"
  282. "#define A_H\n"
  283. "class A {};\n"
  284. "#define MMM 123\n"
  285. "#endif";
  286. std::string Expected = "#ifndef A_H\n"
  287. "#define A_H\n"
  288. "#include \"b.h\"\n"
  289. "class A {};\n"
  290. "#define MMM 123\n"
  291. "#endif";
  292. tooling::Replacements Replaces =
  293. toReplacements({createInsertion("#include \"b.h\"")});
  294. EXPECT_EQ(Expected, apply(Code, Replaces));
  295. }
  296. TEST_F(CleanUpReplacementsTest, InsertBeforeCategoryWithLowerPriority) {
  297. std::string Code = "#ifndef A_H\n"
  298. "#define A_H\n"
  299. "\n"
  300. "\n"
  301. "\n"
  302. "#include <vector>\n"
  303. "class A {};\n"
  304. "#define MMM 123\n"
  305. "#endif";
  306. std::string Expected = "#ifndef A_H\n"
  307. "#define A_H\n"
  308. "\n"
  309. "\n"
  310. "\n"
  311. "#include \"a.h\"\n"
  312. "#include <vector>\n"
  313. "class A {};\n"
  314. "#define MMM 123\n"
  315. "#endif";
  316. tooling::Replacements Replaces =
  317. toReplacements({createInsertion("#include \"a.h\"")});
  318. EXPECT_EQ(Expected, apply(Code, Replaces));
  319. }
  320. TEST_F(CleanUpReplacementsTest, InsertAfterMainHeader) {
  321. std::string Code = "#include \"fix.h\"\n"
  322. "\n"
  323. "int main() {}";
  324. std::string Expected = "#include \"fix.h\"\n"
  325. "#include <a>\n"
  326. "\n"
  327. "int main() {}";
  328. tooling::Replacements Replaces =
  329. toReplacements({createInsertion("#include <a>")});
  330. Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  331. EXPECT_EQ(Expected, apply(Code, Replaces));
  332. }
  333. TEST_F(CleanUpReplacementsTest, InsertBeforeSystemHeaderLLVM) {
  334. std::string Code = "#include <memory>\n"
  335. "\n"
  336. "int main() {}";
  337. std::string Expected = "#include \"z.h\"\n"
  338. "#include <memory>\n"
  339. "\n"
  340. "int main() {}";
  341. tooling::Replacements Replaces =
  342. toReplacements({createInsertion("#include \"z.h\"")});
  343. EXPECT_EQ(Expected, apply(Code, Replaces));
  344. }
  345. TEST_F(CleanUpReplacementsTest, InsertAfterSystemHeaderGoogle) {
  346. std::string Code = "#include <memory>\n"
  347. "\n"
  348. "int main() {}";
  349. std::string Expected = "#include <memory>\n"
  350. "#include \"z.h\"\n"
  351. "\n"
  352. "int main() {}";
  353. tooling::Replacements Replaces =
  354. toReplacements({createInsertion("#include \"z.h\"")});
  355. Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  356. EXPECT_EQ(Expected, apply(Code, Replaces));
  357. }
  358. TEST_F(CleanUpReplacementsTest, InsertOneIncludeLLVMStyle) {
  359. std::string Code = "#include \"x/fix.h\"\n"
  360. "#include \"a.h\"\n"
  361. "#include \"b.h\"\n"
  362. "#include \"clang/Format/Format.h\"\n"
  363. "#include <memory>\n";
  364. std::string Expected = "#include \"x/fix.h\"\n"
  365. "#include \"a.h\"\n"
  366. "#include \"b.h\"\n"
  367. "#include \"d.h\"\n"
  368. "#include \"clang/Format/Format.h\"\n"
  369. "#include \"llvm/x/y.h\"\n"
  370. "#include <memory>\n";
  371. tooling::Replacements Replaces =
  372. toReplacements({createInsertion("#include \"d.h\""),
  373. createInsertion("#include \"llvm/x/y.h\"")});
  374. EXPECT_EQ(Expected, apply(Code, Replaces));
  375. }
  376. TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesLLVMStyle) {
  377. std::string Code = "#include \"x/fix.h\"\n"
  378. "#include \"a.h\"\n"
  379. "#include \"b.h\"\n"
  380. "#include \"clang/Format/Format.h\"\n"
  381. "#include <memory>\n";
  382. std::string Expected = "#include \"x/fix.h\"\n"
  383. "#include \"a.h\"\n"
  384. "#include \"b.h\"\n"
  385. "#include \"new/new.h\"\n"
  386. "#include \"clang/Format/Format.h\"\n"
  387. "#include <memory>\n"
  388. "#include <list>\n";
  389. tooling::Replacements Replaces =
  390. toReplacements({createInsertion("#include <list>"),
  391. createInsertion("#include \"new/new.h\"")});
  392. EXPECT_EQ(Expected, apply(Code, Replaces));
  393. }
  394. TEST_F(CleanUpReplacementsTest, InsertNewSystemIncludeGoogleStyle) {
  395. std::string Code = "#include \"x/fix.h\"\n"
  396. "\n"
  397. "#include \"y/a.h\"\n"
  398. "#include \"z/b.h\"\n";
  399. // FIXME: inserting after the empty line following the main header might be
  400. // prefered.
  401. std::string Expected = "#include \"x/fix.h\"\n"
  402. "#include <vector>\n"
  403. "\n"
  404. "#include \"y/a.h\"\n"
  405. "#include \"z/b.h\"\n";
  406. tooling::Replacements Replaces =
  407. toReplacements({createInsertion("#include <vector>")});
  408. Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  409. EXPECT_EQ(Expected, apply(Code, Replaces));
  410. }
  411. TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesGoogleStyle) {
  412. std::string Code = "#include \"x/fix.h\"\n"
  413. "\n"
  414. "#include <vector>\n"
  415. "\n"
  416. "#include \"y/a.h\"\n"
  417. "#include \"z/b.h\"\n";
  418. std::string Expected = "#include \"x/fix.h\"\n"
  419. "\n"
  420. "#include <vector>\n"
  421. "#include <list>\n"
  422. "\n"
  423. "#include \"y/a.h\"\n"
  424. "#include \"z/b.h\"\n"
  425. "#include \"x/x.h\"\n";
  426. tooling::Replacements Replaces =
  427. toReplacements({createInsertion("#include <list>"),
  428. createInsertion("#include \"x/x.h\"")});
  429. Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  430. EXPECT_EQ(Expected, apply(Code, Replaces));
  431. }
  432. TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortLLVM) {
  433. std::string Code = "\nint x;";
  434. std::string Expected = "\n#include \"fix.h\"\n"
  435. "#include \"a.h\"\n"
  436. "#include \"b.h\"\n"
  437. "#include \"c.h\"\n"
  438. "#include <list>\n"
  439. "#include <vector>\n"
  440. "int x;";
  441. tooling::Replacements Replaces = toReplacements(
  442. {createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""),
  443. createInsertion("#include \"b.h\""),
  444. createInsertion("#include <vector>"), createInsertion("#include <list>"),
  445. createInsertion("#include \"fix.h\"")});
  446. EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
  447. }
  448. TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortGoogle) {
  449. std::string Code = "\nint x;";
  450. std::string Expected = "\n#include \"fix.h\"\n"
  451. "#include <list>\n"
  452. "#include <vector>\n"
  453. "#include \"a.h\"\n"
  454. "#include \"b.h\"\n"
  455. "#include \"c.h\"\n"
  456. "int x;";
  457. tooling::Replacements Replaces = toReplacements(
  458. {createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""),
  459. createInsertion("#include \"b.h\""),
  460. createInsertion("#include <vector>"), createInsertion("#include <list>"),
  461. createInsertion("#include \"fix.h\"")});
  462. Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  463. EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
  464. }
  465. TEST_F(CleanUpReplacementsTest, FormatCorrectLineWhenHeadersAreInserted) {
  466. std::string Code = "\n"
  467. "int x;\n"
  468. "int a;\n"
  469. "int a;\n"
  470. "int a;";
  471. std::string Expected = "\n#include \"x.h\"\n"
  472. "#include \"y.h\"\n"
  473. "#include \"clang/x/x.h\"\n"
  474. "#include <list>\n"
  475. "#include <vector>\n"
  476. "int x;\n"
  477. "int a;\n"
  478. "int b;\n"
  479. "int a;";
  480. tooling::Replacements Replaces = toReplacements(
  481. {createReplacement(getOffset(Code, 4, 8), 1, "b"),
  482. createInsertion("#include <vector>"), createInsertion("#include <list>"),
  483. createInsertion("#include \"clang/x/x.h\""),
  484. createInsertion("#include \"y.h\""),
  485. createInsertion("#include \"x.h\"")});
  486. EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
  487. }
  488. TEST_F(CleanUpReplacementsTest, NotConfusedByDefine) {
  489. std::string Code = "void f() {}\n"
  490. "#define A \\\n"
  491. " int i;";
  492. std::string Expected = "#include <vector>\n"
  493. "void f() {}\n"
  494. "#define A \\\n"
  495. " int i;";
  496. tooling::Replacements Replaces =
  497. toReplacements({createInsertion("#include <vector>")});
  498. EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
  499. }
  500. TEST_F(CleanUpReplacementsTest, SkippedTopComment) {
  501. std::string Code = "// comment\n"
  502. "\n"
  503. " // comment\n";
  504. std::string Expected = "// comment\n"
  505. "\n"
  506. " // comment\n"
  507. "#include <vector>\n";
  508. tooling::Replacements Replaces =
  509. toReplacements({createInsertion("#include <vector>")});
  510. EXPECT_EQ(Expected, apply(Code, Replaces));
  511. }
  512. TEST_F(CleanUpReplacementsTest, SkippedMixedComments) {
  513. std::string Code = "// comment\n"
  514. "// comment \\\n"
  515. " comment continued\n"
  516. "/*\n"
  517. "* comment\n"
  518. "*/\n";
  519. std::string Expected = "// comment\n"
  520. "// comment \\\n"
  521. " comment continued\n"
  522. "/*\n"
  523. "* comment\n"
  524. "*/\n"
  525. "#include <vector>\n";
  526. tooling::Replacements Replaces =
  527. toReplacements({createInsertion("#include <vector>")});
  528. EXPECT_EQ(Expected, apply(Code, Replaces));
  529. }
  530. TEST_F(CleanUpReplacementsTest, MultipleBlockCommentsInOneLine) {
  531. std::string Code = "/*\n"
  532. "* comment\n"
  533. "*/ /* comment\n"
  534. "*/\n"
  535. "\n\n"
  536. "/* c1 */ /*c2 */\n";
  537. std::string Expected = "/*\n"
  538. "* comment\n"
  539. "*/ /* comment\n"
  540. "*/\n"
  541. "\n\n"
  542. "/* c1 */ /*c2 */\n"
  543. "#include <vector>\n";
  544. tooling::Replacements Replaces =
  545. toReplacements({createInsertion("#include <vector>")});
  546. EXPECT_EQ(Expected, apply(Code, Replaces));
  547. }
  548. TEST_F(CleanUpReplacementsTest, CodeAfterComments) {
  549. std::string Code = "/*\n"
  550. "* comment\n"
  551. "*/ /* comment\n"
  552. "*/\n"
  553. "\n\n"
  554. "/* c1 */ /*c2 */\n"
  555. "\n"
  556. "int x;\n";
  557. std::string Expected = "/*\n"
  558. "* comment\n"
  559. "*/ /* comment\n"
  560. "*/\n"
  561. "\n\n"
  562. "/* c1 */ /*c2 */\n"
  563. "\n"
  564. "#include <vector>\n"
  565. "int x;\n";
  566. tooling::Replacements Replaces =
  567. toReplacements({createInsertion("#include <vector>")});
  568. EXPECT_EQ(Expected, apply(Code, Replaces));
  569. }
  570. TEST_F(CleanUpReplacementsTest, FakeHeaderGuardIfDef) {
  571. std::string Code = "// comment \n"
  572. "#ifdef X\n"
  573. "#define X\n";
  574. std::string Expected = "// comment \n"
  575. "#include <vector>\n"
  576. "#ifdef X\n"
  577. "#define X\n";
  578. tooling::Replacements Replaces =
  579. toReplacements({createInsertion("#include <vector>")});
  580. EXPECT_EQ(Expected, apply(Code, Replaces));
  581. }
  582. TEST_F(CleanUpReplacementsTest, RealHeaderGuardAfterComments) {
  583. std::string Code = "// comment \n"
  584. "#ifndef X\n"
  585. "#define X\n"
  586. "int x;\n"
  587. "#define Y 1\n";
  588. std::string Expected = "// comment \n"
  589. "#ifndef X\n"
  590. "#define X\n"
  591. "#include <vector>\n"
  592. "int x;\n"
  593. "#define Y 1\n";
  594. tooling::Replacements Replaces =
  595. toReplacements({createInsertion("#include <vector>")});
  596. EXPECT_EQ(Expected, apply(Code, Replaces));
  597. }
  598. TEST_F(CleanUpReplacementsTest, IfNDefWithNoDefine) {
  599. std::string Code = "// comment \n"
  600. "#ifndef X\n"
  601. "int x;\n"
  602. "#define Y 1\n";
  603. std::string Expected = "// comment \n"
  604. "#include <vector>\n"
  605. "#ifndef X\n"
  606. "int x;\n"
  607. "#define Y 1\n";
  608. tooling::Replacements Replaces =
  609. toReplacements({createInsertion("#include <vector>")});
  610. EXPECT_EQ(Expected, apply(Code, Replaces));
  611. }
  612. TEST_F(CleanUpReplacementsTest, HeaderGuardWithComment) {
  613. std::string Code = "// comment \n"
  614. "#ifndef X // comment\n"
  615. "// comment\n"
  616. "/* comment\n"
  617. "*/\n"
  618. "/* comment */ #define X\n"
  619. "int x;\n"
  620. "#define Y 1\n";
  621. std::string Expected = "// comment \n"
  622. "#ifndef X // comment\n"
  623. "// comment\n"
  624. "/* comment\n"
  625. "*/\n"
  626. "/* comment */ #define X\n"
  627. "#include <vector>\n"
  628. "int x;\n"
  629. "#define Y 1\n";
  630. tooling::Replacements Replaces =
  631. toReplacements({createInsertion("#include <vector>")});
  632. EXPECT_EQ(Expected, apply(Code, Replaces));
  633. }
  634. TEST_F(CleanUpReplacementsTest, EmptyCode) {
  635. std::string Code = "";
  636. std::string Expected = "#include <vector>\n";
  637. tooling::Replacements Replaces =
  638. toReplacements({createInsertion("#include <vector>")});
  639. EXPECT_EQ(Expected, apply(Code, Replaces));
  640. }
  641. // FIXME: although this case does not crash, the insertion is wrong. A '\n'
  642. // should be inserted between the two #includes.
  643. TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCode) {
  644. std::string Code = "#include <map>";
  645. std::string Expected = "#include <map>#include <vector>\n";
  646. tooling::Replacements Replaces =
  647. toReplacements({createInsertion("#include <vector>")});
  648. EXPECT_EQ(Expected, apply(Code, Replaces));
  649. }
  650. TEST_F(CleanUpReplacementsTest, SkipExistingHeaders) {
  651. std::string Code = "#include \"a.h\"\n"
  652. "#include <vector>\n";
  653. std::string Expected = "#include \"a.h\"\n"
  654. "#include <vector>\n";
  655. tooling::Replacements Replaces =
  656. toReplacements({createInsertion("#include <vector>"),
  657. createInsertion("#include \"a.h\"")});
  658. EXPECT_EQ(Expected, apply(Code, Replaces));
  659. }
  660. TEST_F(CleanUpReplacementsTest, AddIncludesWithDifferentForms) {
  661. std::string Code = "#include \"a.h\"\n"
  662. "#include <vector>\n";
  663. // FIXME: this might not be the best behavior.
  664. std::string Expected = "#include \"a.h\"\n"
  665. "#include \"vector\"\n"
  666. "#include <vector>\n"
  667. "#include <a.h>\n";
  668. tooling::Replacements Replaces =
  669. toReplacements({createInsertion("#include \"vector\""),
  670. createInsertion("#include <a.h>")});
  671. EXPECT_EQ(Expected, apply(Code, Replaces));
  672. }
  673. } // end namespace
  674. } // end namespace format
  675. } // end namespace clang