HeaderSearch.h revision 822da61b74ce14e89b3fa8774db18c833aa5748b
1//===--- HeaderSearch.h - Resolve Header File Locations ---------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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  FileManager &getFileMgr() const { return FileMgr; }
97
98  /// SetSearchPaths - Interface for setting the file search paths.
99  ///
100  void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
101                      unsigned systemDirIdx, bool noCurDirSearch) {
102    SearchDirs = dirs;
103    SystemDirIdx = systemDirIdx;
104    NoCurDirSearch = noCurDirSearch;
105    //LookupFileCache.clear();
106  }
107
108  /// ClearFileInfo - Forget everything we know about headers so far.
109  void ClearFileInfo() {
110    FileInfo.clear();
111  }
112
113  /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
114  /// return null on failure.  isAngled indicates whether the file reference is
115  /// a <> reference.  If successful, this returns 'UsedDir', the
116  /// DirectoryLookup member the file was found in, or null if not applicable.
117  /// If CurDir is non-null, the file was found in the specified directory
118  /// search location.  This is used to implement #include_next.  CurFileEnt, if
119  /// non-null, indicates where the #including file is, in case a relative
120  /// search is needed.
121  const FileEntry *LookupFile(const char *FilenameStart,
122                              const char *FilenameEnd, bool isAngled,
123                              const DirectoryLookup *FromDir,
124                              const DirectoryLookup *&CurDir,
125                              const FileEntry *CurFileEnt);
126
127  /// LookupSubframeworkHeader - Look up a subframework for the specified
128  /// #include file.  For example, if #include'ing <HIToolbox/HIToolbox.h> from
129  /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
130  /// is a subframework within Carbon.framework.  If so, return the FileEntry
131  /// for the designated file, otherwise return null.
132  const FileEntry *LookupSubframeworkHeader(const char *FilenameStart,
133                                            const char *FilenameEnd,
134                                            const FileEntry *RelativeFileEnt);
135
136  /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
137  /// #include, #include_next, or #import directive.  Return false if #including
138  /// the file will have no effect or true if we should include it.
139  bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
140
141
142  /// getFileDirFlavor - Return whether the specified file is a normal header,
143  /// a system header, or a C++ friendly system header.
144  DirectoryLookup::DirType getFileDirFlavor(const FileEntry *File) {
145    return getFileInfo(File).DirInfo;
146  }
147
148  /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g.
149  /// due to #pragma once.
150  void MarkFileIncludeOnce(const FileEntry *File) {
151    getFileInfo(File).isImport = true;
152  }
153
154  /// MarkFileSystemHeader - Mark the specified fiel as a system header, e.g.
155  /// due to #pragma GCC system_header.
156  void MarkFileSystemHeader(const FileEntry *File) {
157    getFileInfo(File).DirInfo = DirectoryLookup::SystemHeaderDir;
158  }
159
160  /// IncrementIncludeCount - Increment the count for the number of times the
161  /// specified FileEntry has been entered.
162  void IncrementIncludeCount(const FileEntry *File) {
163    ++getFileInfo(File).NumIncludes;
164  }
165
166  /// SetFileControllingMacro - Mark the specified file as having a controlling
167  /// macro.  This is used by the multiple-include optimization to eliminate
168  /// no-op #includes.
169  void SetFileControllingMacro(const FileEntry *File,
170                               const IdentifierInfo *ControllingMacro) {
171    getFileInfo(File).ControllingMacro = ControllingMacro;
172  }
173
174  /// CreateHeaderMap - This method returns a HeaderMap for the specified
175  /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
176  const HeaderMap *CreateHeaderMap(const FileEntry *FE, std::string &ErrorInfo);
177
178  void PrintStats();
179private:
180  const FileEntry *DoFrameworkLookup(const DirectoryEntry *Dir,
181                                     const char *FilenameStart,
182                                     const char *FilenameEnd);
183
184  /// getFileInfo - Return the PerFileInfo structure for the specified
185  /// FileEntry.
186  PerFileInfo &getFileInfo(const FileEntry *FE);
187};
188
189}  // end namespace clang
190
191#endif
192