Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 1 | //===-- IncludeFixerTest.cpp - Include fixer 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 | |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 10 | #include "InMemorySymbolIndex.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 11 | #include "IncludeFixer.h" |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 12 | #include "SymbolIndexManager.h" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 13 | #include "unittests/Tooling/RewriterTestContext.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 14 | #include "clang/Tooling/Tooling.h" |
| 15 | #include "gtest/gtest.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 16 | |
| 17 | namespace clang { |
| 18 | namespace include_fixer { |
| 19 | namespace { |
| 20 | |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 21 | using find_all_symbols::SymbolInfo; |
| 22 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 23 | static bool runOnCode(tooling::ToolAction *ToolAction, StringRef Code, |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 24 | StringRef FileName, |
| 25 | const std::vector<std::string> &ExtraArgs) { |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 26 | llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 27 | new vfs::InMemoryFileSystem); |
| 28 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
| 29 | new FileManager(FileSystemOptions(), InMemoryFileSystem)); |
Benjamin Kramer | 2ecd090 | 2016-05-18 09:28:45 +0000 | [diff] [blame] | 30 | // FIXME: Investigate why -fms-compatibility breaks tests. |
| 31 | std::vector<std::string> Args = {"include_fixer", "-fsyntax-only", |
| 32 | "-fno-ms-compatibility", FileName}; |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 33 | Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 34 | tooling::ToolInvocation Invocation( |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 35 | Args, ToolAction, Files.get(), |
| 36 | std::make_shared<PCHContainerOperations>()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 37 | |
| 38 | InMemoryFileSystem->addFile(FileName, 0, |
| 39 | llvm::MemoryBuffer::getMemBuffer(Code)); |
| 40 | |
| 41 | InMemoryFileSystem->addFile("foo.h", 0, |
| 42 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 43 | InMemoryFileSystem->addFile("dir/bar.h", 0, |
| 44 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 45 | InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0, |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 46 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 47 | return Invocation.run(); |
| 48 | } |
| 49 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 50 | static std::string runIncludeFixer( |
| 51 | StringRef Code, |
| 52 | const std::vector<std::string> &ExtraArgs = std::vector<std::string>()) { |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 53 | std::vector<SymbolInfo> Symbols = { |
| 54 | SymbolInfo("string", SymbolInfo::SymbolKind::Class, "<string>", 1, |
| 55 | {{SymbolInfo::ContextType::Namespace, "std"}}), |
| 56 | SymbolInfo("sting", SymbolInfo::SymbolKind::Class, "\"sting\"", 1, |
| 57 | {{SymbolInfo::ContextType::Namespace, "std"}}), |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 58 | SymbolInfo("foo", SymbolInfo::SymbolKind::Class, "\"dir/otherdir/qux.h\"", |
| 59 | 1, {{SymbolInfo::ContextType::Namespace, "b"}, |
| 60 | {SymbolInfo::ContextType::Namespace, "a"}}), |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 61 | SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar.h\"", |
| 62 | 1, {{SymbolInfo::ContextType::Namespace, "b"}, |
| 63 | {SymbolInfo::ContextType::Namespace, "a"}}), |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 64 | SymbolInfo("Green", SymbolInfo::SymbolKind::Class, "\"color.h\"", |
| 65 | 1, {{SymbolInfo::ContextType::EnumDecl, "Color"}, |
| 66 | {SymbolInfo::ContextType::Namespace, "b"}, |
| 67 | {SymbolInfo::ContextType::Namespace, "a"}}), |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 68 | }; |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 69 | auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>(); |
| 70 | SymbolIndexMgr->addSymbolIndex( |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 71 | llvm::make_unique<include_fixer::InMemorySymbolIndex>(Symbols)); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 72 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 73 | std::set<std::string> Headers; |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 74 | std::vector<clang::tooling::Replacement> Replacements; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 75 | IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements, |
| 76 | "llvm"); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 77 | runOnCode(&Factory, Code, "input.cc", ExtraArgs); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 78 | clang::RewriterTestContext Context; |
| 79 | clang::FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 80 | clang::tooling::applyAllReplacements(Replacements, Context.Rewrite); |
| 81 | return Context.getRewrittenText(ID); |
| 82 | } |
| 83 | |
| 84 | TEST(IncludeFixer, Typo) { |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 85 | EXPECT_EQ("#include <string>\n\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 86 | runIncludeFixer("std::string foo;\n")); |
| 87 | |
| 88 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 89 | "// comment\n#include \"foo.h\"\n#include <string>\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 90 | "#include \"dir/bar.h\"\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 91 | runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 92 | "#include \"dir/bar.h\"\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 93 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 94 | EXPECT_EQ("#include \"foo.h\"\n#include <string>\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 95 | runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n")); |
| 96 | |
| 97 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 98 | "#include \"foo.h\"\n#include <string>\nstd::string::size_type foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 99 | runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); |
| 100 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 101 | // string without "std::" can also be fixed since fixed db results go through |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 102 | // SymbolIndexManager, and SymbolIndexManager matches unqualified identifiers |
| 103 | // too. |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 104 | EXPECT_EQ("#include <string>\n\nstring foo;\n", |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 105 | runIncludeFixer("string foo;\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST(IncludeFixer, IncompleteType) { |
| 109 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 110 | "#include \"foo.h\"\n#include <string>\n" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 111 | "namespace std {\nclass string;\n}\nstring foo;\n", |
| 112 | runIncludeFixer("#include \"foo.h\"\n" |
| 113 | "namespace std {\nclass string;\n}\nstring foo;\n")); |
| 114 | } |
| 115 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 116 | TEST(IncludeFixer, MinimizeInclude) { |
| 117 | std::vector<std::string> IncludePath = {"-Idir/"}; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 118 | EXPECT_EQ("#include \"otherdir/qux.h\"\n\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 119 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 120 | |
| 121 | IncludePath = {"-isystemdir"}; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 122 | EXPECT_EQ("#include <otherdir/qux.h>\n\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 123 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 124 | |
| 125 | IncludePath = {"-iquotedir"}; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 126 | EXPECT_EQ("#include \"otherdir/qux.h\"\n\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 127 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 128 | |
| 129 | IncludePath = {"-Idir", "-Idir/otherdir"}; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 130 | EXPECT_EQ("#include \"qux.h\"\n\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 131 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 132 | } |
| 133 | |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 134 | TEST(IncludeFixer, NestedName) { |
NAKAMURA Takumi | 62e8ec5 | 2016-05-18 00:20:46 +0000 | [diff] [blame] | 135 | // Some tests don't pass for target *-win32. |
| 136 | std::vector<std::string> args = {"-target", "x86_64-unknown-unknown"}; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 137 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n\n" |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 138 | "int x = a::b::foo(0);\n", |
NAKAMURA Takumi | 62e8ec5 | 2016-05-18 00:20:46 +0000 | [diff] [blame] | 139 | runIncludeFixer("int x = a::b::foo(0);\n", args)); |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 140 | |
| 141 | // FIXME: Handle simple macros. |
| 142 | EXPECT_EQ("#define FOO a::b::foo\nint x = FOO;\n", |
| 143 | runIncludeFixer("#define FOO a::b::foo\nint x = FOO;\n")); |
| 144 | EXPECT_EQ("#define FOO(x) a::##x\nint x = FOO(b::foo);\n", |
| 145 | runIncludeFixer("#define FOO(x) a::##x\nint x = FOO(b::foo);\n")); |
| 146 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 147 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n\n" |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 148 | "namespace a {}\nint a = a::b::foo(0);\n", |
NAKAMURA Takumi | 62e8ec5 | 2016-05-18 00:20:46 +0000 | [diff] [blame] | 149 | runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n", args)); |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 152 | TEST(IncludeFixer, MultipleMissingSymbols) { |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 153 | EXPECT_EQ("#include <string>\n\nstd::string bar;\nstd::sting foo;\n", |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 154 | runIncludeFixer("std::string bar;\nstd::sting foo;\n")); |
| 155 | } |
| 156 | |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 157 | TEST(IncludeFixer, ScopedNamespaceSymbols) { |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 158 | EXPECT_EQ("#include \"bar.h\"\n\nnamespace a {\nb::bar b;\n}", |
| 159 | runIncludeFixer("namespace a {\nb::bar b;\n}")); |
| 160 | EXPECT_EQ("#include \"bar.h\"\n\nnamespace A {\na::b::bar b;\n}", |
| 161 | runIncludeFixer("namespace A {\na::b::bar b;\n}")); |
| 162 | EXPECT_EQ("#include \"bar.h\"\n\nnamespace a {\nvoid func() { b::bar b; }\n}", |
| 163 | runIncludeFixer("namespace a {\nvoid func() { b::bar b; }\n}")); |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 164 | EXPECT_EQ("namespace A { c::b::bar b; }\n", |
| 165 | runIncludeFixer("namespace A { c::b::bar b; }\n")); |
| 166 | // FIXME: The header should not be added here. Remove this after we support |
| 167 | // full match. |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 168 | EXPECT_EQ("#include \"bar.h\"\n\nnamespace A {\nb::bar b;\n}", |
| 169 | runIncludeFixer("namespace A {\nb::bar b;\n}")); |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 170 | } |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 171 | |
| 172 | TEST(IncludeFixer, EnumConstantSymbols) { |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 173 | EXPECT_EQ("#include \"color.h\"\n\nint test = a::b::Green;\n", |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 174 | runIncludeFixer("int test = a::b::Green;\n")); |
| 175 | } |
| 176 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame^] | 177 | // FIXME: add test cases for inserting and sorting multiple headers when |
| 178 | // include-fixer supports multiple headers insertion. |
| 179 | TEST(IncludeFixer, InsertAndSortSingleHeader) { |
| 180 | // Insert one header. |
| 181 | std::string Code = "#include \"a.h\"\n" |
| 182 | "#include \"foo.h\"\n" |
| 183 | "\n" |
| 184 | "namespace a { b::bar b; }"; |
| 185 | std::string Expected = "#include \"a.h\"\n" |
| 186 | "#include \"bar.h\"\n" |
| 187 | "#include \"foo.h\"\n" |
| 188 | "\n" |
| 189 | "namespace a { b::bar b; }"; |
| 190 | EXPECT_EQ(Expected, runIncludeFixer(Code)); |
| 191 | } |
| 192 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 193 | } // namespace |
| 194 | } // namespace include_fixer |
| 195 | } // namespace clang |