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" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 16 | |
| 17 | namespace clang { |
| 18 | namespace include_fixer { |
| 19 | namespace { |
| 20 | |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 21 | using find_all_symbols::SymbolInfo; |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 22 | using find_all_symbols::SymbolAndSignals; |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 23 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 24 | static bool runOnCode(tooling::ToolAction *ToolAction, StringRef Code, |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 25 | StringRef FileName, |
| 26 | const std::vector<std::string> &ExtraArgs) { |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 27 | llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 28 | new vfs::InMemoryFileSystem); |
| 29 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
| 30 | new FileManager(FileSystemOptions(), InMemoryFileSystem)); |
Benjamin Kramer | 2ecd090 | 2016-05-18 09:28:45 +0000 | [diff] [blame] | 31 | // FIXME: Investigate why -fms-compatibility breaks tests. |
| 32 | std::vector<std::string> Args = {"include_fixer", "-fsyntax-only", |
| 33 | "-fno-ms-compatibility", FileName}; |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 34 | Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 35 | tooling::ToolInvocation Invocation( |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 36 | Args, ToolAction, Files.get(), |
| 37 | std::make_shared<PCHContainerOperations>()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 38 | |
| 39 | InMemoryFileSystem->addFile(FileName, 0, |
| 40 | llvm::MemoryBuffer::getMemBuffer(Code)); |
| 41 | |
| 42 | InMemoryFileSystem->addFile("foo.h", 0, |
| 43 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 44 | InMemoryFileSystem->addFile("dir/bar.h", 0, |
| 45 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 46 | InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0, |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 47 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Haojian Wu | 9c6cb03 | 2016-06-03 11:26:02 +0000 | [diff] [blame] | 48 | InMemoryFileSystem->addFile("header.h", 0, |
| 49 | llvm::MemoryBuffer::getMemBuffer("bar b;")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 50 | return Invocation.run(); |
| 51 | } |
| 52 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 53 | static std::string runIncludeFixer( |
| 54 | StringRef Code, |
| 55 | const std::vector<std::string> &ExtraArgs = std::vector<std::string>()) { |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 56 | std::vector<SymbolAndSignals> Symbols = { |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 57 | {SymbolInfo("string", SymbolInfo::SymbolKind::Class, "<string>", |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 58 | {{SymbolInfo::ContextType::Namespace, "std"}}), |
| 59 | SymbolInfo::Signals{}}, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 60 | {SymbolInfo("sting", SymbolInfo::SymbolKind::Class, "\"sting\"", |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 61 | {{SymbolInfo::ContextType::Namespace, "std"}}), |
| 62 | SymbolInfo::Signals{}}, |
| 63 | {SymbolInfo("foo", SymbolInfo::SymbolKind::Class, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 64 | "\"dir/otherdir/qux.h\"", |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 65 | {{SymbolInfo::ContextType::Namespace, "b"}, |
| 66 | {SymbolInfo::ContextType::Namespace, "a"}}), |
| 67 | SymbolInfo::Signals{}}, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 68 | {SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar.h\"", |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 69 | {{SymbolInfo::ContextType::Namespace, "b"}, |
| 70 | {SymbolInfo::ContextType::Namespace, "a"}}), |
| 71 | SymbolInfo::Signals{}}, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 72 | {SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar2.h\"", |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 73 | {{SymbolInfo::ContextType::Namespace, "c"}, |
| 74 | {SymbolInfo::ContextType::Namespace, "a"}}), |
| 75 | SymbolInfo::Signals{}}, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 76 | {SymbolInfo("Green", SymbolInfo::SymbolKind::Class, "\"color.h\"", |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 77 | {{SymbolInfo::ContextType::EnumDecl, "Color"}, |
| 78 | {SymbolInfo::ContextType::Namespace, "b"}, |
| 79 | {SymbolInfo::ContextType::Namespace, "a"}}), |
| 80 | SymbolInfo::Signals{}}, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 81 | {SymbolInfo("Vector", SymbolInfo::SymbolKind::Class, "\"Vector.h\"", |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 82 | {{SymbolInfo::ContextType::Namespace, "__a"}, |
| 83 | {SymbolInfo::ContextType::Namespace, "a"}}), |
| 84 | SymbolInfo::Signals{/*Seen=*/2, 0}}, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 85 | {SymbolInfo("Vector", SymbolInfo::SymbolKind::Class, "\"Vector.h\"", |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 86 | {{SymbolInfo::ContextType::Namespace, "a"}}), |
| 87 | SymbolInfo::Signals{/*Seen=*/2, 0}}, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 88 | {SymbolInfo("StrCat", SymbolInfo::SymbolKind::Class, "\"strcat.h\"", |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 89 | {{SymbolInfo::ContextType::Namespace, "str"}}), |
| 90 | SymbolInfo::Signals{}}, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 91 | {SymbolInfo("str", SymbolInfo::SymbolKind::Class, "\"str.h\"", {}), |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 92 | SymbolInfo::Signals{}}, |
Sam McCall | 573050e | 2017-03-09 10:47:44 +0000 | [diff] [blame] | 93 | {SymbolInfo("foo2", SymbolInfo::SymbolKind::Class, "\"foo2.h\"", {}), |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 94 | SymbolInfo::Signals{}}, |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 95 | }; |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 96 | auto SymbolIndexMgr = llvm::make_unique<SymbolIndexManager>(); |
| 97 | SymbolIndexMgr->addSymbolIndex( |
| 98 | [=]() { return llvm::make_unique<InMemorySymbolIndex>(Symbols); }); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 99 | |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 100 | std::vector<IncludeFixerContext> FixerContexts; |
| 101 | IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContexts, "llvm"); |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 102 | std::string FakeFileName = "input.cc"; |
| 103 | runOnCode(&Factory, Code, FakeFileName, ExtraArgs); |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame] | 104 | assert(FixerContexts.size() == 1); |
| 105 | if (FixerContexts.front().getHeaderInfos().empty()) |
Eric Liu | f83187d | 2016-05-31 13:52:59 +0000 | [diff] [blame] | 106 | return Code; |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 107 | auto Replaces = createIncludeFixerReplacements(Code, FixerContexts.front()); |
Eric Liu | a452db4 | 2016-07-11 13:53:21 +0000 | [diff] [blame] | 108 | EXPECT_TRUE(static_cast<bool>(Replaces)) |
| 109 | << llvm::toString(Replaces.takeError()) << "\n"; |
| 110 | if (!Replaces) |
| 111 | return ""; |
Sam McCall | b27dc22 | 2017-02-28 08:13:15 +0000 | [diff] [blame] | 112 | RewriterTestContext Context; |
| 113 | FileID ID = Context.createInMemoryFile(FakeFileName, Code); |
| 114 | tooling::applyAllReplacements(*Replaces, Context.Rewrite); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 115 | return Context.getRewrittenText(ID); |
| 116 | } |
| 117 | |
| 118 | TEST(IncludeFixer, Typo) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 119 | EXPECT_EQ("#include <string>\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 120 | runIncludeFixer("std::string foo;\n")); |
| 121 | |
Eric Liu | 4bf67af | 2016-12-02 11:23:07 +0000 | [diff] [blame] | 122 | EXPECT_EQ("// comment\n#include \"foo.h\"\n#include <string>\n" |
| 123 | "std::string foo;\n#include \"dir/bar.h\"\n", |
| 124 | runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n" |
| 125 | "#include \"dir/bar.h\"\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 126 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 127 | EXPECT_EQ("#include \"foo.h\"\n#include <string>\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 128 | runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n")); |
| 129 | |
| 130 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 131 | "#include \"foo.h\"\n#include <string>\nstd::string::size_type foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 132 | runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); |
| 133 | |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 134 | EXPECT_EQ("#include <string>\nstd::string foo;\n", |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 135 | runIncludeFixer("string foo;\n")); |
Benjamin Kramer | 9b15b6f | 2016-05-19 12:41:56 +0000 | [diff] [blame] | 136 | |
Benjamin Kramer | 9b15b6f | 2016-05-19 12:41:56 +0000 | [diff] [blame] | 137 | // Should not match std::string. |
| 138 | EXPECT_EQ("::string foo;\n", runIncludeFixer("::string foo;\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | TEST(IncludeFixer, IncompleteType) { |
| 142 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 143 | "#include \"foo.h\"\n#include <string>\n" |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 144 | "namespace std {\nclass string;\n}\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 145 | runIncludeFixer("#include \"foo.h\"\n" |
| 146 | "namespace std {\nclass string;\n}\nstring foo;\n")); |
Benjamin Kramer | bc805ae | 2016-12-16 16:09:06 +0000 | [diff] [blame] | 147 | |
| 148 | EXPECT_EQ("#include <string>\n" |
| 149 | "class string;\ntypedef string foo;\nfoo f;\n", |
| 150 | runIncludeFixer("class string;\ntypedef string foo;\nfoo f;\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 153 | TEST(IncludeFixer, MinimizeInclude) { |
| 154 | std::vector<std::string> IncludePath = {"-Idir/"}; |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 155 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 156 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 157 | |
| 158 | IncludePath = {"-isystemdir"}; |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 159 | EXPECT_EQ("#include <otherdir/qux.h>\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 160 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 161 | |
| 162 | IncludePath = {"-iquotedir"}; |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 163 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 164 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 165 | |
| 166 | IncludePath = {"-Idir", "-Idir/otherdir"}; |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 167 | EXPECT_EQ("#include \"qux.h\"\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 168 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 169 | } |
| 170 | |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 171 | TEST(IncludeFixer, NestedName) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 172 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n" |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 173 | "int x = a::b::foo(0);\n", |
Benjamin Kramer | 04666a2 | 2016-05-19 11:39:56 +0000 | [diff] [blame] | 174 | runIncludeFixer("int x = a::b::foo(0);\n")); |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 175 | |
| 176 | // FIXME: Handle simple macros. |
| 177 | EXPECT_EQ("#define FOO a::b::foo\nint x = FOO;\n", |
| 178 | runIncludeFixer("#define FOO a::b::foo\nint x = FOO;\n")); |
| 179 | EXPECT_EQ("#define FOO(x) a::##x\nint x = FOO(b::foo);\n", |
| 180 | runIncludeFixer("#define FOO(x) a::##x\nint x = FOO(b::foo);\n")); |
| 181 | |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 182 | // The empty namespace is cleaned up by clang-format after include-fixer |
| 183 | // finishes. |
| 184 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n" |
| 185 | "\nint a = a::b::foo(0);\n", |
Benjamin Kramer | 04666a2 | 2016-05-19 11:39:56 +0000 | [diff] [blame] | 186 | runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n")); |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 189 | TEST(IncludeFixer, MultipleMissingSymbols) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 190 | EXPECT_EQ("#include <string>\nstd::string bar;\nstd::sting foo;\n", |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 191 | runIncludeFixer("std::string bar;\nstd::sting foo;\n")); |
| 192 | } |
| 193 | |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 194 | TEST(IncludeFixer, ScopedNamespaceSymbols) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 195 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}", |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 196 | runIncludeFixer("namespace a {\nb::bar b;\n}")); |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 197 | EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n}", |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 198 | runIncludeFixer("namespace A {\na::b::bar b;\n}")); |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 199 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nvoid func() { b::bar b; }\n}", |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 200 | runIncludeFixer("namespace a {\nvoid func() { b::bar b; }\n}")); |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 201 | EXPECT_EQ("namespace A { c::b::bar b; }\n", |
| 202 | runIncludeFixer("namespace A { c::b::bar b; }\n")); |
| 203 | // FIXME: The header should not be added here. Remove this after we support |
| 204 | // full match. |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 205 | EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n}", |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 206 | runIncludeFixer("namespace A {\nb::bar b;\n}")); |
Haojian Wu | adedac6 | 2016-08-02 10:43:10 +0000 | [diff] [blame] | 207 | |
| 208 | // Finds candidates for "str::StrCat". |
| 209 | EXPECT_EQ("#include \"strcat.h\"\nnamespace foo2 {\nstr::StrCat b;\n}", |
| 210 | runIncludeFixer("namespace foo2 {\nstr::StrCat b;\n}")); |
| 211 | // str::StrCat2 doesn't exist. |
| 212 | // In these two cases, StrCat2 is a nested class of class str. |
| 213 | EXPECT_EQ("#include \"str.h\"\nnamespace foo2 {\nstr::StrCat2 b;\n}", |
| 214 | runIncludeFixer("namespace foo2 {\nstr::StrCat2 b;\n}")); |
| 215 | EXPECT_EQ("#include \"str.h\"\nnamespace ns {\nstr::StrCat2 b;\n}", |
| 216 | runIncludeFixer("namespace ns {\nstr::StrCat2 b;\n}")); |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 217 | } |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 218 | |
| 219 | TEST(IncludeFixer, EnumConstantSymbols) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 220 | EXPECT_EQ("#include \"color.h\"\nint test = a::b::Green;\n", |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 221 | runIncludeFixer("int test = a::b::Green;\n")); |
| 222 | } |
| 223 | |
Haojian Wu | 9c6cb03 | 2016-06-03 11:26:02 +0000 | [diff] [blame] | 224 | TEST(IncludeFixer, IgnoreSymbolFromHeader) { |
| 225 | std::string Code = "#include \"header.h\""; |
| 226 | EXPECT_EQ(Code, runIncludeFixer(Code)); |
| 227 | } |
| 228 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 229 | // FIXME: add test cases for inserting and sorting multiple headers when |
| 230 | // include-fixer supports multiple headers insertion. |
| 231 | TEST(IncludeFixer, InsertAndSortSingleHeader) { |
| 232 | // Insert one header. |
| 233 | std::string Code = "#include \"a.h\"\n" |
| 234 | "#include \"foo.h\"\n" |
| 235 | "\n" |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 236 | "namespace a {\nb::bar b;\n}\n"; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 237 | std::string Expected = "#include \"a.h\"\n" |
| 238 | "#include \"bar.h\"\n" |
| 239 | "#include \"foo.h\"\n" |
| 240 | "\n" |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 241 | "namespace a {\nb::bar b;\n}\n"; |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 242 | EXPECT_EQ(Expected, runIncludeFixer(Code)); |
| 243 | } |
| 244 | |
Eric Liu | a4be83a | 2016-06-13 19:05:07 +0000 | [diff] [blame] | 245 | TEST(IncludeFixer, DoNotDeleteMatchedSymbol) { |
| 246 | EXPECT_EQ("#include \"Vector.h\"\na::Vector v;", |
| 247 | runIncludeFixer("a::Vector v;")); |
| 248 | } |
| 249 | |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 250 | TEST(IncludeFixer, FixNamespaceQualifiers) { |
| 251 | EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", |
| 252 | runIncludeFixer("b::bar b;\n")); |
| 253 | EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", |
| 254 | runIncludeFixer("a::b::bar b;\n")); |
| 255 | EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", |
| 256 | runIncludeFixer("bar b;\n")); |
| 257 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n", |
| 258 | runIncludeFixer("namespace a {\nb::bar b;\n}\n")); |
| 259 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n", |
| 260 | runIncludeFixer("namespace a {\nbar b;\n}\n")); |
| 261 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nnamespace b{\nbar b;\n}\n}\n", |
| 262 | runIncludeFixer("namespace a {\nnamespace b{\nbar b;\n}\n}\n")); |
| 263 | EXPECT_EQ("c::b::bar b;\n", |
| 264 | runIncludeFixer("c::b::bar b;\n")); |
Haojian Wu | 0c05e2e | 2016-07-14 09:39:12 +0000 | [diff] [blame] | 265 | EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar b;\n}\n", |
| 266 | runIncludeFixer("namespace d {\nbar b;\n}\n")); |
| 267 | EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar b;\n}\n", |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 268 | runIncludeFixer("namespace c {\nbar b;\n}\n")); |
| 269 | |
Haojian Wu | 39a718c | 2016-07-15 08:12:48 +0000 | [diff] [blame] | 270 | // Test common qualifers reduction. |
| 271 | EXPECT_EQ( |
| 272 | "#include \"bar.h\"\nnamespace a {\nnamespace d {\nb::bar b;\n}\n}\n", |
| 273 | runIncludeFixer("namespace a {\nnamespace d {\nbar b;\n}\n}\n")); |
| 274 | EXPECT_EQ( |
| 275 | "#include \"bar.h\"\nnamespace d {\nnamespace a {\na::b::bar b;\n}\n}\n", |
| 276 | runIncludeFixer("namespace d {\nnamespace a {\nbar b;\n}\n}\n")); |
| 277 | |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 278 | // Test nested classes. |
Haojian Wu | 0c05e2e | 2016-07-14 09:39:12 +0000 | [diff] [blame] | 279 | EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar::t b;\n}\n", |
| 280 | runIncludeFixer("namespace d {\nbar::t b;\n}\n")); |
Haojian Wu | adedac6 | 2016-08-02 10:43:10 +0000 | [diff] [blame] | 281 | EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar::t b;\n}\n", |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 282 | runIncludeFixer("namespace c {\nbar::t b;\n}\n")); |
| 283 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar::t b;\n}\n", |
| 284 | runIncludeFixer("namespace a {\nbar::t b;\n}\n")); |
| 285 | |
Haojian Wu | 5d9482d | 2016-07-08 14:28:43 +0000 | [diff] [blame] | 286 | EXPECT_EQ("#include \"color.h\"\nint test = a::b::Green;\n", |
| 287 | runIncludeFixer("int test = Green;\n")); |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 288 | EXPECT_EQ("#include \"color.h\"\nnamespace d {\nint test = a::b::Green;\n}\n", |
| 289 | runIncludeFixer("namespace d {\nint test = Green;\n}\n")); |
| 290 | EXPECT_EQ("#include \"color.h\"\nnamespace a {\nint test = b::Green;\n}\n", |
| 291 | runIncludeFixer("namespace a {\nint test = Green;\n}\n")); |
| 292 | |
Haojian Wu | 5d9482d | 2016-07-08 14:28:43 +0000 | [diff] [blame] | 293 | // Test global scope operator. |
| 294 | EXPECT_EQ("#include \"bar.h\"\n::a::b::bar b;\n", |
| 295 | runIncludeFixer("::a::b::bar b;\n")); |
| 296 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\n::a::b::bar b;\n}\n", |
| 297 | runIncludeFixer("namespace a {\n::a::b::bar b;\n}\n")); |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 300 | TEST(IncludeFixer, FixNamespaceQualifiersForAllInstances) { |
| 301 | const char TestCode[] = R"( |
| 302 | namespace a { |
| 303 | bar b; |
| 304 | int func1() { |
| 305 | bar a; |
| 306 | bar *p = new bar(); |
| 307 | return 0; |
| 308 | } |
| 309 | } // namespace a |
| 310 | |
| 311 | namespace a { |
| 312 | bar func2() { |
| 313 | bar f; |
| 314 | return f; |
| 315 | } |
| 316 | } // namespace a |
| 317 | |
| 318 | // Non-fixed cases: |
| 319 | void f() { |
| 320 | bar b; |
| 321 | } |
| 322 | |
| 323 | namespace a { |
| 324 | namespace c { |
| 325 | bar b; |
| 326 | } // namespace c |
| 327 | } // namespace a |
| 328 | )"; |
| 329 | |
| 330 | const char ExpectedCode[] = R"( |
| 331 | #include "bar.h" |
| 332 | namespace a { |
| 333 | b::bar b; |
| 334 | int func1() { |
| 335 | b::bar a; |
| 336 | b::bar *p = new b::bar(); |
| 337 | return 0; |
| 338 | } |
| 339 | } // namespace a |
| 340 | |
| 341 | namespace a { |
| 342 | b::bar func2() { |
| 343 | b::bar f; |
| 344 | return f; |
| 345 | } |
| 346 | } // namespace a |
| 347 | |
| 348 | // Non-fixed cases: |
| 349 | void f() { |
| 350 | bar b; |
| 351 | } |
| 352 | |
| 353 | namespace a { |
| 354 | namespace c { |
| 355 | bar b; |
| 356 | } // namespace c |
| 357 | } // namespace a |
| 358 | )"; |
| 359 | |
| 360 | EXPECT_EQ(ExpectedCode, runIncludeFixer(TestCode)); |
| 361 | } |
| 362 | |
Haojian Wu | 5644618 | 2016-07-26 16:32:42 +0000 | [diff] [blame] | 363 | TEST(IncludeFixer, DontAddQualifiersForMissingCompleteType) { |
| 364 | EXPECT_EQ("#include \"bar.h\"\nclass bar;\nvoid f() {\nbar* b;\nb->f();\n}", |
| 365 | runIncludeFixer("class bar;\nvoid f() {\nbar* b;\nb->f();\n}")); |
| 366 | } |
| 367 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 368 | } // namespace |
| 369 | } // namespace include_fixer |
| 370 | } // namespace clang |