Mercurial > hg > CbC > CbC_llvm
comparison clang/lib/Frontend/InitHeaderSearch.cpp @ 150:1d019706d866
LLVM10
author | anatofuz |
---|---|
date | Thu, 13 Feb 2020 15:10:13 +0900 |
parents | |
children | 0572611fdcc8 |
comparison
equal
deleted
inserted
replaced
147:c2174574ed3a | 150:1d019706d866 |
---|---|
1 //===--- InitHeaderSearch.cpp - Initialize header search paths ------------===// | |
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 // This file implements the InitHeaderSearch class. | |
10 // | |
11 //===----------------------------------------------------------------------===// | |
12 | |
13 #include "clang/Basic/FileManager.h" | |
14 #include "clang/Basic/LangOptions.h" | |
15 #include "clang/Config/config.h" // C_INCLUDE_DIRS | |
16 #include "clang/Frontend/FrontendDiagnostic.h" | |
17 #include "clang/Frontend/Utils.h" | |
18 #include "clang/Lex/HeaderMap.h" | |
19 #include "clang/Lex/HeaderSearch.h" | |
20 #include "clang/Lex/HeaderSearchOptions.h" | |
21 #include "llvm/ADT/SmallPtrSet.h" | |
22 #include "llvm/ADT/SmallString.h" | |
23 #include "llvm/ADT/SmallVector.h" | |
24 #include "llvm/ADT/StringExtras.h" | |
25 #include "llvm/ADT/Triple.h" | |
26 #include "llvm/ADT/Twine.h" | |
27 #include "llvm/Support/ErrorHandling.h" | |
28 #include "llvm/Support/Path.h" | |
29 #include "llvm/Support/raw_ostream.h" | |
30 | |
31 using namespace clang; | |
32 using namespace clang::frontend; | |
33 | |
34 namespace { | |
35 | |
36 /// InitHeaderSearch - This class makes it easier to set the search paths of | |
37 /// a HeaderSearch object. InitHeaderSearch stores several search path lists | |
38 /// internally, which can be sent to a HeaderSearch object in one swoop. | |
39 class InitHeaderSearch { | |
40 std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath; | |
41 typedef std::vector<std::pair<IncludeDirGroup, | |
42 DirectoryLookup> >::const_iterator path_iterator; | |
43 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes; | |
44 HeaderSearch &Headers; | |
45 bool Verbose; | |
46 std::string IncludeSysroot; | |
47 bool HasSysroot; | |
48 | |
49 public: | |
50 InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot) | |
51 : Headers(HS), Verbose(verbose), IncludeSysroot(std::string(sysroot)), | |
52 HasSysroot(!(sysroot.empty() || sysroot == "/")) {} | |
53 | |
54 /// AddPath - Add the specified path to the specified group list, prefixing | |
55 /// the sysroot if used. | |
56 /// Returns true if the path exists, false if it was ignored. | |
57 bool AddPath(const Twine &Path, IncludeDirGroup Group, bool isFramework); | |
58 | |
59 /// AddUnmappedPath - Add the specified path to the specified group list, | |
60 /// without performing any sysroot remapping. | |
61 /// Returns true if the path exists, false if it was ignored. | |
62 bool AddUnmappedPath(const Twine &Path, IncludeDirGroup Group, | |
63 bool isFramework); | |
64 | |
65 /// AddSystemHeaderPrefix - Add the specified prefix to the system header | |
66 /// prefix list. | |
67 void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) { | |
68 SystemHeaderPrefixes.emplace_back(std::string(Prefix), IsSystemHeader); | |
69 } | |
70 | |
71 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu | |
72 /// libstdc++. | |
73 /// Returns true if the \p Base path was found, false if it does not exist. | |
74 bool AddGnuCPlusPlusIncludePaths(StringRef Base, StringRef ArchDir, | |
75 StringRef Dir32, StringRef Dir64, | |
76 const llvm::Triple &triple); | |
77 | |
78 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW | |
79 /// libstdc++. | |
80 void AddMinGWCPlusPlusIncludePaths(StringRef Base, | |
81 StringRef Arch, | |
82 StringRef Version); | |
83 | |
84 // AddDefaultCIncludePaths - Add paths that should always be searched. | |
85 void AddDefaultCIncludePaths(const llvm::Triple &triple, | |
86 const HeaderSearchOptions &HSOpts); | |
87 | |
88 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when | |
89 // compiling c++. | |
90 void AddDefaultCPlusPlusIncludePaths(const LangOptions &LangOpts, | |
91 const llvm::Triple &triple, | |
92 const HeaderSearchOptions &HSOpts); | |
93 | |
94 /// AddDefaultSystemIncludePaths - Adds the default system include paths so | |
95 /// that e.g. stdio.h is found. | |
96 void AddDefaultIncludePaths(const LangOptions &Lang, | |
97 const llvm::Triple &triple, | |
98 const HeaderSearchOptions &HSOpts); | |
99 | |
100 /// Realize - Merges all search path lists into one list and send it to | |
101 /// HeaderSearch. | |
102 void Realize(const LangOptions &Lang); | |
103 }; | |
104 | |
105 } // end anonymous namespace. | |
106 | |
107 static bool CanPrefixSysroot(StringRef Path) { | |
108 #if defined(_WIN32) | |
109 return !Path.empty() && llvm::sys::path::is_separator(Path[0]); | |
110 #else | |
111 return llvm::sys::path::is_absolute(Path); | |
112 #endif | |
113 } | |
114 | |
115 bool InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group, | |
116 bool isFramework) { | |
117 // Add the path with sysroot prepended, if desired and this is a system header | |
118 // group. | |
119 if (HasSysroot) { | |
120 SmallString<256> MappedPathStorage; | |
121 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage); | |
122 if (CanPrefixSysroot(MappedPathStr)) { | |
123 return AddUnmappedPath(IncludeSysroot + Path, Group, isFramework); | |
124 } | |
125 } | |
126 | |
127 return AddUnmappedPath(Path, Group, isFramework); | |
128 } | |
129 | |
130 bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group, | |
131 bool isFramework) { | |
132 assert(!Path.isTriviallyEmpty() && "can't handle empty path here"); | |
133 | |
134 FileManager &FM = Headers.getFileMgr(); | |
135 SmallString<256> MappedPathStorage; | |
136 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage); | |
137 | |
138 // If use system headers while cross-compiling, emit the warning. | |
139 if (HasSysroot && (MappedPathStr.startswith("/usr/include") || | |
140 MappedPathStr.startswith("/usr/local/include"))) { | |
141 Headers.getDiags().Report(diag::warn_poison_system_directories) | |
142 << MappedPathStr; | |
143 } | |
144 | |
145 // Compute the DirectoryLookup type. | |
146 SrcMgr::CharacteristicKind Type; | |
147 if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) { | |
148 Type = SrcMgr::C_User; | |
149 } else if (Group == ExternCSystem) { | |
150 Type = SrcMgr::C_ExternCSystem; | |
151 } else { | |
152 Type = SrcMgr::C_System; | |
153 } | |
154 | |
155 // If the directory exists, add it. | |
156 if (auto DE = FM.getOptionalDirectoryRef(MappedPathStr)) { | |
157 IncludePath.push_back( | |
158 std::make_pair(Group, DirectoryLookup(*DE, Type, isFramework))); | |
159 return true; | |
160 } | |
161 | |
162 // Check to see if this is an apple-style headermap (which are not allowed to | |
163 // be frameworks). | |
164 if (!isFramework) { | |
165 if (auto FE = FM.getFile(MappedPathStr)) { | |
166 if (const HeaderMap *HM = Headers.CreateHeaderMap(*FE)) { | |
167 // It is a headermap, add it to the search path. | |
168 IncludePath.push_back( | |
169 std::make_pair(Group, | |
170 DirectoryLookup(HM, Type, Group == IndexHeaderMap))); | |
171 return true; | |
172 } | |
173 } | |
174 } | |
175 | |
176 if (Verbose) | |
177 llvm::errs() << "ignoring nonexistent directory \"" | |
178 << MappedPathStr << "\"\n"; | |
179 return false; | |
180 } | |
181 | |
182 bool InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base, | |
183 StringRef ArchDir, | |
184 StringRef Dir32, | |
185 StringRef Dir64, | |
186 const llvm::Triple &triple) { | |
187 // Add the base dir | |
188 bool IsBaseFound = AddPath(Base, CXXSystem, false); | |
189 | |
190 // Add the multilib dirs | |
191 llvm::Triple::ArchType arch = triple.getArch(); | |
192 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64; | |
193 if (is64bit) | |
194 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false); | |
195 else | |
196 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false); | |
197 | |
198 // Add the backward dir | |
199 AddPath(Base + "/backward", CXXSystem, false); | |
200 return IsBaseFound; | |
201 } | |
202 | |
203 void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base, | |
204 StringRef Arch, | |
205 StringRef Version) { | |
206 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++", | |
207 CXXSystem, false); | |
208 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch, | |
209 CXXSystem, false); | |
210 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward", | |
211 CXXSystem, false); | |
212 } | |
213 | |
214 void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple, | |
215 const HeaderSearchOptions &HSOpts) { | |
216 llvm::Triple::OSType os = triple.getOS(); | |
217 | |
218 if (triple.isOSDarwin()) { | |
219 llvm_unreachable("Include management is handled in the driver."); | |
220 } | |
221 | |
222 if (HSOpts.UseStandardSystemIncludes) { | |
223 switch (os) { | |
224 case llvm::Triple::CloudABI: | |
225 case llvm::Triple::FreeBSD: | |
226 case llvm::Triple::NetBSD: | |
227 case llvm::Triple::OpenBSD: | |
228 case llvm::Triple::NaCl: | |
229 case llvm::Triple::PS4: | |
230 case llvm::Triple::ELFIAMCU: | |
231 case llvm::Triple::Fuchsia: | |
232 break; | |
233 case llvm::Triple::Win32: | |
234 if (triple.getEnvironment() != llvm::Triple::Cygnus) | |
235 break; | |
236 LLVM_FALLTHROUGH; | |
237 default: | |
238 // FIXME: temporary hack: hard-coded paths. | |
239 AddPath("/usr/local/include", System, false); | |
240 break; | |
241 } | |
242 } | |
243 | |
244 // Builtin includes use #include_next directives and should be positioned | |
245 // just prior C include dirs. | |
246 if (HSOpts.UseBuiltinIncludes) { | |
247 // Ignore the sys root, we *always* look for clang headers relative to | |
248 // supplied path. | |
249 SmallString<128> P = StringRef(HSOpts.ResourceDir); | |
250 llvm::sys::path::append(P, "include"); | |
251 AddUnmappedPath(P, ExternCSystem, false); | |
252 } | |
253 | |
254 // All remaining additions are for system include directories, early exit if | |
255 // we aren't using them. | |
256 if (!HSOpts.UseStandardSystemIncludes) | |
257 return; | |
258 | |
259 // Add dirs specified via 'configure --with-c-include-dirs'. | |
260 StringRef CIncludeDirs(C_INCLUDE_DIRS); | |
261 if (CIncludeDirs != "") { | |
262 SmallVector<StringRef, 5> dirs; | |
263 CIncludeDirs.split(dirs, ":"); | |
264 for (StringRef dir : dirs) | |
265 AddPath(dir, ExternCSystem, false); | |
266 return; | |
267 } | |
268 | |
269 switch (os) { | |
270 case llvm::Triple::Linux: | |
271 case llvm::Triple::Hurd: | |
272 case llvm::Triple::Solaris: | |
273 llvm_unreachable("Include management is handled in the driver."); | |
274 | |
275 case llvm::Triple::CloudABI: { | |
276 // <sysroot>/<triple>/include | |
277 SmallString<128> P = StringRef(HSOpts.ResourceDir); | |
278 llvm::sys::path::append(P, "../../..", triple.str(), "include"); | |
279 AddPath(P, System, false); | |
280 break; | |
281 } | |
282 | |
283 case llvm::Triple::Haiku: | |
284 AddPath("/boot/system/non-packaged/develop/headers", System, false); | |
285 AddPath("/boot/system/develop/headers/os", System, false); | |
286 AddPath("/boot/system/develop/headers/os/app", System, false); | |
287 AddPath("/boot/system/develop/headers/os/arch", System, false); | |
288 AddPath("/boot/system/develop/headers/os/device", System, false); | |
289 AddPath("/boot/system/develop/headers/os/drivers", System, false); | |
290 AddPath("/boot/system/develop/headers/os/game", System, false); | |
291 AddPath("/boot/system/develop/headers/os/interface", System, false); | |
292 AddPath("/boot/system/develop/headers/os/kernel", System, false); | |
293 AddPath("/boot/system/develop/headers/os/locale", System, false); | |
294 AddPath("/boot/system/develop/headers/os/mail", System, false); | |
295 AddPath("/boot/system/develop/headers/os/media", System, false); | |
296 AddPath("/boot/system/develop/headers/os/midi", System, false); | |
297 AddPath("/boot/system/develop/headers/os/midi2", System, false); | |
298 AddPath("/boot/system/develop/headers/os/net", System, false); | |
299 AddPath("/boot/system/develop/headers/os/opengl", System, false); | |
300 AddPath("/boot/system/develop/headers/os/storage", System, false); | |
301 AddPath("/boot/system/develop/headers/os/support", System, false); | |
302 AddPath("/boot/system/develop/headers/os/translation", System, false); | |
303 AddPath("/boot/system/develop/headers/os/add-ons/graphics", System, false); | |
304 AddPath("/boot/system/develop/headers/os/add-ons/input_server", System, false); | |
305 AddPath("/boot/system/develop/headers/os/add-ons/mail_daemon", System, false); | |
306 AddPath("/boot/system/develop/headers/os/add-ons/registrar", System, false); | |
307 AddPath("/boot/system/develop/headers/os/add-ons/screen_saver", System, false); | |
308 AddPath("/boot/system/develop/headers/os/add-ons/tracker", System, false); | |
309 AddPath("/boot/system/develop/headers/os/be_apps/Deskbar", System, false); | |
310 AddPath("/boot/system/develop/headers/os/be_apps/NetPositive", System, false); | |
311 AddPath("/boot/system/develop/headers/os/be_apps/Tracker", System, false); | |
312 AddPath("/boot/system/develop/headers/3rdparty", System, false); | |
313 AddPath("/boot/system/develop/headers/bsd", System, false); | |
314 AddPath("/boot/system/develop/headers/glibc", System, false); | |
315 AddPath("/boot/system/develop/headers/posix", System, false); | |
316 AddPath("/boot/system/develop/headers", System, false); | |
317 break; | |
318 case llvm::Triple::RTEMS: | |
319 break; | |
320 case llvm::Triple::Win32: | |
321 switch (triple.getEnvironment()) { | |
322 default: llvm_unreachable("Include management is handled in the driver."); | |
323 case llvm::Triple::Cygnus: | |
324 AddPath("/usr/include/w32api", System, false); | |
325 break; | |
326 case llvm::Triple::GNU: | |
327 break; | |
328 } | |
329 break; | |
330 default: | |
331 break; | |
332 } | |
333 | |
334 switch (os) { | |
335 case llvm::Triple::CloudABI: | |
336 case llvm::Triple::RTEMS: | |
337 case llvm::Triple::NaCl: | |
338 case llvm::Triple::ELFIAMCU: | |
339 case llvm::Triple::Fuchsia: | |
340 break; | |
341 case llvm::Triple::PS4: { | |
342 // <isysroot> gets prepended later in AddPath(). | |
343 std::string BaseSDKPath = ""; | |
344 if (!HasSysroot) { | |
345 const char *envValue = getenv("SCE_ORBIS_SDK_DIR"); | |
346 if (envValue) | |
347 BaseSDKPath = envValue; | |
348 else { | |
349 // HSOpts.ResourceDir variable contains the location of Clang's | |
350 // resource files. | |
351 // Assuming that Clang is configured for PS4 without | |
352 // --with-clang-resource-dir option, the location of Clang's resource | |
353 // files is <SDK_DIR>/host_tools/lib/clang | |
354 SmallString<128> P = StringRef(HSOpts.ResourceDir); | |
355 llvm::sys::path::append(P, "../../.."); | |
356 BaseSDKPath = std::string(P.str()); | |
357 } | |
358 } | |
359 AddPath(BaseSDKPath + "/target/include", System, false); | |
360 if (triple.isPS4CPU()) | |
361 AddPath(BaseSDKPath + "/target/include_common", System, false); | |
362 LLVM_FALLTHROUGH; | |
363 } | |
364 default: | |
365 AddPath("/usr/include", ExternCSystem, false); | |
366 break; | |
367 } | |
368 } | |
369 | |
370 void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths( | |
371 const LangOptions &LangOpts, const llvm::Triple &triple, | |
372 const HeaderSearchOptions &HSOpts) { | |
373 llvm::Triple::OSType os = triple.getOS(); | |
374 // FIXME: temporary hack: hard-coded paths. | |
375 | |
376 if (triple.isOSDarwin()) { | |
377 llvm_unreachable("Include management is handled in the driver."); | |
378 } | |
379 | |
380 switch (os) { | |
381 case llvm::Triple::Linux: | |
382 case llvm::Triple::Hurd: | |
383 case llvm::Triple::Solaris: | |
384 llvm_unreachable("Include management is handled in the driver."); | |
385 break; | |
386 case llvm::Triple::Win32: | |
387 switch (triple.getEnvironment()) { | |
388 default: llvm_unreachable("Include management is handled in the driver."); | |
389 case llvm::Triple::Cygnus: | |
390 // Cygwin-1.7 | |
391 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3"); | |
392 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3"); | |
393 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4"); | |
394 // g++-4 / Cygwin-1.5 | |
395 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2"); | |
396 break; | |
397 } | |
398 break; | |
399 case llvm::Triple::DragonFly: | |
400 AddPath("/usr/include/c++/5.0", CXXSystem, false); | |
401 break; | |
402 case llvm::Triple::Minix: | |
403 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3", | |
404 "", "", "", triple); | |
405 break; | |
406 default: | |
407 break; | |
408 } | |
409 } | |
410 | |
411 void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang, | |
412 const llvm::Triple &triple, | |
413 const HeaderSearchOptions &HSOpts) { | |
414 // NB: This code path is going away. All of the logic is moving into the | |
415 // driver which has the information necessary to do target-specific | |
416 // selections of default include paths. Each target which moves there will be | |
417 // exempted from this logic here until we can delete the entire pile of code. | |
418 switch (triple.getOS()) { | |
419 default: | |
420 break; // Everything else continues to use this routine's logic. | |
421 | |
422 case llvm::Triple::Emscripten: | |
423 case llvm::Triple::Linux: | |
424 case llvm::Triple::Hurd: | |
425 case llvm::Triple::Solaris: | |
426 case llvm::Triple::WASI: | |
427 return; | |
428 | |
429 case llvm::Triple::Win32: | |
430 if (triple.getEnvironment() != llvm::Triple::Cygnus || | |
431 triple.isOSBinFormatMachO()) | |
432 return; | |
433 break; | |
434 | |
435 case llvm::Triple::UnknownOS: | |
436 if (triple.getArch() == llvm::Triple::wasm32 || | |
437 triple.getArch() == llvm::Triple::wasm64) | |
438 return; | |
439 break; | |
440 } | |
441 | |
442 // All header search logic is handled in the Driver for Darwin. | |
443 if (triple.isOSDarwin()) { | |
444 if (HSOpts.UseStandardSystemIncludes) { | |
445 // Add the default framework include paths on Darwin. | |
446 AddPath("/System/Library/Frameworks", System, true); | |
447 AddPath("/Library/Frameworks", System, true); | |
448 } | |
449 return; | |
450 } | |
451 | |
452 if (Lang.CPlusPlus && !Lang.AsmPreprocessor && | |
453 HSOpts.UseStandardCXXIncludes && HSOpts.UseStandardSystemIncludes) { | |
454 if (HSOpts.UseLibcxx) { | |
455 AddPath("/usr/include/c++/v1", CXXSystem, false); | |
456 } else { | |
457 AddDefaultCPlusPlusIncludePaths(Lang, triple, HSOpts); | |
458 } | |
459 } | |
460 | |
461 AddDefaultCIncludePaths(triple, HSOpts); | |
462 } | |
463 | |
464 /// RemoveDuplicates - If there are duplicate directory entries in the specified | |
465 /// search list, remove the later (dead) ones. Returns the number of non-system | |
466 /// headers removed, which is used to update NumAngled. | |
467 static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, | |
468 unsigned First, bool Verbose) { | |
469 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; | |
470 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; | |
471 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; | |
472 unsigned NonSystemRemoved = 0; | |
473 for (unsigned i = First; i != SearchList.size(); ++i) { | |
474 unsigned DirToRemove = i; | |
475 | |
476 const DirectoryLookup &CurEntry = SearchList[i]; | |
477 | |
478 if (CurEntry.isNormalDir()) { | |
479 // If this isn't the first time we've seen this dir, remove it. | |
480 if (SeenDirs.insert(CurEntry.getDir()).second) | |
481 continue; | |
482 } else if (CurEntry.isFramework()) { | |
483 // If this isn't the first time we've seen this framework dir, remove it. | |
484 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()).second) | |
485 continue; | |
486 } else { | |
487 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); | |
488 // If this isn't the first time we've seen this headermap, remove it. | |
489 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()).second) | |
490 continue; | |
491 } | |
492 | |
493 // If we have a normal #include dir/framework/headermap that is shadowed | |
494 // later in the chain by a system include location, we actually want to | |
495 // ignore the user's request and drop the user dir... keeping the system | |
496 // dir. This is weird, but required to emulate GCC's search path correctly. | |
497 // | |
498 // Since dupes of system dirs are rare, just rescan to find the original | |
499 // that we're nuking instead of using a DenseMap. | |
500 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { | |
501 // Find the dir that this is the same of. | |
502 unsigned FirstDir; | |
503 for (FirstDir = First;; ++FirstDir) { | |
504 assert(FirstDir != i && "Didn't find dupe?"); | |
505 | |
506 const DirectoryLookup &SearchEntry = SearchList[FirstDir]; | |
507 | |
508 // If these are different lookup types, then they can't be the dupe. | |
509 if (SearchEntry.getLookupType() != CurEntry.getLookupType()) | |
510 continue; | |
511 | |
512 bool isSame; | |
513 if (CurEntry.isNormalDir()) | |
514 isSame = SearchEntry.getDir() == CurEntry.getDir(); | |
515 else if (CurEntry.isFramework()) | |
516 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); | |
517 else { | |
518 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); | |
519 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); | |
520 } | |
521 | |
522 if (isSame) | |
523 break; | |
524 } | |
525 | |
526 // If the first dir in the search path is a non-system dir, zap it | |
527 // instead of the system one. | |
528 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) | |
529 DirToRemove = FirstDir; | |
530 } | |
531 | |
532 if (Verbose) { | |
533 llvm::errs() << "ignoring duplicate directory \"" | |
534 << CurEntry.getName() << "\"\n"; | |
535 if (DirToRemove != i) | |
536 llvm::errs() << " as it is a non-system directory that duplicates " | |
537 << "a system directory\n"; | |
538 } | |
539 if (DirToRemove != i) | |
540 ++NonSystemRemoved; | |
541 | |
542 // This is reached if the current entry is a duplicate. Remove the | |
543 // DirToRemove (usually the current dir). | |
544 SearchList.erase(SearchList.begin()+DirToRemove); | |
545 --i; | |
546 } | |
547 return NonSystemRemoved; | |
548 } | |
549 | |
550 | |
551 void InitHeaderSearch::Realize(const LangOptions &Lang) { | |
552 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. | |
553 std::vector<DirectoryLookup> SearchList; | |
554 SearchList.reserve(IncludePath.size()); | |
555 | |
556 // Quoted arguments go first. | |
557 for (auto &Include : IncludePath) | |
558 if (Include.first == Quoted) | |
559 SearchList.push_back(Include.second); | |
560 | |
561 // Deduplicate and remember index. | |
562 RemoveDuplicates(SearchList, 0, Verbose); | |
563 unsigned NumQuoted = SearchList.size(); | |
564 | |
565 for (auto &Include : IncludePath) | |
566 if (Include.first == Angled || Include.first == IndexHeaderMap) | |
567 SearchList.push_back(Include.second); | |
568 | |
569 RemoveDuplicates(SearchList, NumQuoted, Verbose); | |
570 unsigned NumAngled = SearchList.size(); | |
571 | |
572 for (auto &Include : IncludePath) | |
573 if (Include.first == System || Include.first == ExternCSystem || | |
574 (!Lang.ObjC && !Lang.CPlusPlus && Include.first == CSystem) || | |
575 (/*FIXME !Lang.ObjC && */ Lang.CPlusPlus && | |
576 Include.first == CXXSystem) || | |
577 (Lang.ObjC && !Lang.CPlusPlus && Include.first == ObjCSystem) || | |
578 (Lang.ObjC && Lang.CPlusPlus && Include.first == ObjCXXSystem)) | |
579 SearchList.push_back(Include.second); | |
580 | |
581 for (auto &Include : IncludePath) | |
582 if (Include.first == After) | |
583 SearchList.push_back(Include.second); | |
584 | |
585 // Remove duplicates across both the Angled and System directories. GCC does | |
586 // this and failing to remove duplicates across these two groups breaks | |
587 // #include_next. | |
588 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose); | |
589 NumAngled -= NonSystemRemoved; | |
590 | |
591 bool DontSearchCurDir = false; // TODO: set to true if -I- is set? | |
592 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir); | |
593 | |
594 Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes); | |
595 | |
596 // If verbose, print the list of directories that will be searched. | |
597 if (Verbose) { | |
598 llvm::errs() << "#include \"...\" search starts here:\n"; | |
599 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { | |
600 if (i == NumQuoted) | |
601 llvm::errs() << "#include <...> search starts here:\n"; | |
602 StringRef Name = SearchList[i].getName(); | |
603 const char *Suffix; | |
604 if (SearchList[i].isNormalDir()) | |
605 Suffix = ""; | |
606 else if (SearchList[i].isFramework()) | |
607 Suffix = " (framework directory)"; | |
608 else { | |
609 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); | |
610 Suffix = " (headermap)"; | |
611 } | |
612 llvm::errs() << " " << Name << Suffix << "\n"; | |
613 } | |
614 llvm::errs() << "End of search list.\n"; | |
615 } | |
616 } | |
617 | |
618 void clang::ApplyHeaderSearchOptions(HeaderSearch &HS, | |
619 const HeaderSearchOptions &HSOpts, | |
620 const LangOptions &Lang, | |
621 const llvm::Triple &Triple) { | |
622 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot); | |
623 | |
624 // Add the user defined entries. | |
625 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) { | |
626 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i]; | |
627 if (E.IgnoreSysRoot) { | |
628 Init.AddUnmappedPath(E.Path, E.Group, E.IsFramework); | |
629 } else { | |
630 Init.AddPath(E.Path, E.Group, E.IsFramework); | |
631 } | |
632 } | |
633 | |
634 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts); | |
635 | |
636 for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i) | |
637 Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix, | |
638 HSOpts.SystemHeaderPrefixes[i].IsSystemHeader); | |
639 | |
640 if (HSOpts.UseBuiltinIncludes) { | |
641 // Set up the builtin include directory in the module map. | |
642 SmallString<128> P = StringRef(HSOpts.ResourceDir); | |
643 llvm::sys::path::append(P, "include"); | |
644 if (auto Dir = HS.getFileMgr().getDirectory(P)) | |
645 HS.getModuleMap().setBuiltinIncludeDir(*Dir); | |
646 } | |
647 | |
648 Init.Realize(Lang); | |
649 } |