Debian Codegraph @

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 #include <memory>
23 #include <string>
24
25 namespace clang {
26 namespace {
27
28 // The test fixture.
29 class HeaderSearchTest : public ::testing::Test {
30 protected:
31 HeaderSearchTest()
32 : VFS(new llvm::vfs::InMemoryFileSystem), FileMgr(FileMgrOpts, VFS),
33 DiagID(new DiagnosticIDs()),
34 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
35 SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions),
36 Search(std::make_shared<HeaderSearchOptions>(), SourceMgr, Diags,
37 LangOpts, Target.get()) {
38 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
39 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
40 }
41
42 void addSearchDir(llvm::StringRef Dir) {
43 VFS->addFile(
44 Dir, 0, llvm::MemoryBuffer::getMemBuffer(""), /*User=*/std::nullopt,
45 /*Group=*/std::nullopt, llvm::sys::fs::file_type::directory_file);
46 auto DE = FileMgr.getOptionalDirectoryRef(Dir);
47 assert(DE);
48 auto DL = DirectoryLookup(*DE, SrcMgr::C_User, /*isFramework=*/false);
49 Search.AddSearchPath(DL, /*isAngled=*/false);
50 }
51
52 void addFrameworkSearchDir(llvm::StringRef Dir, bool IsSystem = true) {
53 VFS->addFile(
54 Dir, 0, llvm::MemoryBuffer::getMemBuffer(""), /*User=*/std::nullopt,
55 /*Group=*/std::nullopt, llvm::sys::fs::file_type::directory_file);
56 auto DE = FileMgr.getOptionalDirectoryRef(Dir);
57 assert(DE);
58 auto DL = DirectoryLookup(*DE, IsSystem ? SrcMgr::C_System : SrcMgr::C_User,
59 /*isFramework=*/true);
60 if (IsSystem)
61 Search.AddSystemSearchPath(DL);
62 else
63 Search.AddSearchPath(DL, /*isAngled=*/true);
64 }
65
66 void addHeaderMap(llvm::StringRef Filename,
67 std::unique_ptr<llvm::MemoryBuffer> Buf,
68 bool isAngled = false) {
69 VFS->addFile(Filename, 0, std::move(Buf), /*User=*/std::nullopt,
70 /*Group=*/std::nullopt,
71 llvm::sys::fs::file_type::regular_file);
72 auto FE = FileMgr.getOptionalFileRef(Filename, true);
73 assert(FE);
74
75 // Test class supports only one HMap at a time.
76 assert(!HMap);
77 HMap = HeaderMap::Create(*FE, FileMgr);
78 auto DL =
79 DirectoryLookup(HMap.get(), SrcMgr::C_User, /*isFramework=*/false);
80 Search.AddSearchPath(DL, isAngled);
81 }
82
83 IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS;
84 FileSystemOptions FileMgrOpts;
85 FileManager FileMgr;
86 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
87 DiagnosticsEngine Diags;
88 SourceManager SourceMgr;
89 LangOptions LangOpts;
90 std::shared_ptr<TargetOptions> TargetOpts;
91 IntrusiveRefCntPtr<TargetInfo> Target;
92 HeaderSearch Search;
93 std::unique_ptr<HeaderMap> HMap;
94 };
95
96 TEST_F(HeaderSearchTest, NoSearchDir) {
97 EXPECT_EQ(Search.search_dir_size(), 0u);
98 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/x/y/z", /*WorkingDir=*/"",
99 /*MainFile=*/""),
100 "/x/y/z");
101 }
102
103 TEST_F(HeaderSearchTest, SimpleShorten) {
104 addSearchDir("/x");
105 addSearchDir("/x/y");
106 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/x/y/z", /*WorkingDir=*/"",
107 /*MainFile=*/""),
108 "z");
109 addSearchDir("/a/b/");
110 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/a/b/c", /*WorkingDir=*/"",
111 /*MainFile=*/""),
112 "c");
113 }
114
115 TEST_F(HeaderSearchTest, ShortenWithWorkingDir) {
116 addSearchDir("x/y");
117 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/a/b/c/x/y/z",
118 /*WorkingDir=*/"/a/b/c",
119 /*MainFile=*/""),
120 "z");
121 }
122
123 TEST_F(HeaderSearchTest, Dots) {
124 addSearchDir("/x/./y/");
125 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/x/y/./z",
126 /*WorkingDir=*/"",
127 /*MainFile=*/""),
128 "z");
129 addSearchDir("a/.././c/");
130 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/m/n/./c/z",
131 /*WorkingDir=*/"/m/n/",
132 /*MainFile=*/""),
133 "z");
134 }
135
136 TEST_F(HeaderSearchTest, RelativeDirs) {
137 ASSERT_FALSE(VFS->setCurrentWorkingDirectory("/root/some/dir"));
138 addSearchDir("..");
139 EXPECT_EQ(
140 Search.suggestPathToFileForDiagnostics("/root/some/foo.h",
141 /*WorkingDir=*/"/root/some/dir",
142 /*MainFile=*/""),
143 "foo.h");
144 EXPECT_EQ(
145 Search.suggestPathToFileForDiagnostics("../foo.h",
146 /*WorkingDir=*/"/root/some/dir",
147 /*MainFile=*/""),
148 "foo.h");
149 }
150
151 #ifdef _WIN32
152 TEST_F(HeaderSearchTest, BackSlash) {
153 addSearchDir("C:\\x\\y\\");
154 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("C:\\x\\y\\z\\t",
155 /*WorkingDir=*/"",
156 /*MainFile=*/""),
157 "z/t");
158 }
159
160 TEST_F(HeaderSearchTest, BackSlashWithDotDot) {
161 addSearchDir("..\\y");
162 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("C:\\x\\y\\z\\t",
163 /*WorkingDir=*/"C:/x/y/",
164 /*MainFile=*/""),
165 "z/t");
166 }
167 #endif
168
169 TEST_F(HeaderSearchTest, DotDotsWithAbsPath) {
170 addSearchDir("/x/../y/");
171 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/y/z",
172 /*WorkingDir=*/"",
173 /*MainFile=*/""),
174 "z");
175 }
176
177 TEST_F(HeaderSearchTest, BothDotDots) {
178 addSearchDir("/x/../y/");
179 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/x/../y/z",
180 /*WorkingDir=*/"",
181 /*MainFile=*/""),
182 "z");
183 }
184
185 TEST_F(HeaderSearchTest, IncludeFromSameDirectory) {
186 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/y/z/t.h",
187 /*WorkingDir=*/"",
188 /*MainFile=*/"/y/a.cc"),
189 "z/t.h");
190
191 addSearchDir("/");
192 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/y/z/t.h",
193 /*WorkingDir=*/"",
194 /*MainFile=*/"/y/a.cc"),
195 "y/z/t.h");
196 }
197
198 TEST_F(HeaderSearchTest, SdkFramework) {
199 addFrameworkSearchDir(
200 "/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/Frameworks/");
201 bool IsAngled = false;
202 EXPECT_EQ(Search.suggestPathToFileForDiagnostics(
203 "/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/"
204 "Frameworks/AppKit.framework/Headers/NSView.h",
205 /*WorkingDir=*/"",
206 /*MainFile=*/"", &IsAngled),
207 "AppKit/NSView.h");
208 EXPECT_TRUE(IsAngled);
209
210 addFrameworkSearchDir("/System/Developer/Library/Framworks/",
211 /*IsSystem*/ false);
212 EXPECT_EQ(Search.suggestPathToFileForDiagnostics(
213 "/System/Developer/Library/Framworks/"
214 "Foo.framework/Headers/Foo.h",
215 /*WorkingDir=*/"",
216 /*MainFile=*/"", &IsAngled),
217 "Foo/Foo.h");
218 // Expect to be true even though we passed false to IsSystem earlier since
219 // all frameworks should be treated as <>.
220 EXPECT_TRUE(IsAngled);
221 }
222
223 TEST_F(HeaderSearchTest, NestedFramework) {
224 addFrameworkSearchDir("/Platforms/MacOSX/Frameworks");
225 EXPECT_EQ(Search.suggestPathToFileForDiagnostics(
226 "/Platforms/MacOSX/Frameworks/AppKit.framework/Frameworks/"
227 "Sub.framework/Headers/Sub.h",
228 /*WorkingDir=*/"",
229 /*MainFile=*/""),
230 "Sub/Sub.h");
231 }
232
233 TEST_F(HeaderSearchTest, HeaderFrameworkLookup) {
234 std::string HeaderPath = "/tmp/Frameworks/Foo.framework/Headers/Foo.h";
235 addFrameworkSearchDir("/tmp/Frameworks");
236 VFS->addFile(HeaderPath, 0,
237 llvm::MemoryBuffer::getMemBufferCopy("", HeaderPath),
238 /*User=*/std::nullopt, /*Group=*/std::nullopt,
239 llvm::sys::fs::file_type::regular_file);
240
241 bool IsFrameworkFound = false;
242 auto FoundFile = Search.LookupFile(
243 "Foo/Foo.h", SourceLocation(), /*isAngled=*/true, /*FromDir=*/nullptr,
244 /*CurDir=*/nullptr, /*Includers=*/{}, /*SearchPath=*/nullptr,
245 /*RelativePath=*/nullptr, /*RequestingModule=*/nullptr,
246 /*SuggestedModule=*/nullptr, /*IsMapped=*/nullptr, &IsFrameworkFound);
247
248 EXPECT_TRUE(FoundFile.has_value());
249 EXPECT_TRUE(IsFrameworkFound);
250 auto &FE = *FoundFile;
251 auto FI = Search.getExistingFileInfo(FE);
252 EXPECT_TRUE(FI);
253 EXPECT_TRUE(FI->IsValid);
254 EXPECT_EQ(FI->Framework.str(), "Foo");
255 EXPECT_EQ(Search.getIncludeNameForHeader(FE), "Foo/Foo.h");
256 }
257
258 // Helper struct with null terminator character to make MemoryBuffer happy.
259 template <class FileTy, class PaddingTy>
260 struct NullTerminatedFile : public FileTy {
261 PaddingTy Padding = 0;
262 };
263
264 TEST_F(HeaderSearchTest, HeaderMapReverseLookup) {
265 typedef NullTerminatedFile<test::HMapFileMock<2, 32>, char> FileTy;
266 FileTy File;
267 File.init();
268
269 test::HMapFileMockMaker<FileTy> Maker(File);
270 auto a = Maker.addString("d.h");
271 auto b = Maker.addString("b/");
272 auto c = Maker.addString("c.h");
273 Maker.addBucket("d.h", a, b, c);
274
275 addHeaderMap("/x/y/z.hmap", File.getBuffer());
276 addSearchDir("/a");
277
278 EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/a/b/c.h",
279 /*WorkingDir=*/"",
280 /*MainFile=*/""),
281 "d.h");
282 }
283
284 TEST_F(HeaderSearchTest, HeaderMapFrameworkLookup) {
285 typedef NullTerminatedFile<test::HMapFileMock<4, 128>, char> FileTy;
286 FileTy File;
287 File.init();
288
289 std::string HeaderDirName = "/tmp/Sources/Foo/Headers/";
290 std::string HeaderName = "Foo.h";
291 if (is_style_windows(llvm::sys::path::Style::native)) {
292 // Force header path to be absolute on windows.
293 // As headermap content should represent absolute locations.
294 HeaderDirName = "C:" + HeaderDirName;
295 }
296
297 test::HMapFileMockMaker<FileTy> Maker(File);
298 auto a = Maker.addString("Foo/Foo.h");
299 auto b = Maker.addString(HeaderDirName);
300 auto c = Maker.addString(HeaderName);
301 Maker.addBucket("Foo/Foo.h", a, b, c);
302 addHeaderMap("product-headers.hmap", File.getBuffer(), /*isAngled=*/true);
303
304 VFS->addFile(
305 HeaderDirName + HeaderName, 0,
306 llvm::MemoryBuffer::getMemBufferCopy("", HeaderDirName + HeaderName),
307 /*User=*/std::nullopt, /*Group=*/std::nullopt,
308 llvm::sys::fs::file_type::regular_file);
309
310 bool IsMapped = false;
311 auto FoundFile = Search.LookupFile(
312 "Foo/Foo.h", SourceLocation(), /*isAngled=*/true, /*FromDir=*/nullptr,
313 /*CurDir=*/nullptr, /*Includers=*/{}, /*SearchPath=*/nullptr,
314 /*RelativePath=*/nullptr, /*RequestingModule=*/nullptr,
315 /*SuggestedModule=*/nullptr, &IsMapped,
316 /*IsFrameworkFound=*/nullptr);
317
318 EXPECT_TRUE(FoundFile.has_value());
319 EXPECT_TRUE(IsMapped);
320 auto &FE = *FoundFile;
321 auto FI = Search.getExistingFileInfo(FE);
322 EXPECT_TRUE(FI);
323 EXPECT_TRUE(FI->IsValid);
324 EXPECT_EQ(FI->Framework.str(), "Foo");
325 EXPECT_EQ(Search.getIncludeNameForHeader(FE), "Foo/Foo.h");
326 }
327
328 TEST_F(HeaderSearchTest, HeaderFileInfoMerge) {
329 auto AddHeader = [&](std::string HeaderPath) -> FileEntryRef {
330 VFS->addFile(HeaderPath, 0,
331 llvm::MemoryBuffer::getMemBufferCopy("", HeaderPath),
332 /*User=*/std::nullopt, /*Group=*/std::nullopt,
333 llvm::sys::fs::file_type::regular_file);
334 return *FileMgr.getOptionalFileRef(HeaderPath);
335 };
336
337 class MockExternalHeaderFileInfoSource : public ExternalHeaderFileInfoSource {
338 HeaderFileInfo GetHeaderFileInfo(FileEntryRef FE) {
339 HeaderFileInfo HFI;
340 auto FileName = FE.getName();
341 if (FileName == ModularPath)
342 HFI.mergeModuleMembership(ModuleMap::NormalHeader);
343 else if (FileName == TextualPath)
344 HFI.mergeModuleMembership(ModuleMap::TextualHeader);
345 HFI.External = true;
346 HFI.IsValid = true;
347 return HFI;
348 }
349
350 public:
351 std::string ModularPath = "/modular.h";
352 std::string TextualPath = "/textual.h";
353 };
354
355 auto ExternalSource = std::make_unique<MockExternalHeaderFileInfoSource>();
356 Search.SetExternalSource(ExternalSource.get());
357
358 // Everything should start out external.
359 auto ModularFE = AddHeader(ExternalSource->ModularPath);
360 auto TextualFE = AddHeader(ExternalSource->TextualPath);
361 EXPECT_TRUE(Search.getExistingFileInfo(ModularFE)->External);
362 EXPECT_TRUE(Search.getExistingFileInfo(TextualFE)->External);
363
364 // Marking the same role should keep it external
365 Search.MarkFileModuleHeader(ModularFE, ModuleMap::NormalHeader,
366 /*isCompilingModuleHeader=*/false);
367 Search.MarkFileModuleHeader(TextualFE, ModuleMap::TextualHeader,
368 /*isCompilingModuleHeader=*/false);
369 EXPECT_TRUE(Search.getExistingFileInfo(ModularFE)->External);
370 EXPECT_TRUE(Search.getExistingFileInfo(TextualFE)->External);
371
372 // textual -> modular should update the HFI, but modular -> textual should be
373 // a no-op.
374 Search.MarkFileModuleHeader(ModularFE, ModuleMap::TextualHeader,
375 /*isCompilingModuleHeader=*/false);
376 Search.MarkFileModuleHeader(TextualFE, ModuleMap::NormalHeader,
377 /*isCompilingModuleHeader=*/false);
378 auto ModularFI = Search.getExistingFileInfo(ModularFE);
379 auto TextualFI = Search.getExistingFileInfo(TextualFE);
380 EXPECT_TRUE(ModularFI->External);
381 EXPECT_TRUE(ModularFI->isModuleHeader);
382 EXPECT_FALSE(ModularFI->isTextualModuleHeader);
383 EXPECT_FALSE(TextualFI->External);
384 EXPECT_TRUE(TextualFI->isModuleHeader);
385 EXPECT_FALSE(TextualFI->isTextualModuleHeader);
386
387 // Compiling the module should make the HFI local.
388 Search.MarkFileModuleHeader(ModularFE, ModuleMap::NormalHeader,
389 /*isCompilingModuleHeader=*/true);
390 Search.MarkFileModuleHeader(TextualFE, ModuleMap::NormalHeader,
391 /*isCompilingModuleHeader=*/true);
392 EXPECT_FALSE(Search.getExistingFileInfo(ModularFE)->External);
393 EXPECT_FALSE(Search.getExistingFileInfo(TextualFE)->External);
394 }
395
396 } // namespace
397 } // namespace clang