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