InitHeaderSearch.cpp 27 KB

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