blob: 75e09e00559077b643267b6d52c5788f81f07f9e [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
129 // Step #0, unless disabled, check to see if the file is in the #includer's
130 // directory. This search is not done for <> headers.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000131 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000132 // Concatenate the requested file onto the directory.
133 // FIXME: Portability. Filename concatenation should be in sys::Path.
Chris Lattnerffda8962006-10-27 05:15:55 +0000134 std::string Name = CurFileEnt->getDir()->getName();
135 if (const FileEntry *FE = FileMgr.getFile(Name+"/"+Filename)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000136 // Leave CurDir unset.
137
138 // This file is a system header or C++ unfriendly if the old file is.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000139 getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000140 return FE;
141 }
142 }
143
144 CurDir = 0;
145
146 // If this is a system #include, ignore the user #include locs.
147 unsigned i = isAngled ? SystemDirIdx : 0;
148
149 // If this is a #include_next request, start searching after the directory the
150 // file was found in.
151 if (FromDir)
152 i = FromDir-&SearchDirs[0];
153
154 // Check each directory in sequence to see if it contains this file.
155 for (; i != SearchDirs.size(); ++i) {
156 // Concatenate the requested file onto the directory.
157 std::string SearchDir;
158
Chris Lattner577377e2006-10-20 04:55:45 +0000159 const FileEntry *FE = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000160 if (!SearchDirs[i].isFramework()) {
161 // FIXME: Portability. Adding file to dir should be in sys::Path.
Chris Lattnerffda8962006-10-27 05:15:55 +0000162 std::string Name = SearchDirs[i].getDir()->getName();
163 FE = FileMgr.getFile(Name+"/"+Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000164 } else {
Chris Lattner641a0be2006-10-20 06:23:14 +0000165 FE = DoFrameworkLookup(SearchDirs[i].getDir(), Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000166 }
167
Chris Lattner577377e2006-10-20 04:55:45 +0000168 if (FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000169 CurDir = &SearchDirs[i];
170
171 // This file is a system header or C++ unfriendly if the dir is.
172 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
173 return FE;
174 }
175 }
176
177 // Otherwise, didn't find it.
178 return 0;
179}
180
Chris Lattner63dd32b2006-10-20 04:42:40 +0000181/// LookupSubframeworkHeader - Look up a subframework for the specified
182/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
183/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
184/// is a subframework within Carbon.framework. If so, return the FileEntry
185/// for the designated file, otherwise return null.
186const FileEntry *HeaderSearch::
187LookupSubframeworkHeader(const std::string &Filename,
188 const FileEntry *ContextFileEnt) {
189 // Framework names must have a '/' in the filename. Find it.
190 std::string::size_type SlashPos = Filename.find('/');
191 if (SlashPos == std::string::npos) return 0;
192
Chris Lattner63dd32b2006-10-20 04:42:40 +0000193 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000194 const char *ContextName = ContextFileEnt->getName();
195
Chris Lattner63dd32b2006-10-20 04:42:40 +0000196 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000197 const char *FrameworkPos = strstr(ContextName, ".framework/");
198 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000199 return 0;
200
Chris Lattner43fd42e2006-10-30 03:40:58 +0000201 SmallString<1024> FrameworkName(ContextName,
202 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000203
Chris Lattner63dd32b2006-10-20 04:42:40 +0000204 // Append Frameworks/HIToolbox.framework/
205 FrameworkName += "Frameworks/";
Chris Lattner43fd42e2006-10-30 03:40:58 +0000206 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000207 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000208
Chris Lattner5ed76da2006-10-22 07:24:13 +0000209 const DirectoryEntry *&CacheLookup =
210 FrameworkMap[std::string(Filename.begin(), Filename.begin()+SlashPos)];
211
212 // Some other location?
Chris Lattner43fd42e2006-10-30 03:40:58 +0000213 if (CacheLookup && strcmp(CacheLookup->getName(), FrameworkName.c_str()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000214 return 0;
215
216 // Cache subframework.
217 if (CacheLookup == 0) {
218 ++NumSubFrameworkLookups;
219
220 // If the framework dir doesn't exist, we fail.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000221 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
222 FrameworkName.end());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000223 if (Dir == 0) return 0;
224
225 // Otherwise, if it does, remember that this is the right direntry for this
226 // framework.
227 CacheLookup = Dir;
228 }
229
Chris Lattner577377e2006-10-20 04:55:45 +0000230 const FileEntry *FE = 0;
231
Chris Lattner63dd32b2006-10-20 04:42:40 +0000232 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000233 SmallString<1024> HeadersFilename(FrameworkName);
234 HeadersFilename += "Headers/";
235 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
236 if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
237 HeadersFilename.end()))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000238
239 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000240 HeadersFilename = FrameworkName;
241 HeadersFilename += "PrivateHeaders/";
242 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
243 if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000244 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000245 }
246
Chris Lattner577377e2006-10-20 04:55:45 +0000247 // This file is a system header or C++ unfriendly if the old file is.
248 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
249 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000250}
251
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000252//===----------------------------------------------------------------------===//
253// File Info Management.
254//===----------------------------------------------------------------------===//
255
256
257/// getFileInfo - Return the PerFileInfo structure for the specified
258/// FileEntry.
259HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
260 if (FE->getUID() >= FileInfo.size())
261 FileInfo.resize(FE->getUID()+1);
262 return FileInfo[FE->getUID()];
263}
264
265/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
266/// #include, #include_next, or #import directive. Return false if #including
267/// the file will have no effect or true if we should include it.
268bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
269 ++NumIncluded; // Count # of attempted #includes.
270
271 // Get information about this file.
272 PerFileInfo &FileInfo = getFileInfo(File);
273
274 // If this is a #import directive, check that we have not already imported
275 // this header.
276 if (isImport) {
277 // If this has already been imported, don't import it again.
278 FileInfo.isImport = true;
279
280 // Has this already been #import'ed or #include'd?
281 if (FileInfo.NumIncludes) return false;
282 } else {
283 // Otherwise, if this is a #include of a file that was previously #import'd
284 // or if this is the second #include of a #pragma once file, ignore it.
285 if (FileInfo.isImport)
286 return false;
287 }
288
289 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
290 // if the macro that guards it is defined, we know the #include has no effect.
291 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
292 ++NumMultiIncludeFileOptzn;
293 return false;
294 }
295
296 // Increment the number of times this file has been included.
297 ++FileInfo.NumIncludes;
298
299 return true;
300}
301
302