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