InitHeaderSearch.cpp 26 KB

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