InitHeaderSearch.cpp 24 KB

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