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