InitHeaderSearch.cpp 25 KB

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