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