blob: 769b1682de0ed3de7b6bcdcea6768ed30cff31b7 [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
Chris Lattner59a9ebd2006-10-18 05:34:33 +000014#include "clang/Lex/HeaderSearch.h"
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000015#include "clang/Lex/HeaderMap.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/IdentifierTable.h"
Chris Lattner59a9ebd2006-10-18 05:34:33 +000018#include "llvm/System/Path.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +000019#include "llvm/ADT/SmallString.h"
Chris Lattner59a9ebd2006-10-18 05:34:33 +000020using namespace clang;
21
Chris Lattner23d89032007-04-03 22:14:25 +000022HeaderSearch::HeaderSearch(FileManager &FM) : FileMgr(FM), FrameworkMap(64) {
Chris Lattner641a0be2006-10-20 06:23:14 +000023 SystemDirIdx = 0;
24 NoCurDirSearch = false;
25
26 NumIncluded = 0;
27 NumMultiIncludeFileOptzn = 0;
28 NumFrameworkLookups = NumSubFrameworkLookups = 0;
29}
30
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000031HeaderSearch::~HeaderSearch() {
32 // Delete headermaps.
33 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
34 delete HeaderMaps[i].second;
35}
36
Chris Lattner59a9ebd2006-10-18 05:34:33 +000037void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000038 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
39 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000040 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
41 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
42 NumOnceOnlyFiles += FileInfo[i].isImport;
43 if (MaxNumIncludes < FileInfo[i].NumIncludes)
44 MaxNumIncludes = FileInfo[i].NumIncludes;
45 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
46 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000047 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
48 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
49 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000050
Chris Lattner23b7eb62007-06-15 23:05:46 +000051 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
52 fprintf(stderr, " %d #includes skipped due to"
53 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Chris Lattner641a0be2006-10-20 06:23:14 +000054
Chris Lattner23b7eb62007-06-15 23:05:46 +000055 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
56 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000057}
58
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000059/// CreateHeaderMap - This method returns a HeaderMap for the specified
60/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
61const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE,
62 std::string &ErrorInfo) {
63 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerf62f7582007-12-17 07:52:39 +000064 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000065 if (!HeaderMaps.empty()) {
66 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerf62f7582007-12-17 07:52:39 +000067 // Pointer equality comparison of FileEntries works because they are
68 // already uniqued by inode.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000069 if (HeaderMaps[i].first == FE)
70 return HeaderMaps[i].second;
71 }
72
73 if (const HeaderMap *HM = HeaderMap::Create(FE, ErrorInfo)) {
74 HeaderMaps.push_back(std::make_pair(FE, HM));
75 return HM;
76 }
77
78 return 0;
79}
80
Chris Lattnerf62f7582007-12-17 07:52:39 +000081//===----------------------------------------------------------------------===//
82// File lookup within a DirectoryLookup scope
83//===----------------------------------------------------------------------===//
84
85/// LookupFile - Lookup the specified file in this search path, returning it
86/// if it exists or returning null if not.
87const FileEntry *DirectoryLookup::LookupFile(const char *FilenameStart,
88 const char *FilenameEnd,
89 FileManager &FileMgr) const {
90 llvm::SmallString<1024> TmpDir;
91
92 // Concatenate the requested file onto the directory.
93 // FIXME: Portability. Filename concatenation should be in sys::Path.
94 TmpDir += getDir()->getName();
95 TmpDir.push_back('/');
96 TmpDir.append(FilenameStart, FilenameEnd);
97 return FileMgr.getFile(TmpDir.begin(), TmpDir.end());
98}
99
100
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000101
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000102//===----------------------------------------------------------------------===//
103// Header File Location.
104//===----------------------------------------------------------------------===//
105
Chris Lattner641a0be2006-10-20 06:23:14 +0000106const FileEntry *HeaderSearch::DoFrameworkLookup(const DirectoryEntry *Dir,
Chris Lattner7cdbad92006-10-30 05:33:15 +0000107 const char *FilenameStart,
108 const char *FilenameEnd) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000109 // Framework names must have a '/' in the filename.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000110 const char *SlashPos = std::find(FilenameStart, FilenameEnd, '/');
111 if (SlashPos == FilenameEnd) return 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000112
Chris Lattner23b7eb62007-06-15 23:05:46 +0000113 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner7cdbad92006-10-30 05:33:15 +0000114 FrameworkMap.GetOrCreateValue(FilenameStart, SlashPos);
Chris Lattner641a0be2006-10-20 06:23:14 +0000115
Chris Lattner5ed76da2006-10-22 07:24:13 +0000116 // If it is some other directory, fail.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000117 if (CacheLookup.getValue() && CacheLookup.getValue() != Dir)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000118 return 0;
119
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000120 // FrameworkName = "/System/Library/Frameworks/"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000121 llvm::SmallString<1024> FrameworkName;
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000122 FrameworkName += Dir->getName();
123 if (FrameworkName.empty() || FrameworkName.back() != '/')
124 FrameworkName.push_back('/');
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000125
126 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattner7cdbad92006-10-30 05:33:15 +0000127 FrameworkName.append(FilenameStart, SlashPos);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000128
129 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
130 FrameworkName += ".framework/";
Chris Lattner5ed76da2006-10-22 07:24:13 +0000131
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000132 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000133 ++NumFrameworkLookups;
134
135 // If the framework dir doesn't exist, we fail.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000136 if (!llvm::sys::Path(std::string(FrameworkName.begin(),
137 FrameworkName.end())).exists())
Chris Lattner5ed76da2006-10-22 07:24:13 +0000138 return 0;
139
140 // Otherwise, if it does, remember that this is the right direntry for this
141 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000142 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000143 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000144
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000145 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000146 unsigned OrigSize = FrameworkName.size();
147
148 FrameworkName += "Headers/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000149 FrameworkName.append(SlashPos+1, FilenameEnd);
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000150 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.begin(),
151 FrameworkName.end())) {
Chris Lattner577377e2006-10-20 04:55:45 +0000152 return FE;
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000153 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000154
155 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000156 const char *Private = "Private";
157 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
158 Private+strlen(Private));
159 return FileMgr.getFile(FrameworkName.begin(), FrameworkName.end());
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000160}
161
Chris Lattnerf62f7582007-12-17 07:52:39 +0000162
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000163/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
164/// return null on failure. isAngled indicates whether the file reference is
165/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
166/// non-null, indicates where the #including file is, in case a relative search
167/// is needed.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000168const FileEntry *HeaderSearch::LookupFile(const char *FilenameStart,
169 const char *FilenameEnd,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000170 bool isAngled,
171 const DirectoryLookup *FromDir,
172 const DirectoryLookup *&CurDir,
173 const FileEntry *CurFileEnt) {
174 // If 'Filename' is absolute, check to see if it exists and no searching.
175 // FIXME: Portability. This should be a sys::Path interface, this doesn't
176 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000177 if (FilenameStart[0] == '/') {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000178 CurDir = 0;
179
180 // If this was an #include_next "/absolute/file", fail.
181 if (FromDir) return 0;
182
183 // Otherwise, just return the file.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000184 return FileMgr.getFile(FilenameStart, FilenameEnd);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000185 }
186
187 // Step #0, unless disabled, check to see if the file is in the #includer's
Chris Lattnerf62f7582007-12-17 07:52:39 +0000188 // directory. This has to be based on CurFileEnt, not CurDir, because
189 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
190 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
191 // This search is not done for <> headers.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000192 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
Chris Lattnerf62f7582007-12-17 07:52:39 +0000193 llvm::SmallString<1024> TmpDir;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000194 // Concatenate the requested file onto the directory.
195 // FIXME: Portability. Filename concatenation should be in sys::Path.
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000196 TmpDir += CurFileEnt->getDir()->getName();
197 TmpDir.push_back('/');
Chris Lattner7cdbad92006-10-30 05:33:15 +0000198 TmpDir.append(FilenameStart, FilenameEnd);
Chris Lattnercf8ddac2006-10-30 04:42:33 +0000199 if (const FileEntry *FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end())) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000200 // Leave CurDir unset.
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000201 // This file is a system header or C++ unfriendly if the old file is.
Chris Lattner63dd32b2006-10-20 04:42:40 +0000202 getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000203 return FE;
204 }
205 }
206
207 CurDir = 0;
208
209 // If this is a system #include, ignore the user #include locs.
210 unsigned i = isAngled ? SystemDirIdx : 0;
211
212 // If this is a #include_next request, start searching after the directory the
213 // file was found in.
214 if (FromDir)
215 i = FromDir-&SearchDirs[0];
216
Chris Lattnerd4275422007-07-22 07:28:00 +0000217 // Cache all of the lookups performed by this method. Many headers are
218 // multiply included, and the "pragma once" optimization prevents them from
219 // being relex/pp'd, but they would still have to search through a
220 // (potentially huge) series of SearchDirs to find it.
221 std::pair<unsigned, unsigned> &CacheLookup =
222 LookupFileCache.GetOrCreateValue(FilenameStart, FilenameEnd).getValue();
223
224 // If the entry has been previously looked up, the first value will be
225 // non-zero. If the value is equal to i (the start point of our search), then
226 // this is a matching hit.
227 if (CacheLookup.first == i+1) {
228 // Skip querying potentially lots of directories for this lookup.
229 i = CacheLookup.second;
230 } else {
231 // Otherwise, this is the first query, or the previous query didn't match
232 // our search start. We will fill in our found location below, so prime the
233 // start point value.
234 CacheLookup.first = i+1;
235 }
Chris Lattnerf62f7582007-12-17 07:52:39 +0000236
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000237 // Check each directory in sequence to see if it contains this file.
238 for (; i != SearchDirs.size(); ++i) {
Chris Lattner577377e2006-10-20 04:55:45 +0000239 const FileEntry *FE = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000240 if (!SearchDirs[i].isFramework()) {
Chris Lattnerf62f7582007-12-17 07:52:39 +0000241 FE = SearchDirs[i].LookupFile(FilenameStart, FilenameEnd, FileMgr);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000242 } else {
Chris Lattnerf62f7582007-12-17 07:52:39 +0000243 FE = DoFrameworkLookup(SearchDirs[i].getFrameworkDir(),
244 FilenameStart, FilenameEnd);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000245 }
246
Chris Lattner577377e2006-10-20 04:55:45 +0000247 if (FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000248 CurDir = &SearchDirs[i];
249
250 // This file is a system header or C++ unfriendly if the dir is.
251 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Chris Lattnerd4275422007-07-22 07:28:00 +0000252
253 // Remember this location for the next lookup we do.
254 CacheLookup.second = i;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000255 return FE;
256 }
257 }
258
Chris Lattnerd4275422007-07-22 07:28:00 +0000259 // Otherwise, didn't find it. Remember we didn't find this.
260 CacheLookup.second = SearchDirs.size();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000261 return 0;
262}
263
Chris Lattner63dd32b2006-10-20 04:42:40 +0000264/// LookupSubframeworkHeader - Look up a subframework for the specified
265/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
266/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
267/// is a subframework within Carbon.framework. If so, return the FileEntry
268/// for the designated file, otherwise return null.
269const FileEntry *HeaderSearch::
Chris Lattner7cdbad92006-10-30 05:33:15 +0000270LookupSubframeworkHeader(const char *FilenameStart,
271 const char *FilenameEnd,
Chris Lattner63dd32b2006-10-20 04:42:40 +0000272 const FileEntry *ContextFileEnt) {
273 // Framework names must have a '/' in the filename. Find it.
Chris Lattner7cdbad92006-10-30 05:33:15 +0000274 const char *SlashPos = std::find(FilenameStart, FilenameEnd, '/');
275 if (SlashPos == FilenameEnd) return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000276
Chris Lattner63dd32b2006-10-20 04:42:40 +0000277 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000278 const char *ContextName = ContextFileEnt->getName();
279
Chris Lattner63dd32b2006-10-20 04:42:40 +0000280 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000281 const char *FrameworkPos = strstr(ContextName, ".framework/");
282 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000283 return 0;
284
Chris Lattner23b7eb62007-06-15 23:05:46 +0000285 llvm::SmallString<1024> FrameworkName(ContextName,
286 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000287
Chris Lattner63dd32b2006-10-20 04:42:40 +0000288 // Append Frameworks/HIToolbox.framework/
289 FrameworkName += "Frameworks/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000290 FrameworkName.append(FilenameStart, SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000291 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000292
Chris Lattner23b7eb62007-06-15 23:05:46 +0000293 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner7cdbad92006-10-30 05:33:15 +0000294 FrameworkMap.GetOrCreateValue(FilenameStart, SlashPos);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000295
296 // Some other location?
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000297 if (CacheLookup.getValue() &&
298 CacheLookup.getKeyLength() == FrameworkName.size() &&
299 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
300 CacheLookup.getKeyLength()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000301 return 0;
302
303 // Cache subframework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000304 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000305 ++NumSubFrameworkLookups;
306
307 // If the framework dir doesn't exist, we fail.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000308 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
309 FrameworkName.end());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000310 if (Dir == 0) return 0;
311
312 // Otherwise, if it does, remember that this is the right direntry for this
313 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000314 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000315 }
316
Chris Lattner577377e2006-10-20 04:55:45 +0000317 const FileEntry *FE = 0;
318
Chris Lattner63dd32b2006-10-20 04:42:40 +0000319 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000320 llvm::SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000321 HeadersFilename += "Headers/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000322 HeadersFilename.append(SlashPos+1, FilenameEnd);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000323 if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
324 HeadersFilename.end()))) {
Chris Lattner63dd32b2006-10-20 04:42:40 +0000325
326 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000327 HeadersFilename = FrameworkName;
328 HeadersFilename += "PrivateHeaders/";
Chris Lattner7cdbad92006-10-30 05:33:15 +0000329 HeadersFilename.append(SlashPos+1, FilenameEnd);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000330 if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000331 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000332 }
333
Chris Lattner577377e2006-10-20 04:55:45 +0000334 // This file is a system header or C++ unfriendly if the old file is.
335 getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
336 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000337}
338
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000339//===----------------------------------------------------------------------===//
340// File Info Management.
341//===----------------------------------------------------------------------===//
342
343
344/// getFileInfo - Return the PerFileInfo structure for the specified
345/// FileEntry.
346HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
347 if (FE->getUID() >= FileInfo.size())
348 FileInfo.resize(FE->getUID()+1);
349 return FileInfo[FE->getUID()];
350}
351
352/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
353/// #include, #include_next, or #import directive. Return false if #including
354/// the file will have no effect or true if we should include it.
355bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
356 ++NumIncluded; // Count # of attempted #includes.
357
358 // Get information about this file.
359 PerFileInfo &FileInfo = getFileInfo(File);
360
361 // If this is a #import directive, check that we have not already imported
362 // this header.
363 if (isImport) {
364 // If this has already been imported, don't import it again.
365 FileInfo.isImport = true;
366
367 // Has this already been #import'ed or #include'd?
368 if (FileInfo.NumIncludes) return false;
369 } else {
370 // Otherwise, if this is a #include of a file that was previously #import'd
371 // or if this is the second #include of a #pragma once file, ignore it.
372 if (FileInfo.isImport)
373 return false;
374 }
375
376 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
377 // if the macro that guards it is defined, we know the #include has no effect.
Chris Lattnerd7b971b2007-10-07 07:57:27 +0000378 if (FileInfo.ControllingMacro &&
379 FileInfo.ControllingMacro->hasMacroDefinition()) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000380 ++NumMultiIncludeFileOptzn;
381 return false;
382 }
383
384 // Increment the number of times this file has been included.
385 ++FileInfo.NumIncludes;
386
387 return true;
388}
389
390