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