InitHeaderSearch.cpp revision 239462
1212904Sdim//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This file implements the InitHeaderSearch class.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14199482Srdivacky#include "clang/Frontend/Utils.h"
15193326Sed#include "clang/Basic/FileManager.h"
16193326Sed#include "clang/Basic/LangOptions.h"
17208961Srdivacky#include "clang/Basic/Version.h"
18199482Srdivacky#include "clang/Frontend/HeaderSearchOptions.h"
19199482Srdivacky#include "clang/Lex/HeaderSearch.h"
20193326Sed#include "llvm/ADT/SmallString.h"
21193326Sed#include "llvm/ADT/SmallPtrSet.h"
22199482Srdivacky#include "llvm/ADT/SmallVector.h"
23199482Srdivacky#include "llvm/ADT/StringExtras.h"
24199482Srdivacky#include "llvm/ADT/Triple.h"
25200583Srdivacky#include "llvm/ADT/Twine.h"
26198092Srdivacky#include "llvm/Support/raw_ostream.h"
27228379Sdim#include "llvm/Support/ErrorHandling.h"
28218893Sdim#include "llvm/Support/Path.h"
29234353Sdim
30234353Sdim#include "clang/Config/config.h" // C_INCLUDE_DIRS
31234353Sdim
32193326Sedusing namespace clang;
33199482Srdivackyusing namespace clang::frontend;
34193326Sed
35199482Srdivackynamespace {
36199482Srdivacky
37199482Srdivacky/// InitHeaderSearch - This class makes it easier to set the search paths of
38199482Srdivacky///  a HeaderSearch object. InitHeaderSearch stores several search path lists
39199482Srdivacky///  internally, which can be sent to a HeaderSearch object in one swoop.
40199482Srdivackyclass InitHeaderSearch {
41219077Sdim  std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
42219077Sdim  typedef std::vector<std::pair<IncludeDirGroup,
43219077Sdim                      DirectoryLookup> >::const_iterator path_iterator;
44239462Sdim  std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
45224145Sdim  HeaderSearch &Headers;
46199482Srdivacky  bool Verbose;
47218893Sdim  std::string IncludeSysroot;
48218893Sdim  bool IsNotEmptyOrRoot;
49199482Srdivacky
50199482Srdivackypublic:
51199482Srdivacky
52226633Sdim  InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
53218893Sdim    : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
54218893Sdim      IsNotEmptyOrRoot(!(sysroot.empty() || sysroot == "/")) {
55218893Sdim  }
56199482Srdivacky
57199482Srdivacky  /// AddPath - Add the specified path to the specified group list.
58226633Sdim  void AddPath(const Twine &Path, IncludeDirGroup Group,
59199482Srdivacky               bool isCXXAware, bool isUserSupplied,
60199482Srdivacky               bool isFramework, bool IgnoreSysRoot = false);
61199482Srdivacky
62239462Sdim  /// AddSystemHeaderPrefix - Add the specified prefix to the system header
63239462Sdim  /// prefix list.
64239462Sdim  void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
65239462Sdim    SystemHeaderPrefixes.push_back(std::make_pair(Prefix, IsSystemHeader));
66239462Sdim  }
67239462Sdim
68208600Srdivacky  /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
69199482Srdivacky  ///  libstdc++.
70226633Sdim  void AddGnuCPlusPlusIncludePaths(StringRef Base,
71226633Sdim                                   StringRef ArchDir,
72226633Sdim                                   StringRef Dir32,
73226633Sdim                                   StringRef Dir64,
74199482Srdivacky                                   const llvm::Triple &triple);
75199482Srdivacky
76218893Sdim  /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
77199482Srdivacky  ///  libstdc++.
78226633Sdim  void AddMinGWCPlusPlusIncludePaths(StringRef Base,
79226633Sdim                                     StringRef Arch,
80226633Sdim                                     StringRef Version);
81199482Srdivacky
82221345Sdim  /// AddMinGW64CXXPaths - Add the necessary paths to support
83221345Sdim  /// libstdc++ of x86_64-w64-mingw32 aka mingw-w64.
84226633Sdim  void AddMinGW64CXXPaths(StringRef Base,
85226633Sdim                          StringRef Version);
86221345Sdim
87199482Srdivacky  // AddDefaultCIncludePaths - Add paths that should always be searched.
88208600Srdivacky  void AddDefaultCIncludePaths(const llvm::Triple &triple,
89208600Srdivacky                               const HeaderSearchOptions &HSOpts);
90199482Srdivacky
91199482Srdivacky  // AddDefaultCPlusPlusIncludePaths -  Add paths that should be searched when
92199482Srdivacky  //  compiling c++.
93224145Sdim  void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple,
94224145Sdim                                       const HeaderSearchOptions &HSOpts);
95199482Srdivacky
96199482Srdivacky  /// AddDefaultSystemIncludePaths - Adds the default system include paths so
97199482Srdivacky  ///  that e.g. stdio.h is found.
98226633Sdim  void AddDefaultIncludePaths(const LangOptions &Lang,
99226633Sdim                              const llvm::Triple &triple,
100226633Sdim                              const HeaderSearchOptions &HSOpts);
101199482Srdivacky
102199482Srdivacky  /// Realize - Merges all search path lists into one list and send it to
103199482Srdivacky  /// HeaderSearch.
104219077Sdim  void Realize(const LangOptions &Lang);
105199482Srdivacky};
106199482Srdivacky
107224145Sdim}  // end anonymous namespace.
108199482Srdivacky
109226633Sdimvoid InitHeaderSearch::AddPath(const Twine &Path,
110198092Srdivacky                               IncludeDirGroup Group, bool isCXXAware,
111198092Srdivacky                               bool isUserSupplied, bool isFramework,
112198092Srdivacky                               bool IgnoreSysRoot) {
113200583Srdivacky  assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
114193326Sed  FileManager &FM = Headers.getFileMgr();
115198092Srdivacky
116193326Sed  // Compute the actual path, taking into consideration -isysroot.
117234353Sdim  SmallString<256> MappedPathStorage;
118226633Sdim  StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
119198092Srdivacky
120193326Sed  // Handle isysroot.
121221345Sdim  if ((Group == System || Group == CXXSystem) && !IgnoreSysRoot &&
122221345Sdim#if defined(_WIN32)
123221345Sdim      !MappedPathStr.empty() &&
124221345Sdim      llvm::sys::path::is_separator(MappedPathStr[0]) &&
125221345Sdim#else
126218893Sdim      llvm::sys::path::is_absolute(MappedPathStr) &&
127221345Sdim#endif
128218893Sdim      IsNotEmptyOrRoot) {
129218893Sdim    MappedPathStorage.clear();
130218893Sdim    MappedPathStr =
131218893Sdim      (IncludeSysroot + Path).toStringRef(MappedPathStorage);
132193326Sed  }
133198092Srdivacky
134193326Sed  // Compute the DirectoryLookup type.
135193326Sed  SrcMgr::CharacteristicKind Type;
136226633Sdim  if (Group == Quoted || Group == Angled || Group == IndexHeaderMap)
137193326Sed    Type = SrcMgr::C_User;
138193326Sed  else if (isCXXAware)
139193326Sed    Type = SrcMgr::C_System;
140193326Sed  else
141193326Sed    Type = SrcMgr::C_ExternCSystem;
142198092Srdivacky
143198092Srdivacky
144193326Sed  // If the directory exists, add it.
145218893Sdim  if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
146219077Sdim    IncludePath.push_back(std::make_pair(Group, DirectoryLookup(DE, Type,
147219077Sdim                          isUserSupplied, isFramework)));
148193326Sed    return;
149193326Sed  }
150198092Srdivacky
151193326Sed  // Check to see if this is an apple-style headermap (which are not allowed to
152193326Sed  // be frameworks).
153193326Sed  if (!isFramework) {
154218893Sdim    if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
155193326Sed      if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
156193326Sed        // It is a headermap, add it to the search path.
157219077Sdim        IncludePath.push_back(std::make_pair(Group, DirectoryLookup(HM, Type,
158226633Sdim                              isUserSupplied, Group == IndexHeaderMap)));
159193326Sed        return;
160193326Sed      }
161193326Sed    }
162193326Sed  }
163198092Srdivacky
164193326Sed  if (Verbose)
165198092Srdivacky    llvm::errs() << "ignoring nonexistent directory \""
166218893Sdim                 << MappedPathStr << "\"\n";
167193326Sed}
168193326Sed
169226633Sdimvoid InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
170226633Sdim                                                   StringRef ArchDir,
171226633Sdim                                                   StringRef Dir32,
172226633Sdim                                                   StringRef Dir64,
173198092Srdivacky                                                   const llvm::Triple &triple) {
174199990Srdivacky  // Add the base dir
175219077Sdim  AddPath(Base, CXXSystem, true, false, false);
176199482Srdivacky
177199482Srdivacky  // Add the multilib dirs
178198092Srdivacky  llvm::Triple::ArchType arch = triple.getArch();
179198092Srdivacky  bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
180198092Srdivacky  if (is64bit)
181219077Sdim    AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, true, false, false);
182198092Srdivacky  else
183219077Sdim    AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, true, false, false);
184199990Srdivacky
185199990Srdivacky  // Add the backward dir
186219077Sdim  AddPath(Base + "/backward", CXXSystem, true, false, false);
187198092Srdivacky}
188193326Sed
189226633Sdimvoid InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
190226633Sdim                                                     StringRef Arch,
191226633Sdim                                                     StringRef Version) {
192202879Srdivacky  AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
193219077Sdim          CXXSystem, true, false, false);
194212904Sdim  AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
195219077Sdim          CXXSystem, true, false, false);
196202879Srdivacky  AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
197219077Sdim          CXXSystem, true, false, false);
198198092Srdivacky}
199198092Srdivacky
200226633Sdimvoid InitHeaderSearch::AddMinGW64CXXPaths(StringRef Base,
201226633Sdim                                          StringRef Version) {
202224145Sdim  // Assumes Base is HeaderSearchOpts' ResourceDir
203224145Sdim  AddPath(Base + "/../../../include/c++/" + Version,
204221345Sdim          CXXSystem, true, false, false);
205224145Sdim  AddPath(Base + "/../../../include/c++/" + Version + "/x86_64-w64-mingw32",
206221345Sdim          CXXSystem, true, false, false);
207224145Sdim  AddPath(Base + "/../../../include/c++/" + Version + "/i686-w64-mingw32",
208221345Sdim          CXXSystem, true, false, false);
209224145Sdim  AddPath(Base + "/../../../include/c++/" + Version + "/backward",
210224145Sdim          CXXSystem, true, false, false);
211221345Sdim}
212221345Sdim
213208600Srdivackyvoid InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
214208600Srdivacky                                            const HeaderSearchOptions &HSOpts) {
215218893Sdim  llvm::Triple::OSType os = triple.getOS();
216208600Srdivacky
217226633Sdim  if (HSOpts.UseStandardSystemIncludes) {
218226633Sdim    switch (os) {
219226633Sdim    case llvm::Triple::FreeBSD:
220226633Sdim    case llvm::Triple::NetBSD:
221239462Sdim    case llvm::Triple::OpenBSD:
222239462Sdim    case llvm::Triple::Bitrig:
223226633Sdim      break;
224226633Sdim    default:
225226633Sdim      // FIXME: temporary hack: hard-coded paths.
226226633Sdim      AddPath("/usr/local/include", System, true, false, false);
227226633Sdim      break;
228226633Sdim    }
229218893Sdim  }
230218893Sdim
231208600Srdivacky  // Builtin includes use #include_next directives and should be positioned
232208600Srdivacky  // just prior C include dirs.
233208600Srdivacky  if (HSOpts.UseBuiltinIncludes) {
234208600Srdivacky    // Ignore the sys root, we *always* look for clang headers relative to
235208600Srdivacky    // supplied path.
236208600Srdivacky    llvm::sys::Path P(HSOpts.ResourceDir);
237208600Srdivacky    P.appendComponent("include");
238208600Srdivacky    AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
239208600Srdivacky  }
240208600Srdivacky
241226633Sdim  // All remaining additions are for system include directories, early exit if
242226633Sdim  // we aren't using them.
243226633Sdim  if (!HSOpts.UseStandardSystemIncludes)
244226633Sdim    return;
245226633Sdim
246208600Srdivacky  // Add dirs specified via 'configure --with-c-include-dirs'.
247226633Sdim  StringRef CIncludeDirs(C_INCLUDE_DIRS);
248199482Srdivacky  if (CIncludeDirs != "") {
249226633Sdim    SmallVector<StringRef, 5> dirs;
250199482Srdivacky    CIncludeDirs.split(dirs, ":");
251226633Sdim    for (SmallVectorImpl<StringRef>::iterator i = dirs.begin();
252199482Srdivacky         i != dirs.end();
253218893Sdim         ++i)
254199482Srdivacky      AddPath(*i, System, false, false, false);
255199482Srdivacky    return;
256199482Srdivacky  }
257218893Sdim
258198092Srdivacky  switch (os) {
259228379Sdim  case llvm::Triple::Linux:
260228379Sdim  case llvm::Triple::Win32:
261228379Sdim    llvm_unreachable("Include management is handled in the driver.");
262228379Sdim
263207619Srdivacky  case llvm::Triple::Haiku:
264207619Srdivacky    AddPath("/boot/common/include", System, true, false, false);
265207619Srdivacky    AddPath("/boot/develop/headers/os", System, true, false, false);
266207619Srdivacky    AddPath("/boot/develop/headers/os/app", System, true, false, false);
267207619Srdivacky    AddPath("/boot/develop/headers/os/arch", System, true, false, false);
268207619Srdivacky    AddPath("/boot/develop/headers/os/device", System, true, false, false);
269207619Srdivacky    AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
270218893Sdim    AddPath("/boot/develop/headers/os/game", System, true, false, false);
271207619Srdivacky    AddPath("/boot/develop/headers/os/interface", System, true, false, false);
272207619Srdivacky    AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
273207619Srdivacky    AddPath("/boot/develop/headers/os/locale", System, true, false, false);
274207619Srdivacky    AddPath("/boot/develop/headers/os/mail", System, true, false, false);
275207619Srdivacky    AddPath("/boot/develop/headers/os/media", System, true, false, false);
276207619Srdivacky    AddPath("/boot/develop/headers/os/midi", System, true, false, false);
277207619Srdivacky    AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
278207619Srdivacky    AddPath("/boot/develop/headers/os/net", System, true, false, false);
279207619Srdivacky    AddPath("/boot/develop/headers/os/storage", System, true, false, false);
280207619Srdivacky    AddPath("/boot/develop/headers/os/support", System, true, false, false);
281207619Srdivacky    AddPath("/boot/develop/headers/os/translation",
282207619Srdivacky      System, true, false, false);
283218893Sdim    AddPath("/boot/develop/headers/os/add-ons/graphics",
284207619Srdivacky      System, true, false, false);
285218893Sdim    AddPath("/boot/develop/headers/os/add-ons/input_server",
286207619Srdivacky      System, true, false, false);
287218893Sdim    AddPath("/boot/develop/headers/os/add-ons/screen_saver",
288207619Srdivacky      System, true, false, false);
289218893Sdim    AddPath("/boot/develop/headers/os/add-ons/tracker",
290207619Srdivacky      System, true, false, false);
291207619Srdivacky    AddPath("/boot/develop/headers/os/be_apps/Deskbar",
292207619Srdivacky      System, true, false, false);
293207619Srdivacky    AddPath("/boot/develop/headers/os/be_apps/NetPositive",
294207619Srdivacky      System, true, false, false);
295207619Srdivacky    AddPath("/boot/develop/headers/os/be_apps/Tracker",
296207619Srdivacky      System, true, false, false);
297207619Srdivacky    AddPath("/boot/develop/headers/cpp", System, true, false, false);
298218893Sdim    AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
299207619Srdivacky      System, true, false, false);
300207619Srdivacky    AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
301207619Srdivacky    AddPath("/boot/develop/headers/bsd", System, true, false, false);
302207619Srdivacky    AddPath("/boot/develop/headers/glibc", System, true, false, false);
303207619Srdivacky    AddPath("/boot/develop/headers/posix", System, true, false, false);
304207619Srdivacky    AddPath("/boot/develop/headers",  System, true, false, false);
305212904Sdim    break;
306224145Sdim  case llvm::Triple::RTEMS:
307224145Sdim    break;
308218893Sdim  case llvm::Triple::Cygwin:
309218893Sdim    AddPath("/usr/include/w32api", System, true, false, false);
310218893Sdim    break;
311224145Sdim  case llvm::Triple::MinGW32: {
312224145Sdim      // mingw-w64 crt include paths
313224145Sdim      llvm::sys::Path P(HSOpts.ResourceDir);
314224145Sdim      P.appendComponent("../../../i686-w64-mingw32/include"); // <sysroot>/i686-w64-mingw32/include
315224145Sdim      AddPath(P.str(), System, true, false, false);
316224145Sdim      P = llvm::sys::Path(HSOpts.ResourceDir);
317224145Sdim      P.appendComponent("../../../x86_64-w64-mingw32/include"); // <sysroot>/x86_64-w64-mingw32/include
318224145Sdim      AddPath(P.str(), System, true, false, false);
319224145Sdim      // mingw.org crt include paths
320224145Sdim      P = llvm::sys::Path(HSOpts.ResourceDir);
321224145Sdim      P.appendComponent("../../../include"); // <sysroot>/include
322224145Sdim      AddPath(P.str(), System, true, false, false);
323224145Sdim      AddPath("/mingw/include", System, true, false, false);
324224145Sdim      AddPath("c:/mingw/include", System, true, false, false);
325224145Sdim    }
326198893Srdivacky    break;
327211573Srpaulo  case llvm::Triple::FreeBSD:
328234982Sdim    AddPath("/usr/include/clang/" CLANG_VERSION_STRING,
329211573Srpaulo      System, false, false, false);
330211573Srpaulo    break;
331224145Sdim
332198893Srdivacky  default:
333198893Srdivacky    break;
334198893Srdivacky  }
335198893Srdivacky
336224145Sdim  if ( os != llvm::Triple::RTEMS )
337234982Sdim    AddPath("/usr/include", System, false, false, false);
338198893Srdivacky}
339198893Srdivacky
340208600Srdivackyvoid InitHeaderSearch::
341224145SdimAddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
342198893Srdivacky  llvm::Triple::OSType os = triple.getOS();
343198893Srdivacky  // FIXME: temporary hack: hard-coded paths.
344221345Sdim
345221345Sdim  if (triple.isOSDarwin()) {
346210299Sed    switch (triple.getArch()) {
347210299Sed    default: break;
348210299Sed
349218893Sdim    case llvm::Triple::ppc:
350210299Sed    case llvm::Triple::ppc64:
351210299Sed      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
352218893Sdim                                  "powerpc-apple-darwin10", "", "ppc64",
353210299Sed                                  triple);
354210299Sed      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
355218893Sdim                                  "powerpc-apple-darwin10", "", "ppc64",
356210299Sed                                  triple);
357210299Sed      break;
358210299Sed
359210299Sed    case llvm::Triple::x86:
360210299Sed    case llvm::Triple::x86_64:
361210299Sed      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
362210299Sed                                  "i686-apple-darwin10", "", "x86_64", triple);
363210299Sed      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
364210299Sed                                  "i686-apple-darwin8", "", "", triple);
365210299Sed      break;
366210299Sed
367210299Sed    case llvm::Triple::arm:
368210299Sed    case llvm::Triple::thumb:
369210299Sed      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
370210299Sed                                  "arm-apple-darwin10", "v7", "", triple);
371210299Sed      AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
372210299Sed                                  "arm-apple-darwin10", "v6", "", triple);
373210299Sed      break;
374210299Sed    }
375221345Sdim    return;
376221345Sdim  }
377221345Sdim
378221345Sdim  switch (os) {
379228379Sdim  case llvm::Triple::Linux:
380228379Sdim  case llvm::Triple::Win32:
381228379Sdim    llvm_unreachable("Include management is handled in the driver.");
382228379Sdim
383221345Sdim  case llvm::Triple::Cygwin:
384221345Sdim    // Cygwin-1.7
385234353Sdim    AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3");
386221345Sdim    AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
387221345Sdim    // g++-4 / Cygwin-1.5
388221345Sdim    AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
389198893Srdivacky    break;
390221345Sdim  case llvm::Triple::MinGW32:
391224145Sdim    // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
392224145Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0");
393224145Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1");
394224145Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2");
395224145Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3");
396234353Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.4");
397224145Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0");
398224145Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1");
399224145Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2");
400234353Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.3");
401224145Sdim    AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
402224145Sdim    // mingw.org C++ include paths
403224145Sdim    AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS
404234353Sdim    AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.2");
405234353Sdim    AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.1");
406234353Sdim    AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.2");
407221345Sdim    AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
408221345Sdim    AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
409221345Sdim    AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
410221345Sdim    break;
411202379Srdivacky  case llvm::Triple::DragonFly:
412219077Sdim    AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
413202379Srdivacky    break;
414198893Srdivacky  case llvm::Triple::FreeBSD:
415208600Srdivacky    // FreeBSD 8.0
416208600Srdivacky    // FreeBSD 7.3
417239462Sdim    AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
418239462Sdim                                "", "", "", triple);
419234982Sdim    AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2/backward",
420211573Srpaulo                                "", "", "", triple);
421198893Srdivacky    break;
422212904Sdim  case llvm::Triple::NetBSD:
423212904Sdim    AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
424212904Sdim    break;
425212904Sdim  case llvm::Triple::OpenBSD: {
426212904Sdim    std::string t = triple.getTriple();
427212904Sdim    if (t.substr(0, 6) == "x86_64")
428212904Sdim      t.replace(0, 6, "amd64");
429212904Sdim    AddGnuCPlusPlusIncludePaths("/usr/include/g++",
430212904Sdim                                t, "", "", triple);
431212904Sdim    break;
432212904Sdim  }
433210299Sed  case llvm::Triple::Minix:
434210299Sed    AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
435210299Sed                                "", "", "", triple);
436210299Sed    break;
437198893Srdivacky  case llvm::Triple::Solaris:
438234353Sdim    AddGnuCPlusPlusIncludePaths("/usr/gcc/4.5/include/c++/4.5.2/",
439234353Sdim                                "i386-pc-solaris2.11", "", "", triple);
440198893Srdivacky    // Solaris - Fall though..
441198893Srdivacky  case llvm::Triple::AuroraUX:
442198893Srdivacky    // AuroraUX
443198893Srdivacky    AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
444199482Srdivacky                                "i386-pc-solaris2.11", "", "", triple);
445198893Srdivacky    break;
446198092Srdivacky  default:
447198092Srdivacky    break;
448193326Sed  }
449198893Srdivacky}
450193326Sed
451226633Sdimvoid InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
452226633Sdim                                              const llvm::Triple &triple,
453208600Srdivacky                                            const HeaderSearchOptions &HSOpts) {
454228379Sdim  // NB: This code path is going away. All of the logic is moving into the
455228379Sdim  // driver which has the information necessary to do target-specific
456228379Sdim  // selections of default include paths. Each target which moves there will be
457228379Sdim  // exempted from this logic here until we can delete the entire pile of code.
458228379Sdim  switch (triple.getOS()) {
459228379Sdim  default:
460228379Sdim    break; // Everything else continues to use this routine's logic.
461228379Sdim
462228379Sdim  case llvm::Triple::Linux:
463228379Sdim  case llvm::Triple::Win32:
464228379Sdim    return;
465228379Sdim  }
466228379Sdim
467226633Sdim  if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes &&
468226633Sdim      HSOpts.UseStandardSystemIncludes) {
469226633Sdim    if (HSOpts.UseLibcxx) {
470226633Sdim      if (triple.isOSDarwin()) {
471226633Sdim        // On Darwin, libc++ may be installed alongside the compiler in
472226633Sdim        // lib/c++/v1.
473226633Sdim        llvm::sys::Path P(HSOpts.ResourceDir);
474226633Sdim        if (!P.isEmpty()) {
475226633Sdim          P.eraseComponent();  // Remove version from foo/lib/clang/version
476226633Sdim          P.eraseComponent();  // Remove clang from foo/lib/clang
477226633Sdim
478226633Sdim          // Get foo/lib/c++/v1
479226633Sdim          P.appendComponent("c++");
480226633Sdim          P.appendComponent("v1");
481226633Sdim          AddPath(P.str(), CXXSystem, true, false, false, true);
482226633Sdim        }
483226633Sdim      }
484234353Sdim      // On Solaris, include the support directory for things like xlocale and
485234353Sdim      // fudged system headers.
486234353Sdim      if (triple.getOS() == llvm::Triple::Solaris)
487234353Sdim        AddPath("/usr/include/c++/v1/support/solaris", CXXSystem, true, false,
488234353Sdim            false);
489226633Sdim
490224145Sdim      AddPath("/usr/include/c++/v1", CXXSystem, true, false, false);
491226633Sdim    } else {
492224145Sdim      AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
493226633Sdim    }
494224145Sdim  }
495199990Srdivacky
496208600Srdivacky  AddDefaultCIncludePaths(triple, HSOpts);
497199482Srdivacky
498199482Srdivacky  // Add the default framework include paths on Darwin.
499226633Sdim  if (HSOpts.UseStandardSystemIncludes) {
500226633Sdim    if (triple.isOSDarwin()) {
501226633Sdim      AddPath("/System/Library/Frameworks", System, true, false, true);
502226633Sdim      AddPath("/Library/Frameworks", System, true, false, true);
503226633Sdim    }
504199482Srdivacky  }
505198893Srdivacky}
506198893Srdivacky
507193326Sed/// RemoveDuplicates - If there are duplicate directory entries in the specified
508226633Sdim/// search list, remove the later (dead) ones.  Returns the number of non-system
509226633Sdim/// headers removed, which is used to update NumAngled.
510226633Sdimstatic unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
511226633Sdim                                 unsigned First, bool Verbose) {
512193326Sed  llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
513193326Sed  llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
514193326Sed  llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
515226633Sdim  unsigned NonSystemRemoved = 0;
516219077Sdim  for (unsigned i = First; i != SearchList.size(); ++i) {
517193326Sed    unsigned DirToRemove = i;
518198092Srdivacky
519193326Sed    const DirectoryLookup &CurEntry = SearchList[i];
520198092Srdivacky
521193326Sed    if (CurEntry.isNormalDir()) {
522193326Sed      // If this isn't the first time we've seen this dir, remove it.
523193326Sed      if (SeenDirs.insert(CurEntry.getDir()))
524193326Sed        continue;
525193326Sed    } else if (CurEntry.isFramework()) {
526193326Sed      // If this isn't the first time we've seen this framework dir, remove it.
527193326Sed      if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
528193326Sed        continue;
529193326Sed    } else {
530193326Sed      assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
531193326Sed      // If this isn't the first time we've seen this headermap, remove it.
532193326Sed      if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
533193326Sed        continue;
534193326Sed    }
535198092Srdivacky
536193326Sed    // If we have a normal #include dir/framework/headermap that is shadowed
537193326Sed    // later in the chain by a system include location, we actually want to
538193326Sed    // ignore the user's request and drop the user dir... keeping the system
539193326Sed    // dir.  This is weird, but required to emulate GCC's search path correctly.
540193326Sed    //
541193326Sed    // Since dupes of system dirs are rare, just rescan to find the original
542193326Sed    // that we're nuking instead of using a DenseMap.
543193326Sed    if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
544193326Sed      // Find the dir that this is the same of.
545193326Sed      unsigned FirstDir;
546193326Sed      for (FirstDir = 0; ; ++FirstDir) {
547193326Sed        assert(FirstDir != i && "Didn't find dupe?");
548198092Srdivacky
549193326Sed        const DirectoryLookup &SearchEntry = SearchList[FirstDir];
550193326Sed
551193326Sed        // If these are different lookup types, then they can't be the dupe.
552193326Sed        if (SearchEntry.getLookupType() != CurEntry.getLookupType())
553193326Sed          continue;
554198092Srdivacky
555193326Sed        bool isSame;
556193326Sed        if (CurEntry.isNormalDir())
557193326Sed          isSame = SearchEntry.getDir() == CurEntry.getDir();
558193326Sed        else if (CurEntry.isFramework())
559193326Sed          isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
560193326Sed        else {
561193326Sed          assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
562193326Sed          isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
563193326Sed        }
564198092Srdivacky
565193326Sed        if (isSame)
566193326Sed          break;
567193326Sed      }
568198092Srdivacky
569193326Sed      // If the first dir in the search path is a non-system dir, zap it
570193326Sed      // instead of the system one.
571193326Sed      if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
572193326Sed        DirToRemove = FirstDir;
573193326Sed    }
574193326Sed
575193326Sed    if (Verbose) {
576200583Srdivacky      llvm::errs() << "ignoring duplicate directory \""
577200583Srdivacky                   << CurEntry.getName() << "\"\n";
578193326Sed      if (DirToRemove != i)
579200583Srdivacky        llvm::errs() << "  as it is a non-system directory that duplicates "
580200583Srdivacky                     << "a system directory\n";
581193326Sed    }
582226633Sdim    if (DirToRemove != i)
583226633Sdim      ++NonSystemRemoved;
584198092Srdivacky
585193326Sed    // This is reached if the current entry is a duplicate.  Remove the
586193326Sed    // DirToRemove (usually the current dir).
587193326Sed    SearchList.erase(SearchList.begin()+DirToRemove);
588193326Sed    --i;
589193326Sed  }
590226633Sdim  return NonSystemRemoved;
591193326Sed}
592193326Sed
593193326Sed
594219077Sdimvoid InitHeaderSearch::Realize(const LangOptions &Lang) {
595193326Sed  // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
596193326Sed  std::vector<DirectoryLookup> SearchList;
597219077Sdim  SearchList.reserve(IncludePath.size());
598198092Srdivacky
599224145Sdim  // Quoted arguments go first.
600219077Sdim  for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
601219077Sdim       it != ie; ++it) {
602219077Sdim    if (it->first == Quoted)
603219077Sdim      SearchList.push_back(it->second);
604219077Sdim  }
605224145Sdim  // Deduplicate and remember index.
606219077Sdim  RemoveDuplicates(SearchList, 0, Verbose);
607224145Sdim  unsigned NumQuoted = SearchList.size();
608193326Sed
609219077Sdim  for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
610219077Sdim       it != ie; ++it) {
611226633Sdim    if (it->first == Angled || it->first == IndexHeaderMap)
612219077Sdim      SearchList.push_back(it->second);
613219077Sdim  }
614198092Srdivacky
615224145Sdim  RemoveDuplicates(SearchList, NumQuoted, Verbose);
616224145Sdim  unsigned NumAngled = SearchList.size();
617224145Sdim
618219077Sdim  for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
619219077Sdim       it != ie; ++it) {
620226633Sdim    if (it->first == System ||
621226633Sdim        (!Lang.ObjC1 && !Lang.CPlusPlus && it->first == CSystem)    ||
622226633Sdim        (/*FIXME !Lang.ObjC1 && */Lang.CPlusPlus  && it->first == CXXSystem)  ||
623226633Sdim        (Lang.ObjC1  && !Lang.CPlusPlus && it->first == ObjCSystem) ||
624226633Sdim        (Lang.ObjC1  && Lang.CPlusPlus  && it->first == ObjCXXSystem))
625219077Sdim      SearchList.push_back(it->second);
626219077Sdim  }
627219077Sdim
628219077Sdim  for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
629219077Sdim       it != ie; ++it) {
630219077Sdim    if (it->first == After)
631219077Sdim      SearchList.push_back(it->second);
632219077Sdim  }
633219077Sdim
634224145Sdim  // Remove duplicates across both the Angled and System directories.  GCC does
635224145Sdim  // this and failing to remove duplicates across these two groups breaks
636224145Sdim  // #include_next.
637226633Sdim  unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
638226633Sdim  NumAngled -= NonSystemRemoved;
639219077Sdim
640193326Sed  bool DontSearchCurDir = false;  // TODO: set to true if -I- is set?
641224145Sdim  Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
642193326Sed
643239462Sdim  Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
644239462Sdim
645193326Sed  // If verbose, print the list of directories that will be searched.
646193326Sed  if (Verbose) {
647200583Srdivacky    llvm::errs() << "#include \"...\" search starts here:\n";
648193326Sed    for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
649224145Sdim      if (i == NumQuoted)
650200583Srdivacky        llvm::errs() << "#include <...> search starts here:\n";
651193326Sed      const char *Name = SearchList[i].getName();
652193326Sed      const char *Suffix;
653193326Sed      if (SearchList[i].isNormalDir())
654193326Sed        Suffix = "";
655193326Sed      else if (SearchList[i].isFramework())
656193326Sed        Suffix = " (framework directory)";
657193326Sed      else {
658193326Sed        assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
659193326Sed        Suffix = " (headermap)";
660193326Sed      }
661200583Srdivacky      llvm::errs() << " " << Name << Suffix << "\n";
662193326Sed    }
663200583Srdivacky    llvm::errs() << "End of search list.\n";
664193326Sed  }
665193326Sed}
666199482Srdivacky
667199482Srdivackyvoid clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
668199482Srdivacky                                     const HeaderSearchOptions &HSOpts,
669199482Srdivacky                                     const LangOptions &Lang,
670199482Srdivacky                                     const llvm::Triple &Triple) {
671199482Srdivacky  InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
672199482Srdivacky
673199482Srdivacky  // Add the user defined entries.
674199482Srdivacky  for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
675199482Srdivacky    const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
676228379Sdim    Init.AddPath(E.Path, E.Group, !E.ImplicitExternC, E.IsUserSupplied,
677228379Sdim                 E.IsFramework, E.IgnoreSysRoot);
678199482Srdivacky  }
679199482Srdivacky
680226633Sdim  Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
681199482Srdivacky
682239462Sdim  for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
683239462Sdim    Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix,
684239462Sdim                               HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
685239462Sdim
686234353Sdim  if (HSOpts.UseBuiltinIncludes) {
687234353Sdim    // Set up the builtin include directory in the module map.
688234353Sdim    llvm::sys::Path P(HSOpts.ResourceDir);
689234353Sdim    P.appendComponent("include");
690234353Sdim    if (const DirectoryEntry *Dir = HS.getFileMgr().getDirectory(P.str()))
691234353Sdim      HS.getModuleMap().setBuiltinIncludeDir(Dir);
692234353Sdim  }
693234353Sdim
694219077Sdim  Init.Realize(Lang);
695199482Srdivacky}
696