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