HeaderSearch.h revision 7412494982c8b50c90961302c3a718633b2c3ab7
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 suppressed.
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.
186  ///
187  /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member
188  /// the file was found in, or null if not applicable.
189  ///
190  /// \param isAngled indicates whether the file reference is a <> reference.
191  ///
192  /// \param CurDir If non-null, the file was found in the specified directory
193  /// search location.  This is used to implement #include_next.
194  ///
195  /// \param CurFileEnt If non-null, indicates where the #including file is, in
196  /// case a relative search is needed.
197  ///
198  /// \param SearchPath If non-null, will be set to the search path relative
199  /// to which the file was found. If the include path is absolute, SearchPath
200  /// will be set to an empty string.
201  ///
202  /// \param RelativePath If non-null, will be set to the path relative to
203  /// SearchPath at which the file was found. This only differs from the
204  /// Filename for framework includes.
205  const FileEntry *LookupFile(llvm::StringRef Filename, bool isAngled,
206                              const DirectoryLookup *FromDir,
207                              const DirectoryLookup *&CurDir,
208                              const FileEntry *CurFileEnt,
209                              llvm::SmallVectorImpl<char> *SearchPath,
210                              llvm::SmallVectorImpl<char> *RelativePath);
211
212  /// LookupSubframeworkHeader - Look up a subframework for the specified
213  /// #include file.  For example, if #include'ing <HIToolbox/HIToolbox.h> from
214  /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
215  /// is a subframework within Carbon.framework.  If so, return the FileEntry
216  /// for the designated file, otherwise return null.
217  const FileEntry *LookupSubframeworkHeader(
218      llvm::StringRef Filename,
219      const FileEntry *RelativeFileEnt,
220      llvm::SmallVectorImpl<char> *SearchPath,
221      llvm::SmallVectorImpl<char> *RelativePath);
222
223  /// LookupFrameworkCache - Look up the specified framework name in our
224  /// framework cache, returning the DirectoryEntry it is in if we know,
225  /// otherwise, return null.
226  const DirectoryEntry *&LookupFrameworkCache(llvm::StringRef FWName) {
227    return FrameworkMap.GetOrCreateValue(FWName).getValue();
228  }
229
230  /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
231  /// #include, #include_next, or #import directive.  Return false if #including
232  /// the file will have no effect or true if we should include it.
233  bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
234
235
236  /// getFileDirFlavor - Return whether the specified file is a normal header,
237  /// a system header, or a C++ friendly system header.
238  SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) {
239    return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
240  }
241
242  /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g.
243  /// due to #pragma once.
244  void MarkFileIncludeOnce(const FileEntry *File) {
245    getFileInfo(File).isImport = true;
246  }
247
248  /// MarkFileSystemHeader - Mark the specified file as a system header, e.g.
249  /// due to #pragma GCC system_header.
250  void MarkFileSystemHeader(const FileEntry *File) {
251    getFileInfo(File).DirInfo = SrcMgr::C_System;
252  }
253
254  /// IncrementIncludeCount - Increment the count for the number of times the
255  /// specified FileEntry has been entered.
256  void IncrementIncludeCount(const FileEntry *File) {
257    ++getFileInfo(File).NumIncludes;
258  }
259
260  /// SetFileControllingMacro - Mark the specified file as having a controlling
261  /// macro.  This is used by the multiple-include optimization to eliminate
262  /// no-op #includes.
263  void SetFileControllingMacro(const FileEntry *File,
264                               const IdentifierInfo *ControllingMacro) {
265    getFileInfo(File).ControllingMacro = ControllingMacro;
266  }
267
268  /// CreateHeaderMap - This method returns a HeaderMap for the specified
269  /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
270  const HeaderMap *CreateHeaderMap(const FileEntry *FE);
271
272  void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
273
274  typedef std::vector<HeaderFileInfo>::const_iterator header_file_iterator;
275  header_file_iterator header_file_begin() const { return FileInfo.begin(); }
276  header_file_iterator header_file_end() const { return FileInfo.end(); }
277  unsigned header_file_size() const { return FileInfo.size(); }
278
279  // Used by ASTReader.
280  void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID);
281
282  // Used by external tools
283  typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator;
284  search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); }
285  search_dir_iterator search_dir_end() const { return SearchDirs.end(); }
286  unsigned search_dir_size() const { return SearchDirs.size(); }
287
288  search_dir_iterator system_dir_begin() const {
289    return SearchDirs.begin() + SystemDirIdx;
290  }
291  search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
292
293  void PrintStats();
294private:
295
296  /// getFileInfo - Return the HeaderFileInfo structure for the specified
297  /// FileEntry.
298  HeaderFileInfo &getFileInfo(const FileEntry *FE);
299};
300
301}  // end namespace clang
302
303#endif
304