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" |
| 16 | using namespace clang; |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace include_fixer { |
| 20 | namespace { |
| 21 | |
| 22 | static bool runOnCode(tooling::ToolAction *ToolAction, StringRef Code, |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 23 | StringRef FileName, |
| 24 | const std::vector<std::string> &ExtraArgs) { |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 25 | llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 26 | new vfs::InMemoryFileSystem); |
| 27 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
| 28 | new FileManager(FileSystemOptions(), InMemoryFileSystem)); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 29 | std::vector<std::string> Args = {"include_fixer", "-fsyntax-only", FileName}; |
| 30 | Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 31 | tooling::ToolInvocation Invocation( |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 32 | Args, ToolAction, Files.get(), |
| 33 | std::make_shared<PCHContainerOperations>()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 34 | |
| 35 | InMemoryFileSystem->addFile(FileName, 0, |
| 36 | llvm::MemoryBuffer::getMemBuffer(Code)); |
| 37 | |
| 38 | InMemoryFileSystem->addFile("foo.h", 0, |
| 39 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 40 | InMemoryFileSystem->addFile("dir/bar.h", 0, |
| 41 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 42 | InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0, |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 43 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 44 | return Invocation.run(); |
| 45 | } |
| 46 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 47 | static std::string runIncludeFixer( |
| 48 | StringRef Code, |
| 49 | const std::vector<std::string> &ExtraArgs = std::vector<std::string>()) { |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 50 | std::map<std::string, std::vector<std::string>> SymbolsMap = { |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 51 | {"std::string", {"<string>"}}, |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 52 | {"std::sting", {"\"sting\""}}, |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 53 | {"std::string::size_type", {"<string>"}}, |
| 54 | {"a::b::foo", {"dir/otherdir/qux.h"}}, |
| 55 | }; |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 56 | auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>(); |
| 57 | SymbolIndexMgr->addSymbolIndex( |
| 58 | llvm::make_unique<include_fixer::InMemorySymbolIndex>( |
| 59 | std::move(SymbolsMap))); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 60 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 61 | std::vector<clang::tooling::Replacement> Replacements; |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 62 | IncludeFixerActionFactory Factory(*SymbolIndexMgr, Replacements); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 63 | runOnCode(&Factory, Code, "input.cc", ExtraArgs); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 64 | clang::RewriterTestContext Context; |
| 65 | clang::FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 66 | clang::tooling::applyAllReplacements(Replacements, Context.Rewrite); |
| 67 | return Context.getRewrittenText(ID); |
| 68 | } |
| 69 | |
| 70 | TEST(IncludeFixer, Typo) { |
| 71 | EXPECT_EQ("#include <string>\nstd::string foo;\n", |
| 72 | runIncludeFixer("std::string foo;\n")); |
| 73 | |
| 74 | EXPECT_EQ( |
| 75 | "// comment\n#include <string>\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 76 | "#include \"dir/bar.h\"\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 77 | runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 78 | "#include \"dir/bar.h\"\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 79 | |
| 80 | EXPECT_EQ("#include <string>\n#include \"foo.h\"\nstd::string foo;\n", |
| 81 | runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n")); |
| 82 | |
| 83 | EXPECT_EQ( |
| 84 | "#include <string>\n#include \"foo.h\"\nstd::string::size_type foo;\n", |
| 85 | runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); |
| 86 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 87 | // 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] | 88 | // SymbolIndexManager, and SymbolIndexManager matches unqualified identifiers |
| 89 | // too. |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 90 | EXPECT_EQ("#include <string>\nstring foo;\n", |
| 91 | runIncludeFixer("string foo;\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TEST(IncludeFixer, IncompleteType) { |
| 95 | EXPECT_EQ( |
| 96 | "#include <string>\n#include \"foo.h\"\n" |
| 97 | "namespace std {\nclass string;\n}\nstring foo;\n", |
| 98 | runIncludeFixer("#include \"foo.h\"\n" |
| 99 | "namespace std {\nclass string;\n}\nstring foo;\n")); |
| 100 | } |
| 101 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 102 | TEST(IncludeFixer, MinimizeInclude) { |
| 103 | std::vector<std::string> IncludePath = {"-Idir/"}; |
| 104 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 105 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 106 | |
| 107 | IncludePath = {"-isystemdir"}; |
| 108 | EXPECT_EQ("#include <otherdir/qux.h>\na::b::foo bar;\n", |
| 109 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 110 | |
| 111 | IncludePath = {"-iquotedir"}; |
| 112 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 113 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 114 | |
| 115 | IncludePath = {"-Idir", "-Idir/otherdir"}; |
| 116 | EXPECT_EQ("#include \"qux.h\"\na::b::foo bar;\n", |
| 117 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 118 | } |
| 119 | |
NAKAMURA Takumi | c92b8e5 | 2016-05-10 22:46:32 +0000 | [diff] [blame] | 120 | #if 0 |
| 121 | // It doesn't pass for targeting win32. Investigating. |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 122 | TEST(IncludeFixer, NestedName) { |
| 123 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n" |
| 124 | "namespace a {}\nint a = a::b::foo(0);\n", |
| 125 | runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n")); |
| 126 | } |
NAKAMURA Takumi | c92b8e5 | 2016-05-10 22:46:32 +0000 | [diff] [blame] | 127 | #endif |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 128 | |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 129 | TEST(IncludeFixer, MultipleMissingSymbols) { |
| 130 | EXPECT_EQ("#include <string>\nstd::string bar;\nstd::sting foo;\n", |
| 131 | runIncludeFixer("std::string bar;\nstd::sting foo;\n")); |
| 132 | } |
| 133 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 134 | } // namespace |
| 135 | } // namespace include_fixer |
| 136 | } // namespace clang |