HeaderSearch.h revision 5f016e2cb5d11daeb237544de1c5d59f20fe1a6e
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  /// FrameworkMap - This is a collection mapping a framework or subframework
72  /// name like "Carbon" to the Carbon.framework directory.
73  llvm::StringMap<const DirectoryEntry *> FrameworkMap;
74
75  // Various statistics we track for performance analysis.
76  unsigned NumIncluded;
77  unsigned NumMultiIncludeFileOptzn;
78  unsigned NumFrameworkLookups, NumSubFrameworkLookups;
79public:
80  HeaderSearch(FileManager &FM);
81
82  FileManager &getFileMgr() const { return FileMgr; }
83
84  /// SetSearchPaths - Interface for setting the file search paths.
85  ///
86  void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
87                      unsigned systemDirIdx, bool noCurDirSearch) {
88    SearchDirs = dirs;
89    SystemDirIdx = systemDirIdx;
90    NoCurDirSearch = noCurDirSearch;
91  }
92
93  /// ClearFileInfo - Forget everything we know about headers so far.
94  void ClearFileInfo() {
95    FileInfo.clear();
96  }
97
98  /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
99  /// return null on failure.  isAngled indicates whether the file reference is
100  /// a <> reference.  If successful, this returns 'UsedDir', the
101  /// DirectoryLookup member the file was found in, or null if not applicable.
102  /// If CurDir is non-null, the file was found in the specified directory
103  /// search location.  This is used to implement #include_next.  CurFileEnt, if
104  /// non-null, indicates where the #including file is, in case a relative
105  /// search is needed.
106  const FileEntry *LookupFile(const char *FilenameStart,
107                              const char *FilenameEnd, bool isAngled,
108                              const DirectoryLookup *FromDir,
109                              const DirectoryLookup *&CurDir,
110                              const FileEntry *CurFileEnt);
111
112  /// LookupSubframeworkHeader - Look up a subframework for the specified
113  /// #include file.  For example, if #include'ing <HIToolbox/HIToolbox.h> from
114  /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
115  /// is a subframework within Carbon.framework.  If so, return the FileEntry
116  /// for the designated file, otherwise return null.
117  const FileEntry *LookupSubframeworkHeader(const char *FilenameStart,
118                                            const char *FilenameEnd,
119                                            const FileEntry *RelativeFileEnt);
120
121  /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
122  /// #include, #include_next, or #import directive.  Return false if #including
123  /// the file will have no effect or true if we should include it.
124  bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
125
126
127  /// getFileDirFlavor - Return whether the specified file is a normal header,
128  /// a system header, or a C++ friendly system header.
129  DirectoryLookup::DirType getFileDirFlavor(const FileEntry *File) {
130    return getFileInfo(File).DirInfo;
131  }
132
133  /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g.
134  /// due to #pragma once.
135  void MarkFileIncludeOnce(const FileEntry *File) {
136    getFileInfo(File).isImport = true;
137  }
138
139  /// MarkFileSystemHeader - Mark the specified fiel as a system header, e.g.
140  /// due to #pragma GCC system_header.
141  void MarkFileSystemHeader(const FileEntry *File) {
142    getFileInfo(File).DirInfo = DirectoryLookup::SystemHeaderDir;
143  }
144
145  /// SetFileControllingMacro - Mark the specified file as having a controlling
146  /// macro.  This is used by the multiple-include optimization to eliminate
147  /// no-op #includes.
148  void SetFileControllingMacro(const FileEntry *File,
149                               const IdentifierInfo *ControllingMacro) {
150    getFileInfo(File).ControllingMacro = ControllingMacro;
151  }
152
153  void PrintStats();
154private:
155  const FileEntry *DoFrameworkLookup(const DirectoryEntry *Dir,
156                                     const char *FilenameStart,
157                                     const char *FilenameEnd);
158
159  /// getFileInfo - Return the PerFileInfo structure for the specified
160  /// FileEntry.
161  PerFileInfo &getFileInfo(const FileEntry *FE);
162};
163
164}  // end namespace clang
165
166#endif
167