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