1193326Sed//===--- HeaderSearch.h - Resolve Header File Locations ---------*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This file defines the HeaderSearch interface.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#ifndef LLVM_CLANG_LEX_HEADERSEARCH_H
15193326Sed#define LLVM_CLANG_LEX_HEADERSEARCH_H
16193326Sed
17193326Sed#include "clang/Lex/DirectoryLookup.h"
18234353Sdim#include "clang/Lex/ModuleMap.h"
19239462Sdim#include "llvm/ADT/ArrayRef.h"
20243830Sdim#include "llvm/ADT/IntrusiveRefCntPtr.h"
21249423Sdim#include "llvm/ADT/OwningPtr.h"
22193326Sed#include "llvm/ADT/StringMap.h"
23226633Sdim#include "llvm/ADT/StringSet.h"
24226633Sdim#include "llvm/Support/Allocator.h"
25193326Sed#include <vector>
26193326Sed
27193326Sednamespace clang {
28234353Sdim
29234353Sdimclass DiagnosticsEngine;
30193326Sedclass ExternalIdentifierLookup;
31193326Sedclass FileEntry;
32193326Sedclass FileManager;
33243830Sdimclass HeaderSearchOptions;
34193326Sedclass IdentifierInfo;
35193326Sed
36239462Sdim/// \brief The preprocessor keeps track of this information for each
37239462Sdim/// file that is \#included.
38193326Sedstruct HeaderFileInfo {
39239462Sdim  /// \brief True if this is a \#import'd or \#pragma once file.
40218893Sdim  unsigned isImport : 1;
41198092Srdivacky
42239462Sdim  /// \brief True if this is a \#pragma once file.
43223017Sdim  unsigned isPragmaOnce : 1;
44223017Sdim
45193326Sed  /// DirInfo - Keep track of whether this is a system header, and if so,
46193326Sed  /// whether it is C++ clean or not.  This can be set by the include paths or
47239462Sdim  /// by \#pragma gcc system_header.  This is an instance of
48193326Sed  /// SrcMgr::CharacteristicKind.
49193326Sed  unsigned DirInfo : 2;
50198092Srdivacky
51218893Sdim  /// \brief Whether this header file info was supplied by an external source.
52218893Sdim  unsigned External : 1;
53249423Sdim
54249423Sdim  /// \brief Whether this header is part of a module.
55249423Sdim  unsigned isModuleHeader : 1;
56263508Sdim
57263508Sdim  /// \brief Whether this header is part of the module that we are building.
58263508Sdim  unsigned isCompilingModuleHeader : 1;
59263508Sdim
60263508Sdim  /// \brief Whether this header is part of the module that we are building.
61263508Sdim  /// This is an instance of ModuleMap::ModuleHeaderRole.
62263508Sdim  unsigned HeaderRole : 2;
63218893Sdim
64218893Sdim  /// \brief Whether this structure is considered to already have been
65218893Sdim  /// "resolved", meaning that it was loaded from the external source.
66218893Sdim  unsigned Resolved : 1;
67218893Sdim
68226633Sdim  /// \brief Whether this is a header inside a framework that is currently
69226633Sdim  /// being built.
70226633Sdim  ///
71226633Sdim  /// When a framework is being built, the headers have not yet been placed
72226633Sdim  /// into the appropriate framework subdirectories, and therefore are
73226633Sdim  /// provided via a header map. This bit indicates when this is one of
74226633Sdim  /// those framework headers.
75226633Sdim  unsigned IndexHeaderMapHeader : 1;
76226633Sdim
77239462Sdim  /// \brief The number of times the file has been included already.
78193326Sed  unsigned short NumIncludes;
79198092Srdivacky
80218893Sdim  /// \brief The ID number of the controlling macro.
81218893Sdim  ///
82218893Sdim  /// This ID number will be non-zero when there is a controlling
83218893Sdim  /// macro whose IdentifierInfo may not yet have been loaded from
84218893Sdim  /// external storage.
85218893Sdim  unsigned ControllingMacroID;
86218893Sdim
87239462Sdim  /// If this file has a \#ifndef XXX (or equivalent) guard that
88239462Sdim  /// protects the entire contents of the file, this is the identifier
89193326Sed  /// for the macro that controls whether or not it has any effect.
90193326Sed  ///
91193326Sed  /// Note: Most clients should use getControllingMacro() to access
92193326Sed  /// the controlling macro of this header, since
93193326Sed  /// getControllingMacro() is able to load a controlling macro from
94193326Sed  /// external storage.
95193326Sed  const IdentifierInfo *ControllingMacro;
96193326Sed
97226633Sdim  /// \brief If this header came from a framework include, this is the name
98226633Sdim  /// of the framework.
99226633Sdim  StringRef Framework;
100226633Sdim
101198092Srdivacky  HeaderFileInfo()
102223017Sdim    : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User),
103263508Sdim      External(false), isModuleHeader(false), isCompilingModuleHeader(false),
104263508Sdim      HeaderRole(ModuleMap::NormalHeader),
105263508Sdim      Resolved(false), IndexHeaderMapHeader(false),
106226633Sdim      NumIncludes(0), ControllingMacroID(0), ControllingMacro(0)  {}
107193326Sed
108193326Sed  /// \brief Retrieve the controlling macro for this header file, if
109193326Sed  /// any.
110193326Sed  const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External);
111218893Sdim
112218893Sdim  /// \brief Determine whether this is a non-default header file info, e.g.,
113218893Sdim  /// it corresponds to an actual header we've included or tried to include.
114218893Sdim  bool isNonDefault() const {
115223017Sdim    return isImport || isPragmaOnce || NumIncludes || ControllingMacro ||
116223017Sdim      ControllingMacroID;
117218893Sdim  }
118263508Sdim
119263508Sdim  /// \brief Get the HeaderRole properly typed.
120263508Sdim  ModuleMap::ModuleHeaderRole getHeaderRole() const {
121263508Sdim    return static_cast<ModuleMap::ModuleHeaderRole>(HeaderRole);
122263508Sdim  }
123263508Sdim
124263508Sdim  /// \brief Set the HeaderRole properly typed.
125263508Sdim  void setHeaderRole(ModuleMap::ModuleHeaderRole Role) {
126263508Sdim    HeaderRole = Role;
127263508Sdim  }
128193326Sed};
129193326Sed
130218893Sdim/// \brief An external source of header file information, which may supply
131218893Sdim/// information about header files already included.
132218893Sdimclass ExternalHeaderFileInfoSource {
133218893Sdimpublic:
134218893Sdim  virtual ~ExternalHeaderFileInfoSource();
135218893Sdim
136218893Sdim  /// \brief Retrieve the header file information for the given file entry.
137218893Sdim  ///
138218893Sdim  /// \returns Header file information for the given file entry, with the
139218893Sdim  /// \c External bit set. If the file entry is not known, return a
140218893Sdim  /// default-constructed \c HeaderFileInfo.
141218893Sdim  virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0;
142218893Sdim};
143218893Sdim
144239462Sdim/// \brief Encapsulates the information needed to find the file referenced
145239462Sdim/// by a \#include or \#include_next, (sub-)framework lookup, etc.
146193326Sedclass HeaderSearch {
147234353Sdim  /// This structure is used to record entries in our framework cache.
148234353Sdim  struct FrameworkCacheEntry {
149234353Sdim    /// The directory entry which should be used for the cached framework.
150234353Sdim    const DirectoryEntry *Directory;
151234353Sdim
152234353Sdim    /// Whether this framework has been "user-specified" to be treated as if it
153234353Sdim    /// were a system framework (even if it was found outside a system framework
154234353Sdim    /// directory).
155234353Sdim    bool IsUserSpecifiedSystemFramework;
156234353Sdim  };
157234353Sdim
158243830Sdim  /// \brief Header-search options used to initialize this header search.
159249423Sdim  IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts;
160243830Sdim
161193326Sed  FileManager &FileMgr;
162239462Sdim  /// \#include search path information.  Requests for \#include "x" search the
163239462Sdim  /// directory of the \#including file first, then each directory in SearchDirs
164223017Sdim  /// consecutively. Requests for <x> search the current dir first, then each
165223017Sdim  /// directory in SearchDirs, starting at AngledDirIdx, consecutively.  If
166193326Sed  /// NoCurDirSearch is true, then the check for the file in the current
167221345Sdim  /// directory is suppressed.
168193326Sed  std::vector<DirectoryLookup> SearchDirs;
169223017Sdim  unsigned AngledDirIdx;
170193326Sed  unsigned SystemDirIdx;
171193326Sed  bool NoCurDirSearch;
172198092Srdivacky
173239462Sdim  /// \brief \#include prefixes for which the 'system header' property is
174239462Sdim  /// overridden.
175239462Sdim  ///
176239462Sdim  /// For a \#include "x" or \#include \<x> directive, the last string in this
177239462Sdim  /// list which is a prefix of 'x' determines whether the file is treated as
178239462Sdim  /// a system header.
179239462Sdim  std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
180239462Sdim
181226633Sdim  /// \brief The path to the module cache.
182226633Sdim  std::string ModuleCachePath;
183226633Sdim
184239462Sdim  /// \brief All of the preprocessor-specific data about files that are
185239462Sdim  /// included, indexed by the FileEntry's UID.
186193326Sed  std::vector<HeaderFileInfo> FileInfo;
187193326Sed
188239462Sdim  /// \brief Keeps track of each lookup performed by LookupFile.
189239462Sdim  ///
190239462Sdim  /// The first part of the value is the starting index in SearchDirs
191239462Sdim  /// that the cached search was performed from.  If there is a hit and
192239462Sdim  /// this value doesn't match the current query, the cache has to be
193239462Sdim  /// ignored.  The second value is the entry in SearchDirs that satisfied
194239462Sdim  /// the query.
195226633Sdim  llvm::StringMap<std::pair<unsigned, unsigned>, llvm::BumpPtrAllocator>
196226633Sdim    LookupFileCache;
197198092Srdivacky
198239462Sdim  /// \brief Collection mapping a framework or subframework
199193326Sed  /// name like "Carbon" to the Carbon.framework directory.
200234353Sdim  llvm::StringMap<FrameworkCacheEntry, llvm::BumpPtrAllocator> FrameworkMap;
201193326Sed
202234353Sdim  /// IncludeAliases - maps include file names (including the quotes or
203234353Sdim  /// angle brackets) to other include file names.  This is used to support the
204234353Sdim  /// include_alias pragma for Microsoft compatibility.
205234353Sdim  typedef llvm::StringMap<std::string, llvm::BumpPtrAllocator>
206234353Sdim    IncludeAliasMap;
207234353Sdim  OwningPtr<IncludeAliasMap> IncludeAliases;
208234353Sdim
209198092Srdivacky  /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing
210193326Sed  /// headermaps.  This vector owns the headermap.
211193326Sed  std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps;
212193326Sed
213234353Sdim  /// \brief The mapping between modules and headers.
214249423Sdim  mutable ModuleMap ModMap;
215234353Sdim
216234353Sdim  /// \brief Describes whether a given directory has a module map in it.
217234353Sdim  llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap;
218234353Sdim
219226633Sdim  /// \brief Uniqued set of framework names, which is used to track which
220226633Sdim  /// headers were included as framework headers.
221226633Sdim  llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames;
222226633Sdim
223193326Sed  /// \brief Entity used to resolve the identifier IDs of controlling
224193326Sed  /// macros into IdentifierInfo pointers, as needed.
225193326Sed  ExternalIdentifierLookup *ExternalLookup;
226193326Sed
227218893Sdim  /// \brief Entity used to look up stored header file information.
228218893Sdim  ExternalHeaderFileInfoSource *ExternalSource;
229218893Sdim
230193326Sed  // Various statistics we track for performance analysis.
231193326Sed  unsigned NumIncluded;
232193326Sed  unsigned NumMultiIncludeFileOptzn;
233193326Sed  unsigned NumFrameworkLookups, NumSubFrameworkLookups;
234193326Sed
235193326Sed  // HeaderSearch doesn't support default or copy construction.
236243830Sdim  HeaderSearch(const HeaderSearch&) LLVM_DELETED_FUNCTION;
237243830Sdim  void operator=(const HeaderSearch&) LLVM_DELETED_FUNCTION;
238243830Sdim
239234353Sdim  friend class DirectoryLookup;
240234353Sdim
241193326Sedpublic:
242249423Sdim  HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
243263508Sdim               SourceManager &SourceMgr, DiagnosticsEngine &Diags,
244234353Sdim               const LangOptions &LangOpts, const TargetInfo *Target);
245193326Sed  ~HeaderSearch();
246193326Sed
247243830Sdim  /// \brief Retrieve the header-search options with which this header search
248243830Sdim  /// was initialized.
249243830Sdim  HeaderSearchOptions &getHeaderSearchOpts() const { return *HSOpts; }
250243830Sdim
251193326Sed  FileManager &getFileMgr() const { return FileMgr; }
252193326Sed
253239462Sdim  /// \brief Interface for setting the file search paths.
254193326Sed  void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
255223017Sdim                      unsigned angledDirIdx, unsigned systemDirIdx,
256223017Sdim                      bool noCurDirSearch) {
257223017Sdim    assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
258223017Sdim        "Directory indicies are unordered");
259193326Sed    SearchDirs = dirs;
260223017Sdim    AngledDirIdx = angledDirIdx;
261193326Sed    SystemDirIdx = systemDirIdx;
262193326Sed    NoCurDirSearch = noCurDirSearch;
263193326Sed    //LookupFileCache.clear();
264193326Sed  }
265198092Srdivacky
266239462Sdim  /// \brief Add an additional search path.
267234353Sdim  void AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
268234353Sdim    unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
269234353Sdim    SearchDirs.insert(SearchDirs.begin() + idx, dir);
270234353Sdim    if (!isAngled)
271234353Sdim      AngledDirIdx++;
272234353Sdim    SystemDirIdx++;
273234353Sdim  }
274234353Sdim
275239462Sdim  /// \brief Set the list of system header prefixes.
276239462Sdim  void SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string, bool> > P) {
277239462Sdim    SystemHeaderPrefixes.assign(P.begin(), P.end());
278239462Sdim  }
279239462Sdim
280239462Sdim  /// \brief Checks whether the map exists or not.
281234353Sdim  bool HasIncludeAliasMap() const {
282263508Sdim    return IncludeAliases.isValid();
283234353Sdim  }
284234353Sdim
285239462Sdim  /// \brief Map the source include name to the dest include name.
286239462Sdim  ///
287234353Sdim  /// The Source should include the angle brackets or quotes, the dest
288234353Sdim  /// should not.  This allows for distinction between <> and "" headers.
289234353Sdim  void AddIncludeAlias(StringRef Source, StringRef Dest) {
290234353Sdim    if (!IncludeAliases)
291234353Sdim      IncludeAliases.reset(new IncludeAliasMap);
292234353Sdim    (*IncludeAliases)[Source] = Dest;
293234353Sdim  }
294234353Sdim
295234353Sdim  /// MapHeaderToIncludeAlias - Maps one header file name to a different header
296234353Sdim  /// file name, for use with the include_alias pragma.  Note that the source
297234353Sdim  /// file name should include the angle brackets or quotes.  Returns StringRef
298234353Sdim  /// as null if the header cannot be mapped.
299234353Sdim  StringRef MapHeaderToIncludeAlias(StringRef Source) {
300234353Sdim    assert(IncludeAliases && "Trying to map headers when there's no map");
301234353Sdim
302234353Sdim    // Do any filename replacements before anything else
303234353Sdim    IncludeAliasMap::const_iterator Iter = IncludeAliases->find(Source);
304234353Sdim    if (Iter != IncludeAliases->end())
305234353Sdim      return Iter->second;
306234353Sdim    return StringRef();
307234353Sdim  }
308234353Sdim
309234353Sdim  /// \brief Set the path to the module cache.
310234353Sdim  void setModuleCachePath(StringRef CachePath) {
311226633Sdim    ModuleCachePath = CachePath;
312226633Sdim  }
313226633Sdim
314234353Sdim  /// \brief Retrieve the path to the module cache.
315234353Sdim  StringRef getModuleCachePath() const { return ModuleCachePath; }
316243830Sdim
317243830Sdim  /// \brief Consider modules when including files from this directory.
318243830Sdim  void setDirectoryHasModuleMap(const DirectoryEntry* Dir) {
319243830Sdim    DirectoryHasModuleMap[Dir] = true;
320243830Sdim  }
321234353Sdim
322239462Sdim  /// \brief Forget everything we know about headers so far.
323193326Sed  void ClearFileInfo() {
324193326Sed    FileInfo.clear();
325193326Sed  }
326198092Srdivacky
327193326Sed  void SetExternalLookup(ExternalIdentifierLookup *EIL) {
328193326Sed    ExternalLookup = EIL;
329193326Sed  }
330193326Sed
331218893Sdim  ExternalIdentifierLookup *getExternalLookup() const {
332218893Sdim    return ExternalLookup;
333218893Sdim  }
334218893Sdim
335218893Sdim  /// \brief Set the external source of header information.
336218893Sdim  void SetExternalSource(ExternalHeaderFileInfoSource *ES) {
337218893Sdim    ExternalSource = ES;
338218893Sdim  }
339218893Sdim
340234353Sdim  /// \brief Set the target information for the header search, if not
341234353Sdim  /// already known.
342234353Sdim  void setTarget(const TargetInfo &Target);
343234353Sdim
344239462Sdim  /// \brief Given a "foo" or \<foo> reference, look up the indicated file,
345221345Sdim  /// return null on failure.
346221345Sdim  ///
347221345Sdim  /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member
348221345Sdim  /// the file was found in, or null if not applicable.
349221345Sdim  ///
350221345Sdim  /// \param isAngled indicates whether the file reference is a <> reference.
351221345Sdim  ///
352221345Sdim  /// \param CurDir If non-null, the file was found in the specified directory
353239462Sdim  /// search location.  This is used to implement \#include_next.
354221345Sdim  ///
355239462Sdim  /// \param CurFileEnt If non-null, indicates where the \#including file is, in
356221345Sdim  /// case a relative search is needed.
357221345Sdim  ///
358221345Sdim  /// \param SearchPath If non-null, will be set to the search path relative
359221345Sdim  /// to which the file was found. If the include path is absolute, SearchPath
360221345Sdim  /// will be set to an empty string.
361221345Sdim  ///
362221345Sdim  /// \param RelativePath If non-null, will be set to the path relative to
363221345Sdim  /// SearchPath at which the file was found. This only differs from the
364221345Sdim  /// Filename for framework includes.
365226633Sdim  ///
366226633Sdim  /// \param SuggestedModule If non-null, and the file found is semantically
367234353Sdim  /// part of a known module, this will be set to the module that should
368234353Sdim  /// be imported instead of preprocessing/parsing the file found.
369226633Sdim  const FileEntry *LookupFile(StringRef Filename, bool isAngled,
370193326Sed                              const DirectoryLookup *FromDir,
371193326Sed                              const DirectoryLookup *&CurDir,
372221345Sdim                              const FileEntry *CurFileEnt,
373226633Sdim                              SmallVectorImpl<char> *SearchPath,
374226633Sdim                              SmallVectorImpl<char> *RelativePath,
375263508Sdim                              ModuleMap::KnownHeader *SuggestedModule,
376234353Sdim                              bool SkipCache = false);
377198092Srdivacky
378239462Sdim  /// \brief Look up a subframework for the specified \#include file.
379239462Sdim  ///
380239462Sdim  /// For example, if \#include'ing <HIToolbox/HIToolbox.h> from
381239462Sdim  /// within ".../Carbon.framework/Headers/Carbon.h", check to see if
382239462Sdim  /// HIToolbox is a subframework within Carbon.framework.  If so, return
383239462Sdim  /// the FileEntry for the designated file, otherwise return null.
384221345Sdim  const FileEntry *LookupSubframeworkHeader(
385226633Sdim      StringRef Filename,
386221345Sdim      const FileEntry *RelativeFileEnt,
387226633Sdim      SmallVectorImpl<char> *SearchPath,
388249423Sdim      SmallVectorImpl<char> *RelativePath,
389263508Sdim      ModuleMap::KnownHeader *SuggestedModule);
390198092Srdivacky
391239462Sdim  /// \brief Look up the specified framework name in our framework cache.
392239462Sdim  /// \returns The DirectoryEntry it is in if we know, null otherwise.
393234353Sdim  FrameworkCacheEntry &LookupFrameworkCache(StringRef FWName) {
394202379Srdivacky    return FrameworkMap.GetOrCreateValue(FWName).getValue();
395193326Sed  }
396198092Srdivacky
397239462Sdim  /// \brief Mark the specified file as a target of of a \#include,
398239462Sdim  /// \#include_next, or \#import directive.
399239462Sdim  ///
400239462Sdim  /// \return false if \#including the file will have no effect or true
401239462Sdim  /// if we should include it.
402193326Sed  bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
403198092Srdivacky
404198092Srdivacky
405239462Sdim  /// \brief Return whether the specified file is a normal header,
406193326Sed  /// a system header, or a C++ friendly system header.
407193326Sed  SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) {
408193326Sed    return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
409193326Sed  }
410198092Srdivacky
411239462Sdim  /// \brief Mark the specified file as a "once only" file, e.g. due to
412239462Sdim  /// \#pragma once.
413193326Sed  void MarkFileIncludeOnce(const FileEntry *File) {
414223017Sdim    HeaderFileInfo &FI = getFileInfo(File);
415223017Sdim    FI.isImport = true;
416223017Sdim    FI.isPragmaOnce = true;
417193326Sed  }
418193326Sed
419239462Sdim  /// \brief Mark the specified file as a system header, e.g. due to
420239462Sdim  /// \#pragma GCC system_header.
421193326Sed  void MarkFileSystemHeader(const FileEntry *File) {
422193326Sed    getFileInfo(File).DirInfo = SrcMgr::C_System;
423193326Sed  }
424198092Srdivacky
425249423Sdim  /// \brief Mark the specified file as part of a module.
426263508Sdim  void MarkFileModuleHeader(const FileEntry *File,
427263508Sdim                            ModuleMap::ModuleHeaderRole Role,
428263508Sdim                            bool IsCompiledModuleHeader);
429249423Sdim
430239462Sdim  /// \brief Increment the count for the number of times the specified
431239462Sdim  /// FileEntry has been entered.
432193326Sed  void IncrementIncludeCount(const FileEntry *File) {
433193326Sed    ++getFileInfo(File).NumIncludes;
434193326Sed  }
435198092Srdivacky
436239462Sdim  /// \brief Mark the specified file as having a controlling macro.
437239462Sdim  ///
438239462Sdim  /// This is used by the multiple-include optimization to eliminate
439239462Sdim  /// no-op \#includes.
440193326Sed  void SetFileControllingMacro(const FileEntry *File,
441193326Sed                               const IdentifierInfo *ControllingMacro) {
442193326Sed    getFileInfo(File).ControllingMacro = ControllingMacro;
443193326Sed  }
444198092Srdivacky
445263508Sdim  /// \brief Return true if this is the first time encountering this header.
446263508Sdim  bool FirstTimeLexingFile(const FileEntry *File) {
447263508Sdim    return getFileInfo(File).NumIncludes == 1;
448263508Sdim  }
449263508Sdim
450223017Sdim  /// \brief Determine whether this file is intended to be safe from
451239462Sdim  /// multiple inclusions, e.g., it has \#pragma once or a controlling
452223017Sdim  /// macro.
453223017Sdim  ///
454239462Sdim  /// This routine does not consider the effect of \#import
455223017Sdim  bool isFileMultipleIncludeGuarded(const FileEntry *File);
456223017Sdim
457193326Sed  /// CreateHeaderMap - This method returns a HeaderMap for the specified
458239462Sdim  /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
459193326Sed  const HeaderMap *CreateHeaderMap(const FileEntry *FE);
460198092Srdivacky
461234353Sdim  /// \brief Retrieve the name of the module file that should be used to
462234353Sdim  /// load the given module.
463226633Sdim  ///
464234353Sdim  /// \param Module The module whose module file name will be returned.
465226633Sdim  ///
466234353Sdim  /// \returns The name of the module file that corresponds to this module,
467234353Sdim  /// or an empty string if this module does not correspond to any module file.
468234353Sdim  std::string getModuleFileName(Module *Module);
469234353Sdim
470234353Sdim  /// \brief Retrieve the name of the module file that should be used to
471234353Sdim  /// load a module with the given name.
472226633Sdim  ///
473239462Sdim  /// \param ModuleName The module whose module file name will be returned.
474234353Sdim  ///
475234353Sdim  /// \returns The name of the module file that corresponds to this module,
476234353Sdim  /// or an empty string if this module does not correspond to any module file.
477234353Sdim  std::string getModuleFileName(StringRef ModuleName);
478234353Sdim
479234353Sdim  /// \brief Lookup a module Search for a module with the given name.
480234353Sdim  ///
481234353Sdim  /// \param ModuleName The name of the module we're looking for.
482234353Sdim  ///
483234353Sdim  /// \param AllowSearch Whether we are allowed to search in the various
484234353Sdim  /// search directories to produce a module definition. If not, this lookup
485234353Sdim  /// will only return an already-known module.
486234353Sdim  ///
487234353Sdim  /// \returns The module with the given name.
488234353Sdim  Module *lookupModule(StringRef ModuleName, bool AllowSearch = true);
489226633Sdim
490193326Sed  void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
491193326Sed
492234353Sdim  /// \brief Determine whether there is a module map that may map the header
493234353Sdim  /// with the given file name to a (sub)module.
494234353Sdim  ///
495234353Sdim  /// \param Filename The name of the file.
496234353Sdim  ///
497234353Sdim  /// \param Root The "root" directory, at which we should stop looking for
498234353Sdim  /// module maps.
499263508Sdim  ///
500263508Sdim  /// \param IsSystem Whether the directories we're looking at are system
501263508Sdim  /// header directories.
502263508Sdim  bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root,
503263508Sdim                    bool IsSystem);
504234353Sdim
505234353Sdim  /// \brief Retrieve the module that corresponds to the given file, if any.
506234353Sdim  ///
507234353Sdim  /// \param File The header that we wish to map to a module.
508263508Sdim  ModuleMap::KnownHeader findModuleForHeader(const FileEntry *File) const;
509234353Sdim
510234353Sdim  /// \brief Read the contents of the given module map file.
511234353Sdim  ///
512234353Sdim  /// \param File The module map file.
513263508Sdim  /// \param IsSystem Whether this file is in a system header directory.
514234353Sdim  ///
515234353Sdim  /// \returns true if an error occurred, false otherwise.
516263508Sdim  bool loadModuleMapFile(const FileEntry *File, bool IsSystem);
517234353Sdim
518234353Sdim  /// \brief Collect the set of all known, top-level modules.
519234353Sdim  ///
520234353Sdim  /// \param Modules Will be filled with the set of known, top-level modules.
521249423Sdim  void collectAllModules(SmallVectorImpl<Module *> &Modules);
522263508Sdim
523263508Sdim  /// \brief Load all known, top-level system modules.
524263508Sdim  void loadTopLevelSystemModules();
525263508Sdim
526234353Sdimprivate:
527234353Sdim  /// \brief Retrieve a module with the given name, which may be part of the
528234353Sdim  /// given framework.
529234353Sdim  ///
530234353Sdim  /// \param Name The name of the module to retrieve.
531234353Sdim  ///
532234353Sdim  /// \param Dir The framework directory (e.g., ModuleName.framework).
533234353Sdim  ///
534234353Sdim  /// \param IsSystem Whether the framework directory is part of the system
535234353Sdim  /// frameworks.
536234353Sdim  ///
537234353Sdim  /// \returns The module, if found; otherwise, null.
538234353Sdim  Module *loadFrameworkModule(StringRef Name,
539234353Sdim                              const DirectoryEntry *Dir,
540234353Sdim                              bool IsSystem);
541249423Sdim
542249423Sdim  /// \brief Load all of the module maps within the immediate subdirectories
543249423Sdim  /// of the given search directory.
544249423Sdim  void loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir);
545249423Sdim
546234353Sdimpublic:
547234353Sdim  /// \brief Retrieve the module map.
548234353Sdim  ModuleMap &getModuleMap() { return ModMap; }
549234353Sdim
550205408Srdivacky  unsigned header_file_size() const { return FileInfo.size(); }
551193326Sed
552239462Sdim  /// \brief Return the HeaderFileInfo structure for the specified FileEntry.
553234353Sdim  const HeaderFileInfo &getFileInfo(const FileEntry *FE) const {
554234353Sdim    return const_cast<HeaderSearch*>(this)->getFileInfo(FE);
555234353Sdim  }
556234353Sdim
557221345Sdim  // Used by external tools
558221345Sdim  typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator;
559221345Sdim  search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); }
560221345Sdim  search_dir_iterator search_dir_end() const { return SearchDirs.end(); }
561221345Sdim  unsigned search_dir_size() const { return SearchDirs.size(); }
562221345Sdim
563223017Sdim  search_dir_iterator quoted_dir_begin() const {
564223017Sdim    return SearchDirs.begin();
565223017Sdim  }
566223017Sdim  search_dir_iterator quoted_dir_end() const {
567223017Sdim    return SearchDirs.begin() + AngledDirIdx;
568223017Sdim  }
569223017Sdim
570223017Sdim  search_dir_iterator angled_dir_begin() const {
571223017Sdim    return SearchDirs.begin() + AngledDirIdx;
572223017Sdim  }
573223017Sdim  search_dir_iterator angled_dir_end() const {
574223017Sdim    return SearchDirs.begin() + SystemDirIdx;
575223017Sdim  }
576223017Sdim
577221345Sdim  search_dir_iterator system_dir_begin() const {
578221345Sdim    return SearchDirs.begin() + SystemDirIdx;
579221345Sdim  }
580221345Sdim  search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
581221345Sdim
582226633Sdim  /// \brief Retrieve a uniqued framework name.
583226633Sdim  StringRef getUniqueFrameworkName(StringRef Framework);
584226633Sdim
585193326Sed  void PrintStats();
586226633Sdim
587226633Sdim  size_t getTotalMemory() const;
588226633Sdim
589234353Sdim  static std::string NormalizeDashIncludePath(StringRef File,
590234353Sdim                                              FileManager &FileMgr);
591234353Sdim
592193326Sedprivate:
593234353Sdim  /// \brief Describes what happened when we tried to load a module map file.
594234353Sdim  enum LoadModuleMapResult {
595234353Sdim    /// \brief The module map file had already been loaded.
596234353Sdim    LMM_AlreadyLoaded,
597234353Sdim    /// \brief The module map file was loaded by this invocation.
598234353Sdim    LMM_NewlyLoaded,
599234353Sdim    /// \brief There is was directory with the given name.
600234353Sdim    LMM_NoDirectory,
601234353Sdim    /// \brief There was either no module map file or the module map file was
602234353Sdim    /// invalid.
603234353Sdim    LMM_InvalidModuleMap
604234353Sdim  };
605234353Sdim
606234353Sdim  /// \brief Try to load the module map file in the given directory.
607234353Sdim  ///
608234353Sdim  /// \param DirName The name of the directory where we will look for a module
609234353Sdim  /// map file.
610263508Sdim  /// \param IsSystem Whether this is a system header directory.
611234353Sdim  ///
612234353Sdim  /// \returns The result of attempting to load the module map file from the
613234353Sdim  /// named directory.
614263508Sdim  LoadModuleMapResult loadModuleMapFile(StringRef DirName, bool IsSystem);
615198092Srdivacky
616234353Sdim  /// \brief Try to load the module map file in the given directory.
617234353Sdim  ///
618234353Sdim  /// \param Dir The directory where we will look for a module map file.
619263508Sdim  /// \param IsSystem Whether this is a system header directory.
620234353Sdim  ///
621234353Sdim  /// \returns The result of attempting to load the module map file from the
622234353Sdim  /// named directory.
623263508Sdim  LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir,
624263508Sdim                                        bool IsSystem);
625234353Sdim
626239462Sdim  /// \brief Return the HeaderFileInfo structure for the specified FileEntry.
627193326Sed  HeaderFileInfo &getFileInfo(const FileEntry *FE);
628193326Sed};
629193326Sed
630193326Sed}  // end namespace clang
631193326Sed
632193326Sed#endif
633