InitHeaderSearch.cpp 24 KB

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