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 
17 #include "clang/Lex/DirectoryLookup.h"
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 ExternalIdentifierLookup;
31 class FileEntry;
32 class FileManager;
33 class HeaderSearchOptions;
34 class IdentifierInfo;
35 
36 /// \brief The preprocessor keeps track of this information for each
37 /// file that is \#included.
38 struct HeaderFileInfo {
39   /// \brief True if this is a \#import'd or \#pragma once file.
40   unsigned isImport : 1;
41 
42   /// \brief True if this is a \#pragma once file.
43   unsigned isPragmaOnce : 1;
44 
45   /// DirInfo - Keep track of whether this is a system header, and if so,
46   /// whether it is C++ clean or not.  This can be set by the include paths or
47   /// by \#pragma gcc system_header.  This is an instance of
48   /// SrcMgr::CharacteristicKind.
49   unsigned DirInfo : 2;
50 
51   /// \brief Whether this header file info was supplied by an external source.
52   unsigned External : 1;
53 
54   /// \brief Whether this header is part of a module.
55   unsigned isModuleHeader : 1;
56 
57   /// \brief Whether this header is part of the module that we are building.
58   unsigned isCompilingModuleHeader : 1;
59 
60   /// \brief Whether this header is part of the module that we are building.
61   /// This is an instance of ModuleMap::ModuleHeaderRole.
62   unsigned HeaderRole : 2;
63 
64   /// \brief Whether this structure is considered to already have been
65   /// "resolved", meaning that it was loaded from the external source.
66   unsigned Resolved : 1;
67 
68   /// \brief Whether this is a header inside a framework that is currently
69   /// being built.
70   ///
71   /// When a framework is being built, the headers have not yet been placed
72   /// into the appropriate framework subdirectories, and therefore are
73   /// provided via a header map. This bit indicates when this is one of
74   /// those framework headers.
75   unsigned IndexHeaderMapHeader : 1;
76 
77   /// \brief Whether this file had been looked up as a header.
78   unsigned IsValid : 1;
79 
80   /// \brief The number of times the file has been included already.
81   unsigned short NumIncludes;
82 
83   /// \brief The ID number of the controlling macro.
84   ///
85   /// This ID number will be non-zero when there is a controlling
86   /// macro whose IdentifierInfo may not yet have been loaded from
87   /// external storage.
88   unsigned ControllingMacroID;
89 
90   /// If this file has a \#ifndef XXX (or equivalent) guard that
91   /// protects the entire contents of the file, this is the identifier
92   /// for the macro that controls whether or not it has any effect.
93   ///
94   /// Note: Most clients should use getControllingMacro() to access
95   /// the controlling macro of this header, since
96   /// getControllingMacro() is able to load a controlling macro from
97   /// external storage.
98   const IdentifierInfo *ControllingMacro;
99 
100   /// \brief If this header came from a framework include, this is the name
101   /// of the framework.
102   StringRef Framework;
103 
HeaderFileInfoHeaderFileInfo104   HeaderFileInfo()
105     : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User),
106       External(false), isModuleHeader(false), isCompilingModuleHeader(false),
107       HeaderRole(ModuleMap::NormalHeader),
108       Resolved(false), IndexHeaderMapHeader(false), IsValid(0),
109       NumIncludes(0), ControllingMacroID(0), ControllingMacro(nullptr)  {}
110 
111   /// \brief Retrieve the controlling macro for this header file, if
112   /// any.
113   const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External);
114 
115   /// \brief Determine whether this is a non-default header file info, e.g.,
116   /// it corresponds to an actual header we've included or tried to include.
isNonDefaultHeaderFileInfo117   bool isNonDefault() const {
118     return isImport || isPragmaOnce || NumIncludes || ControllingMacro ||
119       ControllingMacroID;
120   }
121 
122   /// \brief Get the HeaderRole properly typed.
getHeaderRoleHeaderFileInfo123   ModuleMap::ModuleHeaderRole getHeaderRole() const {
124     return static_cast<ModuleMap::ModuleHeaderRole>(HeaderRole);
125   }
126 
127   /// \brief Set the HeaderRole properly typed.
setHeaderRoleHeaderFileInfo128   void setHeaderRole(ModuleMap::ModuleHeaderRole Role) {
129     HeaderRole = Role;
130   }
131 };
132 
133 /// \brief An external source of header file information, which may supply
134 /// information about header files already included.
135 class ExternalHeaderFileInfoSource {
136 public:
137   virtual ~ExternalHeaderFileInfoSource();
138 
139   /// \brief Retrieve the header file information for the given file entry.
140   ///
141   /// \returns Header file information for the given file entry, with the
142   /// \c External bit set. If the file entry is not known, return a
143   /// default-constructed \c HeaderFileInfo.
144   virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0;
145 };
146 
147 /// \brief Encapsulates the information needed to find the file referenced
148 /// by a \#include or \#include_next, (sub-)framework lookup, etc.
149 class HeaderSearch {
150   /// This structure is used to record entries in our framework cache.
151   struct FrameworkCacheEntry {
152     /// The directory entry which should be used for the cached framework.
153     const DirectoryEntry *Directory;
154 
155     /// Whether this framework has been "user-specified" to be treated as if it
156     /// were a system framework (even if it was found outside a system framework
157     /// directory).
158     bool IsUserSpecifiedSystemFramework;
159   };
160 
161   /// \brief Header-search options used to initialize this header search.
162   IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts;
163 
164   DiagnosticsEngine &Diags;
165   FileManager &FileMgr;
166   /// \#include search path information.  Requests for \#include "x" search the
167   /// directory of the \#including file first, then each directory in SearchDirs
168   /// consecutively. Requests for <x> search the current dir first, then each
169   /// directory in SearchDirs, starting at AngledDirIdx, consecutively.  If
170   /// NoCurDirSearch is true, then the check for the file in the current
171   /// directory is suppressed.
172   std::vector<DirectoryLookup> SearchDirs;
173   unsigned AngledDirIdx;
174   unsigned SystemDirIdx;
175   bool NoCurDirSearch;
176 
177   /// \brief \#include prefixes for which the 'system header' property is
178   /// overridden.
179   ///
180   /// For a \#include "x" or \#include \<x> directive, the last string in this
181   /// list which is a prefix of 'x' determines whether the file is treated as
182   /// a system header.
183   std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
184 
185   /// \brief The path to the module cache.
186   std::string ModuleCachePath;
187 
188   /// \brief All of the preprocessor-specific data about files that are
189   /// included, indexed by the FileEntry's UID.
190   std::vector<HeaderFileInfo> FileInfo;
191 
192   /// Keeps track of each lookup performed by LookupFile.
193   struct LookupFileCacheInfo {
194     /// Starting index in SearchDirs that the cached search was performed from.
195     /// If there is a hit and this value doesn't match the current query, the
196     /// cache has to be ignored.
197     unsigned StartIdx;
198     /// The entry in SearchDirs that satisfied the query.
199     unsigned HitIdx;
200     /// This is non-null if the original filename was mapped to a framework
201     /// include via a headermap.
202     const char *MappedName;
203 
204     /// Default constructor -- Initialize all members with zero.
LookupFileCacheInfoLookupFileCacheInfo205     LookupFileCacheInfo(): StartIdx(0), HitIdx(0), MappedName(nullptr) {}
206 
resetLookupFileCacheInfo207     void reset(unsigned StartIdx) {
208       this->StartIdx = StartIdx;
209       this->MappedName = nullptr;
210     }
211   };
212   llvm::StringMap<LookupFileCacheInfo, llvm::BumpPtrAllocator> LookupFileCache;
213 
214   /// \brief Collection mapping a framework or subframework
215   /// name like "Carbon" to the Carbon.framework directory.
216   llvm::StringMap<FrameworkCacheEntry, llvm::BumpPtrAllocator> FrameworkMap;
217 
218   /// IncludeAliases - maps include file names (including the quotes or
219   /// angle brackets) to other include file names.  This is used to support the
220   /// include_alias pragma for Microsoft compatibility.
221   typedef llvm::StringMap<std::string, llvm::BumpPtrAllocator>
222     IncludeAliasMap;
223   std::unique_ptr<IncludeAliasMap> IncludeAliases;
224 
225   /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing
226   /// headermaps.  This vector owns the headermap.
227   std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps;
228 
229   /// \brief The mapping between modules and headers.
230   mutable ModuleMap ModMap;
231 
232   /// \brief Describes whether a given directory has a module map in it.
233   llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap;
234 
235   /// \brief Set of module map files we've already loaded, and a flag indicating
236   /// whether they were valid or not.
237   llvm::DenseMap<const FileEntry *, bool> LoadedModuleMaps;
238 
239   /// \brief Uniqued set of framework names, which is used to track which
240   /// headers were included as framework headers.
241   llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames;
242 
243   /// \brief Entity used to resolve the identifier IDs of controlling
244   /// macros into IdentifierInfo pointers, as needed.
245   ExternalIdentifierLookup *ExternalLookup;
246 
247   /// \brief Entity used to look up stored header file information.
248   ExternalHeaderFileInfoSource *ExternalSource;
249 
250   // Various statistics we track for performance analysis.
251   unsigned NumIncluded;
252   unsigned NumMultiIncludeFileOptzn;
253   unsigned NumFrameworkLookups, NumSubFrameworkLookups;
254 
255   const LangOptions &LangOpts;
256 
257   // HeaderSearch doesn't support default or copy construction.
258   HeaderSearch(const HeaderSearch&) = delete;
259   void operator=(const HeaderSearch&) = delete;
260 
261   friend class DirectoryLookup;
262 
263 public:
264   HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
265                SourceManager &SourceMgr, DiagnosticsEngine &Diags,
266                const LangOptions &LangOpts, const TargetInfo *Target);
267   ~HeaderSearch();
268 
269   /// \brief Retrieve the header-search options with which this header search
270   /// was initialized.
getHeaderSearchOpts()271   HeaderSearchOptions &getHeaderSearchOpts() const { return *HSOpts; }
272 
getFileMgr()273   FileManager &getFileMgr() const { return FileMgr; }
274 
275   /// \brief Interface for setting the file search paths.
SetSearchPaths(const std::vector<DirectoryLookup> & dirs,unsigned angledDirIdx,unsigned systemDirIdx,bool noCurDirSearch)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.
AddSearchPath(const DirectoryLookup & dir,bool isAngled)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.
SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string,bool>> P)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.
HasIncludeAliasMap()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.
AddIncludeAlias(StringRef Source,StringRef Dest)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.
MapHeaderToIncludeAlias(StringRef Source)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 StringRef();
327   }
328 
329   /// \brief Set the path to the module cache.
setModuleCachePath(StringRef CachePath)330   void setModuleCachePath(StringRef CachePath) {
331     ModuleCachePath = CachePath;
332   }
333 
334   /// \brief Retrieve the path to the module cache.
getModuleCachePath()335   StringRef getModuleCachePath() const { return ModuleCachePath; }
336 
337   /// \brief Consider modules when including files from this directory.
setDirectoryHasModuleMap(const DirectoryEntry * Dir)338   void setDirectoryHasModuleMap(const DirectoryEntry* Dir) {
339     DirectoryHasModuleMap[Dir] = true;
340   }
341 
342   /// \brief Forget everything we know about headers so far.
ClearFileInfo()343   void ClearFileInfo() {
344     FileInfo.clear();
345   }
346 
SetExternalLookup(ExternalIdentifierLookup * EIL)347   void SetExternalLookup(ExternalIdentifierLookup *EIL) {
348     ExternalLookup = EIL;
349   }
350 
getExternalLookup()351   ExternalIdentifierLookup *getExternalLookup() const {
352     return ExternalLookup;
353   }
354 
355   /// \brief Set the external source of header information.
SetExternalSource(ExternalHeaderFileInfoSource * ES)356   void SetExternalSource(ExternalHeaderFileInfoSource *ES) {
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   const FileEntry *LookupFile(
392       StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
393       const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
394       ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
395       SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
396       ModuleMap::KnownHeader *SuggestedModule, bool SkipCache = false);
397 
398   /// \brief Look up a subframework for the specified \#include file.
399   ///
400   /// For example, if \#include'ing <HIToolbox/HIToolbox.h> from
401   /// within ".../Carbon.framework/Headers/Carbon.h", check to see if
402   /// HIToolbox is a subframework within Carbon.framework.  If so, return
403   /// the FileEntry for the designated file, otherwise return null.
404   const FileEntry *LookupSubframeworkHeader(
405       StringRef Filename,
406       const FileEntry *RelativeFileEnt,
407       SmallVectorImpl<char> *SearchPath,
408       SmallVectorImpl<char> *RelativePath,
409       ModuleMap::KnownHeader *SuggestedModule);
410 
411   /// \brief Look up the specified framework name in our framework cache.
412   /// \returns The DirectoryEntry it is in if we know, null otherwise.
LookupFrameworkCache(StringRef FWName)413   FrameworkCacheEntry &LookupFrameworkCache(StringRef FWName) {
414     return FrameworkMap[FWName];
415   }
416 
417   /// \brief Mark the specified file as a target of of a \#include,
418   /// \#include_next, or \#import directive.
419   ///
420   /// \return false if \#including the file will have no effect or true
421   /// if we should include it.
422   bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
423 
424 
425   /// \brief Return whether the specified file is a normal header,
426   /// a system header, or a C++ friendly system header.
getFileDirFlavor(const FileEntry * File)427   SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) {
428     return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
429   }
430 
431   /// \brief Mark the specified file as a "once only" file, e.g. due to
432   /// \#pragma once.
MarkFileIncludeOnce(const FileEntry * File)433   void MarkFileIncludeOnce(const FileEntry *File) {
434     HeaderFileInfo &FI = getFileInfo(File);
435     FI.isImport = true;
436     FI.isPragmaOnce = true;
437   }
438 
439   /// \brief Mark the specified file as a system header, e.g. due to
440   /// \#pragma GCC system_header.
MarkFileSystemHeader(const FileEntry * File)441   void MarkFileSystemHeader(const FileEntry *File) {
442     getFileInfo(File).DirInfo = SrcMgr::C_System;
443   }
444 
445   /// \brief Mark the specified file as part of a module.
446   void MarkFileModuleHeader(const FileEntry *File,
447                             ModuleMap::ModuleHeaderRole Role,
448                             bool IsCompiledModuleHeader);
449 
450   /// \brief Increment the count for the number of times the specified
451   /// FileEntry has been entered.
IncrementIncludeCount(const FileEntry * File)452   void IncrementIncludeCount(const FileEntry *File) {
453     ++getFileInfo(File).NumIncludes;
454   }
455 
456   /// \brief Mark the specified file as having a controlling macro.
457   ///
458   /// This is used by the multiple-include optimization to eliminate
459   /// no-op \#includes.
SetFileControllingMacro(const FileEntry * File,const IdentifierInfo * ControllingMacro)460   void SetFileControllingMacro(const FileEntry *File,
461                                const IdentifierInfo *ControllingMacro) {
462     getFileInfo(File).ControllingMacro = ControllingMacro;
463   }
464 
465   /// \brief Return true if this is the first time encountering this header.
FirstTimeLexingFile(const FileEntry * File)466   bool FirstTimeLexingFile(const FileEntry *File) {
467     return getFileInfo(File).NumIncludes == 1;
468   }
469 
470   /// \brief Determine whether this file is intended to be safe from
471   /// multiple inclusions, e.g., it has \#pragma once or a controlling
472   /// macro.
473   ///
474   /// This routine does not consider the effect of \#import
475   bool isFileMultipleIncludeGuarded(const FileEntry *File);
476 
477   /// CreateHeaderMap - This method returns a HeaderMap for the specified
478   /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
479   const HeaderMap *CreateHeaderMap(const FileEntry *FE);
480 
481   /// Returns true if modules are enabled.
enabledModules()482   bool enabledModules() const { return LangOpts.Modules; }
483 
484   /// \brief Retrieve the name of the module file that should be used to
485   /// load the given module.
486   ///
487   /// \param Module The module whose module file name will be returned.
488   ///
489   /// \returns The name of the module file that corresponds to this module,
490   /// or an empty string if this module does not correspond to any module file.
491   std::string getModuleFileName(Module *Module);
492 
493   /// \brief Retrieve the name of the module file that should be used to
494   /// load a module with the given name.
495   ///
496   /// \param ModuleName The module whose module file name will be returned.
497   ///
498   /// \param ModuleMapPath A path that when combined with \c ModuleName
499   /// uniquely identifies this module. See Module::ModuleMap.
500   ///
501   /// \returns The name of the module file that corresponds to this module,
502   /// or an empty string if this module does not correspond to any module file.
503   std::string getModuleFileName(StringRef ModuleName, StringRef ModuleMapPath);
504 
505   /// \brief Lookup a module Search for a module with the given name.
506   ///
507   /// \param ModuleName The name of the module we're looking for.
508   ///
509   /// \param AllowSearch Whether we are allowed to search in the various
510   /// search directories to produce a module definition. If not, this lookup
511   /// will only return an already-known module.
512   ///
513   /// \returns The module with the given name.
514   Module *lookupModule(StringRef ModuleName, bool AllowSearch = true);
515 
516   /// \brief Try to find a module map file in the given directory, returning
517   /// \c nullptr if none is found.
518   const FileEntry *lookupModuleMapFile(const DirectoryEntry *Dir,
519                                        bool IsFramework);
520 
IncrementFrameworkLookupCount()521   void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
522 
523   /// \brief Determine whether there is a module map that may map the header
524   /// with the given file name to a (sub)module.
525   /// Always returns false if modules are disabled.
526   ///
527   /// \param Filename The name of the file.
528   ///
529   /// \param Root The "root" directory, at which we should stop looking for
530   /// module maps.
531   ///
532   /// \param IsSystem Whether the directories we're looking at are system
533   /// header directories.
534   bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root,
535                     bool IsSystem);
536 
537   /// \brief Retrieve the module that corresponds to the given file, if any.
538   ///
539   /// \param File The header that we wish to map to a module.
540   ModuleMap::KnownHeader findModuleForHeader(const FileEntry *File) const;
541 
542   /// \brief Read the contents of the given module map file.
543   ///
544   /// \param File The module map file.
545   /// \param IsSystem Whether this file is in a system header directory.
546   ///
547   /// \returns true if an error occurred, false otherwise.
548   bool loadModuleMapFile(const FileEntry *File, bool IsSystem);
549 
550   /// \brief Collect the set of all known, top-level modules.
551   ///
552   /// \param Modules Will be filled with the set of known, top-level modules.
553   void collectAllModules(SmallVectorImpl<Module *> &Modules);
554 
555   /// \brief Load all known, top-level system modules.
556   void loadTopLevelSystemModules();
557 
558 private:
559   /// \brief Retrieve a module with the given name, which may be part of the
560   /// given framework.
561   ///
562   /// \param Name The name of the module to retrieve.
563   ///
564   /// \param Dir The framework directory (e.g., ModuleName.framework).
565   ///
566   /// \param IsSystem Whether the framework directory is part of the system
567   /// frameworks.
568   ///
569   /// \returns The module, if found; otherwise, null.
570   Module *loadFrameworkModule(StringRef Name,
571                               const DirectoryEntry *Dir,
572                               bool IsSystem);
573 
574   /// \brief Load all of the module maps within the immediate subdirectories
575   /// of the given search directory.
576   void loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir);
577 
578   /// \brief Return the HeaderFileInfo structure for the specified FileEntry.
getFileInfo(const FileEntry * FE)579   const HeaderFileInfo &getFileInfo(const FileEntry *FE) const {
580     return const_cast<HeaderSearch*>(this)->getFileInfo(FE);
581   }
582 
583 public:
584   /// \brief Retrieve the module map.
getModuleMap()585   ModuleMap &getModuleMap() { return ModMap; }
586 
header_file_size()587   unsigned header_file_size() const { return FileInfo.size(); }
588 
589   /// \brief Get a \c HeaderFileInfo structure for the specified \c FileEntry,
590   /// if one exists.
591   bool tryGetFileInfo(const FileEntry *FE, HeaderFileInfo &Result) const;
592 
593   // Used by external tools
594   typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator;
search_dir_begin()595   search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); }
search_dir_end()596   search_dir_iterator search_dir_end() const { return SearchDirs.end(); }
search_dir_size()597   unsigned search_dir_size() const { return SearchDirs.size(); }
598 
quoted_dir_begin()599   search_dir_iterator quoted_dir_begin() const {
600     return SearchDirs.begin();
601   }
quoted_dir_end()602   search_dir_iterator quoted_dir_end() const {
603     return SearchDirs.begin() + AngledDirIdx;
604   }
605 
angled_dir_begin()606   search_dir_iterator angled_dir_begin() const {
607     return SearchDirs.begin() + AngledDirIdx;
608   }
angled_dir_end()609   search_dir_iterator angled_dir_end() const {
610     return SearchDirs.begin() + SystemDirIdx;
611   }
612 
system_dir_begin()613   search_dir_iterator system_dir_begin() const {
614     return SearchDirs.begin() + SystemDirIdx;
615   }
system_dir_end()616   search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
617 
618   /// \brief Retrieve a uniqued framework name.
619   StringRef getUniqueFrameworkName(StringRef Framework);
620 
621   void PrintStats();
622 
623   size_t getTotalMemory() const;
624 
625   static std::string NormalizeDashIncludePath(StringRef File,
626                                               FileManager &FileMgr);
627 
628 private:
629   /// \brief Describes what happened when we tried to load a module map file.
630   enum LoadModuleMapResult {
631     /// \brief The module map file had already been loaded.
632     LMM_AlreadyLoaded,
633     /// \brief The module map file was loaded by this invocation.
634     LMM_NewlyLoaded,
635     /// \brief There is was directory with the given name.
636     LMM_NoDirectory,
637     /// \brief There was either no module map file or the module map file was
638     /// invalid.
639     LMM_InvalidModuleMap
640   };
641 
642   LoadModuleMapResult loadModuleMapFileImpl(const FileEntry *File,
643                                             bool IsSystem,
644                                             const DirectoryEntry *Dir);
645 
646   /// \brief Try to load the module map file in the given directory.
647   ///
648   /// \param DirName The name of the directory where we will look for a module
649   /// map file.
650   /// \param IsSystem Whether this is a system header directory.
651   /// \param IsFramework Whether this is a framework directory.
652   ///
653   /// \returns The result of attempting to load the module map file from the
654   /// named directory.
655   LoadModuleMapResult loadModuleMapFile(StringRef DirName, bool IsSystem,
656                                         bool IsFramework);
657 
658   /// \brief Try to load the module map file in the given directory.
659   ///
660   /// \param Dir The directory where we will look for a module map file.
661   /// \param IsSystem Whether this is a system header directory.
662   /// \param IsFramework Whether this is a framework directory.
663   ///
664   /// \returns The result of attempting to load the module map file from the
665   /// named directory.
666   LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir,
667                                         bool IsSystem, bool IsFramework);
668 
669   /// \brief Return the HeaderFileInfo structure for the specified FileEntry.
670   HeaderFileInfo &getFileInfo(const FileEntry *FE);
671 };
672 
673 }  // end namespace clang
674 
675 #endif
676