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/Frontend/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 AddPath("c:/mingw/include", System, true, false, false); 325 } 326 break; 327 case llvm::Triple::FreeBSD: 328 AddPath("/usr/include/clang/" CLANG_VERSION_STRING, 329 System, false, false, false); 330 break; 331 332 default: 333 break; 334 } 335 336 if ( os != llvm::Triple::RTEMS ) 337 AddPath("/usr/include", System, false, false, false); 338 } 339 340 void InitHeaderSearch:: 341 AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) { 342 llvm::Triple::OSType os = triple.getOS(); 343 // FIXME: temporary hack: hard-coded paths. 344 345 if (triple.isOSDarwin()) { 346 switch (triple.getArch()) { 347 default: break; 348 349 case llvm::Triple::ppc: 350 case llvm::Triple::ppc64: 351 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 352 "powerpc-apple-darwin10", "", "ppc64", 353 triple); 354 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", 355 "powerpc-apple-darwin10", "", "ppc64", 356 triple); 357 break; 358 359 case llvm::Triple::x86: 360 case llvm::Triple::x86_64: 361 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 362 "i686-apple-darwin10", "", "x86_64", triple); 363 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", 364 "i686-apple-darwin8", "", "", triple); 365 break; 366 367 case llvm::Triple::arm: 368 case llvm::Triple::thumb: 369 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 370 "arm-apple-darwin10", "v7", "", triple); 371 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 372 "arm-apple-darwin10", "v6", "", triple); 373 break; 374 } 375 return; 376 } 377 378 switch (os) { 379 case llvm::Triple::Linux: 380 case llvm::Triple::Win32: 381 llvm_unreachable("Include management is handled in the driver."); 382 383 case llvm::Triple::Cygwin: 384 // Cygwin-1.7 385 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3"); 386 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4"); 387 // g++-4 / Cygwin-1.5 388 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2"); 389 break; 390 case llvm::Triple::MinGW32: 391 // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32) 392 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0"); 393 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1"); 394 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2"); 395 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3"); 396 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.4"); 397 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0"); 398 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1"); 399 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2"); 400 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.3"); 401 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0"); 402 // mingw.org C++ include paths 403 AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS 404 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.2"); 405 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.1"); 406 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.2"); 407 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0"); 408 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0"); 409 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0"); 410 break; 411 case llvm::Triple::DragonFly: 412 AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false); 413 break; 414 case llvm::Triple::FreeBSD: 415 // FreeBSD 8.0 416 // FreeBSD 7.3 417 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", 418 "", "", "", triple); 419 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2/backward", 420 "", "", "", triple); 421 break; 422 case llvm::Triple::NetBSD: 423 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple); 424 break; 425 case llvm::Triple::OpenBSD: { 426 std::string t = triple.getTriple(); 427 if (t.substr(0, 6) == "x86_64") 428 t.replace(0, 6, "amd64"); 429 AddGnuCPlusPlusIncludePaths("/usr/include/g++", 430 t, "", "", triple); 431 break; 432 } 433 case llvm::Triple::Minix: 434 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3", 435 "", "", "", triple); 436 break; 437 case llvm::Triple::Solaris: 438 AddGnuCPlusPlusIncludePaths("/usr/gcc/4.5/include/c++/4.5.2/", 439 "i386-pc-solaris2.11", "", "", triple); 440 // Solaris - Fall though.. 441 case llvm::Triple::AuroraUX: 442 // AuroraUX 443 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4", 444 "i386-pc-solaris2.11", "", "", 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::Win32: 464 return; 465 } 466 467 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes && 468 HSOpts.UseStandardSystemIncludes) { 469 if (HSOpts.UseLibcxx) { 470 if (triple.isOSDarwin()) { 471 // On Darwin, libc++ may be installed alongside the compiler in 472 // lib/c++/v1. 473 llvm::sys::Path P(HSOpts.ResourceDir); 474 if (!P.isEmpty()) { 475 P.eraseComponent(); // Remove version from foo/lib/clang/version 476 P.eraseComponent(); // Remove clang from foo/lib/clang 477 478 // Get foo/lib/c++/v1 479 P.appendComponent("c++"); 480 P.appendComponent("v1"); 481 AddPath(P.str(), CXXSystem, true, false, false, true); 482 } 483 } 484 // On Solaris, include the support directory for things like xlocale and 485 // fudged system headers. 486 if (triple.getOS() == llvm::Triple::Solaris) 487 AddPath("/usr/include/c++/v1/support/solaris", CXXSystem, true, false, 488 false); 489 490 AddPath("/usr/include/c++/v1", CXXSystem, true, false, false); 491 } else { 492 AddDefaultCPlusPlusIncludePaths(triple, HSOpts); 493 } 494 } 495 496 AddDefaultCIncludePaths(triple, HSOpts); 497 498 // Add the default framework include paths on Darwin. 499 if (HSOpts.UseStandardSystemIncludes) { 500 if (triple.isOSDarwin()) { 501 AddPath("/System/Library/Frameworks", System, true, false, true); 502 AddPath("/Library/Frameworks", System, true, false, true); 503 } 504 } 505 } 506 507 /// RemoveDuplicates - If there are duplicate directory entries in the specified 508 /// search list, remove the later (dead) ones. Returns the number of non-system 509 /// headers removed, which is used to update NumAngled. 510 static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, 511 unsigned First, bool Verbose) { 512 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; 513 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; 514 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; 515 unsigned NonSystemRemoved = 0; 516 for (unsigned i = First; i != SearchList.size(); ++i) { 517 unsigned DirToRemove = i; 518 519 const DirectoryLookup &CurEntry = SearchList[i]; 520 521 if (CurEntry.isNormalDir()) { 522 // If this isn't the first time we've seen this dir, remove it. 523 if (SeenDirs.insert(CurEntry.getDir())) 524 continue; 525 } else if (CurEntry.isFramework()) { 526 // If this isn't the first time we've seen this framework dir, remove it. 527 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir())) 528 continue; 529 } else { 530 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 531 // If this isn't the first time we've seen this headermap, remove it. 532 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap())) 533 continue; 534 } 535 536 // If we have a normal #include dir/framework/headermap that is shadowed 537 // later in the chain by a system include location, we actually want to 538 // ignore the user's request and drop the user dir... keeping the system 539 // dir. This is weird, but required to emulate GCC's search path correctly. 540 // 541 // Since dupes of system dirs are rare, just rescan to find the original 542 // that we're nuking instead of using a DenseMap. 543 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { 544 // Find the dir that this is the same of. 545 unsigned FirstDir; 546 for (FirstDir = 0; ; ++FirstDir) { 547 assert(FirstDir != i && "Didn't find dupe?"); 548 549 const DirectoryLookup &SearchEntry = SearchList[FirstDir]; 550 551 // If these are different lookup types, then they can't be the dupe. 552 if (SearchEntry.getLookupType() != CurEntry.getLookupType()) 553 continue; 554 555 bool isSame; 556 if (CurEntry.isNormalDir()) 557 isSame = SearchEntry.getDir() == CurEntry.getDir(); 558 else if (CurEntry.isFramework()) 559 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); 560 else { 561 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 562 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); 563 } 564 565 if (isSame) 566 break; 567 } 568 569 // If the first dir in the search path is a non-system dir, zap it 570 // instead of the system one. 571 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) 572 DirToRemove = FirstDir; 573 } 574 575 if (Verbose) { 576 llvm::errs() << "ignoring duplicate directory \"" 577 << CurEntry.getName() << "\"\n"; 578 if (DirToRemove != i) 579 llvm::errs() << " as it is a non-system directory that duplicates " 580 << "a system directory\n"; 581 } 582 if (DirToRemove != i) 583 ++NonSystemRemoved; 584 585 // This is reached if the current entry is a duplicate. Remove the 586 // DirToRemove (usually the current dir). 587 SearchList.erase(SearchList.begin()+DirToRemove); 588 --i; 589 } 590 return NonSystemRemoved; 591 } 592 593 594 void InitHeaderSearch::Realize(const LangOptions &Lang) { 595 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. 596 std::vector<DirectoryLookup> SearchList; 597 SearchList.reserve(IncludePath.size()); 598 599 // Quoted arguments go first. 600 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end(); 601 it != ie; ++it) { 602 if (it->first == Quoted) 603 SearchList.push_back(it->second); 604 } 605 // Deduplicate and remember index. 606 RemoveDuplicates(SearchList, 0, Verbose); 607 unsigned NumQuoted = SearchList.size(); 608 609 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end(); 610 it != ie; ++it) { 611 if (it->first == Angled || it->first == IndexHeaderMap) 612 SearchList.push_back(it->second); 613 } 614 615 RemoveDuplicates(SearchList, NumQuoted, Verbose); 616 unsigned NumAngled = SearchList.size(); 617 618 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end(); 619 it != ie; ++it) { 620 if (it->first == System || 621 (!Lang.ObjC1 && !Lang.CPlusPlus && it->first == CSystem) || 622 (/*FIXME !Lang.ObjC1 && */Lang.CPlusPlus && it->first == CXXSystem) || 623 (Lang.ObjC1 && !Lang.CPlusPlus && it->first == ObjCSystem) || 624 (Lang.ObjC1 && Lang.CPlusPlus && it->first == ObjCXXSystem)) 625 SearchList.push_back(it->second); 626 } 627 628 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end(); 629 it != ie; ++it) { 630 if (it->first == After) 631 SearchList.push_back(it->second); 632 } 633 634 // Remove duplicates across both the Angled and System directories. GCC does 635 // this and failing to remove duplicates across these two groups breaks 636 // #include_next. 637 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose); 638 NumAngled -= NonSystemRemoved; 639 640 bool DontSearchCurDir = false; // TODO: set to true if -I- is set? 641 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir); 642 643 Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes); 644 645 // If verbose, print the list of directories that will be searched. 646 if (Verbose) { 647 llvm::errs() << "#include \"...\" search starts here:\n"; 648 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { 649 if (i == NumQuoted) 650 llvm::errs() << "#include <...> search starts here:\n"; 651 const char *Name = SearchList[i].getName(); 652 const char *Suffix; 653 if (SearchList[i].isNormalDir()) 654 Suffix = ""; 655 else if (SearchList[i].isFramework()) 656 Suffix = " (framework directory)"; 657 else { 658 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); 659 Suffix = " (headermap)"; 660 } 661 llvm::errs() << " " << Name << Suffix << "\n"; 662 } 663 llvm::errs() << "End of search list.\n"; 664 } 665 } 666 667 void clang::ApplyHeaderSearchOptions(HeaderSearch &HS, 668 const HeaderSearchOptions &HSOpts, 669 const LangOptions &Lang, 670 const llvm::Triple &Triple) { 671 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot); 672 673 // Add the user defined entries. 674 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) { 675 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i]; 676 Init.AddPath(E.Path, E.Group, !E.ImplicitExternC, E.IsUserSupplied, 677 E.IsFramework, E.IgnoreSysRoot); 678 } 679 680 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts); 681 682 for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i) 683 Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix, 684 HSOpts.SystemHeaderPrefixes[i].IsSystemHeader); 685 686 if (HSOpts.UseBuiltinIncludes) { 687 // Set up the builtin include directory in the module map. 688 llvm::sys::Path P(HSOpts.ResourceDir); 689 P.appendComponent("include"); 690 if (const DirectoryEntry *Dir = HS.getFileMgr().getDirectory(P.str())) 691 HS.getModuleMap().setBuiltinIncludeDir(Dir); 692 } 693 694 Init.Realize(Lang); 695 } 696