Debian Codegraph @

llvm-project/clang/unittests/Lex/HeaderSearchTest.cpp

raw
1 //===- unittests/Lex/HeaderSearchTest.cpp ------ HeaderSearch tests -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Lex/HeaderSearch.h"
10 #include "HeaderMapTestUtils.h"
11 #include "clang/Basic/Diagnostic.h"
12 #include "clang/Basic/DiagnosticOptions.h"
13 #include "clang/Basic/FileManager.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Basic/TargetOptions.h"
18 #include "clang/Lex/HeaderSearchOptions.h"
19 #include "clang/Serialization/InMemoryModuleCache.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "gtest/gtest.h"
22
23 namespace clang {
24 namespace {
25
26 // The test fixture.
27 class HeaderSearchTest : public ::testing::Test {
28 protected:
29 HeaderSearchTest()
30 : VFS(new llvm::vfs::InMemoryFileSystem), FileMgr(FileMgrOpts, VFS),
31 DiagID(new DiagnosticIDs()),
32 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
33 SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions),
34 Search(std::make_shared<HeaderSearchOptions>(), SourceMgr, Diags,
35 LangOpts, Target.get()) {
36 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
37 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
38 }
39
40 void addSearchDir(llvm::StringRef Dir) {
41 VFS->addFile(
42 Dir, 0, llvm::MemoryBuffer::getMemBuffer(""), /*User=*/std::nullopt,
43 /*Group=*/std::nullopt, llvm::sys::fs::file_type::directory_file);
44 auto DE = FileMgr.getOptionalDirectoryRef(Dir);
45 assert(DE);
46 auto DL = DirectoryLookup(*DE, SrcMgr::C_User, /*isFramework=*/false);
47 Search.AddSearchPath(DL, /*isAngled=*/false);
48 }
49
50 void addSystemFrameworkSearchDir(llvm::StringRef Dir) {
51 VFS->addFile(
52 Dir, 0, llvm::MemoryBuffer::getMemBuffer(""), /*User=*/std::nullopt,
53 /*Group=*/std::nullopt, llvm::sys::fs::file_type::directory_file);
54 auto DE = FileMgr.getOptionalDirectoryRef(Dir);
55 assert(DE);
56 auto DL = DirectoryLookup(*DE, SrcMgr::C_System, /*isFramework=*/true);
57 Search.AddSystemSearchPath(DL);
58 }
59
60 void addHeaderMap(llvm::StringRef Filename,
61 std::unique_ptr<llvm::MemoryBuffer> Buf,
62 bool isAngled = false) {
63 VFS->addFile(Filename, 0, std::move(Buf), /*User=*/std::nullopt,
64 /*Group=*/std::nullopt,
65 llvm::sys::fs::file_type::regular_file);
66 auto FE = FileMgr.getFile(Filename, true);
67 assert(FE);
68
69 // Test class supports only one HMap at a time.
70 assert(!HMap);
71 HMap = HeaderMap::Create(*FE, FileMgr);
72 auto DL =
73 DirectoryLookup(HMap.get(), SrcMgr::C_User, /*isFramework=*/false);
74 Search.AddSearchPath(DL, isAngled);
75 }
76
77 IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS;
78 FileSystemOptions FileMgrOpts;
79 FileManager FileMgr;
80 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
81 DiagnosticsEngine Diags;
82 SourceManager SourceMgr;
83 LangOptions LangOpts;
84 std::shared_ptr<TargetOptions> TargetOpts;
85 IntrusiveRefCntPtr<TargetInfo> Target;
86 HeaderSearch Search;
87 std::unique_ptr<HeaderMap> HMap;
88 };
89
90 TEST_F(HeaderSearchTest, NoSearchDir) {
91 EXPECT_EQ(Search.search_dir_size(), 0u);
92 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/x/y/z", /*WorkingDir=*/"",
93 /*MainFile=*/""),
94 "/x/y/z");
95 }
96
97 TEST_F(HeaderSearchTest, SimpleShorten) {
98 addSearchDir("/x");
99 addSearchDir("/x/y");
100 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/x/y/z", /*WorkingDir=*/"",
101 /*MainFile=*/""),
102 "z");
103 addSearchDir("/a/b/");
104 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/a/b/c", /*WorkingDir=*/"",
105 /*MainFile=*/""),
106 "c");
107 }
108
109 TEST_F(HeaderSearchTest, ShortenWithWorkingDir) {
110 addSearchDir("x/y");
111 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/a/b/c/x/y/z",
112 /*WorkingDir=*/"/a/b/c",
113 /*MainFile=*/""),
114 "z");
115 }
116
117 TEST_F(HeaderSearchTest, Dots) {
118 addSearchDir("/x/./y/");
119 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/x/y/./z",
120 /*WorkingDir=*/"",
121 /*MainFile=*/""),
122 "z");
123 addSearchDir("a/.././c/");
124 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/m/n/./c/z",
125 /*WorkingDir=*/"/m/n/",
126 /*MainFile=*/""),
127 "z");
128 }
129
130 #ifdef _WIN32
131 TEST_F(HeaderSearchTest, BackSlash) {
132 addSearchDir("C:\\x\\y\\");
133 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("C:\\x\\y\\z\\t",
134 /*WorkingDir=*/"",
135 /*MainFile=*/""),
136 "z/t");
137 }
138
139 TEST_F(HeaderSearchTest, BackSlashWithDotDot) {
140 addSearchDir("..\\y");
141 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("C:\\x\\y\\z\\t",
142 /*WorkingDir=*/"C:/x/y/",
143 /*MainFile=*/""),
144 "z/t");
145 }
146 #endif
147
148 TEST_F(HeaderSearchTest, DotDotsWithAbsPath) {
149 addSearchDir("/x/../y/");
150 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/y/z",
151 /*WorkingDir=*/"",
152 /*MainFile=*/""),
153 "z");
154 }
155
156 TEST_F(HeaderSearchTest, BothDotDots) {
157 addSearchDir("/x/../y/");
158 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/x/../y/z",
159 /*WorkingDir=*/"",
160 /*MainFile=*/""),
161 "z");
162 }
163
164 TEST_F(HeaderSearchTest, IncludeFromSameDirectory) {
165 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/y/z/t.h",
166 /*WorkingDir=*/"",
167 /*MainFile=*/"/y/a.cc"),
168 "z/t.h");
169
170 addSearchDir("/");
171 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/y/z/t.h",
172 /*WorkingDir=*/"",
173 /*MainFile=*/"/y/a.cc"),
174 "y/z/t.h");
175 }
176
177 TEST_F(HeaderSearchTest, SdkFramework) {
178 addSystemFrameworkSearchDir(
179 "/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/Frameworks/");
180 bool IsSystem = false;
181 EXPECT_EQ(Search.suggestPathToFileForDiagnostics(
182 "/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/"
183 "Frameworks/AppKit.framework/Headers/NSView.h",
184 /*WorkingDir=*/"",
185 /*MainFile=*/"", &IsSystem),
186 "AppKit/NSView.h");
187 EXPECT_TRUE(IsSystem);
188 }
189
190 TEST_F(HeaderSearchTest, NestedFramework) {
191 addSystemFrameworkSearchDir("/Platforms/MacOSX/Frameworks");
192 EXPECT_EQ(Search.suggestPathToFileForDiagnostics(
193 "/Platforms/MacOSX/Frameworks/AppKit.framework/Frameworks/"
194 "Sub.framework/Headers/Sub.h",
195 /*WorkingDir=*/"",
196 /*MainFile=*/""),
197 "Sub/Sub.h");
198 }
199
200 TEST_F(HeaderSearchTest, HeaderFrameworkLookup) {
201 std::string HeaderPath = "/tmp/Frameworks/Foo.framework/Headers/Foo.h";
202 addSystemFrameworkSearchDir("/tmp/Frameworks");
203 VFS->addFile(HeaderPath, 0,
204 llvm::MemoryBuffer::getMemBufferCopy("", HeaderPath),
205 /*User=*/std::nullopt, /*Group=*/std::nullopt,
206 llvm::sys::fs::file_type::regular_file);
207
208 bool IsFrameworkFound = false;
209 auto FoundFile = Search.LookupFile(
210 "Foo/Foo.h", SourceLocation(), /*isAngled=*/true, /*FromDir=*/nullptr,
211 /*CurDir=*/nullptr, /*Includers=*/{}, /*SearchPath=*/nullptr,
212 /*RelativePath=*/nullptr, /*RequestingModule=*/nullptr,
213 /*SuggestedModule=*/nullptr, /*IsMapped=*/nullptr, &IsFrameworkFound);
214
215 EXPECT_TRUE(FoundFile.has_value());
216 EXPECT_TRUE(IsFrameworkFound);
217 auto &FE = *FoundFile;
218 auto FI = Search.getExistingFileInfo(FE);
219 EXPECT_TRUE(FI);
220 EXPECT_TRUE(FI->IsValid);
221 EXPECT_EQ(FI->Framework.str(), "Foo");
222 EXPECT_EQ(Search.getIncludeNameForHeader(FE), "Foo/Foo.h");
223 }
224
225 // Helper struct with null terminator character to make MemoryBuffer happy.
226 template <class FileTy, class PaddingTy>
227 struct NullTerminatedFile : public FileTy {
228 PaddingTy Padding = 0;
229 };
230
231 TEST_F(HeaderSearchTest, HeaderMapReverseLookup) {
232 typedef NullTerminatedFile<test::HMapFileMock<2, 32>, char> FileTy;
233 FileTy File;
234 File.init();
235
236 test::HMapFileMockMaker<FileTy> Maker(File);
237 auto a = Maker.addString("d.h");
238 auto b = Maker.addString("b/");
239 auto c = Maker.addString("c.h");
240 Maker.addBucket("d.h", a, b, c);
241
242 addHeaderMap("/x/y/z.hmap", File.getBuffer());
243 addSearchDir("/a");
244
245 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/a/b/c.h",
246 /*WorkingDir=*/"",
247 /*MainFile=*/""),
248 "d.h");
249 }
250
251 TEST_F(HeaderSearchTest, HeaderMapFrameworkLookup) {
252 typedef NullTerminatedFile<test::HMapFileMock<4, 128>, char> FileTy;
253 FileTy File;
254 File.init();
255
256 std::string HeaderDirName = "/tmp/Sources/Foo/Headers/";
257 std::string HeaderName = "Foo.h";
258 if (is_style_windows(llvm::sys::path::Style::native)) {
259 // Force header path to be absolute on windows.
260 // As headermap content should represent absolute locations.
261 HeaderDirName = "C:" + HeaderDirName;
262 }
263
264 test::HMapFileMockMaker<FileTy> Maker(File);
265 auto a = Maker.addString("Foo/Foo.h");
266 auto b = Maker.addString(HeaderDirName);
267 auto c = Maker.addString(HeaderName);
268 Maker.addBucket("Foo/Foo.h", a, b, c);
269 addHeaderMap("product-headers.hmap", File.getBuffer(), /*isAngled=*/true);
270
271 VFS->addFile(
272 HeaderDirName + HeaderName, 0,
273 llvm::MemoryBuffer::getMemBufferCopy("", HeaderDirName + HeaderName),
274 /*User=*/std::nullopt, /*Group=*/std::nullopt,
275 llvm::sys::fs::file_type::regular_file);
276
277 bool IsMapped = false;
278 auto FoundFile = Search.LookupFile(
279 "Foo/Foo.h", SourceLocation(), /*isAngled=*/true, /*FromDir=*/nullptr,
280 /*CurDir=*/nullptr, /*Includers=*/{}, /*SearchPath=*/nullptr,
281 /*RelativePath=*/nullptr, /*RequestingModule=*/nullptr,
282 /*SuggestedModule=*/nullptr, &IsMapped,
283 /*IsFrameworkFound=*/nullptr);
284
285 EXPECT_TRUE(FoundFile.has_value());
286 EXPECT_TRUE(IsMapped);
287 auto &FE = *FoundFile;
288 auto FI = Search.getExistingFileInfo(FE);
289 EXPECT_TRUE(FI);
290 EXPECT_TRUE(FI->IsValid);
291 EXPECT_EQ(FI->Framework.str(), "Foo");
292 EXPECT_EQ(Search.getIncludeNameForHeader(FE), "Foo/Foo.h");
293 }
294
295 } // namespace
296 } // namespace clang