summaryrefslogtreecommitdiffstats
path: root/unittests/clang-include-fixer/IncludeFixerTest.cpp
blob: ab7ef7973a759d7300255ffe413e1b4fa39e6a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//===-- IncludeFixerTest.cpp - Include fixer unit tests -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "InMemorySymbolIndex.h"
#include "IncludeFixer.h"
#include "SymbolIndexManager.h"
#include "unittests/Tooling/RewriterTestContext.h"
#include "clang/Tooling/Tooling.h"
#include "gtest/gtest.h"

namespace clang {
namespace include_fixer {
namespace {

using find_all_symbols::SymbolInfo;
using find_all_symbols::SymbolAndSignals;

static bool runOnCode(tooling::ToolAction *ToolAction, StringRef Code,
                      StringRef FileName,
                      const std::vector<std::string> &ExtraArgs) {
  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
      new llvm::vfs::InMemoryFileSystem);
  llvm::IntrusiveRefCntPtr<FileManager> Files(
      new FileManager(FileSystemOptions(), InMemoryFileSystem));
  // FIXME: Investigate why -fms-compatibility breaks tests.
  std::vector<std::string> Args = {"include_fixer", "-fsyntax-only",
                                   "-fno-ms-compatibility", FileName};
  Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
  tooling::ToolInvocation Invocation(
      Args, ToolAction, Files.get(),
      std::make_shared<PCHContainerOperations>());

  InMemoryFileSystem->addFile(FileName, 0,
                              llvm::MemoryBuffer::getMemBuffer(Code));

  InMemoryFileSystem->addFile("foo.h", 0,
                              llvm::MemoryBuffer::getMemBuffer("\n"));
  InMemoryFileSystem->addFile("dir/bar.h", 0,
                              llvm::MemoryBuffer::getMemBuffer("\n"));
  InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
                              llvm::MemoryBuffer::getMemBuffer("\n"));
  InMemoryFileSystem->addFile("header.h", 0,
                              llvm::MemoryBuffer::getMemBuffer("bar b;"));
  return Invocation.run();
}

static std::string runIncludeFixer(
    StringRef Code,
    const std::vector<std::string> &ExtraArgs = std::vector<std::string>()) {
  std::vector<SymbolAndSignals> Symbols = {
      {SymbolInfo("string", SymbolInfo::SymbolKind::Class, "<string>",
                  {{SymbolInfo::ContextType::Namespace, "std"}}),
       SymbolInfo::Signals{}},
      {SymbolInfo("sting", SymbolInfo::SymbolKind::Class, "\"sting\"",
                  {{SymbolInfo::ContextType::Namespace, "std"}}),
       SymbolInfo::Signals{}},
      {SymbolInfo("foo", SymbolInfo::SymbolKind::Class,
                  "\"dir/otherdir/qux.h\"",
                  {{SymbolInfo::ContextType::Namespace, "b"},
                   {SymbolInfo::ContextType::Namespace, "a"}}),
       SymbolInfo::Signals{}},
      {SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar.h\"",
                  {{SymbolInfo::ContextType::Namespace, "b"},
                   {SymbolInfo::ContextType::Namespace, "a"}}),
       SymbolInfo::Signals{}},
      {SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar2.h\"",
                  {{SymbolInfo::ContextType::Namespace, "c"},
                   {SymbolInfo::ContextType::Namespace, "a"}}),
       SymbolInfo::Signals{}},
      {SymbolInfo("Green", SymbolInfo::SymbolKind::Class, "\"color.h\"",
                  {{SymbolInfo::ContextType::EnumDecl, "Color"},
                   {SymbolInfo::ContextType::Namespace, "b"},
                   {SymbolInfo::ContextType::Namespace, "a"}}),
       SymbolInfo::Signals{}},
      {SymbolInfo("Vector", SymbolInfo::SymbolKind::Class, "\"Vector.h\"",
                  {{SymbolInfo::ContextType::Namespace, "__a"},
                   {SymbolInfo::ContextType::Namespace, "a"}}),
       SymbolInfo::Signals{/*Seen=*/2, 0}},
      {SymbolInfo("Vector", SymbolInfo::SymbolKind::Class, "\"Vector.h\"",
                  {{SymbolInfo::ContextType::Namespace, "a"}}),
       SymbolInfo::Signals{/*Seen=*/2, 0}},
      {SymbolInfo("StrCat", SymbolInfo::SymbolKind::Class, "\"strcat.h\"",
                  {{SymbolInfo::ContextType::Namespace, "str"}}),
       SymbolInfo::Signals{}},
      {SymbolInfo("str", SymbolInfo::SymbolKind::Class, "\"str.h\"", {}),
       SymbolInfo::Signals{}},
      {SymbolInfo("foo2", SymbolInfo::SymbolKind::Class, "\"foo2.h\"", {}),
       SymbolInfo::Signals{}},
  };
  auto SymbolIndexMgr = llvm::make_unique<SymbolIndexManager>();
  SymbolIndexMgr->addSymbolIndex(
      [=]() { return llvm::make_unique<InMemorySymbolIndex>(Symbols); });

  std::vector<IncludeFixerContext> FixerContexts;
  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContexts, "llvm");
  std::string FakeFileName = "input.cc";
  runOnCode(&Factory, Code, FakeFileName, ExtraArgs);
  assert(FixerContexts.size() == 1);
  if (FixerContexts.front().getHeaderInfos().empty())
    return Code;
  auto Replaces = createIncludeFixerReplacements(Code, FixerContexts.front());
  EXPECT_TRUE(static_cast<bool>(Replaces))
      << llvm::toString(Replaces.takeError()) << "\n";
  if (!Replaces)
    return "";
  RewriterTestContext Context;
  FileID ID = Context.createInMemoryFile(FakeFileName, Code);
  tooling::applyAllReplacements(*Replaces, Context.Rewrite);
  return Context.getRewrittenText(ID);
}

TEST(IncludeFixer, Typo) {
  EXPECT_EQ("#include <string>\nstd::string foo;\n",
            runIncludeFixer("std::string foo;\n"));

  EXPECT_EQ("// comment\n#include \"foo.h\"\n#include <string>\n"
            "std::string foo;\n#include \"dir/bar.h\"\n",
            runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n"
                            "#include \"dir/bar.h\"\n"));

  EXPECT_EQ("#include \"foo.h\"\n#include <string>\nstd::string foo;\n",
            runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n"));

  EXPECT_EQ(
      "#include \"foo.h\"\n#include <string>\nstd::string::size_type foo;\n",
      runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n"));

  EXPECT_EQ("#include <string>\nstd::string foo;\n",
            runIncludeFixer("string foo;\n"));

  // Should not match std::string.
  EXPECT_EQ("::string foo;\n", runIncludeFixer("::string foo;\n"));
}

TEST(IncludeFixer, IncompleteType) {
  EXPECT_EQ(
      "#include \"foo.h\"\n#include <string>\n"
      "namespace std {\nclass string;\n}\nstd::string foo;\n",
      runIncludeFixer("#include \"foo.h\"\n"
                      "namespace std {\nclass string;\n}\nstring foo;\n"));

  EXPECT_EQ("#include <string>\n"
            "class string;\ntypedef string foo;\nfoo f;\n",
            runIncludeFixer("class string;\ntypedef string foo;\nfoo f;\n"));
}

TEST(IncludeFixer, MinimizeInclude) {
  std::vector<std::string> IncludePath = {"-Idir/"};
  EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n",
            runIncludeFixer("a::b::foo bar;\n", IncludePath));

  IncludePath = {"-isystemdir"};
  EXPECT_EQ("#include <otherdir/qux.h>\na::b::foo bar;\n",
            runIncludeFixer("a::b::foo bar;\n", IncludePath));

  IncludePath = {"-iquotedir"};
  EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n",
            runIncludeFixer("a::b::foo bar;\n", IncludePath));

  IncludePath = {"-Idir", "-Idir/otherdir"};
  EXPECT_EQ("#include \"qux.h\"\na::b::foo bar;\n",
            runIncludeFixer("a::b::foo bar;\n", IncludePath));
}

TEST(IncludeFixer, NestedName) {
  EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n"
            "int x = a::b::foo(0);\n",
            runIncludeFixer("int x = a::b::foo(0);\n"));

  // FIXME: Handle simple macros.
  EXPECT_EQ("#define FOO a::b::foo\nint x = FOO;\n",
            runIncludeFixer("#define FOO a::b::foo\nint x = FOO;\n"));
  EXPECT_EQ("#define FOO(x) a::##x\nint x = FOO(b::foo);\n",
            runIncludeFixer("#define FOO(x) a::##x\nint x = FOO(b::foo);\n"));

  // The empty namespace is cleaned up by clang-format after clang-include-fixer
  // finishes.
  EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n"
            "\nint a = a::b::foo(0);\n",
            runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n"));
}

TEST(IncludeFixer, MultipleMissingSymbols) {
  EXPECT_EQ("#include <string>\nstd::string bar;\nstd::sting foo;\n",
            runIncludeFixer("std::string bar;\nstd::sting foo;\n"));
}

TEST(IncludeFixer, ScopedNamespaceSymbols) {
  EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}",
            runIncludeFixer("namespace a {\nb::bar b;\n}"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n}",
            runIncludeFixer("namespace A {\na::b::bar b;\n}"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nvoid func() { b::bar b; }\n} "
            "// namespace a",
            runIncludeFixer("namespace a {\nvoid func() { b::bar b; }\n}"));
  EXPECT_EQ("namespace A { c::b::bar b; }\n",
            runIncludeFixer("namespace A { c::b::bar b; }\n"));
  // FIXME: The header should not be added here. Remove this after we support
  // full match.
  EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n}",
            runIncludeFixer("namespace A {\nb::bar b;\n}"));

  // Finds candidates for "str::StrCat".
  EXPECT_EQ("#include \"strcat.h\"\nnamespace foo2 {\nstr::StrCat b;\n}",
            runIncludeFixer("namespace foo2 {\nstr::StrCat b;\n}"));
  // str::StrCat2 doesn't exist.
  // In these two cases, StrCat2 is a nested class of class str.
  EXPECT_EQ("#include \"str.h\"\nnamespace foo2 {\nstr::StrCat2 b;\n}",
            runIncludeFixer("namespace foo2 {\nstr::StrCat2 b;\n}"));
  EXPECT_EQ("#include \"str.h\"\nnamespace ns {\nstr::StrCat2 b;\n}",
            runIncludeFixer("namespace ns {\nstr::StrCat2 b;\n}"));
}

TEST(IncludeFixer, EnumConstantSymbols) {
  EXPECT_EQ("#include \"color.h\"\nint test = a::b::Green;\n",
            runIncludeFixer("int test = a::b::Green;\n"));
}

TEST(IncludeFixer, IgnoreSymbolFromHeader) {
  std::string Code = "#include \"header.h\"";
  EXPECT_EQ(Code, runIncludeFixer(Code));
}

// FIXME: add test cases for inserting and sorting multiple headers when
// clang-include-fixer supports multiple headers insertion.
TEST(IncludeFixer, InsertAndSortSingleHeader) {
  // Insert one header.
  std::string Code = "#include \"a.h\"\n"
                     "#include \"foo.h\"\n"
                     "\n"
                     "namespace a {\nb::bar b;\n}\n";
  std::string Expected = "#include \"a.h\"\n"
                         "#include \"bar.h\"\n"
                         "#include \"foo.h\"\n"
                         "\n"
                         "namespace a {\nb::bar b;\n}\n";
  EXPECT_EQ(Expected, runIncludeFixer(Code));
}

TEST(IncludeFixer, DoNotDeleteMatchedSymbol) {
  EXPECT_EQ("#include \"Vector.h\"\na::Vector v;",
            runIncludeFixer("a::Vector v;"));
}

TEST(IncludeFixer, FixNamespaceQualifiers) {
  EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n",
            runIncludeFixer("b::bar b;\n"));
  EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n",
            runIncludeFixer("a::b::bar b;\n"));
  EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n",
            runIncludeFixer("bar b;\n"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n",
            runIncludeFixer("namespace a {\nb::bar b;\n}\n"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n",
            runIncludeFixer("namespace a {\nbar b;\n}\n"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nnamespace b{\nbar b;\n}\n} "
            "// namespace a\n",
            runIncludeFixer("namespace a {\nnamespace b{\nbar b;\n}\n}\n"));
  EXPECT_EQ("c::b::bar b;\n",
            runIncludeFixer("c::b::bar b;\n"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar b;\n}\n",
            runIncludeFixer("namespace d {\nbar b;\n}\n"));
  EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar b;\n}\n",
            runIncludeFixer("namespace c {\nbar b;\n}\n"));

  // Test common qualifers reduction.
  EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nnamespace d {\nb::bar b;\n}\n} "
            "// namespace a\n",
            runIncludeFixer("namespace a {\nnamespace d {\nbar b;\n}\n}\n"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace d {\nnamespace a {\na::b::bar "
            "b;\n}\n} // namespace d\n",
            runIncludeFixer("namespace d {\nnamespace a {\nbar b;\n}\n}\n"));

  // Test nested classes.
  EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar::t b;\n}\n",
            runIncludeFixer("namespace d {\nbar::t b;\n}\n"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar::t b;\n}\n",
            runIncludeFixer("namespace c {\nbar::t b;\n}\n"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar::t b;\n}\n",
            runIncludeFixer("namespace a {\nbar::t b;\n}\n"));

  EXPECT_EQ("#include \"color.h\"\nint test = a::b::Green;\n",
            runIncludeFixer("int test = Green;\n"));
  EXPECT_EQ("#include \"color.h\"\nnamespace d {\nint test = a::b::Green;\n}\n",
            runIncludeFixer("namespace d {\nint test = Green;\n}\n"));
  EXPECT_EQ("#include \"color.h\"\nnamespace a {\nint test = b::Green;\n}\n",
            runIncludeFixer("namespace a {\nint test = Green;\n}\n"));

  // Test global scope operator.
  EXPECT_EQ("#include \"bar.h\"\n::a::b::bar b;\n",
            runIncludeFixer("::a::b::bar b;\n"));
  EXPECT_EQ("#include \"bar.h\"\nnamespace a {\n::a::b::bar b;\n}\n",
            runIncludeFixer("namespace a {\n::a::b::bar b;\n}\n"));
}

TEST(IncludeFixer, FixNamespaceQualifiersForAllInstances) {
  const char TestCode[] = R"(
namespace a {
bar b;
int func1() {
  bar a;
                                                             bar *p = new bar();
  return 0;
}
} // namespace a

namespace a {
bar func2() {
  bar f;
  return f;
}
} // namespace a

// Non-fixed cases:
void f() {
  bar b;
}

namespace a {
namespace c {
  bar b;
} // namespace c
} // namespace a
)";

  const char ExpectedCode[] = R"(
#include "bar.h"
namespace a {
b::bar b;
int func1() {
  b::bar a;
  b::bar *p = new b::bar();
  return 0;
}
} // namespace a

namespace a {
b::bar func2() {
  b::bar f;
  return f;
}
} // namespace a

// Non-fixed cases:
void f() {
  bar b;
}

namespace a {
namespace c {
  bar b;
} // namespace c
} // namespace a
)";

  EXPECT_EQ(ExpectedCode, runIncludeFixer(TestCode));
}

TEST(IncludeFixer, DontAddQualifiersForMissingCompleteType) {
  EXPECT_EQ("#include \"bar.h\"\nclass bar;\nvoid f() {\nbar* b;\nb->f();\n}",
            runIncludeFixer("class bar;\nvoid f() {\nbar* b;\nb->f();\n}"));
}

} // namespace
} // namespace include_fixer
} // namespace clang