16#include "clang/Config/config.h"
20#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Twine.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/Path.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/TargetParser/Triple.h"
36struct DirectoryLookupInfo {
39 std::optional<unsigned> UserEntryIdx;
42 std::optional<unsigned> UserEntryIdx)
43 :
Group(
Group), Lookup(Lookup), UserEntryIdx(UserEntryIdx) {}
49class InitHeaderSearch {
50 std::vector<DirectoryLookupInfo> IncludePath;
51 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
54 std::string IncludeSysroot;
58 InitHeaderSearch(
HeaderSearch &HS,
bool verbose, StringRef sysroot)
59 : Headers(HS), Verbose(verbose), IncludeSysroot(
std::string(sysroot)),
60 HasSysroot(!(sysroot.empty() || sysroot ==
"/")) {}
66 std::optional<unsigned> UserEntryIdx = std::nullopt);
73 std::optional<unsigned> UserEntryIdx = std::nullopt);
76 void AddSystemHeaderPrefix(StringRef Prefix,
bool IsSystemHeader) {
77 SystemHeaderPrefixes.emplace_back(std::string(Prefix), IsSystemHeader);
81 void AddMinGWCPlusPlusIncludePaths(StringRef
Base,
86 void AddDefaultCIncludePaths(
const llvm::Triple &triple,
90 void AddDefaultCPlusPlusIncludePaths(
const LangOptions &LangOpts,
91 const llvm::Triple &triple,
96 bool ShouldAddDefaultIncludePaths(
const llvm::Triple &triple);
99 void AddDefaultIncludePaths(
const LangOptions &Lang,
100 const llvm::Triple &triple,
111 return !
Path.empty() && llvm::sys::path::is_separator(
Path[0]);
113 return llvm::sys::path::is_absolute(
Path);
119 std::optional<unsigned> UserEntryIdx) {
124 StringRef MappedPathStr =
Path.toStringRef(MappedPathStorage);
126 return AddUnmappedPath(IncludeSysroot +
Path, Group, isFramework,
131 return AddUnmappedPath(
Path, Group, isFramework, UserEntryIdx);
136 std::optional<unsigned> UserEntryIdx) {
137 assert(!
Path.isTriviallyEmpty() &&
"can't handle empty path here");
141 StringRef MappedPathStr =
Path.toStringRef(MappedPathStorage);
144 if (HasSysroot && (MappedPathStr.starts_with(
"/usr/include") ||
145 MappedPathStr.starts_with(
"/usr/local/include"))) {
173 IncludePath.emplace_back(
182 llvm::errs() <<
"ignoring nonexistent directory \""
183 << MappedPathStr <<
"\"\n";
187void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef
Base,
190 AddPath(
Base +
"/" + Arch +
"/" + Version +
"/include/c++",
192 AddPath(
Base +
"/" + Arch +
"/" + Version +
"/include/c++/" + Arch,
194 AddPath(
Base +
"/" + Arch +
"/" + Version +
"/include/c++/backward",
198void InitHeaderSearch::AddDefaultCIncludePaths(
const llvm::Triple &triple,
200 if (!ShouldAddDefaultIncludePaths(triple))
201 llvm_unreachable(
"Include management is handled in the driver.");
203 llvm::Triple::OSType os = triple.getOS();
207 case llvm::Triple::Win32:
208 if (triple.getEnvironment() != llvm::Triple::Cygnus)
213 AddPath(
"/usr/local/include",
System,
false);
224 llvm::sys::path::append(
P,
"include");
234 StringRef CIncludeDirs(C_INCLUDE_DIRS);
235 if (CIncludeDirs !=
"") {
237 CIncludeDirs.split(dirs,
":");
238 for (StringRef dir : dirs)
244 case llvm::Triple::Win32:
245 switch (triple.getEnvironment()) {
246 default: llvm_unreachable(
"Include management is handled in the driver.");
247 case llvm::Triple::Cygnus:
248 AddPath(
"/usr/include/w32api",
System,
false);
250 case llvm::Triple::GNU:
261void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(
262 const LangOptions &LangOpts,
const llvm::Triple &triple,
264 if (!ShouldAddDefaultIncludePaths(triple))
265 llvm_unreachable(
"Include management is handled in the driver.");
268 llvm::Triple::OSType os = triple.getOS();
270 case llvm::Triple::Win32:
271 switch (triple.getEnvironment()) {
272 default: llvm_unreachable(
"Include management is handled in the driver.");
273 case llvm::Triple::Cygnus:
275 AddMinGWCPlusPlusIncludePaths(
"/usr/lib/gcc",
"i686-pc-cygwin",
"4.7.3");
276 AddMinGWCPlusPlusIncludePaths(
"/usr/lib/gcc",
"i686-pc-cygwin",
"4.5.3");
277 AddMinGWCPlusPlusIncludePaths(
"/usr/lib/gcc",
"i686-pc-cygwin",
"4.3.4");
279 AddMinGWCPlusPlusIncludePaths(
"/usr/lib/gcc",
"i686-pc-cygwin",
"4.3.2");
288bool InitHeaderSearch::ShouldAddDefaultIncludePaths(
289 const llvm::Triple &triple) {
290 switch (triple.getOS()) {
291 case llvm::Triple::AIX:
292 case llvm::Triple::DragonFly:
293 case llvm::Triple::ELFIAMCU:
294 case llvm::Triple::Emscripten:
295 case llvm::Triple::FreeBSD:
296 case llvm::Triple::Fuchsia:
297 case llvm::Triple::Haiku:
298 case llvm::Triple::Hurd:
299 case llvm::Triple::Linux:
300 case llvm::Triple::LiteOS:
301 case llvm::Triple::NaCl:
302 case llvm::Triple::NetBSD:
303 case llvm::Triple::OpenBSD:
304 case llvm::Triple::PS4:
305 case llvm::Triple::PS5:
306 case llvm::Triple::RTEMS:
307 case llvm::Triple::Solaris:
308 case llvm::Triple::WASI:
309 case llvm::Triple::ZOS:
312 case llvm::Triple::Win32:
313 if (triple.getEnvironment() != llvm::Triple::Cygnus ||
314 triple.isOSBinFormatMachO())
318 case llvm::Triple::UnknownOS:
330void InitHeaderSearch::AddDefaultIncludePaths(
331 const LangOptions &Lang,
const llvm::Triple &triple,
338 if (!ShouldAddDefaultIncludePaths(triple))
343 if (triple.isOSDarwin()) {
346 if (triple.isDriverKit()) {
347 AddPath(
"/System/DriverKit/System/Library/Frameworks",
System,
true);
349 AddPath(
"/System/Library/Frameworks",
System,
true);
350 AddPath(
"/Library/Frameworks",
System,
true);
356 if (
Lang.CPlusPlus && !
Lang.AsmPreprocessor &&
359 AddPath(
"/usr/include/c++/v1",
CXXSystem,
false);
361 AddDefaultCPlusPlusIncludePaths(Lang, triple, HSOpts);
365 AddDefaultCIncludePaths(triple, HSOpts);
372 unsigned First,
bool Verbose) {
376 unsigned NonSystemRemoved = 0;
377 for (
unsigned i =
First; i != SearchList.size(); ++i) {
378 unsigned DirToRemove = i;
384 if (SeenDirs.insert(CurEntry.
getDir()).second)
391 assert(CurEntry.
isHeaderMap() &&
"Not a headermap or normal dir?");
393 if (SeenHeaderMaps.insert(CurEntry.
getHeaderMap()).second)
407 for (FirstDir =
First;; ++FirstDir) {
408 assert(FirstDir != i &&
"Didn't find dupe?");
422 assert(CurEntry.
isHeaderMap() &&
"Not a headermap or normal dir?");
432 if (SearchList[FirstDir].Lookup.getDirCharacteristic() ==
SrcMgr::C_User)
433 DirToRemove = FirstDir;
437 llvm::errs() <<
"ignoring duplicate directory \""
438 << CurEntry.
getName() <<
"\"\n";
439 if (DirToRemove != i)
440 llvm::errs() <<
" as it is a non-system directory that duplicates "
441 <<
"a system directory\n";
443 if (DirToRemove != i)
448 SearchList.erase(SearchList.begin()+DirToRemove);
451 return NonSystemRemoved;
455static std::vector<DirectoryLookup>
457 std::vector<DirectoryLookup> Lookups;
458 Lookups.reserve(Infos.size());
459 llvm::transform(Infos, std::back_inserter(Lookups),
460 [](
const DirectoryLookupInfo &Info) {
return Info.Lookup; });
465static llvm::DenseMap<unsigned, unsigned>
467 llvm::DenseMap<unsigned, unsigned> LookupsToUserEntries;
468 for (
unsigned I = 0,
E = Infos.size();
I <
E; ++
I) {
470 if (Infos[
I].UserEntryIdx)
471 LookupsToUserEntries.insert({
I, *Infos[
I].UserEntryIdx});
473 return LookupsToUserEntries;
476void InitHeaderSearch::Realize(
const LangOptions &Lang) {
478 std::vector<DirectoryLookupInfo> SearchList;
479 SearchList.reserve(IncludePath.size());
482 for (
auto &Include : IncludePath)
484 SearchList.push_back(Include);
488 unsigned NumQuoted = SearchList.size();
490 for (
auto &Include : IncludePath)
492 SearchList.push_back(Include);
495 unsigned NumAngled = SearchList.size();
497 for (
auto &Include : IncludePath)
504 SearchList.push_back(Include);
506 for (
auto &Include : IncludePath)
508 SearchList.push_back(Include);
513 unsigned NonSystemRemoved =
RemoveDuplicates(SearchList, NumQuoted, Verbose);
514 NumAngled -= NonSystemRemoved;
523 llvm::errs() <<
"#include \"...\" search starts here:\n";
524 for (
unsigned i = 0, e = SearchList.size(); i != e; ++i) {
526 llvm::errs() <<
"#include <...> search starts here:\n";
527 StringRef Name = SearchList[i].Lookup.getName();
529 if (SearchList[i].Lookup.isNormalDir())
531 else if (SearchList[i].Lookup.isFramework())
532 Suffix =
" (framework directory)";
534 assert(SearchList[i].Lookup.isHeaderMap() &&
"Unknown DirectoryLookup");
535 Suffix =
" (headermap)";
537 llvm::errs() <<
" " << Name << Suffix <<
"\n";
539 llvm::errs() <<
"End of search list.\n";
546 const llvm::Triple &Triple) {
550 for (
unsigned i = 0, e = HSOpts.
UserEntries.size(); i != e; ++i) {
552 if (
E.IgnoreSysRoot) {
553 Init.AddUnmappedPath(
E.Path,
E.Group,
E.IsFramework, i);
555 Init.AddPath(
E.Path,
E.Group,
E.IsFramework, i);
559 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
568 llvm::sys::path::append(
P,
"include");
Defines the clang::FileManager interface and associated types.
Defines the clang::LangOptions interface.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
DirectoryLookup - This class represents one entry in the search list that specifies the search order ...
SrcMgr::CharacteristicKind getDirCharacteristic() const
DirCharacteristic - The type of directory this is, one of the DirType enum values.
const DirectoryEntry * getFrameworkDir() const
getFrameworkDir - Return the directory that this framework refers to.
bool isFramework() const
isFramework - True if this is a framework directory.
bool isHeaderMap() const
isHeaderMap - Return true if this is a header map, not a normal directory.
StringRef getName() const
getName - Return the directory or filename corresponding to this lookup object.
LookupType_t getLookupType() const
getLookupType - Return the kind of directory lookup that this is: either a normal directory,...
const DirectoryEntry * getDir() const
getDir - Return the directory that this entry refers to.
bool isNormalDir() const
isNormalDir - Return true if this is a normal directory, not a header map.
const HeaderMap * getHeaderMap() const
getHeaderMap - Return the directory that this entry refers to.
Implements support for file system lookup, file system caching, and directory search management.
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Get a FileEntryRef if it exists, without doing anything on error.
OptionalDirectoryEntryRef getOptionalDirectoryRef(StringRef DirName, bool CacheFailure=true)
Get a DirectoryEntryRef if it exists, without doing anything on error.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
void setBuiltinIncludeDir(DirectoryEntryRef Dir)
Set the directory that contains Clang-supplied include files, such as our stdarg.h or tgmath....
The base class of the type hierarchy.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
IncludeDirGroup
IncludeDirGroup - Identifies the group an include Entry belongs to, representing its relative positiv...
@ CXXSystem
Like System, but only used for C++.
@ Angled
Paths for '#include <>' added by '-I'.
@ CSystem
Like System, but only used for C.
@ System
Like Angled, but marks system directories.
@ Quoted
'#include ""' paths, added by 'gcc -iquote'.
@ ExternCSystem
Like System, but headers are implicitly wrapped in extern "C".
@ ObjCSystem
Like System, but only used for ObjC.
@ IndexHeaderMap
Like Angled, but marks header maps used when building frameworks.
@ ObjCXXSystem
Like System, but only used for ObjC++.
@ After
Like System, but searched after the system directories.
The JSON file list parser is used to communicate input to InstallAPI.
void ApplyHeaderSearchOptions(HeaderSearch &HS, const HeaderSearchOptions &HSOpts, const LangOptions &Lang, const llvm::Triple &triple)
Apply the header search options to get given HeaderSearch object.