blob: ec55e02e272d8708938009222e635bf8ef6f73e7 [file] [log] [blame]
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
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 implements the DirectoryLookup and HeaderSearch interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/FileManager.h"
15#include "clang/Lex/HeaderSearch.h"
16#include "clang/Lex/IdentifierTable.h"
17#include "llvm/System/Path.h"
18#include <iostream>
19using namespace llvm;
20using namespace clang;
21
Chris Lattner641a0be2006-10-20 06:23:14 +000022HeaderSearch::HeaderSearch(FileManager &FM) : FileMgr(FM) {
23 SystemDirIdx = 0;
24 NoCurDirSearch = false;
25
26 NumIncluded = 0;
27 NumMultiIncludeFileOptzn = 0;
28 NumFrameworkLookups = NumSubFrameworkLookups = 0;
29}
30
Chris Lattner59a9ebd2006-10-18 05:34:33 +000031void HeaderSearch::PrintStats() {
32 std::cerr << "\n*** HeaderSearch Stats:\n";
33 std::cerr << FileInfo.size() << " files tracked.\n";
34 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
35 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
36 NumOnceOnlyFiles += FileInfo[i].isImport;
37 if (MaxNumIncludes < FileInfo[i].NumIncludes)
38 MaxNumIncludes = FileInfo[i].NumIncludes;
39 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
40 }
41 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
42 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
43 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
44
45 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
46 std::cerr << " " << NumMultiIncludeFileOptzn << " #includes skipped due to"
Chris Lattner63dd32b2006-10-20 04:42:40 +000047 << " the multi-include optimization.\n";
Chris Lattner641a0be2006-10-20 06:23:14 +000048
49 std::cerr << NumFrameworkLookups << " framework lookups.\n";
50 std::cerr << NumSubFrameworkLookups << " subframework lookups.\n";
Chris Lattner59a9ebd2006-10-18 05:34:33 +000051}
52
53//===----------------------------------------------------------------------===//
54// Header File Location.
55//===----------------------------------------------------------------------===//
56
Chris Lattner641a0be2006-10-20 06:23:14 +000057const FileEntry *HeaderSearch::DoFrameworkLookup(const DirectoryEntry *Dir,
58 const std::string &Filename) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +000059 // Framework names must have a '/' in the filename.
60 std::string::size_type SlashPos = Filename.find('/');
Chris Lattner577377e2006-10-20 04:55:45 +000061 if (SlashPos == std::string::npos) return 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000062
Chris Lattner5ed76da2006-10-22 07:24:13 +000063 const DirectoryEntry *&CacheLookup =
64 FrameworkMap[std::string(Filename.begin(), Filename.begin()+SlashPos)];
Chris Lattner641a0be2006-10-20 06:23:14 +000065
Chris Lattner5ed76da2006-10-22 07:24:13 +000066 // If it is some other directory, fail.
67 if (CacheLookup && CacheLookup != Dir)
68 return 0;
69
Chris Lattner59a9ebd2006-10-18 05:34:33 +000070 // FrameworkName = "/System/Library/Frameworks/"
71 std::string FrameworkName = Dir->getName();
72 if (FrameworkName.empty() || FrameworkName[FrameworkName.size()-1] != '/')
73 FrameworkName += '/';
74
75 // FrameworkName = "/System/Library/Frameworks/Cocoa"
76 FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos);
77
78 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
79 FrameworkName += ".framework/";
Chris Lattner5ed76da2006-10-22 07:24:13 +000080
81 if (CacheLookup == 0) {
82 ++NumFrameworkLookups;
83
84 // If the framework dir doesn't exist, we fail.
85 if (!sys::Path(FrameworkName).exists())
86 return 0;
87
88 // Otherwise, if it does, remember that this is the right direntry for this
89 // framework.
90 CacheLookup = Dir;
91 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +000092
Chris Lattner59a9ebd2006-10-18 05:34:33 +000093 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
94 std::string HeadersFilename = FrameworkName + "Headers/" +
95 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner641a0be2006-10-20 06:23:14 +000096 if (const FileEntry *FE = FileMgr.getFile(HeadersFilename))
Chris Lattner577377e2006-10-20 04:55:45 +000097 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000098
99 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
100 std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" +
101 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner641a0be2006-10-20 06:23:14 +0000102 return FileMgr.getFile(PrivateHeadersFilename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000103}
104
105/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
106/// return null on failure. isAngled indicates whether the file reference is
107/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
108/// non-null, indicates where the #including file is, in case a relative search
109/// is needed.
110const FileEntry *HeaderSearch::LookupFile(const std::string &Filename,
111 bool isAngled,
112 const DirectoryLookup *FromDir,
113 const DirectoryLookup *&CurDir,
114 const FileEntry *CurFileEnt) {
115 // If 'Filename' is absolute, check to see if it exists and no searching.
116 // FIXME: Portability. This should be a sys::Path interface, this doesn't
117 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
118 if (Filename[0] == '/') {
119 CurDir = 0;
120
121 // If this was an #include_next "/absolute/file", fail.
122 if (FromDir) return 0;
123
124 // Otherwise, just return the file.
125 return FileMgr.getFile(Filename);
126 }
127
128 // Step #0, unless disabled, check to see if the file is in the #includer's
129 // directory. This search is not done for <> headers.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000130 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000131 // Concatenate the requested file onto the directory.
132 // FIXME: Portability. Filename concatenation should be in sys::Path.
133 if (const FileEntry *FE =
134 FileMgr.getFile(CurFileEnt->getDir()->getName()+"/"+Filename)) {
135 // Leave CurDir unset.
136
137 // This file is a system header or C++ unfriendly if the old file is.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000138 getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000139 return FE;
140 }
141 }
142
143 CurDir = 0;
144
145 // If this is a system #include, ignore the user #include locs.
146 unsigned i = isAngled ? SystemDirIdx : 0;
147
148 // If this is a #include_next request, start searching after the directory the
149 // file was found in.
150 if (FromDir)
151 i = FromDir-&SearchDirs[0];
152
153 // Check each directory in sequence to see if it contains this file.
154 for (; i != SearchDirs.size(); ++i) {
155 // Concatenate the requested file onto the directory.
156 std::string SearchDir;
157
Chris Lattner577377e2006-10-20 04:55:45 +0000158 const FileEntry *FE = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000159 if (!SearchDirs[i].isFramework()) {
160 // FIXME: Portability. Adding file to dir should be in sys::Path.
Chris Lattner641a0be2006-10-20 06:23:14 +0000161 FE = FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000162 } else {
Chris Lattner641a0be2006-10-20 06:23:14 +0000163 FE = DoFrameworkLookup(SearchDirs[i].getDir(), Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000164 }
165
Chris Lattner577377e2006-10-20 04:55:45 +0000166 if (FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000167 CurDir = &SearchDirs[i];
168
169 // This file is a system header or C++ unfriendly if the dir is.
170 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
171 return FE;
172 }
173 }
174
175 // Otherwise, didn't find it.
176 return 0;
177}
178
Chris Lattner63dd32b2006-10-20 04:42:40 +0000179/// LookupSubframeworkHeader - Look up a subframework for the specified
180/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
181/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
182/// is a subframework within Carbon.framework. If so, return the FileEntry
183/// for the designated file, otherwise return null.
184const FileEntry *HeaderSearch::
185LookupSubframeworkHeader(const std::string &Filename,
186 const FileEntry *ContextFileEnt) {
187 // Framework names must have a '/' in the filename. Find it.
188 std::string::size_type SlashPos = Filename.find('/');
189 if (SlashPos == std::string::npos) return 0;
190
Chris Lattner63dd32b2006-10-20 04:42:40 +0000191 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000192 const char *ContextName = ContextFileEnt->getName();
193
Chris Lattner63dd32b2006-10-20 04:42:40 +0000194 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000195 const char *FrameworkPos = strstr(ContextName, ".framework/");
196 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000197 return 0;
198
Chris Lattner48043482006-10-27 05:12:36 +0000199 std::string FrameworkName(ContextName, FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000200
Chris Lattner63dd32b2006-10-20 04:42:40 +0000201 // Append Frameworks/HIToolbox.framework/
202 FrameworkName += "Frameworks/";
203 FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos);
204 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000205
Chris Lattner5ed76da2006-10-22 07:24:13 +0000206 const DirectoryEntry *&CacheLookup =
207 FrameworkMap[std::string(Filename.begin(), Filename.begin()+SlashPos)];
208
209 // Some other location?
210 if (CacheLookup && CacheLookup->getName() != FrameworkName)
211 return 0;
212
213 // Cache subframework.
214 if (CacheLookup == 0) {
215 ++NumSubFrameworkLookups;
216
217 // If the framework dir doesn't exist, we fail.
218 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
219 if (Dir == 0) return 0;
220
221 // Otherwise, if it does, remember that this is the right direntry for this
222 // framework.
223 CacheLookup = Dir;
224 }
225
Chris Lattner577377e2006-10-20 04:55:45 +0000226 const FileEntry *FE = 0;
227
Chris Lattner63dd32b2006-10-20 04:42:40 +0000228 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
229 std::string HeadersFilename = FrameworkName + "Headers/" +
230 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner577377e2006-10-20 04:55:45 +0000231 if (!(FE = FileMgr.getFile(HeadersFilename))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000232
233 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
234 std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" +
235 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner577377e2006-10-20 04:55:45 +0000236 if (!(FE = FileMgr.getFile(PrivateHeadersFilename)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000237 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000238 }
239
Chris Lattner577377e2006-10-20 04:55:45 +0000240 // This file is a system header or C++ unfriendly if the old file is.
241 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
242 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000243}
244
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000245//===----------------------------------------------------------------------===//
246// File Info Management.
247//===----------------------------------------------------------------------===//
248
249
250/// getFileInfo - Return the PerFileInfo structure for the specified
251/// FileEntry.
252HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
253 if (FE->getUID() >= FileInfo.size())
254 FileInfo.resize(FE->getUID()+1);
255 return FileInfo[FE->getUID()];
256}
257
258/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
259/// #include, #include_next, or #import directive. Return false if #including
260/// the file will have no effect or true if we should include it.
261bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
262 ++NumIncluded; // Count # of attempted #includes.
263
264 // Get information about this file.
265 PerFileInfo &FileInfo = getFileInfo(File);
266
267 // If this is a #import directive, check that we have not already imported
268 // this header.
269 if (isImport) {
270 // If this has already been imported, don't import it again.
271 FileInfo.isImport = true;
272
273 // Has this already been #import'ed or #include'd?
274 if (FileInfo.NumIncludes) return false;
275 } else {
276 // Otherwise, if this is a #include of a file that was previously #import'd
277 // or if this is the second #include of a #pragma once file, ignore it.
278 if (FileInfo.isImport)
279 return false;
280 }
281
282 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
283 // if the macro that guards it is defined, we know the #include has no effect.
284 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
285 ++NumMultiIncludeFileOptzn;
286 return false;
287 }
288
289 // Increment the number of times this file has been included.
290 ++FileInfo.NumIncludes;
291
292 return true;
293}
294
295