blob: 8b7d63995b835aa0ad60b83fd36f68fa281ebca8 [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"
18#include <iostream>
19using namespace llvm;
20using namespace clang;
21
Chris Lattner641a0be2006-10-20 06:23:14 +000022HeaderSearch::HeaderSearch(FileManager &FM) : FileMgr(FM) {
23 SystemDirIdx = 0;
24 NoCurDirSearch = false;
25
26 NumIncluded = 0;
27 NumMultiIncludeFileOptzn = 0;
28 NumFrameworkLookups = NumSubFrameworkLookups = 0;
29}
30
Chris Lattner59a9ebd2006-10-18 05:34:33 +000031void HeaderSearch::PrintStats() {
32 std::cerr << "\n*** HeaderSearch Stats:\n";
33 std::cerr << FileInfo.size() << " files tracked.\n";
34 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
35 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
36 NumOnceOnlyFiles += FileInfo[i].isImport;
37 if (MaxNumIncludes < FileInfo[i].NumIncludes)
38 MaxNumIncludes = FileInfo[i].NumIncludes;
39 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
40 }
41 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
42 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
43 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
44
45 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
46 std::cerr << " " << NumMultiIncludeFileOptzn << " #includes skipped due to"
Chris Lattner63dd32b2006-10-20 04:42:40 +000047 << " the multi-include optimization.\n";
Chris Lattner641a0be2006-10-20 06:23:14 +000048
49 std::cerr << NumFrameworkLookups << " framework lookups.\n";
50 std::cerr << NumSubFrameworkLookups << " subframework lookups.\n";
Chris Lattner59a9ebd2006-10-18 05:34:33 +000051}
52
53//===----------------------------------------------------------------------===//
54// Header File Location.
55//===----------------------------------------------------------------------===//
56
Chris Lattner641a0be2006-10-20 06:23:14 +000057const FileEntry *HeaderSearch::DoFrameworkLookup(const DirectoryEntry *Dir,
58 const std::string &Filename) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +000059 // Framework names must have a '/' in the filename.
60 std::string::size_type SlashPos = Filename.find('/');
Chris Lattner577377e2006-10-20 04:55:45 +000061 if (SlashPos == std::string::npos) return 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000062
Chris Lattner641a0be2006-10-20 06:23:14 +000063 // TODO: caching.
64 ++NumFrameworkLookups;
65
Chris Lattner59a9ebd2006-10-18 05:34:33 +000066 // FrameworkName = "/System/Library/Frameworks/"
67 std::string FrameworkName = Dir->getName();
68 if (FrameworkName.empty() || FrameworkName[FrameworkName.size()-1] != '/')
69 FrameworkName += '/';
70
71 // FrameworkName = "/System/Library/Frameworks/Cocoa"
72 FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos);
73
74 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
75 FrameworkName += ".framework/";
76
Chris Lattner59a9ebd2006-10-18 05:34:33 +000077 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
78 std::string HeadersFilename = FrameworkName + "Headers/" +
79 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner641a0be2006-10-20 06:23:14 +000080 if (const FileEntry *FE = FileMgr.getFile(HeadersFilename))
Chris Lattner577377e2006-10-20 04:55:45 +000081 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000082
83 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
84 std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" +
85 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner641a0be2006-10-20 06:23:14 +000086 return FileMgr.getFile(PrivateHeadersFilename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000087}
88
89/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
90/// return null on failure. isAngled indicates whether the file reference is
91/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
92/// non-null, indicates where the #including file is, in case a relative search
93/// is needed.
94const FileEntry *HeaderSearch::LookupFile(const std::string &Filename,
95 bool isAngled,
96 const DirectoryLookup *FromDir,
97 const DirectoryLookup *&CurDir,
98 const FileEntry *CurFileEnt) {
99 // If 'Filename' is absolute, check to see if it exists and no searching.
100 // FIXME: Portability. This should be a sys::Path interface, this doesn't
101 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
102 if (Filename[0] == '/') {
103 CurDir = 0;
104
105 // If this was an #include_next "/absolute/file", fail.
106 if (FromDir) return 0;
107
108 // Otherwise, just return the file.
109 return FileMgr.getFile(Filename);
110 }
111
112 // Step #0, unless disabled, check to see if the file is in the #includer's
113 // directory. This search is not done for <> headers.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000114 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000115 // Concatenate the requested file onto the directory.
116 // FIXME: Portability. Filename concatenation should be in sys::Path.
117 if (const FileEntry *FE =
118 FileMgr.getFile(CurFileEnt->getDir()->getName()+"/"+Filename)) {
119 // Leave CurDir unset.
120
121 // This file is a system header or C++ unfriendly if the old file is.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000122 getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000123 return FE;
124 }
125 }
126
127 CurDir = 0;
128
129 // If this is a system #include, ignore the user #include locs.
130 unsigned i = isAngled ? SystemDirIdx : 0;
131
132 // If this is a #include_next request, start searching after the directory the
133 // file was found in.
134 if (FromDir)
135 i = FromDir-&SearchDirs[0];
136
137 // Check each directory in sequence to see if it contains this file.
138 for (; i != SearchDirs.size(); ++i) {
139 // Concatenate the requested file onto the directory.
140 std::string SearchDir;
141
Chris Lattner577377e2006-10-20 04:55:45 +0000142 const FileEntry *FE = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000143 if (!SearchDirs[i].isFramework()) {
144 // FIXME: Portability. Adding file to dir should be in sys::Path.
Chris Lattner641a0be2006-10-20 06:23:14 +0000145 FE = FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000146 } else {
Chris Lattner641a0be2006-10-20 06:23:14 +0000147 FE = DoFrameworkLookup(SearchDirs[i].getDir(), Filename);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000148 }
149
Chris Lattner577377e2006-10-20 04:55:45 +0000150 if (FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000151 CurDir = &SearchDirs[i];
152
153 // This file is a system header or C++ unfriendly if the dir is.
154 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
155 return FE;
156 }
157 }
158
159 // Otherwise, didn't find it.
160 return 0;
161}
162
Chris Lattner63dd32b2006-10-20 04:42:40 +0000163/// LookupSubframeworkHeader - Look up a subframework for the specified
164/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
165/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
166/// is a subframework within Carbon.framework. If so, return the FileEntry
167/// for the designated file, otherwise return null.
168const FileEntry *HeaderSearch::
169LookupSubframeworkHeader(const std::string &Filename,
170 const FileEntry *ContextFileEnt) {
171 // Framework names must have a '/' in the filename. Find it.
172 std::string::size_type SlashPos = Filename.find('/');
173 if (SlashPos == std::string::npos) return 0;
174
175 // TODO: Cache subframework.
Chris Lattner641a0be2006-10-20 06:23:14 +0000176 ++NumSubFrameworkLookups;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000177
178 // Look up the base framework name of the ContextFileEnt.
179 const std::string &ContextName = ContextFileEnt->getName();
180 std::string::size_type FrameworkPos = ContextName.find(".framework/");
181 // If the context info wasn't a framework, couldn't be a subframework.
182 if (FrameworkPos == std::string::npos)
183 return 0;
184
185 std::string FrameworkName(ContextName.begin(),
186 ContextName.begin()+FrameworkPos+strlen(".framework/"));
187 // Append Frameworks/HIToolbox.framework/
188 FrameworkName += "Frameworks/";
189 FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos);
190 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000191
192 const FileEntry *FE = 0;
193
Chris Lattner63dd32b2006-10-20 04:42:40 +0000194 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
195 std::string HeadersFilename = FrameworkName + "Headers/" +
196 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner577377e2006-10-20 04:55:45 +0000197 if (!(FE = FileMgr.getFile(HeadersFilename))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000198
199 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
200 std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" +
201 std::string(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner577377e2006-10-20 04:55:45 +0000202 if (!(FE = FileMgr.getFile(PrivateHeadersFilename)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000203 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000204 }
205
Chris Lattner577377e2006-10-20 04:55:45 +0000206 // This file is a system header or C++ unfriendly if the old file is.
207 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
208 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000209}
210
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000211//===----------------------------------------------------------------------===//
212// File Info Management.
213//===----------------------------------------------------------------------===//
214
215
216/// getFileInfo - Return the PerFileInfo structure for the specified
217/// FileEntry.
218HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
219 if (FE->getUID() >= FileInfo.size())
220 FileInfo.resize(FE->getUID()+1);
221 return FileInfo[FE->getUID()];
222}
223
224/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
225/// #include, #include_next, or #import directive. Return false if #including
226/// the file will have no effect or true if we should include it.
227bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
228 ++NumIncluded; // Count # of attempted #includes.
229
230 // Get information about this file.
231 PerFileInfo &FileInfo = getFileInfo(File);
232
233 // If this is a #import directive, check that we have not already imported
234 // this header.
235 if (isImport) {
236 // If this has already been imported, don't import it again.
237 FileInfo.isImport = true;
238
239 // Has this already been #import'ed or #include'd?
240 if (FileInfo.NumIncludes) return false;
241 } else {
242 // Otherwise, if this is a #include of a file that was previously #import'd
243 // or if this is the second #include of a #pragma once file, ignore it.
244 if (FileInfo.isImport)
245 return false;
246 }
247
248 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
249 // if the macro that guards it is defined, we know the #include has no effect.
250 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
251 ++NumMultiIncludeFileOptzn;
252 return false;
253 }
254
255 // Increment the number of times this file has been included.
256 ++FileInfo.NumIncludes;
257
258 return true;
259}
260
261