blob: 5e89c8c4d302a0dc10c63376faca7c976885371b [file] [log] [blame]
Nick Lewyckydf22c2c2010-07-25 03:12:58 +00001//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
Nico Weber0fca0222008-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 Dunbar2cdafa82009-11-09 23:02:47 +000014#include "clang/Frontend/Utils.h"
Nico Weber0fca0222008-08-22 09:25:22 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/LangOptions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Config/config.h" // C_INCLUDE_DIRS
Daniel Dunbar63c8b772009-11-07 04:20:50 +000018#include "clang/Lex/HeaderSearch.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Lex/HeaderSearchOptions.h"
Nico Weber0fca0222008-08-22 09:25:22 +000020#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "llvm/ADT/SmallString.h"
Rafael Espindolaaadd7a42009-11-13 05:13:58 +000022#include "llvm/ADT/SmallVector.h"
Rafael Espindolaf0a2f512009-11-12 05:48:41 +000023#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000024#include "llvm/ADT/Triple.h"
Benjamin Kramere89ba592009-12-08 12:38:20 +000025#include "llvm/ADT/Twine.h"
Chandler Carruthca234192011-11-04 23:49:05 +000026#include "llvm/Support/ErrorHandling.h"
John McCall8cfb7202013-04-11 22:55:55 +000027#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000028#include "llvm/Support/Path.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000029#include "llvm/Support/raw_ostream.h"
Dylan Noblesmith69d3b4f2012-02-01 14:25:28 +000030
Nico Weber0fca0222008-08-22 09:25:22 +000031using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000032using namespace clang::frontend;
33
34namespace {
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.
39class InitHeaderSearch {
Joerg Sonnenberger2df66472011-02-22 00:40:56 +000040 std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
41 typedef std::vector<std::pair<IncludeDirGroup,
42 DirectoryLookup> >::const_iterator path_iterator;
Richard Smithf122a132012-06-13 20:27:03 +000043 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
Chris Lattnerebb61642011-06-16 22:56:45 +000044 HeaderSearch &Headers;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000045 bool Verbose;
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000046 std::string IncludeSysroot;
Daniel Dunbarf85541c2013-01-29 23:59:43 +000047 bool HasSysroot;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000048
49public:
50
Chris Lattner5f9e2722011-07-23 10:55:15 +000051 InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000052 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
Daniel Dunbarf85541c2013-01-29 23:59:43 +000053 HasSysroot(!(sysroot.empty() || sysroot == "/")) {
Chandler Carruthc09265a2010-11-15 07:15:26 +000054 }
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000055
Daniel Dunbar9cd90a22013-01-30 01:06:03 +000056 /// 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);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000064
Richard Smithf122a132012-06-13 20:27:03 +000065 /// 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
mike-ma6087372010-05-05 17:00:31 +000071 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000072 /// libstdc++.
Chris Lattner5f9e2722011-07-23 10:55:15 +000073 void AddGnuCPlusPlusIncludePaths(StringRef Base,
74 StringRef ArchDir,
75 StringRef Dir32,
76 StringRef Dir64,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000077 const llvm::Triple &triple);
78
Michael J. Spencer06a8dc62010-12-21 16:45:42 +000079 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000080 /// libstdc++.
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +000081 void AddMinGWCPlusPlusIncludePaths(StringRef Base,
82 StringRef Arch,
83 StringRef Version);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000084
NAKAMURA Takumi9db48462011-03-15 02:32:36 +000085 /// AddMinGW64CXXPaths - Add the necessary paths to support
86 /// libstdc++ of x86_64-w64-mingw32 aka mingw-w64.
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +000087 void AddMinGW64CXXPaths(StringRef Base,
88 StringRef Version);
NAKAMURA Takumi9db48462011-03-15 02:32:36 +000089
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000090 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-m79bc57c2010-05-16 19:03:52 +000091 void AddDefaultCIncludePaths(const llvm::Triple &triple,
92 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000093
94 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
95 // compiling c++.
Douglas Gregord944a9b2011-06-27 15:47:15 +000096 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple,
97 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000098
99 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
100 /// that e.g. stdio.h is found.
Daniel Dunbara268fc02011-10-11 18:20:10 +0000101 void AddDefaultIncludePaths(const LangOptions &Lang,
102 const llvm::Triple &triple,
103 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000104
105 /// Realize - Merges all search path lists into one list and send it to
106 /// HeaderSearch.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000107 void Realize(const LangOptions &Lang);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000108};
109
Chris Lattnerebb61642011-06-16 22:56:45 +0000110} // end anonymous namespace.
Nico Weber0fca0222008-08-22 09:25:22 +0000111
Daniel Dunbar49ffaef2013-01-29 23:59:37 +0000112static bool CanPrefixSysroot(StringRef Path) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700113#if defined(LLVM_ON_WIN32)
Daniel Dunbar49ffaef2013-01-29 23:59:37 +0000114 return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
115#else
116 return llvm::sys::path::is_absolute(Path);
117#endif
118}
119
Daniel Dunbaref845542013-01-30 00:19:24 +0000120void InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group,
Daniel Dunbar9cd90a22013-01-30 01:06:03 +0000121 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Daniel Dunbar9cd90a22013-01-30 01:06:03 +0000133 AddUnmappedPath(Path, Group, isFramework);
134}
135
136void 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();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000141 SmallString<256> MappedPathStorage;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Nico Weber0fca0222008-08-22 09:25:22 +0000144 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000145 SrcMgr::CharacteristicKind Type;
Daniel Dunbaref845542013-01-30 00:19:24 +0000146 if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
Chris Lattner0b9e7362008-09-26 21:18:42 +0000147 Type = SrcMgr::C_User;
Daniel Dunbaref845542013-01-30 00:19:24 +0000148 } else if (Group == ExternCSystem) {
Chris Lattner0b9e7362008-09-26 21:18:42 +0000149 Type = SrcMgr::C_ExternCSystem;
Daniel Dunbaref845542013-01-30 00:19:24 +0000150 } else {
151 Type = SrcMgr::C_System;
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Nico Weber0fca0222008-08-22 09:25:22 +0000154 // If the directory exists, add it.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000155 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Daniel Dunbar1ea6bc02013-01-25 01:50:28 +0000156 IncludePath.push_back(
157 std::make_pair(Group, DirectoryLookup(DE, Type, isFramework)));
Nico Weber0fca0222008-08-22 09:25:22 +0000158 return;
159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Nico Weber0fca0222008-08-22 09:25:22 +0000161 // Check to see if this is an apple-style headermap (which are not allowed to
162 // be frameworks).
163 if (!isFramework) {
Chris Lattner39b49bc2010-11-23 08:35:12 +0000164 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000165 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
166 // It is a headermap, add it to the search path.
Daniel Dunbar1ea6bc02013-01-25 01:50:28 +0000167 IncludePath.push_back(
168 std::make_pair(Group,
169 DirectoryLookup(HM, Type, Group == IndexHeaderMap)));
Nico Weber0fca0222008-08-22 09:25:22 +0000170 return;
171 }
172 }
173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Nico Weber0fca0222008-08-22 09:25:22 +0000175 if (Verbose)
Chandler Carruthf3721452010-11-15 00:48:13 +0000176 llvm::errs() << "ignoring nonexistent directory \""
177 << MappedPathStr << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000178}
179
Chris Lattner5f9e2722011-07-23 10:55:15 +0000180void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
181 StringRef ArchDir,
182 StringRef Dir32,
183 StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000184 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000185 // Add the base dir
Daniel Dunbaref845542013-01-30 00:19:24 +0000186 AddPath(Base, CXXSystem, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000187
188 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000189 llvm::Triple::ArchType arch = triple.getArch();
190 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000191 if (is64bit)
Daniel Dunbaref845542013-01-30 00:19:24 +0000192 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000193 else
Daniel Dunbaref845542013-01-30 00:19:24 +0000194 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000195
196 // Add the backward dir
Daniel Dunbaref845542013-01-30 00:19:24 +0000197 AddPath(Base + "/backward", CXXSystem, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000198}
Nico Weber0fca0222008-08-22 09:25:22 +0000199
Aaron Ballmanb3656d32012-03-25 15:47:41 +0000200void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +0000201 StringRef Arch,
202 StringRef Version) {
203 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
Daniel Dunbaref845542013-01-30 00:19:24 +0000204 CXXSystem, false);
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +0000205 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
Daniel Dunbaref845542013-01-30 00:19:24 +0000206 CXXSystem, false);
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +0000207 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
Daniel Dunbaref845542013-01-30 00:19:24 +0000208 CXXSystem, false);
Aaron Ballmanb3656d32012-03-25 15:47:41 +0000209}
210
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +0000211void InitHeaderSearch::AddMinGW64CXXPaths(StringRef Base,
212 StringRef Version) {
Douglas Gregord944a9b2011-06-27 15:47:15 +0000213 // Assumes Base is HeaderSearchOpts' ResourceDir
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +0000214 AddPath(Base + "/../../../include/c++/" + Version,
Daniel Dunbaref845542013-01-30 00:19:24 +0000215 CXXSystem, false);
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +0000216 AddPath(Base + "/../../../include/c++/" + Version + "/x86_64-w64-mingw32",
Daniel Dunbaref845542013-01-30 00:19:24 +0000217 CXXSystem, false);
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +0000218 AddPath(Base + "/../../../include/c++/" + Version + "/i686-w64-mingw32",
Daniel Dunbaref845542013-01-30 00:19:24 +0000219 CXXSystem, false);
Aaron Ballmanb3dcbbd2012-03-25 22:46:17 +0000220 AddPath(Base + "/../../../include/c++/" + Version + "/backward",
Daniel Dunbaref845542013-01-30 00:19:24 +0000221 CXXSystem, false);
NAKAMURA Takumi9db48462011-03-15 02:32:36 +0000222}
223
mike-m79bc57c2010-05-16 19:03:52 +0000224void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
225 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000226 llvm::Triple::OSType os = triple.getOS();
227
Daniel Dunbara268fc02011-10-11 18:20:10 +0000228 if (HSOpts.UseStandardSystemIncludes) {
229 switch (os) {
230 case llvm::Triple::FreeBSD:
231 case llvm::Triple::NetBSD:
Hans Wennborg9d82a032012-08-02 12:27:08 +0000232 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +0000233 case llvm::Triple::Bitrig:
Daniel Dunbara268fc02011-10-11 18:20:10 +0000234 break;
235 default:
236 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbaref845542013-01-30 00:19:24 +0000237 AddPath("/usr/local/include", System, false);
Daniel Dunbara268fc02011-10-11 18:20:10 +0000238 break;
239 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000240 }
mike-m79bc57c2010-05-16 19:03:52 +0000241
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.
Benjamin Kramer0b214902013-06-13 14:26:04 +0000247 SmallString<128> P = StringRef(HSOpts.ResourceDir);
248 llvm::sys::path::append(P, "include");
Daniel Dunbar9cd90a22013-01-30 01:06:03 +0000249 AddUnmappedPath(P.str(), ExternCSystem, false);
mike-m79bc57c2010-05-16 19:03:52 +0000250 }
251
Daniel Dunbara268fc02011-10-11 18:20:10 +0000252 // All remaining additions are for system include directories, early exit if
253 // we aren't using them.
254 if (!HSOpts.UseStandardSystemIncludes)
255 return;
256
mike-m79bc57c2010-05-16 19:03:52 +0000257 // Add dirs specified via 'configure --with-c-include-dirs'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000258 StringRef CIncludeDirs(C_INCLUDE_DIRS);
Daniel Dunbarc7064682009-11-12 07:28:29 +0000259 if (CIncludeDirs != "") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000260 SmallVector<StringRef, 5> dirs;
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000261 CIncludeDirs.split(dirs, ":");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000262 for (SmallVectorImpl<StringRef>::iterator i = dirs.begin();
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000263 i != dirs.end();
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000264 ++i)
Daniel Dunbaref845542013-01-30 00:19:24 +0000265 AddPath(*i, ExternCSystem, false);
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000266 return;
267 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000268
Mike Stump43d81762009-10-08 23:29:47 +0000269 switch (os) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000270 case llvm::Triple::Linux:
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000271 llvm_unreachable("Include management is handled in the driver.");
Chandler Carruthca234192011-11-04 23:49:05 +0000272
Chris Lattner86ed3a32010-04-11 19:29:39 +0000273 case llvm::Triple::Haiku:
Daniel Dunbaref845542013-01-30 00:19:24 +0000274 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);
Eli Friedmana7e68452010-08-22 01:00:03 +0000306 break;
Douglas Gregordca52262011-07-01 22:41:14 +0000307 case llvm::Triple::RTEMS:
308 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700309 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:
Douglas Gregord944a9b2011-06-27 15:47:15 +0000316 // mingw-w64 crt include paths
Benjamin Kramer0b214902013-06-13 14:26:04 +0000317 // <sysroot>/i686-w64-mingw32/include
318 SmallString<128> P = StringRef(HSOpts.ResourceDir);
319 llvm::sys::path::append(P, "../../../i686-w64-mingw32/include");
Daniel Dunbaref845542013-01-30 00:19:24 +0000320 AddPath(P.str(), System, false);
Benjamin Kramer0b214902013-06-13 14:26:04 +0000321
322 // <sysroot>/x86_64-w64-mingw32/include
323 P.resize(HSOpts.ResourceDir.size());
324 llvm::sys::path::append(P, "../../../x86_64-w64-mingw32/include");
Daniel Dunbaref845542013-01-30 00:19:24 +0000325 AddPath(P.str(), System, false);
Benjamin Kramer0b214902013-06-13 14:26:04 +0000326
Douglas Gregord944a9b2011-06-27 15:47:15 +0000327 // mingw.org crt include paths
Benjamin Kramer0b214902013-06-13 14:26:04 +0000328 // <sysroot>/include
329 P.resize(HSOpts.ResourceDir.size());
330 llvm::sys::path::append(P, "../../../include");
Daniel Dunbaref845542013-01-30 00:19:24 +0000331 AddPath(P.str(), System, false);
332 AddPath("/mingw/include", System, false);
Stephen Hines651f13c2014-04-23 16:59:28 -0700333#if defined(LLVM_ON_WIN32)
Daniel Dunbaref845542013-01-30 00:19:24 +0000334 AddPath("c:/mingw/include", System, false);
NAKAMURA Takumi1696bc22012-09-13 05:53:23 +0000335#endif
Stephen Hines651f13c2014-04-23 16:59:28 -0700336 break;
Douglas Gregord944a9b2011-06-27 15:47:15 +0000337 }
Mike Stump43d81762009-10-08 23:29:47 +0000338 break;
339 default:
Mike Stump43d81762009-10-08 23:29:47 +0000340 break;
341 }
John Thompsond3f88342009-10-13 18:51:32 +0000342
Douglas Gregordca52262011-07-01 22:41:14 +0000343 if ( os != llvm::Triple::RTEMS )
Daniel Dunbaref845542013-01-30 00:19:24 +0000344 AddPath("/usr/include", ExternCSystem, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000345}
346
Chris Lattner0e3cc052010-05-05 05:28:39 +0000347void InitHeaderSearch::
Douglas Gregord944a9b2011-06-27 15:47:15 +0000348AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000349 llvm::Triple::OSType os = triple.getOS();
350 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000351
352 if (triple.isOSDarwin()) {
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000353 switch (triple.getArch()) {
354 default: break;
355
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000356 case llvm::Triple::ppc:
Douglas Gregor582c3012010-05-29 01:15:12 +0000357 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000358 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000359 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor616d4362010-05-29 01:21:11 +0000360 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000361 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000362 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor582c3012010-05-29 01:15:12 +0000363 triple);
364 break;
365
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000366 case llvm::Triple::x86:
367 case llvm::Triple::x86_64:
368 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
369 "i686-apple-darwin10", "", "x86_64", triple);
370 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
371 "i686-apple-darwin8", "", "", triple);
372 break;
373
374 case llvm::Triple::arm:
375 case llvm::Triple::thumb:
376 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
377 "arm-apple-darwin10", "v7", "", triple);
378 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
379 "arm-apple-darwin10", "v6", "", triple);
380 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700381
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700382 case llvm::Triple::aarch64:
Stephen Hines651f13c2014-04-23 16:59:28 -0700383 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
384 "arm64-apple-darwin10", "", "", triple);
385 break;
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000386 }
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000387 return;
388 }
389
390 switch (os) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000391 case llvm::Triple::Linux:
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000392 llvm_unreachable("Include management is handled in the driver.");
Stephen Hines176edba2014-12-01 14:53:08 -0800393 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700394 case llvm::Triple::Win32:
395 switch (triple.getEnvironment()) {
396 default: llvm_unreachable("Include management is handled in the driver.");
397 case llvm::Triple::Cygnus:
398 // Cygwin-1.7
399 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3");
400 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3");
401 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
402 // g++-4 / Cygwin-1.5
403 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
404 break;
405 case llvm::Triple::GNU:
406 // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
407 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
408 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.1");
409 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.2");
410 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.3");
411 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.8.0");
412 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.8.1");
413 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.8.2");
414 // mingw.org C++ include paths
415#if defined(LLVM_ON_WIN32)
416 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.7.0");
417 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.7.1");
418 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.7.2");
419 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.7.3");
420 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.8.0");
421 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.8.1");
422 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.8.2");
NAKAMURA Takumi1696bc22012-09-13 05:53:23 +0000423#endif
Stephen Hines651f13c2014-04-23 16:59:28 -0700424 break;
425 }
Chris Lattner7a7ca282010-01-09 05:41:14 +0000426 case llvm::Triple::DragonFly:
John McCall8cfb7202013-04-11 22:55:55 +0000427 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
428 AddPath("/usr/include/c++/4.7", CXXSystem, false);
429 else
430 AddPath("/usr/include/c++/4.4", CXXSystem, false);
Chris Lattner7a7ca282010-01-09 05:41:14 +0000431 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000432 case llvm::Triple::OpenBSD: {
433 std::string t = triple.getTriple();
434 if (t.substr(0, 6) == "x86_64")
435 t.replace(0, 6, "amd64");
436 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
437 t, "", "", triple);
438 break;
439 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000440 case llvm::Triple::Minix:
441 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
442 "", "", "", triple);
443 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000444 case llvm::Triple::Solaris:
David Chisnallb6229592012-02-15 18:24:31 +0000445 AddGnuCPlusPlusIncludePaths("/usr/gcc/4.5/include/c++/4.5.2/",
446 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000447 break;
448 default:
449 break;
450 }
451}
452
Daniel Dunbara268fc02011-10-11 18:20:10 +0000453void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
454 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000455 const HeaderSearchOptions &HSOpts) {
Chandler Carruthca234192011-11-04 23:49:05 +0000456 // NB: This code path is going away. All of the logic is moving into the
457 // driver which has the information necessary to do target-specific
458 // selections of default include paths. Each target which moves there will be
459 // exempted from this logic here until we can delete the entire pile of code.
460 switch (triple.getOS()) {
461 default:
462 break; // Everything else continues to use this routine's logic.
463
Chandler Carruth7d7e9f92011-11-05 20:17:13 +0000464 case llvm::Triple::Linux:
Chandler Carruthca234192011-11-04 23:49:05 +0000465 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700466
467 case llvm::Triple::Win32:
468 if (triple.getEnvironment() == llvm::Triple::MSVC ||
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700469 triple.getEnvironment() == llvm::Triple::Itanium ||
470 triple.getObjectFormat() == llvm::Triple::MachO)
Stephen Hines651f13c2014-04-23 16:59:28 -0700471 return;
472 break;
Chandler Carruthca234192011-11-04 23:49:05 +0000473 }
474
Daniel Dunbara268fc02011-10-11 18:20:10 +0000475 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes &&
476 HSOpts.UseStandardSystemIncludes) {
Douglas Gregorbaf41f12011-07-29 20:21:18 +0000477 if (HSOpts.UseLibcxx) {
478 if (triple.isOSDarwin()) {
479 // On Darwin, libc++ may be installed alongside the compiler in
Justin Bognerc3e16a22013-11-15 18:07:59 +0000480 // include/c++/v1.
Benjamin Kramer0b214902013-06-13 14:26:04 +0000481 if (!HSOpts.ResourceDir.empty()) {
482 // Remove version from foo/lib/clang/version
483 StringRef NoVer = llvm::sys::path::parent_path(HSOpts.ResourceDir);
484 // Remove clang from foo/lib/clang
Justin Bognerc3e16a22013-11-15 18:07:59 +0000485 StringRef Lib = llvm::sys::path::parent_path(NoVer);
486 // Remove lib from foo/lib
487 SmallString<128> P = llvm::sys::path::parent_path(Lib);
488
489 // Get foo/include/c++/v1
490 llvm::sys::path::append(P, "include", "c++", "v1");
Daniel Dunbar9cd90a22013-01-30 01:06:03 +0000491 AddUnmappedPath(P.str(), CXXSystem, false);
Douglas Gregorbaf41f12011-07-29 20:21:18 +0000492 }
493 }
David Chisnallb4f0bd62012-03-02 10:49:52 +0000494 // On Solaris, include the support directory for things like xlocale and
495 // fudged system headers.
496 if (triple.getOS() == llvm::Triple::Solaris)
Daniel Dunbaref845542013-01-30 00:19:24 +0000497 AddPath("/usr/include/c++/v1/support/solaris", CXXSystem, false);
Douglas Gregorbaf41f12011-07-29 20:21:18 +0000498
Daniel Dunbaref845542013-01-30 00:19:24 +0000499 AddPath("/usr/include/c++/v1", CXXSystem, false);
Daniel Dunbara268fc02011-10-11 18:20:10 +0000500 } else {
Douglas Gregord944a9b2011-06-27 15:47:15 +0000501 AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
Daniel Dunbara268fc02011-10-11 18:20:10 +0000502 }
Bob Wilson13c4f212011-06-21 21:12:29 +0000503 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000504
mike-m79bc57c2010-05-16 19:03:52 +0000505 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000506
507 // Add the default framework include paths on Darwin.
Daniel Dunbara268fc02011-10-11 18:20:10 +0000508 if (HSOpts.UseStandardSystemIncludes) {
509 if (triple.isOSDarwin()) {
Daniel Dunbaref845542013-01-30 00:19:24 +0000510 AddPath("/System/Library/Frameworks", System, true);
511 AddPath("/Library/Frameworks", System, true);
Daniel Dunbara268fc02011-10-11 18:20:10 +0000512 }
Daniel Dunbare1665822009-11-07 04:20:39 +0000513 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000514}
515
Nico Weber0fca0222008-08-22 09:25:22 +0000516/// RemoveDuplicates - If there are duplicate directory entries in the specified
Chad Rosiere305e812011-10-10 18:44:24 +0000517/// search list, remove the later (dead) ones. Returns the number of non-system
518/// headers removed, which is used to update NumAngled.
519static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
520 unsigned First, bool Verbose) {
Nico Weber0fca0222008-08-22 09:25:22 +0000521 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
522 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
523 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Chad Rosiere305e812011-10-10 18:44:24 +0000524 unsigned NonSystemRemoved = 0;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000525 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000526 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Chris Lattner43eee072009-02-08 01:00:10 +0000528 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattner43eee072009-02-08 01:00:10 +0000530 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000531 // If this isn't the first time we've seen this dir, remove it.
Stephen Hines176edba2014-12-01 14:53:08 -0800532 if (SeenDirs.insert(CurEntry.getDir()).second)
Nico Weber0fca0222008-08-22 09:25:22 +0000533 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000534 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000535 // If this isn't the first time we've seen this framework dir, remove it.
Stephen Hines176edba2014-12-01 14:53:08 -0800536 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()).second)
Nico Weber0fca0222008-08-22 09:25:22 +0000537 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000538 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000539 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000540 // If this isn't the first time we've seen this headermap, remove it.
Stephen Hines176edba2014-12-01 14:53:08 -0800541 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()).second)
Nico Weber0fca0222008-08-22 09:25:22 +0000542 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattner30f05b52009-02-08 00:55:22 +0000545 // If we have a normal #include dir/framework/headermap that is shadowed
546 // later in the chain by a system include location, we actually want to
547 // ignore the user's request and drop the user dir... keeping the system
548 // dir. This is weird, but required to emulate GCC's search path correctly.
549 //
550 // Since dupes of system dirs are rare, just rescan to find the original
551 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000552 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000553 // Find the dir that this is the same of.
554 unsigned FirstDir;
555 for (FirstDir = 0; ; ++FirstDir) {
556 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner43eee072009-02-08 01:00:10 +0000558 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
559
Chris Lattner30f05b52009-02-08 00:55:22 +0000560 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000561 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000562 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Chris Lattner30f05b52009-02-08 00:55:22 +0000564 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000565 if (CurEntry.isNormalDir())
566 isSame = SearchEntry.getDir() == CurEntry.getDir();
567 else if (CurEntry.isFramework())
568 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000569 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000570 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
571 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Chris Lattner30f05b52009-02-08 00:55:22 +0000574 if (isSame)
575 break;
576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Chris Lattner30f05b52009-02-08 00:55:22 +0000578 // If the first dir in the search path is a non-system dir, zap it
579 // instead of the system one.
580 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
581 DirToRemove = FirstDir;
582 }
583
584 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000585 llvm::errs() << "ignoring duplicate directory \""
586 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000587 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000588 llvm::errs() << " as it is a non-system directory that duplicates "
589 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000590 }
Chad Rosiere305e812011-10-10 18:44:24 +0000591 if (DirToRemove != i)
592 ++NonSystemRemoved;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Chris Lattner7a739402008-09-26 17:46:45 +0000594 // This is reached if the current entry is a duplicate. Remove the
595 // DirToRemove (usually the current dir).
596 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000597 --i;
598 }
Chad Rosiere305e812011-10-10 18:44:24 +0000599 return NonSystemRemoved;
Nico Weber0fca0222008-08-22 09:25:22 +0000600}
601
602
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000603void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Weber0fca0222008-08-22 09:25:22 +0000604 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
605 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000606 SearchList.reserve(IncludePath.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Chris Lattnerebb61642011-06-16 22:56:45 +0000608 // Quoted arguments go first.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000609 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
610 it != ie; ++it) {
611 if (it->first == Quoted)
612 SearchList.push_back(it->second);
613 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000614 // Deduplicate and remember index.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000615 RemoveDuplicates(SearchList, 0, Verbose);
Chris Lattnerebb61642011-06-16 22:56:45 +0000616 unsigned NumQuoted = SearchList.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000618 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
619 it != ie; ++it) {
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000620 if (it->first == Angled || it->first == IndexHeaderMap)
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000621 SearchList.push_back(it->second);
622 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000623
624 RemoveDuplicates(SearchList, NumQuoted, Verbose);
625 unsigned NumAngled = SearchList.size();
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000626
627 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
628 it != ie; ++it) {
Daniel Dunbaref845542013-01-30 00:19:24 +0000629 if (it->first == System || it->first == ExternCSystem ||
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000630 (!Lang.ObjC1 && !Lang.CPlusPlus && it->first == CSystem) ||
Benjamin Kramerc535d972011-09-23 02:25:14 +0000631 (/*FIXME !Lang.ObjC1 && */Lang.CPlusPlus && it->first == CXXSystem) ||
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000632 (Lang.ObjC1 && !Lang.CPlusPlus && it->first == ObjCSystem) ||
633 (Lang.ObjC1 && Lang.CPlusPlus && it->first == ObjCXXSystem))
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000634 SearchList.push_back(it->second);
635 }
636
637 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
638 it != ie; ++it) {
639 if (it->first == After)
640 SearchList.push_back(it->second);
641 }
642
Chris Lattner1d7f12b2011-06-16 22:58:10 +0000643 // Remove duplicates across both the Angled and System directories. GCC does
644 // this and failing to remove duplicates across these two groups breaks
645 // #include_next.
Chad Rosiere305e812011-10-10 18:44:24 +0000646 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
647 NumAngled -= NonSystemRemoved;
Nico Weber0fca0222008-08-22 09:25:22 +0000648
649 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Chris Lattnerebb61642011-06-16 22:56:45 +0000650 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
Nico Weber0fca0222008-08-22 09:25:22 +0000651
Richard Smithf122a132012-06-13 20:27:03 +0000652 Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
653
Nico Weber0fca0222008-08-22 09:25:22 +0000654 // If verbose, print the list of directories that will be searched.
655 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000656 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000657 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Chris Lattnerebb61642011-06-16 22:56:45 +0000658 if (i == NumQuoted)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000659 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000660 const char *Name = SearchList[i].getName();
661 const char *Suffix;
662 if (SearchList[i].isNormalDir())
663 Suffix = "";
664 else if (SearchList[i].isFramework())
665 Suffix = " (framework directory)";
666 else {
667 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
668 Suffix = " (headermap)";
669 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000670 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000671 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000672 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000673 }
674}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000675
Daniel Dunbar5814e652009-11-11 21:44:21 +0000676void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
677 const HeaderSearchOptions &HSOpts,
678 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000679 const llvm::Triple &Triple) {
680 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
681
682 // Add the user defined entries.
683 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
684 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar9cd90a22013-01-30 01:06:03 +0000685 if (E.IgnoreSysRoot) {
686 Init.AddUnmappedPath(E.Path, E.Group, E.IsFramework);
687 } else {
688 Init.AddPath(E.Path, E.Group, E.IsFramework);
689 }
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000690 }
691
Daniel Dunbara268fc02011-10-11 18:20:10 +0000692 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000693
Richard Smithf122a132012-06-13 20:27:03 +0000694 for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
695 Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix,
696 HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
697
Douglas Gregor2f04f182012-02-02 18:42:48 +0000698 if (HSOpts.UseBuiltinIncludes) {
699 // Set up the builtin include directory in the module map.
Benjamin Kramer0b214902013-06-13 14:26:04 +0000700 SmallString<128> P = StringRef(HSOpts.ResourceDir);
701 llvm::sys::path::append(P, "include");
Douglas Gregor2f04f182012-02-02 18:42:48 +0000702 if (const DirectoryEntry *Dir = HS.getFileMgr().getDirectory(P.str()))
703 HS.getModuleMap().setBuiltinIncludeDir(Dir);
704 }
705
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000706 Init.Realize(Lang);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000707}