blob: f595fb1585f413aba682c9cddf625ddadc0f316b [file] [log] [blame]
Nick Lewyckye602efc2010-07-25 03:12:58 +00001//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
Nico Webered9b4102008-08-22 09:25:22 +00002//
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
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000014#include "clang/Frontend/Utils.h"
Nico Webered9b4102008-08-22 09:25:22 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/LangOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Config/config.h" // C_INCLUDE_DIRS
Daniel Dunbar08d5669b2009-11-07 04:20:50 +000018#include "clang/Lex/HeaderSearch.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Lex/HeaderSearchOptions.h"
Nico Webered9b4102008-08-22 09:25:22 +000020#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "llvm/ADT/SmallString.h"
Rafael Espindola46129b02009-11-13 05:13:58 +000022#include "llvm/ADT/SmallVector.h"
Rafael Espindolaf401fa02009-11-12 05:48:41 +000023#include "llvm/ADT/StringExtras.h"
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000024#include "llvm/ADT/Triple.h"
Benjamin Kramerc6ad84c2009-12-08 12:38:20 +000025#include "llvm/ADT/Twine.h"
Chandler Carruthdf527832011-11-04 23:49:05 +000026#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000027#include "llvm/Support/Path.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "llvm/Support/raw_ostream.h"
Dylan Noblesmith86780e92012-02-01 14:25:28 +000029
Nico Webered9b4102008-08-22 09:25:22 +000030using namespace clang;
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000031using namespace clang::frontend;
32
33namespace {
34
35/// InitHeaderSearch - This class makes it easier to set the search paths of
36/// a HeaderSearch object. InitHeaderSearch stores several search path lists
37/// internally, which can be sent to a HeaderSearch object in one swoop.
38class InitHeaderSearch {
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +000039 std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
40 typedef std::vector<std::pair<IncludeDirGroup,
41 DirectoryLookup> >::const_iterator path_iterator;
Richard Smith8acadcb2012-06-13 20:27:03 +000042 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
Chris Lattner0c64f4b2011-06-16 22:56:45 +000043 HeaderSearch &Headers;
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000044 bool Verbose;
Michael J. Spencerbc7fcc22010-12-25 20:09:27 +000045 std::string IncludeSysroot;
46 bool IsNotEmptyOrRoot;
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000047
48public:
49
Chris Lattner0e62c1c2011-07-23 10:55:15 +000050 InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
Michael J. Spencerbc7fcc22010-12-25 20:09:27 +000051 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
52 IsNotEmptyOrRoot(!(sysroot.empty() || sysroot == "/")) {
Chandler Carrutheb842002010-11-15 07:15:26 +000053 }
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000054
55 /// AddPath - Add the specified path to the specified group list.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000056 void AddPath(const Twine &Path, IncludeDirGroup Group,
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +000057 bool isCXXAware, bool isFramework, bool IgnoreSysRoot = false);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000058
Richard Smith8acadcb2012-06-13 20:27:03 +000059 /// AddSystemHeaderPrefix - Add the specified prefix to the system header
60 /// prefix list.
61 void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
62 SystemHeaderPrefixes.push_back(std::make_pair(Prefix, IsSystemHeader));
63 }
64
mike-m6fa3cd22010-05-05 17:00:31 +000065 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000066 /// libstdc++.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000067 void AddGnuCPlusPlusIncludePaths(StringRef Base,
68 StringRef ArchDir,
69 StringRef Dir32,
70 StringRef Dir64,
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000071 const llvm::Triple &triple);
72
Michael J. Spencerbc721822010-12-21 16:45:42 +000073 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000074 /// libstdc++.
Aaron Ballmaneceaddc2012-03-25 22:46:17 +000075 void AddMinGWCPlusPlusIncludePaths(StringRef Base,
76 StringRef Arch,
77 StringRef Version);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000078
NAKAMURA Takumi63b4b452011-03-15 02:32:36 +000079 /// AddMinGW64CXXPaths - Add the necessary paths to support
80 /// libstdc++ of x86_64-w64-mingw32 aka mingw-w64.
Aaron Ballmaneceaddc2012-03-25 22:46:17 +000081 void AddMinGW64CXXPaths(StringRef Base,
82 StringRef Version);
NAKAMURA Takumi63b4b452011-03-15 02:32:36 +000083
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000084 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-mc6da2612010-05-16 19:03:52 +000085 void AddDefaultCIncludePaths(const llvm::Triple &triple,
86 const HeaderSearchOptions &HSOpts);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000087
88 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
89 // compiling c++.
Douglas Gregor7a200962011-06-27 15:47:15 +000090 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple,
91 const HeaderSearchOptions &HSOpts);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000092
93 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
94 /// that e.g. stdio.h is found.
Daniel Dunbarb25bfde2011-10-11 18:20:10 +000095 void AddDefaultIncludePaths(const LangOptions &Lang,
96 const llvm::Triple &triple,
97 const HeaderSearchOptions &HSOpts);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000098
99 /// Realize - Merges all search path lists into one list and send it to
100 /// HeaderSearch.
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000101 void Realize(const LangOptions &Lang);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +0000102};
103
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000104} // end anonymous namespace.
Nico Webered9b4102008-08-22 09:25:22 +0000105
Daniel Dunbar2e8eb012013-01-29 23:59:37 +0000106static bool CanPrefixSysroot(StringRef Path) {
107#if defined(_WIN32)
108 return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
109#else
110 return llvm::sys::path::is_absolute(Path);
111#endif
112}
113
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000114void InitHeaderSearch::AddPath(const Twine &Path,
Benjamin Kramer26db6482009-09-05 09:49:39 +0000115 IncludeDirGroup Group, bool isCXXAware,
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000116 bool isFramework, bool IgnoreSysRoot) {
Benjamin Kramerc6ad84c2009-12-08 12:38:20 +0000117 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Webered9b4102008-08-22 09:25:22 +0000118 FileManager &FM = Headers.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000119
Nico Webered9b4102008-08-22 09:25:22 +0000120 // Compute the actual path, taking into consideration -isysroot.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000121 SmallString<256> MappedPathStorage;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000122 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump11289f42009-09-09 15:08:12 +0000123
Nico Webered9b4102008-08-22 09:25:22 +0000124 // Handle isysroot.
Rafael Espindolac4eb31e52011-03-02 21:30:07 +0000125 if ((Group == System || Group == CXXSystem) && !IgnoreSysRoot &&
Daniel Dunbar2e8eb012013-01-29 23:59:37 +0000126 CanPrefixSysroot(MappedPathStr) && IsNotEmptyOrRoot) {
Chandler Carruth3b702672010-11-15 09:28:23 +0000127 MappedPathStorage.clear();
Daniel Dunbar2e8eb012013-01-29 23:59:37 +0000128 MappedPathStr = (IncludeSysroot + Path).toStringRef(MappedPathStorage);
Nico Webered9b4102008-08-22 09:25:22 +0000129 }
Mike Stump11289f42009-09-09 15:08:12 +0000130
Nico Webered9b4102008-08-22 09:25:22 +0000131 // Compute the DirectoryLookup type.
Chris Lattner66a740e2008-10-27 01:19:25 +0000132 SrcMgr::CharacteristicKind Type;
Douglas Gregor9f93e382011-07-28 04:45:53 +0000133 if (Group == Quoted || Group == Angled || Group == IndexHeaderMap)
Chris Lattnerb03dc762008-09-26 21:18:42 +0000134 Type = SrcMgr::C_User;
Nico Webered9b4102008-08-22 09:25:22 +0000135 else if (isCXXAware)
Chris Lattnerb03dc762008-09-26 21:18:42 +0000136 Type = SrcMgr::C_System;
Nico Webered9b4102008-08-22 09:25:22 +0000137 else
Chris Lattnerb03dc762008-09-26 21:18:42 +0000138 Type = SrcMgr::C_ExternCSystem;
Mike Stump11289f42009-09-09 15:08:12 +0000139
140
Nico Webered9b4102008-08-22 09:25:22 +0000141 // If the directory exists, add it.
Chris Lattner5159f612010-11-23 08:35:12 +0000142 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Daniel Dunbarae4feb62013-01-25 01:50:28 +0000143 IncludePath.push_back(
144 std::make_pair(Group, DirectoryLookup(DE, Type, isFramework)));
Nico Webered9b4102008-08-22 09:25:22 +0000145 return;
146 }
Mike Stump11289f42009-09-09 15:08:12 +0000147
Nico Webered9b4102008-08-22 09:25:22 +0000148 // Check to see if this is an apple-style headermap (which are not allowed to
149 // be frameworks).
150 if (!isFramework) {
Chris Lattner5159f612010-11-23 08:35:12 +0000151 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Webered9b4102008-08-22 09:25:22 +0000152 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
153 // It is a headermap, add it to the search path.
Daniel Dunbarae4feb62013-01-25 01:50:28 +0000154 IncludePath.push_back(
155 std::make_pair(Group,
156 DirectoryLookup(HM, Type, Group == IndexHeaderMap)));
Nico Webered9b4102008-08-22 09:25:22 +0000157 return;
158 }
159 }
160 }
Mike Stump11289f42009-09-09 15:08:12 +0000161
Nico Webered9b4102008-08-22 09:25:22 +0000162 if (Verbose)
Chandler Carruth03ac1b02010-11-15 00:48:13 +0000163 llvm::errs() << "ignoring nonexistent directory \""
164 << MappedPathStr << "\"\n";
Nico Webered9b4102008-08-22 09:25:22 +0000165}
166
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000167void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
168 StringRef ArchDir,
169 StringRef Dir32,
170 StringRef Dir64,
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000171 const llvm::Triple &triple) {
Rafael Espindola962e5182009-11-23 16:31:19 +0000172 // Add the base dir
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000173 AddPath(Base, CXXSystem, true, false);
Rafael Espindolaabab8792009-11-16 19:49:37 +0000174
175 // Add the multilib dirs
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000176 llvm::Triple::ArchType arch = triple.getArch();
177 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000178 if (is64bit)
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000179 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, true, false);
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000180 else
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000181 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, true, false);
Rafael Espindola962e5182009-11-23 16:31:19 +0000182
183 // Add the backward dir
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000184 AddPath(Base + "/backward", CXXSystem, true, false);
Rafael Espindolac3031a92009-10-06 01:33:02 +0000185}
Nico Webered9b4102008-08-22 09:25:22 +0000186
Aaron Ballman9345d682012-03-25 15:47:41 +0000187void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000188 StringRef Arch,
189 StringRef Version) {
190 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000191 CXXSystem, true, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000192 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000193 CXXSystem, true, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000194 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000195 CXXSystem, true, false);
Aaron Ballman9345d682012-03-25 15:47:41 +0000196}
197
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000198void InitHeaderSearch::AddMinGW64CXXPaths(StringRef Base,
199 StringRef Version) {
Douglas Gregor7a200962011-06-27 15:47:15 +0000200 // Assumes Base is HeaderSearchOpts' ResourceDir
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000201 AddPath(Base + "/../../../include/c++/" + Version,
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000202 CXXSystem, true, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000203 AddPath(Base + "/../../../include/c++/" + Version + "/x86_64-w64-mingw32",
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000204 CXXSystem, true, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000205 AddPath(Base + "/../../../include/c++/" + Version + "/i686-w64-mingw32",
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000206 CXXSystem, true, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000207 AddPath(Base + "/../../../include/c++/" + Version + "/backward",
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000208 CXXSystem, true, false);
NAKAMURA Takumi63b4b452011-03-15 02:32:36 +0000209}
210
mike-mc6da2612010-05-16 19:03:52 +0000211void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
212 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000213 llvm::Triple::OSType os = triple.getOS();
214
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000215 if (HSOpts.UseStandardSystemIncludes) {
216 switch (os) {
217 case llvm::Triple::FreeBSD:
218 case llvm::Triple::NetBSD:
Hans Wennborgae298f22012-08-02 12:27:08 +0000219 case llvm::Triple::OpenBSD:
Eli Friedman9fa28852012-08-08 23:57:20 +0000220 case llvm::Triple::Bitrig:
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000221 break;
222 default:
223 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000224 AddPath("/usr/local/include", System, true, false);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000225 break;
226 }
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000227 }
mike-mc6da2612010-05-16 19:03:52 +0000228
229 // Builtin includes use #include_next directives and should be positioned
230 // just prior C include dirs.
231 if (HSOpts.UseBuiltinIncludes) {
232 // Ignore the sys root, we *always* look for clang headers relative to
233 // supplied path.
234 llvm::sys::Path P(HSOpts.ResourceDir);
235 P.appendComponent("include");
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000236 AddPath(P.str(), System, false, false, /*IgnoreSysRoot=*/ true);
mike-mc6da2612010-05-16 19:03:52 +0000237 }
238
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000239 // All remaining additions are for system include directories, early exit if
240 // we aren't using them.
241 if (!HSOpts.UseStandardSystemIncludes)
242 return;
243
mike-mc6da2612010-05-16 19:03:52 +0000244 // Add dirs specified via 'configure --with-c-include-dirs'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000245 StringRef CIncludeDirs(C_INCLUDE_DIRS);
Daniel Dunbar71ed08b2009-11-12 07:28:29 +0000246 if (CIncludeDirs != "") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000247 SmallVector<StringRef, 5> dirs;
Rafael Espindola46129b02009-11-13 05:13:58 +0000248 CIncludeDirs.split(dirs, ":");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000249 for (SmallVectorImpl<StringRef>::iterator i = dirs.begin();
Rafael Espindola46129b02009-11-13 05:13:58 +0000250 i != dirs.end();
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000251 ++i)
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000252 AddPath(*i, System, false, false);
Rafael Espindolaf401fa02009-11-12 05:48:41 +0000253 return;
254 }
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000255
Mike Stump904ad902009-10-08 23:29:47 +0000256 switch (os) {
Chandler Carrutha796f532011-11-05 20:17:13 +0000257 case llvm::Triple::Linux:
Chandler Carruthdf527832011-11-04 23:49:05 +0000258 case llvm::Triple::Win32:
Chandler Carrutha796f532011-11-05 20:17:13 +0000259 llvm_unreachable("Include management is handled in the driver.");
Chandler Carruthdf527832011-11-04 23:49:05 +0000260
Chris Lattnerb986aba2010-04-11 19:29:39 +0000261 case llvm::Triple::Haiku:
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000262 AddPath("/boot/common/include", System, true, false);
263 AddPath("/boot/develop/headers/os", System, true, false);
264 AddPath("/boot/develop/headers/os/app", System, true, false);
265 AddPath("/boot/develop/headers/os/arch", System, true, false);
266 AddPath("/boot/develop/headers/os/device", System, true, false);
267 AddPath("/boot/develop/headers/os/drivers", System, true, false);
268 AddPath("/boot/develop/headers/os/game", System, true, false);
269 AddPath("/boot/develop/headers/os/interface", System, true, false);
270 AddPath("/boot/develop/headers/os/kernel", System, true, false);
271 AddPath("/boot/develop/headers/os/locale", System, true, false);
272 AddPath("/boot/develop/headers/os/mail", System, true, false);
273 AddPath("/boot/develop/headers/os/media", System, true, false);
274 AddPath("/boot/develop/headers/os/midi", System, true, false);
275 AddPath("/boot/develop/headers/os/midi2", System, true, false);
276 AddPath("/boot/develop/headers/os/net", System, true, false);
277 AddPath("/boot/develop/headers/os/storage", System, true, false);
278 AddPath("/boot/develop/headers/os/support", System, true, false);
279 AddPath("/boot/develop/headers/os/translation", System, true, false);
280 AddPath("/boot/develop/headers/os/add-ons/graphics", System, true, false);
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000281 AddPath("/boot/develop/headers/os/add-ons/input_server",
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000282 System, true, false);
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000283 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000284 System, true, false);
285 AddPath("/boot/develop/headers/os/add-ons/tracker", System, true, false);
286 AddPath("/boot/develop/headers/os/be_apps/Deskbar", System, true, false);
Chris Lattnerb986aba2010-04-11 19:29:39 +0000287 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000288 System, true, false);
289 AddPath("/boot/develop/headers/os/be_apps/Tracker", System, true, false);
290 AddPath("/boot/develop/headers/cpp", System, true, false);
291 AddPath("/boot/develop/headers/cpp/i586-pc-haiku", System, true, false);
292 AddPath("/boot/develop/headers/3rdparty", System, true, false);
293 AddPath("/boot/develop/headers/bsd", System, true, false);
294 AddPath("/boot/develop/headers/glibc", System, true, false);
295 AddPath("/boot/develop/headers/posix", System, true, false);
296 AddPath("/boot/develop/headers", System, true, false);
Eli Friedman04831922010-08-22 01:00:03 +0000297 break;
Douglas Gregor9fabd852011-07-01 22:41:14 +0000298 case llvm::Triple::RTEMS:
299 break;
NAKAMURA Takumie4faafb2010-10-11 02:27:37 +0000300 case llvm::Triple::Cygwin:
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000301 AddPath("/usr/include/w32api", System, true, false);
NAKAMURA Takumie4faafb2010-10-11 02:27:37 +0000302 break;
Douglas Gregor7a200962011-06-27 15:47:15 +0000303 case llvm::Triple::MinGW32: {
304 // mingw-w64 crt include paths
305 llvm::sys::Path P(HSOpts.ResourceDir);
306 P.appendComponent("../../../i686-w64-mingw32/include"); // <sysroot>/i686-w64-mingw32/include
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000307 AddPath(P.str(), System, true, false);
Douglas Gregor7a200962011-06-27 15:47:15 +0000308 P = llvm::sys::Path(HSOpts.ResourceDir);
309 P.appendComponent("../../../x86_64-w64-mingw32/include"); // <sysroot>/x86_64-w64-mingw32/include
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000310 AddPath(P.str(), System, true, false);
Douglas Gregor7a200962011-06-27 15:47:15 +0000311 // mingw.org crt include paths
312 P = llvm::sys::Path(HSOpts.ResourceDir);
313 P.appendComponent("../../../include"); // <sysroot>/include
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000314 AddPath(P.str(), System, true, false);
315 AddPath("/mingw/include", System, true, false);
NAKAMURA Takumi4a7a5612012-09-13 05:53:23 +0000316#if defined(_WIN32)
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000317 AddPath("c:/mingw/include", System, true, false);
NAKAMURA Takumi4a7a5612012-09-13 05:53:23 +0000318#endif
Douglas Gregor7a200962011-06-27 15:47:15 +0000319 }
Mike Stump904ad902009-10-08 23:29:47 +0000320 break;
Douglas Gregor7a200962011-06-27 15:47:15 +0000321
Mike Stump904ad902009-10-08 23:29:47 +0000322 default:
Mike Stump904ad902009-10-08 23:29:47 +0000323 break;
324 }
John Thompson4334ce62009-10-13 18:51:32 +0000325
Douglas Gregor9fabd852011-07-01 22:41:14 +0000326 if ( os != llvm::Triple::RTEMS )
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000327 AddPath("/usr/include", System, false, false);
Rafael Espindola177f1d92009-10-27 14:47:31 +0000328}
329
Chris Lattner5561bf32010-05-05 05:28:39 +0000330void InitHeaderSearch::
Douglas Gregor7a200962011-06-27 15:47:15 +0000331AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
Rafael Espindola177f1d92009-10-27 14:47:31 +0000332 llvm::Triple::OSType os = triple.getOS();
333 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000334
335 if (triple.isOSDarwin()) {
Daniel Dunbareaff5fa2010-05-28 01:54:31 +0000336 switch (triple.getArch()) {
337 default: break;
338
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000339 case llvm::Triple::ppc:
Douglas Gregoreb0bdf02010-05-29 01:15:12 +0000340 case llvm::Triple::ppc64:
Douglas Gregor117ef272010-05-29 01:21:11 +0000341 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000342 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor117ef272010-05-29 01:21:11 +0000343 triple);
Douglas Gregoreb0bdf02010-05-29 01:15:12 +0000344 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000345 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregoreb0bdf02010-05-29 01:15:12 +0000346 triple);
347 break;
348
Daniel Dunbareaff5fa2010-05-28 01:54:31 +0000349 case llvm::Triple::x86:
350 case llvm::Triple::x86_64:
351 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
352 "i686-apple-darwin10", "", "x86_64", triple);
353 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
354 "i686-apple-darwin8", "", "", triple);
355 break;
356
357 case llvm::Triple::arm:
358 case llvm::Triple::thumb:
359 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
360 "arm-apple-darwin10", "v7", "", triple);
361 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
362 "arm-apple-darwin10", "v6", "", triple);
363 break;
364 }
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000365 return;
366 }
367
368 switch (os) {
Chandler Carrutha796f532011-11-05 20:17:13 +0000369 case llvm::Triple::Linux:
370 case llvm::Triple::Win32:
371 llvm_unreachable("Include management is handled in the driver.");
372
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000373 case llvm::Triple::Cygwin:
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000374 // Cygwin-1.7
375 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3");
376 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
377 // g++-4 / Cygwin-1.5
378 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000379 break;
380 case llvm::Triple::MinGW32:
Douglas Gregor7a200962011-06-27 15:47:15 +0000381 // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000382 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0");
383 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1");
384 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2");
385 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3");
386 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.4");
387 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0");
388 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1");
389 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2");
390 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.3");
391 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
Douglas Gregor7a200962011-06-27 15:47:15 +0000392 // mingw.org C++ include paths
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000393 AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS
NAKAMURA Takumi4a7a5612012-09-13 05:53:23 +0000394#if defined(_WIN32)
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000395 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.2");
396 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.1");
397 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.2");
398 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
399 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
400 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
NAKAMURA Takumi4a7a5612012-09-13 05:53:23 +0000401#endif
Rafael Espindola177f1d92009-10-27 14:47:31 +0000402 break;
Chris Lattner002ba6b2010-01-09 05:41:14 +0000403 case llvm::Triple::DragonFly:
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000404 AddPath("/usr/include/c++/4.1", CXXSystem, true, false);
Chris Lattner002ba6b2010-01-09 05:41:14 +0000405 break;
Rafael Espindola177f1d92009-10-27 14:47:31 +0000406 case llvm::Triple::FreeBSD:
mike-m94ab2902010-05-06 14:11:13 +0000407 // FreeBSD 8.0
408 // FreeBSD 7.3
Nuno Lopes28b40dd2010-01-17 00:00:11 +0000409 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindola177f1d92009-10-27 14:47:31 +0000410 break;
Anton Korobeynikov1b80c2a2010-08-31 22:39:50 +0000411 case llvm::Triple::NetBSD:
412 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
413 break;
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000414 case llvm::Triple::OpenBSD: {
415 std::string t = triple.getTriple();
416 if (t.substr(0, 6) == "x86_64")
417 t.replace(0, 6, "amd64");
418 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
419 t, "", "", triple);
420 break;
421 }
Chris Lattner3e2ee142010-07-07 16:01:42 +0000422 case llvm::Triple::Minix:
423 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
424 "", "", "", triple);
425 break;
Rafael Espindola177f1d92009-10-27 14:47:31 +0000426 case llvm::Triple::Solaris:
David Chisnall1026fb02012-02-15 18:24:31 +0000427 AddGnuCPlusPlusIncludePaths("/usr/gcc/4.5/include/c++/4.5.2/",
428 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindola177f1d92009-10-27 14:47:31 +0000429 // Solaris - Fall though..
430 case llvm::Triple::AuroraUX:
431 // AuroraUX
432 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaabab8792009-11-16 19:49:37 +0000433 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindola177f1d92009-10-27 14:47:31 +0000434 break;
435 default:
436 break;
437 }
438}
439
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000440void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
441 const llvm::Triple &triple,
mike-mc6da2612010-05-16 19:03:52 +0000442 const HeaderSearchOptions &HSOpts) {
Chandler Carruthdf527832011-11-04 23:49:05 +0000443 // NB: This code path is going away. All of the logic is moving into the
444 // driver which has the information necessary to do target-specific
445 // selections of default include paths. Each target which moves there will be
446 // exempted from this logic here until we can delete the entire pile of code.
447 switch (triple.getOS()) {
448 default:
449 break; // Everything else continues to use this routine's logic.
450
Chandler Carrutha796f532011-11-05 20:17:13 +0000451 case llvm::Triple::Linux:
Chandler Carruthdf527832011-11-04 23:49:05 +0000452 case llvm::Triple::Win32:
453 return;
454 }
455
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000456 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes &&
457 HSOpts.UseStandardSystemIncludes) {
Douglas Gregorf4016fd2011-07-29 20:21:18 +0000458 if (HSOpts.UseLibcxx) {
459 if (triple.isOSDarwin()) {
460 // On Darwin, libc++ may be installed alongside the compiler in
461 // lib/c++/v1.
462 llvm::sys::Path P(HSOpts.ResourceDir);
463 if (!P.isEmpty()) {
464 P.eraseComponent(); // Remove version from foo/lib/clang/version
465 P.eraseComponent(); // Remove clang from foo/lib/clang
466
467 // Get foo/lib/c++/v1
468 P.appendComponent("c++");
469 P.appendComponent("v1");
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000470 AddPath(P.str(), CXXSystem, true, false, true);
Douglas Gregorf4016fd2011-07-29 20:21:18 +0000471 }
472 }
David Chisnalle04307e2012-03-02 10:49:52 +0000473 // On Solaris, include the support directory for things like xlocale and
474 // fudged system headers.
475 if (triple.getOS() == llvm::Triple::Solaris)
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000476 AddPath("/usr/include/c++/v1/support/solaris", CXXSystem, true, false);
Douglas Gregorf4016fd2011-07-29 20:21:18 +0000477
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000478 AddPath("/usr/include/c++/v1", CXXSystem, true, false);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000479 } else {
Douglas Gregor7a200962011-06-27 15:47:15 +0000480 AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000481 }
Bob Wilsonb02ea3d2011-06-21 21:12:29 +0000482 }
Rafael Espindola962e5182009-11-23 16:31:19 +0000483
mike-mc6da2612010-05-16 19:03:52 +0000484 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbarec879912009-11-07 04:20:39 +0000485
486 // Add the default framework include paths on Darwin.
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000487 if (HSOpts.UseStandardSystemIncludes) {
488 if (triple.isOSDarwin()) {
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000489 AddPath("/System/Library/Frameworks", System, true, true);
490 AddPath("/Library/Frameworks", System, true, true);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000491 }
Daniel Dunbarec879912009-11-07 04:20:39 +0000492 }
Rafael Espindola177f1d92009-10-27 14:47:31 +0000493}
494
Nico Webered9b4102008-08-22 09:25:22 +0000495/// RemoveDuplicates - If there are duplicate directory entries in the specified
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000496/// search list, remove the later (dead) ones. Returns the number of non-system
497/// headers removed, which is used to update NumAngled.
498static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
499 unsigned First, bool Verbose) {
Nico Webered9b4102008-08-22 09:25:22 +0000500 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
501 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
502 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000503 unsigned NonSystemRemoved = 0;
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000504 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattnere744d322008-09-26 17:46:45 +0000505 unsigned DirToRemove = i;
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattner24c911e2009-02-08 01:00:10 +0000507 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump11289f42009-09-09 15:08:12 +0000508
Chris Lattner24c911e2009-02-08 01:00:10 +0000509 if (CurEntry.isNormalDir()) {
Nico Webered9b4102008-08-22 09:25:22 +0000510 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner24c911e2009-02-08 01:00:10 +0000511 if (SeenDirs.insert(CurEntry.getDir()))
Nico Webered9b4102008-08-22 09:25:22 +0000512 continue;
Chris Lattner24c911e2009-02-08 01:00:10 +0000513 } else if (CurEntry.isFramework()) {
Nico Webered9b4102008-08-22 09:25:22 +0000514 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner24c911e2009-02-08 01:00:10 +0000515 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Webered9b4102008-08-22 09:25:22 +0000516 continue;
Nico Webered9b4102008-08-22 09:25:22 +0000517 } else {
Chris Lattner24c911e2009-02-08 01:00:10 +0000518 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Webered9b4102008-08-22 09:25:22 +0000519 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner24c911e2009-02-08 01:00:10 +0000520 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Webered9b4102008-08-22 09:25:22 +0000521 continue;
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000522 }
Mike Stump11289f42009-09-09 15:08:12 +0000523
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000524 // If we have a normal #include dir/framework/headermap that is shadowed
525 // later in the chain by a system include location, we actually want to
526 // ignore the user's request and drop the user dir... keeping the system
527 // dir. This is weird, but required to emulate GCC's search path correctly.
528 //
529 // Since dupes of system dirs are rare, just rescan to find the original
530 // that we're nuking instead of using a DenseMap.
Chris Lattner24c911e2009-02-08 01:00:10 +0000531 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000532 // Find the dir that this is the same of.
533 unsigned FirstDir;
534 for (FirstDir = 0; ; ++FirstDir) {
535 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattner24c911e2009-02-08 01:00:10 +0000537 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
538
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000539 // If these are different lookup types, then they can't be the dupe.
Chris Lattner24c911e2009-02-08 01:00:10 +0000540 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000541 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000542
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000543 bool isSame;
Chris Lattner24c911e2009-02-08 01:00:10 +0000544 if (CurEntry.isNormalDir())
545 isSame = SearchEntry.getDir() == CurEntry.getDir();
546 else if (CurEntry.isFramework())
547 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000548 else {
Chris Lattner24c911e2009-02-08 01:00:10 +0000549 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
550 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000551 }
Mike Stump11289f42009-09-09 15:08:12 +0000552
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000553 if (isSame)
554 break;
555 }
Mike Stump11289f42009-09-09 15:08:12 +0000556
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000557 // If the first dir in the search path is a non-system dir, zap it
558 // instead of the system one.
559 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
560 DirToRemove = FirstDir;
561 }
562
563 if (Verbose) {
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000564 llvm::errs() << "ignoring duplicate directory \""
565 << CurEntry.getName() << "\"\n";
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000566 if (DirToRemove != i)
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000567 llvm::errs() << " as it is a non-system directory that duplicates "
568 << "a system directory\n";
Nico Webered9b4102008-08-22 09:25:22 +0000569 }
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000570 if (DirToRemove != i)
571 ++NonSystemRemoved;
Mike Stump11289f42009-09-09 15:08:12 +0000572
Chris Lattnere744d322008-09-26 17:46:45 +0000573 // This is reached if the current entry is a duplicate. Remove the
574 // DirToRemove (usually the current dir).
575 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Webered9b4102008-08-22 09:25:22 +0000576 --i;
577 }
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000578 return NonSystemRemoved;
Nico Webered9b4102008-08-22 09:25:22 +0000579}
580
581
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000582void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Webered9b4102008-08-22 09:25:22 +0000583 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
584 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000585 SearchList.reserve(IncludePath.size());
Mike Stump11289f42009-09-09 15:08:12 +0000586
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000587 // Quoted arguments go first.
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000588 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
589 it != ie; ++it) {
590 if (it->first == Quoted)
591 SearchList.push_back(it->second);
592 }
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000593 // Deduplicate and remember index.
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000594 RemoveDuplicates(SearchList, 0, Verbose);
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000595 unsigned NumQuoted = SearchList.size();
Mike Stump11289f42009-09-09 15:08:12 +0000596
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000597 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
598 it != ie; ++it) {
Douglas Gregor9f93e382011-07-28 04:45:53 +0000599 if (it->first == Angled || it->first == IndexHeaderMap)
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000600 SearchList.push_back(it->second);
601 }
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000602
603 RemoveDuplicates(SearchList, NumQuoted, Verbose);
604 unsigned NumAngled = SearchList.size();
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000605
606 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
607 it != ie; ++it) {
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000608 if (it->first == System ||
609 (!Lang.ObjC1 && !Lang.CPlusPlus && it->first == CSystem) ||
Benjamin Kramer559865c2011-09-23 02:25:14 +0000610 (/*FIXME !Lang.ObjC1 && */Lang.CPlusPlus && it->first == CXXSystem) ||
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000611 (Lang.ObjC1 && !Lang.CPlusPlus && it->first == ObjCSystem) ||
612 (Lang.ObjC1 && Lang.CPlusPlus && it->first == ObjCXXSystem))
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000613 SearchList.push_back(it->second);
614 }
615
616 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
617 it != ie; ++it) {
618 if (it->first == After)
619 SearchList.push_back(it->second);
620 }
621
Chris Lattner705c5c82011-06-16 22:58:10 +0000622 // Remove duplicates across both the Angled and System directories. GCC does
623 // this and failing to remove duplicates across these two groups breaks
624 // #include_next.
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000625 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
626 NumAngled -= NonSystemRemoved;
Nico Webered9b4102008-08-22 09:25:22 +0000627
628 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000629 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
Nico Webered9b4102008-08-22 09:25:22 +0000630
Richard Smith8acadcb2012-06-13 20:27:03 +0000631 Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
632
Nico Webered9b4102008-08-22 09:25:22 +0000633 // If verbose, print the list of directories that will be searched.
634 if (Verbose) {
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000635 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Webered9b4102008-08-22 09:25:22 +0000636 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000637 if (i == NumQuoted)
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000638 llvm::errs() << "#include <...> search starts here:\n";
Nico Webered9b4102008-08-22 09:25:22 +0000639 const char *Name = SearchList[i].getName();
640 const char *Suffix;
641 if (SearchList[i].isNormalDir())
642 Suffix = "";
643 else if (SearchList[i].isFramework())
644 Suffix = " (framework directory)";
645 else {
646 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
647 Suffix = " (headermap)";
648 }
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000649 llvm::errs() << " " << Name << Suffix << "\n";
Nico Webered9b4102008-08-22 09:25:22 +0000650 }
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000651 llvm::errs() << "End of search list.\n";
Nico Webered9b4102008-08-22 09:25:22 +0000652 }
653}
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000654
Daniel Dunbar0c6c9302009-11-11 21:44:21 +0000655void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
656 const HeaderSearchOptions &HSOpts,
657 const LangOptions &Lang,
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000658 const llvm::Triple &Triple) {
659 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
660
661 // Add the user defined entries.
662 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
663 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar6b04f1d2013-01-25 01:50:39 +0000664 Init.AddPath(E.Path, E.Group, !E.ImplicitExternC, E.IsFramework,
665 E.IgnoreSysRoot);
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000666 }
667
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000668 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000669
Richard Smith8acadcb2012-06-13 20:27:03 +0000670 for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
671 Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix,
672 HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
673
Douglas Gregor3ec66632012-02-02 18:42:48 +0000674 if (HSOpts.UseBuiltinIncludes) {
675 // Set up the builtin include directory in the module map.
676 llvm::sys::Path P(HSOpts.ResourceDir);
677 P.appendComponent("include");
678 if (const DirectoryEntry *Dir = HS.getFileMgr().getDirectory(P.str()))
679 HS.getModuleMap().setBuiltinIncludeDir(Dir);
680 }
681
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000682 Init.Realize(Lang);
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000683}