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 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 73 | IncludeFixerContext FixerContext; |
| 74 | IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm"); |
| 75 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 76 | runOnCode(&Factory, Code, "input.cc", ExtraArgs); |
Eric Liu | f83187d | 2016-05-31 13:52:59 +0000 | [diff] [blame^] | 77 | if (FixerContext.Headers.empty()) |
| 78 | return Code; |
| 79 | tooling::Replacements Replacements = |
| 80 | clang::include_fixer::createInsertHeaderReplacements( |
| 81 | Code, "input.cc", FixerContext.Headers.front(), |
| 82 | FixerContext.FirstIncludeOffset); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 83 | clang::RewriterTestContext Context; |
| 84 | clang::FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 85 | clang::tooling::applyAllReplacements(Replacements, Context.Rewrite); |
| 86 | return Context.getRewrittenText(ID); |
| 87 | } |
| 88 | |
| 89 | TEST(IncludeFixer, Typo) { |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 90 | EXPECT_EQ("#include <string>\n\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 91 | runIncludeFixer("std::string foo;\n")); |
| 92 | |
| 93 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 94 | "// comment\n#include \"foo.h\"\n#include <string>\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 95 | "#include \"dir/bar.h\"\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 96 | runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 97 | "#include \"dir/bar.h\"\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 98 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 99 | EXPECT_EQ("#include \"foo.h\"\n#include <string>\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 100 | runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n")); |
| 101 | |
| 102 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 103 | "#include \"foo.h\"\n#include <string>\nstd::string::size_type foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 104 | runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); |
| 105 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 106 | // 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] | 107 | // SymbolIndexManager, and SymbolIndexManager matches unqualified identifiers |
| 108 | // too. |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 109 | EXPECT_EQ("#include <string>\n\nstring foo;\n", |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 110 | runIncludeFixer("string foo;\n")); |
Benjamin Kramer | 9b15b6f | 2016-05-19 12:41:56 +0000 | [diff] [blame] | 111 | |
| 112 | // Fully qualified name. |
| 113 | EXPECT_EQ("#include <string>\n\n::std::string foo;\n", |
| 114 | runIncludeFixer("::std::string foo;\n")); |
| 115 | // Should not match std::string. |
| 116 | EXPECT_EQ("::string foo;\n", runIncludeFixer("::string foo;\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST(IncludeFixer, IncompleteType) { |
| 120 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 121 | "#include \"foo.h\"\n#include <string>\n" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 122 | "namespace std {\nclass string;\n}\nstring foo;\n", |
| 123 | runIncludeFixer("#include \"foo.h\"\n" |
| 124 | "namespace std {\nclass string;\n}\nstring foo;\n")); |
| 125 | } |
| 126 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 127 | TEST(IncludeFixer, MinimizeInclude) { |
| 128 | std::vector<std::string> IncludePath = {"-Idir/"}; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 129 | EXPECT_EQ("#include \"otherdir/qux.h\"\n\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 130 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 131 | |
| 132 | IncludePath = {"-isystemdir"}; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 133 | EXPECT_EQ("#include <otherdir/qux.h>\n\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 134 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 135 | |
| 136 | IncludePath = {"-iquotedir"}; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 137 | EXPECT_EQ("#include \"otherdir/qux.h\"\n\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 138 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 139 | |
| 140 | IncludePath = {"-Idir", "-Idir/otherdir"}; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 141 | EXPECT_EQ("#include \"qux.h\"\n\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 142 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 143 | } |
| 144 | |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 145 | TEST(IncludeFixer, NestedName) { |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 146 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n\n" |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 147 | "int x = a::b::foo(0);\n", |
Benjamin Kramer | 04666a2 | 2016-05-19 11:39:56 +0000 | [diff] [blame] | 148 | runIncludeFixer("int x = a::b::foo(0);\n")); |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 149 | |
| 150 | // FIXME: Handle simple macros. |
| 151 | EXPECT_EQ("#define FOO a::b::foo\nint x = FOO;\n", |
| 152 | runIncludeFixer("#define FOO a::b::foo\nint x = FOO;\n")); |
| 153 | EXPECT_EQ("#define FOO(x) a::##x\nint x = FOO(b::foo);\n", |
| 154 | runIncludeFixer("#define FOO(x) a::##x\nint x = FOO(b::foo);\n")); |
| 155 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 156 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n\n" |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 157 | "namespace a {}\nint a = a::b::foo(0);\n", |
Benjamin Kramer | 04666a2 | 2016-05-19 11:39:56 +0000 | [diff] [blame] | 158 | runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n")); |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 161 | TEST(IncludeFixer, MultipleMissingSymbols) { |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 162 | EXPECT_EQ("#include <string>\n\nstd::string bar;\nstd::sting foo;\n", |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 163 | runIncludeFixer("std::string bar;\nstd::sting foo;\n")); |
| 164 | } |
| 165 | |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 166 | TEST(IncludeFixer, ScopedNamespaceSymbols) { |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 167 | EXPECT_EQ("#include \"bar.h\"\n\nnamespace a {\nb::bar b;\n}", |
| 168 | runIncludeFixer("namespace a {\nb::bar b;\n}")); |
| 169 | EXPECT_EQ("#include \"bar.h\"\n\nnamespace A {\na::b::bar b;\n}", |
| 170 | runIncludeFixer("namespace A {\na::b::bar b;\n}")); |
| 171 | EXPECT_EQ("#include \"bar.h\"\n\nnamespace a {\nvoid func() { b::bar b; }\n}", |
| 172 | runIncludeFixer("namespace a {\nvoid func() { b::bar b; }\n}")); |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 173 | EXPECT_EQ("namespace A { c::b::bar b; }\n", |
| 174 | runIncludeFixer("namespace A { c::b::bar b; }\n")); |
| 175 | // FIXME: The header should not be added here. Remove this after we support |
| 176 | // full match. |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 177 | EXPECT_EQ("#include \"bar.h\"\n\nnamespace A {\nb::bar b;\n}", |
| 178 | runIncludeFixer("namespace A {\nb::bar b;\n}")); |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 179 | } |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 180 | |
| 181 | TEST(IncludeFixer, EnumConstantSymbols) { |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 182 | EXPECT_EQ("#include \"color.h\"\n\nint test = a::b::Green;\n", |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 183 | runIncludeFixer("int test = a::b::Green;\n")); |
| 184 | } |
| 185 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 186 | // FIXME: add test cases for inserting and sorting multiple headers when |
| 187 | // include-fixer supports multiple headers insertion. |
| 188 | TEST(IncludeFixer, InsertAndSortSingleHeader) { |
| 189 | // Insert one header. |
| 190 | std::string Code = "#include \"a.h\"\n" |
| 191 | "#include \"foo.h\"\n" |
| 192 | "\n" |
| 193 | "namespace a { b::bar b; }"; |
| 194 | std::string Expected = "#include \"a.h\"\n" |
| 195 | "#include \"bar.h\"\n" |
| 196 | "#include \"foo.h\"\n" |
| 197 | "\n" |
| 198 | "namespace a { b::bar b; }"; |
| 199 | EXPECT_EQ(Expected, runIncludeFixer(Code)); |
| 200 | } |
| 201 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 202 | } // namespace |
| 203 | } // namespace include_fixer |
| 204 | } // namespace clang |