blob: 00a36eef63456320a5f7edfb3f7b77c234d14df8 [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"
Chris Lattner43fd42e2006-10-30 03:40:58 +000018#include "llvm/ADT/SmallString.h"
Chris Lattner59a9ebd2006-10-18 05:34:33 +000019using namespace clang;
20
Chris Lattner23d89032007-04-03 22:14:25 +000021HeaderSearch::HeaderSearch(FileManager &FM) : FileMgr(FM), FrameworkMap(64) {
Chris Lattner641a0be2006-10-20 06:23:14 +000022 SystemDirIdx = 0;
23 NoCurDirSearch = false;
24
25 NumIncluded = 0;
26 NumMultiIncludeFileOptzn = 0;
27 NumFrameworkLookups = NumSubFrameworkLookups = 0;
28}
29
Chris Lattner59a9ebd2006-10-18 05:34:33 +000030void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000031 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
32 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000033 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
34 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
35 NumOnceOnlyFiles += FileInfo[i].isImport;
36 if (MaxNumIncludes < FileInfo[i].NumIncludes)
37 MaxNumIncludes = FileInfo[i].NumIncludes;
38 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
39 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000040 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
41 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
42 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000043
Chris Lattner23b7eb62007-06-15 23:05:46 +000044 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
45 fprintf(stderr, " %d #includes skipped due to"
46 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Chris Lattner641a0be2006-10-20 06:23:14 +000047
Chris Lattner23b7eb62007-06-15 23:05:46 +000048 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
49 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000050}
51
52//===----------------------------------------------------------------------===//
53// Header File Location.
54//===----------------------------------------------------------------------===//
55
Chris Lattner641a0be2006-10-20 06:23:14 +000056const FileEntry *HeaderSearch::DoFrameworkLookup(const DirectoryEntry *Dir,
Chris Lattner7cdbad92006-10-30 05:33:15 +000057 const char *FilenameStart,
58 const char *FilenameEnd) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +000059 // Framework names must have a '/' in the filename.
Chris Lattner7cdbad92006-10-30 05:33:15 +000060 const char *SlashPos = std::find(FilenameStart, FilenameEnd, '/');
61 if (SlashPos == FilenameEnd) return 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000062
Chris Lattner23b7eb62007-06-15 23:05:46 +000063 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner7cdbad92006-10-30 05:33:15 +000064 FrameworkMap.GetOrCreateValue(FilenameStart, SlashPos);
Chris Lattner641a0be2006-10-20 06:23:14 +000065
Chris Lattner5ed76da2006-10-22 07:24:13 +000066 // If it is some other directory, fail.
Chris Lattner34d1f5a2007-02-08 19:08:49 +000067 if (CacheLookup.getValue() && CacheLookup.getValue() != Dir)
Chris Lattner5ed76da2006-10-22 07:24:13 +000068 return 0;
69
Chris Lattner59a9ebd2006-10-18 05:34:33 +000070 // FrameworkName = "/System/Library/Frameworks/"
Chris Lattner23b7eb62007-06-15 23:05:46 +000071 llvm::SmallString<1024> FrameworkName;
Chris Lattnerb201d9b2006-10-30 05:09:49 +000072 FrameworkName += Dir->getName();
73 if (FrameworkName.empty() || FrameworkName.back() != '/')
74 FrameworkName.push_back('/');
Chris Lattner59a9ebd2006-10-18 05:34:33 +000075
76 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattner7cdbad92006-10-30 05:33:15 +000077 FrameworkName.append(FilenameStart, SlashPos);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000078
79 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
80 FrameworkName += ".framework/";
Chris Lattner5ed76da2006-10-22 07:24:13 +000081
Chris Lattner34d1f5a2007-02-08 19:08:49 +000082 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +000083 ++NumFrameworkLookups;
84
85 // If the framework dir doesn't exist, we fail.
Chris Lattner23b7eb62007-06-15 23:05:46 +000086 if (!llvm::sys::Path(std::string(FrameworkName.begin(),
87 FrameworkName.end())).exists())
Chris Lattner5ed76da2006-10-22 07:24:13 +000088 return 0;
89
90 // Otherwise, if it does, remember that this is the right direntry for this
91 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +000092 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +000093 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +000094
Chris Lattner59a9ebd2006-10-18 05:34:33 +000095 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +000096 unsigned OrigSize = FrameworkName.size();
97
98 FrameworkName += "Headers/";
Chris Lattner7cdbad92006-10-30 05:33:15 +000099 FrameworkName.append(SlashPos+1, FilenameEnd);
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000100 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.begin(),
101 FrameworkName.end())) {
Chris Lattner577377e2006-10-20 04:55:45 +0000102 return FE;
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000103 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000104
105 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000106 const char *Private = "Private";
107 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
108 Private+strlen(Private));
109 return FileMgr.getFile(FrameworkName.begin(), FrameworkName.end());
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000110}
111
112/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
113/// return null on failure. isAngled indicates whether the file reference is
114/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
115/// non-null, indicates where the #including file is, in case a relative search
116/// is needed.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000117const FileEntry *HeaderSearch::LookupFile(const char *FilenameStart,
118 const char *FilenameEnd,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000119 bool isAngled,
120 const DirectoryLookup *FromDir,
121 const DirectoryLookup *&CurDir,
122 const FileEntry *CurFileEnt) {
123 // If 'Filename' is absolute, check to see if it exists and no searching.
124 // FIXME: Portability. This should be a sys::Path interface, this doesn't
125 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000126 if (FilenameStart[0] == '/') {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000127 CurDir = 0;
128
129 // If this was an #include_next "/absolute/file", fail.
130 if (FromDir) return 0;
131
132 // Otherwise, just return the file.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000133 return FileMgr.getFile(FilenameStart, FilenameEnd);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000134 }
135
Chris Lattner23b7eb62007-06-15 23:05:46 +0000136 llvm::SmallString<1024> TmpDir;
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000137
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000138 // Step #0, unless disabled, check to see if the file is in the #includer's
139 // directory. This search is not done for <> headers.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000140 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000141 // Concatenate the requested file onto the directory.
142 // FIXME: Portability. Filename concatenation should be in sys::Path.
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000143 TmpDir += CurFileEnt->getDir()->getName();
144 TmpDir.push_back('/');
Chris Lattner7cdbad92006-10-30 05:33:15 +0000145 TmpDir.append(FilenameStart, FilenameEnd);
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000146 if (const FileEntry *FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end())) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000147 // Leave CurDir unset.
148
149 // This file is a system header or C++ unfriendly if the old file is.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000150 getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000151 return FE;
152 }
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000153 TmpDir.clear();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000154 }
155
156 CurDir = 0;
157
158 // If this is a system #include, ignore the user #include locs.
159 unsigned i = isAngled ? SystemDirIdx : 0;
160
161 // If this is a #include_next request, start searching after the directory the
162 // file was found in.
163 if (FromDir)
164 i = FromDir-&SearchDirs[0];
165
Chris Lattnerd4275422007-07-22 07:28:00 +0000166 // Cache all of the lookups performed by this method. Many headers are
167 // multiply included, and the "pragma once" optimization prevents them from
168 // being relex/pp'd, but they would still have to search through a
169 // (potentially huge) series of SearchDirs to find it.
170 std::pair<unsigned, unsigned> &CacheLookup =
171 LookupFileCache.GetOrCreateValue(FilenameStart, FilenameEnd).getValue();
172
173 // If the entry has been previously looked up, the first value will be
174 // non-zero. If the value is equal to i (the start point of our search), then
175 // this is a matching hit.
176 if (CacheLookup.first == i+1) {
177 // Skip querying potentially lots of directories for this lookup.
178 i = CacheLookup.second;
179 } else {
180 // Otherwise, this is the first query, or the previous query didn't match
181 // our search start. We will fill in our found location below, so prime the
182 // start point value.
183 CacheLookup.first = i+1;
184 }
185
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000186 // Check each directory in sequence to see if it contains this file.
187 for (; i != SearchDirs.size(); ++i) {
Chris Lattner577377e2006-10-20 04:55:45 +0000188 const FileEntry *FE = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000189 if (!SearchDirs[i].isFramework()) {
190 // FIXME: Portability. Adding file to dir should be in sys::Path.
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000191 // Concatenate the requested file onto the directory.
192 TmpDir.clear();
193 TmpDir += SearchDirs[i].getDir()->getName();
194 TmpDir.push_back('/');
Chris Lattner7cdbad92006-10-30 05:33:15 +0000195 TmpDir.append(FilenameStart, FilenameEnd);
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000196 FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end());
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000197 } else {
Chris Lattner7cdbad92006-10-30 05:33:15 +0000198 FE = DoFrameworkLookup(SearchDirs[i].getDir(), FilenameStart,FilenameEnd);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000199 }
200
Chris Lattner577377e2006-10-20 04:55:45 +0000201 if (FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000202 CurDir = &SearchDirs[i];
203
204 // This file is a system header or C++ unfriendly if the dir is.
205 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Chris Lattnerd4275422007-07-22 07:28:00 +0000206
207 // Remember this location for the next lookup we do.
208 CacheLookup.second = i;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000209 return FE;
210 }
211 }
212
Chris Lattnerd4275422007-07-22 07:28:00 +0000213 // Otherwise, didn't find it. Remember we didn't find this.
214 CacheLookup.second = SearchDirs.size();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000215 return 0;
216}
217
Chris Lattner63dd32b2006-10-20 04:42:40 +0000218/// LookupSubframeworkHeader - Look up a subframework for the specified
219/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
220/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
221/// is a subframework within Carbon.framework. If so, return the FileEntry
222/// for the designated file, otherwise return null.
223const FileEntry *HeaderSearch::
Chris Lattner7cdbad92006-10-30 05:33:15 +0000224LookupSubframeworkHeader(const char *FilenameStart,
225 const char *FilenameEnd,
Chris Lattner63dd32b2006-10-20 04:42:40 +0000226 const FileEntry *ContextFileEnt) {
227 // Framework names must have a '/' in the filename. Find it.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000228 const char *SlashPos = std::find(FilenameStart, FilenameEnd, '/');
229 if (SlashPos == FilenameEnd) return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000230
Chris Lattner63dd32b2006-10-20 04:42:40 +0000231 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000232 const char *ContextName = ContextFileEnt->getName();
233
Chris Lattner63dd32b2006-10-20 04:42:40 +0000234 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000235 const char *FrameworkPos = strstr(ContextName, ".framework/");
236 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000237 return 0;
238
Chris Lattner23b7eb62007-06-15 23:05:46 +0000239 llvm::SmallString<1024> FrameworkName(ContextName,
240 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000241
Chris Lattner63dd32b2006-10-20 04:42:40 +0000242 // Append Frameworks/HIToolbox.framework/
243 FrameworkName += "Frameworks/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000244 FrameworkName.append(FilenameStart, SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000245 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000246
Chris Lattner23b7eb62007-06-15 23:05:46 +0000247 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner7cdbad92006-10-30 05:33:15 +0000248 FrameworkMap.GetOrCreateValue(FilenameStart, SlashPos);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000249
250 // Some other location?
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000251 if (CacheLookup.getValue() &&
252 CacheLookup.getKeyLength() == FrameworkName.size() &&
253 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
254 CacheLookup.getKeyLength()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000255 return 0;
256
257 // Cache subframework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000258 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000259 ++NumSubFrameworkLookups;
260
261 // If the framework dir doesn't exist, we fail.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000262 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
263 FrameworkName.end());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000264 if (Dir == 0) return 0;
265
266 // Otherwise, if it does, remember that this is the right direntry for this
267 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000268 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000269 }
270
Chris Lattner577377e2006-10-20 04:55:45 +0000271 const FileEntry *FE = 0;
272
Chris Lattner63dd32b2006-10-20 04:42:40 +0000273 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000274 llvm::SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000275 HeadersFilename += "Headers/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000276 HeadersFilename.append(SlashPos+1, FilenameEnd);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000277 if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
278 HeadersFilename.end()))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000279
280 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000281 HeadersFilename = FrameworkName;
282 HeadersFilename += "PrivateHeaders/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000283 HeadersFilename.append(SlashPos+1, FilenameEnd);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000284 if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000285 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000286 }
287
Chris Lattner577377e2006-10-20 04:55:45 +0000288 // This file is a system header or C++ unfriendly if the old file is.
289 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
290 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000291}
292
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000293//===----------------------------------------------------------------------===//
294// File Info Management.
295//===----------------------------------------------------------------------===//
296
297
298/// getFileInfo - Return the PerFileInfo structure for the specified
299/// FileEntry.
300HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
301 if (FE->getUID() >= FileInfo.size())
302 FileInfo.resize(FE->getUID()+1);
303 return FileInfo[FE->getUID()];
304}
305
306/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
307/// #include, #include_next, or #import directive. Return false if #including
308/// the file will have no effect or true if we should include it.
309bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
310 ++NumIncluded; // Count # of attempted #includes.
311
312 // Get information about this file.
313 PerFileInfo &FileInfo = getFileInfo(File);
314
315 // If this is a #import directive, check that we have not already imported
316 // this header.
317 if (isImport) {
318 // If this has already been imported, don't import it again.
319 FileInfo.isImport = true;
320
321 // Has this already been #import'ed or #include'd?
322 if (FileInfo.NumIncludes) return false;
323 } else {
324 // Otherwise, if this is a #include of a file that was previously #import'd
325 // or if this is the second #include of a #pragma once file, ignore it.
326 if (FileInfo.isImport)
327 return false;
328 }
329
330 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
331 // if the macro that guards it is defined, we know the #include has no effect.
Chris Lattnerd7b971b2007-10-07 07:57:27 +0000332 if (FileInfo.ControllingMacro &&
333 FileInfo.ControllingMacro->hasMacroDefinition()) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000334 ++NumMultiIncludeFileOptzn;
335 return false;
336 }
337
338 // Increment the number of times this file has been included.
339 ++FileInfo.NumIncludes;
340
341 return true;
342}
343
344