InitHeaderSearch.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. #ifdef HAVE_CLANG_CONFIG_H
  14. # include "clang/Config/config.h"
  15. #endif
  16. #include "clang/Frontend/Utils.h"
  17. #include "clang/Basic/FileManager.h"
  18. #include "clang/Basic/LangOptions.h"
  19. #include "clang/Frontend/HeaderSearchOptions.h"
  20. #include "clang/Lex/HeaderSearch.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/ADT/SmallPtrSet.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/StringExtras.h"
  25. #include "llvm/ADT/Triple.h"
  26. #include "llvm/ADT/Twine.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Support/Path.h"
  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. llvm::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. StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
  294. if (CxxIncludeRoot != "") {
  295. StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
  296. if (CxxIncludeArch == "")
  297. AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
  298. CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
  299. triple);
  300. else
  301. AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
  302. CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
  303. triple);
  304. return;
  305. }
  306. // FIXME: temporary hack: hard-coded paths.
  307. if (triple.isOSDarwin()) {
  308. switch (triple.getArch()) {
  309. default: break;
  310. case llvm::Triple::ppc:
  311. case llvm::Triple::ppc64:
  312. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
  313. "powerpc-apple-darwin10", "", "ppc64",
  314. triple);
  315. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
  316. "powerpc-apple-darwin10", "", "ppc64",
  317. triple);
  318. break;
  319. case llvm::Triple::x86:
  320. case llvm::Triple::x86_64:
  321. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
  322. "i686-apple-darwin10", "", "x86_64", triple);
  323. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
  324. "i686-apple-darwin8", "", "", triple);
  325. break;
  326. case llvm::Triple::arm:
  327. case llvm::Triple::thumb:
  328. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
  329. "arm-apple-darwin10", "v7", "", triple);
  330. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
  331. "arm-apple-darwin10", "v6", "", triple);
  332. break;
  333. }
  334. return;
  335. }
  336. switch (os) {
  337. case llvm::Triple::Linux:
  338. case llvm::Triple::Win32:
  339. llvm_unreachable("Include management is handled in the driver.");
  340. case llvm::Triple::Cygwin:
  341. // Cygwin-1.7
  342. AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
  343. // g++-4 / Cygwin-1.5
  344. AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
  345. break;
  346. case llvm::Triple::MinGW32:
  347. // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
  348. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0");
  349. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1");
  350. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2");
  351. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3");
  352. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.4");
  353. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0");
  354. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1");
  355. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2");
  356. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.3");
  357. AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
  358. // mingw.org C++ include paths
  359. AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS
  360. AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
  361. AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
  362. AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
  363. break;
  364. case llvm::Triple::DragonFly:
  365. AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
  366. break;
  367. case llvm::Triple::FreeBSD:
  368. // FreeBSD 8.0
  369. // FreeBSD 7.3
  370. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
  371. break;
  372. case llvm::Triple::NetBSD:
  373. AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
  374. break;
  375. case llvm::Triple::OpenBSD: {
  376. std::string t = triple.getTriple();
  377. if (t.substr(0, 6) == "x86_64")
  378. t.replace(0, 6, "amd64");
  379. AddGnuCPlusPlusIncludePaths("/usr/include/g++",
  380. t, "", "", triple);
  381. break;
  382. }
  383. case llvm::Triple::Minix:
  384. AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
  385. "", "", "", triple);
  386. break;
  387. case llvm::Triple::Solaris:
  388. // Solaris - Fall though..
  389. case llvm::Triple::AuroraUX:
  390. // AuroraUX
  391. AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
  392. "i386-pc-solaris2.11", "", "", triple);
  393. break;
  394. default:
  395. break;
  396. }
  397. }
  398. void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
  399. const llvm::Triple &triple,
  400. const HeaderSearchOptions &HSOpts) {
  401. // NB: This code path is going away. All of the logic is moving into the
  402. // driver which has the information necessary to do target-specific
  403. // selections of default include paths. Each target which moves there will be
  404. // exempted from this logic here until we can delete the entire pile of code.
  405. switch (triple.getOS()) {
  406. default:
  407. break; // Everything else continues to use this routine's logic.
  408. case llvm::Triple::Linux:
  409. case llvm::Triple::Win32:
  410. return;
  411. }
  412. if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes &&
  413. HSOpts.UseStandardSystemIncludes) {
  414. if (HSOpts.UseLibcxx) {
  415. if (triple.isOSDarwin()) {
  416. // On Darwin, libc++ may be installed alongside the compiler in
  417. // lib/c++/v1.
  418. llvm::sys::Path P(HSOpts.ResourceDir);
  419. if (!P.isEmpty()) {
  420. P.eraseComponent(); // Remove version from foo/lib/clang/version
  421. P.eraseComponent(); // Remove clang from foo/lib/clang
  422. // Get foo/lib/c++/v1
  423. P.appendComponent("c++");
  424. P.appendComponent("v1");
  425. AddPath(P.str(), CXXSystem, true, false, false, true);
  426. }
  427. }
  428. AddPath("/usr/include/c++/v1", CXXSystem, true, false, false);
  429. } else {
  430. AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
  431. }
  432. }
  433. AddDefaultCIncludePaths(triple, HSOpts);
  434. // Add the default framework include paths on Darwin.
  435. if (HSOpts.UseStandardSystemIncludes) {
  436. if (triple.isOSDarwin()) {
  437. AddPath("/System/Library/Frameworks", System, true, false, true);
  438. AddPath("/Library/Frameworks", System, true, false, true);
  439. }
  440. }
  441. }
  442. /// RemoveDuplicates - If there are duplicate directory entries in the specified
  443. /// search list, remove the later (dead) ones. Returns the number of non-system
  444. /// headers removed, which is used to update NumAngled.
  445. static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
  446. unsigned First, bool Verbose) {
  447. llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
  448. llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
  449. llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
  450. unsigned NonSystemRemoved = 0;
  451. for (unsigned i = First; i != SearchList.size(); ++i) {
  452. unsigned DirToRemove = i;
  453. const DirectoryLookup &CurEntry = SearchList[i];
  454. if (CurEntry.isNormalDir()) {
  455. // If this isn't the first time we've seen this dir, remove it.
  456. if (SeenDirs.insert(CurEntry.getDir()))
  457. continue;
  458. } else if (CurEntry.isFramework()) {
  459. // If this isn't the first time we've seen this framework dir, remove it.
  460. if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
  461. continue;
  462. } else {
  463. assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
  464. // If this isn't the first time we've seen this headermap, remove it.
  465. if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
  466. continue;
  467. }
  468. // If we have a normal #include dir/framework/headermap that is shadowed
  469. // later in the chain by a system include location, we actually want to
  470. // ignore the user's request and drop the user dir... keeping the system
  471. // dir. This is weird, but required to emulate GCC's search path correctly.
  472. //
  473. // Since dupes of system dirs are rare, just rescan to find the original
  474. // that we're nuking instead of using a DenseMap.
  475. if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
  476. // Find the dir that this is the same of.
  477. unsigned FirstDir;
  478. for (FirstDir = 0; ; ++FirstDir) {
  479. assert(FirstDir != i && "Didn't find dupe?");
  480. const DirectoryLookup &SearchEntry = SearchList[FirstDir];
  481. // If these are different lookup types, then they can't be the dupe.
  482. if (SearchEntry.getLookupType() != CurEntry.getLookupType())
  483. continue;
  484. bool isSame;
  485. if (CurEntry.isNormalDir())
  486. isSame = SearchEntry.getDir() == CurEntry.getDir();
  487. else if (CurEntry.isFramework())
  488. isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
  489. else {
  490. assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
  491. isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
  492. }
  493. if (isSame)
  494. break;
  495. }
  496. // If the first dir in the search path is a non-system dir, zap it
  497. // instead of the system one.
  498. if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
  499. DirToRemove = FirstDir;
  500. }
  501. if (Verbose) {
  502. llvm::errs() << "ignoring duplicate directory \""
  503. << CurEntry.getName() << "\"\n";
  504. if (DirToRemove != i)
  505. llvm::errs() << " as it is a non-system directory that duplicates "
  506. << "a system directory\n";
  507. }
  508. if (DirToRemove != i)
  509. ++NonSystemRemoved;
  510. // This is reached if the current entry is a duplicate. Remove the
  511. // DirToRemove (usually the current dir).
  512. SearchList.erase(SearchList.begin()+DirToRemove);
  513. --i;
  514. }
  515. return NonSystemRemoved;
  516. }
  517. void InitHeaderSearch::Realize(const LangOptions &Lang) {
  518. // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
  519. std::vector<DirectoryLookup> SearchList;
  520. SearchList.reserve(IncludePath.size());
  521. // Quoted arguments go first.
  522. for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
  523. it != ie; ++it) {
  524. if (it->first == Quoted)
  525. SearchList.push_back(it->second);
  526. }
  527. // Deduplicate and remember index.
  528. RemoveDuplicates(SearchList, 0, Verbose);
  529. unsigned NumQuoted = SearchList.size();
  530. for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
  531. it != ie; ++it) {
  532. if (it->first == Angled || it->first == IndexHeaderMap)
  533. SearchList.push_back(it->second);
  534. }
  535. RemoveDuplicates(SearchList, NumQuoted, Verbose);
  536. unsigned NumAngled = SearchList.size();
  537. for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
  538. it != ie; ++it) {
  539. if (it->first == System ||
  540. (!Lang.ObjC1 && !Lang.CPlusPlus && it->first == CSystem) ||
  541. (/*FIXME !Lang.ObjC1 && */Lang.CPlusPlus && it->first == CXXSystem) ||
  542. (Lang.ObjC1 && !Lang.CPlusPlus && it->first == ObjCSystem) ||
  543. (Lang.ObjC1 && Lang.CPlusPlus && it->first == ObjCXXSystem))
  544. SearchList.push_back(it->second);
  545. }
  546. for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
  547. it != ie; ++it) {
  548. if (it->first == After)
  549. SearchList.push_back(it->second);
  550. }
  551. // Remove duplicates across both the Angled and System directories. GCC does
  552. // this and failing to remove duplicates across these two groups breaks
  553. // #include_next.
  554. unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
  555. NumAngled -= NonSystemRemoved;
  556. bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
  557. Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
  558. // If verbose, print the list of directories that will be searched.
  559. if (Verbose) {
  560. llvm::errs() << "#include \"...\" search starts here:\n";
  561. for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
  562. if (i == NumQuoted)
  563. llvm::errs() << "#include <...> search starts here:\n";
  564. const char *Name = SearchList[i].getName();
  565. const char *Suffix;
  566. if (SearchList[i].isNormalDir())
  567. Suffix = "";
  568. else if (SearchList[i].isFramework())
  569. Suffix = " (framework directory)";
  570. else {
  571. assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
  572. Suffix = " (headermap)";
  573. }
  574. llvm::errs() << " " << Name << Suffix << "\n";
  575. }
  576. llvm::errs() << "End of search list.\n";
  577. }
  578. }
  579. void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
  580. const HeaderSearchOptions &HSOpts,
  581. const LangOptions &Lang,
  582. const llvm::Triple &Triple) {
  583. InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
  584. // Add the user defined entries.
  585. for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
  586. const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
  587. Init.AddPath(E.Path, E.Group, !E.ImplicitExternC, E.IsUserSupplied,
  588. E.IsFramework, E.IgnoreSysRoot);
  589. }
  590. Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
  591. Init.Realize(Lang);
  592. }