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