blob: dacd10eb640739a03564f7d7a660398fcd4399ca [file] [log] [blame]
Benjamin Kramer6b236262016-04-20 12:43:43 +00001//===-- 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 Kramera3d82332016-05-13 09:27:54 +000010#include "InMemorySymbolIndex.h"
Benjamin Kramer6b236262016-04-20 12:43:43 +000011#include "IncludeFixer.h"
Benjamin Kramera3d82332016-05-13 09:27:54 +000012#include "SymbolIndexManager.h"
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000013#include "unittests/Tooling/RewriterTestContext.h"
Benjamin Kramer6b236262016-04-20 12:43:43 +000014#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
Benjamin Kramer6b236262016-04-20 12:43:43 +000016
17namespace clang {
18namespace include_fixer {
19namespace {
20
Haojian Wu631e5f22016-05-13 15:17:17 +000021using find_all_symbols::SymbolInfo;
22
Benjamin Kramer6b236262016-04-20 12:43:43 +000023static bool runOnCode(tooling::ToolAction *ToolAction, StringRef Code,
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000024 StringRef FileName,
25 const std::vector<std::string> &ExtraArgs) {
Benjamin Kramer6b236262016-04-20 12:43:43 +000026 llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
27 new vfs::InMemoryFileSystem);
28 llvm::IntrusiveRefCntPtr<FileManager> Files(
29 new FileManager(FileSystemOptions(), InMemoryFileSystem));
Benjamin Kramer2ecd0902016-05-18 09:28:45 +000030 // FIXME: Investigate why -fms-compatibility breaks tests.
31 std::vector<std::string> Args = {"include_fixer", "-fsyntax-only",
32 "-fno-ms-compatibility", FileName};
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000033 Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
Benjamin Kramer6b236262016-04-20 12:43:43 +000034 tooling::ToolInvocation Invocation(
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000035 Args, ToolAction, Files.get(),
36 std::make_shared<PCHContainerOperations>());
Benjamin Kramer6b236262016-04-20 12:43:43 +000037
38 InMemoryFileSystem->addFile(FileName, 0,
39 llvm::MemoryBuffer::getMemBuffer(Code));
40
41 InMemoryFileSystem->addFile("foo.h", 0,
42 llvm::MemoryBuffer::getMemBuffer("\n"));
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000043 InMemoryFileSystem->addFile("dir/bar.h", 0,
44 llvm::MemoryBuffer::getMemBuffer("\n"));
45 InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
Benjamin Kramer6b236262016-04-20 12:43:43 +000046 llvm::MemoryBuffer::getMemBuffer("\n"));
47 return Invocation.run();
48}
49
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000050static std::string runIncludeFixer(
51 StringRef Code,
52 const std::vector<std::string> &ExtraArgs = std::vector<std::string>()) {
Haojian Wu631e5f22016-05-13 15:17:17 +000053 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 Wu57cdcb02016-05-13 15:44:16 +000064 SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar.h\"",
65 1, {{SymbolInfo::ContextType::Namespace, "b"},
66 {SymbolInfo::ContextType::Namespace, "a"}}),
Haojian Wuff6d1952016-05-18 09:04:43 +000067 SymbolInfo("Green", SymbolInfo::SymbolKind::Class, "\"color.h\"",
68 1, {{SymbolInfo::ContextType::EnumDecl, "Color"},
69 {SymbolInfo::ContextType::Namespace, "b"},
70 {SymbolInfo::ContextType::Namespace, "a"}}),
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000071 };
Benjamin Kramera3d82332016-05-13 09:27:54 +000072 auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>();
73 SymbolIndexMgr->addSymbolIndex(
Haojian Wu631e5f22016-05-13 15:17:17 +000074 llvm::make_unique<include_fixer::InMemorySymbolIndex>(Symbols));
Eric Liu692aca62016-05-04 08:22:35 +000075
Benjamin Kramer6b236262016-04-20 12:43:43 +000076 std::vector<clang::tooling::Replacement> Replacements;
Benjamin Kramera3d82332016-05-13 09:27:54 +000077 IncludeFixerActionFactory Factory(*SymbolIndexMgr, Replacements);
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000078 runOnCode(&Factory, Code, "input.cc", ExtraArgs);
Benjamin Kramer6b236262016-04-20 12:43:43 +000079 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
85TEST(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 Kramer3a45fab2016-04-28 11:21:29 +000091 "#include \"dir/bar.h\"\n",
Benjamin Kramer6b236262016-04-20 12:43:43 +000092 runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n"
Benjamin Kramer3a45fab2016-04-28 11:21:29 +000093 "#include \"dir/bar.h\"\n"));
Benjamin Kramer6b236262016-04-20 12:43:43 +000094
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 Liu692aca62016-05-04 08:22:35 +0000102 // string without "std::" can also be fixed since fixed db results go through
Benjamin Kramera3d82332016-05-13 09:27:54 +0000103 // SymbolIndexManager, and SymbolIndexManager matches unqualified identifiers
104 // too.
Eric Liu692aca62016-05-04 08:22:35 +0000105 EXPECT_EQ("#include <string>\nstring foo;\n",
106 runIncludeFixer("string foo;\n"));
Benjamin Kramer6b236262016-04-20 12:43:43 +0000107}
108
109TEST(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 Kramer3a45fab2016-04-28 11:21:29 +0000117TEST(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 Kramerad935002016-05-10 08:25:31 +0000135TEST(IncludeFixer, NestedName) {
NAKAMURA Takumi62e8ec52016-05-18 00:20:46 +0000136 // Some tests don't pass for target *-win32.
137 std::vector<std::string> args = {"-target", "x86_64-unknown-unknown"};
Benjamin Kramerad935002016-05-10 08:25:31 +0000138 EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n"
Benjamin Krameraf34e062016-05-17 12:35:18 +0000139 "int x = a::b::foo(0);\n",
NAKAMURA Takumi62e8ec52016-05-18 00:20:46 +0000140 runIncludeFixer("int x = a::b::foo(0);\n", args));
Benjamin Krameraf34e062016-05-17 12:35:18 +0000141
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 Kramerad935002016-05-10 08:25:31 +0000149 "namespace a {}\nint a = a::b::foo(0);\n",
NAKAMURA Takumi62e8ec52016-05-18 00:20:46 +0000150 runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n", args));
Benjamin Kramerad935002016-05-10 08:25:31 +0000151}
152
Benjamin Kramerc3459a52016-05-10 08:25:28 +0000153TEST(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 Wu57cdcb02016-05-13 15:44:16 +0000158TEST(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 Wuff6d1952016-05-18 09:04:43 +0000172
173TEST(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 Kramer6b236262016-04-20 12:43:43 +0000178} // namespace
179} // namespace include_fixer
180} // namespace clang