Clang Project

clang_source_code/lib/Lex/HeaderSearch.cpp
1//===- HeaderSearch.cpp - Resolve Header File Locations -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//  This file implements the DirectoryLookup and HeaderSearch interfaces.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Lex/HeaderSearch.h"
14#include "clang/Basic/Diagnostic.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/IdentifierTable.h"
17#include "clang/Basic/Module.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Lex/DirectoryLookup.h"
20#include "clang/Lex/ExternalPreprocessorSource.h"
21#include "clang/Lex/HeaderMap.h"
22#include "clang/Lex/HeaderSearchOptions.h"
23#include "clang/Lex/LexDiagnostic.h"
24#include "clang/Lex/ModuleMap.h"
25#include "clang/Lex/Preprocessor.h"
26#include "llvm/ADT/APInt.h"
27#include "llvm/ADT/Hashing.h"
28#include "llvm/ADT/SmallString.h"
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/StringRef.h"
31#include "llvm/Support/Allocator.h"
32#include "llvm/Support/Capacity.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/FileSystem.h"
35#include "llvm/Support/Path.h"
36#include "llvm/Support/VirtualFileSystem.h"
37#include <algorithm>
38#include <cassert>
39#include <cstddef>
40#include <cstdio>
41#include <cstring>
42#include <string>
43#include <system_error>
44#include <utility>
45
46using namespace clang;
47
48const IdentifierInfo *
49HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
50  if (ControllingMacro) {
51    if (ControllingMacro->isOutOfDate()) {
52       (0) . __assert_fail ("External && \"We must have an external source if we have a \" \"controlling macro that is out of date.\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 53, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(External && "We must have an external source if we have a "
53 (0) . __assert_fail ("External && \"We must have an external source if we have a \" \"controlling macro that is out of date.\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 53, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">                         "controlling macro that is out of date.");
54      External->updateOutOfDateIdentifier(
55          *const_cast<IdentifierInfo *>(ControllingMacro));
56    }
57    return ControllingMacro;
58  }
59
60  if (!ControllingMacroID || !External)
61    return nullptr;
62
63  ControllingMacro = External->GetIdentifier(ControllingMacroID);
64  return ControllingMacro;
65}
66
67ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() = default;
68
69HeaderSearch::HeaderSearch(std::shared_ptr<HeaderSearchOptionsHSOpts,
70                           SourceManager &SourceMgrDiagnosticsEngine &Diags,
71                           const LangOptions &LangOpts,
72                           const TargetInfo *Target)
73    : HSOpts(std::move(HSOpts)), Diags(Diags),
74      FileMgr(SourceMgr.getFileManager()), FrameworkMap(64),
75      ModMap(SourceMgr, Diags, LangOpts, Target, *this) {}
76
77void HeaderSearch::PrintStats() {
78  fprintf(stderr"\n*** HeaderSearch Stats:\n");
79  fprintf(stderr"%d files tracked.\n", (int)FileInfo.size());
80  unsigned NumOnceOnlyFiles = 0MaxNumIncludes = 0NumSingleIncludedFiles = 0;
81  for (unsigned i = 0e = FileInfo.size(); i != e; ++i) {
82    NumOnceOnlyFiles += FileInfo[i].isImport;
83    if (MaxNumIncludes < FileInfo[i].NumIncludes)
84      MaxNumIncludes = FileInfo[i].NumIncludes;
85    NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
86  }
87  fprintf(stderr"  %d #import/#pragma once files.\n"NumOnceOnlyFiles);
88  fprintf(stderr"  %d included exactly once.\n"NumSingleIncludedFiles);
89  fprintf(stderr"  %d max times a file is included.\n"MaxNumIncludes);
90
91  fprintf(stderr"  %d #include/#include_next/#import.\n"NumIncluded);
92  fprintf(stderr"    %d #includes skipped due to"
93          " the multi-include optimization.\n"NumMultiIncludeFileOptzn);
94
95  fprintf(stderr"%d framework lookups.\n"NumFrameworkLookups);
96  fprintf(stderr"%d subframework lookups.\n"NumSubFrameworkLookups);
97}
98
99/// CreateHeaderMap - This method returns a HeaderMap for the specified
100/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
101const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
102  // We expect the number of headermaps to be small, and almost always empty.
103  // If it ever grows, use of a linear search should be re-evaluated.
104  if (!HeaderMaps.empty()) {
105    for (unsigned i = 0e = HeaderMaps.size(); i != e; ++i)
106      // Pointer equality comparison of FileEntries works because they are
107      // already uniqued by inode.
108      if (HeaderMaps[i].first == FE)
109        return HeaderMaps[i].second.get();
110  }
111
112  if (std::unique_ptr<HeaderMapHM = HeaderMap::Create(FEFileMgr)) {
113    HeaderMaps.emplace_back(FEstd::move(HM));
114    return HeaderMaps.back().second.get();
115  }
116
117  return nullptr;
118}
119
120/// Get filenames for all registered header maps.
121void HeaderSearch::getHeaderMapFileNames(
122    SmallVectorImpl<std::string> &Namesconst {
123  for (auto &HM : HeaderMaps)
124    Names.push_back(HM.first->getName());
125}
126
127std::string HeaderSearch::getCachedModuleFileName(Module *Module) {
128  const FileEntry *ModuleMap =
129      getModuleMap().getModuleMapFileForUniquing(Module);
130  return getCachedModuleFileName(Module->NameModuleMap->getName());
131}
132
133std::string HeaderSearch::getPrebuiltModuleFileName(StringRef ModuleName,
134                                                    bool FileMapOnly) {
135  // First check the module name to pcm file map.
136  auto i (HSOpts->PrebuiltModuleFiles.find(ModuleName));
137  if (i != HSOpts->PrebuiltModuleFiles.end())
138    return i->second;
139
140  if (FileMapOnly || HSOpts->PrebuiltModulePaths.empty())
141    return {};
142
143  // Then go through each prebuilt module directory and try to find the pcm
144  // file.
145  for (const std::string &Dir : HSOpts->PrebuiltModulePaths) {
146    SmallString<256Result(Dir);
147    llvm::sys::fs::make_absolute(Result);
148    llvm::sys::path::append(Result, ModuleName + ".pcm");
149    if (getFileMgr().getFile(Result.str()))
150      return Result.str().str();
151  }
152  return {};
153}
154
155std::string HeaderSearch::getCachedModuleFileName(StringRef ModuleName,
156                                                  StringRef ModuleMapPath) {
157  // If we don't have a module cache path or aren't supposed to use one, we
158  // can't do anything.
159  if (getModuleCachePath().empty())
160    return {};
161
162  SmallString<256Result(getModuleCachePath());
163  llvm::sys::fs::make_absolute(Result);
164
165  if (HSOpts->DisableModuleHash) {
166    llvm::sys::path::append(Result, ModuleName + ".pcm");
167  } else {
168    // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
169    // ideally be globally unique to this particular module. Name collisions
170    // in the hash are safe (because any translation unit can only import one
171    // module with each name), but result in a loss of caching.
172    //
173    // To avoid false-negatives, we form as canonical a path as we can, and map
174    // to lower-case in case we're on a case-insensitive file system.
175    std::string Parent = llvm::sys::path::parent_path(ModuleMapPath);
176    if (Parent.empty())
177      Parent = ".";
178    auto *Dir = FileMgr.getDirectory(Parent);
179    if (!Dir)
180      return {};
181    auto DirName = FileMgr.getCanonicalName(Dir);
182    auto FileName = llvm::sys::path::filename(ModuleMapPath);
183
184    llvm::hash_code Hash =
185      llvm::hash_combine(DirName.lower(), FileName.lower());
186
187    SmallString<128HashStr;
188    llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
189    llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
190  }
191  return Result.str().str();
192}
193
194Module *HeaderSearch::lookupModule(StringRef ModuleNamebool AllowSearch,
195                                   bool AllowExtraModuleMapSearch) {
196  // Look in the module map to determine if there is a module by this name.
197  Module *Module = ModMap.findModule(ModuleName);
198  if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
199    return Module;
200
201  StringRef SearchName = ModuleName;
202  Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
203
204  // The facility for "private modules" -- adjacent, optional module maps named
205  // module.private.modulemap that are supposed to define private submodules --
206  // may have different flavors of names: FooPrivate, Foo_Private and Foo.Private.
207  //
208  // Foo.Private is now deprecated in favor of Foo_Private. Users of FooPrivate
209  // should also rename to Foo_Private. Representing private as submodules
210  // could force building unwanted dependencies into the parent module and cause
211  // dependency cycles.
212  if (!Module && SearchName.consume_back("_Private"))
213    Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
214  if (!Module && SearchName.consume_back("Private"))
215    Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
216  return Module;
217}
218
219Module *HeaderSearch::lookupModule(StringRef ModuleNameStringRef SearchName,
220                                   bool AllowExtraModuleMapSearch) {
221  Module *Module = nullptr;
222
223  // Look through the various header search paths to load any available module
224  // maps, searching for a module map that describes this module.
225  for (unsigned Idx = 0N = SearchDirs.size(); Idx != N; ++Idx) {
226    if (SearchDirs[Idx].isFramework()) {
227      // Search for or infer a module map for a framework. Here we use
228      // SearchName rather than ModuleName, to permit finding private modules
229      // named FooPrivate in buggy frameworks named Foo.
230      SmallString<128FrameworkDirName;
231      FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
232      llvm::sys::path::append(FrameworkDirName, SearchName + ".framework");
233      if (const DirectoryEntry *FrameworkDir
234            = FileMgr.getDirectory(FrameworkDirName)) {
235        bool IsSystem
236          = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
237        Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
238        if (Module)
239          break;
240      }
241    }
242
243    // FIXME: Figure out how header maps and module maps will work together.
244
245    // Only deal with normal search directories.
246    if (!SearchDirs[Idx].isNormalDir())
247      continue;
248
249    bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
250    // Search for a module map file in this directory.
251    if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
252                          /*IsFramework*/false) == LMM_NewlyLoaded) {
253      // We just loaded a module map file; check whether the module is
254      // available now.
255      Module = ModMap.findModule(ModuleName);
256      if (Module)
257        break;
258    }
259
260    // Search for a module map in a subdirectory with the same name as the
261    // module.
262    SmallString<128NestedModuleMapDirName;
263    NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
264    llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
265    if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
266                          /*IsFramework*/false) == LMM_NewlyLoaded){
267      // If we just loaded a module map file, look for the module again.
268      Module = ModMap.findModule(ModuleName);
269      if (Module)
270        break;
271    }
272
273    // If we've already performed the exhaustive search for module maps in this
274    // search directory, don't do it again.
275    if (SearchDirs[Idx].haveSearchedAllModuleMaps())
276      continue;
277
278    // Load all module maps in the immediate subdirectories of this search
279    // directory if ModuleName was from @import.
280    if (AllowExtraModuleMapSearch)
281      loadSubdirectoryModuleMaps(SearchDirs[Idx]);
282
283    // Look again for the module.
284    Module = ModMap.findModule(ModuleName);
285    if (Module)
286      break;
287  }
288
289  return Module;
290}
291
292//===----------------------------------------------------------------------===//
293// File lookup within a DirectoryLookup scope
294//===----------------------------------------------------------------------===//
295
296/// getName - Return the directory or filename corresponding to this lookup
297/// object.
298StringRef DirectoryLookup::getName() const {
299  if (isNormalDir())
300    return getDir()->getName();
301  if (isFramework())
302    return getFrameworkDir()->getName();
303   (0) . __assert_fail ("isHeaderMap() && \"Unknown DirectoryLookup\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 303, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isHeaderMap() && "Unknown DirectoryLookup");
304  return getHeaderMap()->getFileName();
305}
306
307const FileEntry *HeaderSearch::getFileAndSuggestModule(
308    StringRef FileNameSourceLocation IncludeLocconst DirectoryEntry *Dir,
309    bool IsSystemHeaderDirModule *RequestingModule,
310    ModuleMap::KnownHeader *SuggestedModule) {
311  // If we have a module map that might map this header, load it and
312  // check whether we'll have a suggestion for a module.
313  const FileEntry *File = getFileMgr().getFile(FileName, /*OpenFile=*/true);
314  if (!File)
315    return nullptr;
316
317  // If there is a module that corresponds to this header, suggest it.
318  if (!findUsableModuleForHeader(FileDir ? Dir : File->getDir(),
319                                 RequestingModuleSuggestedModule,
320                                 IsSystemHeaderDir))
321    return nullptr;
322
323  return File;
324}
325
326/// LookupFile - Lookup the specified file in this search path, returning it
327/// if it exists or returning null if not.
328const FileEntry *DirectoryLookup::LookupFile(
329    StringRef &Filename,
330    HeaderSearch &HS,
331    SourceLocation IncludeLoc,
332    SmallVectorImpl<char> *SearchPath,
333    SmallVectorImpl<char> *RelativePath,
334    Module *RequestingModule,
335    ModuleMap::KnownHeader *SuggestedModule,
336    bool &InUserSpecifiedSystemFramework,
337    bool &IsFrameworkFound,
338    bool &HasBeenMapped,
339    SmallVectorImpl<char> &MappedNameconst {
340  InUserSpecifiedSystemFramework = false;
341  HasBeenMapped = false;
342
343  SmallString<1024TmpDir;
344  if (isNormalDir()) {
345    // Concatenate the requested file onto the directory.
346    TmpDir = getDir()->getName();
347    llvm::sys::path::append(TmpDir, Filename);
348    if (SearchPath) {
349      StringRef SearchPathRef(getDir()->getName());
350      SearchPath->clear();
351      SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
352    }
353    if (RelativePath) {
354      RelativePath->clear();
355      RelativePath->append(Filename.begin(), Filename.end());
356    }
357
358    return HS.getFileAndSuggestModule(TmpDir, IncludeLoc, getDir(),
359                                      isSystemHeaderDirectory(),
360                                      RequestingModule, SuggestedModule);
361  }
362
363  if (isFramework())
364    return DoFrameworkLookup(FilenameHSSearchPathRelativePath,
365                             RequestingModuleSuggestedModule,
366                             InUserSpecifiedSystemFrameworkIsFrameworkFound);
367
368   (0) . __assert_fail ("isHeaderMap() && \"Unknown directory lookup\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 368, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isHeaderMap() && "Unknown directory lookup");
369  const HeaderMap *HM = getHeaderMap();
370  SmallString<1024Path;
371  StringRef Dest = HM->lookupFilename(Filename, Path);
372  if (Dest.empty())
373    return nullptr;
374
375  const FileEntry *Result;
376
377  // Check if the headermap maps the filename to a framework include
378  // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
379  // framework include.
380  if (llvm::sys::path::is_relative(Dest)) {
381    MappedName.clear();
382    MappedName.append(Dest.begin(), Dest.end());
383    Filename = StringRef(MappedName.begin(), MappedName.size());
384    HasBeenMapped = true;
385    Result = HM->LookupFile(FilenameHS.getFileMgr());
386  } else {
387    Result = HS.getFileMgr().getFile(Dest);
388  }
389
390  if (Result) {
391    if (SearchPath) {
392      StringRef SearchPathRef(getName());
393      SearchPath->clear();
394      SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
395    }
396    if (RelativePath) {
397      RelativePath->clear();
398      RelativePath->append(Filename.begin(), Filename.end());
399    }
400  }
401  return Result;
402}
403
404/// Given a framework directory, find the top-most framework directory.
405///
406/// \param FileMgr The file manager to use for directory lookups.
407/// \param DirName The name of the framework directory.
408/// \param SubmodulePath Will be populated with the submodule path from the
409/// returned top-level module to the originally named framework.
410static const DirectoryEntry *
411getTopFrameworkDir(FileManager &FileMgrStringRef DirName,
412                   SmallVectorImpl<std::string> &SubmodulePath) {
413   (0) . __assert_fail ("llvm..sys..path..extension(DirName) == \".framework\" && \"Not a framework directory\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 414, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(llvm::sys::path::extension(DirName) == ".framework" &&
414 (0) . __assert_fail ("llvm..sys..path..extension(DirName) == \".framework\" && \"Not a framework directory\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 414, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">         "Not a framework directory");
415
416  // Note: as an egregious but useful hack we use the real path here, because
417  // frameworks moving between top-level frameworks to embedded frameworks tend
418  // to be symlinked, and we base the logical structure of modules on the
419  // physical layout. In particular, we need to deal with crazy includes like
420  //
421  //   #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
422  //
423  // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
424  // which one should access with, e.g.,
425  //
426  //   #include <Bar/Wibble.h>
427  //
428  // Similar issues occur when a top-level framework has moved into an
429  // embedded framework.
430  const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
431  DirName = FileMgr.getCanonicalName(TopFrameworkDir);
432  do {
433    // Get the parent directory name.
434    DirName = llvm::sys::path::parent_path(DirName);
435    if (DirName.empty())
436      break;
437
438    // Determine whether this directory exists.
439    const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
440    if (!Dir)
441      break;
442
443    // If this is a framework directory, then we're a subframework of this
444    // framework.
445    if (llvm::sys::path::extension(DirName) == ".framework") {
446      SubmodulePath.push_back(llvm::sys::path::stem(DirName));
447      TopFrameworkDir = Dir;
448    }
449  } while (true);
450
451  return TopFrameworkDir;
452}
453
454static bool needModuleLookup(Module *RequestingModule,
455                             bool HasSuggestedModule) {
456  return HasSuggestedModule ||
457         (RequestingModule && RequestingModule->NoUndeclaredIncludes);
458}
459
460/// DoFrameworkLookup - Do a lookup of the specified file in the current
461/// DirectoryLookup, which is a framework directory.
462const FileEntry *DirectoryLookup::DoFrameworkLookup(
463    StringRef FilenameHeaderSearch &HSSmallVectorImpl<char> *SearchPath,
464    SmallVectorImpl<char> *RelativePathModule *RequestingModule,
465    ModuleMap::KnownHeader *SuggestedModule,
466    bool &InUserSpecifiedSystemFrameworkbool &IsFrameworkFoundconst {
467  FileManager &FileMgr = HS.getFileMgr();
468
469  // Framework names must have a '/' in the filename.
470  size_t SlashPos = Filename.find('/');
471  if (SlashPos == StringRef::npos) return nullptr;
472
473  // Find out if this is the home for the specified framework, by checking
474  // HeaderSearch.  Possible answers are yes/no and unknown.
475  FrameworkCacheEntry &CacheEntry =
476    HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
477
478  // If it is known and in some other directory, fail.
479  if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
480    return nullptr;
481
482  // Otherwise, construct the path to this framework dir.
483
484  // FrameworkName = "/System/Library/Frameworks/"
485  SmallString<1024FrameworkName;
486  FrameworkName += getFrameworkDir()->getName();
487  if (FrameworkName.empty() || FrameworkName.back() != '/')
488    FrameworkName.push_back('/');
489
490  // FrameworkName = "/System/Library/Frameworks/Cocoa"
491  StringRef ModuleName(Filename.begin(), SlashPos);
492  FrameworkName += ModuleName;
493
494  // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
495  FrameworkName += ".framework/";
496
497  // If the cache entry was unresolved, populate it now.
498  if (!CacheEntry.Directory) {
499    HS.IncrementFrameworkLookupCount();
500
501    // If the framework dir doesn't exist, we fail.
502    const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
503    if (!Dirreturn nullptr;
504
505    // Otherwise, if it does, remember that this is the right direntry for this
506    // framework.
507    CacheEntry.Directory = getFrameworkDir();
508
509    // If this is a user search directory, check if the framework has been
510    // user-specified as a system framework.
511    if (getDirCharacteristic() == SrcMgr::C_User) {
512      SmallString<1024SystemFrameworkMarker(FrameworkName);
513      SystemFrameworkMarker += ".system_framework";
514      if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
515        CacheEntry.IsUserSpecifiedSystemFramework = true;
516      }
517    }
518  }
519
520  // Set out flags.
521  InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
522  IsFrameworkFound = CacheEntry.Directory;
523
524  if (RelativePath) {
525    RelativePath->clear();
526    RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
527  }
528
529  // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
530  unsigned OrigSize = FrameworkName.size();
531
532  FrameworkName += "Headers/";
533
534  if (SearchPath) {
535    SearchPath->clear();
536    // Without trailing '/'.
537    SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
538  }
539
540  FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
541  const FileEntry *FE = FileMgr.getFile(FrameworkName,
542                                        /*openFile=*/!SuggestedModule);
543  if (!FE) {
544    // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
545    const char *Private = "Private";
546    FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
547                         Private+strlen(Private));
548    if (SearchPath)
549      SearchPath->insert(SearchPath->begin()+OrigSize, Private,
550                         Private+strlen(Private));
551
552    FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
553  }
554
555  // If we found the header and are allowed to suggest a module, do so now.
556  if (FE && needModuleLookup(RequestingModuleSuggestedModule)) {
557    // Find the framework in which this header occurs.
558    StringRef FrameworkPath = FE->getDir()->getName();
559    bool FoundFramework = false;
560    do {
561      // Determine whether this directory exists.
562      const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
563      if (!Dir)
564        break;
565
566      // If this is a framework directory, then we're a subframework of this
567      // framework.
568      if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
569        FoundFramework = true;
570        break;
571      }
572
573      // Get the parent directory name.
574      FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
575      if (FrameworkPath.empty())
576        break;
577    } while (true);
578
579    bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
580    if (FoundFramework) {
581      if (!HS.findUsableModuleForFrameworkHeader(
582              FE, FrameworkPath, RequestingModule, SuggestedModule, IsSystem))
583        return nullptr;
584    } else {
585      if (!HS.findUsableModuleForHeader(FEgetDir(), RequestingModule,
586                                        SuggestedModuleIsSystem))
587        return nullptr;
588    }
589  }
590  return FE;
591}
592
593void HeaderSearch::setTarget(const TargetInfo &Target) {
594  ModMap.setTarget(Target);
595}
596
597//===----------------------------------------------------------------------===//
598// Header File Location.
599//===----------------------------------------------------------------------===//
600
601/// Return true with a diagnostic if the file that MSVC would have found
602/// fails to match the one that Clang would have found with MSVC header search
603/// disabled.
604static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
605                                  const FileEntry *MSFEconst FileEntry *FE,
606                                  SourceLocation IncludeLoc) {
607  if (MSFE && FE != MSFE) {
608    Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
609    return true;
610  }
611  return false;
612}
613
614static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
615  assert(!Str.empty());
616  char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
617  std::copy(Str.begin(), Str.end(), CopyStr);
618  CopyStr[Str.size()] = '\0';
619  return CopyStr;
620}
621
622static bool isFrameworkStylePath(StringRef Pathbool &IsPrivateHeader,
623                                 SmallVectorImpl<char> &FrameworkName) {
624  using namespace llvm::sys;
625  path::const_iterator I = path::begin(Path);
626  path::const_iterator E = path::end(Path);
627  IsPrivateHeader = false;
628
629  // Detect different types of framework style paths:
630  //
631  //   ...Foo.framework/{Headers,PrivateHeaders}
632  //   ...Foo.framework/Versions/{A,Current}/{Headers,PrivateHeaders}
633  //   ...Foo.framework/Frameworks/Nested.framework/{Headers,PrivateHeaders}
634  //   ...<other variations with 'Versions' like in the above path>
635  //
636  // and some other variations among these lines.
637  int FoundComp = 0;
638  while (I != E) {
639    if (*I == "Headers")
640      ++FoundComp;
641    if (I->endswith(".framework")) {
642      FrameworkName.append(I->begin(), I->end());
643      ++FoundComp;
644    }
645    if (*I == "PrivateHeaders") {
646      ++FoundComp;
647      IsPrivateHeader = true;
648    }
649    ++I;
650  }
651
652  return !FrameworkName.empty() && FoundComp >= 2;
653}
654
655static void
656diagnoseFrameworkInclude(DiagnosticsEngine &DiagsSourceLocation IncludeLoc,
657                         StringRef IncluderStringRef IncludeFilename,
658                         const FileEntry *IncludeFEbool isAngled = false,
659                         bool FoundByHeaderMap = false) {
660  bool IsIncluderPrivateHeader = false;
661  SmallString<128FromFrameworkToFramework;
662  if (!isFrameworkStylePath(Includer, IsIncluderPrivateHeader, FromFramework))
663    return;
664  bool IsIncludeePrivateHeader = false;
665  bool IsIncludeeInFramework = isFrameworkStylePath(
666      IncludeFE->getName(), IsIncludeePrivateHeader, ToFramework);
667
668  if (!isAngled && !FoundByHeaderMap) {
669    SmallString<128NewInclude("<");
670    if (IsIncludeeInFramework) {
671      NewInclude += StringRef(ToFramework).drop_back(10); // drop .framework
672      NewInclude += "/";
673    }
674    NewInclude += IncludeFilename;
675    NewInclude += ">";
676    Diags.Report(IncludeLoc, diag::warn_quoted_include_in_framework_header)
677        << IncludeFilename
678        << FixItHint::CreateReplacement(IncludeLoc, NewInclude);
679  }
680
681  // Headers in Foo.framework/Headers should not include headers
682  // from Foo.framework/PrivateHeaders, since this violates public/private
683  // API boundaries and can cause modular dependency cycles.
684  if (!IsIncluderPrivateHeader && IsIncludeeInFramework &&
685      IsIncludeePrivateHeader && FromFramework == ToFramework)
686    Diags.Report(IncludeLoc, diag::warn_framework_include_private_from_public)
687        << IncludeFilename;
688}
689
690/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
691/// return null on failure.  isAngled indicates whether the file reference is
692/// for system \#include's or not (i.e. using <> instead of ""). Includers, if
693/// non-empty, indicates where the \#including file(s) are, in case a relative
694/// search is needed. Microsoft mode will pass all \#including files.
695const FileEntry *HeaderSearch::LookupFile(
696    StringRef FilenameSourceLocation IncludeLocbool isAngled,
697    const DirectoryLookup *FromDirconst DirectoryLookup *&CurDir,
698    ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
699    SmallVectorImpl<char> *SearchPathSmallVectorImpl<char> *RelativePath,
700    Module *RequestingModuleModuleMap::KnownHeader *SuggestedModule,
701    bool *IsMappedbool *IsFrameworkFoundbool SkipCache,
702    bool BuildSystemModule) {
703  if (IsMapped)
704    *IsMapped = false;
705
706  if (IsFrameworkFound)
707    *IsFrameworkFound = false;
708
709  if (SuggestedModule)
710    *SuggestedModule = ModuleMap::KnownHeader();
711
712  // If 'Filename' is absolute, check to see if it exists and no searching.
713  if (llvm::sys::path::is_absolute(Filename)) {
714    CurDir = nullptr;
715
716    // If this was an #include_next "/absolute/file", fail.
717    if (FromDirreturn nullptr;
718
719    if (SearchPath)
720      SearchPath->clear();
721    if (RelativePath) {
722      RelativePath->clear();
723      RelativePath->append(Filename.begin(), Filename.end());
724    }
725    // Otherwise, just return the file.
726    return getFileAndSuggestModule(Filename, IncludeLoc, nullptr,
727                                   /*IsSystemHeaderDir*/false,
728                                   RequestingModule, SuggestedModule);
729  }
730
731  // This is the header that MSVC's header search would have found.
732  const FileEntry *MSFE = nullptr;
733  ModuleMap::KnownHeader MSSuggestedModule;
734
735  // Unless disabled, check to see if the file is in the #includer's
736  // directory.  This cannot be based on CurDir, because each includer could be
737  // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
738  // include of "baz.h" should resolve to "whatever/foo/baz.h".
739  // This search is not done for <> headers.
740  if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
741    SmallString<1024TmpDir;
742    bool First = true;
743    for (const auto &IncluderAndDir : Includers) {
744      const FileEntry *Includer = IncluderAndDir.first;
745
746      // Concatenate the requested file onto the directory.
747      // FIXME: Portability.  Filename concatenation should be in sys::Path.
748      TmpDir = IncluderAndDir.second->getName();
749      TmpDir.push_back('/');
750      TmpDir.append(Filename.begin(), Filename.end());
751
752      // FIXME: We don't cache the result of getFileInfo across the call to
753      // getFileAndSuggestModule, because it's a reference to an element of
754      // a container that could be reallocated across this call.
755      //
756      // If we have no includer, that means we're processing a #include
757      // from a module build. We should treat this as a system header if we're
758      // building a [system] module.
759      bool IncluderIsSystemHeader =
760          Includer ? getFileInfo(Includer).DirInfo != SrcMgr::C_User :
761          BuildSystemModule;
762      if (const FileEntry *FE = getFileAndSuggestModule(
763              TmpDir, IncludeLoc, IncluderAndDir.second, IncluderIsSystemHeader,
764              RequestingModule, SuggestedModule)) {
765        if (!Includer) {
766           (0) . __assert_fail ("First && \"only first includer can have no file\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 766, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(First && "only first includer can have no file");
767          return FE;
768        }
769
770        // Leave CurDir unset.
771        // This file is a system header or C++ unfriendly if the old file is.
772        //
773        // Note that we only use one of FromHFI/ToHFI at once, due to potential
774        // reallocation of the underlying vector potentially making the first
775        // reference binding dangling.
776        HeaderFileInfo &FromHFI = getFileInfo(Includer);
777        unsigned DirInfo = FromHFI.DirInfo;
778        bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
779        StringRef Framework = FromHFI.Framework;
780
781        HeaderFileInfo &ToHFI = getFileInfo(FE);
782        ToHFI.DirInfo = DirInfo;
783        ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
784        ToHFI.Framework = Framework;
785
786        if (SearchPath) {
787          StringRef SearchPathRef(IncluderAndDir.second->getName());
788          SearchPath->clear();
789          SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
790        }
791        if (RelativePath) {
792          RelativePath->clear();
793          RelativePath->append(Filename.begin(), Filename.end());
794        }
795        if (First) {
796          diagnoseFrameworkInclude(Diags, IncludeLoc,
797                                   IncluderAndDir.second->getName(), Filename,
798                                   FE);
799          return FE;
800        }
801
802        // Otherwise, we found the path via MSVC header search rules.  If
803        // -Wmsvc-include is enabled, we have to keep searching to see if we
804        // would've found this header in -I or -isystem directories.
805        if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
806          return FE;
807        } else {
808          MSFE = FE;
809          if (SuggestedModule) {
810            MSSuggestedModule = *SuggestedModule;
811            *SuggestedModule = ModuleMap::KnownHeader();
812          }
813          break;
814        }
815      }
816      First = false;
817    }
818  }
819
820  CurDir = nullptr;
821
822  // If this is a system #include, ignore the user #include locs.
823  unsigned i = isAngled ? AngledDirIdx : 0;
824
825  // If this is a #include_next request, start searching after the directory the
826  // file was found in.
827  if (FromDir)
828    i = FromDir-&SearchDirs[0];
829
830  // Cache all of the lookups performed by this method.  Many headers are
831  // multiply included, and the "pragma once" optimization prevents them from
832  // being relex/pp'd, but they would still have to search through a
833  // (potentially huge) series of SearchDirs to find it.
834  LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
835
836  // If the entry has been previously looked up, the first value will be
837  // non-zero.  If the value is equal to i (the start point of our search), then
838  // this is a matching hit.
839  if (!SkipCache && CacheLookup.StartIdx == i+1) {
840    // Skip querying potentially lots of directories for this lookup.
841    i = CacheLookup.HitIdx;
842    if (CacheLookup.MappedName) {
843      Filename = CacheLookup.MappedName;
844      if (IsMapped)
845        *IsMapped = true;
846    }
847  } else {
848    // Otherwise, this is the first query, or the previous query didn't match
849    // our search start.  We will fill in our found location below, so prime the
850    // start point value.
851    CacheLookup.reset(/*StartIdx=*/i+1);
852  }
853
854  SmallString<64MappedName;
855
856  // Check each directory in sequence to see if it contains this file.
857  for (; i != SearchDirs.size(); ++i) {
858    bool InUserSpecifiedSystemFramework = false;
859    bool HasBeenMapped = false;
860    bool IsFrameworkFoundInDir = false;
861    const FileEntry *FE = SearchDirs[i].LookupFile(
862        Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
863        SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
864        HasBeenMapped, MappedName);
865    if (HasBeenMapped) {
866      CacheLookup.MappedName =
867          copyString(Filename, LookupFileCache.getAllocator());
868      if (IsMapped)
869        *IsMapped = true;
870    }
871    if (IsFrameworkFound)
872      *IsFrameworkFound |= IsFrameworkFoundInDir;
873    if (!FEcontinue;
874
875    CurDir = &SearchDirs[i];
876
877    // This file is a system header or C++ unfriendly if the dir is.
878    HeaderFileInfo &HFI = getFileInfo(FE);
879    HFI.DirInfo = CurDir->getDirCharacteristic();
880
881    // If the directory characteristic is User but this framework was
882    // user-specified to be treated as a system framework, promote the
883    // characteristic.
884    if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
885      HFI.DirInfo = SrcMgr::C_System;
886
887    // If the filename matches a known system header prefix, override
888    // whether the file is a system header.
889    for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
890      if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
891        HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
892                                                       : SrcMgr::C_User;
893        break;
894      }
895    }
896
897    // If this file is found in a header map and uses the framework style of
898    // includes, then this header is part of a framework we're building.
899    if (CurDir->isIndexHeaderMap()) {
900      size_t SlashPos = Filename.find('/');
901      if (SlashPos != StringRef::npos) {
902        HFI.IndexHeaderMapHeader = 1;
903        HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
904                                                         SlashPos));
905      }
906    }
907
908    if (checkMSVCHeaderSearch(DiagsMSFEFEIncludeLoc)) {
909      if (SuggestedModule)
910        *SuggestedModule = MSSuggestedModule;
911      return MSFE;
912    }
913
914    bool FoundByHeaderMap = !IsMapped ? false : *IsMapped;
915    if (!Includers.empty())
916      diagnoseFrameworkInclude(Diags, IncludeLoc,
917                               Includers.front().second->getName(), Filename,
918                               FE, isAngled, FoundByHeaderMap);
919
920    // Remember this location for the next lookup we do.
921    CacheLookup.HitIdx = i;
922    return FE;
923  }
924
925  // If we are including a file with a quoted include "foo.h" from inside
926  // a header in a framework that is currently being built, and we couldn't
927  // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
928  // "Foo" is the name of the framework in which the including header was found.
929  if (!Includers.empty() && Includers.front().first && !isAngled &&
930      Filename.find('/') == StringRef::npos) {
931    HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
932    if (IncludingHFI.IndexHeaderMapHeader) {
933      SmallString<128ScratchFilename;
934      ScratchFilename += IncludingHFI.Framework;
935      ScratchFilename += '/';
936      ScratchFilename += Filename;
937
938      const FileEntry *FE = LookupFile(
939          ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, CurDir,
940          Includers.front(), SearchPath, RelativePath, RequestingModule,
941          SuggestedModule, IsMapped, /*IsFrameworkFound=*/nullptr);
942
943      if (checkMSVCHeaderSearch(DiagsMSFEFEIncludeLoc)) {
944        if (SuggestedModule)
945          *SuggestedModule = MSSuggestedModule;
946        return MSFE;
947      }
948
949      LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
950      CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
951      // FIXME: SuggestedModule.
952      return FE;
953    }
954  }
955
956  if (checkMSVCHeaderSearch(DiagsMSFEnullptrIncludeLoc)) {
957    if (SuggestedModule)
958      *SuggestedModule = MSSuggestedModule;
959    return MSFE;
960  }
961
962  // Otherwise, didn't find it. Remember we didn't find this.
963  CacheLookup.HitIdx = SearchDirs.size();
964  return nullptr;
965}
966
967/// LookupSubframeworkHeader - Look up a subframework for the specified
968/// \#include file.  For example, if \#include'ing <HIToolbox/HIToolbox.h> from
969/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
970/// is a subframework within Carbon.framework.  If so, return the FileEntry
971/// for the designated file, otherwise return null.
972const FileEntry *HeaderSearch::
973LookupSubframeworkHeader(StringRef Filename,
974                         const FileEntry *ContextFileEnt,
975                         SmallVectorImpl<char> *SearchPath,
976                         SmallVectorImpl<char> *RelativePath,
977                         Module *RequestingModule,
978                         ModuleMap::KnownHeader *SuggestedModule) {
979   (0) . __assert_fail ("ContextFileEnt && \"No context file?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 979, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ContextFileEnt && "No context file?");
980
981  // Framework names must have a '/' in the filename.  Find it.
982  // FIXME: Should we permit '\' on Windows?
983  size_t SlashPos = Filename.find('/');
984  if (SlashPos == StringRef::npos) return nullptr;
985
986  // Look up the base framework name of the ContextFileEnt.
987  StringRef ContextName = ContextFileEnt->getName();
988
989  // If the context info wasn't a framework, couldn't be a subframework.
990  const unsigned DotFrameworkLen = 10;
991  auto FrameworkPos = ContextName.find(".framework");
992  if (FrameworkPos == StringRef::npos ||
993      (ContextName[FrameworkPos + DotFrameworkLen] != '/' &&
994       ContextName[FrameworkPos + DotFrameworkLen] != '\\'))
995    return nullptr;
996
997  SmallString<1024FrameworkName(ContextName.data(), ContextName.data() +
998                                                          FrameworkPos +
999                                                          DotFrameworkLen + 1);
1000
1001  // Append Frameworks/HIToolbox.framework/
1002  FrameworkName += "Frameworks/";
1003  FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
1004  FrameworkName += ".framework/";
1005
1006  auto &CacheLookup =
1007      *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
1008                                          FrameworkCacheEntry())).first;
1009
1010  // Some other location?
1011  if (CacheLookup.second.Directory &&
1012      CacheLookup.first().size() == FrameworkName.size() &&
1013      memcmp(CacheLookup.first().data(), &FrameworkName[0],
1014             CacheLookup.first().size()) != 0)
1015    return nullptr;
1016
1017  // Cache subframework.
1018  if (!CacheLookup.second.Directory) {
1019    ++NumSubFrameworkLookups;
1020
1021    // If the framework dir doesn't exist, we fail.
1022    const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
1023    if (!Dirreturn nullptr;
1024
1025    // Otherwise, if it does, remember that this is the right direntry for this
1026    // framework.
1027    CacheLookup.second.Directory = Dir;
1028  }
1029
1030  const FileEntry *FE = nullptr;
1031
1032  if (RelativePath) {
1033    RelativePath->clear();
1034    RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
1035  }
1036
1037  // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
1038  SmallString<1024HeadersFilename(FrameworkName);
1039  HeadersFilename += "Headers/";
1040  if (SearchPath) {
1041    SearchPath->clear();
1042    // Without trailing '/'.
1043    SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1044  }
1045
1046  HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1047  if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
1048    // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
1049    HeadersFilename = FrameworkName;
1050    HeadersFilename += "PrivateHeaders/";
1051    if (SearchPath) {
1052      SearchPath->clear();
1053      // Without trailing '/'.
1054      SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1055    }
1056
1057    HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1058    if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true)))
1059      return nullptr;
1060  }
1061
1062  // This file is a system header or C++ unfriendly if the old file is.
1063  //
1064  // Note that the temporary 'DirInfo' is required here, as either call to
1065  // getFileInfo could resize the vector and we don't want to rely on order
1066  // of evaluation.
1067  unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
1068  getFileInfo(FE).DirInfo = DirInfo;
1069
1070  FrameworkName.pop_back(); // remove the trailing '/'
1071  if (!findUsableModuleForFrameworkHeader(FE, FrameworkName, RequestingModule,
1072                                          SuggestedModule, /*IsSystem*/ false))
1073    return nullptr;
1074
1075  return FE;
1076}
1077
1078//===----------------------------------------------------------------------===//
1079// File Info Management.
1080//===----------------------------------------------------------------------===//
1081
1082/// Merge the header file info provided by \p OtherHFI into the current
1083/// header file info (\p HFI)
1084static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
1085                                const HeaderFileInfo &OtherHFI) {
1086   (0) . __assert_fail ("OtherHFI.External && \"expected to merge external HFI\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 1086, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OtherHFI.External && "expected to merge external HFI");
1087
1088  HFI.isImport |= OtherHFI.isImport;
1089  HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
1090  HFI.isModuleHeader |= OtherHFI.isModuleHeader;
1091  HFI.NumIncludes += OtherHFI.NumIncludes;
1092
1093  if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
1094    HFI.ControllingMacro = OtherHFI.ControllingMacro;
1095    HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
1096  }
1097
1098  HFI.DirInfo = OtherHFI.DirInfo;
1099  HFI.External = (!HFI.IsValid || HFI.External);
1100  HFI.IsValid = true;
1101  HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
1102
1103  if (HFI.Framework.empty())
1104    HFI.Framework = OtherHFI.Framework;
1105}
1106
1107/// getFileInfo - Return the HeaderFileInfo structure for the specified
1108/// FileEntry.
1109HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
1110  if (FE->getUID() >= FileInfo.size())
1111    FileInfo.resize(FE->getUID() + 1);
1112
1113  HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
1114  // FIXME: Use a generation count to check whether this is really up to date.
1115  if (ExternalSource && !HFI->Resolved) {
1116    HFI->Resolved = true;
1117    auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1118
1119    HFI = &FileInfo[FE->getUID()];
1120    if (ExternalHFI.External)
1121      mergeHeaderFileInfo(*HFIExternalHFI);
1122  }
1123
1124  HFI->IsValid = true;
1125  // We have local information about this header file, so it's no longer
1126  // strictly external.
1127  HFI->External = false;
1128  return *HFI;
1129}
1130
1131const HeaderFileInfo *
1132HeaderSearch::getExistingFileInfo(const FileEntry *FE,
1133                                  bool WantExternalconst {
1134  // If we have an external source, ensure we have the latest information.
1135  // FIXME: Use a generation count to check whether this is really up to date.
1136  HeaderFileInfo *HFI;
1137  if (ExternalSource) {
1138    if (FE->getUID() >= FileInfo.size()) {
1139      if (!WantExternal)
1140        return nullptr;
1141      FileInfo.resize(FE->getUID() + 1);
1142    }
1143
1144    HFI = &FileInfo[FE->getUID()];
1145    if (!WantExternal && (!HFI->IsValid || HFI->External))
1146      return nullptr;
1147    if (!HFI->Resolved) {
1148      HFI->Resolved = true;
1149      auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1150
1151      HFI = &FileInfo[FE->getUID()];
1152      if (ExternalHFI.External)
1153        mergeHeaderFileInfo(*HFIExternalHFI);
1154    }
1155  } else if (FE->getUID() >= FileInfo.size()) {
1156    return nullptr;
1157  } else {
1158    HFI = &FileInfo[FE->getUID()];
1159  }
1160
1161  if (!HFI->IsValid || (HFI->External && !WantExternal))
1162    return nullptr;
1163
1164  return HFI;
1165}
1166
1167bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1168  // Check if we've ever seen this file as a header.
1169  if (auto *HFI = getExistingFileInfo(File))
1170    return HFI->isPragmaOnce || HFI->isImport || HFI->ControllingMacro ||
1171           HFI->ControllingMacroID;
1172  return false;
1173}
1174
1175void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
1176                                        ModuleMap::ModuleHeaderRole Role,
1177                                        bool isCompilingModuleHeader) {
1178  bool isModularHeader = !(Role & ModuleMap::TextualHeader);
1179
1180  // Don't mark the file info as non-external if there's nothing to change.
1181  if (!isCompilingModuleHeader) {
1182    if (!isModularHeader)
1183      return;
1184    auto *HFI = getExistingFileInfo(FE);
1185    if (HFI && HFI->isModuleHeader)
1186      return;
1187  }
1188
1189  auto &HFI = getFileInfo(FE);
1190  HFI.isModuleHeader |= isModularHeader;
1191  HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
1192}
1193
1194bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1195                                          const FileEntry *Filebool isImport,
1196                                          bool ModulesEnabledModule *M) {
1197  ++NumIncluded// Count # of attempted #includes.
1198
1199  // Get information about this file.
1200  HeaderFileInfo &FileInfo = getFileInfo(File);
1201
1202  // FIXME: this is a workaround for the lack of proper modules-aware support
1203  // for #import / #pragma once
1204  auto TryEnterImported = [&]() -> bool {
1205    if (!ModulesEnabled)
1206      return false;
1207    // Ensure FileInfo bits are up to date.
1208    ModMap.resolveHeaderDirectives(File);
1209    // Modules with builtins are special; multiple modules use builtins as
1210    // modular headers, example:
1211    //
1212    //    module stddef { header "stddef.h" export * }
1213    //
1214    // After module map parsing, this expands to:
1215    //
1216    //    module stddef {
1217    //      header "/path_to_builtin_dirs/stddef.h"
1218    //      textual "stddef.h"
1219    //    }
1220    //
1221    // It's common that libc++ and system modules will both define such
1222    // submodules. Make sure cached results for a builtin header won't
1223    // prevent other builtin modules to potentially enter the builtin header.
1224    // Note that builtins are header guarded and the decision to actually
1225    // enter them is postponed to the controlling macros logic below.
1226    bool TryEnterHdr = false;
1227    if (FileInfo.isCompilingModuleHeader && FileInfo.isModuleHeader)
1228      TryEnterHdr = File->getDir() == ModMap.getBuiltinDir() &&
1229                    ModuleMap::isBuiltinHeader(
1230                        llvm::sys::path::filename(File->getName()));
1231
1232    // Textual headers can be #imported from different modules. Since ObjC
1233    // headers find in the wild might rely only on #import and do not contain
1234    // controlling macros, be conservative and only try to enter textual headers
1235    // if such macro is present.
1236    if (!FileInfo.isModuleHeader &&
1237        FileInfo.getControllingMacro(ExternalLookup))
1238      TryEnterHdr = true;
1239    return TryEnterHdr;
1240  };
1241
1242  // If this is a #import directive, check that we have not already imported
1243  // this header.
1244  if (isImport) {
1245    // If this has already been imported, don't import it again.
1246    FileInfo.isImport = true;
1247
1248    // Has this already been #import'ed or #include'd?
1249    if (FileInfo.NumIncludes && !TryEnterImported())
1250      return false;
1251  } else {
1252    // Otherwise, if this is a #include of a file that was previously #import'd
1253    // or if this is the second #include of a #pragma once file, ignore it.
1254    if (FileInfo.isImport && !TryEnterImported())
1255      return false;
1256  }
1257
1258  // Next, check to see if the file is wrapped with #ifndef guards.  If so, and
1259  // if the macro that guards it is defined, we know the #include has no effect.
1260  if (const IdentifierInfo *ControllingMacro
1261      = FileInfo.getControllingMacro(ExternalLookup)) {
1262    // If the header corresponds to a module, check whether the macro is already
1263    // defined in that module rather than checking in the current set of visible
1264    // modules.
1265    if (M ? PP.isMacroDefinedInLocalModule(ControllingMacroM)
1266          : PP.isMacroDefined(ControllingMacro)) {
1267      ++NumMultiIncludeFileOptzn;
1268      return false;
1269    }
1270  }
1271
1272  // Increment the number of times this file has been included.
1273  ++FileInfo.NumIncludes;
1274
1275  return true;
1276}
1277
1278size_t HeaderSearch::getTotalMemory() const {
1279  return SearchDirs.capacity()
1280    + llvm::capacity_in_bytes(FileInfo)
1281    + llvm::capacity_in_bytes(HeaderMaps)
1282    + LookupFileCache.getAllocator().getTotalMemory()
1283    + FrameworkMap.getAllocator().getTotalMemory();
1284}
1285
1286StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
1287  return FrameworkNames.insert(Framework).first->first();
1288}
1289
1290bool HeaderSearch::hasModuleMap(StringRef FileName,
1291                                const DirectoryEntry *Root,
1292                                bool IsSystem) {
1293  if (!HSOpts->ImplicitModuleMaps)
1294    return false;
1295
1296  SmallVector<const DirectoryEntry *, 2FixUpDirectories;
1297
1298  StringRef DirName = FileName;
1299  do {
1300    // Get the parent directory name.
1301    DirName = llvm::sys::path::parent_path(DirName);
1302    if (DirName.empty())
1303      return false;
1304
1305    // Determine whether this directory exists.
1306    const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
1307    if (!Dir)
1308      return false;
1309
1310    // Try to load the module map file in this directory.
1311    switch (loadModuleMapFile(Dir, IsSystem,
1312                              llvm::sys::path::extension(Dir->getName()) ==
1313                                  ".framework")) {
1314    case LMM_NewlyLoaded:
1315    case LMM_AlreadyLoaded:
1316      // Success. All of the directories we stepped through inherit this module
1317      // map file.
1318      for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1319        DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1320      return true;
1321
1322    case LMM_NoDirectory:
1323    case LMM_InvalidModuleMap:
1324      break;
1325    }
1326
1327    // If we hit the top of our search, we're done.
1328    if (Dir == Root)
1329      return false;
1330
1331    // Keep track of all of the directories we checked, so we can mark them as
1332    // having module maps if we eventually do find a module map.
1333    FixUpDirectories.push_back(Dir);
1334  } while (true);
1335}
1336
1337ModuleMap::KnownHeader
1338HeaderSearch::findModuleForHeader(const FileEntry *File,
1339                                  bool AllowTextualconst {
1340  if (ExternalSource) {
1341    // Make sure the external source has handled header info about this file,
1342    // which includes whether the file is part of a module.
1343    (void)getExistingFileInfo(File);
1344  }
1345  return ModMap.findModuleForHeader(File, AllowTextual);
1346}
1347
1348static bool suggestModule(HeaderSearch &HSconst FileEntry *File,
1349                          Module *RequestingModule,
1350                          ModuleMap::KnownHeader *SuggestedModule) {
1351  ModuleMap::KnownHeader Module =
1352      HS.findModuleForHeader(File/*AllowTextual*/true);
1353  if (SuggestedModule)
1354    *SuggestedModule = (Module.getRole() & ModuleMap::TextualHeader)
1355                           ? ModuleMap::KnownHeader()
1356                           : Module;
1357
1358  // If this module specifies [no_undeclared_includes], we cannot find any
1359  // file that's in a non-dependency module.
1360  if (RequestingModule && Module && RequestingModule->NoUndeclaredIncludes) {
1361    HS.getModuleMap().resolveUses(RequestingModule/*Complain*/false);
1362    if (!RequestingModule->directlyUses(Module.getModule())) {
1363      return false;
1364    }
1365  }
1366
1367  return true;
1368}
1369
1370bool HeaderSearch::findUsableModuleForHeader(
1371    const FileEntry *Fileconst DirectoryEntry *RootModule *RequestingModule,
1372    ModuleMap::KnownHeader *SuggestedModulebool IsSystemHeaderDir) {
1373  if (File && needModuleLookup(RequestingModuleSuggestedModule)) {
1374    // If there is a module that corresponds to this header, suggest it.
1375    hasModuleMap(File->getName(), RootIsSystemHeaderDir);
1376    return suggestModule(*thisFileRequestingModuleSuggestedModule);
1377  }
1378  return true;
1379}
1380
1381bool HeaderSearch::findUsableModuleForFrameworkHeader(
1382    const FileEntry *FileStringRef FrameworkNameModule *RequestingModule,
1383    ModuleMap::KnownHeader *SuggestedModulebool IsSystemFramework) {
1384  // If we're supposed to suggest a module, look for one now.
1385  if (needModuleLookup(RequestingModuleSuggestedModule)) {
1386    // Find the top-level framework based on this framework.
1387    SmallVector<std::string4SubmodulePath;
1388    const DirectoryEntry *TopFrameworkDir
1389      = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1390
1391    // Determine the name of the top-level framework.
1392    StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1393
1394    // Load this framework module. If that succeeds, find the suggested module
1395    // for this header, if any.
1396    loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1397
1398    // FIXME: This can find a module not part of ModuleName, which is
1399    // important so that we're consistent about whether this header
1400    // corresponds to a module. Possibly we should lock down framework modules
1401    // so that this is not possible.
1402    return suggestModule(*thisFileRequestingModuleSuggestedModule);
1403  }
1404  return true;
1405}
1406
1407static const FileEntry *getPrivateModuleMap(const FileEntry *File,
1408                                            FileManager &FileMgr) {
1409  StringRef Filename = llvm::sys::path::filename(File->getName());
1410  SmallString<128>  PrivateFilename(File->getDir()->getName());
1411  if (Filename == "module.map")
1412    llvm::sys::path::append(PrivateFilename, "module_private.map");
1413  else if (Filename == "module.modulemap")
1414    llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1415  else
1416    return nullptr;
1417  return FileMgr.getFile(PrivateFilename);
1418}
1419
1420bool HeaderSearch::loadModuleMapFile(const FileEntry *Filebool IsSystem,
1421                                     FileID IDunsigned *Offset,
1422                                     StringRef OriginalModuleMapFile) {
1423  // Find the directory for the module. For frameworks, that may require going
1424  // up from the 'Modules' directory.
1425  const DirectoryEntry *Dir = nullptr;
1426  if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
1427    Dir = FileMgr.getDirectory(".");
1428  else {
1429    if (!OriginalModuleMapFile.empty()) {
1430      // We're building a preprocessed module map. Find or invent the directory
1431      // that it originally occupied.
1432      Dir = FileMgr.getDirectory(
1433          llvm::sys::path::parent_path(OriginalModuleMapFile));
1434      if (!Dir) {
1435        auto *FakeFile = FileMgr.getVirtualFile(OriginalModuleMapFile, 00);
1436        Dir = FakeFile->getDir();
1437      }
1438    } else {
1439      Dir = File->getDir();
1440    }
1441
1442    StringRef DirName(Dir->getName());
1443    if (llvm::sys::path::filename(DirName) == "Modules") {
1444      DirName = llvm::sys::path::parent_path(DirName);
1445      if (DirName.endswith(".framework"))
1446        Dir = FileMgr.getDirectory(DirName);
1447      // FIXME: This assert can fail if there's a race between the above check
1448      // and the removal of the directory.
1449       (0) . __assert_fail ("Dir && \"parent must exist\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 1449, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Dir && "parent must exist");
1450    }
1451  }
1452
1453  switch (loadModuleMapFileImpl(FileIsSystemDirIDOffset)) {
1454  case LMM_AlreadyLoaded:
1455  case LMM_NewlyLoaded:
1456    return false;
1457  case LMM_NoDirectory:
1458  case LMM_InvalidModuleMap:
1459    return true;
1460  }
1461  llvm_unreachable("Unknown load module map result");
1462}
1463
1464HeaderSearch::LoadModuleMapResult
1465HeaderSearch::loadModuleMapFileImpl(const FileEntry *Filebool IsSystem,
1466                                    const DirectoryEntry *DirFileID ID,
1467                                    unsigned *Offset) {
1468   (0) . __assert_fail ("File && \"expected FileEntry\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 1468, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(File && "expected FileEntry");
1469
1470  // Check whether we've already loaded this module map, and mark it as being
1471  // loaded in case we recursively try to load it from itself.
1472  auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1473  if (!AddResult.second)
1474    return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1475
1476  if (ModMap.parseModuleMapFile(File, IsSystem, Dir, ID, Offset)) {
1477    LoadedModuleMaps[File] = false;
1478    return LMM_InvalidModuleMap;
1479  }
1480
1481  // Try to load a corresponding private module map.
1482  if (const FileEntry *PMMFile = getPrivateModuleMap(FileFileMgr)) {
1483    if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
1484      LoadedModuleMaps[File] = false;
1485      return LMM_InvalidModuleMap;
1486    }
1487  }
1488
1489  // This directory has a module map.
1490  return LMM_NewlyLoaded;
1491}
1492
1493const FileEntry *
1494HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dirbool IsFramework) {
1495  if (!HSOpts->ImplicitModuleMaps)
1496    return nullptr;
1497  // For frameworks, the preferred spelling is Modules/module.modulemap, but
1498  // module.map at the framework root is also accepted.
1499  SmallString<128ModuleMapFileName(Dir->getName());
1500  if (IsFramework)
1501    llvm::sys::path::append(ModuleMapFileName, "Modules");
1502  llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1503  if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
1504    return F;
1505
1506  // Continue to allow module.map
1507  ModuleMapFileName = Dir->getName();
1508  llvm::sys::path::append(ModuleMapFileName, "module.map");
1509  return FileMgr.getFile(ModuleMapFileName);
1510}
1511
1512Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1513                                          const DirectoryEntry *Dir,
1514                                          bool IsSystem) {
1515  if (Module *Module = ModMap.findModule(Name))
1516    return Module;
1517
1518  // Try to load a module map file.
1519  switch (loadModuleMapFile(DirIsSystem/*IsFramework*/true)) {
1520  case LMM_InvalidModuleMap:
1521    // Try to infer a module map from the framework directory.
1522    if (HSOpts->ImplicitModuleMaps)
1523      ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
1524    break;
1525
1526  case LMM_AlreadyLoaded:
1527  case LMM_NoDirectory:
1528    return nullptr;
1529
1530  case LMM_NewlyLoaded:
1531    break;
1532  }
1533
1534  return ModMap.findModule(Name);
1535}
1536
1537HeaderSearch::LoadModuleMapResult
1538HeaderSearch::loadModuleMapFile(StringRef DirNamebool IsSystem,
1539                                bool IsFramework) {
1540  if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
1541    return loadModuleMapFile(DirIsSystemIsFramework);
1542
1543  return LMM_NoDirectory;
1544}
1545
1546HeaderSearch::LoadModuleMapResult
1547HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dirbool IsSystem,
1548                                bool IsFramework) {
1549  auto KnownDir = DirectoryHasModuleMap.find(Dir);
1550  if (KnownDir != DirectoryHasModuleMap.end())
1551    return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1552
1553  if (const FileEntry *ModuleMapFile = lookupModuleMapFile(DirIsFramework)) {
1554    LoadModuleMapResult Result =
1555        loadModuleMapFileImpl(ModuleMapFileIsSystemDir);
1556    // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1557    // E.g. Foo.framework/Modules/module.modulemap
1558    //      ^Dir                  ^ModuleMapFile
1559    if (Result == LMM_NewlyLoaded)
1560      DirectoryHasModuleMap[Dir] = true;
1561    else if (Result == LMM_InvalidModuleMap)
1562      DirectoryHasModuleMap[Dir] = false;
1563    return Result;
1564  }
1565  return LMM_InvalidModuleMap;
1566}
1567
1568void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
1569  Modules.clear();
1570
1571  if (HSOpts->ImplicitModuleMaps) {
1572    // Load module maps for each of the header search directories.
1573    for (unsigned Idx = 0N = SearchDirs.size(); Idx != N; ++Idx) {
1574      bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1575      if (SearchDirs[Idx].isFramework()) {
1576        std::error_code EC;
1577        SmallString<128DirNative;
1578        llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1579                                DirNative);
1580
1581        // Search each of the ".framework" directories to load them as modules.
1582        llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1583        for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
1584                                           DirEnd;
1585             Dir != DirEnd && !EC; Dir.increment(EC)) {
1586          if (llvm::sys::path::extension(Dir->path()) != ".framework")
1587            continue;
1588
1589          const DirectoryEntry *FrameworkDir =
1590              FileMgr.getDirectory(Dir->path());
1591          if (!FrameworkDir)
1592            continue;
1593
1594          // Load this framework module.
1595          loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1596                              IsSystem);
1597        }
1598        continue;
1599      }
1600
1601      // FIXME: Deal with header maps.
1602      if (SearchDirs[Idx].isHeaderMap())
1603        continue;
1604
1605      // Try to load a module map file for the search directory.
1606      loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1607                        /*IsFramework*/ false);
1608
1609      // Try to load module map files for immediate subdirectories of this
1610      // search directory.
1611      loadSubdirectoryModuleMaps(SearchDirs[Idx]);
1612    }
1613  }
1614
1615  // Populate the list of modules.
1616  for (ModuleMap::module_iterator M = ModMap.module_begin(),
1617                               MEnd = ModMap.module_end();
1618       M != MEnd; ++M) {
1619    Modules.push_back(M->getValue());
1620  }
1621}
1622
1623void HeaderSearch::loadTopLevelSystemModules() {
1624  if (!HSOpts->ImplicitModuleMaps)
1625    return;
1626
1627  // Load module maps for each of the header search directories.
1628  for (unsigned Idx = 0N = SearchDirs.size(); Idx != N; ++Idx) {
1629    // We only care about normal header directories.
1630    if (!SearchDirs[Idx].isNormalDir()) {
1631      continue;
1632    }
1633
1634    // Try to load a module map file for the search directory.
1635    loadModuleMapFile(SearchDirs[Idx].getDir(),
1636                      SearchDirs[Idx].isSystemHeaderDirectory(),
1637                      SearchDirs[Idx].isFramework());
1638  }
1639}
1640
1641void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1642   (0) . __assert_fail ("HSOpts->ImplicitModuleMaps && \"Should not be loading subdirectory module maps\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 1643, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(HSOpts->ImplicitModuleMaps &&
1643 (0) . __assert_fail ("HSOpts->ImplicitModuleMaps && \"Should not be loading subdirectory module maps\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/HeaderSearch.cpp", 1643, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">         "Should not be loading subdirectory module maps");
1644
1645  if (SearchDir.haveSearchedAllModuleMaps())
1646    return;
1647
1648  std::error_code EC;
1649  SmallString<128Dir = SearchDir.getDir()->getName();
1650  FileMgr.makeAbsolutePath(Dir);
1651  SmallString<128DirNative;
1652  llvm::sys::path::native(Dir, DirNative);
1653  llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1654  for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
1655       Dir != DirEnd && !EC; Dir.increment(EC)) {
1656    bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1657    if (IsFramework == SearchDir.isFramework())
1658      loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1659                        SearchDir.isFramework());
1660  }
1661
1662  SearchDir.setSearchedAllModuleMaps(true);
1663}
1664
1665std::string HeaderSearch::suggestPathToFileForDiagnostics(const FileEntry *File,
1666                                                          bool *IsSystem) {
1667  // FIXME: We assume that the path name currently cached in the FileEntry is
1668  // the most appropriate one for this analysis (and that it's spelled the
1669  // same way as the corresponding header search path).
1670  return suggestPathToFileForDiagnostics(File->getName(), /*BuildDir=*/"",
1671                                         IsSystem);
1672}
1673
1674std::string HeaderSearch::suggestPathToFileForDiagnostics(
1675    llvm::StringRef Filellvm::StringRef WorkingDirbool *IsSystem) {
1676  using namespace llvm::sys;
1677
1678  unsigned BestPrefixLength = 0;
1679  unsigned BestSearchDir;
1680
1681  for (unsigned I = 0I != SearchDirs.size(); ++I) {
1682    // FIXME: Support this search within frameworks and header maps.
1683    if (!SearchDirs[I].isNormalDir())
1684      continue;
1685
1686    StringRef Dir = SearchDirs[I].getDir()->getName();
1687    llvm::SmallString<32DirPath(Dir.begin(), Dir.end());
1688    if (!WorkingDir.empty() && !path::is_absolute(Dir)) {
1689      fs::make_absolute(WorkingDir, DirPath);
1690      path::remove_dots(DirPath, /*remove_dot_dot=*/true);
1691      Dir = DirPath;
1692    }
1693    for (auto NI = path::begin(File), NE = path::end(File),
1694              DI = path::begin(Dir), DE = path::end(Dir);
1695         /*termination condition in loop*/; ++NI, ++DI) {
1696      // '.' components in File are ignored.
1697      while (NI != NE && *NI == ".")
1698        ++NI;
1699      if (NI == NE)
1700        break;
1701
1702      // '.' components in Dir are ignored.
1703      while (DI != DE && *DI == ".")
1704        ++DI;
1705      if (DI == DE) {
1706        // Dir is a prefix of File, up to '.' components and choice of path
1707        // separators.
1708        unsigned PrefixLength = NI - path::begin(File);
1709        if (PrefixLength > BestPrefixLength) {
1710          BestPrefixLength = PrefixLength;
1711          BestSearchDir = I;
1712        }
1713        break;
1714      }
1715
1716      if (*NI != *DI)
1717        break;
1718    }
1719  }
1720
1721  if (IsSystem)
1722    *IsSystem = BestPrefixLength ? BestSearchDir >= SystemDirIdx : false;
1723  return File.drop_front(BestPrefixLength);
1724}
1725
clang::HeaderFileInfo::getControllingMacro
clang::HeaderSearch::PrintStats
clang::HeaderSearch::CreateHeaderMap
clang::HeaderSearch::getHeaderMapFileNames
clang::HeaderSearch::getCachedModuleFileName
clang::HeaderSearch::getPrebuiltModuleFileName
clang::HeaderSearch::getCachedModuleFileName
clang::HeaderSearch::lookupModule
clang::HeaderSearch::lookupModule
clang::DirectoryLookup::getName
clang::HeaderSearch::getFileAndSuggestModule
clang::DirectoryLookup::LookupFile
clang::DirectoryLookup::DoFrameworkLookup
clang::HeaderSearch::setTarget
clang::HeaderSearch::LookupFile
clang::HeaderSearch::LookupSubframeworkHeader
clang::HeaderSearch::getFileInfo
clang::HeaderSearch::getExistingFileInfo
clang::HeaderSearch::isFileMultipleIncludeGuarded
clang::HeaderSearch::MarkFileModuleHeader
clang::HeaderSearch::ShouldEnterIncludeFile
clang::HeaderSearch::getTotalMemory
clang::HeaderSearch::getUniqueFrameworkName
clang::HeaderSearch::hasModuleMap
clang::HeaderSearch::findModuleForHeader
clang::HeaderSearch::findUsableModuleForHeader
clang::HeaderSearch::findUsableModuleForFrameworkHeader
clang::HeaderSearch::loadModuleMapFile
clang::HeaderSearch::loadModuleMapFileImpl
clang::HeaderSearch::lookupModuleMapFile
clang::HeaderSearch::loadFrameworkModule
clang::HeaderSearch::loadModuleMapFile
clang::HeaderSearch::loadModuleMapFile
clang::HeaderSearch::collectAllModules
clang::HeaderSearch::loadTopLevelSystemModules
clang::HeaderSearch::loadSubdirectoryModuleMaps
clang::HeaderSearch::suggestPathToFileForDiagnostics
clang::HeaderSearch::suggestPathToFileForDiagnostics