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