17330f729Sjoerg //===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file implements the InitHeaderSearch class.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/Basic/FileManager.h"
147330f729Sjoerg #include "clang/Basic/LangOptions.h"
157330f729Sjoerg #include "clang/Config/config.h" // C_INCLUDE_DIRS
167330f729Sjoerg #include "clang/Frontend/FrontendDiagnostic.h"
177330f729Sjoerg #include "clang/Frontend/Utils.h"
187330f729Sjoerg #include "clang/Lex/HeaderMap.h"
197330f729Sjoerg #include "clang/Lex/HeaderSearch.h"
207330f729Sjoerg #include "clang/Lex/HeaderSearchOptions.h"
217330f729Sjoerg #include "llvm/ADT/SmallPtrSet.h"
227330f729Sjoerg #include "llvm/ADT/SmallString.h"
237330f729Sjoerg #include "llvm/ADT/SmallVector.h"
247330f729Sjoerg #include "llvm/ADT/StringExtras.h"
257330f729Sjoerg #include "llvm/ADT/Triple.h"
267330f729Sjoerg #include "llvm/ADT/Twine.h"
277330f729Sjoerg #include "llvm/Support/ErrorHandling.h"
287330f729Sjoerg #include "llvm/Support/Path.h"
297330f729Sjoerg #include "llvm/Support/raw_ostream.h"
307330f729Sjoerg
317330f729Sjoerg using namespace clang;
327330f729Sjoerg using namespace clang::frontend;
337330f729Sjoerg
347330f729Sjoerg namespace {
357330f729Sjoerg
367330f729Sjoerg /// InitHeaderSearch - This class makes it easier to set the search paths of
377330f729Sjoerg /// a HeaderSearch object. InitHeaderSearch stores several search path lists
387330f729Sjoerg /// internally, which can be sent to a HeaderSearch object in one swoop.
397330f729Sjoerg class InitHeaderSearch {
407330f729Sjoerg std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
417330f729Sjoerg typedef std::vector<std::pair<IncludeDirGroup,
427330f729Sjoerg DirectoryLookup> >::const_iterator path_iterator;
437330f729Sjoerg std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
447330f729Sjoerg HeaderSearch &Headers;
457330f729Sjoerg bool Verbose;
467330f729Sjoerg std::string IncludeSysroot;
477330f729Sjoerg bool HasSysroot;
487330f729Sjoerg
497330f729Sjoerg public:
InitHeaderSearch(HeaderSearch & HS,bool verbose,StringRef sysroot)507330f729Sjoerg InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
51*e038c9c4Sjoerg : Headers(HS), Verbose(verbose), IncludeSysroot(std::string(sysroot)),
52*e038c9c4Sjoerg HasSysroot(!(sysroot.empty() || sysroot == "/")) {}
537330f729Sjoerg
547330f729Sjoerg /// AddPath - Add the specified path to the specified group list, prefixing
557330f729Sjoerg /// the sysroot if used.
567330f729Sjoerg /// Returns true if the path exists, false if it was ignored.
577330f729Sjoerg bool AddPath(const Twine &Path, IncludeDirGroup Group, bool isFramework);
587330f729Sjoerg
597330f729Sjoerg /// AddUnmappedPath - Add the specified path to the specified group list,
607330f729Sjoerg /// without performing any sysroot remapping.
617330f729Sjoerg /// Returns true if the path exists, false if it was ignored.
627330f729Sjoerg bool AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
637330f729Sjoerg bool isFramework);
647330f729Sjoerg
657330f729Sjoerg /// AddSystemHeaderPrefix - Add the specified prefix to the system header
667330f729Sjoerg /// prefix list.
AddSystemHeaderPrefix(StringRef Prefix,bool IsSystemHeader)677330f729Sjoerg void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
68*e038c9c4Sjoerg SystemHeaderPrefixes.emplace_back(std::string(Prefix), IsSystemHeader);
697330f729Sjoerg }
707330f729Sjoerg
717330f729Sjoerg /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
727330f729Sjoerg /// libstdc++.
737330f729Sjoerg /// Returns true if the \p Base path was found, false if it does not exist.
747330f729Sjoerg bool AddGnuCPlusPlusIncludePaths(StringRef Base, StringRef ArchDir,
757330f729Sjoerg StringRef Dir32, StringRef Dir64,
767330f729Sjoerg const llvm::Triple &triple);
777330f729Sjoerg
787330f729Sjoerg /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
797330f729Sjoerg /// libstdc++.
807330f729Sjoerg void AddMinGWCPlusPlusIncludePaths(StringRef Base,
817330f729Sjoerg StringRef Arch,
827330f729Sjoerg StringRef Version);
837330f729Sjoerg
847330f729Sjoerg // AddDefaultCIncludePaths - Add paths that should always be searched.
857330f729Sjoerg void AddDefaultCIncludePaths(const llvm::Triple &triple,
867330f729Sjoerg const HeaderSearchOptions &HSOpts);
877330f729Sjoerg
887330f729Sjoerg // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
897330f729Sjoerg // compiling c++.
907330f729Sjoerg void AddDefaultCPlusPlusIncludePaths(const LangOptions &LangOpts,
917330f729Sjoerg const llvm::Triple &triple,
927330f729Sjoerg const HeaderSearchOptions &HSOpts);
937330f729Sjoerg
947330f729Sjoerg /// AddDefaultSystemIncludePaths - Adds the default system include paths so
957330f729Sjoerg /// that e.g. stdio.h is found.
967330f729Sjoerg void AddDefaultIncludePaths(const LangOptions &Lang,
977330f729Sjoerg const llvm::Triple &triple,
987330f729Sjoerg const HeaderSearchOptions &HSOpts);
997330f729Sjoerg
1007330f729Sjoerg /// Realize - Merges all search path lists into one list and send it to
1017330f729Sjoerg /// HeaderSearch.
1027330f729Sjoerg void Realize(const LangOptions &Lang);
1037330f729Sjoerg };
1047330f729Sjoerg
1057330f729Sjoerg } // end anonymous namespace.
1067330f729Sjoerg
CanPrefixSysroot(StringRef Path)1077330f729Sjoerg static bool CanPrefixSysroot(StringRef Path) {
1087330f729Sjoerg #if defined(_WIN32)
1097330f729Sjoerg return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
1107330f729Sjoerg #else
1117330f729Sjoerg return llvm::sys::path::is_absolute(Path);
1127330f729Sjoerg #endif
1137330f729Sjoerg }
1147330f729Sjoerg
AddPath(const Twine & Path,IncludeDirGroup Group,bool isFramework)1157330f729Sjoerg bool InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group,
1167330f729Sjoerg bool isFramework) {
1177330f729Sjoerg // Add the path with sysroot prepended, if desired and this is a system header
1187330f729Sjoerg // group.
1197330f729Sjoerg if (HasSysroot) {
1207330f729Sjoerg SmallString<256> MappedPathStorage;
1217330f729Sjoerg StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
1227330f729Sjoerg if (CanPrefixSysroot(MappedPathStr)) {
1237330f729Sjoerg return AddUnmappedPath(IncludeSysroot + Path, Group, isFramework);
1247330f729Sjoerg }
1257330f729Sjoerg }
1267330f729Sjoerg
1277330f729Sjoerg return AddUnmappedPath(Path, Group, isFramework);
1287330f729Sjoerg }
1297330f729Sjoerg
AddUnmappedPath(const Twine & Path,IncludeDirGroup Group,bool isFramework)1307330f729Sjoerg bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
1317330f729Sjoerg bool isFramework) {
1327330f729Sjoerg assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
1337330f729Sjoerg
1347330f729Sjoerg FileManager &FM = Headers.getFileMgr();
1357330f729Sjoerg SmallString<256> MappedPathStorage;
1367330f729Sjoerg StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
1377330f729Sjoerg
1387330f729Sjoerg // If use system headers while cross-compiling, emit the warning.
1397330f729Sjoerg if (HasSysroot && (MappedPathStr.startswith("/usr/include") ||
1407330f729Sjoerg MappedPathStr.startswith("/usr/local/include"))) {
1417330f729Sjoerg Headers.getDiags().Report(diag::warn_poison_system_directories)
1427330f729Sjoerg << MappedPathStr;
1437330f729Sjoerg }
1447330f729Sjoerg
1457330f729Sjoerg // Compute the DirectoryLookup type.
1467330f729Sjoerg SrcMgr::CharacteristicKind Type;
1477330f729Sjoerg if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
1487330f729Sjoerg Type = SrcMgr::C_User;
1497330f729Sjoerg } else if (Group == ExternCSystem) {
1507330f729Sjoerg Type = SrcMgr::C_ExternCSystem;
1517330f729Sjoerg } else {
1527330f729Sjoerg Type = SrcMgr::C_System;
1537330f729Sjoerg }
1547330f729Sjoerg
1557330f729Sjoerg // If the directory exists, add it.
1567330f729Sjoerg if (auto DE = FM.getOptionalDirectoryRef(MappedPathStr)) {
1577330f729Sjoerg IncludePath.push_back(
1587330f729Sjoerg std::make_pair(Group, DirectoryLookup(*DE, Type, isFramework)));
1597330f729Sjoerg return true;
1607330f729Sjoerg }
1617330f729Sjoerg
1627330f729Sjoerg // Check to see if this is an apple-style headermap (which are not allowed to
1637330f729Sjoerg // be frameworks).
1647330f729Sjoerg if (!isFramework) {
1657330f729Sjoerg if (auto FE = FM.getFile(MappedPathStr)) {
1667330f729Sjoerg if (const HeaderMap *HM = Headers.CreateHeaderMap(*FE)) {
1677330f729Sjoerg // It is a headermap, add it to the search path.
1687330f729Sjoerg IncludePath.push_back(
1697330f729Sjoerg std::make_pair(Group,
1707330f729Sjoerg DirectoryLookup(HM, Type, Group == IndexHeaderMap)));
1717330f729Sjoerg return true;
1727330f729Sjoerg }
1737330f729Sjoerg }
1747330f729Sjoerg }
1757330f729Sjoerg
1767330f729Sjoerg if (Verbose)
1777330f729Sjoerg llvm::errs() << "ignoring nonexistent directory \""
1787330f729Sjoerg << MappedPathStr << "\"\n";
1797330f729Sjoerg return false;
1807330f729Sjoerg }
1817330f729Sjoerg
AddGnuCPlusPlusIncludePaths(StringRef Base,StringRef ArchDir,StringRef Dir32,StringRef Dir64,const llvm::Triple & triple)1827330f729Sjoerg bool InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
1837330f729Sjoerg StringRef ArchDir,
1847330f729Sjoerg StringRef Dir32,
1857330f729Sjoerg StringRef Dir64,
1867330f729Sjoerg const llvm::Triple &triple) {
1877330f729Sjoerg // Add the base dir
1887330f729Sjoerg bool IsBaseFound = AddPath(Base, CXXSystem, false);
1897330f729Sjoerg
1907330f729Sjoerg // Add the multilib dirs
1917330f729Sjoerg llvm::Triple::ArchType arch = triple.getArch();
1927330f729Sjoerg bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
1937330f729Sjoerg if (is64bit)
1947330f729Sjoerg AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false);
1957330f729Sjoerg else
1967330f729Sjoerg AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false);
1977330f729Sjoerg
1987330f729Sjoerg // Add the backward dir
1997330f729Sjoerg AddPath(Base + "/backward", CXXSystem, false);
2007330f729Sjoerg return IsBaseFound;
2017330f729Sjoerg }
2027330f729Sjoerg
AddMinGWCPlusPlusIncludePaths(StringRef Base,StringRef Arch,StringRef Version)2037330f729Sjoerg void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
2047330f729Sjoerg StringRef Arch,
2057330f729Sjoerg StringRef Version) {
2067330f729Sjoerg AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
2077330f729Sjoerg CXXSystem, false);
2087330f729Sjoerg AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
2097330f729Sjoerg CXXSystem, false);
2107330f729Sjoerg AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
2117330f729Sjoerg CXXSystem, false);
2127330f729Sjoerg }
2137330f729Sjoerg
AddDefaultCIncludePaths(const llvm::Triple & triple,const HeaderSearchOptions & HSOpts)2147330f729Sjoerg void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
2157330f729Sjoerg const HeaderSearchOptions &HSOpts) {
2167330f729Sjoerg llvm::Triple::OSType os = triple.getOS();
2177330f729Sjoerg
2187330f729Sjoerg if (triple.isOSDarwin()) {
2197330f729Sjoerg llvm_unreachable("Include management is handled in the driver.");
2207330f729Sjoerg }
2217330f729Sjoerg
2227330f729Sjoerg if (HSOpts.UseStandardSystemIncludes) {
2237330f729Sjoerg switch (os) {
2247330f729Sjoerg case llvm::Triple::CloudABI:
2257330f729Sjoerg case llvm::Triple::FreeBSD:
2267330f729Sjoerg case llvm::Triple::NetBSD:
2277330f729Sjoerg case llvm::Triple::OpenBSD:
2287330f729Sjoerg case llvm::Triple::NaCl:
2297330f729Sjoerg case llvm::Triple::PS4:
2307330f729Sjoerg case llvm::Triple::ELFIAMCU:
2317330f729Sjoerg case llvm::Triple::Fuchsia:
2327330f729Sjoerg break;
2337330f729Sjoerg case llvm::Triple::Win32:
2347330f729Sjoerg if (triple.getEnvironment() != llvm::Triple::Cygnus)
2357330f729Sjoerg break;
2367330f729Sjoerg LLVM_FALLTHROUGH;
2377330f729Sjoerg default:
2387330f729Sjoerg // FIXME: temporary hack: hard-coded paths.
2397330f729Sjoerg AddPath("/usr/local/include", System, false);
2407330f729Sjoerg break;
2417330f729Sjoerg }
2427330f729Sjoerg }
2437330f729Sjoerg
2447330f729Sjoerg // Builtin includes use #include_next directives and should be positioned
2457330f729Sjoerg // just prior C include dirs.
2467330f729Sjoerg if (HSOpts.UseBuiltinIncludes) {
2477330f729Sjoerg // Ignore the sys root, we *always* look for clang headers relative to
2487330f729Sjoerg // supplied path.
2497330f729Sjoerg SmallString<128> P = StringRef(HSOpts.ResourceDir);
2507330f729Sjoerg llvm::sys::path::append(P, "include");
2517330f729Sjoerg AddUnmappedPath(P, ExternCSystem, false);
2527330f729Sjoerg }
2537330f729Sjoerg
2547330f729Sjoerg // All remaining additions are for system include directories, early exit if
2557330f729Sjoerg // we aren't using them.
2567330f729Sjoerg if (!HSOpts.UseStandardSystemIncludes)
2577330f729Sjoerg return;
2587330f729Sjoerg
2597330f729Sjoerg // Add dirs specified via 'configure --with-c-include-dirs'.
2607330f729Sjoerg StringRef CIncludeDirs(C_INCLUDE_DIRS);
2617330f729Sjoerg if (CIncludeDirs != "") {
2627330f729Sjoerg SmallVector<StringRef, 5> dirs;
2637330f729Sjoerg CIncludeDirs.split(dirs, ":");
2647330f729Sjoerg for (StringRef dir : dirs)
2657330f729Sjoerg AddPath(dir, ExternCSystem, false);
2667330f729Sjoerg return;
2677330f729Sjoerg }
2687330f729Sjoerg
2697330f729Sjoerg switch (os) {
2707330f729Sjoerg case llvm::Triple::Linux:
2717330f729Sjoerg case llvm::Triple::Hurd:
2727330f729Sjoerg case llvm::Triple::Solaris:
273*e038c9c4Sjoerg case llvm::Triple::OpenBSD:
2747330f729Sjoerg llvm_unreachable("Include management is handled in the driver.");
2757330f729Sjoerg
2767330f729Sjoerg case llvm::Triple::CloudABI: {
2777330f729Sjoerg // <sysroot>/<triple>/include
2787330f729Sjoerg SmallString<128> P = StringRef(HSOpts.ResourceDir);
2797330f729Sjoerg llvm::sys::path::append(P, "../../..", triple.str(), "include");
2807330f729Sjoerg AddPath(P, System, false);
2817330f729Sjoerg break;
2827330f729Sjoerg }
2837330f729Sjoerg
2847330f729Sjoerg case llvm::Triple::Haiku:
2857330f729Sjoerg AddPath("/boot/system/non-packaged/develop/headers", System, false);
2867330f729Sjoerg AddPath("/boot/system/develop/headers/os", System, false);
2877330f729Sjoerg AddPath("/boot/system/develop/headers/os/app", System, false);
2887330f729Sjoerg AddPath("/boot/system/develop/headers/os/arch", System, false);
2897330f729Sjoerg AddPath("/boot/system/develop/headers/os/device", System, false);
2907330f729Sjoerg AddPath("/boot/system/develop/headers/os/drivers", System, false);
2917330f729Sjoerg AddPath("/boot/system/develop/headers/os/game", System, false);
2927330f729Sjoerg AddPath("/boot/system/develop/headers/os/interface", System, false);
2937330f729Sjoerg AddPath("/boot/system/develop/headers/os/kernel", System, false);
2947330f729Sjoerg AddPath("/boot/system/develop/headers/os/locale", System, false);
2957330f729Sjoerg AddPath("/boot/system/develop/headers/os/mail", System, false);
2967330f729Sjoerg AddPath("/boot/system/develop/headers/os/media", System, false);
2977330f729Sjoerg AddPath("/boot/system/develop/headers/os/midi", System, false);
2987330f729Sjoerg AddPath("/boot/system/develop/headers/os/midi2", System, false);
2997330f729Sjoerg AddPath("/boot/system/develop/headers/os/net", System, false);
3007330f729Sjoerg AddPath("/boot/system/develop/headers/os/opengl", System, false);
3017330f729Sjoerg AddPath("/boot/system/develop/headers/os/storage", System, false);
3027330f729Sjoerg AddPath("/boot/system/develop/headers/os/support", System, false);
3037330f729Sjoerg AddPath("/boot/system/develop/headers/os/translation", System, false);
3047330f729Sjoerg AddPath("/boot/system/develop/headers/os/add-ons/graphics", System, false);
3057330f729Sjoerg AddPath("/boot/system/develop/headers/os/add-ons/input_server", System, false);
3067330f729Sjoerg AddPath("/boot/system/develop/headers/os/add-ons/mail_daemon", System, false);
3077330f729Sjoerg AddPath("/boot/system/develop/headers/os/add-ons/registrar", System, false);
3087330f729Sjoerg AddPath("/boot/system/develop/headers/os/add-ons/screen_saver", System, false);
3097330f729Sjoerg AddPath("/boot/system/develop/headers/os/add-ons/tracker", System, false);
3107330f729Sjoerg AddPath("/boot/system/develop/headers/os/be_apps/Deskbar", System, false);
3117330f729Sjoerg AddPath("/boot/system/develop/headers/os/be_apps/NetPositive", System, false);
3127330f729Sjoerg AddPath("/boot/system/develop/headers/os/be_apps/Tracker", System, false);
3137330f729Sjoerg AddPath("/boot/system/develop/headers/3rdparty", System, false);
3147330f729Sjoerg AddPath("/boot/system/develop/headers/bsd", System, false);
3157330f729Sjoerg AddPath("/boot/system/develop/headers/glibc", System, false);
3167330f729Sjoerg AddPath("/boot/system/develop/headers/posix", System, false);
3177330f729Sjoerg AddPath("/boot/system/develop/headers", System, false);
3187330f729Sjoerg break;
3197330f729Sjoerg case llvm::Triple::RTEMS:
3207330f729Sjoerg break;
3217330f729Sjoerg case llvm::Triple::Win32:
3227330f729Sjoerg switch (triple.getEnvironment()) {
3237330f729Sjoerg default: llvm_unreachable("Include management is handled in the driver.");
3247330f729Sjoerg case llvm::Triple::Cygnus:
3257330f729Sjoerg AddPath("/usr/include/w32api", System, false);
3267330f729Sjoerg break;
3277330f729Sjoerg case llvm::Triple::GNU:
3287330f729Sjoerg break;
3297330f729Sjoerg }
3307330f729Sjoerg break;
3317330f729Sjoerg default:
3327330f729Sjoerg break;
3337330f729Sjoerg }
3347330f729Sjoerg
3357330f729Sjoerg switch (os) {
3367330f729Sjoerg case llvm::Triple::CloudABI:
3377330f729Sjoerg case llvm::Triple::RTEMS:
3387330f729Sjoerg case llvm::Triple::NaCl:
3397330f729Sjoerg case llvm::Triple::ELFIAMCU:
3407330f729Sjoerg case llvm::Triple::Fuchsia:
3417330f729Sjoerg break;
3427330f729Sjoerg case llvm::Triple::PS4: {
3437330f729Sjoerg // <isysroot> gets prepended later in AddPath().
3447330f729Sjoerg std::string BaseSDKPath = "";
3457330f729Sjoerg if (!HasSysroot) {
3467330f729Sjoerg const char *envValue = getenv("SCE_ORBIS_SDK_DIR");
3477330f729Sjoerg if (envValue)
3487330f729Sjoerg BaseSDKPath = envValue;
3497330f729Sjoerg else {
3507330f729Sjoerg // HSOpts.ResourceDir variable contains the location of Clang's
3517330f729Sjoerg // resource files.
3527330f729Sjoerg // Assuming that Clang is configured for PS4 without
3537330f729Sjoerg // --with-clang-resource-dir option, the location of Clang's resource
3547330f729Sjoerg // files is <SDK_DIR>/host_tools/lib/clang
3557330f729Sjoerg SmallString<128> P = StringRef(HSOpts.ResourceDir);
3567330f729Sjoerg llvm::sys::path::append(P, "../../..");
357*e038c9c4Sjoerg BaseSDKPath = std::string(P.str());
3587330f729Sjoerg }
3597330f729Sjoerg }
3607330f729Sjoerg AddPath(BaseSDKPath + "/target/include", System, false);
3617330f729Sjoerg if (triple.isPS4CPU())
3627330f729Sjoerg AddPath(BaseSDKPath + "/target/include_common", System, false);
3637330f729Sjoerg LLVM_FALLTHROUGH;
3647330f729Sjoerg }
3657330f729Sjoerg default:
3667330f729Sjoerg AddPath("/usr/include", ExternCSystem, false);
3677330f729Sjoerg break;
3687330f729Sjoerg }
3697330f729Sjoerg }
3707330f729Sjoerg
AddDefaultCPlusPlusIncludePaths(const LangOptions & LangOpts,const llvm::Triple & triple,const HeaderSearchOptions & HSOpts)3717330f729Sjoerg void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(
3727330f729Sjoerg const LangOptions &LangOpts, const llvm::Triple &triple,
3737330f729Sjoerg const HeaderSearchOptions &HSOpts) {
3747330f729Sjoerg llvm::Triple::OSType os = triple.getOS();
3757330f729Sjoerg // FIXME: temporary hack: hard-coded paths.
3767330f729Sjoerg
3777330f729Sjoerg if (triple.isOSDarwin()) {
3787330f729Sjoerg llvm_unreachable("Include management is handled in the driver.");
3797330f729Sjoerg }
3807330f729Sjoerg
3817330f729Sjoerg switch (os) {
3827330f729Sjoerg case llvm::Triple::Linux:
3837330f729Sjoerg case llvm::Triple::Hurd:
3847330f729Sjoerg case llvm::Triple::Solaris:
385*e038c9c4Sjoerg case llvm::Triple::AIX:
3867330f729Sjoerg llvm_unreachable("Include management is handled in the driver.");
3877330f729Sjoerg break;
3887330f729Sjoerg case llvm::Triple::Win32:
3897330f729Sjoerg switch (triple.getEnvironment()) {
3907330f729Sjoerg default: llvm_unreachable("Include management is handled in the driver.");
3917330f729Sjoerg case llvm::Triple::Cygnus:
3927330f729Sjoerg // Cygwin-1.7
3937330f729Sjoerg AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3");
3947330f729Sjoerg AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3");
3957330f729Sjoerg AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
3967330f729Sjoerg // g++-4 / Cygwin-1.5
3977330f729Sjoerg AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
3987330f729Sjoerg break;
3997330f729Sjoerg }
4007330f729Sjoerg break;
4017330f729Sjoerg case llvm::Triple::DragonFly:
4027330f729Sjoerg AddPath("/usr/include/c++/5.0", CXXSystem, false);
4037330f729Sjoerg break;
4047330f729Sjoerg case llvm::Triple::Minix:
4057330f729Sjoerg AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
4067330f729Sjoerg "", "", "", triple);
4077330f729Sjoerg break;
4087330f729Sjoerg default:
4097330f729Sjoerg break;
4107330f729Sjoerg }
4117330f729Sjoerg }
4127330f729Sjoerg
AddDefaultIncludePaths(const LangOptions & Lang,const llvm::Triple & triple,const HeaderSearchOptions & HSOpts)4137330f729Sjoerg void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
4147330f729Sjoerg const llvm::Triple &triple,
4157330f729Sjoerg const HeaderSearchOptions &HSOpts) {
4167330f729Sjoerg // NB: This code path is going away. All of the logic is moving into the
4177330f729Sjoerg // driver which has the information necessary to do target-specific
4187330f729Sjoerg // selections of default include paths. Each target which moves there will be
4197330f729Sjoerg // exempted from this logic here until we can delete the entire pile of code.
4207330f729Sjoerg switch (triple.getOS()) {
4217330f729Sjoerg default:
4227330f729Sjoerg break; // Everything else continues to use this routine's logic.
4237330f729Sjoerg
4247330f729Sjoerg case llvm::Triple::Emscripten:
4257330f729Sjoerg case llvm::Triple::Linux:
4267330f729Sjoerg case llvm::Triple::Hurd:
427*e038c9c4Sjoerg case llvm::Triple::OpenBSD:
4287330f729Sjoerg case llvm::Triple::Solaris:
4297330f729Sjoerg case llvm::Triple::WASI:
430*e038c9c4Sjoerg case llvm::Triple::AIX:
4317330f729Sjoerg return;
4327330f729Sjoerg
4337330f729Sjoerg case llvm::Triple::Win32:
4347330f729Sjoerg if (triple.getEnvironment() != llvm::Triple::Cygnus ||
4357330f729Sjoerg triple.isOSBinFormatMachO())
4367330f729Sjoerg return;
4377330f729Sjoerg break;
4387330f729Sjoerg
4397330f729Sjoerg case llvm::Triple::UnknownOS:
440*e038c9c4Sjoerg if (triple.isWasm())
4417330f729Sjoerg return;
4427330f729Sjoerg break;
4437330f729Sjoerg }
4447330f729Sjoerg
4457330f729Sjoerg // All header search logic is handled in the Driver for Darwin.
4467330f729Sjoerg if (triple.isOSDarwin()) {
4477330f729Sjoerg if (HSOpts.UseStandardSystemIncludes) {
4487330f729Sjoerg // Add the default framework include paths on Darwin.
4497330f729Sjoerg AddPath("/System/Library/Frameworks", System, true);
4507330f729Sjoerg AddPath("/Library/Frameworks", System, true);
4517330f729Sjoerg }
4527330f729Sjoerg return;
4537330f729Sjoerg }
4547330f729Sjoerg
4557330f729Sjoerg if (Lang.CPlusPlus && !Lang.AsmPreprocessor &&
4567330f729Sjoerg HSOpts.UseStandardCXXIncludes && HSOpts.UseStandardSystemIncludes) {
4577330f729Sjoerg if (HSOpts.UseLibcxx) {
4587330f729Sjoerg AddPath("/usr/include/c++/v1", CXXSystem, false);
4597330f729Sjoerg } else {
4607330f729Sjoerg AddDefaultCPlusPlusIncludePaths(Lang, triple, HSOpts);
4617330f729Sjoerg }
4627330f729Sjoerg }
4637330f729Sjoerg
4647330f729Sjoerg AddDefaultCIncludePaths(triple, HSOpts);
4657330f729Sjoerg }
4667330f729Sjoerg
4677330f729Sjoerg /// RemoveDuplicates - If there are duplicate directory entries in the specified
4687330f729Sjoerg /// search list, remove the later (dead) ones. Returns the number of non-system
4697330f729Sjoerg /// headers removed, which is used to update NumAngled.
RemoveDuplicates(std::vector<DirectoryLookup> & SearchList,unsigned First,bool Verbose)4707330f729Sjoerg static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
4717330f729Sjoerg unsigned First, bool Verbose) {
4727330f729Sjoerg llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
4737330f729Sjoerg llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
4747330f729Sjoerg llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
4757330f729Sjoerg unsigned NonSystemRemoved = 0;
4767330f729Sjoerg for (unsigned i = First; i != SearchList.size(); ++i) {
4777330f729Sjoerg unsigned DirToRemove = i;
4787330f729Sjoerg
4797330f729Sjoerg const DirectoryLookup &CurEntry = SearchList[i];
4807330f729Sjoerg
4817330f729Sjoerg if (CurEntry.isNormalDir()) {
4827330f729Sjoerg // If this isn't the first time we've seen this dir, remove it.
4837330f729Sjoerg if (SeenDirs.insert(CurEntry.getDir()).second)
4847330f729Sjoerg continue;
4857330f729Sjoerg } else if (CurEntry.isFramework()) {
4867330f729Sjoerg // If this isn't the first time we've seen this framework dir, remove it.
4877330f729Sjoerg if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()).second)
4887330f729Sjoerg continue;
4897330f729Sjoerg } else {
4907330f729Sjoerg assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
4917330f729Sjoerg // If this isn't the first time we've seen this headermap, remove it.
4927330f729Sjoerg if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()).second)
4937330f729Sjoerg continue;
4947330f729Sjoerg }
4957330f729Sjoerg
4967330f729Sjoerg // If we have a normal #include dir/framework/headermap that is shadowed
4977330f729Sjoerg // later in the chain by a system include location, we actually want to
4987330f729Sjoerg // ignore the user's request and drop the user dir... keeping the system
4997330f729Sjoerg // dir. This is weird, but required to emulate GCC's search path correctly.
5007330f729Sjoerg //
5017330f729Sjoerg // Since dupes of system dirs are rare, just rescan to find the original
5027330f729Sjoerg // that we're nuking instead of using a DenseMap.
5037330f729Sjoerg if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
5047330f729Sjoerg // Find the dir that this is the same of.
5057330f729Sjoerg unsigned FirstDir;
5067330f729Sjoerg for (FirstDir = First;; ++FirstDir) {
5077330f729Sjoerg assert(FirstDir != i && "Didn't find dupe?");
5087330f729Sjoerg
5097330f729Sjoerg const DirectoryLookup &SearchEntry = SearchList[FirstDir];
5107330f729Sjoerg
5117330f729Sjoerg // If these are different lookup types, then they can't be the dupe.
5127330f729Sjoerg if (SearchEntry.getLookupType() != CurEntry.getLookupType())
5137330f729Sjoerg continue;
5147330f729Sjoerg
5157330f729Sjoerg bool isSame;
5167330f729Sjoerg if (CurEntry.isNormalDir())
5177330f729Sjoerg isSame = SearchEntry.getDir() == CurEntry.getDir();
5187330f729Sjoerg else if (CurEntry.isFramework())
5197330f729Sjoerg isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
5207330f729Sjoerg else {
5217330f729Sjoerg assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
5227330f729Sjoerg isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
5237330f729Sjoerg }
5247330f729Sjoerg
5257330f729Sjoerg if (isSame)
5267330f729Sjoerg break;
5277330f729Sjoerg }
5287330f729Sjoerg
5297330f729Sjoerg // If the first dir in the search path is a non-system dir, zap it
5307330f729Sjoerg // instead of the system one.
5317330f729Sjoerg if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
5327330f729Sjoerg DirToRemove = FirstDir;
5337330f729Sjoerg }
5347330f729Sjoerg
5357330f729Sjoerg if (Verbose) {
5367330f729Sjoerg llvm::errs() << "ignoring duplicate directory \""
5377330f729Sjoerg << CurEntry.getName() << "\"\n";
5387330f729Sjoerg if (DirToRemove != i)
5397330f729Sjoerg llvm::errs() << " as it is a non-system directory that duplicates "
5407330f729Sjoerg << "a system directory\n";
5417330f729Sjoerg }
5427330f729Sjoerg if (DirToRemove != i)
5437330f729Sjoerg ++NonSystemRemoved;
5447330f729Sjoerg
5457330f729Sjoerg // This is reached if the current entry is a duplicate. Remove the
5467330f729Sjoerg // DirToRemove (usually the current dir).
5477330f729Sjoerg SearchList.erase(SearchList.begin()+DirToRemove);
5487330f729Sjoerg --i;
5497330f729Sjoerg }
5507330f729Sjoerg return NonSystemRemoved;
5517330f729Sjoerg }
5527330f729Sjoerg
5537330f729Sjoerg
Realize(const LangOptions & Lang)5547330f729Sjoerg void InitHeaderSearch::Realize(const LangOptions &Lang) {
5557330f729Sjoerg // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
5567330f729Sjoerg std::vector<DirectoryLookup> SearchList;
5577330f729Sjoerg SearchList.reserve(IncludePath.size());
5587330f729Sjoerg
5597330f729Sjoerg // Quoted arguments go first.
5607330f729Sjoerg for (auto &Include : IncludePath)
5617330f729Sjoerg if (Include.first == Quoted)
5627330f729Sjoerg SearchList.push_back(Include.second);
5637330f729Sjoerg
5647330f729Sjoerg // Deduplicate and remember index.
5657330f729Sjoerg RemoveDuplicates(SearchList, 0, Verbose);
5667330f729Sjoerg unsigned NumQuoted = SearchList.size();
5677330f729Sjoerg
5687330f729Sjoerg for (auto &Include : IncludePath)
5697330f729Sjoerg if (Include.first == Angled || Include.first == IndexHeaderMap)
5707330f729Sjoerg SearchList.push_back(Include.second);
5717330f729Sjoerg
5727330f729Sjoerg RemoveDuplicates(SearchList, NumQuoted, Verbose);
5737330f729Sjoerg unsigned NumAngled = SearchList.size();
5747330f729Sjoerg
5757330f729Sjoerg for (auto &Include : IncludePath)
5767330f729Sjoerg if (Include.first == System || Include.first == ExternCSystem ||
5777330f729Sjoerg (!Lang.ObjC && !Lang.CPlusPlus && Include.first == CSystem) ||
5787330f729Sjoerg (/*FIXME !Lang.ObjC && */ Lang.CPlusPlus &&
5797330f729Sjoerg Include.first == CXXSystem) ||
5807330f729Sjoerg (Lang.ObjC && !Lang.CPlusPlus && Include.first == ObjCSystem) ||
5817330f729Sjoerg (Lang.ObjC && Lang.CPlusPlus && Include.first == ObjCXXSystem))
5827330f729Sjoerg SearchList.push_back(Include.second);
5837330f729Sjoerg
5847330f729Sjoerg for (auto &Include : IncludePath)
5857330f729Sjoerg if (Include.first == After)
5867330f729Sjoerg SearchList.push_back(Include.second);
5877330f729Sjoerg
5887330f729Sjoerg // Remove duplicates across both the Angled and System directories. GCC does
5897330f729Sjoerg // this and failing to remove duplicates across these two groups breaks
5907330f729Sjoerg // #include_next.
5917330f729Sjoerg unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
5927330f729Sjoerg NumAngled -= NonSystemRemoved;
5937330f729Sjoerg
5947330f729Sjoerg bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
5957330f729Sjoerg Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
5967330f729Sjoerg
5977330f729Sjoerg Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
5987330f729Sjoerg
5997330f729Sjoerg // If verbose, print the list of directories that will be searched.
6007330f729Sjoerg if (Verbose) {
6017330f729Sjoerg llvm::errs() << "#include \"...\" search starts here:\n";
6027330f729Sjoerg for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
6037330f729Sjoerg if (i == NumQuoted)
6047330f729Sjoerg llvm::errs() << "#include <...> search starts here:\n";
6057330f729Sjoerg StringRef Name = SearchList[i].getName();
6067330f729Sjoerg const char *Suffix;
6077330f729Sjoerg if (SearchList[i].isNormalDir())
6087330f729Sjoerg Suffix = "";
6097330f729Sjoerg else if (SearchList[i].isFramework())
6107330f729Sjoerg Suffix = " (framework directory)";
6117330f729Sjoerg else {
6127330f729Sjoerg assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
6137330f729Sjoerg Suffix = " (headermap)";
6147330f729Sjoerg }
6157330f729Sjoerg llvm::errs() << " " << Name << Suffix << "\n";
6167330f729Sjoerg }
6177330f729Sjoerg llvm::errs() << "End of search list.\n";
6187330f729Sjoerg }
6197330f729Sjoerg }
6207330f729Sjoerg
ApplyHeaderSearchOptions(HeaderSearch & HS,const HeaderSearchOptions & HSOpts,const LangOptions & Lang,const llvm::Triple & Triple)6217330f729Sjoerg void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
6227330f729Sjoerg const HeaderSearchOptions &HSOpts,
6237330f729Sjoerg const LangOptions &Lang,
6247330f729Sjoerg const llvm::Triple &Triple) {
6257330f729Sjoerg InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
6267330f729Sjoerg
6277330f729Sjoerg // Add the user defined entries.
6287330f729Sjoerg for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
6297330f729Sjoerg const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
6307330f729Sjoerg if (E.IgnoreSysRoot) {
6317330f729Sjoerg Init.AddUnmappedPath(E.Path, E.Group, E.IsFramework);
6327330f729Sjoerg } else {
6337330f729Sjoerg Init.AddPath(E.Path, E.Group, E.IsFramework);
6347330f729Sjoerg }
6357330f729Sjoerg }
6367330f729Sjoerg
6377330f729Sjoerg Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
6387330f729Sjoerg
6397330f729Sjoerg for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
6407330f729Sjoerg Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix,
6417330f729Sjoerg HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
6427330f729Sjoerg
6437330f729Sjoerg if (HSOpts.UseBuiltinIncludes) {
6447330f729Sjoerg // Set up the builtin include directory in the module map.
6457330f729Sjoerg SmallString<128> P = StringRef(HSOpts.ResourceDir);
6467330f729Sjoerg llvm::sys::path::append(P, "include");
6477330f729Sjoerg if (auto Dir = HS.getFileMgr().getDirectory(P))
6487330f729Sjoerg HS.getModuleMap().setBuiltinIncludeDir(*Dir);
6497330f729Sjoerg }
6507330f729Sjoerg
6517330f729Sjoerg Init.Realize(Lang);
6527330f729Sjoerg }
653