HeaderSearch.h revision 27e2eb40b9adc876c05e0260d1100385c9f0e5f5
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 {
22class FileEntry;
23class FileManager;
24class IdentifierInfo;
25
26
27/// HeaderSearch - This class encapsulates the information needed to find the
28/// file referenced by a #include or #include_next, (sub-)framework lookup, etc.
29class HeaderSearch {
30  FileManager &FileMgr;
31
32  /// #include search path information.  Requests for #include "x" search the
33  /// directory of the #including file first, then each directory in SearchDirs
34  /// consequtively. Requests for <x> search the current dir first, then each
35  /// directory in SearchDirs, starting at SystemDirIdx, consequtively.  If
36  /// NoCurDirSearch is true, then the check for the file in the current
37  /// directory is supressed.
38  std::vector<DirectoryLookup> SearchDirs;
39  unsigned SystemDirIdx;
40  bool NoCurDirSearch;
41
42  /// PreFileInfo - The preprocessor keeps track of this information for each
43  /// file that is #included.
44  struct PerFileInfo {
45    /// isImport - True if this is a #import'd or #pragma once file.
46    bool isImport : 1;
47
48    /// DirInfo - Keep track of whether this is a system header, and if so,
49    /// whether it is C++ clean or not.  This can be set by the include paths or
50    /// by #pragma gcc system_header.
51    DirectoryLookup::DirType DirInfo : 2;
52
53    /// NumIncludes - This is the number of times the file has been included
54    /// already.
55    unsigned short NumIncludes;
56
57    /// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard
58    /// that protects the entire contents of the file, this is the identifier
59    /// for the macro that controls whether or not it has any effect.
60    const IdentifierInfo *ControllingMacro;
61
62    PerFileInfo() : isImport(false), DirInfo(DirectoryLookup::NormalHeaderDir),
63      NumIncludes(0), ControllingMacro(0) {}
64  };
65
66  /// FileInfo - This contains all of the preprocessor-specific data about files
67  /// that are included.  The vector is indexed by the FileEntry's UID.
68  ///
69  std::vector<PerFileInfo> FileInfo;
70
71  /// LookupFileCache - This is keeps track of each lookup performed by
72  /// LookupFile.  The first part of the value is the starting index in
73  /// SearchDirs that the cached search was performed from.  If there is a hit
74  /// and this value doesn't match the current query, the cache has to be
75  /// ignored.  The second value is the entry in SearchDirs that satisfied the
76  /// query.
77  llvm::StringMap<std::pair<unsigned, unsigned> > LookupFileCache;
78
79
80  /// FrameworkMap - This is a collection mapping a framework or subframework
81  /// name like "Carbon" to the Carbon.framework directory.
82  llvm::StringMap<const DirectoryEntry *> FrameworkMap;
83
84  /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing
85  /// headermaps.  This vector owns the headermap.
86  std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps;
87
88  // Various statistics we track for performance analysis.
89  unsigned NumIncluded;
90  unsigned NumMultiIncludeFileOptzn;
91  unsigned NumFrameworkLookups, NumSubFrameworkLookups;
92public:
93  HeaderSearch(FileManager &FM);
94  ~HeaderSearch();
95
96  // HeaderSearch doesn't support copy construction.
97  explicit HeaderSearch(const HeaderSearch&);
98
99  FileManager &getFileMgr() const { return FileMgr; }
100
101  /// SetSearchPaths - Interface for setting the file search paths.
102  ///
103  void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
104                      unsigned systemDirIdx, bool noCurDirSearch) {
105    SearchDirs = dirs;
106    SystemDirIdx = systemDirIdx;
107    NoCurDirSearch = noCurDirSearch;
108    //LookupFileCache.clear();
109  }
110
111  /// ClearFileInfo - Forget everything we know about headers so far.
112  void ClearFileInfo() {
113    FileInfo.clear();
114  }
115
116  /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
117  /// return null on failure.  isAngled indicates whether the file reference is
118  /// a <> reference.  If successful, this returns 'UsedDir', the
119  /// DirectoryLookup member the file was found in, or null if not applicable.
120  /// If CurDir is non-null, the file was found in the specified directory
121  /// search location.  This is used to implement #include_next.  CurFileEnt, if
122  /// non-null, indicates where the #including file is, in case a relative
123  /// search is needed.
124  const FileEntry *LookupFile(const char *FilenameStart,
125                              const char *FilenameEnd, bool isAngled,
126                              const DirectoryLookup *FromDir,
127                              const DirectoryLookup *&CurDir,
128                              const FileEntry *CurFileEnt);
129
130  /// LookupSubframeworkHeader - Look up a subframework for the specified
131  /// #include file.  For example, if #include'ing <HIToolbox/HIToolbox.h> from
132  /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
133  /// is a subframework within Carbon.framework.  If so, return the FileEntry
134  /// for the designated file, otherwise return null.
135  const FileEntry *LookupSubframeworkHeader(const char *FilenameStart,
136                                            const char *FilenameEnd,
137                                            const FileEntry *RelativeFileEnt);
138
139  /// LookupFrameworkCache - Look up the specified framework name in our
140  /// framework cache, returning the DirectoryEntry it is in if we know,
141  /// otherwise, return null.
142  const DirectoryEntry *&LookupFrameworkCache(const char *FWNameStart,
143                                              const char *FWNameEnd) {
144    return FrameworkMap.GetOrCreateValue(FWNameStart, FWNameEnd).getValue();
145  }
146
147  /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
148  /// #include, #include_next, or #import directive.  Return false if #including
149  /// the file will have no effect or true if we should include it.
150  bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
151
152
153  /// getFileDirFlavor - Return whether the specified file is a normal header,
154  /// a system header, or a C++ friendly system header.
155  DirectoryLookup::DirType getFileDirFlavor(const FileEntry *File) {
156    return getFileInfo(File).DirInfo;
157  }
158
159  /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g.
160  /// due to #pragma once.
161  void MarkFileIncludeOnce(const FileEntry *File) {
162    getFileInfo(File).isImport = true;
163  }
164
165  /// MarkFileSystemHeader - Mark the specified fiel as a system header, e.g.
166  /// due to #pragma GCC system_header.
167  void MarkFileSystemHeader(const FileEntry *File) {
168    getFileInfo(File).DirInfo = DirectoryLookup::SystemHeaderDir;
169  }
170
171  /// IncrementIncludeCount - Increment the count for the number of times the
172  /// specified FileEntry has been entered.
173  void IncrementIncludeCount(const FileEntry *File) {
174    ++getFileInfo(File).NumIncludes;
175  }
176
177  /// SetFileControllingMacro - Mark the specified file as having a controlling
178  /// macro.  This is used by the multiple-include optimization to eliminate
179  /// no-op #includes.
180  void SetFileControllingMacro(const FileEntry *File,
181                               const IdentifierInfo *ControllingMacro) {
182    getFileInfo(File).ControllingMacro = ControllingMacro;
183  }
184
185  /// CreateHeaderMap - This method returns a HeaderMap for the specified
186  /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
187  const HeaderMap *CreateHeaderMap(const FileEntry *FE);
188
189  void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
190
191  void PrintStats();
192private:
193
194  /// getFileInfo - Return the PerFileInfo structure for the specified
195  /// FileEntry.
196  PerFileInfo &getFileInfo(const FileEntry *FE);
197};
198
199}  // end namespace clang
200
201#endif
202