#include "clang/Basic/DiagnosticFrontend.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Config/config.h"
#include "clang/Lex/HeaderMap.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/HeaderSearchOptions.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace clang::frontend;
namespace {
struct DirectoryLookupInfo {
IncludeDirGroup Group;
DirectoryLookup Lookup;
Optional<unsigned> UserEntryIdx;
DirectoryLookupInfo(IncludeDirGroup Group, DirectoryLookup Lookup,
Optional<unsigned> UserEntryIdx)
: Group(Group), Lookup(Lookup), UserEntryIdx(UserEntryIdx) {}
};
class InitHeaderSearch {
std::vector<DirectoryLookupInfo> IncludePath;
std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
HeaderSearch &Headers;
bool Verbose;
std::string IncludeSysroot;
bool HasSysroot;
public:
InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
: Headers(HS), Verbose(verbose), IncludeSysroot(std::string(sysroot)),
HasSysroot(!(sysroot.empty() || sysroot == "/")) {}
bool AddPath(const Twine &Path, IncludeDirGroup Group, bool isFramework,
Optional<unsigned> UserEntryIdx = None);
bool AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
bool isFramework,
Optional<unsigned> UserEntryIdx = None);
void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
SystemHeaderPrefixes.emplace_back(std::string(Prefix), IsSystemHeader);
}
bool AddGnuCPlusPlusIncludePaths(StringRef Base, StringRef ArchDir,
StringRef Dir32, StringRef Dir64,
const llvm::Triple &triple);
void AddMinGWCPlusPlusIncludePaths(StringRef Base,
StringRef Arch,
StringRef Version);
void AddDefaultCIncludePaths(const llvm::Triple &triple,
const HeaderSearchOptions &HSOpts);
void AddDefaultCPlusPlusIncludePaths(const LangOptions &LangOpts,
const llvm::Triple &triple,
const HeaderSearchOptions &HSOpts);
void AddDefaultIncludePaths(const LangOptions &Lang,
const llvm::Triple &triple,
const HeaderSearchOptions &HSOpts);
void Realize(const LangOptions &Lang);
};
}
static bool CanPrefixSysroot(StringRef Path) {
#if defined(_WIN32)
return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
#else
return llvm::sys::path::is_absolute(Path);
#endif
}
bool InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group,
bool isFramework,
Optional<unsigned> UserEntryIdx) {
if (HasSysroot) {
SmallString<256> MappedPathStorage;
StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
if (CanPrefixSysroot(MappedPathStr)) {
return AddUnmappedPath(IncludeSysroot + Path, Group, isFramework,
UserEntryIdx);
}
}
return AddUnmappedPath(Path, Group, isFramework, UserEntryIdx);
}
bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
bool isFramework,
Optional<unsigned> UserEntryIdx) {
assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
FileManager &FM = Headers.getFileMgr();
SmallString<256> MappedPathStorage;
StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
if (HasSysroot && (MappedPathStr.startswith("/usr/include") ||
MappedPathStr.startswith("/usr/local/include"))) {
Headers.getDiags().Report(diag::warn_poison_system_directories)
<< MappedPathStr;
}
SrcMgr::CharacteristicKind Type;
if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
Type = SrcMgr::C_User;
} else if (Group == ExternCSystem) {
Type = SrcMgr::C_ExternCSystem;
} else {
Type = SrcMgr::C_System;
}
if (auto DE = FM.getOptionalDirectoryRef(MappedPathStr)) {
IncludePath.emplace_back(Group, DirectoryLookup(*DE, Type, isFramework),
UserEntryIdx);
return true;
}
if (!isFramework) {
if (auto FE = FM.getFile(MappedPathStr)) {
if (const HeaderMap *HM = Headers.CreateHeaderMap(*FE)) {
IncludePath.emplace_back(
Group, DirectoryLookup(HM, Type, Group == IndexHeaderMap),
UserEntryIdx);
return true;
}
}
}
if (Verbose)
llvm::errs() << "ignoring nonexistent directory \""
<< MappedPathStr << "\"\n";
return false;
}
bool InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
StringRef ArchDir,
StringRef Dir32,
StringRef Dir64,
const llvm::Triple &triple) {
bool IsBaseFound = AddPath(Base, CXXSystem, false);
llvm::Triple::ArchType arch = triple.getArch();
bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
if (is64bit)
AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false);
else
AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false);
AddPath(Base + "/backward", CXXSystem, false);
return IsBaseFound;
}
void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
StringRef Arch,
StringRef Version) {
AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
CXXSystem, false);
AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
CXXSystem, false);
AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
CXXSystem, false);
}
void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
const HeaderSearchOptions &HSOpts) {
llvm::Triple::OSType os = triple.getOS();
if (triple.isOSDarwin()) {
llvm_unreachable("Include management is handled in the driver.");
}
if (HSOpts.UseStandardSystemIncludes) {
switch (os) {
case llvm::Triple::CloudABI:
case llvm::Triple::FreeBSD:
case llvm::Triple::NetBSD:
case llvm::Triple::OpenBSD:
case llvm::Triple::NaCl:
case llvm::Triple::PS4:
case llvm::Triple::PS5:
case llvm::Triple::ELFIAMCU:
case llvm::Triple::Fuchsia:
break;
case llvm::Triple::Win32:
if (triple.getEnvironment() != llvm::Triple::Cygnus)
break;
LLVM_FALLTHROUGH;
default:
AddPath("/usr/local/include", System, false);
break;
}
}
if (HSOpts.UseBuiltinIncludes) {
SmallString<128> P = StringRef(HSOpts.ResourceDir);
llvm::sys::path::append(P, "include");
AddUnmappedPath(P, ExternCSystem, false);
}
if (!HSOpts.UseStandardSystemIncludes)
return;
StringRef CIncludeDirs(C_INCLUDE_DIRS);
if (CIncludeDirs != "") {
SmallVector<StringRef, 5> dirs;
CIncludeDirs.split(dirs, ":");
for (StringRef dir : dirs)
AddPath(dir, ExternCSystem, false);
return;
}
switch (os) {
case llvm::Triple::Linux:
case llvm::Triple::Hurd:
case llvm::Triple::Solaris:
case llvm::Triple::OpenBSD:
llvm_unreachable("Include management is handled in the driver.");
case llvm::Triple::CloudABI: {
SmallString<128> P = StringRef(HSOpts.ResourceDir);
llvm::sys::path::append(P, "../../..", triple.str(), "include");
AddPath(P, System, false);
break;
}
case llvm::Triple::Haiku:
AddPath("/boot/system/non-packaged/develop/headers", System, false);
AddPath("/boot/system/develop/headers/os", System, false);
AddPath("/boot/system/develop/headers/os/app", System, false);
AddPath("/boot/system/develop/headers/os/arch", System, false);
AddPath("/boot/system/develop/headers/os/device", System, false);
AddPath("/boot/system/develop/headers/os/drivers", System, false);
AddPath("/boot/system/develop/headers/os/game", System, false);
AddPath("/boot/system/develop/headers/os/interface", System, false);
AddPath("/boot/system/develop/headers/os/kernel", System, false);
AddPath("/boot/system/develop/headers/os/locale", System, false);
AddPath("/boot/system/develop/headers/os/mail", System, false);
AddPath("/boot/system/develop/headers/os/media", System, false);
AddPath("/boot/system/develop/headers/os/midi", System, false);
AddPath("/boot/system/develop/headers/os/midi2", System, false);
AddPath("/boot/system/develop/headers/os/net", System, false);
AddPath("/boot/system/develop/headers/os/opengl", System, false);
AddPath("/boot/system/develop/headers/os/storage", System, false);
AddPath("/boot/system/develop/headers/os/support", System, false);
AddPath("/boot/system/develop/headers/os/translation", System, false);
AddPath("/boot/system/develop/headers/os/add-ons/graphics", System, false);
AddPath("/boot/system/develop/headers/os/add-ons/input_server", System, false);
AddPath("/boot/system/develop/headers/os/add-ons/mail_daemon", System, false);
AddPath("/boot/system/develop/headers/os/add-ons/registrar", System, false);
AddPath("/boot/system/develop/headers/os/add-ons/screen_saver", System, false);
AddPath("/boot/system/develop/headers/os/add-ons/tracker", System, false);
AddPath("/boot/system/develop/headers/os/be_apps/Deskbar", System, false);
AddPath("/boot/system/develop/headers/os/be_apps/NetPositive", System, false);
AddPath("/boot/system/develop/headers/os/be_apps/Tracker", System, false);
AddPath("/boot/system/develop/headers/3rdparty", System, false);
AddPath("/boot/system/develop/headers/bsd", System, false);
AddPath("/boot/system/develop/headers/glibc", System, false);
AddPath("/boot/system/develop/headers/posix", System, false);
AddPath("/boot/system/develop/headers", System, false);
break;
case llvm::Triple::RTEMS:
break;
case llvm::Triple::Win32:
switch (triple.getEnvironment()) {
default: llvm_unreachable("Include management is handled in the driver.");
case llvm::Triple::Cygnus:
AddPath("/usr/include/w32api", System, false);
break;
case llvm::Triple::GNU:
break;
}
break;
default:
break;
}
switch (os) {
case llvm::Triple::CloudABI:
case llvm::Triple::RTEMS:
case llvm::Triple::NaCl:
case llvm::Triple::ELFIAMCU:
case llvm::Triple::Fuchsia:
break;
case llvm::Triple::PS4:
case llvm::Triple::PS5: {
std::string BaseSDKPath;
if (!HasSysroot) {
const char *EnvVar = (os == llvm::Triple::PS4) ? "SCE_ORBIS_SDK_DIR"
: "SCE_PROSPERO_SDK_DIR";
const char *envValue = getenv(EnvVar);
if (envValue)
BaseSDKPath = envValue;
else {
SmallString<128> P = StringRef(HSOpts.ResourceDir);
llvm::sys::path::append(P, "../../..");
BaseSDKPath = std::string(P.str());
}
}
AddPath(BaseSDKPath + "/target/include", System, false);
AddPath(BaseSDKPath + "/target/include_common", System, false);
break;
}
default:
AddPath("/usr/include", ExternCSystem, false);
break;
}
}
void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(
const LangOptions &LangOpts, const llvm::Triple &triple,
const HeaderSearchOptions &HSOpts) {
llvm::Triple::OSType os = triple.getOS();
if (triple.isOSDarwin()) {
llvm_unreachable("Include management is handled in the driver.");
}
switch (os) {
case llvm::Triple::Linux:
case llvm::Triple::Hurd:
case llvm::Triple::Solaris:
case llvm::Triple::AIX:
llvm_unreachable("Include management is handled in the driver.");
break;
case llvm::Triple::Win32:
switch (triple.getEnvironment()) {
default: llvm_unreachable("Include management is handled in the driver.");
case llvm::Triple::Cygnus:
AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3");
AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3");
AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
break;
}
break;
case llvm::Triple::DragonFly:
AddPath("/usr/include/c++/5.0", CXXSystem, false);
break;
case llvm::Triple::Minix:
AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
"", "", "", triple);
break;
default:
break;
}
}
void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
const llvm::Triple &triple,
const HeaderSearchOptions &HSOpts) {
switch (triple.getOS()) {
default:
break;
case llvm::Triple::Emscripten:
case llvm::Triple::Linux:
case llvm::Triple::Hurd:
case llvm::Triple::OpenBSD:
case llvm::Triple::Solaris:
case llvm::Triple::WASI:
case llvm::Triple::AIX:
return;
case llvm::Triple::Win32:
if (triple.getEnvironment() != llvm::Triple::Cygnus ||
triple.isOSBinFormatMachO())
return;
break;
case llvm::Triple::UnknownOS:
if (triple.isWasm())
return;
break;
}
if (triple.isOSDarwin()) {
if (HSOpts.UseStandardSystemIncludes) {
AddPath("/System/Library/Frameworks", System, true);
AddPath("/Library/Frameworks", System, true);
}
return;
}
if (Lang.CPlusPlus && !Lang.AsmPreprocessor &&
HSOpts.UseStandardCXXIncludes && HSOpts.UseStandardSystemIncludes) {
if (HSOpts.UseLibcxx) {
AddPath("/usr/include/c++/v1", CXXSystem, false);
} else {
AddDefaultCPlusPlusIncludePaths(Lang, triple, HSOpts);
}
}
AddDefaultCIncludePaths(triple, HSOpts);
}
static unsigned RemoveDuplicates(std::vector<DirectoryLookupInfo> &SearchList,
unsigned First, bool Verbose) {
llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
unsigned NonSystemRemoved = 0;
for (unsigned i = First; i != SearchList.size(); ++i) {
unsigned DirToRemove = i;
const DirectoryLookup &CurEntry = SearchList[i].Lookup;
if (CurEntry.isNormalDir()) {
if (SeenDirs.insert(CurEntry.getDir()).second)
continue;
} else if (CurEntry.isFramework()) {
if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()).second)
continue;
} else {
assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()).second)
continue;
}
if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
unsigned FirstDir;
for (FirstDir = First;; ++FirstDir) {
assert(FirstDir != i && "Didn't find dupe?");
const DirectoryLookup &SearchEntry = SearchList[FirstDir].Lookup;
if (SearchEntry.getLookupType() != CurEntry.getLookupType())
continue;
bool isSame;
if (CurEntry.isNormalDir())
isSame = SearchEntry.getDir() == CurEntry.getDir();
else if (CurEntry.isFramework())
isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
else {
assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
}
if (isSame)
break;
}
if (SearchList[FirstDir].Lookup.getDirCharacteristic() == SrcMgr::C_User)
DirToRemove = FirstDir;
}
if (Verbose) {
llvm::errs() << "ignoring duplicate directory \""
<< CurEntry.getName() << "\"\n";
if (DirToRemove != i)
llvm::errs() << " as it is a non-system directory that duplicates "
<< "a system directory\n";
}
if (DirToRemove != i)
++NonSystemRemoved;
SearchList.erase(SearchList.begin()+DirToRemove);
--i;
}
return NonSystemRemoved;
}
static std::vector<DirectoryLookup>
extractLookups(const std::vector<DirectoryLookupInfo> &Infos) {
std::vector<DirectoryLookup> Lookups;
Lookups.reserve(Infos.size());
llvm::transform(Infos, std::back_inserter(Lookups),
[](const DirectoryLookupInfo &Info) { return Info.Lookup; });
return Lookups;
}
static llvm::DenseMap<unsigned, unsigned>
mapToUserEntries(const std::vector<DirectoryLookupInfo> &Infos) {
llvm::DenseMap<unsigned, unsigned> LookupsToUserEntries;
for (unsigned I = 0, E = Infos.size(); I < E; ++I) {
if (Infos[I].UserEntryIdx)
LookupsToUserEntries.insert({I, *Infos[I].UserEntryIdx});
}
return LookupsToUserEntries;
}
void InitHeaderSearch::Realize(const LangOptions &Lang) {
std::vector<DirectoryLookupInfo> SearchList;
SearchList.reserve(IncludePath.size());
for (auto &Include : IncludePath)
if (Include.Group == Quoted)
SearchList.push_back(Include);
RemoveDuplicates(SearchList, 0, Verbose);
unsigned NumQuoted = SearchList.size();
for (auto &Include : IncludePath)
if (Include.Group == Angled || Include.Group == IndexHeaderMap)
SearchList.push_back(Include);
RemoveDuplicates(SearchList, NumQuoted, Verbose);
unsigned NumAngled = SearchList.size();
for (auto &Include : IncludePath)
if (Include.Group == System || Include.Group == ExternCSystem ||
(!Lang.ObjC && !Lang.CPlusPlus && Include.Group == CSystem) ||
( Lang.CPlusPlus &&
Include.Group == CXXSystem) ||
(Lang.ObjC && !Lang.CPlusPlus && Include.Group == ObjCSystem) ||
(Lang.ObjC && Lang.CPlusPlus && Include.Group == ObjCXXSystem))
SearchList.push_back(Include);
for (auto &Include : IncludePath)
if (Include.Group == After)
SearchList.push_back(Include);
unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
NumAngled -= NonSystemRemoved;
bool DontSearchCurDir = false; Headers.SetSearchPaths(extractLookups(SearchList), NumQuoted, NumAngled,
DontSearchCurDir, mapToUserEntries(SearchList));
Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
if (Verbose) {
llvm::errs() << "#include \"...\" search starts here:\n";
for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
if (i == NumQuoted)
llvm::errs() << "#include <...> search starts here:\n";
StringRef Name = SearchList[i].Lookup.getName();
const char *Suffix;
if (SearchList[i].Lookup.isNormalDir())
Suffix = "";
else if (SearchList[i].Lookup.isFramework())
Suffix = " (framework directory)";
else {
assert(SearchList[i].Lookup.isHeaderMap() && "Unknown DirectoryLookup");
Suffix = " (headermap)";
}
llvm::errs() << " " << Name << Suffix << "\n";
}
llvm::errs() << "End of search list.\n";
}
}
void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
const HeaderSearchOptions &HSOpts,
const LangOptions &Lang,
const llvm::Triple &Triple) {
InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
if (E.IgnoreSysRoot) {
Init.AddUnmappedPath(E.Path, E.Group, E.IsFramework, i);
} else {
Init.AddPath(E.Path, E.Group, E.IsFramework, i);
}
}
Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix,
HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
if (HSOpts.UseBuiltinIncludes) {
SmallString<128> P = StringRef(HSOpts.ResourceDir);
llvm::sys::path::append(P, "include");
if (auto Dir = HS.getFileMgr().getDirectory(P))
HS.getModuleMap().setBuiltinIncludeDir(*Dir);
}
Init.Realize(Lang);
}