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 | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 10 | #include "InMemoryXrefsDB.h" |
| 11 | #include "IncludeFixer.h" |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 12 | #include "XrefsDBManager.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 | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 50 | std::map<std::string, std::vector<std::string>> XrefsMap = { |
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 | }; |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 56 | auto XrefsDBMgr = llvm::make_unique<include_fixer::XrefsDBManager>(); |
| 57 | XrefsDBMgr->addXrefsDB( |
| 58 | llvm::make_unique<include_fixer::InMemoryXrefsDB>(std::move(XrefsMap))); |
| 59 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 60 | std::vector<clang::tooling::Replacement> Replacements; |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 61 | IncludeFixerActionFactory Factory(*XrefsDBMgr, Replacements); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 62 | runOnCode(&Factory, Code, "input.cc", ExtraArgs); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 63 | clang::RewriterTestContext Context; |
| 64 | clang::FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 65 | clang::tooling::applyAllReplacements(Replacements, Context.Rewrite); |
| 66 | return Context.getRewrittenText(ID); |
| 67 | } |
| 68 | |
| 69 | TEST(IncludeFixer, Typo) { |
| 70 | EXPECT_EQ("#include <string>\nstd::string foo;\n", |
| 71 | runIncludeFixer("std::string foo;\n")); |
| 72 | |
| 73 | EXPECT_EQ( |
| 74 | "// comment\n#include <string>\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 75 | "#include \"dir/bar.h\"\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 76 | runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 77 | "#include \"dir/bar.h\"\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 78 | |
| 79 | EXPECT_EQ("#include <string>\n#include \"foo.h\"\nstd::string foo;\n", |
| 80 | runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n")); |
| 81 | |
| 82 | EXPECT_EQ( |
| 83 | "#include <string>\n#include \"foo.h\"\nstd::string::size_type foo;\n", |
| 84 | runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); |
| 85 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 86 | // string without "std::" can also be fixed since fixed db results go through |
| 87 | // XrefsDBManager, and XrefsDBManager matches unqualified identifiers too. |
| 88 | EXPECT_EQ("#include <string>\nstring foo;\n", |
| 89 | runIncludeFixer("string foo;\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST(IncludeFixer, IncompleteType) { |
| 93 | EXPECT_EQ( |
| 94 | "#include <string>\n#include \"foo.h\"\n" |
| 95 | "namespace std {\nclass string;\n}\nstring foo;\n", |
| 96 | runIncludeFixer("#include \"foo.h\"\n" |
| 97 | "namespace std {\nclass string;\n}\nstring foo;\n")); |
| 98 | } |
| 99 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 100 | TEST(IncludeFixer, MinimizeInclude) { |
| 101 | std::vector<std::string> IncludePath = {"-Idir/"}; |
| 102 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 103 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 104 | |
| 105 | IncludePath = {"-isystemdir"}; |
| 106 | EXPECT_EQ("#include <otherdir/qux.h>\na::b::foo bar;\n", |
| 107 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 108 | |
| 109 | IncludePath = {"-iquotedir"}; |
| 110 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 111 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 112 | |
| 113 | IncludePath = {"-Idir", "-Idir/otherdir"}; |
| 114 | EXPECT_EQ("#include \"qux.h\"\na::b::foo bar;\n", |
| 115 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 116 | } |
| 117 | |
NAKAMURA Takumi | c92b8e5 | 2016-05-10 22:46:32 +0000 | [diff] [blame] | 118 | #if 0 |
| 119 | // It doesn't pass for targeting win32. Investigating. |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 120 | TEST(IncludeFixer, NestedName) { |
| 121 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n" |
| 122 | "namespace a {}\nint a = a::b::foo(0);\n", |
| 123 | runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n")); |
| 124 | } |
NAKAMURA Takumi | c92b8e5 | 2016-05-10 22:46:32 +0000 | [diff] [blame] | 125 | #endif |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 126 | |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 127 | TEST(IncludeFixer, MultipleMissingSymbols) { |
| 128 | EXPECT_EQ("#include <string>\nstd::string bar;\nstd::sting foo;\n", |
| 129 | runIncludeFixer("std::string bar;\nstd::sting foo;\n")); |
| 130 | } |
| 131 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 132 | } // namespace |
| 133 | } // namespace include_fixer |
| 134 | } // namespace clang |