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>"}}, |
| 52 | {"std::string::size_type", {"<string>"}}, |
| 53 | {"a::b::foo", {"dir/otherdir/qux.h"}}, |
| 54 | }; |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 55 | auto XrefsDBMgr = llvm::make_unique<include_fixer::XrefsDBManager>(); |
| 56 | XrefsDBMgr->addXrefsDB( |
| 57 | llvm::make_unique<include_fixer::InMemoryXrefsDB>(std::move(XrefsMap))); |
| 58 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 59 | std::vector<clang::tooling::Replacement> Replacements; |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 60 | IncludeFixerActionFactory Factory(*XrefsDBMgr, Replacements); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 61 | runOnCode(&Factory, Code, "input.cc", ExtraArgs); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 62 | clang::RewriterTestContext Context; |
| 63 | clang::FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 64 | clang::tooling::applyAllReplacements(Replacements, Context.Rewrite); |
| 65 | return Context.getRewrittenText(ID); |
| 66 | } |
| 67 | |
| 68 | TEST(IncludeFixer, Typo) { |
| 69 | EXPECT_EQ("#include <string>\nstd::string foo;\n", |
| 70 | runIncludeFixer("std::string foo;\n")); |
| 71 | |
| 72 | EXPECT_EQ( |
| 73 | "// comment\n#include <string>\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 74 | "#include \"dir/bar.h\"\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 75 | runIncludeFixer("// comment\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 | |
| 78 | EXPECT_EQ("#include <string>\n#include \"foo.h\"\nstd::string foo;\n", |
| 79 | runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n")); |
| 80 | |
| 81 | EXPECT_EQ( |
| 82 | "#include <string>\n#include \"foo.h\"\nstd::string::size_type foo;\n", |
| 83 | runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); |
| 84 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 85 | // string without "std::" can also be fixed since fixed db results go through |
| 86 | // XrefsDBManager, and XrefsDBManager matches unqualified identifiers too. |
| 87 | EXPECT_EQ("#include <string>\nstring foo;\n", |
| 88 | runIncludeFixer("string foo;\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | TEST(IncludeFixer, IncompleteType) { |
| 92 | EXPECT_EQ( |
| 93 | "#include <string>\n#include \"foo.h\"\n" |
| 94 | "namespace std {\nclass string;\n}\nstring foo;\n", |
| 95 | runIncludeFixer("#include \"foo.h\"\n" |
| 96 | "namespace std {\nclass string;\n}\nstring foo;\n")); |
| 97 | } |
| 98 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 99 | TEST(IncludeFixer, MinimizeInclude) { |
| 100 | std::vector<std::string> IncludePath = {"-Idir/"}; |
| 101 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 102 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 103 | |
| 104 | IncludePath = {"-isystemdir"}; |
| 105 | EXPECT_EQ("#include <otherdir/qux.h>\na::b::foo bar;\n", |
| 106 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 107 | |
| 108 | IncludePath = {"-iquotedir"}; |
| 109 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 110 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 111 | |
| 112 | IncludePath = {"-Idir", "-Idir/otherdir"}; |
| 113 | EXPECT_EQ("#include \"qux.h\"\na::b::foo bar;\n", |
| 114 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 115 | } |
| 116 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 117 | } // namespace |
| 118 | } // namespace include_fixer |
| 119 | } // namespace clang |