blob: aedaa09a7204488c1a6f724fefe7fd3dd7aa1bed [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 Lattner5ed76da2006-10-22 07:24:13 +000065 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.
69 if (CacheLookup && CacheLookup != Dir)
70 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
84 if (CacheLookup == 0) {
85 ++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.
94 CacheLookup = Dir;
95 }
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 Lattner5ed76da2006-10-22 07:24:13 +0000225 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 Lattner43fd42e2006-10-30 03:40:58 +0000229 if (CacheLookup && strcmp(CacheLookup->getName(), FrameworkName.c_str()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000230 return 0;
231
232 // Cache subframework.
233 if (CacheLookup == 0) {
234 ++NumSubFrameworkLookups;
235
236 // If the framework dir doesn't exist, we fail.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000237 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
238 FrameworkName.end());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000239 if (Dir == 0) return 0;
240
241 // Otherwise, if it does, remember that this is the right direntry for this
242 // framework.
243 CacheLookup = Dir;
244 }
245
Chris Lattner577377e2006-10-20 04:55:45 +0000246 const FileEntry *FE = 0;
247
Chris Lattner63dd32b2006-10-20 04:42:40 +0000248 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000249 SmallString<1024> HeadersFilename(FrameworkName);
250 HeadersFilename += "Headers/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000251 HeadersFilename.append(SlashPos+1, FilenameEnd);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000252 if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
253 HeadersFilename.end()))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000254
255 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000256 HeadersFilename = FrameworkName;
257 HeadersFilename += "PrivateHeaders/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000258 HeadersFilename.append(SlashPos+1, FilenameEnd);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000259 if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000260 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000261 }
262
Chris Lattner577377e2006-10-20 04:55:45 +0000263 // This file is a system header or C++ unfriendly if the old file is.
264 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
265 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000266}
267
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000268//===----------------------------------------------------------------------===//
269// File Info Management.
270//===----------------------------------------------------------------------===//
271
272
273/// getFileInfo - Return the PerFileInfo structure for the specified
274/// FileEntry.
275HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
276 if (FE->getUID() >= FileInfo.size())
277 FileInfo.resize(FE->getUID()+1);
278 return FileInfo[FE->getUID()];
279}
280
281/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
282/// #include, #include_next, or #import directive. Return false if #including
283/// the file will have no effect or true if we should include it.
284bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
285 ++NumIncluded; // Count # of attempted #includes.
286
287 // Get information about this file.
288 PerFileInfo &FileInfo = getFileInfo(File);
289
290 // If this is a #import directive, check that we have not already imported
291 // this header.
292 if (isImport) {
293 // If this has already been imported, don't import it again.
294 FileInfo.isImport = true;
295
296 // Has this already been #import'ed or #include'd?
297 if (FileInfo.NumIncludes) return false;
298 } else {
299 // Otherwise, if this is a #include of a file that was previously #import'd
300 // or if this is the second #include of a #pragma once file, ignore it.
301 if (FileInfo.isImport)
302 return false;
303 }
304
305 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
306 // if the macro that guards it is defined, we know the #include has no effect.
307 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
308 ++NumMultiIncludeFileOptzn;
309 return false;
310 }
311
312 // Increment the number of times this file has been included.
313 ++FileInfo.NumIncludes;
314
315 return true;
316}
317
318