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