Advertisement
Guest User

Untitled

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