xref: /llvm-project/clang/unittests/Tooling/HeaderIncludesTest.cpp (revision f3dcc2351cff7b26c9870d737e5d431551542d9e)
17129e63bSEric Liu //===- unittest/Tooling/CleanupTest.cpp - Include insertion/deletion tests ===//
27129e63bSEric Liu //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67129e63bSEric Liu //
77129e63bSEric Liu //===----------------------------------------------------------------------===//
87129e63bSEric Liu 
944564ac7SEric Liu #include "clang/Tooling/Inclusions/HeaderIncludes.h"
107129e63bSEric Liu #include "../Tooling/ReplacementTest.h"
117129e63bSEric Liu #include "../Tooling/RewriterTestContext.h"
127129e63bSEric Liu #include "clang/Format/Format.h"
137129e63bSEric Liu #include "clang/Tooling/Core/Replacement.h"
147129e63bSEric Liu 
157129e63bSEric Liu #include "gtest/gtest.h"
167129e63bSEric Liu 
177129e63bSEric Liu namespace clang {
187129e63bSEric Liu namespace tooling {
197129e63bSEric Liu namespace {
207129e63bSEric Liu 
217129e63bSEric Liu class HeaderIncludesTest : public ::testing::Test {
227129e63bSEric Liu protected:
insert(llvm::StringRef Code,llvm::StringRef Header,IncludeDirective Directive=IncludeDirective::Include)23fc46d6e6SDavid Goldman   std::string insert(llvm::StringRef Code, llvm::StringRef Header,
24fc46d6e6SDavid Goldman                      IncludeDirective Directive = IncludeDirective::Include) {
257129e63bSEric Liu     HeaderIncludes Includes(FileName, Code, Style);
26*f3dcc235SKazu Hirata     assert(Header.starts_with("\"") || Header.starts_with("<"));
27*f3dcc235SKazu Hirata     auto R = Includes.insert(Header.trim("\"<>"), Header.starts_with("<"),
28*f3dcc235SKazu Hirata                              Directive);
297129e63bSEric Liu     if (!R)
30adcd0268SBenjamin Kramer       return std::string(Code);
317129e63bSEric Liu     auto Result = applyAllReplacements(Code, Replacements(*R));
327129e63bSEric Liu     EXPECT_TRUE(static_cast<bool>(Result));
337129e63bSEric Liu     return *Result;
347129e63bSEric Liu   }
357129e63bSEric Liu 
remove(llvm::StringRef Code,llvm::StringRef Header)367129e63bSEric Liu   std::string remove(llvm::StringRef Code, llvm::StringRef Header) {
377129e63bSEric Liu     HeaderIncludes Includes(FileName, Code, Style);
38*f3dcc235SKazu Hirata     assert(Header.starts_with("\"") || Header.starts_with("<"));
39*f3dcc235SKazu Hirata     auto Replaces =
40*f3dcc235SKazu Hirata         Includes.remove(Header.trim("\"<>"), Header.starts_with("<"));
417129e63bSEric Liu     auto Result = applyAllReplacements(Code, Replaces);
427129e63bSEric Liu     EXPECT_TRUE(static_cast<bool>(Result));
437129e63bSEric Liu     return *Result;
447129e63bSEric Liu   }
457129e63bSEric Liu 
4626682562SSam McCall   std::string FileName = "fix.cpp";
477129e63bSEric Liu   IncludeStyle Style = format::getLLVMStyle().IncludeStyle;
487129e63bSEric Liu };
497129e63bSEric Liu 
TEST_F(HeaderIncludesTest,NoExistingIncludeWithoutDefine)507129e63bSEric Liu TEST_F(HeaderIncludesTest, NoExistingIncludeWithoutDefine) {
517129e63bSEric Liu   std::string Code = "int main() {}";
527129e63bSEric Liu   std::string Expected = "#include \"a.h\"\n"
537129e63bSEric Liu                          "int main() {}";
547129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"a.h\""));
557129e63bSEric Liu }
567129e63bSEric Liu 
TEST_F(HeaderIncludesTest,RepeatedIncludes)5771d7c8d8SIvan Murashko TEST_F(HeaderIncludesTest, RepeatedIncludes) {
5871d7c8d8SIvan Murashko   std::string Code;
5971d7c8d8SIvan Murashko   for (int i = 0; i < 100; ++i) {
6071d7c8d8SIvan Murashko     Code += "#include \"a.h\"\n";
6171d7c8d8SIvan Murashko   }
6271d7c8d8SIvan Murashko   std::string Expected = Code + "#include \"a2.h\"\n";
6371d7c8d8SIvan Murashko   EXPECT_EQ(Expected, insert(Code, "\"a2.h\""));
6471d7c8d8SIvan Murashko }
6571d7c8d8SIvan Murashko 
TEST_F(HeaderIncludesTest,InsertImportWithSameInclude)66fc46d6e6SDavid Goldman TEST_F(HeaderIncludesTest, InsertImportWithSameInclude) {
67fc46d6e6SDavid Goldman   std::string Code = "#include \"a.h\"\n";
68fc46d6e6SDavid Goldman   std::string Expected = Code + "#import \"a.h\"\n";
69fc46d6e6SDavid Goldman   EXPECT_EQ(Expected, insert(Code, "\"a.h\"", IncludeDirective::Import));
70fc46d6e6SDavid Goldman }
71fc46d6e6SDavid Goldman 
TEST_F(HeaderIncludesTest,DontInsertAlreadyImported)72fc46d6e6SDavid Goldman TEST_F(HeaderIncludesTest, DontInsertAlreadyImported) {
73fc46d6e6SDavid Goldman   std::string Code = "#import \"a.h\"\n";
74fc46d6e6SDavid Goldman   EXPECT_EQ(Code, insert(Code, "\"a.h\"", IncludeDirective::Import));
75fc46d6e6SDavid Goldman }
76fc46d6e6SDavid Goldman 
TEST_F(HeaderIncludesTest,DeleteImportAndSameInclude)77fc46d6e6SDavid Goldman TEST_F(HeaderIncludesTest, DeleteImportAndSameInclude) {
78fc46d6e6SDavid Goldman   std::string Code = R"cpp(
79fc46d6e6SDavid Goldman #include <abc.h>
80fc46d6e6SDavid Goldman #import <abc.h>
81fc46d6e6SDavid Goldman int x;)cpp";
82fc46d6e6SDavid Goldman   EXPECT_EQ("\nint x;", remove(Code, "<abc.h>"));
83fc46d6e6SDavid Goldman }
84fc46d6e6SDavid Goldman 
TEST_F(HeaderIncludesTest,NoExistingIncludeWithDefine)857129e63bSEric Liu TEST_F(HeaderIncludesTest, NoExistingIncludeWithDefine) {
867129e63bSEric Liu   std::string Code = "#ifndef A_H\n"
877129e63bSEric Liu                      "#define A_H\n"
887129e63bSEric Liu                      "class A {};\n"
897129e63bSEric Liu                      "#define MMM 123\n"
907129e63bSEric Liu                      "#endif";
917129e63bSEric Liu   std::string Expected = "#ifndef A_H\n"
927129e63bSEric Liu                          "#define A_H\n"
937129e63bSEric Liu                          "#include \"b.h\"\n"
947129e63bSEric Liu                          "class A {};\n"
957129e63bSEric Liu                          "#define MMM 123\n"
967129e63bSEric Liu                          "#endif";
977129e63bSEric Liu 
987129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"b.h\""));
997129e63bSEric Liu }
1007129e63bSEric Liu 
TEST_F(HeaderIncludesTest,InsertBeforeCategoryWithLowerPriority)1017129e63bSEric Liu TEST_F(HeaderIncludesTest, InsertBeforeCategoryWithLowerPriority) {
1027129e63bSEric Liu   std::string Code = "#ifndef A_H\n"
1037129e63bSEric Liu                      "#define A_H\n"
1047129e63bSEric Liu                      "\n"
1057129e63bSEric Liu                      "\n"
1067129e63bSEric Liu                      "\n"
1077129e63bSEric Liu                      "#include <vector>\n"
1087129e63bSEric Liu                      "class A {};\n"
1097129e63bSEric Liu                      "#define MMM 123\n"
1107129e63bSEric Liu                      "#endif";
1117129e63bSEric Liu   std::string Expected = "#ifndef A_H\n"
1127129e63bSEric Liu                          "#define A_H\n"
1137129e63bSEric Liu                          "\n"
1147129e63bSEric Liu                          "\n"
1157129e63bSEric Liu                          "\n"
1167129e63bSEric Liu                          "#include \"a.h\"\n"
1177129e63bSEric Liu                          "#include <vector>\n"
1187129e63bSEric Liu                          "class A {};\n"
1197129e63bSEric Liu                          "#define MMM 123\n"
1207129e63bSEric Liu                          "#endif";
1217129e63bSEric Liu 
1227129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"a.h\""));
1237129e63bSEric Liu }
1247129e63bSEric Liu 
TEST_F(HeaderIncludesTest,InsertAfterMainHeader)1257129e63bSEric Liu TEST_F(HeaderIncludesTest, InsertAfterMainHeader) {
1267129e63bSEric Liu   std::string Code = "#include \"fix.h\"\n"
1277129e63bSEric Liu                      "\n"
1287129e63bSEric Liu                      "int main() {}";
1297129e63bSEric Liu   std::string Expected = "#include \"fix.h\"\n"
1307129e63bSEric Liu                          "#include <a>\n"
1317129e63bSEric Liu                          "\n"
1327129e63bSEric Liu                          "int main() {}";
1337129e63bSEric Liu   Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
1347129e63bSEric Liu               .IncludeStyle;
1357129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<a>"));
13626682562SSam McCall 
13726682562SSam McCall   FileName = "fix.cu.cpp";
13826682562SSam McCall   EXPECT_EQ(Expected, insert(Code, "<a>"));
13926682562SSam McCall 
14026682562SSam McCall   FileName = "fix_test.cu.cpp";
14126682562SSam McCall   EXPECT_EQ(Expected, insert(Code, "<a>"));
14226682562SSam McCall 
14326682562SSam McCall   FileName = "bar.cpp";
14426682562SSam McCall   EXPECT_NE(Expected, insert(Code, "<a>")) << "Not main header";
1457129e63bSEric Liu }
1467129e63bSEric Liu 
TEST_F(HeaderIncludesTest,InsertMainHeader)14785c6d57eSHaojian Wu TEST_F(HeaderIncludesTest, InsertMainHeader) {
14885c6d57eSHaojian Wu   Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
14985c6d57eSHaojian Wu               .IncludeStyle;
15085c6d57eSHaojian Wu   FileName = "fix.cpp";
15185c6d57eSHaojian Wu   EXPECT_EQ(R"cpp(#include "fix.h"
15285c6d57eSHaojian Wu #include "a.h")cpp", insert("#include \"a.h\"", "\"fix.h\""));
15385c6d57eSHaojian Wu 
15485c6d57eSHaojian Wu   // Respect the original main-file header.
15585c6d57eSHaojian Wu   EXPECT_EQ(R"cpp(#include "z/fix.h"
15685c6d57eSHaojian Wu #include "a/fix.h"
15785c6d57eSHaojian Wu )cpp", insert("#include \"z/fix.h\"", "\"a/fix.h\""));
15885c6d57eSHaojian Wu }
15985c6d57eSHaojian Wu 
TEST_F(HeaderIncludesTest,InsertBeforeSystemHeaderLLVM)1607129e63bSEric Liu TEST_F(HeaderIncludesTest, InsertBeforeSystemHeaderLLVM) {
1617129e63bSEric Liu   std::string Code = "#include <memory>\n"
1627129e63bSEric Liu                      "\n"
1637129e63bSEric Liu                      "int main() {}";
1647129e63bSEric Liu   std::string Expected = "#include \"z.h\"\n"
1657129e63bSEric Liu                          "#include <memory>\n"
1667129e63bSEric Liu                          "\n"
1677129e63bSEric Liu                          "int main() {}";
1687129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"z.h\""));
1697129e63bSEric Liu }
1707129e63bSEric Liu 
TEST_F(HeaderIncludesTest,InsertAfterSystemHeaderGoogle)1717129e63bSEric Liu TEST_F(HeaderIncludesTest, InsertAfterSystemHeaderGoogle) {
1727129e63bSEric Liu   std::string Code = "#include <memory>\n"
1737129e63bSEric Liu                      "\n"
1747129e63bSEric Liu                      "int main() {}";
1757129e63bSEric Liu   std::string Expected = "#include <memory>\n"
1767129e63bSEric Liu                          "#include \"z.h\"\n"
1777129e63bSEric Liu                          "\n"
1787129e63bSEric Liu                          "int main() {}";
1797129e63bSEric Liu   Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
1807129e63bSEric Liu               .IncludeStyle;
1817129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"z.h\""));
1827129e63bSEric Liu }
1837129e63bSEric Liu 
TEST_F(HeaderIncludesTest,InsertOneIncludeLLVMStyle)1847129e63bSEric Liu TEST_F(HeaderIncludesTest, InsertOneIncludeLLVMStyle) {
1857129e63bSEric Liu   std::string Code = "#include \"x/fix.h\"\n"
1867129e63bSEric Liu                      "#include \"a.h\"\n"
1877129e63bSEric Liu                      "#include \"b.h\"\n"
1887129e63bSEric Liu                      "#include \"clang/Format/Format.h\"\n"
1897129e63bSEric Liu                      "#include <memory>\n";
1907129e63bSEric Liu   std::string Expected = "#include \"x/fix.h\"\n"
1917129e63bSEric Liu                          "#include \"a.h\"\n"
1927129e63bSEric Liu                          "#include \"b.h\"\n"
1937129e63bSEric Liu                          "#include \"clang/Format/Format.h\"\n"
1947129e63bSEric Liu                          "#include \"llvm/x/y.h\"\n"
1957129e63bSEric Liu                          "#include <memory>\n";
1967129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"llvm/x/y.h\""));
1977129e63bSEric Liu }
1987129e63bSEric Liu 
TEST_F(HeaderIncludesTest,InsertIntoBlockSorted)1997129e63bSEric Liu TEST_F(HeaderIncludesTest, InsertIntoBlockSorted) {
2007129e63bSEric Liu   std::string Code = "#include \"x/fix.h\"\n"
2017129e63bSEric Liu                      "#include \"a.h\"\n"
2027129e63bSEric Liu                      "#include \"c.h\"\n"
2037129e63bSEric Liu                      "#include <memory>\n";
2047129e63bSEric Liu   std::string Expected = "#include \"x/fix.h\"\n"
2057129e63bSEric Liu                          "#include \"a.h\"\n"
2067129e63bSEric Liu                          "#include \"b.h\"\n"
2077129e63bSEric Liu                          "#include \"c.h\"\n"
2087129e63bSEric Liu                          "#include <memory>\n";
2097129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"b.h\""));
2107129e63bSEric Liu }
2117129e63bSEric Liu 
TEST_F(HeaderIncludesTest,InsertIntoFirstBlockOfSameKind)2127129e63bSEric Liu TEST_F(HeaderIncludesTest, InsertIntoFirstBlockOfSameKind) {
2137129e63bSEric Liu   std::string Code = "#include \"x/fix.h\"\n"
2147129e63bSEric Liu                      "#include \"c.h\"\n"
2157129e63bSEric Liu                      "#include \"e.h\"\n"
2167129e63bSEric Liu                      "#include \"f.h\"\n"
2177129e63bSEric Liu                      "#include <memory>\n"
2187129e63bSEric Liu                      "#include <vector>\n"
2197129e63bSEric Liu                      "#include \"m.h\"\n"
2207129e63bSEric Liu                      "#include \"n.h\"\n";
2217129e63bSEric Liu   std::string Expected = "#include \"x/fix.h\"\n"
2227129e63bSEric Liu                          "#include \"c.h\"\n"
2237129e63bSEric Liu                          "#include \"d.h\"\n"
2247129e63bSEric Liu                          "#include \"e.h\"\n"
2257129e63bSEric Liu                          "#include \"f.h\"\n"
2267129e63bSEric Liu                          "#include <memory>\n"
2277129e63bSEric Liu                          "#include <vector>\n"
2287129e63bSEric Liu                          "#include \"m.h\"\n"
2297129e63bSEric Liu                          "#include \"n.h\"\n";
2307129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"d.h\""));
2317129e63bSEric Liu }
2327129e63bSEric Liu 
TEST_F(HeaderIncludesTest,InsertIntoSystemBlockSorted)2337129e63bSEric Liu TEST_F(HeaderIncludesTest, InsertIntoSystemBlockSorted) {
2347129e63bSEric Liu   std::string Code = "#include \"x/fix.h\"\n"
2357129e63bSEric Liu                      "#include \"a.h\"\n"
2367129e63bSEric Liu                      "#include \"c.h\"\n"
2377129e63bSEric Liu                      "#include <a>\n"
2387129e63bSEric Liu                      "#include <z>\n";
2397129e63bSEric Liu   std::string Expected = "#include \"x/fix.h\"\n"
2407129e63bSEric Liu                          "#include \"a.h\"\n"
2417129e63bSEric Liu                          "#include \"c.h\"\n"
2427129e63bSEric Liu                          "#include <a>\n"
2437129e63bSEric Liu                          "#include <vector>\n"
2447129e63bSEric Liu                          "#include <z>\n";
2457129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
2467129e63bSEric Liu }
2477129e63bSEric Liu 
TEST_F(HeaderIncludesTest,InsertNewSystemIncludeGoogleStyle)2487129e63bSEric Liu TEST_F(HeaderIncludesTest, InsertNewSystemIncludeGoogleStyle) {
2497129e63bSEric Liu   std::string Code = "#include \"x/fix.h\"\n"
2507129e63bSEric Liu                      "\n"
2517129e63bSEric Liu                      "#include \"y/a.h\"\n"
2527129e63bSEric Liu                      "#include \"z/b.h\"\n";
2537129e63bSEric Liu   // FIXME: inserting after the empty line following the main header might be
2547129e63bSEric Liu   // preferred.
2557129e63bSEric Liu   std::string Expected = "#include \"x/fix.h\"\n"
2567129e63bSEric Liu                          "#include <vector>\n"
2577129e63bSEric Liu                          "\n"
2587129e63bSEric Liu                          "#include \"y/a.h\"\n"
2597129e63bSEric Liu                          "#include \"z/b.h\"\n";
2607129e63bSEric Liu   Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
2617129e63bSEric Liu               .IncludeStyle;
2627129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
2637129e63bSEric Liu }
2647129e63bSEric Liu 
TEST_F(HeaderIncludesTest,NotConfusedByDefine)2657129e63bSEric Liu TEST_F(HeaderIncludesTest, NotConfusedByDefine) {
2667129e63bSEric Liu   std::string Code = "void f() {}\n"
2677129e63bSEric Liu                      "#define A \\\n"
2687129e63bSEric Liu                      "  int i;";
2697129e63bSEric Liu   std::string Expected = "#include <vector>\n"
2707129e63bSEric Liu                          "void f() {}\n"
2717129e63bSEric Liu                          "#define A \\\n"
2727129e63bSEric Liu                          "  int i;";
2737129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
2747129e63bSEric Liu }
2757129e63bSEric Liu 
TEST_F(HeaderIncludesTest,SkippedTopComment)2767129e63bSEric Liu TEST_F(HeaderIncludesTest, SkippedTopComment) {
2777129e63bSEric Liu   std::string Code = "// comment\n"
2787129e63bSEric Liu                      "\n"
2797129e63bSEric Liu                      "   // comment\n";
2807129e63bSEric Liu   std::string Expected = "// comment\n"
2817129e63bSEric Liu                          "\n"
2827129e63bSEric Liu                          "   // comment\n"
2837129e63bSEric Liu                          "#include <vector>\n";
2847129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
2857129e63bSEric Liu }
2867129e63bSEric Liu 
TEST_F(HeaderIncludesTest,SkippedMixedComments)2877129e63bSEric Liu TEST_F(HeaderIncludesTest, SkippedMixedComments) {
2887129e63bSEric Liu   std::string Code = "// comment\n"
2897129e63bSEric Liu                      "// comment \\\n"
2907129e63bSEric Liu                      " comment continued\n"
2917129e63bSEric Liu                      "/*\n"
2927129e63bSEric Liu                      "* comment\n"
2937129e63bSEric Liu                      "*/\n";
2947129e63bSEric Liu   std::string Expected = "// comment\n"
2957129e63bSEric Liu                          "// comment \\\n"
2967129e63bSEric Liu                          " comment continued\n"
2977129e63bSEric Liu                          "/*\n"
2987129e63bSEric Liu                          "* comment\n"
2997129e63bSEric Liu                          "*/\n"
3007129e63bSEric Liu                          "#include <vector>\n";
3017129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
3027129e63bSEric Liu }
3037129e63bSEric Liu 
TEST_F(HeaderIncludesTest,MultipleBlockCommentsInOneLine)3047129e63bSEric Liu TEST_F(HeaderIncludesTest, MultipleBlockCommentsInOneLine) {
3057129e63bSEric Liu   std::string Code = "/*\n"
3067129e63bSEric Liu                      "* comment\n"
3077129e63bSEric Liu                      "*/ /* comment\n"
3087129e63bSEric Liu                      "*/\n"
3097129e63bSEric Liu                      "\n\n"
3107129e63bSEric Liu                      "/* c1 */ /*c2 */\n";
3117129e63bSEric Liu   std::string Expected = "/*\n"
3127129e63bSEric Liu                          "* comment\n"
3137129e63bSEric Liu                          "*/ /* comment\n"
3147129e63bSEric Liu                          "*/\n"
3157129e63bSEric Liu                          "\n\n"
3167129e63bSEric Liu                          "/* c1 */ /*c2 */\n"
3177129e63bSEric Liu                          "#include <vector>\n";
3187129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
3197129e63bSEric Liu }
3207129e63bSEric Liu 
TEST_F(HeaderIncludesTest,CodeAfterComments)3217129e63bSEric Liu TEST_F(HeaderIncludesTest, CodeAfterComments) {
3227129e63bSEric Liu   std::string Code = "/*\n"
3237129e63bSEric Liu                      "* comment\n"
3247129e63bSEric Liu                      "*/ /* comment\n"
3257129e63bSEric Liu                      "*/\n"
3267129e63bSEric Liu                      "\n\n"
3277129e63bSEric Liu                      "/* c1 */ /*c2 */\n"
3287129e63bSEric Liu                      "\n"
3297129e63bSEric Liu                      "int x;\n";
3307129e63bSEric Liu   std::string Expected = "/*\n"
3317129e63bSEric Liu                          "* comment\n"
3327129e63bSEric Liu                          "*/ /* comment\n"
3337129e63bSEric Liu                          "*/\n"
3347129e63bSEric Liu                          "\n\n"
3357129e63bSEric Liu                          "/* c1 */ /*c2 */\n"
3367129e63bSEric Liu                          "\n"
3377129e63bSEric Liu                          "#include <vector>\n"
3387129e63bSEric Liu                          "int x;\n";
3397129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
3407129e63bSEric Liu }
3417129e63bSEric Liu 
TEST_F(HeaderIncludesTest,FakeHeaderGuardIfDef)3427129e63bSEric Liu TEST_F(HeaderIncludesTest, FakeHeaderGuardIfDef) {
3437129e63bSEric Liu   std::string Code = "// comment \n"
3447129e63bSEric Liu                      "#ifdef X\n"
3457129e63bSEric Liu                      "#define X\n";
3467129e63bSEric Liu   std::string Expected = "// comment \n"
3477129e63bSEric Liu                          "#include <vector>\n"
3487129e63bSEric Liu                          "#ifdef X\n"
3497129e63bSEric Liu                          "#define X\n";
3507129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
3517129e63bSEric Liu }
3527129e63bSEric Liu 
TEST_F(HeaderIncludesTest,RealHeaderGuardAfterComments)3537129e63bSEric Liu TEST_F(HeaderIncludesTest, RealHeaderGuardAfterComments) {
3547129e63bSEric Liu   std::string Code = "// comment \n"
3557129e63bSEric Liu                      "#ifndef X\n"
3567129e63bSEric Liu                      "#define X\n"
3577129e63bSEric Liu                      "int x;\n"
3587129e63bSEric Liu                      "#define Y 1\n";
3597129e63bSEric Liu   std::string Expected = "// comment \n"
3607129e63bSEric Liu                          "#ifndef X\n"
3617129e63bSEric Liu                          "#define X\n"
3627129e63bSEric Liu                          "#include <vector>\n"
3637129e63bSEric Liu                          "int x;\n"
3647129e63bSEric Liu                          "#define Y 1\n";
3657129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
3667129e63bSEric Liu }
3677129e63bSEric Liu 
TEST_F(HeaderIncludesTest,PragmaOnce)36824739613SEric Liu TEST_F(HeaderIncludesTest, PragmaOnce) {
36924739613SEric Liu   std::string Code = "// comment \n"
37024739613SEric Liu                      "#pragma once\n"
37124739613SEric Liu                      "int x;\n";
37224739613SEric Liu   std::string Expected = "// comment \n"
37324739613SEric Liu                          "#pragma once\n"
37424739613SEric Liu                          "#include <vector>\n"
37524739613SEric Liu                          "int x;\n";
37624739613SEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
37724739613SEric Liu }
37824739613SEric Liu 
TEST_F(HeaderIncludesTest,IfNDefWithNoDefine)3797129e63bSEric Liu TEST_F(HeaderIncludesTest, IfNDefWithNoDefine) {
3807129e63bSEric Liu   std::string Code = "// comment \n"
3817129e63bSEric Liu                      "#ifndef X\n"
3827129e63bSEric Liu                      "int x;\n"
3837129e63bSEric Liu                      "#define Y 1\n";
3847129e63bSEric Liu   std::string Expected = "// comment \n"
3857129e63bSEric Liu                          "#include <vector>\n"
3867129e63bSEric Liu                          "#ifndef X\n"
3877129e63bSEric Liu                          "int x;\n"
3887129e63bSEric Liu                          "#define Y 1\n";
3897129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
3907129e63bSEric Liu }
3917129e63bSEric Liu 
TEST_F(HeaderIncludesTest,FakeHeaderGuard)3927129e63bSEric Liu TEST_F(HeaderIncludesTest, FakeHeaderGuard) {
3937129e63bSEric Liu   std::string Code = "// comment \n"
3947129e63bSEric Liu                      "#ifndef X\n"
3957129e63bSEric Liu                      "#define 1\n";
3967129e63bSEric Liu   std::string Expected = "// comment \n"
3977129e63bSEric Liu                          "#include <vector>\n"
3987129e63bSEric Liu                          "#ifndef X\n"
3997129e63bSEric Liu                          "#define 1\n";
4007129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
4017129e63bSEric Liu }
4027129e63bSEric Liu 
TEST_F(HeaderIncludesTest,FakeHeaderGuardIfnDef)403ea8e71c3SKadir Cetinkaya TEST_F(HeaderIncludesTest, FakeHeaderGuardIfnDef) {
404ea8e71c3SKadir Cetinkaya   std::string Code = "#ifndef A_H\n"
405ea8e71c3SKadir Cetinkaya                      "#define A_H 1\n"
406ea8e71c3SKadir Cetinkaya                      "#endif";
407ea8e71c3SKadir Cetinkaya   std::string Expected = "#include \"b.h\"\n"
408ea8e71c3SKadir Cetinkaya                          "#ifndef A_H\n"
409ea8e71c3SKadir Cetinkaya                          "#define A_H 1\n"
410ea8e71c3SKadir Cetinkaya                          "#endif";
411ea8e71c3SKadir Cetinkaya 
412ea8e71c3SKadir Cetinkaya   EXPECT_EQ(Expected, insert(Code, "\"b.h\""));
413ea8e71c3SKadir Cetinkaya }
414ea8e71c3SKadir Cetinkaya 
TEST_F(HeaderIncludesTest,HeaderGuardWithComment)4157129e63bSEric Liu TEST_F(HeaderIncludesTest, HeaderGuardWithComment) {
4167129e63bSEric Liu   std::string Code = "// comment \n"
4177129e63bSEric Liu                      "#ifndef X // comment\n"
4187129e63bSEric Liu                      "// comment\n"
4197129e63bSEric Liu                      "/* comment\n"
4207129e63bSEric Liu                      "*/\n"
4217129e63bSEric Liu                      "/* comment */ #define X\n"
4227129e63bSEric Liu                      "int x;\n"
4237129e63bSEric Liu                      "#define Y 1\n";
4247129e63bSEric Liu   std::string Expected = "// comment \n"
4257129e63bSEric Liu                          "#ifndef X // comment\n"
4267129e63bSEric Liu                          "// comment\n"
4277129e63bSEric Liu                          "/* comment\n"
4287129e63bSEric Liu                          "*/\n"
4297129e63bSEric Liu                          "/* comment */ #define X\n"
4307129e63bSEric Liu                          "#include <vector>\n"
4317129e63bSEric Liu                          "int x;\n"
4327129e63bSEric Liu                          "#define Y 1\n";
4337129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
4347129e63bSEric Liu }
4357129e63bSEric Liu 
TEST_F(HeaderIncludesTest,EmptyCode)4367129e63bSEric Liu TEST_F(HeaderIncludesTest, EmptyCode) {
4377129e63bSEric Liu   std::string Code = "";
4387129e63bSEric Liu   std::string Expected = "#include <vector>\n";
4397129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
4407129e63bSEric Liu }
4417129e63bSEric Liu 
TEST_F(HeaderIncludesTest,NoNewLineAtTheEndOfCode)4427129e63bSEric Liu TEST_F(HeaderIncludesTest, NoNewLineAtTheEndOfCode) {
4437129e63bSEric Liu   std::string Code = "#include <map>";
4447129e63bSEric Liu   std::string Expected = "#include <map>\n#include <vector>\n";
4457129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
4467129e63bSEric Liu }
4477129e63bSEric Liu 
TEST_F(HeaderIncludesTest,SkipExistingHeaders)4487129e63bSEric Liu TEST_F(HeaderIncludesTest, SkipExistingHeaders) {
4497129e63bSEric Liu   std::string Code = "#include \"a.h\"\n"
4507129e63bSEric Liu                      "#include <vector>\n";
4517129e63bSEric Liu   std::string Expected = "#include \"a.h\"\n"
4527129e63bSEric Liu                          "#include <vector>\n";
4537129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<vector>"));
4547129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"a.h\""));
4557129e63bSEric Liu }
4567129e63bSEric Liu 
TEST_F(HeaderIncludesTest,AddIncludesWithDifferentForms)4577129e63bSEric Liu TEST_F(HeaderIncludesTest, AddIncludesWithDifferentForms) {
4587129e63bSEric Liu   std::string Code = "#include <vector>\n";
4597129e63bSEric Liu   // FIXME: this might not be the best behavior.
4607129e63bSEric Liu   std::string Expected = "#include \"vector\"\n"
4617129e63bSEric Liu                          "#include <vector>\n";
4627129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"vector\""));
4637129e63bSEric Liu }
4647129e63bSEric Liu 
TEST_F(HeaderIncludesTest,NoInsertionAfterCode)4657129e63bSEric Liu TEST_F(HeaderIncludesTest, NoInsertionAfterCode) {
4667129e63bSEric Liu   std::string Code = "#include \"a.h\"\n"
4677129e63bSEric Liu                      "void f() {}\n"
4687129e63bSEric Liu                      "#include \"b.h\"\n";
4697129e63bSEric Liu   std::string Expected = "#include \"a.h\"\n"
4707129e63bSEric Liu                          "#include \"c.h\"\n"
4717129e63bSEric Liu                          "void f() {}\n"
4727129e63bSEric Liu                          "#include \"b.h\"\n";
4737129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
4747129e63bSEric Liu }
4757129e63bSEric Liu 
TEST_F(HeaderIncludesTest,NoInsertionInStringLiteral)4767129e63bSEric Liu TEST_F(HeaderIncludesTest, NoInsertionInStringLiteral) {
4777129e63bSEric Liu   std::string Code = "#include \"a.h\"\n"
4787129e63bSEric Liu                      "const char[] = R\"(\n"
4797129e63bSEric Liu                      "#include \"b.h\"\n"
4807129e63bSEric Liu                      ")\";\n";
4817129e63bSEric Liu   std::string Expected = "#include \"a.h\"\n"
4827129e63bSEric Liu                          "#include \"c.h\"\n"
4837129e63bSEric Liu                          "const char[] = R\"(\n"
4847129e63bSEric Liu                          "#include \"b.h\"\n"
4857129e63bSEric Liu                          ")\";\n";
4867129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
4877129e63bSEric Liu }
4887129e63bSEric Liu 
TEST_F(HeaderIncludesTest,NoInsertionAfterOtherDirective)4897129e63bSEric Liu TEST_F(HeaderIncludesTest, NoInsertionAfterOtherDirective) {
4907129e63bSEric Liu   std::string Code = "#include \"a.h\"\n"
4917129e63bSEric Liu                      "#ifdef X\n"
4927129e63bSEric Liu                      "#include \"b.h\"\n"
4937129e63bSEric Liu                      "#endif\n";
4947129e63bSEric Liu   std::string Expected = "#include \"a.h\"\n"
4957129e63bSEric Liu                          "#include \"c.h\"\n"
4967129e63bSEric Liu                          "#ifdef X\n"
4977129e63bSEric Liu                          "#include \"b.h\"\n"
4987129e63bSEric Liu                          "#endif\n";
4997129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
5007129e63bSEric Liu }
5017129e63bSEric Liu 
TEST_F(HeaderIncludesTest,CanInsertAfterLongSystemInclude)5027129e63bSEric Liu TEST_F(HeaderIncludesTest, CanInsertAfterLongSystemInclude) {
5037129e63bSEric Liu   std::string Code = "#include \"a.h\"\n"
5047129e63bSEric Liu                      "// comment\n\n"
5057129e63bSEric Liu                      "#include <a/b/c/d/e.h>\n";
5067129e63bSEric Liu   std::string Expected = "#include \"a.h\"\n"
5077129e63bSEric Liu                          "// comment\n\n"
5087129e63bSEric Liu                          "#include <a/b/c/d/e.h>\n"
5097129e63bSEric Liu                          "#include <x.h>\n";
5107129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "<x.h>"));
5117129e63bSEric Liu }
5127129e63bSEric Liu 
TEST_F(HeaderIncludesTest,CanInsertAfterComment)5137129e63bSEric Liu TEST_F(HeaderIncludesTest, CanInsertAfterComment) {
5147129e63bSEric Liu   std::string Code = "#include \"a.h\"\n"
5157129e63bSEric Liu                      "// Comment\n"
5167129e63bSEric Liu                      "\n"
5177129e63bSEric Liu                      "/* Comment */\n"
5187129e63bSEric Liu                      "// Comment\n"
5197129e63bSEric Liu                      "\n"
5207129e63bSEric Liu                      "#include \"b.h\"\n";
5217129e63bSEric Liu   std::string Expected = "#include \"a.h\"\n"
5227129e63bSEric Liu                          "// Comment\n"
5237129e63bSEric Liu                          "\n"
5247129e63bSEric Liu                          "/* Comment */\n"
5257129e63bSEric Liu                          "// Comment\n"
5267129e63bSEric Liu                          "\n"
5277129e63bSEric Liu                          "#include \"b.h\"\n"
5287129e63bSEric Liu                          "#include \"c.h\"\n";
5297129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"c.h\""));
5307129e63bSEric Liu }
5317129e63bSEric Liu 
TEST_F(HeaderIncludesTest,LongCommentsInTheBeginningOfFile)5327129e63bSEric Liu TEST_F(HeaderIncludesTest, LongCommentsInTheBeginningOfFile) {
5337129e63bSEric Liu   std::string Code = "// Loooooooooooooooooooooooooong comment\n"
5347129e63bSEric Liu                      "// Loooooooooooooooooooooooooong comment\n"
5357129e63bSEric Liu                      "// Loooooooooooooooooooooooooong comment\n"
5367129e63bSEric Liu                      "#include <string>\n"
5377129e63bSEric Liu                      "#include <vector>\n"
5387129e63bSEric Liu                      "\n"
5397129e63bSEric Liu                      "#include \"a.h\"\n"
5407129e63bSEric Liu                      "#include \"b.h\"\n";
5417129e63bSEric Liu   std::string Expected = "// Loooooooooooooooooooooooooong comment\n"
5427129e63bSEric Liu                          "// Loooooooooooooooooooooooooong comment\n"
5437129e63bSEric Liu                          "// Loooooooooooooooooooooooooong comment\n"
5447129e63bSEric Liu                          "#include <string>\n"
5457129e63bSEric Liu                          "#include <vector>\n"
5467129e63bSEric Liu                          "\n"
5477129e63bSEric Liu                          "#include \"a.h\"\n"
5487129e63bSEric Liu                          "#include \"b.h\"\n"
5497129e63bSEric Liu                          "#include \"third.h\"\n";
5507129e63bSEric Liu   Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
5517129e63bSEric Liu               .IncludeStyle;
5527129e63bSEric Liu   EXPECT_EQ(Expected, insert(Code, "\"third.h\""));
5537129e63bSEric Liu }
5547129e63bSEric Liu 
TEST_F(HeaderIncludesTest,SimpleDeleteInclude)5557129e63bSEric Liu TEST_F(HeaderIncludesTest, SimpleDeleteInclude) {
5567129e63bSEric Liu   std::string Code = "#include \"abc.h\"\n"
5577129e63bSEric Liu                      "#include \"xyz.h\" // comment\n"
5587129e63bSEric Liu                      "int x;\n";
5597129e63bSEric Liu   std::string Expected = "#include \"abc.h\"\n"
5607129e63bSEric Liu                          "int x;\n";
5617129e63bSEric Liu   EXPECT_EQ(Expected, remove(Code, "\"xyz.h\""));
5627129e63bSEric Liu }
5637129e63bSEric Liu 
TEST_F(HeaderIncludesTest,DeleteQuotedOnly)5647129e63bSEric Liu TEST_F(HeaderIncludesTest, DeleteQuotedOnly) {
5657129e63bSEric Liu   std::string Code = "#include \"abc.h\"\n"
5667129e63bSEric Liu                      "#include <abc.h>\n"
5677129e63bSEric Liu                      "int x;\n";
5687129e63bSEric Liu   std::string Expected = "#include <abc.h>\n"
5697129e63bSEric Liu                          "int x;\n";
5707129e63bSEric Liu   EXPECT_EQ(Expected, remove(Code, "\"abc.h\""));
5717129e63bSEric Liu }
5727129e63bSEric Liu 
TEST_F(HeaderIncludesTest,DeleteAllCode)5737129e63bSEric Liu TEST_F(HeaderIncludesTest, DeleteAllCode) {
5747129e63bSEric Liu   std::string Code = "#include \"xyz.h\"\n";
5757129e63bSEric Liu   std::string Expected = "";
5767129e63bSEric Liu   EXPECT_EQ(Expected, remove(Code, "\"xyz.h\""));
5777129e63bSEric Liu }
5787129e63bSEric Liu 
TEST_F(HeaderIncludesTest,DeleteOnlyIncludesWithSameQuote)5797129e63bSEric Liu TEST_F(HeaderIncludesTest, DeleteOnlyIncludesWithSameQuote) {
5807129e63bSEric Liu   std::string Code = "#include \"xyz.h\"\n"
5817129e63bSEric Liu                      "#include \"xyz\"\n"
5827129e63bSEric Liu                      "#include <xyz.h>\n";
5837129e63bSEric Liu   std::string Expected = "#include \"xyz.h\"\n"
5847129e63bSEric Liu                          "#include \"xyz\"\n";
5857129e63bSEric Liu   EXPECT_EQ(Expected, remove(Code, "<xyz.h>"));
5867129e63bSEric Liu }
5877129e63bSEric Liu 
TEST_F(HeaderIncludesTest,CanDeleteAfterCode)5887129e63bSEric Liu TEST_F(HeaderIncludesTest, CanDeleteAfterCode) {
5897129e63bSEric Liu   std::string Code = "#include \"a.h\"\n"
5907129e63bSEric Liu                      "void f() {}\n"
5917129e63bSEric Liu                      "#include \"b.h\"\n";
5927129e63bSEric Liu   std::string Expected = "#include \"a.h\"\n"
5937129e63bSEric Liu                          "void f() {}\n";
5947129e63bSEric Liu   EXPECT_EQ(Expected, remove(Code, "\"b.h\""));
5957129e63bSEric Liu }
5967129e63bSEric Liu 
5977129e63bSEric Liu } // namespace
5987129e63bSEric Liu } // namespace tooling
5997129e63bSEric Liu } // namespace clang
600