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