HeaderSearch.h revision 1c2e9332fa69727425a3a2b912e36e2ab62083f8
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/StringMap.h"
20#include "llvm/ADT/StringSet.h"
21#include "llvm/Support/Allocator.h"
22#include <vector>
23
24namespace clang {
25
26class DiagnosticsEngine;
27class ExternalIdentifierLookup;
28class FileEntry;
29class FileManager;
30class IdentifierInfo;
31
32/// HeaderFileInfo - The preprocessor keeps track of this information for each
33/// file that is #included.
34struct HeaderFileInfo {
35  /// isImport - True if this is a #import'd or #pragma once file.
36  unsigned isImport : 1;
37
38  /// isPragmaOnce - True if this is  #pragma once file.
39  unsigned isPragmaOnce : 1;
40
41  /// DirInfo - Keep track of whether this is a system header, and if so,
42  /// whether it is C++ clean or not.  This can be set by the include paths or
43  /// by #pragma gcc system_header.  This is an instance of
44  /// SrcMgr::CharacteristicKind.
45  unsigned DirInfo : 2;
46
47  /// \brief Whether this header file info was supplied by an external source.
48  unsigned External : 1;
49
50  /// \brief Whether this structure is considered to already have been
51  /// "resolved", meaning that it was loaded from the external source.
52  unsigned Resolved : 1;
53
54  /// \brief Whether this is a header inside a framework that is currently
55  /// being built.
56  ///
57  /// When a framework is being built, the headers have not yet been placed
58  /// into the appropriate framework subdirectories, and therefore are
59  /// provided via a header map. This bit indicates when this is one of
60  /// those framework headers.
61  unsigned IndexHeaderMapHeader : 1;
62
63  /// NumIncludes - This is the number of times the file has been included
64  /// already.
65  unsigned short NumIncludes;
66
67  /// \brief The ID number of the controlling macro.
68  ///
69  /// This ID number will be non-zero when there is a controlling
70  /// macro whose IdentifierInfo may not yet have been loaded from
71  /// external storage.
72  unsigned ControllingMacroID;
73
74  /// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard
75  /// that protects the entire contents of the file, this is the identifier
76  /// for the macro that controls whether or not it has any effect.
77  ///
78  /// Note: Most clients should use getControllingMacro() to access
79  /// the controlling macro of this header, since
80  /// getControllingMacro() is able to load a controlling macro from
81  /// external storage.
82  const IdentifierInfo *ControllingMacro;
83
84  /// \brief If this header came from a framework include, this is the name
85  /// of the framework.
86  StringRef Framework;
87
88  HeaderFileInfo()
89    : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User),
90      External(false), Resolved(false), IndexHeaderMapHeader(false),
91      NumIncludes(0), ControllingMacroID(0), ControllingMacro(0)  {}
92
93  /// \brief Retrieve the controlling macro for this header file, if
94  /// any.
95  const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External);
96
97  /// \brief Determine whether this is a non-default header file info, e.g.,
98  /// it corresponds to an actual header we've included or tried to include.
99  bool isNonDefault() const {
100    return isImport || isPragmaOnce || NumIncludes || ControllingMacro ||
101      ControllingMacroID;
102  }
103};
104
105/// \brief An external source of header file information, which may supply
106/// information about header files already included.
107class ExternalHeaderFileInfoSource {
108public:
109  virtual ~ExternalHeaderFileInfoSource();
110
111  /// \brief Retrieve the header file information for the given file entry.
112  ///
113  /// \returns Header file information for the given file entry, with the
114  /// \c External bit set. If the file entry is not known, return a
115  /// default-constructed \c HeaderFileInfo.
116  virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0;
117};
118
119/// HeaderSearch - This class encapsulates the information needed to find the
120/// file referenced by a #include or #include_next, (sub-)framework lookup, etc.
121class HeaderSearch {
122  FileManager &FileMgr;
123  DiagnosticsEngine &Diags;
124  /// #include search path information.  Requests for #include "x" search the
125  /// directory of the #including file first, then each directory in SearchDirs
126  /// consecutively. Requests for <x> search the current dir first, then each
127  /// directory in SearchDirs, starting at AngledDirIdx, consecutively.  If
128  /// NoCurDirSearch is true, then the check for the file in the current
129  /// directory is suppressed.
130  std::vector<DirectoryLookup> SearchDirs;
131  unsigned AngledDirIdx;
132  unsigned SystemDirIdx;
133  bool NoCurDirSearch;
134
135  /// \brief The path to the module cache.
136  std::string ModuleCachePath;
137
138  /// \brief The name of the module we're building.
139  std::string BuildingModule;
140
141  /// FileInfo - This contains all of the preprocessor-specific data about files
142  /// that are included.  The vector is indexed by the FileEntry's UID.
143  ///
144  std::vector<HeaderFileInfo> FileInfo;
145
146  /// LookupFileCache - This is keeps track of each lookup performed by
147  /// LookupFile.  The first part of the value is the starting index in
148  /// SearchDirs that the cached search was performed from.  If there is a hit
149  /// and this value doesn't match the current query, the cache has to be
150  /// ignored.  The second value is the entry in SearchDirs that satisfied the
151  /// query.
152  llvm::StringMap<std::pair<unsigned, unsigned>, llvm::BumpPtrAllocator>
153    LookupFileCache;
154
155  /// FrameworkMap - This is a collection mapping a framework or subframework
156  /// name like "Carbon" to the Carbon.framework directory.
157  llvm::StringMap<const DirectoryEntry *, llvm::BumpPtrAllocator>
158    FrameworkMap;
159
160  /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing
161  /// headermaps.  This vector owns the headermap.
162  std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps;
163
164  /// \brief The mapping between modules and headers.
165  ModuleMap ModMap;
166
167  /// \brief Describes whether a given directory has a module map in it.
168  llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap;
169
170  /// \brief Uniqued set of framework names, which is used to track which
171  /// headers were included as framework headers.
172  llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames;
173
174  /// \brief Entity used to resolve the identifier IDs of controlling
175  /// macros into IdentifierInfo pointers, as needed.
176  ExternalIdentifierLookup *ExternalLookup;
177
178  /// \brief Entity used to look up stored header file information.
179  ExternalHeaderFileInfoSource *ExternalSource;
180
181  // Various statistics we track for performance analysis.
182  unsigned NumIncluded;
183  unsigned NumMultiIncludeFileOptzn;
184  unsigned NumFrameworkLookups, NumSubFrameworkLookups;
185
186  // HeaderSearch doesn't support default or copy construction.
187  explicit HeaderSearch();
188  explicit HeaderSearch(const HeaderSearch&);
189  void operator=(const HeaderSearch&);
190public:
191  HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags);
192  ~HeaderSearch();
193
194  FileManager &getFileMgr() const { return FileMgr; }
195
196  /// SetSearchPaths - Interface for setting the file search paths.
197  ///
198  void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
199                      unsigned angledDirIdx, unsigned systemDirIdx,
200                      bool noCurDirSearch) {
201    assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
202        "Directory indicies are unordered");
203    SearchDirs = dirs;
204    AngledDirIdx = angledDirIdx;
205    SystemDirIdx = systemDirIdx;
206    NoCurDirSearch = noCurDirSearch;
207    //LookupFileCache.clear();
208  }
209
210  /// AddSearchPath - Add an additional search path.
211  void AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
212    unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
213    SearchDirs.insert(SearchDirs.begin() + idx, dir);
214    if (!isAngled)
215      AngledDirIdx++;
216    SystemDirIdx++;
217  }
218
219  /// \brief Set the path to the module cache and the name of the module
220  /// we're building
221  void configureModules(StringRef CachePath, StringRef BuildingModule) {
222    ModuleCachePath = CachePath;
223    this->BuildingModule = BuildingModule;
224  }
225
226  /// \brief Retrieve the path to the module cache.
227  StringRef getModuleCachePath() const { return ModuleCachePath; }
228
229  /// ClearFileInfo - Forget everything we know about headers so far.
230  void ClearFileInfo() {
231    FileInfo.clear();
232  }
233
234  void SetExternalLookup(ExternalIdentifierLookup *EIL) {
235    ExternalLookup = EIL;
236  }
237
238  ExternalIdentifierLookup *getExternalLookup() const {
239    return ExternalLookup;
240  }
241
242  /// \brief Set the external source of header information.
243  void SetExternalSource(ExternalHeaderFileInfoSource *ES) {
244    ExternalSource = ES;
245  }
246
247  /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
248  /// return null on failure.
249  ///
250  /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member
251  /// the file was found in, or null if not applicable.
252  ///
253  /// \param isAngled indicates whether the file reference is a <> reference.
254  ///
255  /// \param CurDir If non-null, the file was found in the specified directory
256  /// search location.  This is used to implement #include_next.
257  ///
258  /// \param CurFileEnt If non-null, indicates where the #including file is, in
259  /// case a relative search is needed.
260  ///
261  /// \param SearchPath If non-null, will be set to the search path relative
262  /// to which the file was found. If the include path is absolute, SearchPath
263  /// will be set to an empty string.
264  ///
265  /// \param RelativePath If non-null, will be set to the path relative to
266  /// SearchPath at which the file was found. This only differs from the
267  /// Filename for framework includes.
268  ///
269  /// \param SuggestedModule If non-null, and the file found is semantically
270  /// part of a known module, this will be set to the module that should
271  /// be imported instead of preprocessing/parsing the file found.
272  const FileEntry *LookupFile(StringRef Filename, bool isAngled,
273                              const DirectoryLookup *FromDir,
274                              const DirectoryLookup *&CurDir,
275                              const FileEntry *CurFileEnt,
276                              SmallVectorImpl<char> *SearchPath,
277                              SmallVectorImpl<char> *RelativePath,
278                              ModuleMap::Module **SuggestedModule,
279                              bool SkipCache = false);
280
281  /// LookupSubframeworkHeader - Look up a subframework for the specified
282  /// #include file.  For example, if #include'ing <HIToolbox/HIToolbox.h> from
283  /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
284  /// is a subframework within Carbon.framework.  If so, return the FileEntry
285  /// for the designated file, otherwise return null.
286  const FileEntry *LookupSubframeworkHeader(
287      StringRef Filename,
288      const FileEntry *RelativeFileEnt,
289      SmallVectorImpl<char> *SearchPath,
290      SmallVectorImpl<char> *RelativePath);
291
292  /// LookupFrameworkCache - Look up the specified framework name in our
293  /// framework cache, returning the DirectoryEntry it is in if we know,
294  /// otherwise, return null.
295  const DirectoryEntry *&LookupFrameworkCache(StringRef FWName) {
296    return FrameworkMap.GetOrCreateValue(FWName).getValue();
297  }
298
299  /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
300  /// #include, #include_next, or #import directive.  Return false if #including
301  /// the file will have no effect or true if we should include it.
302  bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
303
304
305  /// getFileDirFlavor - Return whether the specified file is a normal header,
306  /// a system header, or a C++ friendly system header.
307  SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) {
308    return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
309  }
310
311  /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g.
312  /// due to #pragma once.
313  void MarkFileIncludeOnce(const FileEntry *File) {
314    HeaderFileInfo &FI = getFileInfo(File);
315    FI.isImport = true;
316    FI.isPragmaOnce = true;
317  }
318
319  /// MarkFileSystemHeader - Mark the specified file as a system header, e.g.
320  /// due to #pragma GCC system_header.
321  void MarkFileSystemHeader(const FileEntry *File) {
322    getFileInfo(File).DirInfo = SrcMgr::C_System;
323  }
324
325  /// IncrementIncludeCount - Increment the count for the number of times the
326  /// specified FileEntry has been entered.
327  void IncrementIncludeCount(const FileEntry *File) {
328    ++getFileInfo(File).NumIncludes;
329  }
330
331  /// SetFileControllingMacro - Mark the specified file as having a controlling
332  /// macro.  This is used by the multiple-include optimization to eliminate
333  /// no-op #includes.
334  void SetFileControllingMacro(const FileEntry *File,
335                               const IdentifierInfo *ControllingMacro) {
336    getFileInfo(File).ControllingMacro = ControllingMacro;
337  }
338
339  /// \brief Determine whether this file is intended to be safe from
340  /// multiple inclusions, e.g., it has #pragma once or a controlling
341  /// macro.
342  ///
343  /// This routine does not consider the effect of #import
344  bool isFileMultipleIncludeGuarded(const FileEntry *File);
345
346  /// CreateHeaderMap - This method returns a HeaderMap for the specified
347  /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
348  const HeaderMap *CreateHeaderMap(const FileEntry *FE);
349
350  /// \brief Search in the module cache path for a module with the given
351  /// name.
352  ///
353  /// \param If non-NULL, will be set to the module file name we expected to
354  /// find (regardless of whether it was actually found or not).
355  ///
356  /// \param UmbrellaHeader If non-NULL, and no module was found in the module
357  /// cache, this routine will search in the framework paths to determine
358  /// whether a module can be built from an umbrella header. If so, the pointee
359  /// will be set to the path of the umbrella header.
360  ///
361  /// \returns A file describing the named module, if available, or NULL to
362  /// indicate that the module could not be found.
363  const FileEntry *lookupModule(StringRef ModuleName,
364                                std::string *ModuleFileName = 0,
365                                std::string *UmbrellaHeader = 0);
366
367  void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
368
369  /// \brief Determine whether there is a module map that may map the header
370  /// with the given file name to a (sub)module.
371  ///
372  /// \param Filename The name of the file.
373  ///
374  /// \param Root The "root" directory, at which we should stop looking for
375  /// module maps.
376  bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root);
377
378  /// \brief Retrieve the module that corresponds to the given file, if any.
379  ModuleMap::Module *findModuleForHeader(const FileEntry *File);
380
381
382  /// \brief Read the contents of the given module map file.
383  ///
384  /// \param File The module map file.
385  ///
386  /// \param OnlyModule If non-NULL, this will receive the
387  ///
388  /// \returns true if an error occurred, false otherwise.
389  bool loadModuleMapFile(const FileEntry *File);
390
391  /// \brief Retrieve a module with the given name.
392  ///
393  /// \param Name The name of the module to retrieve.
394  ///
395  /// \param AllowSearch If true, we're allowed to look for module maps within
396  /// the header search path. Otherwise, the module must already be known.
397  ///
398  /// \returns The module, if found; otherwise, null.
399  ModuleMap::Module *getModule(StringRef Name, bool AllowSearch = true);
400
401  /// \brief Retrieve a module with the given name, which may be part of the
402  /// given framework.
403  ///
404  /// \param Name The name of the module to retrieve.
405  ///
406  /// \param Dir The framework directory (e.g., ModuleName.framework).
407  ///
408  /// \returns The module, if found; otherwise, null.
409  ModuleMap::Module *getFrameworkModule(StringRef Name,
410                                        const DirectoryEntry *Dir);
411
412  /// \brief Retrieve the module map.
413  ModuleMap &getModuleMap() { return ModMap; }
414
415  unsigned header_file_size() const { return FileInfo.size(); }
416
417  // Used by ASTReader.
418  void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID);
419
420  /// getFileInfo - Return the HeaderFileInfo structure for the specified
421  /// FileEntry.
422  const HeaderFileInfo &getFileInfo(const FileEntry *FE) const {
423    return const_cast<HeaderSearch*>(this)->getFileInfo(FE);
424  }
425
426  // Used by external tools
427  typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator;
428  search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); }
429  search_dir_iterator search_dir_end() const { return SearchDirs.end(); }
430  unsigned search_dir_size() const { return SearchDirs.size(); }
431
432  search_dir_iterator quoted_dir_begin() const {
433    return SearchDirs.begin();
434  }
435  search_dir_iterator quoted_dir_end() const {
436    return SearchDirs.begin() + AngledDirIdx;
437  }
438
439  search_dir_iterator angled_dir_begin() const {
440    return SearchDirs.begin() + AngledDirIdx;
441  }
442  search_dir_iterator angled_dir_end() const {
443    return SearchDirs.begin() + SystemDirIdx;
444  }
445
446  search_dir_iterator system_dir_begin() const {
447    return SearchDirs.begin() + SystemDirIdx;
448  }
449  search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
450
451  /// \brief Retrieve a uniqued framework name.
452  StringRef getUniqueFrameworkName(StringRef Framework);
453
454  void PrintStats();
455
456  size_t getTotalMemory() const;
457
458private:
459  /// \brief Describes what happened when we tried to load a module map file.
460  enum LoadModuleMapResult {
461    /// \brief The module map file had already been loaded.
462    LMM_AlreadyLoaded,
463    /// \brief The module map file was loaded by this invocation.
464    LMM_NewlyLoaded,
465    /// \brief There is was directory with the given name.
466    LMM_NoDirectory,
467    /// \brief There was either no module map file or the module map file was
468    /// invalid.
469    LMM_InvalidModuleMap
470  };
471
472  /// \brief Try to load the module map file in the given directory.
473  ///
474  /// \param DirName The name of the directory where we will look for a module
475  /// map file.
476  ///
477  /// \returns The result of attempting to load the module map file from the
478  /// named directory.
479  LoadModuleMapResult loadModuleMapFile(StringRef DirName);
480
481  /// \brief Try to load the module map file in the given directory.
482  ///
483  /// \param Dir The directory where we will look for a module map file.
484  ///
485  /// \returns The result of attempting to load the module map file from the
486  /// named directory.
487  LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir);
488
489  /// getFileInfo - Return the HeaderFileInfo structure for the specified
490  /// FileEntry.
491  HeaderFileInfo &getFileInfo(const FileEntry *FE);
492};
493
494}  // end namespace clang
495
496#endif
497