blob: 9b4f7e5180c8ebfeba68444fc4907ca4f1e359f9 [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"
John McCall65b8da02013-04-11 22:55:55 +000027#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000028#include "llvm/Support/Path.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000029#include "llvm/Support/raw_ostream.h"
Dylan Noblesmith86780e92012-02-01 14:25:28 +000030
Nico Webered9b4102008-08-22 09:25:22 +000031using namespace clang;
Daniel Dunbar4df9aa22009-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 Sonnenbergercc9c8eb2011-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 Smith8acadcb2012-06-13 20:27:03 +000043 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
Chris Lattner0c64f4b2011-06-16 22:56:45 +000044 HeaderSearch &Headers;
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000045 bool Verbose;
Michael J. Spencerbc7fcc22010-12-25 20:09:27 +000046 std::string IncludeSysroot;
Daniel Dunbar335a37b2013-01-29 23:59:43 +000047 bool HasSysroot;
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000048
49public:
50
Chris Lattner0e62c1c2011-07-23 10:55:15 +000051 InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
Michael J. Spencerbc7fcc22010-12-25 20:09:27 +000052 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
Daniel Dunbar335a37b2013-01-29 23:59:43 +000053 HasSysroot(!(sysroot.empty() || sysroot == "/")) {
Chandler Carrutheb842002010-11-15 07:15:26 +000054 }
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000055
Daniel Dunbarc6c23752013-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 Dunbar4df9aa22009-11-09 23:02:47 +000064
Richard Smith8acadcb2012-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-m6fa3cd22010-05-05 17:00:31 +000071 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000072 /// libstdc++.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000073 void AddGnuCPlusPlusIncludePaths(StringRef Base,
74 StringRef ArchDir,
75 StringRef Dir32,
76 StringRef Dir64,
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000077 const llvm::Triple &triple);
78
Michael J. Spencerbc721822010-12-21 16:45:42 +000079 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000080 /// libstdc++.
Aaron Ballmaneceaddc2012-03-25 22:46:17 +000081 void AddMinGWCPlusPlusIncludePaths(StringRef Base,
82 StringRef Arch,
83 StringRef Version);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000084
NAKAMURA Takumi63b4b452011-03-15 02:32:36 +000085 /// AddMinGW64CXXPaths - Add the necessary paths to support
86 /// libstdc++ of x86_64-w64-mingw32 aka mingw-w64.
Aaron Ballmaneceaddc2012-03-25 22:46:17 +000087 void AddMinGW64CXXPaths(StringRef Base,
88 StringRef Version);
NAKAMURA Takumi63b4b452011-03-15 02:32:36 +000089
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000090 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-mc6da2612010-05-16 19:03:52 +000091 void AddDefaultCIncludePaths(const llvm::Triple &triple,
92 const HeaderSearchOptions &HSOpts);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000093
94 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
95 // compiling c++.
Douglas Gregor7a200962011-06-27 15:47:15 +000096 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple,
97 const HeaderSearchOptions &HSOpts);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000098
99 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
100 /// that e.g. stdio.h is found.
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000101 void AddDefaultIncludePaths(const LangOptions &Lang,
102 const llvm::Triple &triple,
103 const HeaderSearchOptions &HSOpts);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +0000104
105 /// Realize - Merges all search path lists into one list and send it to
106 /// HeaderSearch.
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000107 void Realize(const LangOptions &Lang);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +0000108};
109
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000110} // end anonymous namespace.
Nico Webered9b4102008-08-22 09:25:22 +0000111
Daniel Dunbar2e8eb012013-01-29 23:59:37 +0000112static bool CanPrefixSysroot(StringRef Path) {
Hans Wennborg501eadb2014-03-12 16:07:46 +0000113#if defined(LLVM_ON_WIN32)
Daniel Dunbar2e8eb012013-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 Dunbar9f237452013-01-30 00:19:24 +0000120void InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group,
Daniel Dunbarc6c23752013-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 Stump11289f42009-09-09 15:08:12 +0000132
Daniel Dunbarc6c23752013-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 Noblesmith2c1dd272012-02-05 02:13:05 +0000141 SmallString<256> MappedPathStorage;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000142 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump11289f42009-09-09 15:08:12 +0000143
Nico Webered9b4102008-08-22 09:25:22 +0000144 // Compute the DirectoryLookup type.
Chris Lattner66a740e2008-10-27 01:19:25 +0000145 SrcMgr::CharacteristicKind Type;
Daniel Dunbar9f237452013-01-30 00:19:24 +0000146 if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
Chris Lattnerb03dc762008-09-26 21:18:42 +0000147 Type = SrcMgr::C_User;
Daniel Dunbar9f237452013-01-30 00:19:24 +0000148 } else if (Group == ExternCSystem) {
Chris Lattnerb03dc762008-09-26 21:18:42 +0000149 Type = SrcMgr::C_ExternCSystem;
Daniel Dunbar9f237452013-01-30 00:19:24 +0000150 } else {
151 Type = SrcMgr::C_System;
152 }
Mike Stump11289f42009-09-09 15:08:12 +0000153
Nico Webered9b4102008-08-22 09:25:22 +0000154 // If the directory exists, add it.
Chris Lattner5159f612010-11-23 08:35:12 +0000155 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Daniel Dunbarae4feb62013-01-25 01:50:28 +0000156 IncludePath.push_back(
157 std::make_pair(Group, DirectoryLookup(DE, Type, isFramework)));
Nico Webered9b4102008-08-22 09:25:22 +0000158 return;
159 }
Mike Stump11289f42009-09-09 15:08:12 +0000160
Nico Webered9b4102008-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 Lattner5159f612010-11-23 08:35:12 +0000164 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Webered9b4102008-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 Dunbarae4feb62013-01-25 01:50:28 +0000167 IncludePath.push_back(
168 std::make_pair(Group,
169 DirectoryLookup(HM, Type, Group == IndexHeaderMap)));
Nico Webered9b4102008-08-22 09:25:22 +0000170 return;
171 }
172 }
173 }
Mike Stump11289f42009-09-09 15:08:12 +0000174
Nico Webered9b4102008-08-22 09:25:22 +0000175 if (Verbose)
Chandler Carruth03ac1b02010-11-15 00:48:13 +0000176 llvm::errs() << "ignoring nonexistent directory \""
177 << MappedPathStr << "\"\n";
Nico Webered9b4102008-08-22 09:25:22 +0000178}
179
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000180void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
181 StringRef ArchDir,
182 StringRef Dir32,
183 StringRef Dir64,
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000184 const llvm::Triple &triple) {
Rafael Espindola962e5182009-11-23 16:31:19 +0000185 // Add the base dir
Daniel Dunbar9f237452013-01-30 00:19:24 +0000186 AddPath(Base, CXXSystem, false);
Rafael Espindolaabab8792009-11-16 19:49:37 +0000187
188 // Add the multilib dirs
Rafael Espindola0a1ac332009-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 Espindola0a1ac332009-10-14 17:09:44 +0000191 if (is64bit)
Daniel Dunbar9f237452013-01-30 00:19:24 +0000192 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false);
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000193 else
Daniel Dunbar9f237452013-01-30 00:19:24 +0000194 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false);
Rafael Espindola962e5182009-11-23 16:31:19 +0000195
196 // Add the backward dir
Daniel Dunbar9f237452013-01-30 00:19:24 +0000197 AddPath(Base + "/backward", CXXSystem, false);
Rafael Espindolac3031a92009-10-06 01:33:02 +0000198}
Nico Webered9b4102008-08-22 09:25:22 +0000199
Aaron Ballman9345d682012-03-25 15:47:41 +0000200void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000201 StringRef Arch,
202 StringRef Version) {
203 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
Daniel Dunbar9f237452013-01-30 00:19:24 +0000204 CXXSystem, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000205 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
Daniel Dunbar9f237452013-01-30 00:19:24 +0000206 CXXSystem, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000207 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
Daniel Dunbar9f237452013-01-30 00:19:24 +0000208 CXXSystem, false);
Aaron Ballman9345d682012-03-25 15:47:41 +0000209}
210
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000211void InitHeaderSearch::AddMinGW64CXXPaths(StringRef Base,
212 StringRef Version) {
Douglas Gregor7a200962011-06-27 15:47:15 +0000213 // Assumes Base is HeaderSearchOpts' ResourceDir
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000214 AddPath(Base + "/../../../include/c++/" + Version,
Daniel Dunbar9f237452013-01-30 00:19:24 +0000215 CXXSystem, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000216 AddPath(Base + "/../../../include/c++/" + Version + "/x86_64-w64-mingw32",
Daniel Dunbar9f237452013-01-30 00:19:24 +0000217 CXXSystem, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000218 AddPath(Base + "/../../../include/c++/" + Version + "/i686-w64-mingw32",
Daniel Dunbar9f237452013-01-30 00:19:24 +0000219 CXXSystem, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000220 AddPath(Base + "/../../../include/c++/" + Version + "/backward",
Daniel Dunbar9f237452013-01-30 00:19:24 +0000221 CXXSystem, false);
NAKAMURA Takumi63b4b452011-03-15 02:32:36 +0000222}
223
mike-mc6da2612010-05-16 19:03:52 +0000224void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
225 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000226 llvm::Triple::OSType os = triple.getOS();
227
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000228 if (HSOpts.UseStandardSystemIncludes) {
229 switch (os) {
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000230 case llvm::Triple::CloudABI:
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000231 case llvm::Triple::FreeBSD:
232 case llvm::Triple::NetBSD:
Hans Wennborgae298f22012-08-02 12:27:08 +0000233 case llvm::Triple::OpenBSD:
Eli Friedman9fa28852012-08-08 23:57:20 +0000234 case llvm::Triple::Bitrig:
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000235 break;
236 default:
237 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbar9f237452013-01-30 00:19:24 +0000238 AddPath("/usr/local/include", System, false);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000239 break;
240 }
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000241 }
mike-mc6da2612010-05-16 19:03:52 +0000242
243 // Builtin includes use #include_next directives and should be positioned
244 // just prior C include dirs.
245 if (HSOpts.UseBuiltinIncludes) {
246 // Ignore the sys root, we *always* look for clang headers relative to
247 // supplied path.
Benjamin Kramer33d43302013-06-13 14:26:04 +0000248 SmallString<128> P = StringRef(HSOpts.ResourceDir);
249 llvm::sys::path::append(P, "include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000250 AddUnmappedPath(P, ExternCSystem, false);
mike-mc6da2612010-05-16 19:03:52 +0000251 }
252
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000253 // All remaining additions are for system include directories, early exit if
254 // we aren't using them.
255 if (!HSOpts.UseStandardSystemIncludes)
256 return;
257
mike-mc6da2612010-05-16 19:03:52 +0000258 // Add dirs specified via 'configure --with-c-include-dirs'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000259 StringRef CIncludeDirs(C_INCLUDE_DIRS);
Daniel Dunbar71ed08b2009-11-12 07:28:29 +0000260 if (CIncludeDirs != "") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000261 SmallVector<StringRef, 5> dirs;
Rafael Espindola46129b02009-11-13 05:13:58 +0000262 CIncludeDirs.split(dirs, ":");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000263 for (SmallVectorImpl<StringRef>::iterator i = dirs.begin();
Rafael Espindola46129b02009-11-13 05:13:58 +0000264 i != dirs.end();
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000265 ++i)
Daniel Dunbar9f237452013-01-30 00:19:24 +0000266 AddPath(*i, ExternCSystem, false);
Rafael Espindolaf401fa02009-11-12 05:48:41 +0000267 return;
268 }
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000269
Mike Stump904ad902009-10-08 23:29:47 +0000270 switch (os) {
Chandler Carrutha796f532011-11-05 20:17:13 +0000271 case llvm::Triple::Linux:
Chandler Carrutha796f532011-11-05 20:17:13 +0000272 llvm_unreachable("Include management is handled in the driver.");
Chandler Carruthdf527832011-11-04 23:49:05 +0000273
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000274 case llvm::Triple::CloudABI: {
275 // <sysroot>/<triple>/include
276 SmallString<128> P = StringRef(HSOpts.ResourceDir);
277 llvm::sys::path::append(P, "../../..", triple.str(), "include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000278 AddPath(P, System, false);
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000279 break;
280 }
281
Chris Lattnerb986aba2010-04-11 19:29:39 +0000282 case llvm::Triple::Haiku:
Daniel Dunbar9f237452013-01-30 00:19:24 +0000283 AddPath("/boot/common/include", System, false);
284 AddPath("/boot/develop/headers/os", System, false);
285 AddPath("/boot/develop/headers/os/app", System, false);
286 AddPath("/boot/develop/headers/os/arch", System, false);
287 AddPath("/boot/develop/headers/os/device", System, false);
288 AddPath("/boot/develop/headers/os/drivers", System, false);
289 AddPath("/boot/develop/headers/os/game", System, false);
290 AddPath("/boot/develop/headers/os/interface", System, false);
291 AddPath("/boot/develop/headers/os/kernel", System, false);
292 AddPath("/boot/develop/headers/os/locale", System, false);
293 AddPath("/boot/develop/headers/os/mail", System, false);
294 AddPath("/boot/develop/headers/os/media", System, false);
295 AddPath("/boot/develop/headers/os/midi", System, false);
296 AddPath("/boot/develop/headers/os/midi2", System, false);
297 AddPath("/boot/develop/headers/os/net", System, false);
298 AddPath("/boot/develop/headers/os/storage", System, false);
299 AddPath("/boot/develop/headers/os/support", System, false);
300 AddPath("/boot/develop/headers/os/translation", System, false);
301 AddPath("/boot/develop/headers/os/add-ons/graphics", System, false);
302 AddPath("/boot/develop/headers/os/add-ons/input_server", System, false);
303 AddPath("/boot/develop/headers/os/add-ons/screen_saver", System, false);
304 AddPath("/boot/develop/headers/os/add-ons/tracker", System, false);
305 AddPath("/boot/develop/headers/os/be_apps/Deskbar", System, false);
306 AddPath("/boot/develop/headers/os/be_apps/NetPositive", System, false);
307 AddPath("/boot/develop/headers/os/be_apps/Tracker", System, false);
308 AddPath("/boot/develop/headers/cpp", System, false);
309 AddPath("/boot/develop/headers/cpp/i586-pc-haiku", System, false);
310 AddPath("/boot/develop/headers/3rdparty", System, false);
311 AddPath("/boot/develop/headers/bsd", System, false);
312 AddPath("/boot/develop/headers/glibc", System, false);
313 AddPath("/boot/develop/headers/posix", System, false);
314 AddPath("/boot/develop/headers", System, false);
Eli Friedman04831922010-08-22 01:00:03 +0000315 break;
Douglas Gregor9fabd852011-07-01 22:41:14 +0000316 case llvm::Triple::RTEMS:
317 break;
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000318 case llvm::Triple::Win32:
319 switch (triple.getEnvironment()) {
320 default: llvm_unreachable("Include management is handled in the driver.");
321 case llvm::Triple::Cygnus:
322 AddPath("/usr/include/w32api", System, false);
323 break;
324 case llvm::Triple::GNU:
Douglas Gregor7a200962011-06-27 15:47:15 +0000325 // mingw-w64 crt include paths
Benjamin Kramer33d43302013-06-13 14:26:04 +0000326 // <sysroot>/i686-w64-mingw32/include
327 SmallString<128> P = StringRef(HSOpts.ResourceDir);
328 llvm::sys::path::append(P, "../../../i686-w64-mingw32/include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000329 AddPath(P, System, false);
Benjamin Kramer33d43302013-06-13 14:26:04 +0000330
331 // <sysroot>/x86_64-w64-mingw32/include
332 P.resize(HSOpts.ResourceDir.size());
333 llvm::sys::path::append(P, "../../../x86_64-w64-mingw32/include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000334 AddPath(P, System, false);
Benjamin Kramer33d43302013-06-13 14:26:04 +0000335
Douglas Gregor7a200962011-06-27 15:47:15 +0000336 // mingw.org crt include paths
Benjamin Kramer33d43302013-06-13 14:26:04 +0000337 // <sysroot>/include
338 P.resize(HSOpts.ResourceDir.size());
339 llvm::sys::path::append(P, "../../../include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000340 AddPath(P, System, false);
Daniel Dunbar9f237452013-01-30 00:19:24 +0000341 AddPath("/mingw/include", System, false);
Hans Wennborg501eadb2014-03-12 16:07:46 +0000342#if defined(LLVM_ON_WIN32)
Daniel Dunbar9f237452013-01-30 00:19:24 +0000343 AddPath("c:/mingw/include", System, false);
NAKAMURA Takumi4a7a5612012-09-13 05:53:23 +0000344#endif
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000345 break;
Douglas Gregor7a200962011-06-27 15:47:15 +0000346 }
Mike Stump904ad902009-10-08 23:29:47 +0000347 break;
348 default:
Mike Stump904ad902009-10-08 23:29:47 +0000349 break;
350 }
John Thompson4334ce62009-10-13 18:51:32 +0000351
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000352 switch (os) {
353 case llvm::Triple::CloudABI:
354 case llvm::Triple::RTEMS:
355 break;
356 default:
Daniel Dunbar9f237452013-01-30 00:19:24 +0000357 AddPath("/usr/include", ExternCSystem, false);
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000358 break;
359 }
Rafael Espindola177f1d92009-10-27 14:47:31 +0000360}
361
Chris Lattner5561bf32010-05-05 05:28:39 +0000362void InitHeaderSearch::
Douglas Gregor7a200962011-06-27 15:47:15 +0000363AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
Rafael Espindola177f1d92009-10-27 14:47:31 +0000364 llvm::Triple::OSType os = triple.getOS();
365 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000366
367 if (triple.isOSDarwin()) {
Daniel Dunbareaff5fa2010-05-28 01:54:31 +0000368 switch (triple.getArch()) {
369 default: break;
370
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000371 case llvm::Triple::ppc:
Douglas Gregoreb0bdf02010-05-29 01:15:12 +0000372 case llvm::Triple::ppc64:
Douglas Gregor117ef272010-05-29 01:21:11 +0000373 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000374 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor117ef272010-05-29 01:21:11 +0000375 triple);
Douglas Gregoreb0bdf02010-05-29 01:15:12 +0000376 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000377 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregoreb0bdf02010-05-29 01:15:12 +0000378 triple);
379 break;
380
Daniel Dunbareaff5fa2010-05-28 01:54:31 +0000381 case llvm::Triple::x86:
382 case llvm::Triple::x86_64:
383 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
384 "i686-apple-darwin10", "", "x86_64", triple);
385 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
386 "i686-apple-darwin8", "", "", triple);
387 break;
388
389 case llvm::Triple::arm:
390 case llvm::Triple::thumb:
391 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
392 "arm-apple-darwin10", "v7", "", triple);
393 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
394 "arm-apple-darwin10", "v6", "", triple);
395 break;
Tim Northovera2ee4332014-03-29 15:09:45 +0000396
Tim Northover573cbee2014-05-24 12:52:07 +0000397 case llvm::Triple::aarch64:
Tim Northovera2ee4332014-03-29 15:09:45 +0000398 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
399 "arm64-apple-darwin10", "", "", triple);
400 break;
Daniel Dunbareaff5fa2010-05-28 01:54:31 +0000401 }
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000402 return;
403 }
404
405 switch (os) {
Chandler Carrutha796f532011-11-05 20:17:13 +0000406 case llvm::Triple::Linux:
Chandler Carrutha796f532011-11-05 20:17:13 +0000407 llvm_unreachable("Include management is handled in the driver.");
Sylvestre Ledru75c29142014-08-18 15:13:44 +0000408 break;
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000409 case llvm::Triple::Win32:
410 switch (triple.getEnvironment()) {
411 default: llvm_unreachable("Include management is handled in the driver.");
412 case llvm::Triple::Cygnus:
413 // Cygwin-1.7
414 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3");
415 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3");
416 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
417 // g++-4 / Cygwin-1.5
418 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
419 break;
420 case llvm::Triple::GNU:
421 // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
422 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
423 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.1");
424 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.2");
425 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.3");
426 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.8.0");
427 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.8.1");
428 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.8.2");
429 // mingw.org C++ include paths
Hans Wennborg501eadb2014-03-12 16:07:46 +0000430#if defined(LLVM_ON_WIN32)
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000431 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.7.0");
432 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.7.1");
433 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.7.2");
434 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.7.3");
435 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.8.0");
436 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.8.1");
437 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.8.2");
NAKAMURA Takumi4a7a5612012-09-13 05:53:23 +0000438#endif
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000439 break;
440 }
Chris Lattner002ba6b2010-01-09 05:41:14 +0000441 case llvm::Triple::DragonFly:
John McCall65b8da02013-04-11 22:55:55 +0000442 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
443 AddPath("/usr/include/c++/4.7", CXXSystem, false);
444 else
445 AddPath("/usr/include/c++/4.4", CXXSystem, false);
Chris Lattner002ba6b2010-01-09 05:41:14 +0000446 break;
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000447 case llvm::Triple::OpenBSD: {
448 std::string t = triple.getTriple();
449 if (t.substr(0, 6) == "x86_64")
450 t.replace(0, 6, "amd64");
451 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
452 t, "", "", triple);
453 break;
454 }
Chris Lattner3e2ee142010-07-07 16:01:42 +0000455 case llvm::Triple::Minix:
456 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
457 "", "", "", triple);
458 break;
Rafael Espindola177f1d92009-10-27 14:47:31 +0000459 case llvm::Triple::Solaris:
David Chisnall1026fb02012-02-15 18:24:31 +0000460 AddGnuCPlusPlusIncludePaths("/usr/gcc/4.5/include/c++/4.5.2/",
461 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindola177f1d92009-10-27 14:47:31 +0000462 break;
463 default:
464 break;
465 }
466}
467
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000468void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
469 const llvm::Triple &triple,
mike-mc6da2612010-05-16 19:03:52 +0000470 const HeaderSearchOptions &HSOpts) {
Chandler Carruthdf527832011-11-04 23:49:05 +0000471 // NB: This code path is going away. All of the logic is moving into the
472 // driver which has the information necessary to do target-specific
473 // selections of default include paths. Each target which moves there will be
474 // exempted from this logic here until we can delete the entire pile of code.
475 switch (triple.getOS()) {
476 default:
477 break; // Everything else continues to use this routine's logic.
478
Chandler Carrutha796f532011-11-05 20:17:13 +0000479 case llvm::Triple::Linux:
Chandler Carruthdf527832011-11-04 23:49:05 +0000480 return;
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000481
482 case llvm::Triple::Win32:
483 if (triple.getEnvironment() == llvm::Triple::MSVC ||
Bob Wilson07216a12014-06-10 21:07:12 +0000484 triple.getEnvironment() == llvm::Triple::Itanium ||
Eric Christopher610952e2014-12-05 00:22:48 +0000485 triple.isOSBinFormatMachO())
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000486 return;
487 break;
Chandler Carruthdf527832011-11-04 23:49:05 +0000488 }
489
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000490 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes &&
491 HSOpts.UseStandardSystemIncludes) {
Douglas Gregorf4016fd2011-07-29 20:21:18 +0000492 if (HSOpts.UseLibcxx) {
493 if (triple.isOSDarwin()) {
494 // On Darwin, libc++ may be installed alongside the compiler in
Justin Bogner13a641b2013-11-15 18:07:59 +0000495 // include/c++/v1.
Benjamin Kramer33d43302013-06-13 14:26:04 +0000496 if (!HSOpts.ResourceDir.empty()) {
497 // Remove version from foo/lib/clang/version
498 StringRef NoVer = llvm::sys::path::parent_path(HSOpts.ResourceDir);
499 // Remove clang from foo/lib/clang
Justin Bogner13a641b2013-11-15 18:07:59 +0000500 StringRef Lib = llvm::sys::path::parent_path(NoVer);
501 // Remove lib from foo/lib
502 SmallString<128> P = llvm::sys::path::parent_path(Lib);
503
504 // Get foo/include/c++/v1
505 llvm::sys::path::append(P, "include", "c++", "v1");
Yaron Keren92e1b622015-03-18 10:17:07 +0000506 AddUnmappedPath(P, CXXSystem, false);
Douglas Gregorf4016fd2011-07-29 20:21:18 +0000507 }
508 }
David Chisnalle04307e2012-03-02 10:49:52 +0000509 // On Solaris, include the support directory for things like xlocale and
510 // fudged system headers.
511 if (triple.getOS() == llvm::Triple::Solaris)
Daniel Dunbar9f237452013-01-30 00:19:24 +0000512 AddPath("/usr/include/c++/v1/support/solaris", CXXSystem, false);
Douglas Gregorf4016fd2011-07-29 20:21:18 +0000513
Daniel Dunbar9f237452013-01-30 00:19:24 +0000514 AddPath("/usr/include/c++/v1", CXXSystem, false);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000515 } else {
Douglas Gregor7a200962011-06-27 15:47:15 +0000516 AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000517 }
Bob Wilsonb02ea3d2011-06-21 21:12:29 +0000518 }
Rafael Espindola962e5182009-11-23 16:31:19 +0000519
mike-mc6da2612010-05-16 19:03:52 +0000520 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbarec879912009-11-07 04:20:39 +0000521
522 // Add the default framework include paths on Darwin.
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000523 if (HSOpts.UseStandardSystemIncludes) {
524 if (triple.isOSDarwin()) {
Daniel Dunbar9f237452013-01-30 00:19:24 +0000525 AddPath("/System/Library/Frameworks", System, true);
526 AddPath("/Library/Frameworks", System, true);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000527 }
Daniel Dunbarec879912009-11-07 04:20:39 +0000528 }
Rafael Espindola177f1d92009-10-27 14:47:31 +0000529}
530
Nico Webered9b4102008-08-22 09:25:22 +0000531/// RemoveDuplicates - If there are duplicate directory entries in the specified
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000532/// search list, remove the later (dead) ones. Returns the number of non-system
533/// headers removed, which is used to update NumAngled.
534static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
535 unsigned First, bool Verbose) {
Nico Webered9b4102008-08-22 09:25:22 +0000536 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
537 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
538 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000539 unsigned NonSystemRemoved = 0;
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000540 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattnere744d322008-09-26 17:46:45 +0000541 unsigned DirToRemove = i;
Mike Stump11289f42009-09-09 15:08:12 +0000542
Chris Lattner24c911e2009-02-08 01:00:10 +0000543 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump11289f42009-09-09 15:08:12 +0000544
Chris Lattner24c911e2009-02-08 01:00:10 +0000545 if (CurEntry.isNormalDir()) {
Nico Webered9b4102008-08-22 09:25:22 +0000546 // If this isn't the first time we've seen this dir, remove it.
David Blaikie82e95a32014-11-19 07:49:47 +0000547 if (SeenDirs.insert(CurEntry.getDir()).second)
Nico Webered9b4102008-08-22 09:25:22 +0000548 continue;
Chris Lattner24c911e2009-02-08 01:00:10 +0000549 } else if (CurEntry.isFramework()) {
Nico Webered9b4102008-08-22 09:25:22 +0000550 // If this isn't the first time we've seen this framework dir, remove it.
David Blaikie82e95a32014-11-19 07:49:47 +0000551 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()).second)
Nico Webered9b4102008-08-22 09:25:22 +0000552 continue;
Nico Webered9b4102008-08-22 09:25:22 +0000553 } else {
Chris Lattner24c911e2009-02-08 01:00:10 +0000554 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Webered9b4102008-08-22 09:25:22 +0000555 // If this isn't the first time we've seen this headermap, remove it.
David Blaikie82e95a32014-11-19 07:49:47 +0000556 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()).second)
Nico Webered9b4102008-08-22 09:25:22 +0000557 continue;
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000558 }
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000560 // If we have a normal #include dir/framework/headermap that is shadowed
561 // later in the chain by a system include location, we actually want to
562 // ignore the user's request and drop the user dir... keeping the system
563 // dir. This is weird, but required to emulate GCC's search path correctly.
564 //
565 // Since dupes of system dirs are rare, just rescan to find the original
566 // that we're nuking instead of using a DenseMap.
Chris Lattner24c911e2009-02-08 01:00:10 +0000567 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000568 // Find the dir that this is the same of.
569 unsigned FirstDir;
570 for (FirstDir = 0; ; ++FirstDir) {
571 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump11289f42009-09-09 15:08:12 +0000572
Chris Lattner24c911e2009-02-08 01:00:10 +0000573 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
574
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000575 // If these are different lookup types, then they can't be the dupe.
Chris Lattner24c911e2009-02-08 01:00:10 +0000576 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000577 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000578
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000579 bool isSame;
Chris Lattner24c911e2009-02-08 01:00:10 +0000580 if (CurEntry.isNormalDir())
581 isSame = SearchEntry.getDir() == CurEntry.getDir();
582 else if (CurEntry.isFramework())
583 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000584 else {
Chris Lattner24c911e2009-02-08 01:00:10 +0000585 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
586 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000589 if (isSame)
590 break;
591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000593 // If the first dir in the search path is a non-system dir, zap it
594 // instead of the system one.
595 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
596 DirToRemove = FirstDir;
597 }
598
599 if (Verbose) {
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000600 llvm::errs() << "ignoring duplicate directory \""
601 << CurEntry.getName() << "\"\n";
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000602 if (DirToRemove != i)
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000603 llvm::errs() << " as it is a non-system directory that duplicates "
604 << "a system directory\n";
Nico Webered9b4102008-08-22 09:25:22 +0000605 }
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000606 if (DirToRemove != i)
607 ++NonSystemRemoved;
Mike Stump11289f42009-09-09 15:08:12 +0000608
Chris Lattnere744d322008-09-26 17:46:45 +0000609 // This is reached if the current entry is a duplicate. Remove the
610 // DirToRemove (usually the current dir).
611 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Webered9b4102008-08-22 09:25:22 +0000612 --i;
613 }
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000614 return NonSystemRemoved;
Nico Webered9b4102008-08-22 09:25:22 +0000615}
616
617
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000618void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Webered9b4102008-08-22 09:25:22 +0000619 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
620 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000621 SearchList.reserve(IncludePath.size());
Mike Stump11289f42009-09-09 15:08:12 +0000622
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000623 // Quoted arguments go first.
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000624 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
625 it != ie; ++it) {
626 if (it->first == Quoted)
627 SearchList.push_back(it->second);
628 }
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000629 // Deduplicate and remember index.
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000630 RemoveDuplicates(SearchList, 0, Verbose);
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000631 unsigned NumQuoted = SearchList.size();
Mike Stump11289f42009-09-09 15:08:12 +0000632
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000633 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
634 it != ie; ++it) {
Douglas Gregor9f93e382011-07-28 04:45:53 +0000635 if (it->first == Angled || it->first == IndexHeaderMap)
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000636 SearchList.push_back(it->second);
637 }
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000638
639 RemoveDuplicates(SearchList, NumQuoted, Verbose);
640 unsigned NumAngled = SearchList.size();
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000641
642 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
643 it != ie; ++it) {
Daniel Dunbar9f237452013-01-30 00:19:24 +0000644 if (it->first == System || it->first == ExternCSystem ||
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000645 (!Lang.ObjC1 && !Lang.CPlusPlus && it->first == CSystem) ||
Benjamin Kramer559865c2011-09-23 02:25:14 +0000646 (/*FIXME !Lang.ObjC1 && */Lang.CPlusPlus && it->first == CXXSystem) ||
Benjamin Kramer8404eb02011-09-22 21:41:16 +0000647 (Lang.ObjC1 && !Lang.CPlusPlus && it->first == ObjCSystem) ||
648 (Lang.ObjC1 && Lang.CPlusPlus && it->first == ObjCXXSystem))
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000649 SearchList.push_back(it->second);
650 }
651
652 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
653 it != ie; ++it) {
654 if (it->first == After)
655 SearchList.push_back(it->second);
656 }
657
Chris Lattner705c5c82011-06-16 22:58:10 +0000658 // Remove duplicates across both the Angled and System directories. GCC does
659 // this and failing to remove duplicates across these two groups breaks
660 // #include_next.
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000661 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
662 NumAngled -= NonSystemRemoved;
Nico Webered9b4102008-08-22 09:25:22 +0000663
664 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000665 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
Nico Webered9b4102008-08-22 09:25:22 +0000666
Richard Smith8acadcb2012-06-13 20:27:03 +0000667 Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
668
Nico Webered9b4102008-08-22 09:25:22 +0000669 // If verbose, print the list of directories that will be searched.
670 if (Verbose) {
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000671 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Webered9b4102008-08-22 09:25:22 +0000672 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000673 if (i == NumQuoted)
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000674 llvm::errs() << "#include <...> search starts here:\n";
Nico Webered9b4102008-08-22 09:25:22 +0000675 const char *Name = SearchList[i].getName();
676 const char *Suffix;
677 if (SearchList[i].isNormalDir())
678 Suffix = "";
679 else if (SearchList[i].isFramework())
680 Suffix = " (framework directory)";
681 else {
682 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
683 Suffix = " (headermap)";
684 }
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000685 llvm::errs() << " " << Name << Suffix << "\n";
Nico Webered9b4102008-08-22 09:25:22 +0000686 }
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000687 llvm::errs() << "End of search list.\n";
Nico Webered9b4102008-08-22 09:25:22 +0000688 }
689}
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000690
Daniel Dunbar0c6c9302009-11-11 21:44:21 +0000691void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
692 const HeaderSearchOptions &HSOpts,
693 const LangOptions &Lang,
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000694 const llvm::Triple &Triple) {
695 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
696
697 // Add the user defined entries.
698 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
699 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbarc6c23752013-01-30 01:06:03 +0000700 if (E.IgnoreSysRoot) {
701 Init.AddUnmappedPath(E.Path, E.Group, E.IsFramework);
702 } else {
703 Init.AddPath(E.Path, E.Group, E.IsFramework);
704 }
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000705 }
706
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000707 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000708
Richard Smith8acadcb2012-06-13 20:27:03 +0000709 for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
710 Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix,
711 HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
712
Douglas Gregor3ec66632012-02-02 18:42:48 +0000713 if (HSOpts.UseBuiltinIncludes) {
714 // Set up the builtin include directory in the module map.
Benjamin Kramer33d43302013-06-13 14:26:04 +0000715 SmallString<128> P = StringRef(HSOpts.ResourceDir);
716 llvm::sys::path::append(P, "include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000717 if (const DirectoryEntry *Dir = HS.getFileMgr().getDirectory(P))
Douglas Gregor3ec66632012-02-02 18:42:48 +0000718 HS.getModuleMap().setBuiltinIncludeDir(Dir);
719 }
720
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000721 Init.Realize(Lang);
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000722}