InitHeaderSearch.cpp 26 KB

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