clang  6.0.0svn
HeaderSearch.h
Go to the documentation of this file.
1 //===- HeaderSearch.h - Resolve Header File Locations -----------*- C++ -*-===//
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 defines the HeaderSearch interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LEX_HEADERSEARCH_H
15 #define LLVM_CLANG_LEX_HEADERSEARCH_H
16 
20 #include "clang/Lex/ModuleMap.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringSet.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Support/Allocator.h"
27 #include <cassert>
28 #include <cstddef>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace clang {
35 
36 class DiagnosticsEngine;
37 class DirectoryEntry;
38 class ExternalPreprocessorSource;
39 class FileEntry;
40 class FileManager;
41 class HeaderMap;
42 class HeaderSearchOptions;
43 class IdentifierInfo;
44 class LangOptions;
45 class Module;
46 class Preprocessor;
47 class TargetInfo;
48 
49 /// \brief The preprocessor keeps track of this information for each
50 /// file that is \#included.
52  /// \brief True if this is a \#import'd or \#pragma once file.
53  unsigned isImport : 1;
54 
55  /// \brief True if this is a \#pragma once file.
56  unsigned isPragmaOnce : 1;
57 
58  /// DirInfo - Keep track of whether this is a system header, and if so,
59  /// whether it is C++ clean or not. This can be set by the include paths or
60  /// by \#pragma gcc system_header. This is an instance of
61  /// SrcMgr::CharacteristicKind.
62  unsigned DirInfo : 3;
63 
64  /// \brief Whether this header file info was supplied by an external source,
65  /// and has not changed since.
66  unsigned External : 1;
67 
68  /// \brief Whether this header is part of a module.
69  unsigned isModuleHeader : 1;
70 
71  /// \brief Whether this header is part of the module that we are building.
73 
74  /// \brief Whether this structure is considered to already have been
75  /// "resolved", meaning that it was loaded from the external source.
76  unsigned Resolved : 1;
77 
78  /// \brief Whether this is a header inside a framework that is currently
79  /// being built.
80  ///
81  /// When a framework is being built, the headers have not yet been placed
82  /// into the appropriate framework subdirectories, and therefore are
83  /// provided via a header map. This bit indicates when this is one of
84  /// those framework headers.
85  unsigned IndexHeaderMapHeader : 1;
86 
87  /// \brief Whether this file has been looked up as a header.
88  unsigned IsValid : 1;
89 
90  /// \brief The number of times the file has been included already.
91  unsigned short NumIncludes = 0;
92 
93  /// \brief The ID number of the controlling macro.
94  ///
95  /// This ID number will be non-zero when there is a controlling
96  /// macro whose IdentifierInfo may not yet have been loaded from
97  /// external storage.
98  unsigned ControllingMacroID = 0;
99 
100  /// If this file has a \#ifndef XXX (or equivalent) guard that
101  /// protects the entire contents of the file, this is the identifier
102  /// for the macro that controls whether or not it has any effect.
103  ///
104  /// Note: Most clients should use getControllingMacro() to access
105  /// the controlling macro of this header, since
106  /// getControllingMacro() is able to load a controlling macro from
107  /// external storage.
109 
110  /// \brief If this header came from a framework include, this is the name
111  /// of the framework.
112  StringRef Framework;
113 
115  : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User),
118 
119  /// \brief Retrieve the controlling macro for this header file, if
120  /// any.
121  const IdentifierInfo *
123 
124  /// \brief Determine whether this is a non-default header file info, e.g.,
125  /// it corresponds to an actual header we've included or tried to include.
126  bool isNonDefault() const {
127  return isImport || isPragmaOnce || NumIncludes || ControllingMacro ||
129  }
130 };
131 
132 /// \brief An external source of header file information, which may supply
133 /// information about header files already included.
135 public:
136  virtual ~ExternalHeaderFileInfoSource();
137 
138  /// \brief Retrieve the header file information for the given file entry.
139  ///
140  /// \returns Header file information for the given file entry, with the
141  /// \c External bit set. If the file entry is not known, return a
142  /// default-constructed \c HeaderFileInfo.
143  virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0;
144 };
145 
146 /// \brief Encapsulates the information needed to find the file referenced
147 /// by a \#include or \#include_next, (sub-)framework lookup, etc.
149  friend class DirectoryLookup;
150 
151  /// This structure is used to record entries in our framework cache.
152  struct FrameworkCacheEntry {
153  /// The directory entry which should be used for the cached framework.
154  const DirectoryEntry *Directory;
155 
156  /// Whether this framework has been "user-specified" to be treated as if it
157  /// were a system framework (even if it was found outside a system framework
158  /// directory).
159  bool IsUserSpecifiedSystemFramework;
160  };
161 
162  /// \brief Header-search options used to initialize this header search.
163  std::shared_ptr<HeaderSearchOptions> HSOpts;
164 
165  DiagnosticsEngine &Diags;
166  FileManager &FileMgr;
167 
168  /// \#include search path information. Requests for \#include "x" search the
169  /// directory of the \#including file first, then each directory in SearchDirs
170  /// consecutively. Requests for <x> search the current dir first, then each
171  /// directory in SearchDirs, starting at AngledDirIdx, consecutively. If
172  /// NoCurDirSearch is true, then the check for the file in the current
173  /// directory is suppressed.
174  std::vector<DirectoryLookup> SearchDirs;
175  unsigned AngledDirIdx = 0;
176  unsigned SystemDirIdx = 0;
177  bool NoCurDirSearch = false;
178 
179  /// \brief \#include prefixes for which the 'system header' property is
180  /// overridden.
181  ///
182  /// For a \#include "x" or \#include <x> directive, the last string in this
183  /// list which is a prefix of 'x' determines whether the file is treated as
184  /// a system header.
185  std::vector<std::pair<std::string, bool>> SystemHeaderPrefixes;
186 
187  /// \brief The path to the module cache.
188  std::string ModuleCachePath;
189 
190  /// \brief All of the preprocessor-specific data about files that are
191  /// included, indexed by the FileEntry's UID.
192  mutable std::vector<HeaderFileInfo> FileInfo;
193 
194  /// Keeps track of each lookup performed by LookupFile.
195  struct LookupFileCacheInfo {
196  /// Starting index in SearchDirs that the cached search was performed from.
197  /// If there is a hit and this value doesn't match the current query, the
198  /// cache has to be ignored.
199  unsigned StartIdx = 0;
200 
201  /// The entry in SearchDirs that satisfied the query.
202  unsigned HitIdx = 0;
203 
204  /// This is non-null if the original filename was mapped to a framework
205  /// include via a headermap.
206  const char *MappedName = nullptr;
207 
208  /// Default constructor -- Initialize all members with zero.
209  LookupFileCacheInfo() = default;
210 
211  void reset(unsigned StartIdx) {
212  this->StartIdx = StartIdx;
213  this->MappedName = nullptr;
214  }
215  };
216  llvm::StringMap<LookupFileCacheInfo, llvm::BumpPtrAllocator> LookupFileCache;
217 
218  /// \brief Collection mapping a framework or subframework
219  /// name like "Carbon" to the Carbon.framework directory.
220  llvm::StringMap<FrameworkCacheEntry, llvm::BumpPtrAllocator> FrameworkMap;
221 
222  /// IncludeAliases - maps include file names (including the quotes or
223  /// angle brackets) to other include file names. This is used to support the
224  /// include_alias pragma for Microsoft compatibility.
225  using IncludeAliasMap =
226  llvm::StringMap<std::string, llvm::BumpPtrAllocator>;
227  std::unique_ptr<IncludeAliasMap> IncludeAliases;
228 
229  /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing
230  /// headermaps. This vector owns the headermap.
231  std::vector<std::pair<const FileEntry *, const HeaderMap *>> HeaderMaps;
232 
233  /// \brief The mapping between modules and headers.
234  mutable ModuleMap ModMap;
235 
236  /// \brief Describes whether a given directory has a module map in it.
237  llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap;
238 
239  /// \brief Set of module map files we've already loaded, and a flag indicating
240  /// whether they were valid or not.
241  llvm::DenseMap<const FileEntry *, bool> LoadedModuleMaps;
242 
243  /// \brief Uniqued set of framework names, which is used to track which
244  /// headers were included as framework headers.
245  llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames;
246 
247  /// \brief Entity used to resolve the identifier IDs of controlling
248  /// macros into IdentifierInfo pointers, and keep the identifire up to date,
249  /// as needed.
250  ExternalPreprocessorSource *ExternalLookup = nullptr;
251 
252  /// \brief Entity used to look up stored header file information.
253  ExternalHeaderFileInfoSource *ExternalSource = nullptr;
254 
255  // Various statistics we track for performance analysis.
256  unsigned NumIncluded = 0;
257  unsigned NumMultiIncludeFileOptzn = 0;
258  unsigned NumFrameworkLookups = 0;
259  unsigned NumSubFrameworkLookups = 0;
260 
261 public:
262  HeaderSearch(std::shared_ptr<HeaderSearchOptions> HSOpts,
263  SourceManager &SourceMgr, DiagnosticsEngine &Diags,
264  const LangOptions &LangOpts, const TargetInfo *Target);
265  HeaderSearch(const HeaderSearch &) = delete;
266  HeaderSearch &operator=(const HeaderSearch &) = delete;
267  ~HeaderSearch();
268 
269  /// \brief Retrieve the header-search options with which this header search
270  /// was initialized.
271  HeaderSearchOptions &getHeaderSearchOpts() const { return *HSOpts; }
272 
273  FileManager &getFileMgr() const { return FileMgr; }
274 
275  /// \brief Interface for setting the file search paths.
276  void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
277  unsigned angledDirIdx, unsigned systemDirIdx,
278  bool noCurDirSearch) {
279  assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
280  "Directory indicies are unordered");
281  SearchDirs = dirs;
282  AngledDirIdx = angledDirIdx;
283  SystemDirIdx = systemDirIdx;
284  NoCurDirSearch = noCurDirSearch;
285  //LookupFileCache.clear();
286  }
287 
288  /// \brief Add an additional search path.
289  void AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
290  unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
291  SearchDirs.insert(SearchDirs.begin() + idx, dir);
292  if (!isAngled)
293  AngledDirIdx++;
294  SystemDirIdx++;
295  }
296 
297  /// \brief Set the list of system header prefixes.
298  void SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string, bool>> P) {
299  SystemHeaderPrefixes.assign(P.begin(), P.end());
300  }
301 
302  /// \brief Checks whether the map exists or not.
303  bool HasIncludeAliasMap() const { return (bool)IncludeAliases; }
304 
305  /// \brief Map the source include name to the dest include name.
306  ///
307  /// The Source should include the angle brackets or quotes, the dest
308  /// should not. This allows for distinction between <> and "" headers.
309  void AddIncludeAlias(StringRef Source, StringRef Dest) {
310  if (!IncludeAliases)
311  IncludeAliases.reset(new IncludeAliasMap);
312  (*IncludeAliases)[Source] = Dest;
313  }
314 
315  /// MapHeaderToIncludeAlias - Maps one header file name to a different header
316  /// file name, for use with the include_alias pragma. Note that the source
317  /// file name should include the angle brackets or quotes. Returns StringRef
318  /// as null if the header cannot be mapped.
319  StringRef MapHeaderToIncludeAlias(StringRef Source) {
320  assert(IncludeAliases && "Trying to map headers when there's no map");
321 
322  // Do any filename replacements before anything else
323  IncludeAliasMap::const_iterator Iter = IncludeAliases->find(Source);
324  if (Iter != IncludeAliases->end())
325  return Iter->second;
326  return {};
327  }
328 
329  /// \brief Set the path to the module cache.
330  void setModuleCachePath(StringRef CachePath) {
331  ModuleCachePath = CachePath;
332  }
333 
334  /// \brief Retrieve the path to the module cache.
335  StringRef getModuleCachePath() const { return ModuleCachePath; }
336 
337  /// \brief Consider modules when including files from this directory.
339  DirectoryHasModuleMap[Dir] = true;
340  }
341 
342  /// \brief Forget everything we know about headers so far.
343  void ClearFileInfo() {
344  FileInfo.clear();
345  }
346 
348  ExternalLookup = EPS;
349  }
350 
352  return ExternalLookup;
353  }
354 
355  /// \brief Set the external source of header information.
357  ExternalSource = ES;
358  }
359 
360  /// \brief Set the target information for the header search, if not
361  /// already known.
362  void setTarget(const TargetInfo &Target);
363 
364  /// \brief Given a "foo" or <foo> reference, look up the indicated file,
365  /// return null on failure.
366  ///
367  /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member
368  /// the file was found in, or null if not applicable.
369  ///
370  /// \param IncludeLoc Used for diagnostics if valid.
371  ///
372  /// \param isAngled indicates whether the file reference is a <> reference.
373  ///
374  /// \param CurDir If non-null, the file was found in the specified directory
375  /// search location. This is used to implement \#include_next.
376  ///
377  /// \param Includers Indicates where the \#including file(s) are, in case
378  /// relative searches are needed. In reverse order of inclusion.
379  ///
380  /// \param SearchPath If non-null, will be set to the search path relative
381  /// to which the file was found. If the include path is absolute, SearchPath
382  /// will be set to an empty string.
383  ///
384  /// \param RelativePath If non-null, will be set to the path relative to
385  /// SearchPath at which the file was found. This only differs from the
386  /// Filename for framework includes.
387  ///
388  /// \param SuggestedModule If non-null, and the file found is semantically
389  /// part of a known module, this will be set to the module that should
390  /// be imported instead of preprocessing/parsing the file found.
391  ///
392  /// \param IsMapped If non-null, and the search involved header maps, set to
393  /// true.
394  const FileEntry *LookupFile(
395  StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
396  const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
397  ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
398  SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
399  Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
400  bool *IsMapped, bool SkipCache = false, bool BuildSystemModule = false);
401 
402  /// \brief Look up a subframework for the specified \#include file.
403  ///
404  /// For example, if \#include'ing <HIToolbox/HIToolbox.h> from
405  /// within ".../Carbon.framework/Headers/Carbon.h", check to see if
406  /// HIToolbox is a subframework within Carbon.framework. If so, return
407  /// the FileEntry for the designated file, otherwise return null.
408  const FileEntry *LookupSubframeworkHeader(
409  StringRef Filename, const FileEntry *RelativeFileEnt,
410  SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
411  Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule);
412 
413  /// \brief Look up the specified framework name in our framework cache.
414  /// \returns The DirectoryEntry it is in if we know, null otherwise.
415  FrameworkCacheEntry &LookupFrameworkCache(StringRef FWName) {
416  return FrameworkMap[FWName];
417  }
418 
419  /// \brief Mark the specified file as a target of of a \#include,
420  /// \#include_next, or \#import directive.
421  ///
422  /// \return false if \#including the file will have no effect or true
423  /// if we should include it.
424  bool ShouldEnterIncludeFile(Preprocessor &PP, const FileEntry *File,
425  bool isImport, bool ModulesEnabled,
426  Module *CorrespondingModule);
427 
428  /// \brief Return whether the specified file is a normal header,
429  /// a system header, or a C++ friendly system header.
431  return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
432  }
433 
434  /// \brief Mark the specified file as a "once only" file, e.g. due to
435  /// \#pragma once.
436  void MarkFileIncludeOnce(const FileEntry *File) {
437  HeaderFileInfo &FI = getFileInfo(File);
438  FI.isImport = true;
439  FI.isPragmaOnce = true;
440  }
441 
442  /// \brief Mark the specified file as a system header, e.g. due to
443  /// \#pragma GCC system_header.
444  void MarkFileSystemHeader(const FileEntry *File) {
445  getFileInfo(File).DirInfo = SrcMgr::C_System;
446  }
447 
448  /// \brief Mark the specified file as part of a module.
449  void MarkFileModuleHeader(const FileEntry *File,
451  bool IsCompiledModuleHeader);
452 
453  /// \brief Increment the count for the number of times the specified
454  /// FileEntry has been entered.
455  void IncrementIncludeCount(const FileEntry *File) {
456  ++getFileInfo(File).NumIncludes;
457  }
458 
459  /// \brief Mark the specified file as having a controlling macro.
460  ///
461  /// This is used by the multiple-include optimization to eliminate
462  /// no-op \#includes.
465  getFileInfo(File).ControllingMacro = ControllingMacro;
466  }
467 
468  /// \brief Return true if this is the first time encountering this header.
469  bool FirstTimeLexingFile(const FileEntry *File) {
470  return getFileInfo(File).NumIncludes == 1;
471  }
472 
473  /// \brief Determine whether this file is intended to be safe from
474  /// multiple inclusions, e.g., it has \#pragma once or a controlling
475  /// macro.
476  ///
477  /// This routine does not consider the effect of \#import
478  bool isFileMultipleIncludeGuarded(const FileEntry *File);
479 
480  /// CreateHeaderMap - This method returns a HeaderMap for the specified
481  /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
482  const HeaderMap *CreateHeaderMap(const FileEntry *FE);
483 
484  /// \brief Get filenames for all registered header maps.
485  void getHeaderMapFileNames(SmallVectorImpl<std::string> &Names) const;
486 
487  /// \brief Retrieve the name of the cached module file that should be used
488  /// to load the given module.
489  ///
490  /// \param Module The module whose module file name will be returned.
491  ///
492  /// \returns The name of the module file that corresponds to this module,
493  /// or an empty string if this module does not correspond to any module file.
494  std::string getCachedModuleFileName(Module *Module);
495 
496  /// \brief Retrieve the name of the prebuilt module file that should be used
497  /// to load a module with the given name.
498  ///
499  /// \param ModuleName The module whose module file name will be returned.
500  ///
501  /// \param FileMapOnly If true, then only look in the explicit module name
502  // to file name map and skip the directory search.
503  ///
504  /// \returns The name of the module file that corresponds to this module,
505  /// or an empty string if this module does not correspond to any module file.
506  std::string getPrebuiltModuleFileName(StringRef ModuleName,
507  bool FileMapOnly = false);
508 
509  /// \brief Retrieve the name of the (to-be-)cached module file that should
510  /// be used to load a module with the given name.
511  ///
512  /// \param ModuleName The module whose module file name will be returned.
513  ///
514  /// \param ModuleMapPath A path that when combined with \c ModuleName
515  /// uniquely identifies this module. See Module::ModuleMap.
516  ///
517  /// \returns The name of the module file that corresponds to this module,
518  /// or an empty string if this module does not correspond to any module file.
519  std::string getCachedModuleFileName(StringRef ModuleName,
520  StringRef ModuleMapPath);
521 
522  /// \brief Lookup a module Search for a module with the given name.
523  ///
524  /// \param ModuleName The name of the module we're looking for.
525  ///
526  /// \param AllowSearch Whether we are allowed to search in the various
527  /// search directories to produce a module definition. If not, this lookup
528  /// will only return an already-known module.
529  ///
530  /// \returns The module with the given name.
531  Module *lookupModule(StringRef ModuleName, bool AllowSearch = true);
532 
533  /// \brief Try to find a module map file in the given directory, returning
534  /// \c nullptr if none is found.
535  const FileEntry *lookupModuleMapFile(const DirectoryEntry *Dir,
536  bool IsFramework);
537 
538  void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
539 
540  /// \brief Determine whether there is a module map that may map the header
541  /// with the given file name to a (sub)module.
542  /// Always returns false if modules are disabled.
543  ///
544  /// \param Filename The name of the file.
545  ///
546  /// \param Root The "root" directory, at which we should stop looking for
547  /// module maps.
548  ///
549  /// \param IsSystem Whether the directories we're looking at are system
550  /// header directories.
551  bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root,
552  bool IsSystem);
553 
554  /// \brief Retrieve the module that corresponds to the given file, if any.
555  ///
556  /// \param File The header that we wish to map to a module.
557  /// \param AllowTextual Whether we want to find textual headers too.
558  ModuleMap::KnownHeader findModuleForHeader(const FileEntry *File,
559  bool AllowTextual = false) const;
560 
561  /// \brief Read the contents of the given module map file.
562  ///
563  /// \param File The module map file.
564  /// \param IsSystem Whether this file is in a system header directory.
565  /// \param ID If the module map file is already mapped (perhaps as part of
566  /// processing a preprocessed module), the ID of the file.
567  /// \param Offset [inout] An offset within ID to start parsing. On exit,
568  /// filled by the end of the parsed contents (either EOF or the
569  /// location of an end-of-module-map pragma).
570  /// \param OriginalModuleMapFile The original path to the module map file,
571  /// used to resolve paths within the module (this is required when
572  /// building the module from preprocessed source).
573  /// \returns true if an error occurred, false otherwise.
574  bool loadModuleMapFile(const FileEntry *File, bool IsSystem,
575  FileID ID = FileID(), unsigned *Offset = nullptr,
576  StringRef OriginalModuleMapFile = StringRef());
577 
578  /// \brief Collect the set of all known, top-level modules.
579  ///
580  /// \param Modules Will be filled with the set of known, top-level modules.
581  void collectAllModules(SmallVectorImpl<Module *> &Modules);
582 
583  /// \brief Load all known, top-level system modules.
584  void loadTopLevelSystemModules();
585 
586 private:
587  /// \brief Lookup a module with the given module name and search-name.
588  ///
589  /// \param ModuleName The name of the module we're looking for.
590  ///
591  /// \param SearchName The "search-name" to derive filesystem paths from
592  /// when looking for the module map; this is usually equal to ModuleName,
593  /// but for compatibility with some buggy frameworks, additional attempts
594  /// may be made to find the module under a related-but-different search-name.
595  ///
596  /// \returns The module named ModuleName.
597  Module *lookupModule(StringRef ModuleName, StringRef SearchName);
598 
599  /// \brief Retrieve a module with the given name, which may be part of the
600  /// given framework.
601  ///
602  /// \param Name The name of the module to retrieve.
603  ///
604  /// \param Dir The framework directory (e.g., ModuleName.framework).
605  ///
606  /// \param IsSystem Whether the framework directory is part of the system
607  /// frameworks.
608  ///
609  /// \returns The module, if found; otherwise, null.
610  Module *loadFrameworkModule(StringRef Name,
611  const DirectoryEntry *Dir,
612  bool IsSystem);
613 
614  /// \brief Load all of the module maps within the immediate subdirectories
615  /// of the given search directory.
616  void loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir);
617 
618  /// \brief Find and suggest a usable module for the given file.
619  ///
620  /// \return \c true if the file can be used, \c false if we are not permitted to
621  /// find this file due to requirements from \p RequestingModule.
622  bool findUsableModuleForHeader(const FileEntry *File,
623  const DirectoryEntry *Root,
624  Module *RequestingModule,
625  ModuleMap::KnownHeader *SuggestedModule,
626  bool IsSystemHeaderDir);
627 
628  /// \brief Find and suggest a usable module for the given file, which is part of
629  /// the specified framework.
630  ///
631  /// \return \c true if the file can be used, \c false if we are not permitted to
632  /// find this file due to requirements from \p RequestingModule.
633  bool findUsableModuleForFrameworkHeader(
634  const FileEntry *File, StringRef FrameworkDir, Module *RequestingModule,
635  ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework);
636 
637  /// \brief Look up the file with the specified name and determine its owning
638  /// module.
639  const FileEntry *
640  getFileAndSuggestModule(StringRef FileName, SourceLocation IncludeLoc,
641  const DirectoryEntry *Dir, bool IsSystemHeaderDir,
642  Module *RequestingModule,
643  ModuleMap::KnownHeader *SuggestedModule);
644 
645 public:
646  /// \brief Retrieve the module map.
647  ModuleMap &getModuleMap() { return ModMap; }
648 
649  /// \brief Retrieve the module map.
650  const ModuleMap &getModuleMap() const { return ModMap; }
651 
652  unsigned header_file_size() const { return FileInfo.size(); }
653 
654  /// \brief Return the HeaderFileInfo structure for the specified FileEntry,
655  /// in preparation for updating it in some way.
656  HeaderFileInfo &getFileInfo(const FileEntry *FE);
657 
658  /// \brief Return the HeaderFileInfo structure for the specified FileEntry,
659  /// if it has ever been filled in.
660  /// \param WantExternal Whether the caller wants purely-external header file
661  /// info (where \p External is true).
662  const HeaderFileInfo *getExistingFileInfo(const FileEntry *FE,
663  bool WantExternal = true) const;
664 
665  // Used by external tools
666  using search_dir_iterator = std::vector<DirectoryLookup>::const_iterator;
667 
668  search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); }
669  search_dir_iterator search_dir_end() const { return SearchDirs.end(); }
670  unsigned search_dir_size() const { return SearchDirs.size(); }
671 
673  return SearchDirs.begin();
674  }
675 
677  return SearchDirs.begin() + AngledDirIdx;
678  }
679 
681  return SearchDirs.begin() + AngledDirIdx;
682  }
683 
685  return SearchDirs.begin() + SystemDirIdx;
686  }
687 
689  return SearchDirs.begin() + SystemDirIdx;
690  }
691 
692  search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
693 
694  /// \brief Retrieve a uniqued framework name.
695  StringRef getUniqueFrameworkName(StringRef Framework);
696 
697  /// \brief Suggest a path by which the specified file could be found, for
698  /// use in diagnostics to suggest a #include.
699  ///
700  /// \param IsSystem If non-null, filled in to indicate whether the suggested
701  /// path is relative to a system header directory.
702  std::string suggestPathToFileForDiagnostics(const FileEntry *File,
703  bool *IsSystem = nullptr);
704 
705  void PrintStats();
706 
707  size_t getTotalMemory() const;
708 
709 private:
710  /// \brief Describes what happened when we tried to load a module map file.
711  enum LoadModuleMapResult {
712  /// \brief The module map file had already been loaded.
713  LMM_AlreadyLoaded,
714 
715  /// \brief The module map file was loaded by this invocation.
716  LMM_NewlyLoaded,
717 
718  /// \brief There is was directory with the given name.
719  LMM_NoDirectory,
720 
721  /// \brief There was either no module map file or the module map file was
722  /// invalid.
723  LMM_InvalidModuleMap
724  };
725 
726  LoadModuleMapResult loadModuleMapFileImpl(const FileEntry *File,
727  bool IsSystem,
728  const DirectoryEntry *Dir,
729  FileID ID = FileID(),
730  unsigned *Offset = nullptr);
731 
732  /// \brief Try to load the module map file in the given directory.
733  ///
734  /// \param DirName The name of the directory where we will look for a module
735  /// map file.
736  /// \param IsSystem Whether this is a system header directory.
737  /// \param IsFramework Whether this is a framework directory.
738  ///
739  /// \returns The result of attempting to load the module map file from the
740  /// named directory.
741  LoadModuleMapResult loadModuleMapFile(StringRef DirName, bool IsSystem,
742  bool IsFramework);
743 
744  /// \brief Try to load the module map file in the given directory.
745  ///
746  /// \param Dir The directory where we will look for a module map file.
747  /// \param IsSystem Whether this is a system header directory.
748  /// \param IsFramework Whether this is a framework directory.
749  ///
750  /// \returns The result of attempting to load the module map file from the
751  /// named directory.
752  LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir,
753  bool IsSystem, bool IsFramework);
754 };
755 
756 } // namespace clang
757 
758 #endif // LLVM_CLANG_LEX_HEADERSEARCH_H
unsigned DirInfo
DirInfo - Keep track of whether this is a system header, and if so, whether it is C++ clean or not...
Definition: HeaderSearch.h:62
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:116
unsigned isPragmaOnce
True if this is a #pragma once file.
Definition: HeaderSearch.h:56
Defines the SourceManager interface.
void SetSearchPaths(const std::vector< DirectoryLookup > &dirs, unsigned angledDirIdx, unsigned systemDirIdx, bool noCurDirSearch)
Interface for setting the file search paths.
Definition: HeaderSearch.h:276
StringRef P
search_dir_iterator angled_dir_end() const
Definition: HeaderSearch.h:684
FrameworkCacheEntry & LookupFrameworkCache(StringRef FWName)
Look up the specified framework name in our framework cache.
Definition: HeaderSearch.h:415
bool HasIncludeAliasMap() const
Checks whether the map exists or not.
Definition: HeaderSearch.h:303
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
unsigned isCompilingModuleHeader
Whether this header is part of the module that we are building.
Definition: HeaderSearch.h:72
unsigned isImport
True if this is a #import&#39;d or #pragma once file.
Definition: HeaderSearch.h:53
void ClearFileInfo()
Forget everything we know about headers so far.
Definition: HeaderSearch.h:343
One of these records is kept for each identifier that is lexed.
This class represents an Apple concept known as a &#39;header map&#39;.
Definition: HeaderMap.h:67
FileManager & getFileMgr() const
Definition: HeaderSearch.h:273
const IdentifierInfo * getControllingMacro(ExternalPreprocessorSource *External)
Retrieve the controlling macro for this header file, if any.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Describes a module or submodule.
Definition: Module.h:65
unsigned External
Whether this header file info was supplied by an external source, and has not changed since...
Definition: HeaderSearch.h:66
uint32_t Offset
Definition: CacheTokens.cpp:43
void AddIncludeAlias(StringRef Source, StringRef Dest)
Map the source include name to the dest include name.
Definition: HeaderSearch.h:309
search_dir_iterator search_dir_begin() const
Definition: HeaderSearch.h:668
search_dir_iterator quoted_dir_end() const
Definition: HeaderSearch.h:676
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:147
unsigned short NumIncludes
The number of times the file has been included already.
Definition: HeaderSearch.h:91
search_dir_iterator search_dir_end() const
Definition: HeaderSearch.h:669
unsigned Resolved
Whether this structure is considered to already have been "resolved", meaning that it was loaded from...
Definition: HeaderSearch.h:76
The preprocessor keeps track of this information for each file that is #included. ...
Definition: HeaderSearch.h:51
unsigned header_file_size() const
Definition: HeaderSearch.h:652
Encapsulates the information needed to find the file referenced by a #include or #include_next, (sub-)framework lookup, etc.
Definition: HeaderSearch.h:148
StringRef getModuleCachePath() const
Retrieve the path to the module cache.
Definition: HeaderSearch.h:335
const ModuleMap & getModuleMap() const
Retrieve the module map.
Definition: HeaderSearch.h:650
ModuleHeaderRole
Flags describing the role of a module header.
Definition: ModuleMap.h:106
StringRef Filename
Definition: Format.cpp:1345
Exposes information about the current target.
Definition: TargetInfo.h:54
Abstract interface for external sources of preprocessor information.
HeaderSearchOptions & getHeaderSearchOpts() const
Retrieve the header-search options with which this header search was initialized. ...
Definition: HeaderSearch.h:271
search_dir_iterator quoted_dir_begin() const
Definition: HeaderSearch.h:672
search_dir_iterator system_dir_end() const
Definition: HeaderSearch.h:692
void IncrementIncludeCount(const FileEntry *File)
Increment the count for the number of times the specified FileEntry has been entered.
Definition: HeaderSearch.h:455
DirectoryLookup - This class represents one entry in the search list that specifies the search order ...
SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File)
Return whether the specified file is a normal header, a system header, or a C++ friendly system heade...
Definition: HeaderSearch.h:430
unsigned isModuleHeader
Whether this header is part of a module.
Definition: HeaderSearch.h:69
#define false
Definition: stdbool.h:33
Encodes a location in the source.
search_dir_iterator system_dir_begin() const
Definition: HeaderSearch.h:688
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
const IdentifierInfo * ControllingMacro
If this file has a #ifndef XXX (or equivalent) guard that protects the entire contents of the file...
Definition: HeaderSearch.h:108
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
unsigned ControllingMacroID
The ID number of the controlling macro.
Definition: HeaderSearch.h:98
Dataflow Directional Tag Classes.
An external source of header file information, which may supply information about header files alread...
Definition: HeaderSearch.h:134
bool isNonDefault() const
Determine whether this is a non-default header file info, e.g., it corresponds to an actual header we...
Definition: HeaderSearch.h:126
void SetExternalSource(ExternalHeaderFileInfoSource *ES)
Set the external source of header information.
Definition: HeaderSearch.h:356
void IncrementFrameworkLookupCount()
Definition: HeaderSearch.h:538
std::vector< DirectoryLookup >::const_iterator search_dir_iterator
Definition: HeaderSearch.h:666
void AddSearchPath(const DirectoryLookup &dir, bool isAngled)
Add an additional search path.
Definition: HeaderSearch.h:289
search_dir_iterator angled_dir_begin() const
Definition: HeaderSearch.h:680
Cached information about one directory (either on disk or in the virtual file system).
Definition: FileManager.h:45
StringRef Framework
If this header came from a framework include, this is the name of the framework.
Definition: HeaderSearch.h:112
Defines the clang::SourceLocation class and associated facilities.
void setDirectoryHasModuleMap(const DirectoryEntry *Dir)
Consider modules when including files from this directory.
Definition: HeaderSearch.h:338
void SetSystemHeaderPrefixes(ArrayRef< std::pair< std::string, bool >> P)
Set the list of system header prefixes.
Definition: HeaderSearch.h:298
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
StringRef MapHeaderToIncludeAlias(StringRef Source)
MapHeaderToIncludeAlias - Maps one header file name to a different header file name, for use with the include_alias pragma.
Definition: HeaderSearch.h:319
bool FirstTimeLexingFile(const FileEntry *File)
Return true if this is the first time encountering this header.
Definition: HeaderSearch.h:469
void SetExternalLookup(ExternalPreprocessorSource *EPS)
Definition: HeaderSearch.h:347
unsigned IndexHeaderMapHeader
Whether this is a header inside a framework that is currently being built.
Definition: HeaderSearch.h:85
unsigned IsValid
Whether this file has been looked up as a header.
Definition: HeaderSearch.h:88
void MarkFileSystemHeader(const FileEntry *File)
Mark the specified file as a system header, e.g.
Definition: HeaderSearch.h:444
unsigned search_dir_size() const
Definition: HeaderSearch.h:670
A header that is known to reside within a given module, whether it was included or excluded...
Definition: ModuleMap.h:133
void MarkFileIncludeOnce(const FileEntry *File)
Mark the specified file as a "once only" file, e.g.
Definition: HeaderSearch.h:436
void setModuleCachePath(StringRef CachePath)
Set the path to the module cache.
Definition: HeaderSearch.h:330
This class handles loading and caching of source files into memory.
ExternalPreprocessorSource * getExternalLookup() const
Definition: HeaderSearch.h:351
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:127
void SetFileControllingMacro(const FileEntry *File, const IdentifierInfo *ControllingMacro)
Mark the specified file as having a controlling macro.
Definition: HeaderSearch.h:463