blob: 6e7551e8f0ae39667516cd3729105ca69da275f9 [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 =
65 FrameworkMap[std::string(Filename.begin(), Filename.begin()+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/"
72 std::string FrameworkName = Dir->getName();
73 if (FrameworkName.empty() || FrameworkName[FrameworkName.size()-1] != '/')
74 FrameworkName += '/';
75
76 // FrameworkName = "/System/Library/Frameworks/Cocoa"
77 FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos);
78
79 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
80 FrameworkName += ".framework/";
Chris Lattner5ed76da2006-10-22 07:24:13 +000081
82 if (CacheLookup == 0) {
83 ++NumFrameworkLookups;
84
85 // If the framework dir doesn't exist, we fail.
86 if (!sys::Path(FrameworkName).exists())
87 return 0;
88
89 // Otherwise, if it does, remember that this is the right direntry for this
90 // framework.
91 CacheLookup = Dir;
92 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +000093
Chris Lattner59a9ebd2006-10-18 05:34:33 +000094 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
95 std::string HeadersFilename = FrameworkName + "Headers/" +
96 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner641a0be2006-10-20 06:23:14 +000097 if (const FileEntry *FE = FileMgr.getFile(HeadersFilename))
Chris Lattner577377e2006-10-20 04:55:45 +000098 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000099
100 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
101 std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" +
102 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner641a0be2006-10-20 06:23:14 +0000103 return FileMgr.getFile(PrivateHeadersFilename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000104}
105
106/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
107/// return null on failure. isAngled indicates whether the file reference is
108/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
109/// non-null, indicates where the #including file is, in case a relative search
110/// is needed.
111const FileEntry *HeaderSearch::LookupFile(const std::string &Filename,
112 bool isAngled,
113 const DirectoryLookup *FromDir,
114 const DirectoryLookup *&CurDir,
115 const FileEntry *CurFileEnt) {
116 // If 'Filename' is absolute, check to see if it exists and no searching.
117 // FIXME: Portability. This should be a sys::Path interface, this doesn't
118 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
119 if (Filename[0] == '/') {
120 CurDir = 0;
121
122 // If this was an #include_next "/absolute/file", fail.
123 if (FromDir) return 0;
124
125 // Otherwise, just return the file.
126 return FileMgr.getFile(Filename);
127 }
128
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000129 SmallString<1024> TmpDir;
130
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000131 // Step #0, unless disabled, check to see if the file is in the #includer's
132 // directory. This search is not done for <> headers.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000133 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000134 // Concatenate the requested file onto the directory.
135 // FIXME: Portability. Filename concatenation should be in sys::Path.
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000136 TmpDir += CurFileEnt->getDir()->getName();
137 TmpDir.push_back('/');
138 TmpDir.append(Filename.begin(), Filename.end());
139 if (const FileEntry *FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end())) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000140 // Leave CurDir unset.
141
142 // This file is a system header or C++ unfriendly if the old file is.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000143 getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000144 return FE;
145 }
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000146 TmpDir.clear();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000147 }
148
149 CurDir = 0;
150
151 // If this is a system #include, ignore the user #include locs.
152 unsigned i = isAngled ? SystemDirIdx : 0;
153
154 // If this is a #include_next request, start searching after the directory the
155 // file was found in.
156 if (FromDir)
157 i = FromDir-&SearchDirs[0];
158
159 // Check each directory in sequence to see if it contains this file.
160 for (; i != SearchDirs.size(); ++i) {
Chris Lattner577377e2006-10-20 04:55:45 +0000161 const FileEntry *FE = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000162 if (!SearchDirs[i].isFramework()) {
163 // FIXME: Portability. Adding file to dir should be in sys::Path.
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000164 // Concatenate the requested file onto the directory.
165 TmpDir.clear();
166 TmpDir += SearchDirs[i].getDir()->getName();
167 TmpDir.push_back('/');
168 TmpDir.append(Filename.begin(), Filename.end());
169 FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end());
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000170 } else {
Chris Lattner641a0be2006-10-20 06:23:14 +0000171 FE = DoFrameworkLookup(SearchDirs[i].getDir(), Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000172 }
173
Chris Lattner577377e2006-10-20 04:55:45 +0000174 if (FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000175 CurDir = &SearchDirs[i];
176
177 // This file is a system header or C++ unfriendly if the dir is.
178 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
179 return FE;
180 }
181 }
182
183 // Otherwise, didn't find it.
184 return 0;
185}
186
Chris Lattner63dd32b2006-10-20 04:42:40 +0000187/// LookupSubframeworkHeader - Look up a subframework for the specified
188/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
189/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
190/// is a subframework within Carbon.framework. If so, return the FileEntry
191/// for the designated file, otherwise return null.
192const FileEntry *HeaderSearch::
193LookupSubframeworkHeader(const std::string &Filename,
194 const FileEntry *ContextFileEnt) {
195 // Framework names must have a '/' in the filename. Find it.
196 std::string::size_type SlashPos = Filename.find('/');
197 if (SlashPos == std::string::npos) return 0;
198
Chris Lattner63dd32b2006-10-20 04:42:40 +0000199 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000200 const char *ContextName = ContextFileEnt->getName();
201
Chris Lattner63dd32b2006-10-20 04:42:40 +0000202 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000203 const char *FrameworkPos = strstr(ContextName, ".framework/");
204 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000205 return 0;
206
Chris Lattner43fd42e2006-10-30 03:40:58 +0000207 SmallString<1024> FrameworkName(ContextName,
208 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000209
Chris Lattner63dd32b2006-10-20 04:42:40 +0000210 // Append Frameworks/HIToolbox.framework/
211 FrameworkName += "Frameworks/";
Chris Lattner43fd42e2006-10-30 03:40:58 +0000212 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000213 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000214
Chris Lattner5ed76da2006-10-22 07:24:13 +0000215 const DirectoryEntry *&CacheLookup =
216 FrameworkMap[std::string(Filename.begin(), Filename.begin()+SlashPos)];
217
218 // Some other location?
Chris Lattner43fd42e2006-10-30 03:40:58 +0000219 if (CacheLookup && strcmp(CacheLookup->getName(), FrameworkName.c_str()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000220 return 0;
221
222 // Cache subframework.
223 if (CacheLookup == 0) {
224 ++NumSubFrameworkLookups;
225
226 // If the framework dir doesn't exist, we fail.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000227 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
228 FrameworkName.end());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000229 if (Dir == 0) return 0;
230
231 // Otherwise, if it does, remember that this is the right direntry for this
232 // framework.
233 CacheLookup = Dir;
234 }
235
Chris Lattner577377e2006-10-20 04:55:45 +0000236 const FileEntry *FE = 0;
237
Chris Lattner63dd32b2006-10-20 04:42:40 +0000238 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000239 SmallString<1024> HeadersFilename(FrameworkName);
240 HeadersFilename += "Headers/";
241 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
242 if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
243 HeadersFilename.end()))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000244
245 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000246 HeadersFilename = FrameworkName;
247 HeadersFilename += "PrivateHeaders/";
248 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
249 if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000250 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000251 }
252
Chris Lattner577377e2006-10-20 04:55:45 +0000253 // This file is a system header or C++ unfriendly if the old file is.
254 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
255 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000256}
257
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000258//===----------------------------------------------------------------------===//
259// File Info Management.
260//===----------------------------------------------------------------------===//
261
262
263/// getFileInfo - Return the PerFileInfo structure for the specified
264/// FileEntry.
265HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
266 if (FE->getUID() >= FileInfo.size())
267 FileInfo.resize(FE->getUID()+1);
268 return FileInfo[FE->getUID()];
269}
270
271/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
272/// #include, #include_next, or #import directive. Return false if #including
273/// the file will have no effect or true if we should include it.
274bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
275 ++NumIncluded; // Count # of attempted #includes.
276
277 // Get information about this file.
278 PerFileInfo &FileInfo = getFileInfo(File);
279
280 // If this is a #import directive, check that we have not already imported
281 // this header.
282 if (isImport) {
283 // If this has already been imported, don't import it again.
284 FileInfo.isImport = true;
285
286 // Has this already been #import'ed or #include'd?
287 if (FileInfo.NumIncludes) return false;
288 } else {
289 // Otherwise, if this is a #include of a file that was previously #import'd
290 // or if this is the second #include of a #pragma once file, ignore it.
291 if (FileInfo.isImport)
292 return false;
293 }
294
295 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
296 // if the macro that guards it is defined, we know the #include has no effect.
297 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
298 ++NumMultiIncludeFileOptzn;
299 return false;
300 }
301
302 // Increment the number of times this file has been included.
303 ++FileInfo.NumIncludes;
304
305 return true;
306}
307
308