HeaderSearch.h revision 701e3366828adf740734ae5bf4424ecac059bc9a
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  /// FileInfo - This contains all of the preprocessor-specific data about files
139  /// that are included.  The vector is indexed by the FileEntry's UID.
140  ///
141  std::vector<HeaderFileInfo> FileInfo;
142
143  /// LookupFileCache - This is keeps track of each lookup performed by
144  /// LookupFile.  The first part of the value is the starting index in
145  /// SearchDirs that the cached search was performed from.  If there is a hit
146  /// and this value doesn't match the current query, the cache has to be
147  /// ignored.  The second value is the entry in SearchDirs that satisfied the
148  /// query.
149  llvm::StringMap<std::pair<unsigned, unsigned>, llvm::BumpPtrAllocator>
150    LookupFileCache;
151
152  /// FrameworkMap - This is a collection mapping a framework or subframework
153  /// name like "Carbon" to the Carbon.framework directory.
154  llvm::StringMap<const DirectoryEntry *, llvm::BumpPtrAllocator>
155    FrameworkMap;
156
157  llvm::StringMap<std::pair<StringRef, bool>, llvm::BumpPtrAllocator>
158    IncludeAliasMap;
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&);
190
191  friend class DirectoryLookup;
192
193public:
194  HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags,
195               const LangOptions &LangOpts, const TargetInfo *Target);
196  ~HeaderSearch();
197
198  FileManager &getFileMgr() const { return FileMgr; }
199
200  /// SetSearchPaths - Interface for setting the file search paths.
201  ///
202  void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
203                      unsigned angledDirIdx, unsigned systemDirIdx,
204                      bool noCurDirSearch) {
205    assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
206        "Directory indicies are unordered");
207    SearchDirs = dirs;
208    AngledDirIdx = angledDirIdx;
209    SystemDirIdx = systemDirIdx;
210    NoCurDirSearch = noCurDirSearch;
211    //LookupFileCache.clear();
212  }
213
214  /// AddSearchPath - Add an additional search path.
215  void AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
216    unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
217    SearchDirs.insert(SearchDirs.begin() + idx, dir);
218    if (!isAngled)
219      AngledDirIdx++;
220    SystemDirIdx++;
221  }
222
223  /// AddHeaderMapping -- Map the source include name to the dest include name
224  void AddHeaderMapping(const StringRef& Source, const StringRef& Dest,
225                        bool IsAngled) {
226    IncludeAliasMap[Source] = std::make_pair(Dest, IsAngled);
227  }
228
229  StringRef MapHeader(const StringRef& Source, bool isAngled) {
230    // Do any filename replacements before anything else
231    llvm::StringMap<std::pair<StringRef,bool> >::const_iterator iter =
232      IncludeAliasMap.find(Source);
233    if (iter != IncludeAliasMap.end()) {
234      // If the angling matches, then we've found a replacement
235      if (iter->second.second == isAngled) {
236        return iter->second.first;
237      }
238    }
239    return Source;
240  }
241
242  /// \brief Set the path to the module cache.
243  void setModuleCachePath(StringRef CachePath) {
244    ModuleCachePath = CachePath;
245  }
246
247  /// \brief Retrieve the path to the module cache.
248  StringRef getModuleCachePath() const { return ModuleCachePath; }
249
250  /// ClearFileInfo - Forget everything we know about headers so far.
251  void ClearFileInfo() {
252    FileInfo.clear();
253  }
254
255  void SetExternalLookup(ExternalIdentifierLookup *EIL) {
256    ExternalLookup = EIL;
257  }
258
259  ExternalIdentifierLookup *getExternalLookup() const {
260    return ExternalLookup;
261  }
262
263  /// \brief Set the external source of header information.
264  void SetExternalSource(ExternalHeaderFileInfoSource *ES) {
265    ExternalSource = ES;
266  }
267
268  /// \brief Set the target information for the header search, if not
269  /// already known.
270  void setTarget(const TargetInfo &Target);
271
272  /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
273  /// return null on failure.
274  ///
275  /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member
276  /// the file was found in, or null if not applicable.
277  ///
278  /// \param isAngled indicates whether the file reference is a <> reference.
279  ///
280  /// \param CurDir If non-null, the file was found in the specified directory
281  /// search location.  This is used to implement #include_next.
282  ///
283  /// \param CurFileEnt If non-null, indicates where the #including file is, in
284  /// case a relative search is needed.
285  ///
286  /// \param SearchPath If non-null, will be set to the search path relative
287  /// to which the file was found. If the include path is absolute, SearchPath
288  /// will be set to an empty string.
289  ///
290  /// \param RelativePath If non-null, will be set to the path relative to
291  /// SearchPath at which the file was found. This only differs from the
292  /// Filename for framework includes.
293  ///
294  /// \param SuggestedModule If non-null, and the file found is semantically
295  /// part of a known module, this will be set to the module that should
296  /// be imported instead of preprocessing/parsing the file found.
297  const FileEntry *LookupFile(StringRef Filename, bool isAngled,
298                              const DirectoryLookup *FromDir,
299                              const DirectoryLookup *&CurDir,
300                              const FileEntry *CurFileEnt,
301                              SmallVectorImpl<char> *SearchPath,
302                              SmallVectorImpl<char> *RelativePath,
303                              Module **SuggestedModule,
304                              bool SkipCache = false);
305
306  /// LookupSubframeworkHeader - Look up a subframework for the specified
307  /// #include file.  For example, if #include'ing <HIToolbox/HIToolbox.h> from
308  /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
309  /// is a subframework within Carbon.framework.  If so, return the FileEntry
310  /// for the designated file, otherwise return null.
311  const FileEntry *LookupSubframeworkHeader(
312      StringRef Filename,
313      const FileEntry *RelativeFileEnt,
314      SmallVectorImpl<char> *SearchPath,
315      SmallVectorImpl<char> *RelativePath);
316
317  /// LookupFrameworkCache - Look up the specified framework name in our
318  /// framework cache, returning the DirectoryEntry it is in if we know,
319  /// otherwise, return null.
320  const DirectoryEntry *&LookupFrameworkCache(StringRef FWName) {
321    return FrameworkMap.GetOrCreateValue(FWName).getValue();
322  }
323
324  /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
325  /// #include, #include_next, or #import directive.  Return false if #including
326  /// the file will have no effect or true if we should include it.
327  bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
328
329
330  /// getFileDirFlavor - Return whether the specified file is a normal header,
331  /// a system header, or a C++ friendly system header.
332  SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) {
333    return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
334  }
335
336  /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g.
337  /// due to #pragma once.
338  void MarkFileIncludeOnce(const FileEntry *File) {
339    HeaderFileInfo &FI = getFileInfo(File);
340    FI.isImport = true;
341    FI.isPragmaOnce = true;
342  }
343
344  /// MarkFileSystemHeader - Mark the specified file as a system header, e.g.
345  /// due to #pragma GCC system_header.
346  void MarkFileSystemHeader(const FileEntry *File) {
347    getFileInfo(File).DirInfo = SrcMgr::C_System;
348  }
349
350  /// IncrementIncludeCount - Increment the count for the number of times the
351  /// specified FileEntry has been entered.
352  void IncrementIncludeCount(const FileEntry *File) {
353    ++getFileInfo(File).NumIncludes;
354  }
355
356  /// SetFileControllingMacro - Mark the specified file as having a controlling
357  /// macro.  This is used by the multiple-include optimization to eliminate
358  /// no-op #includes.
359  void SetFileControllingMacro(const FileEntry *File,
360                               const IdentifierInfo *ControllingMacro) {
361    getFileInfo(File).ControllingMacro = ControllingMacro;
362  }
363
364  /// \brief Determine whether this file is intended to be safe from
365  /// multiple inclusions, e.g., it has #pragma once or a controlling
366  /// macro.
367  ///
368  /// This routine does not consider the effect of #import
369  bool isFileMultipleIncludeGuarded(const FileEntry *File);
370
371  /// CreateHeaderMap - This method returns a HeaderMap for the specified
372  /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
373  const HeaderMap *CreateHeaderMap(const FileEntry *FE);
374
375  /// \brief Retrieve the name of the module file that should be used to
376  /// load the given module.
377  ///
378  /// \param Module The module whose module file name will be returned.
379  ///
380  /// \returns The name of the module file that corresponds to this module,
381  /// or an empty string if this module does not correspond to any module file.
382  std::string getModuleFileName(Module *Module);
383
384  /// \brief Retrieve the name of the module file that should be used to
385  /// load a module with the given name.
386  ///
387  /// \param Module The module whose module file name will be returned.
388  ///
389  /// \returns The name of the module file that corresponds to this module,
390  /// or an empty string if this module does not correspond to any module file.
391  std::string getModuleFileName(StringRef ModuleName);
392
393  /// \brief Lookup a module Search for a module with the given name.
394  ///
395  /// \param ModuleName The name of the module we're looking for.
396  ///
397  /// \param AllowSearch Whether we are allowed to search in the various
398  /// search directories to produce a module definition. If not, this lookup
399  /// will only return an already-known module.
400  ///
401  /// \returns The module with the given name.
402  Module *lookupModule(StringRef ModuleName, bool AllowSearch = true);
403
404  void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
405
406  /// \brief Determine whether there is a module map that may map the header
407  /// with the given file name to a (sub)module.
408  ///
409  /// \param Filename The name of the file.
410  ///
411  /// \param Root The "root" directory, at which we should stop looking for
412  /// module maps.
413  bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root);
414
415  /// \brief Retrieve the module that corresponds to the given file, if any.
416  ///
417  /// \param File The header that we wish to map to a module.
418  Module *findModuleForHeader(const FileEntry *File);
419
420  /// \brief Read the contents of the given module map file.
421  ///
422  /// \param File The module map file.
423  ///
424  /// \param OnlyModule If non-NULL, this will receive the
425  ///
426  /// \returns true if an error occurred, false otherwise.
427  bool loadModuleMapFile(const FileEntry *File);
428
429  /// \brief Collect the set of all known, top-level modules.
430  ///
431  /// \param Modules Will be filled with the set of known, top-level modules.
432  void collectAllModules(llvm::SmallVectorImpl<Module *> &Modules);
433
434private:
435  /// \brief Retrieve a module with the given name, which may be part of the
436  /// given framework.
437  ///
438  /// \param Name The name of the module to retrieve.
439  ///
440  /// \param Dir The framework directory (e.g., ModuleName.framework).
441  ///
442  /// \param IsSystem Whether the framework directory is part of the system
443  /// frameworks.
444  ///
445  /// \returns The module, if found; otherwise, null.
446  Module *loadFrameworkModule(StringRef Name,
447                              const DirectoryEntry *Dir,
448                              bool IsSystem);
449
450public:
451  /// \brief Retrieve the module map.
452  ModuleMap &getModuleMap() { return ModMap; }
453
454  unsigned header_file_size() const { return FileInfo.size(); }
455
456  // Used by ASTReader.
457  void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID);
458
459  /// getFileInfo - Return the HeaderFileInfo structure for the specified
460  /// FileEntry.
461  const HeaderFileInfo &getFileInfo(const FileEntry *FE) const {
462    return const_cast<HeaderSearch*>(this)->getFileInfo(FE);
463  }
464
465  // Used by external tools
466  typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator;
467  search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); }
468  search_dir_iterator search_dir_end() const { return SearchDirs.end(); }
469  unsigned search_dir_size() const { return SearchDirs.size(); }
470
471  search_dir_iterator quoted_dir_begin() const {
472    return SearchDirs.begin();
473  }
474  search_dir_iterator quoted_dir_end() const {
475    return SearchDirs.begin() + AngledDirIdx;
476  }
477
478  search_dir_iterator angled_dir_begin() const {
479    return SearchDirs.begin() + AngledDirIdx;
480  }
481  search_dir_iterator angled_dir_end() const {
482    return SearchDirs.begin() + SystemDirIdx;
483  }
484
485  search_dir_iterator system_dir_begin() const {
486    return SearchDirs.begin() + SystemDirIdx;
487  }
488  search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
489
490  /// \brief Retrieve a uniqued framework name.
491  StringRef getUniqueFrameworkName(StringRef Framework);
492
493  void PrintStats();
494
495  size_t getTotalMemory() const;
496
497  static std::string NormalizeDashIncludePath(StringRef File,
498                                              FileManager &FileMgr);
499
500private:
501  /// \brief Describes what happened when we tried to load a module map file.
502  enum LoadModuleMapResult {
503    /// \brief The module map file had already been loaded.
504    LMM_AlreadyLoaded,
505    /// \brief The module map file was loaded by this invocation.
506    LMM_NewlyLoaded,
507    /// \brief There is was directory with the given name.
508    LMM_NoDirectory,
509    /// \brief There was either no module map file or the module map file was
510    /// invalid.
511    LMM_InvalidModuleMap
512  };
513
514  /// \brief Try to load the module map file in the given directory.
515  ///
516  /// \param DirName The name of the directory where we will look for a module
517  /// map file.
518  ///
519  /// \returns The result of attempting to load the module map file from the
520  /// named directory.
521  LoadModuleMapResult loadModuleMapFile(StringRef DirName);
522
523  /// \brief Try to load the module map file in the given directory.
524  ///
525  /// \param Dir The directory where we will look for a module map file.
526  ///
527  /// \returns The result of attempting to load the module map file from the
528  /// named directory.
529  LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir);
530
531  /// getFileInfo - Return the HeaderFileInfo structure for the specified
532  /// FileEntry.
533  HeaderFileInfo &getFileInfo(const FileEntry *FE);
534};
535
536}  // end namespace clang
537
538#endif
539