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