HeaderSearch.h revision cfbf1c7536e016dc275139dd842d4a5f059a749f
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 "llvm/ADT/StringMap.h"
19#include <vector>
20
21namespace clang {
22
23class ExternalIdentifierLookup;
24class FileEntry;
25class FileManager;
26class IdentifierInfo;
27
28/// HeaderFileInfo - The preprocessor keeps track of this information for each
29/// file that is #included.
30struct HeaderFileInfo {
31  /// isImport - True if this is a #import'd or #pragma once file.
32  unsigned isImport : 1;
33
34  /// DirInfo - Keep track of whether this is a system header, and if so,
35  /// whether it is C++ clean or not.  This can be set by the include paths or
36  /// by #pragma gcc system_header.  This is an instance of
37  /// SrcMgr::CharacteristicKind.
38  unsigned DirInfo : 2;
39
40  /// \brief Whether this header file info was supplied by an external source.
41  unsigned External : 1;
42
43  /// \brief Whether this structure is considered to already have been
44  /// "resolved", meaning that it was loaded from the external source.
45  unsigned Resolved : 1;
46
47  /// NumIncludes - This is the number of times the file has been included
48  /// already.
49  unsigned short NumIncludes;
50
51  /// \brief The ID number of the controlling macro.
52  ///
53  /// This ID number will be non-zero when there is a controlling
54  /// macro whose IdentifierInfo may not yet have been loaded from
55  /// external storage.
56  unsigned ControllingMacroID;
57
58  /// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard
59  /// that protects the entire contents of the file, this is the identifier
60  /// for the macro that controls whether or not it has any effect.
61  ///
62  /// Note: Most clients should use getControllingMacro() to access
63  /// the controlling macro of this header, since
64  /// getControllingMacro() is able to load a controlling macro from
65  /// external storage.
66  const IdentifierInfo *ControllingMacro;
67
68  HeaderFileInfo()
69    : isImport(false), DirInfo(SrcMgr::C_User), External(false),
70      Resolved(false), NumIncludes(0), ControllingMacroID(0),
71      ControllingMacro(0)  {}
72
73  /// \brief Retrieve the controlling macro for this header file, if
74  /// any.
75  const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External);
76
77  /// \brief Determine whether this is a non-default header file info, e.g.,
78  /// it corresponds to an actual header we've included or tried to include.
79  bool isNonDefault() const {
80    return isImport || NumIncludes || ControllingMacro || ControllingMacroID;
81  }
82};
83
84/// \brief An external source of header file information, which may supply
85/// information about header files already included.
86class ExternalHeaderFileInfoSource {
87public:
88  virtual ~ExternalHeaderFileInfoSource();
89
90  /// \brief Retrieve the header file information for the given file entry.
91  ///
92  /// \returns Header file information for the given file entry, with the
93  /// \c External bit set. If the file entry is not known, return a
94  /// default-constructed \c HeaderFileInfo.
95  virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0;
96};
97
98/// HeaderSearch - This class encapsulates the information needed to find the
99/// file referenced by a #include or #include_next, (sub-)framework lookup, etc.
100class HeaderSearch {
101  FileManager &FileMgr;
102  /// #include search path information.  Requests for #include "x" search the
103  /// directory of the #including file first, then each directory in SearchDirs
104  /// consequtively. Requests for <x> search the current dir first, then each
105  /// directory in SearchDirs, starting at SystemDirIdx, consequtively.  If
106  /// NoCurDirSearch is true, then the check for the file in the current
107  /// directory is supressed.
108  std::vector<DirectoryLookup> SearchDirs;
109  unsigned SystemDirIdx;
110  bool NoCurDirSearch;
111
112  /// FileInfo - This contains all of the preprocessor-specific data about files
113  /// that are included.  The vector is indexed by the FileEntry's UID.
114  ///
115  std::vector<HeaderFileInfo> FileInfo;
116
117  /// LookupFileCache - This is keeps track of each lookup performed by
118  /// LookupFile.  The first part of the value is the starting index in
119  /// SearchDirs that the cached search was performed from.  If there is a hit
120  /// and this value doesn't match the current query, the cache has to be
121  /// ignored.  The second value is the entry in SearchDirs that satisfied the
122  /// query.
123  llvm::StringMap<std::pair<unsigned, unsigned> > LookupFileCache;
124
125
126  /// FrameworkMap - This is a collection mapping a framework or subframework
127  /// name like "Carbon" to the Carbon.framework directory.
128  llvm::StringMap<const DirectoryEntry *> FrameworkMap;
129
130  /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing
131  /// headermaps.  This vector owns the headermap.
132  std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps;
133
134  /// \brief Entity used to resolve the identifier IDs of controlling
135  /// macros into IdentifierInfo pointers, as needed.
136  ExternalIdentifierLookup *ExternalLookup;
137
138  /// \brief Entity used to look up stored header file information.
139  ExternalHeaderFileInfoSource *ExternalSource;
140
141  // Various statistics we track for performance analysis.
142  unsigned NumIncluded;
143  unsigned NumMultiIncludeFileOptzn;
144  unsigned NumFrameworkLookups, NumSubFrameworkLookups;
145
146  // HeaderSearch doesn't support default or copy construction.
147  explicit HeaderSearch();
148  explicit HeaderSearch(const HeaderSearch&);
149  void operator=(const HeaderSearch&);
150public:
151  HeaderSearch(FileManager &FM);
152  ~HeaderSearch();
153
154  FileManager &getFileMgr() const { return FileMgr; }
155
156  /// SetSearchPaths - Interface for setting the file search paths.
157  ///
158  void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
159                      unsigned systemDirIdx, bool noCurDirSearch) {
160    SearchDirs = dirs;
161    SystemDirIdx = systemDirIdx;
162    NoCurDirSearch = noCurDirSearch;
163    //LookupFileCache.clear();
164  }
165
166  /// ClearFileInfo - Forget everything we know about headers so far.
167  void ClearFileInfo() {
168    FileInfo.clear();
169  }
170
171  void SetExternalLookup(ExternalIdentifierLookup *EIL) {
172    ExternalLookup = EIL;
173  }
174
175  ExternalIdentifierLookup *getExternalLookup() const {
176    return ExternalLookup;
177  }
178
179  /// \brief Set the external source of header information.
180  void SetExternalSource(ExternalHeaderFileInfoSource *ES) {
181    ExternalSource = ES;
182  }
183
184  /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
185  /// return null on failure.  isAngled indicates whether the file reference is
186  /// a <> reference.  If successful, this returns 'UsedDir', the
187  /// DirectoryLookup member the file was found in, or null if not applicable.
188  /// If CurDir is non-null, the file was found in the specified directory
189  /// search location.  This is used to implement #include_next.  CurFileEnt, if
190  /// non-null, indicates where the #including file is, in case a relative
191  /// search is needed.
192  const FileEntry *LookupFile(llvm::StringRef Filename, bool isAngled,
193                              const DirectoryLookup *FromDir,
194                              const DirectoryLookup *&CurDir,
195                              const FileEntry *CurFileEnt);
196
197  /// LookupSubframeworkHeader - Look up a subframework for the specified
198  /// #include file.  For example, if #include'ing <HIToolbox/HIToolbox.h> from
199  /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
200  /// is a subframework within Carbon.framework.  If so, return the FileEntry
201  /// for the designated file, otherwise return null.
202  const FileEntry *LookupSubframeworkHeader(llvm::StringRef Filename,
203                                            const FileEntry *RelativeFileEnt);
204
205  /// LookupFrameworkCache - Look up the specified framework name in our
206  /// framework cache, returning the DirectoryEntry it is in if we know,
207  /// otherwise, return null.
208  const DirectoryEntry *&LookupFrameworkCache(llvm::StringRef FWName) {
209    return FrameworkMap.GetOrCreateValue(FWName).getValue();
210  }
211
212  /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
213  /// #include, #include_next, or #import directive.  Return false if #including
214  /// the file will have no effect or true if we should include it.
215  bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
216
217
218  /// getFileDirFlavor - Return whether the specified file is a normal header,
219  /// a system header, or a C++ friendly system header.
220  SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) {
221    return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
222  }
223
224  /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g.
225  /// due to #pragma once.
226  void MarkFileIncludeOnce(const FileEntry *File) {
227    getFileInfo(File).isImport = true;
228  }
229
230  /// MarkFileSystemHeader - Mark the specified file as a system header, e.g.
231  /// due to #pragma GCC system_header.
232  void MarkFileSystemHeader(const FileEntry *File) {
233    getFileInfo(File).DirInfo = SrcMgr::C_System;
234  }
235
236  /// IncrementIncludeCount - Increment the count for the number of times the
237  /// specified FileEntry has been entered.
238  void IncrementIncludeCount(const FileEntry *File) {
239    ++getFileInfo(File).NumIncludes;
240  }
241
242  /// SetFileControllingMacro - Mark the specified file as having a controlling
243  /// macro.  This is used by the multiple-include optimization to eliminate
244  /// no-op #includes.
245  void SetFileControllingMacro(const FileEntry *File,
246                               const IdentifierInfo *ControllingMacro) {
247    getFileInfo(File).ControllingMacro = ControllingMacro;
248  }
249
250  /// CreateHeaderMap - This method returns a HeaderMap for the specified
251  /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
252  const HeaderMap *CreateHeaderMap(const FileEntry *FE);
253
254  void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
255
256  typedef std::vector<HeaderFileInfo>::const_iterator header_file_iterator;
257  header_file_iterator header_file_begin() const { return FileInfo.begin(); }
258  header_file_iterator header_file_end() const { return FileInfo.end(); }
259  unsigned header_file_size() const { return FileInfo.size(); }
260
261  // Used by ASTReader.
262  void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID);
263
264  void PrintStats();
265private:
266
267  /// getFileInfo - Return the HeaderFileInfo structure for the specified
268  /// FileEntry.
269  HeaderFileInfo &getFileInfo(const FileEntry *FE);
270};
271
272}  // end namespace clang
273
274#endif
275