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"}}), |
| 58 | SymbolInfo("size_type", SymbolInfo::SymbolKind::Variable, "<string>", 1, |
| 59 | {{SymbolInfo::ContextType::Namespace, "string"}, |
| 60 | {SymbolInfo::ContextType::Namespace, "std"}}), |
| 61 | SymbolInfo("foo", SymbolInfo::SymbolKind::Class, "\"dir/otherdir/qux.h\"", |
| 62 | 1, {{SymbolInfo::ContextType::Namespace, "b"}, |
| 63 | {SymbolInfo::ContextType::Namespace, "a"}}), |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 64 | SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar.h\"", |
| 65 | 1, {{SymbolInfo::ContextType::Namespace, "b"}, |
| 66 | {SymbolInfo::ContextType::Namespace, "a"}}), |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 67 | SymbolInfo("Green", SymbolInfo::SymbolKind::Class, "\"color.h\"", |
| 68 | 1, {{SymbolInfo::ContextType::EnumDecl, "Color"}, |
| 69 | {SymbolInfo::ContextType::Namespace, "b"}, |
| 70 | {SymbolInfo::ContextType::Namespace, "a"}}), |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 71 | }; |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 72 | auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>(); |
| 73 | SymbolIndexMgr->addSymbolIndex( |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 74 | llvm::make_unique<include_fixer::InMemorySymbolIndex>(Symbols)); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 75 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 76 | std::vector<clang::tooling::Replacement> Replacements; |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 77 | IncludeFixerActionFactory Factory(*SymbolIndexMgr, Replacements); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 78 | runOnCode(&Factory, Code, "input.cc", ExtraArgs); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 79 | clang::RewriterTestContext Context; |
| 80 | clang::FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 81 | clang::tooling::applyAllReplacements(Replacements, Context.Rewrite); |
| 82 | return Context.getRewrittenText(ID); |
| 83 | } |
| 84 | |
| 85 | TEST(IncludeFixer, Typo) { |
| 86 | EXPECT_EQ("#include <string>\nstd::string foo;\n", |
| 87 | runIncludeFixer("std::string foo;\n")); |
| 88 | |
| 89 | EXPECT_EQ( |
| 90 | "// comment\n#include <string>\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 91 | "#include \"dir/bar.h\"\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 92 | runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 93 | "#include \"dir/bar.h\"\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 94 | |
| 95 | EXPECT_EQ("#include <string>\n#include \"foo.h\"\nstd::string foo;\n", |
| 96 | runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n")); |
| 97 | |
| 98 | EXPECT_EQ( |
| 99 | "#include <string>\n#include \"foo.h\"\nstd::string::size_type foo;\n", |
| 100 | runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); |
| 101 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 102 | // 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] | 103 | // SymbolIndexManager, and SymbolIndexManager matches unqualified identifiers |
| 104 | // too. |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 105 | EXPECT_EQ("#include <string>\nstring foo;\n", |
| 106 | runIncludeFixer("string foo;\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST(IncludeFixer, IncompleteType) { |
| 110 | EXPECT_EQ( |
| 111 | "#include <string>\n#include \"foo.h\"\n" |
| 112 | "namespace std {\nclass string;\n}\nstring foo;\n", |
| 113 | runIncludeFixer("#include \"foo.h\"\n" |
| 114 | "namespace std {\nclass string;\n}\nstring foo;\n")); |
| 115 | } |
| 116 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 117 | TEST(IncludeFixer, MinimizeInclude) { |
| 118 | std::vector<std::string> IncludePath = {"-Idir/"}; |
| 119 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 120 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 121 | |
| 122 | IncludePath = {"-isystemdir"}; |
| 123 | EXPECT_EQ("#include <otherdir/qux.h>\na::b::foo bar;\n", |
| 124 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 125 | |
| 126 | IncludePath = {"-iquotedir"}; |
| 127 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 128 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 129 | |
| 130 | IncludePath = {"-Idir", "-Idir/otherdir"}; |
| 131 | EXPECT_EQ("#include \"qux.h\"\na::b::foo bar;\n", |
| 132 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 133 | } |
| 134 | |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 135 | TEST(IncludeFixer, NestedName) { |
NAKAMURA Takumi | 62e8ec5 | 2016-05-18 00:20:46 +0000 | [diff] [blame] | 136 | // Some tests don't pass for target *-win32. |
| 137 | std::vector<std::string> args = {"-target", "x86_64-unknown-unknown"}; |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 138 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n" |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 139 | "int x = a::b::foo(0);\n", |
NAKAMURA Takumi | 62e8ec5 | 2016-05-18 00:20:46 +0000 | [diff] [blame] | 140 | runIncludeFixer("int x = a::b::foo(0);\n", args)); |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 141 | |
| 142 | // FIXME: Handle simple macros. |
| 143 | EXPECT_EQ("#define FOO a::b::foo\nint x = FOO;\n", |
| 144 | runIncludeFixer("#define FOO a::b::foo\nint x = FOO;\n")); |
| 145 | EXPECT_EQ("#define FOO(x) a::##x\nint x = FOO(b::foo);\n", |
| 146 | runIncludeFixer("#define FOO(x) a::##x\nint x = FOO(b::foo);\n")); |
| 147 | |
| 148 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n" |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 149 | "namespace a {}\nint a = a::b::foo(0);\n", |
NAKAMURA Takumi | 62e8ec5 | 2016-05-18 00:20:46 +0000 | [diff] [blame] | 150 | runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n", args)); |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 153 | TEST(IncludeFixer, MultipleMissingSymbols) { |
| 154 | EXPECT_EQ("#include <string>\nstd::string bar;\nstd::sting foo;\n", |
| 155 | runIncludeFixer("std::string bar;\nstd::sting foo;\n")); |
| 156 | } |
| 157 | |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 158 | TEST(IncludeFixer, ScopedNamespaceSymbols) { |
| 159 | EXPECT_EQ("#include \"bar.h\"\nnamespace a { b::bar b; }\n", |
| 160 | runIncludeFixer("namespace a { b::bar b; }\n")); |
| 161 | EXPECT_EQ("#include \"bar.h\"\nnamespace A { a::b::bar b; }\n", |
| 162 | runIncludeFixer("namespace A { a::b::bar b; }\n")); |
| 163 | EXPECT_EQ("#include \"bar.h\"\nnamespace a { void func() { b::bar b; } }\n", |
| 164 | runIncludeFixer("namespace a { void func() { b::bar b; } }\n")); |
| 165 | EXPECT_EQ("namespace A { c::b::bar b; }\n", |
| 166 | runIncludeFixer("namespace A { c::b::bar b; }\n")); |
| 167 | // FIXME: The header should not be added here. Remove this after we support |
| 168 | // full match. |
| 169 | EXPECT_EQ("#include \"bar.h\"\nnamespace A { b::bar b; }\n", |
| 170 | runIncludeFixer("namespace A { b::bar b; }\n")); |
| 171 | } |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 172 | |
| 173 | TEST(IncludeFixer, EnumConstantSymbols) { |
| 174 | EXPECT_EQ("#include \"color.h\"\nint test = a::b::Green;\n", |
| 175 | runIncludeFixer("int test = a::b::Green;\n")); |
| 176 | } |
| 177 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 178 | } // namespace |
| 179 | } // namespace include_fixer |
| 180 | } // namespace clang |