blob: cf72e60a01e2cf40781738c888ecca3883117d9b [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 +000019#include <iostream>
20using namespace llvm;
21using namespace clang;
22
Chris Lattner641a0be2006-10-20 06:23:14 +000023HeaderSearch::HeaderSearch(FileManager &FM) : FileMgr(FM) {
24 SystemDirIdx = 0;
25 NoCurDirSearch = false;
26
27 NumIncluded = 0;
28 NumMultiIncludeFileOptzn = 0;
29 NumFrameworkLookups = NumSubFrameworkLookups = 0;
30}
31
Chris Lattner59a9ebd2006-10-18 05:34:33 +000032void HeaderSearch::PrintStats() {
33 std::cerr << "\n*** HeaderSearch Stats:\n";
34 std::cerr << FileInfo.size() << " files tracked.\n";
35 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
36 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
37 NumOnceOnlyFiles += FileInfo[i].isImport;
38 if (MaxNumIncludes < FileInfo[i].NumIncludes)
39 MaxNumIncludes = FileInfo[i].NumIncludes;
40 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
41 }
42 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
43 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
44 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
45
46 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
47 std::cerr << " " << NumMultiIncludeFileOptzn << " #includes skipped due to"
Chris Lattner63dd32b2006-10-20 04:42:40 +000048 << " the multi-include optimization.\n";
Chris Lattner641a0be2006-10-20 06:23:14 +000049
50 std::cerr << NumFrameworkLookups << " framework lookups.\n";
51 std::cerr << NumSubFrameworkLookups << " subframework lookups.\n";
Chris Lattner59a9ebd2006-10-18 05:34:33 +000052}
53
54//===----------------------------------------------------------------------===//
55// Header File Location.
56//===----------------------------------------------------------------------===//
57
Chris Lattner641a0be2006-10-20 06:23:14 +000058const FileEntry *HeaderSearch::DoFrameworkLookup(const DirectoryEntry *Dir,
Chris Lattner7cdbad92006-10-30 05:33:15 +000059 const char *FilenameStart,
60 const char *FilenameEnd) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +000061 // Framework names must have a '/' in the filename.
Chris Lattner7cdbad92006-10-30 05:33:15 +000062 const char *SlashPos = std::find(FilenameStart, FilenameEnd, '/');
63 if (SlashPos == FilenameEnd) return 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000064
Chris Lattner34d1f5a2007-02-08 19:08:49 +000065 StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner7cdbad92006-10-30 05:33:15 +000066 FrameworkMap.GetOrCreateValue(FilenameStart, SlashPos);
Chris Lattner641a0be2006-10-20 06:23:14 +000067
Chris Lattner5ed76da2006-10-22 07:24:13 +000068 // If it is some other directory, fail.
Chris Lattner34d1f5a2007-02-08 19:08:49 +000069 if (CacheLookup.getValue() && CacheLookup.getValue() != Dir)
Chris Lattner5ed76da2006-10-22 07:24:13 +000070 return 0;
71
Chris Lattner59a9ebd2006-10-18 05:34:33 +000072 // FrameworkName = "/System/Library/Frameworks/"
Chris Lattnerb201d9b2006-10-30 05:09:49 +000073 SmallString<1024> FrameworkName;
74 FrameworkName += Dir->getName();
75 if (FrameworkName.empty() || FrameworkName.back() != '/')
76 FrameworkName.push_back('/');
Chris Lattner59a9ebd2006-10-18 05:34:33 +000077
78 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattner7cdbad92006-10-30 05:33:15 +000079 FrameworkName.append(FilenameStart, SlashPos);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000080
81 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
82 FrameworkName += ".framework/";
Chris Lattner5ed76da2006-10-22 07:24:13 +000083
Chris Lattner34d1f5a2007-02-08 19:08:49 +000084 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +000085 ++NumFrameworkLookups;
86
87 // If the framework dir doesn't exist, we fail.
Chris Lattnerb201d9b2006-10-30 05:09:49 +000088 if (!sys::Path(std::string(FrameworkName.begin(),
89 FrameworkName.end())).exists())
Chris Lattner5ed76da2006-10-22 07:24:13 +000090 return 0;
91
92 // Otherwise, if it does, remember that this is the right direntry for this
93 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +000094 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +000095 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +000096
Chris Lattner59a9ebd2006-10-18 05:34:33 +000097 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +000098 unsigned OrigSize = FrameworkName.size();
99
100 FrameworkName += "Headers/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000101 FrameworkName.append(SlashPos+1, FilenameEnd);
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000102 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.begin(),
103 FrameworkName.end())) {
Chris Lattner577377e2006-10-20 04:55:45 +0000104 return FE;
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000105 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000106
107 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000108 const char *Private = "Private";
109 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
110 Private+strlen(Private));
111 return FileMgr.getFile(FrameworkName.begin(), FrameworkName.end());
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000112}
113
114/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
115/// return null on failure. isAngled indicates whether the file reference is
116/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
117/// non-null, indicates where the #including file is, in case a relative search
118/// is needed.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000119const FileEntry *HeaderSearch::LookupFile(const char *FilenameStart,
120 const char *FilenameEnd,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000121 bool isAngled,
122 const DirectoryLookup *FromDir,
123 const DirectoryLookup *&CurDir,
124 const FileEntry *CurFileEnt) {
125 // If 'Filename' is absolute, check to see if it exists and no searching.
126 // FIXME: Portability. This should be a sys::Path interface, this doesn't
127 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000128 if (FilenameStart[0] == '/') {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000129 CurDir = 0;
130
131 // If this was an #include_next "/absolute/file", fail.
132 if (FromDir) return 0;
133
134 // Otherwise, just return the file.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000135 return FileMgr.getFile(FilenameStart, FilenameEnd);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000136 }
137
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000138 SmallString<1024> TmpDir;
139
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000140 // Step #0, unless disabled, check to see if the file is in the #includer's
141 // directory. This search is not done for <> headers.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000142 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000143 // Concatenate the requested file onto the directory.
144 // FIXME: Portability. Filename concatenation should be in sys::Path.
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000145 TmpDir += CurFileEnt->getDir()->getName();
146 TmpDir.push_back('/');
Chris Lattner7cdbad92006-10-30 05:33:15 +0000147 TmpDir.append(FilenameStart, FilenameEnd);
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000148 if (const FileEntry *FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end())) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000149 // Leave CurDir unset.
150
151 // This file is a system header or C++ unfriendly if the old file is.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000152 getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000153 return FE;
154 }
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000155 TmpDir.clear();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000156 }
157
158 CurDir = 0;
159
160 // If this is a system #include, ignore the user #include locs.
161 unsigned i = isAngled ? SystemDirIdx : 0;
162
163 // If this is a #include_next request, start searching after the directory the
164 // file was found in.
165 if (FromDir)
166 i = FromDir-&SearchDirs[0];
167
168 // Check each directory in sequence to see if it contains this file.
169 for (; i != SearchDirs.size(); ++i) {
Chris Lattner577377e2006-10-20 04:55:45 +0000170 const FileEntry *FE = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000171 if (!SearchDirs[i].isFramework()) {
172 // FIXME: Portability. Adding file to dir should be in sys::Path.
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000173 // Concatenate the requested file onto the directory.
174 TmpDir.clear();
175 TmpDir += SearchDirs[i].getDir()->getName();
176 TmpDir.push_back('/');
Chris Lattner7cdbad92006-10-30 05:33:15 +0000177 TmpDir.append(FilenameStart, FilenameEnd);
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000178 FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end());
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000179 } else {
Chris Lattner7cdbad92006-10-30 05:33:15 +0000180 FE = DoFrameworkLookup(SearchDirs[i].getDir(), FilenameStart,FilenameEnd);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000181 }
182
Chris Lattner577377e2006-10-20 04:55:45 +0000183 if (FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000184 CurDir = &SearchDirs[i];
185
186 // This file is a system header or C++ unfriendly if the dir is.
187 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
188 return FE;
189 }
190 }
191
192 // Otherwise, didn't find it.
193 return 0;
194}
195
Chris Lattner63dd32b2006-10-20 04:42:40 +0000196/// LookupSubframeworkHeader - Look up a subframework for the specified
197/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
198/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
199/// is a subframework within Carbon.framework. If so, return the FileEntry
200/// for the designated file, otherwise return null.
201const FileEntry *HeaderSearch::
Chris Lattner7cdbad92006-10-30 05:33:15 +0000202LookupSubframeworkHeader(const char *FilenameStart,
203 const char *FilenameEnd,
Chris Lattner63dd32b2006-10-20 04:42:40 +0000204 const FileEntry *ContextFileEnt) {
205 // Framework names must have a '/' in the filename. Find it.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000206 const char *SlashPos = std::find(FilenameStart, FilenameEnd, '/');
207 if (SlashPos == FilenameEnd) return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000208
Chris Lattner63dd32b2006-10-20 04:42:40 +0000209 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000210 const char *ContextName = ContextFileEnt->getName();
211
Chris Lattner63dd32b2006-10-20 04:42:40 +0000212 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000213 const char *FrameworkPos = strstr(ContextName, ".framework/");
214 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000215 return 0;
216
Chris Lattner43fd42e2006-10-30 03:40:58 +0000217 SmallString<1024> FrameworkName(ContextName,
218 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000219
Chris Lattner63dd32b2006-10-20 04:42:40 +0000220 // Append Frameworks/HIToolbox.framework/
221 FrameworkName += "Frameworks/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000222 FrameworkName.append(FilenameStart, SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000223 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000224
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000225 StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner7cdbad92006-10-30 05:33:15 +0000226 FrameworkMap.GetOrCreateValue(FilenameStart, SlashPos);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000227
228 // Some other location?
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000229 if (CacheLookup.getValue() &&
230 CacheLookup.getKeyLength() == FrameworkName.size() &&
231 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
232 CacheLookup.getKeyLength()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000233 return 0;
234
235 // Cache subframework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000236 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000237 ++NumSubFrameworkLookups;
238
239 // If the framework dir doesn't exist, we fail.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000240 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
241 FrameworkName.end());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000242 if (Dir == 0) return 0;
243
244 // Otherwise, if it does, remember that this is the right direntry for this
245 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000246 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000247 }
248
Chris Lattner577377e2006-10-20 04:55:45 +0000249 const FileEntry *FE = 0;
250
Chris Lattner63dd32b2006-10-20 04:42:40 +0000251 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000252 SmallString<1024> HeadersFilename(FrameworkName);
253 HeadersFilename += "Headers/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000254 HeadersFilename.append(SlashPos+1, FilenameEnd);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000255 if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
256 HeadersFilename.end()))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000257
258 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000259 HeadersFilename = FrameworkName;
260 HeadersFilename += "PrivateHeaders/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000261 HeadersFilename.append(SlashPos+1, FilenameEnd);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000262 if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000263 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000264 }
265
Chris Lattner577377e2006-10-20 04:55:45 +0000266 // This file is a system header or C++ unfriendly if the old file is.
267 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
268 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000269}
270
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000271//===----------------------------------------------------------------------===//
272// File Info Management.
273//===----------------------------------------------------------------------===//
274
275
276/// getFileInfo - Return the PerFileInfo structure for the specified
277/// FileEntry.
278HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
279 if (FE->getUID() >= FileInfo.size())
280 FileInfo.resize(FE->getUID()+1);
281 return FileInfo[FE->getUID()];
282}
283
284/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
285/// #include, #include_next, or #import directive. Return false if #including
286/// the file will have no effect or true if we should include it.
287bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
288 ++NumIncluded; // Count # of attempted #includes.
289
290 // Get information about this file.
291 PerFileInfo &FileInfo = getFileInfo(File);
292
293 // If this is a #import directive, check that we have not already imported
294 // this header.
295 if (isImport) {
296 // If this has already been imported, don't import it again.
297 FileInfo.isImport = true;
298
299 // Has this already been #import'ed or #include'd?
300 if (FileInfo.NumIncludes) return false;
301 } else {
302 // Otherwise, if this is a #include of a file that was previously #import'd
303 // or if this is the second #include of a #pragma once file, ignore it.
304 if (FileInfo.isImport)
305 return false;
306 }
307
308 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
309 // if the macro that guards it is defined, we know the #include has no effect.
310 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
311 ++NumMultiIncludeFileOptzn;
312 return false;
313 }
314
315 // Increment the number of times this file has been included.
316 ++FileInfo.NumIncludes;
317
318 return true;
319}
320
321