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" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 12 | #include "unittests/Tooling/RewriterTestContext.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 13 | #include "clang/Tooling/Tooling.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | using namespace clang; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace include_fixer { |
| 19 | namespace { |
| 20 | |
| 21 | static bool runOnCode(tooling::ToolAction *ToolAction, StringRef Code, |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 22 | StringRef FileName, |
| 23 | const std::vector<std::string> &ExtraArgs) { |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 24 | llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 25 | new vfs::InMemoryFileSystem); |
| 26 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
| 27 | new FileManager(FileSystemOptions(), InMemoryFileSystem)); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 28 | std::vector<std::string> Args = {"include_fixer", "-fsyntax-only", FileName}; |
| 29 | Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 30 | tooling::ToolInvocation Invocation( |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 31 | Args, ToolAction, Files.get(), |
| 32 | std::make_shared<PCHContainerOperations>()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 33 | |
| 34 | InMemoryFileSystem->addFile(FileName, 0, |
| 35 | llvm::MemoryBuffer::getMemBuffer(Code)); |
| 36 | |
| 37 | InMemoryFileSystem->addFile("foo.h", 0, |
| 38 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 39 | InMemoryFileSystem->addFile("dir/bar.h", 0, |
| 40 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 41 | InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0, |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 42 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 43 | return Invocation.run(); |
| 44 | } |
| 45 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 46 | static std::string runIncludeFixer( |
| 47 | StringRef Code, |
| 48 | const std::vector<std::string> &ExtraArgs = std::vector<std::string>()) { |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 49 | std::map<std::string, std::vector<std::string>> XrefsMap = { |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 50 | {"std::string", {"<string>"}}, |
| 51 | {"std::string::size_type", {"<string>"}}, |
| 52 | {"a::b::foo", {"dir/otherdir/qux.h"}}, |
| 53 | }; |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 54 | auto XrefsDB = |
| 55 | llvm::make_unique<include_fixer::InMemoryXrefsDB>(std::move(XrefsMap)); |
| 56 | std::vector<clang::tooling::Replacement> Replacements; |
| 57 | IncludeFixerActionFactory Factory(*XrefsDB, Replacements); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 58 | runOnCode(&Factory, Code, "input.cc", ExtraArgs); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 59 | clang::RewriterTestContext Context; |
| 60 | clang::FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 61 | clang::tooling::applyAllReplacements(Replacements, Context.Rewrite); |
| 62 | return Context.getRewrittenText(ID); |
| 63 | } |
| 64 | |
| 65 | TEST(IncludeFixer, Typo) { |
| 66 | EXPECT_EQ("#include <string>\nstd::string foo;\n", |
| 67 | runIncludeFixer("std::string foo;\n")); |
| 68 | |
| 69 | EXPECT_EQ( |
| 70 | "// comment\n#include <string>\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 71 | "#include \"dir/bar.h\"\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 72 | runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 73 | "#include \"dir/bar.h\"\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 74 | |
| 75 | EXPECT_EQ("#include <string>\n#include \"foo.h\"\nstd::string foo;\n", |
| 76 | runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n")); |
| 77 | |
| 78 | EXPECT_EQ( |
| 79 | "#include <string>\n#include \"foo.h\"\nstd::string::size_type foo;\n", |
| 80 | runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); |
| 81 | |
| 82 | // The fixed xrefs db doesn't know how to handle string without std::. |
| 83 | EXPECT_EQ("string foo;\n", runIncludeFixer("string foo;\n")); |
| 84 | } |
| 85 | |
| 86 | TEST(IncludeFixer, IncompleteType) { |
| 87 | EXPECT_EQ( |
| 88 | "#include <string>\n#include \"foo.h\"\n" |
| 89 | "namespace std {\nclass string;\n}\nstring foo;\n", |
| 90 | runIncludeFixer("#include \"foo.h\"\n" |
| 91 | "namespace std {\nclass string;\n}\nstring foo;\n")); |
| 92 | } |
| 93 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 94 | TEST(IncludeFixer, MinimizeInclude) { |
| 95 | std::vector<std::string> IncludePath = {"-Idir/"}; |
| 96 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 97 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 98 | |
| 99 | IncludePath = {"-isystemdir"}; |
| 100 | EXPECT_EQ("#include <otherdir/qux.h>\na::b::foo bar;\n", |
| 101 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 102 | |
| 103 | IncludePath = {"-iquotedir"}; |
| 104 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
| 105 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 106 | |
| 107 | IncludePath = {"-Idir", "-Idir/otherdir"}; |
| 108 | EXPECT_EQ("#include \"qux.h\"\na::b::foo bar;\n", |
| 109 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 110 | } |
| 111 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 112 | } // namespace |
| 113 | } // namespace include_fixer |
| 114 | } // namespace clang |