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/Config/config.h" // C_INCLUDE_DIRS 18 #include "clang/Lex/HeaderSearch.h" 19 #include "clang/Lex/HeaderSearchOptions.h" 20 #include "llvm/ADT/SmallPtrSet.h" 21 #include "llvm/ADT/SmallString.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/ErrorHandling.h" 27 #include "llvm/Support/FileSystem.h" 28 #include "llvm/Support/Path.h" 29 #include "llvm/Support/raw_ostream.h" 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 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes; 44 HeaderSearch &Headers; 45 bool Verbose; 46 std::string IncludeSysroot; 47 bool HasSysroot; 48 49 public: 50 51 InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot) 52 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot), 53 HasSysroot(!(sysroot.empty() || sysroot == "/")) { 54 } 55 56 /// AddPath - Add the specified path to the specified group list, prefixing 57 /// the sysroot if used. 58 void AddPath(const Twine &Path, IncludeDirGroup Group, bool isFramework); 59 60 /// AddUnmappedPath - Add the specified path to the specified group list, 61 /// without performing any sysroot remapping. 62 void AddUnmappedPath(const Twine &Path, IncludeDirGroup Group, 63 bool isFramework); 64 65 /// AddSystemHeaderPrefix - Add the specified prefix to the system header 66 /// prefix list. 67 void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) { 68 SystemHeaderPrefixes.push_back(std::make_pair(Prefix, IsSystemHeader)); 69 } 70 71 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu 72 /// libstdc++. 73 void AddGnuCPlusPlusIncludePaths(StringRef Base, 74 StringRef ArchDir, 75 StringRef Dir32, 76 StringRef Dir64, 77 const llvm::Triple &triple); 78 79 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW 80 /// libstdc++. 81 void AddMinGWCPlusPlusIncludePaths(StringRef Base, 82 StringRef Arch, 83 StringRef Version); 84 85 /// AddMinGW64CXXPaths - Add the necessary paths to support 86 /// libstdc++ of x86_64-w64-mingw32 aka mingw-w64. 87 void AddMinGW64CXXPaths(StringRef Base, 88 StringRef Version); 89 90 // AddDefaultCIncludePaths - Add paths that should always be searched. 91 void AddDefaultCIncludePaths(const llvm::Triple &triple, 92 const HeaderSearchOptions &HSOpts); 93 94 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when 95 // compiling c++. 96 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, 97 const HeaderSearchOptions &HSOpts); 98 99 /// AddDefaultSystemIncludePaths - Adds the default system include paths so 100 /// that e.g. stdio.h is found. 101 void AddDefaultIncludePaths(const LangOptions &Lang, 102 const llvm::Triple &triple, 103 const HeaderSearchOptions &HSOpts); 104 105 /// Realize - Merges all search path lists into one list and send it to 106 /// HeaderSearch. 107 void Realize(const LangOptions &Lang); 108 }; 109 110 } // end anonymous namespace. 111 112 static bool CanPrefixSysroot(StringRef Path) { 113 #if defined(_WIN32) 114 return !Path.empty() && llvm::sys::path::is_separator(Path[0]); 115 #else 116 return llvm::sys::path::is_absolute(Path); 117 #endif 118 } 119 120 void InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group, 121 bool isFramework) { 122 // Add the path with sysroot prepended, if desired and this is a system header 123 // group. 124 if (HasSysroot) { 125 SmallString<256> MappedPathStorage; 126 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage); 127 if (CanPrefixSysroot(MappedPathStr)) { 128 AddUnmappedPath(IncludeSysroot + Path, Group, isFramework); 129 return; 130 } 131 } 132 133 AddUnmappedPath(Path, Group, isFramework); 134 } 135 136 void InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group, 137 bool isFramework) { 138 assert(!Path.isTriviallyEmpty() && "can't handle empty path here"); 139 140 FileManager &FM = Headers.getFileMgr(); 141 SmallString<256> MappedPathStorage; 142 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage); 143 144 // Compute the DirectoryLookup type. 145 SrcMgr::CharacteristicKind Type; 146 if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) { 147 Type = SrcMgr::C_User; 148 } else if (Group == ExternCSystem) { 149 Type = SrcMgr::C_ExternCSystem; 150 } else { 151 Type = SrcMgr::C_System; 152 } 153 154 // If the directory exists, add it. 155 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) { 156 IncludePath.push_back( 157 std::make_pair(Group, DirectoryLookup(DE, Type, isFramework))); 158 return; 159 } 160 161 // Check to see if this is an apple-style headermap (which are not allowed to 162 // be frameworks). 163 if (!isFramework) { 164 if (const FileEntry *FE = FM.getFile(MappedPathStr)) { 165 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) { 166 // It is a headermap, add it to the search path. 167 IncludePath.push_back( 168 std::make_pair(Group, 169 DirectoryLookup(HM, Type, Group == IndexHeaderMap))); 170 return; 171 } 172 } 173 } 174 175 if (Verbose) 176 llvm::errs() << "ignoring nonexistent directory \"" 177 << MappedPathStr << "\"\n"; 178 } 179 180 void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base, 181 StringRef ArchDir, 182 StringRef Dir32, 183 StringRef Dir64, 184 const llvm::Triple &triple) { 185 // Add the base dir 186 AddPath(Base, CXXSystem, false); 187 188 // Add the multilib dirs 189 llvm::Triple::ArchType arch = triple.getArch(); 190 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64; 191 if (is64bit) 192 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false); 193 else 194 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false); 195 196 // Add the backward dir 197 AddPath(Base + "/backward", CXXSystem, false); 198 } 199 200 void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base, 201 StringRef Arch, 202 StringRef Version) { 203 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++", 204 CXXSystem, false); 205 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch, 206 CXXSystem, false); 207 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward", 208 CXXSystem, false); 209 } 210 211 void InitHeaderSearch::AddMinGW64CXXPaths(StringRef Base, 212 StringRef Version) { 213 // Assumes Base is HeaderSearchOpts' ResourceDir 214 AddPath(Base + "/../../../include/c++/" + Version, 215 CXXSystem, false); 216 AddPath(Base + "/../../../include/c++/" + Version + "/x86_64-w64-mingw32", 217 CXXSystem, false); 218 AddPath(Base + "/../../../include/c++/" + Version + "/i686-w64-mingw32", 219 CXXSystem, false); 220 AddPath(Base + "/../../../include/c++/" + Version + "/backward", 221 CXXSystem, false); 222 } 223 224 void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple, 225 const HeaderSearchOptions &HSOpts) { 226 llvm::Triple::OSType os = triple.getOS(); 227 228 if (HSOpts.UseStandardSystemIncludes) { 229 switch (os) { 230 case llvm::Triple::FreeBSD: 231 case llvm::Triple::NetBSD: 232 case llvm::Triple::OpenBSD: 233 case llvm::Triple::Minix: 234 case llvm::Triple::Bitrig: 235 break; 236 default: 237 // FIXME: temporary hack: hard-coded paths. 238 AddPath("/usr/local/include", System, false); 239 break; 240 } 241 } 242 243 // Builtin includes use #include_next directives and should be positioned 244 // just prior C include dirs. 245 if (HSOpts.UseBuiltinIncludes) { 246 // Ignore the sys root, we *always* look for clang headers relative to 247 // supplied path. 248 SmallString<128> P = StringRef(HSOpts.ResourceDir); 249 llvm::sys::path::append(P, "include"); 250 AddUnmappedPath(P.str(), ExternCSystem, false); 251 } 252 253 // All remaining additions are for system include directories, early exit if 254 // we aren't using them. 255 if (!HSOpts.UseStandardSystemIncludes) 256 return; 257 258 // Add dirs specified via 'configure --with-c-include-dirs'. 259 StringRef CIncludeDirs(C_INCLUDE_DIRS); 260 if (CIncludeDirs != "") { 261 SmallVector<StringRef, 5> dirs; 262 CIncludeDirs.split(dirs, ":"); 263 for (SmallVectorImpl<StringRef>::iterator i = dirs.begin(); 264 i != dirs.end(); 265 ++i) 266 AddPath(*i, ExternCSystem, false); 267 return; 268 } 269 270 switch (os) { 271 case llvm::Triple::Linux: 272 case llvm::Triple::Win32: 273 llvm_unreachable("Include management is handled in the driver."); 274 275 case llvm::Triple::Haiku: 276 AddPath("/boot/common/include", System, false); 277 AddPath("/boot/develop/headers/os", System, false); 278 AddPath("/boot/develop/headers/os/app", System, false); 279 AddPath("/boot/develop/headers/os/arch", System, false); 280 AddPath("/boot/develop/headers/os/device", System, false); 281 AddPath("/boot/develop/headers/os/drivers", System, false); 282 AddPath("/boot/develop/headers/os/game", System, false); 283 AddPath("/boot/develop/headers/os/interface", System, false); 284 AddPath("/boot/develop/headers/os/kernel", System, false); 285 AddPath("/boot/develop/headers/os/locale", System, false); 286 AddPath("/boot/develop/headers/os/mail", System, false); 287 AddPath("/boot/develop/headers/os/media", System, false); 288 AddPath("/boot/develop/headers/os/midi", System, false); 289 AddPath("/boot/develop/headers/os/midi2", System, false); 290 AddPath("/boot/develop/headers/os/net", System, false); 291 AddPath("/boot/develop/headers/os/storage", System, false); 292 AddPath("/boot/develop/headers/os/support", System, false); 293 AddPath("/boot/develop/headers/os/translation", System, false); 294 AddPath("/boot/develop/headers/os/add-ons/graphics", System, false); 295 AddPath("/boot/develop/headers/os/add-ons/input_server", System, false); 296 AddPath("/boot/develop/headers/os/add-ons/screen_saver", System, false); 297 AddPath("/boot/develop/headers/os/add-ons/tracker", System, false); 298 AddPath("/boot/develop/headers/os/be_apps/Deskbar", System, false); 299 AddPath("/boot/develop/headers/os/be_apps/NetPositive", System, false); 300 AddPath("/boot/develop/headers/os/be_apps/Tracker", System, false); 301 AddPath("/boot/develop/headers/cpp", System, false); 302 AddPath("/boot/develop/headers/cpp/i586-pc-haiku", System, false); 303 AddPath("/boot/develop/headers/3rdparty", System, false); 304 AddPath("/boot/develop/headers/bsd", System, false); 305 AddPath("/boot/develop/headers/glibc", System, false); 306 AddPath("/boot/develop/headers/posix", System, false); 307 AddPath("/boot/develop/headers", System, false); 308 break; 309 case llvm::Triple::RTEMS: 310 break; 311 case llvm::Triple::Cygwin: 312 AddPath("/usr/include/w32api", System, false); 313 break; 314 case llvm::Triple::MinGW32: { 315 // mingw-w64 crt include paths 316 // <sysroot>/i686-w64-mingw32/include 317 SmallString<128> P = StringRef(HSOpts.ResourceDir); 318 llvm::sys::path::append(P, "../../../i686-w64-mingw32/include"); 319 AddPath(P.str(), System, false); 320 321 // <sysroot>/x86_64-w64-mingw32/include 322 P.resize(HSOpts.ResourceDir.size()); 323 llvm::sys::path::append(P, "../../../x86_64-w64-mingw32/include"); 324 AddPath(P.str(), System, false); 325 326 // mingw.org crt include paths 327 // <sysroot>/include 328 P.resize(HSOpts.ResourceDir.size()); 329 llvm::sys::path::append(P, "../../../include"); 330 AddPath(P.str(), System, false); 331 AddPath("/mingw/include", System, false); 332 #if defined(_WIN32) 333 AddPath("c:/mingw/include", System, false); 334 #endif 335 } 336 break; 337 338 default: 339 break; 340 } 341 342 if ( os != llvm::Triple::RTEMS ) 343 AddPath("/usr/include", ExternCSystem, false); 344 } 345 346 void InitHeaderSearch:: 347 AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) { 348 llvm::Triple::OSType os = triple.getOS(); 349 // FIXME: temporary hack: hard-coded paths. 350 351 if (triple.isOSDarwin()) { 352 switch (triple.getArch()) { 353 default: break; 354 355 case llvm::Triple::ppc: 356 case llvm::Triple::ppc64: 357 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 358 "powerpc-apple-darwin10", "", "ppc64", 359 triple); 360 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", 361 "powerpc-apple-darwin10", "", "ppc64", 362 triple); 363 break; 364 365 case llvm::Triple::x86: 366 case llvm::Triple::x86_64: 367 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 368 "i686-apple-darwin10", "", "x86_64", triple); 369 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", 370 "i686-apple-darwin8", "", "", triple); 371 break; 372 373 case llvm::Triple::arm: 374 case llvm::Triple::thumb: 375 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 376 "arm-apple-darwin10", "v7", "", triple); 377 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 378 "arm-apple-darwin10", "v6", "", triple); 379 break; 380 } 381 return; 382 } 383 384 switch (os) { 385 case llvm::Triple::Linux: 386 case llvm::Triple::Win32: 387 llvm_unreachable("Include management is handled in the driver."); 388 389 case llvm::Triple::Cygwin: 390 // Cygwin-1.7 391 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3"); 392 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3"); 393 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4"); 394 // g++-4 / Cygwin-1.5 395 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2"); 396 break; 397 case llvm::Triple::MinGW32: 398 // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32) 399 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0"); 400 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1"); 401 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2"); 402 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3"); 403 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.4"); 404 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0"); 405 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1"); 406 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2"); 407 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.3"); 408 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0"); 409 // mingw.org C++ include paths 410 AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS 411 #if defined(_WIN32) 412 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.8.1"); 413 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.2"); 414 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.1"); 415 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.2"); 416 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0"); 417 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0"); 418 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0"); 419 #endif 420 break; 421 case llvm::Triple::DragonFly: 422 if (llvm::sys::fs::exists("/usr/lib/gcc47")) 423 AddPath("/usr/include/c++/4.7", CXXSystem, false); 424 else 425 AddPath("/usr/include/c++/4.4", CXXSystem, false); 426 break; 427 case llvm::Triple::FreeBSD: 428 // FreeBSD 8.0 429 // FreeBSD 7.3 430 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple); 431 break; 432 case llvm::Triple::OpenBSD: { 433 std::string t = triple.getTriple(); 434 if (t.substr(0, 6) == "x86_64") 435 t.replace(0, 6, "amd64"); 436 AddGnuCPlusPlusIncludePaths("/usr/include/g++", 437 t, "", "", triple); 438 break; 439 } 440 case llvm::Triple::Solaris: 441 AddGnuCPlusPlusIncludePaths("/usr/gcc/4.5/include/c++/4.5.2/", 442 "i386-pc-solaris2.11", "", "", triple); 443 // Solaris - Fall though.. 444 case llvm::Triple::AuroraUX: 445 // AuroraUX 446 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4", 447 "i386-pc-solaris2.11", "", "", triple); 448 break; 449 default: 450 break; 451 } 452 } 453 454 void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang, 455 const llvm::Triple &triple, 456 const HeaderSearchOptions &HSOpts) { 457 // NB: This code path is going away. All of the logic is moving into the 458 // driver which has the information necessary to do target-specific 459 // selections of default include paths. Each target which moves there will be 460 // exempted from this logic here until we can delete the entire pile of code. 461 switch (triple.getOS()) { 462 default: 463 break; // Everything else continues to use this routine's logic. 464 465 case llvm::Triple::Linux: 466 case llvm::Triple::Win32: 467 return; 468 } 469 470 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes && 471 HSOpts.UseStandardSystemIncludes) { 472 if (HSOpts.UseLibcxx) { 473 if (triple.isOSDarwin()) { 474 // On Darwin, libc++ may be installed alongside the compiler in 475 // include/c++/v1. 476 if (!HSOpts.ResourceDir.empty()) { 477 // Remove version from foo/lib/clang/version 478 StringRef NoVer = llvm::sys::path::parent_path(HSOpts.ResourceDir); 479 // Remove clang from foo/lib/clang 480 StringRef Lib = llvm::sys::path::parent_path(NoVer); 481 // Remove lib from foo/lib 482 SmallString<128> P = llvm::sys::path::parent_path(Lib); 483 484 // Get foo/include/c++/v1 485 llvm::sys::path::append(P, "include", "c++", "v1"); 486 AddUnmappedPath(P.str(), CXXSystem, false); 487 } 488 } 489 // On Solaris, include the support directory for things like xlocale and 490 // fudged system headers. 491 if (triple.getOS() == llvm::Triple::Solaris) 492 AddPath("/usr/include/c++/v1/support/solaris", CXXSystem, false); 493 494 AddPath("/usr/include/c++/v1", CXXSystem, 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); 506 AddPath("/Library/Frameworks", System, 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 || it->first == ExternCSystem || 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 if (E.IgnoreSysRoot) { 681 Init.AddUnmappedPath(E.Path, E.Group, E.IsFramework); 682 } else { 683 Init.AddPath(E.Path, E.Group, E.IsFramework); 684 } 685 } 686 687 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts); 688 689 for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i) 690 Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix, 691 HSOpts.SystemHeaderPrefixes[i].IsSystemHeader); 692 693 if (HSOpts.UseBuiltinIncludes) { 694 // Set up the builtin include directory in the module map. 695 SmallString<128> P = StringRef(HSOpts.ResourceDir); 696 llvm::sys::path::append(P, "include"); 697 if (const DirectoryEntry *Dir = HS.getFileMgr().getDirectory(P.str())) 698 HS.getModuleMap().setBuiltinIncludeDir(Dir); 699 } 700 701 Init.Realize(Lang); 702 } 703