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