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