blob: 67004c4fe63f876102bdf6df62d3862abc879f2c [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.
Chris Lattnerffda8962006-10-27 05:15:55 +0000133 std::string Name = CurFileEnt->getDir()->getName();
134 if (const FileEntry *FE = FileMgr.getFile(Name+"/"+Filename)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000135 // 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 Lattnerffda8962006-10-27 05:15:55 +0000161 std::string Name = SearchDirs[i].getDir()->getName();
162 FE = FileMgr.getFile(Name+"/"+Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000163 } else {
Chris Lattner641a0be2006-10-20 06:23:14 +0000164 FE = DoFrameworkLookup(SearchDirs[i].getDir(), Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000165 }
166
Chris Lattner577377e2006-10-20 04:55:45 +0000167 if (FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000168 CurDir = &SearchDirs[i];
169
170 // This file is a system header or C++ unfriendly if the dir is.
171 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
172 return FE;
173 }
174 }
175
176 // Otherwise, didn't find it.
177 return 0;
178}
179
Chris Lattner63dd32b2006-10-20 04:42:40 +0000180/// LookupSubframeworkHeader - Look up a subframework for the specified
181/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
182/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
183/// is a subframework within Carbon.framework. If so, return the FileEntry
184/// for the designated file, otherwise return null.
185const FileEntry *HeaderSearch::
186LookupSubframeworkHeader(const std::string &Filename,
187 const FileEntry *ContextFileEnt) {
188 // Framework names must have a '/' in the filename. Find it.
189 std::string::size_type SlashPos = Filename.find('/');
190 if (SlashPos == std::string::npos) return 0;
191
Chris Lattner63dd32b2006-10-20 04:42:40 +0000192 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000193 const char *ContextName = ContextFileEnt->getName();
194
Chris Lattner63dd32b2006-10-20 04:42:40 +0000195 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000196 const char *FrameworkPos = strstr(ContextName, ".framework/");
197 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000198 return 0;
199
Chris Lattner48043482006-10-27 05:12:36 +0000200 std::string FrameworkName(ContextName, FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000201
Chris Lattner63dd32b2006-10-20 04:42:40 +0000202 // Append Frameworks/HIToolbox.framework/
203 FrameworkName += "Frameworks/";
204 FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos);
205 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000206
Chris Lattner5ed76da2006-10-22 07:24:13 +0000207 const DirectoryEntry *&CacheLookup =
208 FrameworkMap[std::string(Filename.begin(), Filename.begin()+SlashPos)];
209
210 // Some other location?
211 if (CacheLookup && CacheLookup->getName() != FrameworkName)
212 return 0;
213
214 // Cache subframework.
215 if (CacheLookup == 0) {
216 ++NumSubFrameworkLookups;
217
218 // If the framework dir doesn't exist, we fail.
219 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
220 if (Dir == 0) return 0;
221
222 // Otherwise, if it does, remember that this is the right direntry for this
223 // framework.
224 CacheLookup = Dir;
225 }
226
Chris Lattner577377e2006-10-20 04:55:45 +0000227 const FileEntry *FE = 0;
228
Chris Lattner63dd32b2006-10-20 04:42:40 +0000229 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
230 std::string HeadersFilename = FrameworkName + "Headers/" +
231 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner577377e2006-10-20 04:55:45 +0000232 if (!(FE = FileMgr.getFile(HeadersFilename))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000233
234 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
235 std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" +
236 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner577377e2006-10-20 04:55:45 +0000237 if (!(FE = FileMgr.getFile(PrivateHeadersFilename)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000238 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000239 }
240
Chris Lattner577377e2006-10-20 04:55:45 +0000241 // This file is a system header or C++ unfriendly if the old file is.
242 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
243 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000244}
245
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000246//===----------------------------------------------------------------------===//
247// File Info Management.
248//===----------------------------------------------------------------------===//
249
250
251/// getFileInfo - Return the PerFileInfo structure for the specified
252/// FileEntry.
253HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
254 if (FE->getUID() >= FileInfo.size())
255 FileInfo.resize(FE->getUID()+1);
256 return FileInfo[FE->getUID()];
257}
258
259/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
260/// #include, #include_next, or #import directive. Return false if #including
261/// the file will have no effect or true if we should include it.
262bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
263 ++NumIncluded; // Count # of attempted #includes.
264
265 // Get information about this file.
266 PerFileInfo &FileInfo = getFileInfo(File);
267
268 // If this is a #import directive, check that we have not already imported
269 // this header.
270 if (isImport) {
271 // If this has already been imported, don't import it again.
272 FileInfo.isImport = true;
273
274 // Has this already been #import'ed or #include'd?
275 if (FileInfo.NumIncludes) return false;
276 } else {
277 // Otherwise, if this is a #include of a file that was previously #import'd
278 // or if this is the second #include of a #pragma once file, ignore it.
279 if (FileInfo.isImport)
280 return false;
281 }
282
283 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
284 // if the macro that guards it is defined, we know the #include has no effect.
285 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
286 ++NumMultiIncludeFileOptzn;
287 return false;
288 }
289
290 // Increment the number of times this file has been included.
291 ++FileInfo.NumIncludes;
292
293 return true;
294}
295
296