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