blob: c12637180cd1a3bd741c9cef2aadc70e618a5280 [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,
59 const std::string &Filename) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +000060 // Framework names must have a '/' in the filename.
61 std::string::size_type SlashPos = Filename.find('/');
Chris Lattner577377e2006-10-20 04:55:45 +000062 if (SlashPos == std::string::npos) return 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000063
Chris Lattner5ed76da2006-10-22 07:24:13 +000064 const DirectoryEntry *&CacheLookup =
Chris Lattneree7bf892006-10-30 05:19:23 +000065 FrameworkMap.GetOrCreateValue(&Filename[0], &Filename[0]+SlashPos);
Chris Lattner641a0be2006-10-20 06:23:14 +000066
Chris Lattner5ed76da2006-10-22 07:24:13 +000067 // If it is some other directory, fail.
68 if (CacheLookup && CacheLookup != Dir)
69 return 0;
70
Chris Lattner59a9ebd2006-10-18 05:34:33 +000071 // FrameworkName = "/System/Library/Frameworks/"
Chris Lattnerb201d9b2006-10-30 05:09:49 +000072 SmallString<1024> FrameworkName;
73 FrameworkName += Dir->getName();
74 if (FrameworkName.empty() || FrameworkName.back() != '/')
75 FrameworkName.push_back('/');
Chris Lattner59a9ebd2006-10-18 05:34:33 +000076
77 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnerb201d9b2006-10-30 05:09:49 +000078 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000079
80 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
81 FrameworkName += ".framework/";
Chris Lattner5ed76da2006-10-22 07:24:13 +000082
83 if (CacheLookup == 0) {
84 ++NumFrameworkLookups;
85
86 // If the framework dir doesn't exist, we fail.
Chris Lattnerb201d9b2006-10-30 05:09:49 +000087 if (!sys::Path(std::string(FrameworkName.begin(),
88 FrameworkName.end())).exists())
Chris Lattner5ed76da2006-10-22 07:24:13 +000089 return 0;
90
91 // Otherwise, if it does, remember that this is the right direntry for this
92 // framework.
93 CacheLookup = Dir;
94 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +000095
Chris Lattner59a9ebd2006-10-18 05:34:33 +000096 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +000097 unsigned OrigSize = FrameworkName.size();
98
99 FrameworkName += "Headers/";
100 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
101 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.begin(),
102 FrameworkName.end())) {
Chris Lattner577377e2006-10-20 04:55:45 +0000103 return FE;
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000104 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000105
106 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000107 const char *Private = "Private";
108 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
109 Private+strlen(Private));
110 return FileMgr.getFile(FrameworkName.begin(), FrameworkName.end());
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000111}
112
113/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
114/// return null on failure. isAngled indicates whether the file reference is
115/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
116/// non-null, indicates where the #including file is, in case a relative search
117/// is needed.
118const FileEntry *HeaderSearch::LookupFile(const std::string &Filename,
119 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.
126 if (Filename[0] == '/') {
127 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.
133 return FileMgr.getFile(Filename);
134 }
135
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000136 SmallString<1024> TmpDir;
137
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('/');
145 TmpDir.append(Filename.begin(), Filename.end());
146 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
166 // Check each directory in sequence to see if it contains this file.
167 for (; i != SearchDirs.size(); ++i) {
Chris Lattner577377e2006-10-20 04:55:45 +0000168 const FileEntry *FE = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000169 if (!SearchDirs[i].isFramework()) {
170 // FIXME: Portability. Adding file to dir should be in sys::Path.
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000171 // Concatenate the requested file onto the directory.
172 TmpDir.clear();
173 TmpDir += SearchDirs[i].getDir()->getName();
174 TmpDir.push_back('/');
175 TmpDir.append(Filename.begin(), Filename.end());
176 FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end());
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000177 } else {
Chris Lattner641a0be2006-10-20 06:23:14 +0000178 FE = DoFrameworkLookup(SearchDirs[i].getDir(), Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000179 }
180
Chris Lattner577377e2006-10-20 04:55:45 +0000181 if (FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000182 CurDir = &SearchDirs[i];
183
184 // This file is a system header or C++ unfriendly if the dir is.
185 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
186 return FE;
187 }
188 }
189
190 // Otherwise, didn't find it.
191 return 0;
192}
193
Chris Lattner63dd32b2006-10-20 04:42:40 +0000194/// LookupSubframeworkHeader - Look up a subframework for the specified
195/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
196/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
197/// is a subframework within Carbon.framework. If so, return the FileEntry
198/// for the designated file, otherwise return null.
199const FileEntry *HeaderSearch::
200LookupSubframeworkHeader(const std::string &Filename,
201 const FileEntry *ContextFileEnt) {
202 // Framework names must have a '/' in the filename. Find it.
203 std::string::size_type SlashPos = Filename.find('/');
204 if (SlashPos == std::string::npos) return 0;
205
Chris Lattner63dd32b2006-10-20 04:42:40 +0000206 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000207 const char *ContextName = ContextFileEnt->getName();
208
Chris Lattner63dd32b2006-10-20 04:42:40 +0000209 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000210 const char *FrameworkPos = strstr(ContextName, ".framework/");
211 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000212 return 0;
213
Chris Lattner43fd42e2006-10-30 03:40:58 +0000214 SmallString<1024> FrameworkName(ContextName,
215 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000216
Chris Lattner63dd32b2006-10-20 04:42:40 +0000217 // Append Frameworks/HIToolbox.framework/
218 FrameworkName += "Frameworks/";
Chris Lattner43fd42e2006-10-30 03:40:58 +0000219 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000220 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000221
Chris Lattner5ed76da2006-10-22 07:24:13 +0000222 const DirectoryEntry *&CacheLookup =
Chris Lattneree7bf892006-10-30 05:19:23 +0000223 FrameworkMap.GetOrCreateValue(&Filename[0], &Filename[0]+SlashPos);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000224
225 // Some other location?
Chris Lattner43fd42e2006-10-30 03:40:58 +0000226 if (CacheLookup && strcmp(CacheLookup->getName(), FrameworkName.c_str()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000227 return 0;
228
229 // Cache subframework.
230 if (CacheLookup == 0) {
231 ++NumSubFrameworkLookups;
232
233 // If the framework dir doesn't exist, we fail.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000234 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
235 FrameworkName.end());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000236 if (Dir == 0) return 0;
237
238 // Otherwise, if it does, remember that this is the right direntry for this
239 // framework.
240 CacheLookup = Dir;
241 }
242
Chris Lattner577377e2006-10-20 04:55:45 +0000243 const FileEntry *FE = 0;
244
Chris Lattner63dd32b2006-10-20 04:42:40 +0000245 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000246 SmallString<1024> HeadersFilename(FrameworkName);
247 HeadersFilename += "Headers/";
248 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
249 if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
250 HeadersFilename.end()))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000251
252 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000253 HeadersFilename = FrameworkName;
254 HeadersFilename += "PrivateHeaders/";
255 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
256 if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000257 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000258 }
259
Chris Lattner577377e2006-10-20 04:55:45 +0000260 // This file is a system header or C++ unfriendly if the old file is.
261 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
262 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000263}
264
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000265//===----------------------------------------------------------------------===//
266// File Info Management.
267//===----------------------------------------------------------------------===//
268
269
270/// getFileInfo - Return the PerFileInfo structure for the specified
271/// FileEntry.
272HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
273 if (FE->getUID() >= FileInfo.size())
274 FileInfo.resize(FE->getUID()+1);
275 return FileInfo[FE->getUID()];
276}
277
278/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
279/// #include, #include_next, or #import directive. Return false if #including
280/// the file will have no effect or true if we should include it.
281bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
282 ++NumIncluded; // Count # of attempted #includes.
283
284 // Get information about this file.
285 PerFileInfo &FileInfo = getFileInfo(File);
286
287 // If this is a #import directive, check that we have not already imported
288 // this header.
289 if (isImport) {
290 // If this has already been imported, don't import it again.
291 FileInfo.isImport = true;
292
293 // Has this already been #import'ed or #include'd?
294 if (FileInfo.NumIncludes) return false;
295 } else {
296 // Otherwise, if this is a #include of a file that was previously #import'd
297 // or if this is the second #include of a #pragma once file, ignore it.
298 if (FileInfo.isImport)
299 return false;
300 }
301
302 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
303 // if the macro that guards it is defined, we know the #include has no effect.
304 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
305 ++NumMultiIncludeFileOptzn;
306 return false;
307 }
308
309 // Increment the number of times this file has been included.
310 ++FileInfo.NumIncludes;
311
312 return true;
313}
314
315