InitHeaderSearch.cpp 27 KB

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