InitHeaderSearch.cpp 25 KB

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