1 //===--- HeaderSearch.h - Resolve Header File Locations ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the HeaderSearch interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LEX_HEADERSEARCH_H
15 #define LLVM_CLANG_LEX_HEADERSEARCH_H
17 #include "clang/Lex/DirectoryLookup.h"
18 #include "llvm/ADT/StringMap.h"
23 class ExternalIdentifierLookup;
28 /// HeaderFileInfo - The preprocessor keeps track of this information for each
29 /// file that is #included.
30 struct HeaderFileInfo {
31 /// isImport - True if this is a #import'd or #pragma once file.
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.
40 /// NumIncludes - This is the number of times the file has been included
42 unsigned short NumIncludes;
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.
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
52 const IdentifierInfo *ControllingMacro;
54 /// \brief The ID number of the controlling macro.
56 /// This ID number will be non-zero when there is a controlling
57 /// macro whose IdentifierInfo may not yet have been loaded from
59 unsigned ControllingMacroID;
62 : isImport(false), DirInfo(SrcMgr::C_User),
63 NumIncludes(0), ControllingMacro(0), ControllingMacroID(0) {}
65 /// \brief Retrieve the controlling macro for this header file, if
67 const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External);
70 /// HeaderSearch - This class encapsulates the information needed to find the
71 /// file referenced by a #include or #include_next, (sub-)framework lookup, etc.
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;
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.
88 std::vector<HeaderFileInfo> FileInfo;
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
96 llvm::StringMap<std::pair<unsigned, unsigned> > LookupFileCache;
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;
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;
107 /// \brief Entity used to resolve the identifier IDs of controlling
108 /// macros into IdentifierInfo pointers, as needed.
109 ExternalIdentifierLookup *ExternalLookup;
111 // Various statistics we track for performance analysis.
112 unsigned NumIncluded;
113 unsigned NumMultiIncludeFileOptzn;
114 unsigned NumFrameworkLookups, NumSubFrameworkLookups;
116 // HeaderSearch doesn't support default or copy construction.
117 explicit HeaderSearch();
118 explicit HeaderSearch(const HeaderSearch&);
119 void operator=(const HeaderSearch&);
121 HeaderSearch(FileManager &FM);
124 FileManager &getFileMgr() const { return FileMgr; }
126 /// SetSearchPaths - Interface for setting the file search paths.
128 void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
129 unsigned systemDirIdx, bool noCurDirSearch) {
131 SystemDirIdx = systemDirIdx;
132 NoCurDirSearch = noCurDirSearch;
133 //LookupFileCache.clear();
136 /// ClearFileInfo - Forget everything we know about headers so far.
137 void ClearFileInfo() {
141 void SetExternalLookup(ExternalIdentifierLookup *EIL) {
142 ExternalLookup = EIL;
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);
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);
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();
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);
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;
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;
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;
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;
203 /// SetFileControllingMacro - Mark the specified file as having a controlling
204 /// macro. This is used by the multiple-include optimization to eliminate
206 void SetFileControllingMacro(const FileEntry *File,
207 const IdentifierInfo *ControllingMacro) {
208 getFileInfo(File).ControllingMacro = ControllingMacro;
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);
215 void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
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(); }
222 // Used by ASTReader.
223 void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID);
228 /// getFileInfo - Return the HeaderFileInfo structure for the specified
230 HeaderFileInfo &getFileInfo(const FileEntry *FE);
233 } // end namespace clang