clang  8.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
18 #include "clang/Frontend/Utils.h"
19 #include "clang/Lex/HeaderMap.h"
20 #include "clang/Lex/HeaderSearch.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/ADT/Twine.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/raw_ostream.h"
31 
32 using namespace clang;
33 using namespace clang::frontend;
34 
35 namespace {
36 
37 /// InitHeaderSearch - This class makes it easier to set the search paths of
38 /// a HeaderSearch object. InitHeaderSearch stores several search path lists
39 /// internally, which can be sent to a HeaderSearch object in one swoop.
40 class InitHeaderSearch {
41  std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
42  typedef std::vector<std::pair<IncludeDirGroup,
43  DirectoryLookup> >::const_iterator path_iterator;
44  std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
45  HeaderSearch &Headers;
46  bool Verbose;
47  std::string IncludeSysroot;
48  bool HasSysroot;
49 
50 public:
51 
52  InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
53  : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
54  HasSysroot(!(sysroot.empty() || sysroot == "/")) {
55  }
56 
57  /// AddPath - Add the specified path to the specified group list, prefixing
58  /// the sysroot if used.
59  /// Returns true if the path exists, false if it was ignored.
60  bool AddPath(const Twine &Path, IncludeDirGroup Group, bool isFramework);
61 
62  /// AddUnmappedPath - Add the specified path to the specified group list,
63  /// without performing any sysroot remapping.
64  /// Returns true if the path exists, false if it was ignored.
65  bool AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
66  bool isFramework);
67 
68  /// AddSystemHeaderPrefix - Add the specified prefix to the system header
69  /// prefix list.
70  void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
71  SystemHeaderPrefixes.emplace_back(Prefix, IsSystemHeader);
72  }
73 
74  /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
75  /// libstdc++.
76  /// Returns true if the \p Base path was found, false if it does not exist.
77  bool AddGnuCPlusPlusIncludePaths(StringRef Base, StringRef ArchDir,
78  StringRef Dir32, StringRef Dir64,
79  const llvm::Triple &triple);
80 
81  /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
82  /// libstdc++.
83  void AddMinGWCPlusPlusIncludePaths(StringRef Base,
84  StringRef Arch,
85  StringRef Version);
86 
87  // AddDefaultCIncludePaths - Add paths that should always be searched.
88  void AddDefaultCIncludePaths(const llvm::Triple &triple,
89  const HeaderSearchOptions &HSOpts);
90 
91  // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
92  // compiling c++.
93  void AddDefaultCPlusPlusIncludePaths(const LangOptions &LangOpts,
94  const llvm::Triple &triple,
95  const HeaderSearchOptions &HSOpts);
96 
97  /// AddDefaultSystemIncludePaths - Adds the default system include paths so
98  /// that e.g. stdio.h is found.
99  void AddDefaultIncludePaths(const LangOptions &Lang,
100  const llvm::Triple &triple,
101  const HeaderSearchOptions &HSOpts);
102 
103  /// Realize - Merges all search path lists into one list and send it to
104  /// HeaderSearch.
105  void Realize(const LangOptions &Lang);
106 };
107 
108 } // end anonymous namespace.
109 
110 static bool CanPrefixSysroot(StringRef Path) {
111 #if defined(_WIN32)
112  return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
113 #else
114  return llvm::sys::path::is_absolute(Path);
115 #endif
116 }
117 
118 bool InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group,
119  bool isFramework) {
120  // Add the path with sysroot prepended, if desired and this is a system header
121  // group.
122  if (HasSysroot) {
123  SmallString<256> MappedPathStorage;
124  StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
125  if (CanPrefixSysroot(MappedPathStr)) {
126  return AddUnmappedPath(IncludeSysroot + Path, Group, isFramework);
127  }
128  }
129 
130  return AddUnmappedPath(Path, Group, isFramework);
131 }
132 
133 bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
134  bool isFramework) {
135  assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
136 
137  FileManager &FM = Headers.getFileMgr();
138  SmallString<256> MappedPathStorage;
139  StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
140 
141  // Compute the DirectoryLookup type.
143  if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
144  Type = SrcMgr::C_User;
145  } else if (Group == ExternCSystem) {
147  } else {
148  Type = SrcMgr::C_System;
149  }
150 
151  // If the directory exists, add it.
152  if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
153  IncludePath.push_back(
154  std::make_pair(Group, DirectoryLookup(DE, Type, isFramework)));
155  return true;
156  }
157 
158  // Check to see if this is an apple-style headermap (which are not allowed to
159  // be frameworks).
160  if (!isFramework) {
161  if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
162  if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
163  // It is a headermap, add it to the search path.
164  IncludePath.push_back(
165  std::make_pair(Group,
166  DirectoryLookup(HM, Type, Group == IndexHeaderMap)));
167  return true;
168  }
169  }
170  }
171 
172  if (Verbose)
173  llvm::errs() << "ignoring nonexistent directory \""
174  << MappedPathStr << "\"\n";
175  return false;
176 }
177 
178 bool InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
179  StringRef ArchDir,
180  StringRef Dir32,
181  StringRef Dir64,
182  const llvm::Triple &triple) {
183  // Add the base dir
184  bool IsBaseFound = AddPath(Base, CXXSystem, false);
185 
186  // Add the multilib dirs
187  llvm::Triple::ArchType arch = triple.getArch();
188  bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
189  if (is64bit)
190  AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false);
191  else
192  AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false);
193 
194  // Add the backward dir
195  AddPath(Base + "/backward", CXXSystem, false);
196  return IsBaseFound;
197 }
198 
199 void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
200  StringRef Arch,
201  StringRef Version) {
202  AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
203  CXXSystem, false);
204  AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
205  CXXSystem, false);
206  AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
207  CXXSystem, false);
208 }
209 
210 void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
211  const HeaderSearchOptions &HSOpts) {
212  llvm::Triple::OSType os = triple.getOS();
213 
214  if (HSOpts.UseStandardSystemIncludes) {
215  switch (os) {
216  case llvm::Triple::CloudABI:
217  case llvm::Triple::FreeBSD:
218  case llvm::Triple::NetBSD:
219  case llvm::Triple::OpenBSD:
220  case llvm::Triple::NaCl:
221  case llvm::Triple::PS4:
222  case llvm::Triple::ELFIAMCU:
223  case llvm::Triple::Fuchsia:
224  break;
225  case llvm::Triple::Win32:
226  if (triple.getEnvironment() != llvm::Triple::Cygnus)
227  break;
228  LLVM_FALLTHROUGH;
229  default:
230  // FIXME: temporary hack: hard-coded paths.
231  AddPath("/usr/local/include", System, false);
232  break;
233  }
234  }
235 
236  // Builtin includes use #include_next directives and should be positioned
237  // just prior C include dirs.
238  if (HSOpts.UseBuiltinIncludes) {
239  // Ignore the sys root, we *always* look for clang headers relative to
240  // supplied path.
241  SmallString<128> P = StringRef(HSOpts.ResourceDir);
242  llvm::sys::path::append(P, "include");
243  AddUnmappedPath(P, ExternCSystem, false);
244  }
245 
246  // All remaining additions are for system include directories, early exit if
247  // we aren't using them.
248  if (!HSOpts.UseStandardSystemIncludes)
249  return;
250 
251  // Add dirs specified via 'configure --with-c-include-dirs'.
252  StringRef CIncludeDirs(C_INCLUDE_DIRS);
253  if (CIncludeDirs != "") {
255  CIncludeDirs.split(dirs, ":");
256  for (StringRef dir : dirs)
257  AddPath(dir, ExternCSystem, false);
258  return;
259  }
260 
261  switch (os) {
262  case llvm::Triple::Linux:
263  case llvm::Triple::Hurd:
264  case llvm::Triple::Solaris:
265  llvm_unreachable("Include management is handled in the driver.");
266 
267  case llvm::Triple::CloudABI: {
268  // <sysroot>/<triple>/include
269  SmallString<128> P = StringRef(HSOpts.ResourceDir);
270  llvm::sys::path::append(P, "../../..", triple.str(), "include");
271  AddPath(P, System, false);
272  break;
273  }
274 
275  case llvm::Triple::Haiku:
276  AddPath("/boot/system/non-packaged/develop/headers", System, false);
277  AddPath("/boot/system/develop/headers/os", System, false);
278  AddPath("/boot/system/develop/headers/os/app", System, false);
279  AddPath("/boot/system/develop/headers/os/arch", System, false);
280  AddPath("/boot/system/develop/headers/os/device", System, false);
281  AddPath("/boot/system/develop/headers/os/drivers", System, false);
282  AddPath("/boot/system/develop/headers/os/game", System, false);
283  AddPath("/boot/system/develop/headers/os/interface", System, false);
284  AddPath("/boot/system/develop/headers/os/kernel", System, false);
285  AddPath("/boot/system/develop/headers/os/locale", System, false);
286  AddPath("/boot/system/develop/headers/os/mail", System, false);
287  AddPath("/boot/system/develop/headers/os/media", System, false);
288  AddPath("/boot/system/develop/headers/os/midi", System, false);
289  AddPath("/boot/system/develop/headers/os/midi2", System, false);
290  AddPath("/boot/system/develop/headers/os/net", System, false);
291  AddPath("/boot/system/develop/headers/os/opengl", System, false);
292  AddPath("/boot/system/develop/headers/os/storage", System, false);
293  AddPath("/boot/system/develop/headers/os/support", System, false);
294  AddPath("/boot/system/develop/headers/os/translation", System, false);
295  AddPath("/boot/system/develop/headers/os/add-ons/graphics", System, false);
296  AddPath("/boot/system/develop/headers/os/add-ons/input_server", System, false);
297  AddPath("/boot/system/develop/headers/os/add-ons/mail_daemon", System, false);
298  AddPath("/boot/system/develop/headers/os/add-ons/registrar", System, false);
299  AddPath("/boot/system/develop/headers/os/add-ons/screen_saver", System, false);
300  AddPath("/boot/system/develop/headers/os/add-ons/tracker", System, false);
301  AddPath("/boot/system/develop/headers/os/be_apps/Deskbar", System, false);
302  AddPath("/boot/system/develop/headers/os/be_apps/NetPositive", System, false);
303  AddPath("/boot/system/develop/headers/os/be_apps/Tracker", System, false);
304  AddPath("/boot/system/develop/headers/3rdparty", System, false);
305  AddPath("/boot/system/develop/headers/bsd", System, false);
306  AddPath("/boot/system/develop/headers/glibc", System, false);
307  AddPath("/boot/system/develop/headers/posix", System, false);
308  AddPath("/boot/system/develop/headers", System, false);
309  break;
310  case llvm::Triple::RTEMS:
311  break;
312  case llvm::Triple::Win32:
313  switch (triple.getEnvironment()) {
314  default: llvm_unreachable("Include management is handled in the driver.");
315  case llvm::Triple::Cygnus:
316  AddPath("/usr/include/w32api", System, false);
317  break;
318  case llvm::Triple::GNU:
319  break;
320  }
321  break;
322  default:
323  break;
324  }
325 
326  switch (os) {
327  case llvm::Triple::CloudABI:
328  case llvm::Triple::RTEMS:
329  case llvm::Triple::NaCl:
330  case llvm::Triple::ELFIAMCU:
331  case llvm::Triple::Fuchsia:
332  break;
333  case llvm::Triple::PS4: {
334  // <isysroot> gets prepended later in AddPath().
335  std::string BaseSDKPath = "";
336  if (!HasSysroot) {
337  const char *envValue = getenv("SCE_ORBIS_SDK_DIR");
338  if (envValue)
339  BaseSDKPath = envValue;
340  else {
341  // HSOpts.ResourceDir variable contains the location of Clang's
342  // resource files.
343  // Assuming that Clang is configured for PS4 without
344  // --with-clang-resource-dir option, the location of Clang's resource
345  // files is <SDK_DIR>/host_tools/lib/clang
346  SmallString<128> P = StringRef(HSOpts.ResourceDir);
347  llvm::sys::path::append(P, "../../..");
348  BaseSDKPath = P.str();
349  }
350  }
351  AddPath(BaseSDKPath + "/target/include", System, false);
352  if (triple.isPS4CPU())
353  AddPath(BaseSDKPath + "/target/include_common", System, false);
354  LLVM_FALLTHROUGH;
355  }
356  default:
357  AddPath("/usr/include", ExternCSystem, false);
358  break;
359  }
360 }
361 
362 void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(
363  const LangOptions &LangOpts, const llvm::Triple &triple,
364  const HeaderSearchOptions &HSOpts) {
365  llvm::Triple::OSType os = triple.getOS();
366  // FIXME: temporary hack: hard-coded paths.
367 
368  if (triple.isOSDarwin()) {
369  bool IsBaseFound = true;
370  switch (triple.getArch()) {
371  default: break;
372 
373  case llvm::Triple::ppc:
374  case llvm::Triple::ppc64:
375  IsBaseFound = AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
376  "powerpc-apple-darwin10", "",
377  "ppc64", triple);
378  IsBaseFound |= AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
379  "powerpc-apple-darwin10", "",
380  "ppc64", triple);
381  break;
382 
383  case llvm::Triple::x86:
384  case llvm::Triple::x86_64:
385  IsBaseFound = AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
386  "i686-apple-darwin10", "",
387  "x86_64", triple);
388  IsBaseFound |= AddGnuCPlusPlusIncludePaths(
389  "/usr/include/c++/4.0.0", "i686-apple-darwin8", "", "", triple);
390  break;
391 
392  case llvm::Triple::arm:
393  case llvm::Triple::thumb:
394  IsBaseFound = AddGnuCPlusPlusIncludePaths(
395  "/usr/include/c++/4.2.1", "arm-apple-darwin10", "v7", "", triple);
396  IsBaseFound |= AddGnuCPlusPlusIncludePaths(
397  "/usr/include/c++/4.2.1", "arm-apple-darwin10", "v6", "", triple);
398  break;
399 
400  case llvm::Triple::aarch64:
401  IsBaseFound = AddGnuCPlusPlusIncludePaths(
402  "/usr/include/c++/4.2.1", "arm64-apple-darwin10", "", "", triple);
403  break;
404  }
405  // Warn when compiling pure C++ / Objective-C++ only.
406  if (!IsBaseFound &&
407  !(LangOpts.CUDA || LangOpts.OpenCL || LangOpts.RenderScript)) {
408  Headers.getDiags().Report(SourceLocation(),
409  diag::warn_stdlibcxx_not_found);
410  }
411  return;
412  }
413 
414  switch (os) {
415  case llvm::Triple::Linux:
416  case llvm::Triple::Hurd:
417  case llvm::Triple::Solaris:
418  llvm_unreachable("Include management is handled in the driver.");
419  break;
420  case llvm::Triple::Win32:
421  switch (triple.getEnvironment()) {
422  default: llvm_unreachable("Include management is handled in the driver.");
423  case llvm::Triple::Cygnus:
424  // Cygwin-1.7
425  AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3");
426  AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3");
427  AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
428  // g++-4 / Cygwin-1.5
429  AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
430  break;
431  }
432  break;
433  case llvm::Triple::DragonFly:
434  AddPath("/usr/include/c++/5.0", CXXSystem, false);
435  break;
436  case llvm::Triple::OpenBSD: {
437  std::string t = triple.getTriple();
438  if (t.substr(0, 6) == "x86_64")
439  t.replace(0, 6, "amd64");
440  AddGnuCPlusPlusIncludePaths("/usr/include/g++",
441  t, "", "", triple);
442  break;
443  }
444  case llvm::Triple::Minix:
445  AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
446  "", "", "", triple);
447  break;
448  default:
449  break;
450  }
451 }
452 
453 void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
454  const llvm::Triple &triple,
455  const HeaderSearchOptions &HSOpts) {
456  // NB: This code path is going away. All of the logic is moving into the
457  // driver which has the information necessary to do target-specific
458  // selections of default include paths. Each target which moves there will be
459  // exempted from this logic here until we can delete the entire pile of code.
460  switch (triple.getOS()) {
461  default:
462  break; // Everything else continues to use this routine's logic.
463 
464  case llvm::Triple::Linux:
465  case llvm::Triple::Hurd:
466  case llvm::Triple::Solaris:
467  return;
468 
469  case llvm::Triple::Win32:
470  if (triple.getEnvironment() != llvm::Triple::Cygnus ||
471  triple.isOSBinFormatMachO())
472  return;
473  break;
474  }
475 
476  if (Lang.CPlusPlus && !Lang.AsmPreprocessor &&
478  if (HSOpts.UseLibcxx) {
479  AddPath("/usr/include/c++/v1", CXXSystem, false);
480  } else {
481  AddDefaultCPlusPlusIncludePaths(Lang, 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.ObjC && !Lang.CPlusPlus && Include.first == CSystem) ||
607  (/*FIXME !Lang.ObjC && */ Lang.CPlusPlus &&
608  Include.first == CXXSystem) ||
609  (Lang.ObjC && !Lang.CPlusPlus && Include.first == ObjCSystem) ||
610  (Lang.ObjC && 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:122
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:1415
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:655
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:271
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.
Encodes a location in the source.
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:60
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:46
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.