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