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