16 #include "clang/Config/config.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"
31 using namespace clang;
32 using namespace clang::frontend;
39 class InitHeaderSearch {
40 std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
43 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
46 std::string IncludeSysroot;
51 InitHeaderSearch(
HeaderSearch &HS,
bool verbose, StringRef sysroot)
52 : Headers(HS),
Verbose(verbose), IncludeSysroot(sysroot),
53 HasSysroot(!(sysroot.empty() || sysroot ==
"/")) {
58 void AddPath(
const Twine &Path,
IncludeDirGroup Group,
bool isFramework);
67 void AddSystemHeaderPrefix(StringRef Prefix,
bool IsSystemHeader) {
68 SystemHeaderPrefixes.emplace_back(Prefix, IsSystemHeader);
73 void AddGnuCPlusPlusIncludePaths(StringRef
Base,
77 const llvm::Triple &triple);
81 void AddMinGWCPlusPlusIncludePaths(StringRef
Base,
86 void AddDefaultCIncludePaths(
const llvm::Triple &triple,
91 void AddDefaultCPlusPlusIncludePaths(
const llvm::Triple &triple,
96 void AddDefaultIncludePaths(
const LangOptions &Lang,
97 const llvm::Triple &triple,
108 #if defined(LLVM_ON_WIN32)
109 return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
111 return llvm::sys::path::is_absolute(Path);
115 void InitHeaderSearch::AddPath(
const Twine &Path,
IncludeDirGroup Group,
121 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
123 AddUnmappedPath(IncludeSysroot + Path, Group, isFramework);
128 AddUnmappedPath(Path, Group, isFramework);
131 void InitHeaderSearch::AddUnmappedPath(
const Twine &Path,
IncludeDirGroup Group,
133 assert(!Path.isTriviallyEmpty() &&
"can't handle empty path here");
137 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
151 IncludePath.push_back(
159 if (
const FileEntry *FE = FM.getFile(MappedPathStr)) {
160 if (
const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
162 IncludePath.push_back(
163 std::make_pair(Group,
171 llvm::errs() <<
"ignoring nonexistent directory \""
172 << MappedPathStr <<
"\"\n";
175 void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef
Base,
179 const llvm::Triple &triple) {
184 llvm::Triple::ArchType arch = triple.getArch();
185 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
187 AddPath(Base +
"/" + ArchDir +
"/" + Dir64,
CXXSystem,
false);
189 AddPath(Base +
"/" + ArchDir +
"/" + Dir32,
CXXSystem,
false);
192 AddPath(Base +
"/backward",
CXXSystem,
false);
195 void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
198 AddPath(Base +
"/" + Arch +
"/" + Version +
"/include/c++",
200 AddPath(Base +
"/" + Arch +
"/" + Version +
"/include/c++/" + Arch,
202 AddPath(Base +
"/" + Arch +
"/" + Version +
"/include/c++/backward",
206 void InitHeaderSearch::AddDefaultCIncludePaths(
const llvm::Triple &triple,
208 llvm::Triple::OSType os = triple.getOS();
212 case llvm::Triple::CloudABI:
213 case llvm::Triple::FreeBSD:
214 case llvm::Triple::NetBSD:
215 case llvm::Triple::OpenBSD:
216 case llvm::Triple::Bitrig:
217 case llvm::Triple::NaCl:
218 case llvm::Triple::PS4:
219 case llvm::Triple::ELFIAMCU:
221 case llvm::Triple::Win32:
222 if (triple.getEnvironment() != llvm::Triple::Cygnus)
227 AddPath(
"/usr/local/include",
System,
false);
238 llvm::sys::path::append(P,
"include");
248 StringRef CIncludeDirs(C_INCLUDE_DIRS);
249 if (CIncludeDirs !=
"") {
251 CIncludeDirs.split(dirs,
":");
252 for (StringRef dir : dirs)
258 case llvm::Triple::Linux:
259 llvm_unreachable(
"Include management is handled in the driver.");
261 case llvm::Triple::CloudABI: {
264 llvm::sys::path::append(P,
"../../..", triple.str(),
"include");
265 AddPath(P,
System,
false);
269 case llvm::Triple::Haiku:
270 AddPath(
"/boot/system/non-packaged/develop/headers",
System,
false);
271 AddPath(
"/boot/system/develop/headers/os",
System,
false);
272 AddPath(
"/boot/system/develop/headers/os/app",
System,
false);
273 AddPath(
"/boot/system/develop/headers/os/arch",
System,
false);
274 AddPath(
"/boot/system/develop/headers/os/device",
System,
false);
275 AddPath(
"/boot/system/develop/headers/os/drivers",
System,
false);
276 AddPath(
"/boot/system/develop/headers/os/game",
System,
false);
277 AddPath(
"/boot/system/develop/headers/os/interface",
System,
false);
278 AddPath(
"/boot/system/develop/headers/os/kernel",
System,
false);
279 AddPath(
"/boot/system/develop/headers/os/locale",
System,
false);
280 AddPath(
"/boot/system/develop/headers/os/mail",
System,
false);
281 AddPath(
"/boot/system/develop/headers/os/media",
System,
false);
282 AddPath(
"/boot/system/develop/headers/os/midi",
System,
false);
283 AddPath(
"/boot/system/develop/headers/os/midi2",
System,
false);
284 AddPath(
"/boot/system/develop/headers/os/net",
System,
false);
285 AddPath(
"/boot/system/develop/headers/os/opengl",
System,
false);
286 AddPath(
"/boot/system/develop/headers/os/storage",
System,
false);
287 AddPath(
"/boot/system/develop/headers/os/support",
System,
false);
288 AddPath(
"/boot/system/develop/headers/os/translation",
System,
false);
289 AddPath(
"/boot/system/develop/headers/os/add-ons/graphics",
System,
false);
290 AddPath(
"/boot/system/develop/headers/os/add-ons/input_server",
System,
false);
291 AddPath(
"/boot/system/develop/headers/os/add-ons/mail_daemon",
System,
false);
292 AddPath(
"/boot/system/develop/headers/os/add-ons/registrar",
System,
false);
293 AddPath(
"/boot/system/develop/headers/os/add-ons/screen_saver",
System,
false);
294 AddPath(
"/boot/system/develop/headers/os/add-ons/tracker",
System,
false);
295 AddPath(
"/boot/system/develop/headers/os/be_apps/Deskbar",
System,
false);
296 AddPath(
"/boot/system/develop/headers/os/be_apps/NetPositive",
System,
false);
297 AddPath(
"/boot/system/develop/headers/os/be_apps/Tracker",
System,
false);
298 AddPath(
"/boot/system/develop/headers/3rdparty",
System,
false);
299 AddPath(
"/boot/system/develop/headers/bsd",
System,
false);
300 AddPath(
"/boot/system/develop/headers/glibc",
System,
false);
301 AddPath(
"/boot/system/develop/headers/posix",
System,
false);
302 AddPath(
"/boot/system/develop/headers",
System,
false);
304 case llvm::Triple::RTEMS:
306 case llvm::Triple::Win32:
307 switch (triple.getEnvironment()) {
308 default: llvm_unreachable(
"Include management is handled in the driver.");
309 case llvm::Triple::Cygnus:
310 AddPath(
"/usr/include/w32api",
System,
false);
312 case llvm::Triple::GNU:
321 case llvm::Triple::CloudABI:
322 case llvm::Triple::RTEMS:
323 case llvm::Triple::NaCl:
324 case llvm::Triple::ELFIAMCU:
326 case llvm::Triple::PS4: {
328 std::string BaseSDKPath =
"";
330 const char *envValue = getenv(
"SCE_ORBIS_SDK_DIR");
332 BaseSDKPath = envValue;
340 llvm::sys::path::append(P,
"../../..");
341 BaseSDKPath = P.str();
344 AddPath(BaseSDKPath +
"/target/include",
System,
false);
345 if (triple.isPS4CPU())
346 AddPath(BaseSDKPath +
"/target/include_common",
System,
false);
355 void InitHeaderSearch::
356 AddDefaultCPlusPlusIncludePaths(
const llvm::Triple &triple,
const HeaderSearchOptions &HSOpts) {
357 llvm::Triple::OSType os = triple.getOS();
360 if (triple.isOSDarwin()) {
361 switch (triple.getArch()) {
364 case llvm::Triple::ppc:
365 case llvm::Triple::ppc64:
366 AddGnuCPlusPlusIncludePaths(
"/usr/include/c++/4.2.1",
367 "powerpc-apple-darwin10",
"",
"ppc64",
369 AddGnuCPlusPlusIncludePaths(
"/usr/include/c++/4.0.0",
370 "powerpc-apple-darwin10",
"",
"ppc64",
374 case llvm::Triple::x86:
375 case llvm::Triple::x86_64:
376 AddGnuCPlusPlusIncludePaths(
"/usr/include/c++/4.2.1",
377 "i686-apple-darwin10",
"",
"x86_64", triple);
378 AddGnuCPlusPlusIncludePaths(
"/usr/include/c++/4.0.0",
379 "i686-apple-darwin8",
"",
"", triple);
382 case llvm::Triple::arm:
383 case llvm::Triple::thumb:
384 AddGnuCPlusPlusIncludePaths(
"/usr/include/c++/4.2.1",
385 "arm-apple-darwin10",
"v7",
"", triple);
386 AddGnuCPlusPlusIncludePaths(
"/usr/include/c++/4.2.1",
387 "arm-apple-darwin10",
"v6",
"", triple);
390 case llvm::Triple::aarch64:
391 AddGnuCPlusPlusIncludePaths(
"/usr/include/c++/4.2.1",
392 "arm64-apple-darwin10",
"",
"", triple);
399 case llvm::Triple::Linux:
400 llvm_unreachable(
"Include management is handled in the driver.");
402 case llvm::Triple::Win32:
403 switch (triple.getEnvironment()) {
404 default: llvm_unreachable(
"Include management is handled in the driver.");
405 case llvm::Triple::Cygnus:
407 AddMinGWCPlusPlusIncludePaths(
"/usr/lib/gcc",
"i686-pc-cygwin",
"4.7.3");
408 AddMinGWCPlusPlusIncludePaths(
"/usr/lib/gcc",
"i686-pc-cygwin",
"4.5.3");
409 AddMinGWCPlusPlusIncludePaths(
"/usr/lib/gcc",
"i686-pc-cygwin",
"4.3.4");
411 AddMinGWCPlusPlusIncludePaths(
"/usr/lib/gcc",
"i686-pc-cygwin",
"4.3.2");
415 case llvm::Triple::DragonFly:
416 AddPath(
"/usr/include/c++/5.0",
CXXSystem,
false);
418 case llvm::Triple::OpenBSD: {
419 std::string t = triple.getTriple();
420 if (t.substr(0, 6) ==
"x86_64")
421 t.replace(0, 6,
"amd64");
422 AddGnuCPlusPlusIncludePaths(
"/usr/include/g++",
426 case llvm::Triple::Minix:
427 AddGnuCPlusPlusIncludePaths(
"/usr/gnu/include/c++/4.4.3",
435 void InitHeaderSearch::AddDefaultIncludePaths(
const LangOptions &Lang,
436 const llvm::Triple &triple,
442 switch (triple.getOS()) {
446 case llvm::Triple::Linux:
449 case llvm::Triple::Win32:
450 if (triple.getEnvironment() != llvm::Triple::Cygnus ||
451 triple.isOSBinFormatMachO())
459 if (triple.isOSDarwin()) {
464 StringRef NoVer = llvm::sys::path::parent_path(HSOpts.
ResourceDir);
466 StringRef Lib = llvm::sys::path::parent_path(NoVer);
471 llvm::sys::path::append(P,
"include",
"c++",
"v1");
475 AddPath(
"/usr/include/c++/v1",
CXXSystem,
false);
477 AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
481 AddDefaultCIncludePaths(triple, HSOpts);
485 if (triple.isOSDarwin()) {
486 AddPath(
"/System/Library/Frameworks",
System,
true);
487 AddPath(
"/Library/Frameworks",
System,
true);
496 unsigned First,
bool Verbose) {
497 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
498 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
499 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
500 unsigned NonSystemRemoved = 0;
501 for (
unsigned i = First; i != SearchList.size(); ++i) {
502 unsigned DirToRemove = i;
508 if (SeenDirs.insert(CurEntry.
getDir()).second)
515 assert(CurEntry.
isHeaderMap() &&
"Not a headermap or normal dir?");
517 if (SeenHeaderMaps.insert(CurEntry.
getHeaderMap()).second)
531 for (FirstDir = First;; ++FirstDir) {
532 assert(FirstDir != i &&
"Didn't find dupe?");
546 assert(CurEntry.
isHeaderMap() &&
"Not a headermap or normal dir?");
556 if (SearchList[FirstDir].getDirCharacteristic() ==
SrcMgr::C_User)
557 DirToRemove = FirstDir;
561 llvm::errs() <<
"ignoring duplicate directory \""
562 << CurEntry.
getName() <<
"\"\n";
563 if (DirToRemove != i)
564 llvm::errs() <<
" as it is a non-system directory that duplicates "
565 <<
"a system directory\n";
567 if (DirToRemove != i)
572 SearchList.erase(SearchList.begin()+DirToRemove);
575 return NonSystemRemoved;
579 void InitHeaderSearch::Realize(
const LangOptions &Lang) {
581 std::vector<DirectoryLookup> SearchList;
582 SearchList.reserve(IncludePath.size());
585 for (
auto &Include : IncludePath)
586 if (Include.first ==
Quoted)
587 SearchList.push_back(Include.second);
591 unsigned NumQuoted = SearchList.size();
593 for (
auto &Include : IncludePath)
595 SearchList.push_back(Include.second);
598 unsigned NumAngled = SearchList.size();
600 for (
auto &Include : IncludePath)
602 (!Lang.ObjC1 && !Lang.CPlusPlus && Include.first ==
CSystem) ||
605 (Lang.ObjC1 && !Lang.CPlusPlus && Include.first ==
ObjCSystem) ||
606 (Lang.ObjC1 && Lang.CPlusPlus && Include.first ==
ObjCXXSystem))
607 SearchList.push_back(Include.second);
609 for (
auto &Include : IncludePath)
610 if (Include.first ==
After)
611 SearchList.push_back(Include.second);
617 NumAngled -= NonSystemRemoved;
619 bool DontSearchCurDir =
false;
620 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
622 Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
626 llvm::errs() <<
"#include \"...\" search starts here:\n";
627 for (
unsigned i = 0, e = SearchList.size(); i != e; ++i) {
629 llvm::errs() <<
"#include <...> search starts here:\n";
630 StringRef
Name = SearchList[i].getName();
632 if (SearchList[i].isNormalDir())
634 else if (SearchList[i].isFramework())
635 Suffix =
" (framework directory)";
637 assert(SearchList[i].isHeaderMap() &&
"Unknown DirectoryLookup");
638 Suffix =
" (headermap)";
640 llvm::errs() <<
" " << Name << Suffix <<
"\n";
642 llvm::errs() <<
"End of search list.\n";
649 const llvm::Triple &Triple) {
653 for (
unsigned i = 0, e = HSOpts.
UserEntries.size(); i != e; ++i) {
662 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
671 llvm::sys::path::append(P,
"include");
Paths for '#include <>' added by '-I'.
Implements support for file system lookup, file system caching, and directory search management...
Defines the clang::FileManager interface and associated types.
Like System, but headers are implicitly wrapped in extern "C".
SrcMgr::CharacteristicKind getDirCharacteristic() const
DirCharacteristic - The type of directory this is, one of the DirType enum values.
Like System, but only used for C++.
The base class of the type hierarchy.
Like System, but only used for ObjC++.
StringRef getName() const
getName - Return the directory or filename corresponding to this lookup object.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Like System, but searched after the system directories.
bool isFramework() const
isFramework - True if this is a framework directory.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
const DirectoryEntry * getDirectory(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
const HeaderMap * getHeaderMap() const
getHeaderMap - Return the directory that this entry refers to.
Defines the clang::LangOptions interface.
IncludeDirGroup
IncludeDirGroup - Identifies the group an include Entry belongs to, representing its relative positiv...
LookupType_t getLookupType() const
getLookupType - Return the kind of directory lookup that this is: either a normal directory...
bool isNormalDir() const
isNormalDir - Return true if this is a normal directory, not a header map.
DirectoryLookup - This class represents one entry in the search list that specifies the search order ...
void setBuiltinIncludeDir(const DirectoryEntry *Dir)
Set the directory that contains Clang-supplied include files, such as our stdarg.h or tgmath...
void ApplyHeaderSearchOptions(HeaderSearch &HS, const HeaderSearchOptions &HSOpts, const LangOptions &Lang, const llvm::Triple &triple)
Apply the header search options to get given HeaderSearch object.
const DirectoryEntry * getFrameworkDir() const
getFrameworkDir - Return the directory that this framework refers to.
Cached information about one file (either on disk or in the virtual file system). ...
Like System, but only used for ObjC.
'#include ""' paths, added by 'gcc -iquote'.
Like System, but only used for C.
detail::InMemoryDirectory::const_iterator E
Cached information about one directory (either on disk or in the virtual file system).
bool isHeaderMap() const
isHeaderMap - Return true if this is a header map, not a normal directory.
Like Angled, but marks header maps used when.
const DirectoryEntry * getDir() const
getDir - Return the directory that this entry refers to.