| 1 |
//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// This file implements the InitHeaderSearch class. |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#include "clang/Basic/DiagnosticFrontend.h" |
| 14 |
#include "clang/Basic/FileManager.h" |
| 15 |
#include "clang/Basic/LangOptions.h" |
| 16 |
#include "clang/Config/config.h" // C_INCLUDE_DIRS |
| 17 |
#include "clang/Lex/HeaderMap.h" |
| 18 |
#include "clang/Lex/HeaderSearch.h" |
| 19 |
#include "clang/Lex/HeaderSearchOptions.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/Triple.h" |
| 25 |
#include "llvm/ADT/Twine.h" |
| 26 |
#include "llvm/Support/ErrorHandling.h" |
| 27 |
#include "llvm/Support/Path.h" |
| 28 |
#include "llvm/Support/raw_ostream.h" |
| 29 |
#include <optional> |
| 30 |
|
| 31 |
using namespace clang; |
| 32 |
using namespace clang::frontend; |
| 33 |
|
| 34 |
namespace { |
| 35 |
/// Holds information about a single DirectoryLookup object. |
| 36 |
struct DirectoryLookupInfo { |
| 37 |
IncludeDirGroup Group; |
| 38 |
DirectoryLookup Lookup; |
| 39 |
std::optional<unsigned> UserEntryIdx; |
| 40 |
|
| 41 |
DirectoryLookupInfo(IncludeDirGroup Group, DirectoryLookup Lookup, |
| 42 |
std::optional<unsigned> UserEntryIdx) |
| 43 |
: Group(Group), Lookup(Lookup), UserEntryIdx(UserEntryIdx) {} |
| 44 |
}; |
| 45 |
|
| 46 |
/// This class makes it easier to set the search paths of a HeaderSearch object. |
| 47 |
/// InitHeaderSearch stores several search path lists internally, which can be |
| 48 |
/// sent to a HeaderSearch object in one swoop. |
| 49 |
class InitHeaderSearch { |
| 50 |
std::vector<DirectoryLookupInfo> IncludePath; |
| 51 |
std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes; |
| 52 |
HeaderSearch &Headers; |
| 53 |
bool Verbose; |
| 54 |
std::string IncludeSysroot; |
| 55 |
bool HasSysroot; |
| 56 |
|
| 57 |
public: |
| 58 |
InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot) |
| 59 |
: Headers(HS), Verbose(verbose), IncludeSysroot(std::string(sysroot)), |
| 60 |
HasSysroot(!(sysroot.empty() || sysroot == "/")) {} |
| 61 |
|
| 62 |
/// Add the specified path to the specified group list, prefixing the sysroot |
| 63 |
/// if used. |
| 64 |
/// Returns true if the path exists, false if it was ignored. |
| 65 |
bool AddPath(const Twine &Path, IncludeDirGroup Group, bool isFramework, |
| 66 |
std::optional<unsigned> UserEntryIdx = std::nullopt); |
| 67 |
|
| 68 |
/// Add the specified path to the specified group list, without performing any |
| 69 |
/// sysroot remapping. |
| 70 |
/// Returns true if the path exists, false if it was ignored. |
| 71 |
bool AddUnmappedPath(const Twine &Path, IncludeDirGroup Group, |
| 72 |
bool isFramework, |
| 73 |
std::optional<unsigned> UserEntryIdx = std::nullopt); |
| 74 |
|
| 75 |
/// Add the specified prefix to the system header prefix list. |
| 76 |
void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) { |
| 77 |
SystemHeaderPrefixes.emplace_back(std::string(Prefix), IsSystemHeader); |
| 78 |
} |
| 79 |
|
| 80 |
/// Add the necessary paths to support a gnu libstdc++. |
| 81 |
/// Returns true if the \p Base path was found, false if it does not exist. |
| 82 |
bool AddGnuCPlusPlusIncludePaths(StringRef Base, StringRef ArchDir, |
| 83 |
StringRef Dir32, StringRef Dir64, |
| 84 |
const llvm::Triple &triple); |
| 85 |
|
| 86 |
/// Add the necessary paths to support a MinGW libstdc++. |
| 87 |
void AddMinGWCPlusPlusIncludePaths(StringRef Base, |
| 88 |
StringRef Arch, |
| 89 |
StringRef Version); |
| 90 |
|
| 91 |
/// Add paths that should always be searched. |
| 92 |
void AddDefaultCIncludePaths(const llvm::Triple &triple, |
| 93 |
const HeaderSearchOptions &HSOpts); |
| 94 |
|
| 95 |
/// Add paths that should be searched when compiling c++. |
| 96 |
void AddDefaultCPlusPlusIncludePaths(const LangOptions &LangOpts, |
| 97 |
const llvm::Triple &triple, |
| 98 |
const HeaderSearchOptions &HSOpts); |
| 99 |
|
| 100 |
/// Returns true iff AddDefaultIncludePaths should do anything. If this |
| 101 |
/// returns false, include paths should instead be handled in the driver. |
| 102 |
bool ShouldAddDefaultIncludePaths(const llvm::Triple &triple); |
| 103 |
|
| 104 |
/// Adds the default system include paths so that e.g. stdio.h is found. |
| 105 |
void AddDefaultIncludePaths(const LangOptions &Lang, |
| 106 |
const llvm::Triple &triple, |
| 107 |
const HeaderSearchOptions &HSOpts); |
| 108 |
|
| 109 |
/// Merges all search path lists into one list and send it to HeaderSearch. |
| 110 |
void Realize(const LangOptions &Lang); |
| 111 |
}; |
| 112 |
|
| 113 |
} // end anonymous namespace. |
| 114 |
|
| 115 |
static bool CanPrefixSysroot(StringRef Path) { |
| 116 |
#if defined(_WIN32) |
| 117 |
return !Path.empty() && llvm::sys::path::is_separator(Path[0]); |
| 118 |
#else |
| 119 |
return llvm::sys::path::is_absolute(Path); |
| 120 |
#endif |
| 121 |
} |
| 122 |
|
| 123 |
bool InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group, |
| 124 |
bool isFramework, |
| 125 |
std::optional<unsigned> UserEntryIdx) { |
| 126 |
// Add the path with sysroot prepended, if desired and this is a system header |
| 127 |
// group. |
| 128 |
if (HasSysroot) { |
| 129 |
SmallString<256> MappedPathStorage; |
| 130 |
StringRef MappedPathStr = Path.toStringRef(MappedPathStorage); |
| 131 |
if (CanPrefixSysroot(MappedPathStr)) { |
| 132 |
return AddUnmappedPath(IncludeSysroot + Path, Group, isFramework, |
| 133 |
UserEntryIdx); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return AddUnmappedPath(Path, Group, isFramework, UserEntryIdx); |
| 138 |
} |
| 139 |
|
| 140 |
bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group, |
| 141 |
bool isFramework, |
| 142 |
std::optional<unsigned> UserEntryIdx) { |
| 143 |
assert(!Path.isTriviallyEmpty() && "can't handle empty path here"); |
| 144 |
|
| 145 |
FileManager &FM = Headers.getFileMgr(); |
| 146 |
SmallString<256> MappedPathStorage; |
| 147 |
StringRef MappedPathStr = Path.toStringRef(MappedPathStorage); |
| 148 |
|
| 149 |
// If use system headers while cross-compiling, emit the warning. |
| 150 |
if (HasSysroot && (MappedPathStr.startswith("/usr/include") || |
| 151 |
MappedPathStr.startswith("/usr/local/include"))) { |
| 152 |
Headers.getDiags().Report(diag::warn_poison_system_directories) |
| 153 |
<< MappedPathStr; |
| 154 |
} |
| 155 |
|
| 156 |
// Compute the DirectoryLookup type. |
| 157 |
SrcMgr::CharacteristicKind Type; |
| 158 |
if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) { |
| 159 |
Type = SrcMgr::C_User; |
| 160 |
} else if (Group == ExternCSystem) { |
| 161 |
Type = SrcMgr::C_ExternCSystem; |
| 162 |
} else { |
| 163 |
Type = SrcMgr::C_System; |
| 164 |
} |
| 165 |
|
| 166 |
// If the directory exists, add it. |
| 167 |
if (auto DE = FM.getOptionalDirectoryRef(MappedPathStr)) { |
| 168 |
IncludePath.emplace_back(Group, DirectoryLookup(*DE, Type, isFramework), |
| 169 |
UserEntryIdx); |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
// Check to see if this is an apple-style headermap (which are not allowed to |
| 174 |
// be frameworks). |
| 175 |
if (!isFramework) { |
| 176 |
if (auto FE = FM.getFile(MappedPathStr)) { |
| 177 |
if (const HeaderMap *HM = Headers.CreateHeaderMap(*FE)) { |
| 178 |
// It is a headermap, add it to the search path. |
| 179 |
IncludePath.emplace_back( |
| 180 |
Group, DirectoryLookup(HM, Type, Group == IndexHeaderMap), |
| 181 |
UserEntryIdx); |
| 182 |
return true; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if (Verbose) |
| 188 |
llvm::errs() << "ignoring nonexistent directory \"" |
| 189 |
<< MappedPathStr << "\"\n"; |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
bool InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base, |
| 194 |
StringRef ArchDir, |
| 195 |
StringRef Dir32, |
| 196 |
StringRef Dir64, |
| 197 |
const llvm::Triple &triple) { |
| 198 |
// Add the base dir |
| 199 |
bool IsBaseFound = AddPath(Base, CXXSystem, false); |
| 200 |
|
| 201 |
// Add the multilib dirs |
| 202 |
llvm::Triple::ArchType arch = triple.getArch(); |
| 203 |
bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64; |
| 204 |
if (is64bit) |
| 205 |
AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false); |
| 206 |
else |
| 207 |
AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false); |
| 208 |
|
| 209 |
// Add the backward dir |
| 210 |
AddPath(Base + "/backward", CXXSystem, false); |
| 211 |
return IsBaseFound; |
| 212 |
} |
| 213 |
|
| 214 |
void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base, |
| 215 |
StringRef Arch, |
| 216 |
StringRef Version) { |
| 217 |
AddPath(Base + "/" + Arch + "/" + Version + "/include/c++", |
| 218 |
CXXSystem, false); |
| 219 |
AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch, |
| 220 |
CXXSystem, false); |
| 221 |
AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward", |
| 222 |
CXXSystem, false); |
| 223 |
} |
| 224 |
|
| 225 |
void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple, |
| 226 |
const HeaderSearchOptions &HSOpts) { |
| 227 |
if (!ShouldAddDefaultIncludePaths(triple)) |
| 228 |
llvm_unreachable("Include management is handled in the driver."); |
| 229 |
|
| 230 |
llvm::Triple::OSType os = triple.getOS(); |
| 231 |
|
| 232 |
if (HSOpts.UseStandardSystemIncludes) { |
| 233 |
switch (os) { |
| 234 |
case llvm::Triple::CloudABI: |
| 235 |
case llvm::Triple::NaCl: |
| 236 |
case llvm::Triple::PS4: |
| 237 |
case llvm::Triple::PS5: |
| 238 |
case llvm::Triple::ELFIAMCU: |
| 239 |
break; |
| 240 |
case llvm::Triple::Win32: |
| 241 |
if (triple.getEnvironment() != llvm::Triple::Cygnus) |
| 242 |
break; |
| 243 |
[[fallthrough]]; |
| 244 |
default: |
| 245 |
// FIXME: temporary hack: hard-coded paths. |
| 246 |
AddPath("/usr/local/include", System, false); |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
// Builtin includes use #include_next directives and should be positioned |
| 252 |
// just prior C include dirs. |
| 253 |
if (HSOpts.UseBuiltinIncludes) { |
| 254 |
// Ignore the sys root, we *always* look for clang headers relative to |
| 255 |
// supplied path. |
| 256 |
SmallString<128> P = StringRef(HSOpts.ResourceDir); |
| 257 |
llvm::sys::path::append(P, "include"); |
| 258 |
AddUnmappedPath(P, ExternCSystem, false); |
| 259 |
} |
| 260 |
|
| 261 |
// All remaining additions are for system include directories, early exit if |
| 262 |
// we aren't using them. |
| 263 |
if (!HSOpts.UseStandardSystemIncludes) |
| 264 |
return; |
| 265 |
|
| 266 |
// Add dirs specified via 'configure --with-c-include-dirs'. |
| 267 |
StringRef CIncludeDirs(C_INCLUDE_DIRS); |
| 268 |
if (CIncludeDirs != "") { |
| 269 |
SmallVector<StringRef, 5> dirs; |
| 270 |
CIncludeDirs.split(dirs, ":"); |
| 271 |
for (StringRef dir : dirs) |
| 272 |
AddPath(dir, ExternCSystem, false); |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
switch (os) { |
| 277 |
case llvm::Triple::CloudABI: { |
| 278 |
// <sysroot>/<triple>/include |
| 279 |
SmallString<128> P = StringRef(HSOpts.ResourceDir); |
| 280 |
llvm::sys::path::append(P, "../../..", triple.str(), "include"); |
| 281 |
AddPath(P, System, false); |
| 282 |
break; |
| 283 |
} |
| 284 |
|
| 285 |
case llvm::Triple::Haiku: |
| 286 |
AddPath("/boot/system/non-packaged/develop/headers", System, false); |
| 287 |
AddPath("/boot/system/develop/headers/os", System, false); |
| 288 |
AddPath("/boot/system/develop/headers/os/app", System, false); |
| 289 |
AddPath("/boot/system/develop/headers/os/arch", System, false); |
| 290 |
AddPath("/boot/system/develop/headers/os/device", System, false); |
| 291 |
AddPath("/boot/system/develop/headers/os/drivers", System, false); |
| 292 |
AddPath("/boot/system/develop/headers/os/game", System, false); |
| 293 |
AddPath("/boot/system/develop/headers/os/interface", System, false); |
| 294 |
AddPath("/boot/system/develop/headers/os/kernel", System, false); |
| 295 |
AddPath("/boot/system/develop/headers/os/locale", System, false); |
| 296 |
AddPath("/boot/system/develop/headers/os/mail", System, false); |
| 297 |
AddPath("/boot/system/develop/headers/os/media", System, false); |
| 298 |
AddPath("/boot/system/develop/headers/os/midi", System, false); |
| 299 |
AddPath("/boot/system/develop/headers/os/midi2", System, false); |
| 300 |
AddPath("/boot/system/develop/headers/os/net", System, false); |
| 301 |
AddPath("/boot/system/develop/headers/os/opengl", System, false); |
| 302 |
AddPath("/boot/system/develop/headers/os/storage", System, false); |
| 303 |
AddPath("/boot/system/develop/headers/os/support", System, false); |
| 304 |
AddPath("/boot/system/develop/headers/os/translation", System, false); |
| 305 |
AddPath("/boot/system/develop/headers/os/add-ons/graphics", System, false); |
| 306 |
AddPath("/boot/system/develop/headers/os/add-ons/input_server", System, false); |
| 307 |
AddPath("/boot/system/develop/headers/os/add-ons/mail_daemon", System, false); |
| 308 |
AddPath("/boot/system/develop/headers/os/add-ons/registrar", System, false); |
| 309 |
AddPath("/boot/system/develop/headers/os/add-ons/screen_saver", System, false); |
| 310 |
AddPath("/boot/system/develop/headers/os/add-ons/tracker", System, false); |
| 311 |
AddPath("/boot/system/develop/headers/os/be_apps/Deskbar", System, false); |
| 312 |
AddPath("/boot/system/develop/headers/os/be_apps/NetPositive", System, false); |
| 313 |
AddPath("/boot/system/develop/headers/os/be_apps/Tracker", System, false); |
| 314 |
AddPath("/boot/system/develop/headers/3rdparty", System, false); |
| 315 |
AddPath("/boot/system/develop/headers/bsd", System, false); |
| 316 |
AddPath("/boot/system/develop/headers/glibc", System, false); |
| 317 |
AddPath("/boot/system/develop/headers/posix", System, false); |
| 318 |
AddPath("/boot/system/develop/headers", System, false); |
| 319 |
break; |
| 320 |
case llvm::Triple::RTEMS: |
| 321 |
break; |
| 322 |
case llvm::Triple::Win32: |
| 323 |
switch (triple.getEnvironment()) { |
| 324 |
default: llvm_unreachable("Include management is handled in the driver."); |
| 325 |
case llvm::Triple::Cygnus: |
| 326 |
AddPath("/usr/include/w32api", System, false); |
| 327 |
break; |
| 328 |
case llvm::Triple::GNU: |
| 329 |
break; |
| 330 |
} |
| 331 |
break; |
| 332 |
default: |
| 333 |
break; |
| 334 |
} |
| 335 |
|
| 336 |
switch (os) { |
| 337 |
case llvm::Triple::CloudABI: |
| 338 |
case llvm::Triple::RTEMS: |
| 339 |
case llvm::Triple::NaCl: |
| 340 |
case llvm::Triple::ELFIAMCU: |
| 341 |
break; |
| 342 |
case llvm::Triple::PS4: |
| 343 |
case llvm::Triple::PS5: { |
| 344 |
// <isysroot> gets prepended later in AddPath(). |
| 345 |
std::string BaseSDKPath; |
| 346 |
if (!HasSysroot) { |
| 347 |
const char *EnvVar = (os == llvm::Triple::PS4) ? "SCE_ORBIS_SDK_DIR" |
| 348 |
: "SCE_PROSPERO_SDK_DIR"; |
| 349 |
const char *envValue = getenv(EnvVar); |
| 350 |
if (envValue) |
| 351 |
BaseSDKPath = envValue; |
| 352 |
else { |
| 353 |
// HSOpts.ResourceDir variable contains the location of Clang's |
| 354 |
// resource files. |
| 355 |
// Assuming that Clang is configured for PS4 without |
| 356 |
// --with-clang-resource-dir option, the location of Clang's resource |
| 357 |
// files is <SDK_DIR>/host_tools/lib/clang |
| 358 |
SmallString<128> P = StringRef(HSOpts.ResourceDir); |
| 359 |
llvm::sys::path::append(P, "../../.."); |
| 360 |
BaseSDKPath = std::string(P.str()); |
| 361 |
} |
| 362 |
} |
| 363 |
AddPath(BaseSDKPath + "/target/include", System, false); |
| 364 |
AddPath(BaseSDKPath + "/target/include_common", System, false); |
| 365 |
break; |
| 366 |
} |
| 367 |
default: |
| 368 |
AddPath("/usr/include", ExternCSystem, false); |
| 369 |
break; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths( |
| 374 |
const LangOptions &LangOpts, const llvm::Triple &triple, |
| 375 |
const HeaderSearchOptions &HSOpts) { |
| 376 |
if (!ShouldAddDefaultIncludePaths(triple)) |
| 377 |
llvm_unreachable("Include management is handled in the driver."); |
| 378 |
|
| 379 |
// FIXME: temporary hack: hard-coded paths. |
| 380 |
llvm::Triple::OSType os = triple.getOS(); |
| 381 |
switch (os) { |
| 382 |
case llvm::Triple::Win32: |
| 383 |
switch (triple.getEnvironment()) { |
| 384 |
default: llvm_unreachable("Include management is handled in the driver."); |
| 385 |
case llvm::Triple::Cygnus: |
| 386 |
// Cygwin-1.7 |
| 387 |
AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3"); |
| 388 |
AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3"); |
| 389 |
AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4"); |
| 390 |
// g++-4 / Cygwin-1.5 |
| 391 |
AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2"); |
| 392 |
break; |
| 393 |
} |
| 394 |
break; |
| 395 |
case llvm::Triple::DragonFly: |
| 396 |
AddPath("/usr/include/c++/5.0", CXXSystem, false); |
| 397 |
break; |
| 398 |
case llvm::Triple::Minix: |
| 399 |
AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3", |
| 400 |
"", "", "", triple); |
| 401 |
break; |
| 402 |
default: |
| 403 |
break; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
bool InitHeaderSearch::ShouldAddDefaultIncludePaths( |
| 408 |
const llvm::Triple &triple) { |
| 409 |
switch (triple.getOS()) { |
| 410 |
case llvm::Triple::AIX: |
| 411 |
case llvm::Triple::Emscripten: |
| 412 |
case llvm::Triple::FreeBSD: |
| 413 |
case llvm::Triple::NetBSD: |
| 414 |
case llvm::Triple::OpenBSD: |
| 415 |
case llvm::Triple::Fuchsia: |
| 416 |
case llvm::Triple::Hurd: |
| 417 |
case llvm::Triple::Linux: |
| 418 |
case llvm::Triple::Solaris: |
| 419 |
case llvm::Triple::WASI: |
| 420 |
return false; |
| 421 |
|
| 422 |
case llvm::Triple::Win32: |
| 423 |
if (triple.getEnvironment() != llvm::Triple::Cygnus || |
| 424 |
triple.isOSBinFormatMachO()) |
| 425 |
return false; |
| 426 |
break; |
| 427 |
|
| 428 |
case llvm::Triple::UnknownOS: |
| 429 |
if (triple.isWasm()) |
| 430 |
return false; |
| 431 |
break; |
| 432 |
|
| 433 |
default: |
| 434 |
break; |
| 435 |
} |
| 436 |
|
| 437 |
return true; // Everything else uses AddDefaultIncludePaths(). |
| 438 |
} |
| 439 |
|
| 440 |
void InitHeaderSearch::AddDefaultIncludePaths( |
| 441 |
const LangOptions &Lang, const llvm::Triple &triple, |
| 442 |
const HeaderSearchOptions &HSOpts) { |
| 443 |
// NB: This code path is going away. All of the logic is moving into the |
| 444 |
// driver which has the information necessary to do target-specific |
| 445 |
// selections of default include paths. Each target which moves there will be |
| 446 |
// exempted from this logic in ShouldAddDefaultIncludePaths() until we can |
| 447 |
// delete the entire pile of code. |
| 448 |
if (!ShouldAddDefaultIncludePaths(triple)) |
| 449 |
return; |
| 450 |
|
| 451 |
// NOTE: some additional header search logic is handled in the driver for |
| 452 |
// Darwin. |
| 453 |
if (triple.isOSDarwin()) { |
| 454 |
if (HSOpts.UseStandardSystemIncludes) { |
| 455 |
// Add the default framework include paths on Darwin. |
| 456 |
if (triple.isDriverKit()) { |
| 457 |
AddPath("/System/DriverKit/System/Library/Frameworks", System, true); |
| 458 |
} else { |
| 459 |
AddPath("/System/Library/Frameworks", System, true); |
| 460 |
AddPath("/Library/Frameworks", System, true); |
| 461 |
} |
| 462 |
} |
| 463 |
return; |
| 464 |
} |
| 465 |
|
| 466 |
if (Lang.CPlusPlus && !Lang.AsmPreprocessor && |
| 467 |
HSOpts.UseStandardCXXIncludes && HSOpts.UseStandardSystemIncludes) { |
| 468 |
if (HSOpts.UseLibcxx) { |
| 469 |
AddPath("/usr/include/c++/v1", CXXSystem, false); |
| 470 |
} else { |
| 471 |
AddDefaultCPlusPlusIncludePaths(Lang, triple, HSOpts); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
AddDefaultCIncludePaths(triple, HSOpts); |
| 476 |
} |
| 477 |
|
| 478 |
/// If there are duplicate directory entries in the specified search list, |
| 479 |
/// remove the later (dead) ones. Returns the number of non-system headers |
| 480 |
/// removed, which is used to update NumAngled. |
| 481 |
static unsigned RemoveDuplicates(std::vector<DirectoryLookupInfo> &SearchList, |
| 482 |
unsigned First, bool Verbose) { |
| 483 |
llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; |
| 484 |
llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; |
| 485 |
llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; |
| 486 |
unsigned NonSystemRemoved = 0; |
| 487 |
for (unsigned i = First; i != SearchList.size(); ++i) { |
| 488 |
unsigned DirToRemove = i; |
| 489 |
|
| 490 |
const DirectoryLookup &CurEntry = SearchList[i].Lookup; |
| 491 |
|
| 492 |
if (CurEntry.isNormalDir()) { |
| 493 |
// If this isn't the first time we've seen this dir, remove it. |
| 494 |
if (SeenDirs.insert(CurEntry.getDir()).second) |
| 495 |
continue; |
| 496 |
} else if (CurEntry.isFramework()) { |
| 497 |
// If this isn't the first time we've seen this framework dir, remove it. |
| 498 |
if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()).second) |
| 499 |
continue; |
| 500 |
} else { |
| 501 |
assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); |
| 502 |
// If this isn't the first time we've seen this headermap, remove it. |
| 503 |
if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()).second) |
| 504 |
continue; |
| 505 |
} |
| 506 |
|
| 507 |
// If we have a normal #include dir/framework/headermap that is shadowed |
| 508 |
// later in the chain by a system include location, we actually want to |
| 509 |
// ignore the user's request and drop the user dir... keeping the system |
| 510 |
// dir. This is weird, but required to emulate GCC's search path correctly. |
| 511 |
// |
| 512 |
// Since dupes of system dirs are rare, just rescan to find the original |
| 513 |
// that we're nuking instead of using a DenseMap. |
| 514 |
if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { |
| 515 |
// Find the dir that this is the same of. |
| 516 |
unsigned FirstDir; |
| 517 |
for (FirstDir = First;; ++FirstDir) { |
| 518 |
assert(FirstDir != i && "Didn't find dupe?"); |
| 519 |
|
| 520 |
const DirectoryLookup &SearchEntry = SearchList[FirstDir].Lookup; |
| 521 |
|
| 522 |
// If these are different lookup types, then they can't be the dupe. |
| 523 |
if (SearchEntry.getLookupType() != CurEntry.getLookupType()) |
| 524 |
continue; |
| 525 |
|
| 526 |
bool isSame; |
| 527 |
if (CurEntry.isNormalDir()) |
| 528 |
isSame = SearchEntry.getDir() == CurEntry.getDir(); |
| 529 |
else if (CurEntry.isFramework()) |
| 530 |
isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); |
| 531 |
else { |
| 532 |
assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); |
| 533 |
isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); |
| 534 |
} |
| 535 |
|
| 536 |
if (isSame) |
| 537 |
break; |
| 538 |
} |
| 539 |
|
| 540 |
// If the first dir in the search path is a non-system dir, zap it |
| 541 |
// instead of the system one. |
| 542 |
if (SearchList[FirstDir].Lookup.getDirCharacteristic() == SrcMgr::C_User) |
| 543 |
DirToRemove = FirstDir; |
| 544 |
} |
| 545 |
|
| 546 |
if (Verbose) { |
| 547 |
llvm::errs() << "ignoring duplicate directory \"" |
| 548 |
<< CurEntry.getName() << "\"\n"; |
| 549 |
if (DirToRemove != i) |
| 550 |
llvm::errs() << " as it is a non-system directory that duplicates " |
| 551 |
<< "a system directory\n"; |
| 552 |
} |
| 553 |
if (DirToRemove != i) |
| 554 |
++NonSystemRemoved; |
| 555 |
|
| 556 |
// This is reached if the current entry is a duplicate. Remove the |
| 557 |
// DirToRemove (usually the current dir). |
| 558 |
SearchList.erase(SearchList.begin()+DirToRemove); |
| 559 |
--i; |
| 560 |
} |
| 561 |
return NonSystemRemoved; |
| 562 |
} |
| 563 |
|
| 564 |
/// Extract DirectoryLookups from DirectoryLookupInfos. |
| 565 |
static std::vector<DirectoryLookup> |
| 566 |
extractLookups(const std::vector<DirectoryLookupInfo> &Infos) { |
| 567 |
std::vector<DirectoryLookup> Lookups; |
| 568 |
Lookups.reserve(Infos.size()); |
| 569 |
llvm::transform(Infos, std::back_inserter(Lookups), |
| 570 |
[](const DirectoryLookupInfo &Info) { return Info.Lookup; }); |
| 571 |
return Lookups; |
| 572 |
} |
| 573 |
|
| 574 |
/// Collect the mapping between indices of DirectoryLookups and UserEntries. |
| 575 |
static llvm::DenseMap<unsigned, unsigned> |
| 576 |
mapToUserEntries(const std::vector<DirectoryLookupInfo> &Infos) { |
| 577 |
llvm::DenseMap<unsigned, unsigned> LookupsToUserEntries; |
| 578 |
for (unsigned I = 0, E = Infos.size(); I < E; ++I) { |
| 579 |
// Check whether this DirectoryLookup maps to a HeaderSearch::UserEntry. |
| 580 |
if (Infos[I].UserEntryIdx) |
| 581 |
LookupsToUserEntries.insert({I, *Infos[I].UserEntryIdx}); |
| 582 |
} |
| 583 |
return LookupsToUserEntries; |
| 584 |
} |
| 585 |
|
| 586 |
void InitHeaderSearch::Realize(const LangOptions &Lang) { |
| 587 |
// Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. |
| 588 |
std::vector<DirectoryLookupInfo> SearchList; |
| 589 |
SearchList.reserve(IncludePath.size()); |
| 590 |
|
| 591 |
// Quoted arguments go first. |
| 592 |
for (auto &Include : IncludePath) |
| 593 |
if (Include.Group == Quoted) |
| 594 |
SearchList.push_back(Include); |
| 595 |
|
| 596 |
// Deduplicate and remember index. |
| 597 |
RemoveDuplicates(SearchList, 0, Verbose); |
| 598 |
unsigned NumQuoted = SearchList.size(); |
| 599 |
|
| 600 |
for (auto &Include : IncludePath) |
| 601 |
if (Include.Group == Angled || Include.Group == IndexHeaderMap) |
| 602 |
SearchList.push_back(Include); |
| 603 |
|
| 604 |
RemoveDuplicates(SearchList, NumQuoted, Verbose); |
| 605 |
unsigned NumAngled = SearchList.size(); |
| 606 |
|
| 607 |
for (auto &Include : IncludePath) |
| 608 |
if (Include.Group == System || Include.Group == ExternCSystem || |
| 609 |
(!Lang.ObjC && !Lang.CPlusPlus && Include.Group == CSystem) || |
| 610 |
(/*FIXME !Lang.ObjC && */ Lang.CPlusPlus && |
| 611 |
Include.Group == CXXSystem) || |
| 612 |
(Lang.ObjC && !Lang.CPlusPlus && Include.Group == ObjCSystem) || |
| 613 |
(Lang.ObjC && Lang.CPlusPlus && Include.Group == ObjCXXSystem)) |
| 614 |
SearchList.push_back(Include); |
| 615 |
|
| 616 |
for (auto &Include : IncludePath) |
| 617 |
if (Include.Group == After) |
| 618 |
SearchList.push_back(Include); |
| 619 |
|
| 620 |
// Remove duplicates across both the Angled and System directories. GCC does |
| 621 |
// this and failing to remove duplicates across these two groups breaks |
| 622 |
// #include_next. |
| 623 |
unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose); |
| 624 |
NumAngled -= NonSystemRemoved; |
| 625 |
|
| 626 |
bool DontSearchCurDir = false; // TODO: set to true if -I- is set? |
| 627 |
Headers.SetSearchPaths(extractLookups(SearchList), NumQuoted, NumAngled, |
| 628 |
DontSearchCurDir, mapToUserEntries(SearchList)); |
| 629 |
|
| 630 |
Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes); |
| 631 |
|
| 632 |
// If verbose, print the list of directories that will be searched. |
| 633 |
if (Verbose) { |
| 634 |
llvm::errs() << "#include \"...\" search starts here:\n"; |
| 635 |
for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { |
| 636 |
if (i == NumQuoted) |
| 637 |
llvm::errs() << "#include <...> search starts here:\n"; |
| 638 |
StringRef Name = SearchList[i].Lookup.getName(); |
| 639 |
const char *Suffix; |
| 640 |
if (SearchList[i].Lookup.isNormalDir()) |
| 641 |
Suffix = ""; |
| 642 |
else if (SearchList[i].Lookup.isFramework()) |
| 643 |
Suffix = " (framework directory)"; |
| 644 |
else { |
| 645 |
assert(SearchList[i].Lookup.isHeaderMap() && "Unknown DirectoryLookup"); |
| 646 |
Suffix = " (headermap)"; |
| 647 |
} |
| 648 |
llvm::errs() << " " << Name << Suffix << "\n"; |
| 649 |
} |
| 650 |
llvm::errs() << "End of search list.\n"; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
void clang::ApplyHeaderSearchOptions(HeaderSearch &HS, |
| 655 |
const HeaderSearchOptions &HSOpts, |
| 656 |
const LangOptions &Lang, |
| 657 |
const llvm::Triple &Triple) { |
| 658 |
InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot); |
| 659 |
|
| 660 |
// Add the user defined entries. |
| 661 |
for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) { |
| 662 |
const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i]; |
| 663 |
if (E.IgnoreSysRoot) { |
| 664 |
Init.AddUnmappedPath(E.Path, E.Group, E.IsFramework, i); |
| 665 |
} else { |
| 666 |
Init.AddPath(E.Path, E.Group, E.IsFramework, i); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
Init.AddDefaultIncludePaths(Lang, Triple, HSOpts); |
| 671 |
|
| 672 |
for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i) |
| 673 |
Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix, |
| 674 |
HSOpts.SystemHeaderPrefixes[i].IsSystemHeader); |
| 675 |
|
| 676 |
if (HSOpts.UseBuiltinIncludes) { |
| 677 |
// Set up the builtin include directory in the module map. |
| 678 |
SmallString<128> P = StringRef(HSOpts.ResourceDir); |
| 679 |
llvm::sys::path::append(P, "include"); |
| 680 |
if (auto Dir = HS.getFileMgr().getDirectory(P)) |
| 681 |
HS.getModuleMap().setBuiltinIncludeDir(*Dir); |
| 682 |
} |
| 683 |
|
| 684 |
Init.Realize(Lang); |
| 685 |
} |