HeaderSearch.cpp revision 256053b31e697fdf0cc48f17d621c82fc3b8dff0
1//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file implements the DirectoryLookup and HeaderSearch interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/HeaderSearch.h"
15#include "clang/Lex/HeaderMap.h"
16#include "clang/Basic/FileManager.h"
17#include "clang/Basic/IdentifierTable.h"
18#include "llvm/Support/Path.h"
19#include "llvm/ADT/SmallString.h"
20#include <cstdio>
21using namespace clang;
22
23const IdentifierInfo *
24HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
25  if (ControllingMacro)
26    return ControllingMacro;
27
28  if (!ControllingMacroID || !External)
29    return 0;
30
31  ControllingMacro = External->GetIdentifier(ControllingMacroID);
32  return ControllingMacro;
33}
34
35HeaderSearch::HeaderSearch(FileManager &FM)
36    : FileMgr(FM), FrameworkMap(64) {
37  SystemDirIdx = 0;
38  NoCurDirSearch = false;
39
40  ExternalLookup = 0;
41  NumIncluded = 0;
42  NumMultiIncludeFileOptzn = 0;
43  NumFrameworkLookups = NumSubFrameworkLookups = 0;
44}
45
46HeaderSearch::~HeaderSearch() {
47  // Delete headermaps.
48  for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
49    delete HeaderMaps[i].second;
50}
51
52void HeaderSearch::PrintStats() {
53  fprintf(stderr, "\n*** HeaderSearch Stats:\n");
54  fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
55  unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
56  for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
57    NumOnceOnlyFiles += FileInfo[i].isImport;
58    if (MaxNumIncludes < FileInfo[i].NumIncludes)
59      MaxNumIncludes = FileInfo[i].NumIncludes;
60    NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
61  }
62  fprintf(stderr, "  %d #import/#pragma once files.\n", NumOnceOnlyFiles);
63  fprintf(stderr, "  %d included exactly once.\n", NumSingleIncludedFiles);
64  fprintf(stderr, "  %d max times a file is included.\n", MaxNumIncludes);
65
66  fprintf(stderr, "  %d #include/#include_next/#import.\n", NumIncluded);
67  fprintf(stderr, "    %d #includes skipped due to"
68          " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
69
70  fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
71  fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
72}
73
74/// CreateHeaderMap - This method returns a HeaderMap for the specified
75/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
76const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
77  // We expect the number of headermaps to be small, and almost always empty.
78  // If it ever grows, use of a linear search should be re-evaluated.
79  if (!HeaderMaps.empty()) {
80    for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
81      // Pointer equality comparison of FileEntries works because they are
82      // already uniqued by inode.
83      if (HeaderMaps[i].first == FE)
84        return HeaderMaps[i].second;
85  }
86
87  if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
88    HeaderMaps.push_back(std::make_pair(FE, HM));
89    return HM;
90  }
91
92  return 0;
93}
94
95//===----------------------------------------------------------------------===//
96// File lookup within a DirectoryLookup scope
97//===----------------------------------------------------------------------===//
98
99/// getName - Return the directory or filename corresponding to this lookup
100/// object.
101const char *DirectoryLookup::getName() const {
102  if (isNormalDir())
103    return getDir()->getName();
104  if (isFramework())
105    return getFrameworkDir()->getName();
106  assert(isHeaderMap() && "Unknown DirectoryLookup");
107  return getHeaderMap()->getFileName();
108}
109
110
111/// LookupFile - Lookup the specified file in this search path, returning it
112/// if it exists or returning null if not.
113const FileEntry *DirectoryLookup::LookupFile(llvm::StringRef Filename,
114                                             HeaderSearch &HS) const {
115  llvm::SmallString<1024> TmpDir;
116  if (isNormalDir()) {
117    // Concatenate the requested file onto the directory.
118    // FIXME: Portability.  Filename concatenation should be in sys::Path.
119    TmpDir += getDir()->getName();
120    TmpDir.push_back('/');
121    TmpDir.append(Filename.begin(), Filename.end());
122    return HS.getFileMgr().getFile(TmpDir.str());
123  }
124
125  if (isFramework())
126    return DoFrameworkLookup(Filename, HS);
127
128  assert(isHeaderMap() && "Unknown directory lookup");
129  return getHeaderMap()->LookupFile(Filename, HS.getFileMgr());
130}
131
132
133/// DoFrameworkLookup - Do a lookup of the specified file in the current
134/// DirectoryLookup, which is a framework directory.
135const FileEntry *DirectoryLookup::DoFrameworkLookup(llvm::StringRef Filename,
136                                                    HeaderSearch &HS) const {
137  FileManager &FileMgr = HS.getFileMgr();
138
139  // Framework names must have a '/' in the filename.
140  size_t SlashPos = Filename.find('/');
141  if (SlashPos == llvm::StringRef::npos) return 0;
142
143  // Find out if this is the home for the specified framework, by checking
144  // HeaderSearch.  Possible answer are yes/no and unknown.
145  const DirectoryEntry *&FrameworkDirCache =
146    HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
147
148  // If it is known and in some other directory, fail.
149  if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
150    return 0;
151
152  // Otherwise, construct the path to this framework dir.
153
154  // FrameworkName = "/System/Library/Frameworks/"
155  llvm::SmallString<1024> FrameworkName;
156  FrameworkName += getFrameworkDir()->getName();
157  if (FrameworkName.empty() || FrameworkName.back() != '/')
158    FrameworkName.push_back('/');
159
160  // FrameworkName = "/System/Library/Frameworks/Cocoa"
161  FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
162
163  // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
164  FrameworkName += ".framework/";
165
166  // If the cache entry is still unresolved, query to see if the cache entry is
167  // still unresolved.  If so, check its existence now.
168  if (FrameworkDirCache == 0) {
169    HS.IncrementFrameworkLookupCount();
170
171    // If the framework dir doesn't exist, we fail.
172    // FIXME: It's probably more efficient to query this with FileMgr.getDir.
173    if (!llvm::sys::Path(std::string(FrameworkName.begin(),
174                                     FrameworkName.end())).exists())
175      return 0;
176
177    // Otherwise, if it does, remember that this is the right direntry for this
178    // framework.
179    FrameworkDirCache = getFrameworkDir();
180  }
181
182  // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
183  unsigned OrigSize = FrameworkName.size();
184
185  FrameworkName += "Headers/";
186  FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
187  if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str()))
188    return FE;
189
190  // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
191  const char *Private = "Private";
192  FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
193                       Private+strlen(Private));
194  return FileMgr.getFile(FrameworkName.str());
195}
196
197
198//===----------------------------------------------------------------------===//
199// Header File Location.
200//===----------------------------------------------------------------------===//
201
202
203/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
204/// return null on failure.  isAngled indicates whether the file reference is
205/// for system #include's or not (i.e. using <> instead of "").  CurFileEnt, if
206/// non-null, indicates where the #including file is, in case a relative search
207/// is needed.
208const FileEntry *HeaderSearch::LookupFile(llvm::StringRef Filename,
209                                          bool isAngled,
210                                          const DirectoryLookup *FromDir,
211                                          const DirectoryLookup *&CurDir,
212                                          const FileEntry *CurFileEnt) {
213  // If 'Filename' is absolute, check to see if it exists and no searching.
214  if (llvm::sys::path::is_absolute(Filename)) {
215    CurDir = 0;
216
217    // If this was an #include_next "/absolute/file", fail.
218    if (FromDir) return 0;
219
220    // Otherwise, just return the file.
221    return FileMgr.getFile(Filename);
222  }
223
224  // Step #0, unless disabled, check to see if the file is in the #includer's
225  // directory.  This has to be based on CurFileEnt, not CurDir, because
226  // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
227  // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
228  // This search is not done for <> headers.
229  if (CurFileEnt && !isAngled && !NoCurDirSearch) {
230    llvm::SmallString<1024> TmpDir;
231    // Concatenate the requested file onto the directory.
232    // FIXME: Portability.  Filename concatenation should be in sys::Path.
233    TmpDir += CurFileEnt->getDir()->getName();
234    TmpDir.push_back('/');
235    TmpDir.append(Filename.begin(), Filename.end());
236    if (const FileEntry *FE = FileMgr.getFile(TmpDir.str())) {
237      // Leave CurDir unset.
238      // This file is a system header or C++ unfriendly if the old file is.
239      //
240      // Note that the temporary 'DirInfo' is required here, as either call to
241      // getFileInfo could resize the vector and we don't want to rely on order
242      // of evaluation.
243      unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
244      getFileInfo(FE).DirInfo = DirInfo;
245      return FE;
246    }
247  }
248
249  CurDir = 0;
250
251  // If this is a system #include, ignore the user #include locs.
252  unsigned i = isAngled ? SystemDirIdx : 0;
253
254  // If this is a #include_next request, start searching after the directory the
255  // file was found in.
256  if (FromDir)
257    i = FromDir-&SearchDirs[0];
258
259  // Cache all of the lookups performed by this method.  Many headers are
260  // multiply included, and the "pragma once" optimization prevents them from
261  // being relex/pp'd, but they would still have to search through a
262  // (potentially huge) series of SearchDirs to find it.
263  std::pair<unsigned, unsigned> &CacheLookup =
264    LookupFileCache.GetOrCreateValue(Filename).getValue();
265
266  // If the entry has been previously looked up, the first value will be
267  // non-zero.  If the value is equal to i (the start point of our search), then
268  // this is a matching hit.
269  if (CacheLookup.first == i+1) {
270    // Skip querying potentially lots of directories for this lookup.
271    i = CacheLookup.second;
272  } else {
273    // Otherwise, this is the first query, or the previous query didn't match
274    // our search start.  We will fill in our found location below, so prime the
275    // start point value.
276    CacheLookup.first = i+1;
277  }
278
279  // Check each directory in sequence to see if it contains this file.
280  for (; i != SearchDirs.size(); ++i) {
281    const FileEntry *FE =
282      SearchDirs[i].LookupFile(Filename, *this);
283    if (!FE) continue;
284
285    CurDir = &SearchDirs[i];
286
287    // This file is a system header or C++ unfriendly if the dir is.
288    getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
289
290    // Remember this location for the next lookup we do.
291    CacheLookup.second = i;
292    return FE;
293  }
294
295  // Otherwise, didn't find it. Remember we didn't find this.
296  CacheLookup.second = SearchDirs.size();
297  return 0;
298}
299
300/// LookupSubframeworkHeader - Look up a subframework for the specified
301/// #include file.  For example, if #include'ing <HIToolbox/HIToolbox.h> from
302/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
303/// is a subframework within Carbon.framework.  If so, return the FileEntry
304/// for the designated file, otherwise return null.
305const FileEntry *HeaderSearch::
306LookupSubframeworkHeader(llvm::StringRef Filename,
307                         const FileEntry *ContextFileEnt) {
308  assert(ContextFileEnt && "No context file?");
309
310  // Framework names must have a '/' in the filename.  Find it.
311  size_t SlashPos = Filename.find('/');
312  if (SlashPos == llvm::StringRef::npos) return 0;
313
314  // Look up the base framework name of the ContextFileEnt.
315  const char *ContextName = ContextFileEnt->getName();
316
317  // If the context info wasn't a framework, couldn't be a subframework.
318  const char *FrameworkPos = strstr(ContextName, ".framework/");
319  if (FrameworkPos == 0)
320    return 0;
321
322  llvm::SmallString<1024> FrameworkName(ContextName,
323                                        FrameworkPos+strlen(".framework/"));
324
325  // Append Frameworks/HIToolbox.framework/
326  FrameworkName += "Frameworks/";
327  FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
328  FrameworkName += ".framework/";
329
330  llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
331    FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
332
333  // Some other location?
334  if (CacheLookup.getValue() &&
335      CacheLookup.getKeyLength() == FrameworkName.size() &&
336      memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
337             CacheLookup.getKeyLength()) != 0)
338    return 0;
339
340  // Cache subframework.
341  if (CacheLookup.getValue() == 0) {
342    ++NumSubFrameworkLookups;
343
344    // If the framework dir doesn't exist, we fail.
345    const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
346    if (Dir == 0) return 0;
347
348    // Otherwise, if it does, remember that this is the right direntry for this
349    // framework.
350    CacheLookup.setValue(Dir);
351  }
352
353  const FileEntry *FE = 0;
354
355  // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
356  llvm::SmallString<1024> HeadersFilename(FrameworkName);
357  HeadersFilename += "Headers/";
358  HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
359  if (!(FE = FileMgr.getFile(HeadersFilename.str()))) {
360
361    // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
362    HeadersFilename = FrameworkName;
363    HeadersFilename += "PrivateHeaders/";
364    HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
365    if (!(FE = FileMgr.getFile(HeadersFilename.str())))
366      return 0;
367  }
368
369  // This file is a system header or C++ unfriendly if the old file is.
370  //
371  // Note that the temporary 'DirInfo' is required here, as either call to
372  // getFileInfo could resize the vector and we don't want to rely on order
373  // of evaluation.
374  unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
375  getFileInfo(FE).DirInfo = DirInfo;
376  return FE;
377}
378
379//===----------------------------------------------------------------------===//
380// File Info Management.
381//===----------------------------------------------------------------------===//
382
383
384/// getFileInfo - Return the HeaderFileInfo structure for the specified
385/// FileEntry.
386HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
387  if (FE->getUID() >= FileInfo.size())
388    FileInfo.resize(FE->getUID()+1);
389  return FileInfo[FE->getUID()];
390}
391
392void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
393  if (UID >= FileInfo.size())
394    FileInfo.resize(UID+1);
395  FileInfo[UID] = HFI;
396}
397
398/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
399/// #include, #include_next, or #import directive.  Return false if #including
400/// the file will have no effect or true if we should include it.
401bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
402  ++NumIncluded; // Count # of attempted #includes.
403
404  // Get information about this file.
405  HeaderFileInfo &FileInfo = getFileInfo(File);
406
407  // If this is a #import directive, check that we have not already imported
408  // this header.
409  if (isImport) {
410    // If this has already been imported, don't import it again.
411    FileInfo.isImport = true;
412
413    // Has this already been #import'ed or #include'd?
414    if (FileInfo.NumIncludes) return false;
415  } else {
416    // Otherwise, if this is a #include of a file that was previously #import'd
417    // or if this is the second #include of a #pragma once file, ignore it.
418    if (FileInfo.isImport)
419      return false;
420  }
421
422  // Next, check to see if the file is wrapped with #ifndef guards.  If so, and
423  // if the macro that guards it is defined, we know the #include has no effect.
424  if (const IdentifierInfo *ControllingMacro
425      = FileInfo.getControllingMacro(ExternalLookup))
426    if (ControllingMacro->hasMacroDefinition()) {
427      ++NumMultiIncludeFileOptzn;
428      return false;
429    }
430
431  // Increment the number of times this file has been included.
432  ++FileInfo.NumIncludes;
433
434  return true;
435}
436
437
438