15#include "clang/Config/config.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/Path.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/TargetParser/Triple.h"
33struct DirectoryLookupInfo {
36 std::optional<unsigned> UserEntryIdx;
39 std::optional<unsigned> UserEntryIdx)
40 :
Group(
Group), Lookup(Lookup), UserEntryIdx(UserEntryIdx) {}
46class InitHeaderSearch {
47 std::vector<DirectoryLookupInfo> IncludePath;
48 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
51 std::string IncludeSysroot;
55 InitHeaderSearch(
HeaderSearch &HS,
bool verbose, StringRef sysroot)
56 : Headers(HS), Verbose(verbose), IncludeSysroot(
std::string(sysroot)),
57 HasSysroot(!(sysroot.empty() || sysroot ==
"/")) {}
63 std::optional<unsigned> UserEntryIdx = std::nullopt);
70 std::optional<unsigned> UserEntryIdx = std::nullopt);
73 void AddSystemHeaderPrefix(StringRef Prefix,
bool IsSystemHeader) {
74 SystemHeaderPrefixes.emplace_back(std::string(Prefix), IsSystemHeader);
78 void AddDefaultCIncludePaths(
const llvm::Triple &triple,
83 bool ShouldAddDefaultIncludePaths(
const llvm::Triple &triple);
86 void AddDefaultIncludePaths(
const LangOptions &Lang,
87 const llvm::Triple &triple,
98 return !
Path.empty() && llvm::sys::path::is_separator(
Path[0]);
100 return llvm::sys::path::is_absolute(
Path);
106 std::optional<unsigned> UserEntryIdx) {
111 StringRef MappedPathStr =
Path.toStringRef(MappedPathStorage);
113 return AddUnmappedPath(IncludeSysroot +
Path, Group, isFramework,
118 return AddUnmappedPath(
Path, Group, isFramework, UserEntryIdx);
123 std::optional<unsigned> UserEntryIdx) {
124 assert(!
Path.isTriviallyEmpty() &&
"can't handle empty path here");
128 StringRef MappedPathStr =
Path.toStringRef(MappedPathStorage);
131 if (HasSysroot && (MappedPathStr.starts_with(
"/usr/include") ||
132 MappedPathStr.starts_with(
"/usr/local/include"))) {
168 llvm::errs() <<
"ignoring nonexistent directory \""
169 << MappedPathStr <<
"\"\n";
173void InitHeaderSearch::AddDefaultCIncludePaths(
const llvm::Triple &triple,
175 if (!ShouldAddDefaultIncludePaths(triple))
176 llvm_unreachable(
"Include management is handled in the driver.");
180 AddPath(
"/usr/local/include",
System,
false);
189 llvm::sys::path::append(
P,
"include");
199 StringRef CIncludeDirs(C_INCLUDE_DIRS);
200 if (CIncludeDirs !=
"") {
202 CIncludeDirs.split(dirs,
":");
203 for (StringRef dir : dirs)
211bool InitHeaderSearch::ShouldAddDefaultIncludePaths(
212 const llvm::Triple &triple) {
213 switch (triple.getOS()) {
214 case llvm::Triple::AIX:
215 case llvm::Triple::DragonFly:
216 case llvm::Triple::ELFIAMCU:
217 case llvm::Triple::Emscripten:
218 case llvm::Triple::FreeBSD:
219 case llvm::Triple::Fuchsia:
220 case llvm::Triple::Haiku:
221 case llvm::Triple::Hurd:
222 case llvm::Triple::Linux:
223 case llvm::Triple::LiteOS:
224 case llvm::Triple::Managarm:
225 case llvm::Triple::NetBSD:
226 case llvm::Triple::OpenBSD:
227 case llvm::Triple::PS4:
228 case llvm::Triple::PS5:
229 case llvm::Triple::RTEMS:
230 case llvm::Triple::Solaris:
231 case llvm::Triple::UEFI:
232 case llvm::Triple::WASI:
233 case llvm::Triple::Win32:
234 case llvm::Triple::ZOS:
237 case llvm::Triple::UnknownOS:
238 if (triple.isWasm() || triple.isAppleMachO())
246 if (triple.isOSDarwin())
252void InitHeaderSearch::AddDefaultIncludePaths(
253 const LangOptions &Lang,
const llvm::Triple &triple,
260 if (!ShouldAddDefaultIncludePaths(triple))
263 if (
Lang.CPlusPlus && !
Lang.AsmPreprocessor &&
266 AddPath(
"/usr/include/c++/v1",
CXXSystem,
false);
270 AddDefaultCIncludePaths(triple, HSOpts);
277 unsigned First,
bool Verbose) {
281 unsigned NonSystemRemoved = 0;
282 for (
unsigned i =
First; i != SearchList.size(); ++i) {
283 unsigned DirToRemove = i;
289 if (SeenDirs.insert(CurEntry.
getDir()).second)
296 assert(CurEntry.
isHeaderMap() &&
"Not a headermap or normal dir?");
298 if (SeenHeaderMaps.insert(CurEntry.
getHeaderMap()).second)
312 for (FirstDir =
First;; ++FirstDir) {
313 assert(FirstDir != i &&
"Didn't find dupe?");
327 assert(CurEntry.
isHeaderMap() &&
"Not a headermap or normal dir?");
337 if (SearchList[FirstDir].Lookup.getDirCharacteristic() ==
SrcMgr::C_User)
338 DirToRemove = FirstDir;
342 llvm::errs() <<
"ignoring duplicate directory \""
343 << CurEntry.
getName() <<
"\"\n";
344 if (DirToRemove != i)
345 llvm::errs() <<
" as it is a non-system directory that duplicates "
346 <<
"a system directory\n";
348 if (DirToRemove != i)
353 SearchList.erase(SearchList.begin()+DirToRemove);
356 return NonSystemRemoved;
360static std::vector<DirectoryLookup>
362 std::vector<DirectoryLookup> Lookups;
363 Lookups.reserve(Infos.size());
364 llvm::transform(Infos, std::back_inserter(Lookups),
365 [](
const DirectoryLookupInfo &Info) {
return Info.Lookup; });
370static llvm::DenseMap<unsigned, unsigned>
372 llvm::DenseMap<unsigned, unsigned> LookupsToUserEntries;
373 for (
unsigned I = 0,
E = Infos.size(); I <
E; ++I) {
375 if (Infos[I].UserEntryIdx)
376 LookupsToUserEntries.insert({I, *Infos[I].UserEntryIdx});
378 return LookupsToUserEntries;
381void InitHeaderSearch::Realize(
const LangOptions &Lang) {
383 std::vector<DirectoryLookupInfo> SearchList;
384 SearchList.reserve(IncludePath.size());
387 for (
auto &Include : IncludePath)
389 SearchList.push_back(Include);
393 unsigned NumQuoted = SearchList.size();
395 for (
auto &Include : IncludePath)
397 SearchList.push_back(Include);
400 unsigned NumAngled = SearchList.size();
402 for (
auto &Include : IncludePath)
409 SearchList.push_back(Include);
411 for (
auto &Include : IncludePath)
413 SearchList.push_back(Include);
418 unsigned NonSystemRemoved =
RemoveDuplicates(SearchList, NumQuoted, Verbose);
419 NumAngled -= NonSystemRemoved;
428 llvm::errs() <<
"#include \"...\" search starts here:\n";
429 for (
unsigned i = 0, e = SearchList.size(); i != e; ++i) {
431 llvm::errs() <<
"#include <...> search starts here:\n";
432 StringRef Name = SearchList[i].Lookup.getName();
434 if (SearchList[i].Lookup.isNormalDir())
436 else if (SearchList[i].Lookup.isFramework())
437 Suffix =
" (framework directory)";
439 assert(SearchList[i].Lookup.isHeaderMap() &&
"Unknown DirectoryLookup");
440 Suffix =
" (headermap)";
442 llvm::errs() <<
" " << Name << Suffix <<
"\n";
444 llvm::errs() <<
"End of search list.\n";
451 const llvm::Triple &Triple) {
455 for (
unsigned i = 0, e = HSOpts.
UserEntries.size(); i != e; ++i) {
457 if (
E.IgnoreSysRoot) {
458 Init.AddUnmappedPath(
E.Path,
E.Group,
E.IsFramework, i);
460 Init.AddPath(
E.Path,
E.Group,
E.IsFramework, i);
464 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
473 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.
@ 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.