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