• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Basic/FileManager.h"
16 #include "clang/Basic/IdentifierTable.h"
17 #include "clang/Frontend/PCHContainerOperations.h"
18 #include "clang/Lex/ExternalPreprocessorSource.h"
19 #include "clang/Lex/HeaderMap.h"
20 #include "clang/Lex/HeaderSearchOptions.h"
21 #include "clang/Lex/LexDiagnostic.h"
22 #include "clang/Lex/Lexer.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/ADT/Hashing.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Support/Capacity.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <cstdio>
32 #if defined(LLVM_ON_UNIX)
33 #include <limits.h>
34 #endif
35 using namespace clang;
36 
37 const IdentifierInfo *
getControllingMacro(ExternalPreprocessorSource * External)38 HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
39   if (ControllingMacro) {
40     if (ControllingMacro->isOutOfDate())
41       External->updateOutOfDateIdentifier(
42           *const_cast<IdentifierInfo *>(ControllingMacro));
43     return ControllingMacro;
44   }
45 
46   if (!ControllingMacroID || !External)
47     return nullptr;
48 
49   ControllingMacro = External->GetIdentifier(ControllingMacroID);
50   return ControllingMacro;
51 }
52 
~ExternalHeaderFileInfoSource()53 ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
54 
HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,SourceManager & SourceMgr,DiagnosticsEngine & Diags,const LangOptions & LangOpts,const TargetInfo * Target)55 HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
56                            SourceManager &SourceMgr, DiagnosticsEngine &Diags,
57                            const LangOptions &LangOpts,
58                            const TargetInfo *Target)
59     : HSOpts(HSOpts), Diags(Diags), FileMgr(SourceMgr.getFileManager()),
60       FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this),
61       LangOpts(LangOpts) {
62   AngledDirIdx = 0;
63   SystemDirIdx = 0;
64   NoCurDirSearch = false;
65 
66   ExternalLookup = nullptr;
67   ExternalSource = nullptr;
68   NumIncluded = 0;
69   NumMultiIncludeFileOptzn = 0;
70   NumFrameworkLookups = NumSubFrameworkLookups = 0;
71 }
72 
~HeaderSearch()73 HeaderSearch::~HeaderSearch() {
74   // Delete headermaps.
75   for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
76     delete HeaderMaps[i].second;
77 }
78 
PrintStats()79 void HeaderSearch::PrintStats() {
80   fprintf(stderr, "\n*** HeaderSearch Stats:\n");
81   fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
82   unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
83   for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
84     NumOnceOnlyFiles += FileInfo[i].isImport;
85     if (MaxNumIncludes < FileInfo[i].NumIncludes)
86       MaxNumIncludes = FileInfo[i].NumIncludes;
87     NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
88   }
89   fprintf(stderr, "  %d #import/#pragma once files.\n", NumOnceOnlyFiles);
90   fprintf(stderr, "  %d included exactly once.\n", NumSingleIncludedFiles);
91   fprintf(stderr, "  %d max times a file is included.\n", MaxNumIncludes);
92 
93   fprintf(stderr, "  %d #include/#include_next/#import.\n", NumIncluded);
94   fprintf(stderr, "    %d #includes skipped due to"
95           " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
96 
97   fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
98   fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
99 }
100 
101 /// CreateHeaderMap - This method returns a HeaderMap for the specified
102 /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
CreateHeaderMap(const FileEntry * FE)103 const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
104   // We expect the number of headermaps to be small, and almost always empty.
105   // If it ever grows, use of a linear search should be re-evaluated.
106   if (!HeaderMaps.empty()) {
107     for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
108       // Pointer equality comparison of FileEntries works because they are
109       // already uniqued by inode.
110       if (HeaderMaps[i].first == FE)
111         return HeaderMaps[i].second;
112   }
113 
114   if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
115     HeaderMaps.push_back(std::make_pair(FE, HM));
116     return HM;
117   }
118 
119   return nullptr;
120 }
121 
getModuleFileName(Module * Module)122 std::string HeaderSearch::getModuleFileName(Module *Module) {
123   const FileEntry *ModuleMap =
124       getModuleMap().getModuleMapFileForUniquing(Module);
125   return getModuleFileName(Module->Name, ModuleMap->getName());
126 }
127 
getModuleFileName(StringRef ModuleName,StringRef ModuleMapPath)128 std::string HeaderSearch::getModuleFileName(StringRef ModuleName,
129                                             StringRef ModuleMapPath) {
130   // If we don't have a module cache path or aren't supposed to use one, we
131   // can't do anything.
132   if (getModuleCachePath().empty())
133     return std::string();
134 
135   SmallString<256> Result(getModuleCachePath());
136   llvm::sys::fs::make_absolute(Result);
137 
138   if (HSOpts->DisableModuleHash) {
139     llvm::sys::path::append(Result, ModuleName + ".pcm");
140   } else {
141     // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
142     // ideally be globally unique to this particular module. Name collisions
143     // in the hash are safe (because any translation unit can only import one
144     // module with each name), but result in a loss of caching.
145     //
146     // To avoid false-negatives, we form as canonical a path as we can, and map
147     // to lower-case in case we're on a case-insensitive file system.
148    auto *Dir =
149         FileMgr.getDirectory(llvm::sys::path::parent_path(ModuleMapPath));
150     if (!Dir)
151       return std::string();
152     auto DirName = FileMgr.getCanonicalName(Dir);
153     auto FileName = llvm::sys::path::filename(ModuleMapPath);
154 
155     llvm::hash_code Hash =
156       llvm::hash_combine(DirName.lower(), FileName.lower(),
157                          HSOpts->ModuleFormat, HSOpts->UseDebugInfo);
158 
159     SmallString<128> HashStr;
160     llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
161     llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
162   }
163   return Result.str().str();
164 }
165 
lookupModule(StringRef ModuleName,bool AllowSearch)166 Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
167   // Look in the module map to determine if there is a module by this name.
168   Module *Module = ModMap.findModule(ModuleName);
169   if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
170     return Module;
171 
172   // Look through the various header search paths to load any available module
173   // maps, searching for a module map that describes this module.
174   for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
175     if (SearchDirs[Idx].isFramework()) {
176       // Search for or infer a module map for a framework.
177       SmallString<128> FrameworkDirName;
178       FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
179       llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
180       if (const DirectoryEntry *FrameworkDir
181             = FileMgr.getDirectory(FrameworkDirName)) {
182         bool IsSystem
183           = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
184         Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
185         if (Module)
186           break;
187       }
188     }
189 
190     // FIXME: Figure out how header maps and module maps will work together.
191 
192     // Only deal with normal search directories.
193     if (!SearchDirs[Idx].isNormalDir())
194       continue;
195 
196     bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
197     // Search for a module map file in this directory.
198     if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
199                           /*IsFramework*/false) == LMM_NewlyLoaded) {
200       // We just loaded a module map file; check whether the module is
201       // available now.
202       Module = ModMap.findModule(ModuleName);
203       if (Module)
204         break;
205     }
206 
207     // Search for a module map in a subdirectory with the same name as the
208     // module.
209     SmallString<128> NestedModuleMapDirName;
210     NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
211     llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
212     if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
213                           /*IsFramework*/false) == LMM_NewlyLoaded){
214       // If we just loaded a module map file, look for the module again.
215       Module = ModMap.findModule(ModuleName);
216       if (Module)
217         break;
218     }
219 
220     // If we've already performed the exhaustive search for module maps in this
221     // search directory, don't do it again.
222     if (SearchDirs[Idx].haveSearchedAllModuleMaps())
223       continue;
224 
225     // Load all module maps in the immediate subdirectories of this search
226     // directory.
227     loadSubdirectoryModuleMaps(SearchDirs[Idx]);
228 
229     // Look again for the module.
230     Module = ModMap.findModule(ModuleName);
231     if (Module)
232       break;
233   }
234 
235   return Module;
236 }
237 
238 //===----------------------------------------------------------------------===//
239 // File lookup within a DirectoryLookup scope
240 //===----------------------------------------------------------------------===//
241 
242 /// getName - Return the directory or filename corresponding to this lookup
243 /// object.
getName() const244 const char *DirectoryLookup::getName() const {
245   if (isNormalDir())
246     return getDir()->getName();
247   if (isFramework())
248     return getFrameworkDir()->getName();
249   assert(isHeaderMap() && "Unknown DirectoryLookup");
250   return getHeaderMap()->getFileName();
251 }
252 
getFileAndSuggestModule(StringRef FileName,const DirectoryEntry * Dir,bool IsSystemHeaderDir,Module * RequestingModule,ModuleMap::KnownHeader * SuggestedModule)253 const FileEntry *HeaderSearch::getFileAndSuggestModule(
254     StringRef FileName, const DirectoryEntry *Dir, bool IsSystemHeaderDir,
255     Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
256   // If we have a module map that might map this header, load it and
257   // check whether we'll have a suggestion for a module.
258   const FileEntry *File = getFileMgr().getFile(FileName, /*OpenFile=*/true);
259   if (!File)
260     return nullptr;
261 
262   // If there is a module that corresponds to this header, suggest it.
263   if (!findUsableModuleForHeader(File, Dir ? Dir : File->getDir(),
264                                  RequestingModule, SuggestedModule,
265                                  IsSystemHeaderDir))
266     return nullptr;
267 
268   return File;
269 }
270 
271 /// LookupFile - Lookup the specified file in this search path, returning it
272 /// if it exists or returning null if not.
LookupFile(StringRef & Filename,HeaderSearch & HS,SmallVectorImpl<char> * SearchPath,SmallVectorImpl<char> * RelativePath,Module * RequestingModule,ModuleMap::KnownHeader * SuggestedModule,bool & InUserSpecifiedSystemFramework,bool & HasBeenMapped,SmallVectorImpl<char> & MappedName) const273 const FileEntry *DirectoryLookup::LookupFile(
274     StringRef &Filename,
275     HeaderSearch &HS,
276     SmallVectorImpl<char> *SearchPath,
277     SmallVectorImpl<char> *RelativePath,
278     Module *RequestingModule,
279     ModuleMap::KnownHeader *SuggestedModule,
280     bool &InUserSpecifiedSystemFramework,
281     bool &HasBeenMapped,
282     SmallVectorImpl<char> &MappedName) const {
283   InUserSpecifiedSystemFramework = false;
284   HasBeenMapped = false;
285 
286   SmallString<1024> TmpDir;
287   if (isNormalDir()) {
288     // Concatenate the requested file onto the directory.
289     TmpDir = getDir()->getName();
290     llvm::sys::path::append(TmpDir, Filename);
291     if (SearchPath) {
292       StringRef SearchPathRef(getDir()->getName());
293       SearchPath->clear();
294       SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
295     }
296     if (RelativePath) {
297       RelativePath->clear();
298       RelativePath->append(Filename.begin(), Filename.end());
299     }
300 
301     return HS.getFileAndSuggestModule(TmpDir, getDir(),
302                                       isSystemHeaderDirectory(),
303                                       RequestingModule, SuggestedModule);
304   }
305 
306   if (isFramework())
307     return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
308                              RequestingModule, SuggestedModule,
309                              InUserSpecifiedSystemFramework);
310 
311   assert(isHeaderMap() && "Unknown directory lookup");
312   const HeaderMap *HM = getHeaderMap();
313   SmallString<1024> Path;
314   StringRef Dest = HM->lookupFilename(Filename, Path);
315   if (Dest.empty())
316     return nullptr;
317 
318   const FileEntry *Result;
319 
320   // Check if the headermap maps the filename to a framework include
321   // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
322   // framework include.
323   if (llvm::sys::path::is_relative(Dest)) {
324     MappedName.clear();
325     MappedName.append(Dest.begin(), Dest.end());
326     Filename = StringRef(MappedName.begin(), MappedName.size());
327     HasBeenMapped = true;
328     Result = HM->LookupFile(Filename, HS.getFileMgr());
329 
330   } else {
331     Result = HS.getFileMgr().getFile(Dest);
332   }
333 
334   if (Result) {
335     if (SearchPath) {
336       StringRef SearchPathRef(getName());
337       SearchPath->clear();
338       SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
339     }
340     if (RelativePath) {
341       RelativePath->clear();
342       RelativePath->append(Filename.begin(), Filename.end());
343     }
344   }
345   return Result;
346 }
347 
348 /// \brief Given a framework directory, find the top-most framework directory.
349 ///
350 /// \param FileMgr The file manager to use for directory lookups.
351 /// \param DirName The name of the framework directory.
352 /// \param SubmodulePath Will be populated with the submodule path from the
353 /// returned top-level module to the originally named framework.
354 static const DirectoryEntry *
getTopFrameworkDir(FileManager & FileMgr,StringRef DirName,SmallVectorImpl<std::string> & SubmodulePath)355 getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
356                    SmallVectorImpl<std::string> &SubmodulePath) {
357   assert(llvm::sys::path::extension(DirName) == ".framework" &&
358          "Not a framework directory");
359 
360   // Note: as an egregious but useful hack we use the real path here, because
361   // frameworks moving between top-level frameworks to embedded frameworks tend
362   // to be symlinked, and we base the logical structure of modules on the
363   // physical layout. In particular, we need to deal with crazy includes like
364   //
365   //   #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
366   //
367   // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
368   // which one should access with, e.g.,
369   //
370   //   #include <Bar/Wibble.h>
371   //
372   // Similar issues occur when a top-level framework has moved into an
373   // embedded framework.
374   const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
375   DirName = FileMgr.getCanonicalName(TopFrameworkDir);
376   do {
377     // Get the parent directory name.
378     DirName = llvm::sys::path::parent_path(DirName);
379     if (DirName.empty())
380       break;
381 
382     // Determine whether this directory exists.
383     const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
384     if (!Dir)
385       break;
386 
387     // If this is a framework directory, then we're a subframework of this
388     // framework.
389     if (llvm::sys::path::extension(DirName) == ".framework") {
390       SubmodulePath.push_back(llvm::sys::path::stem(DirName));
391       TopFrameworkDir = Dir;
392     }
393   } while (true);
394 
395   return TopFrameworkDir;
396 }
397 
398 /// DoFrameworkLookup - Do a lookup of the specified file in the current
399 /// DirectoryLookup, which is a framework directory.
DoFrameworkLookup(StringRef Filename,HeaderSearch & HS,SmallVectorImpl<char> * SearchPath,SmallVectorImpl<char> * RelativePath,Module * RequestingModule,ModuleMap::KnownHeader * SuggestedModule,bool & InUserSpecifiedSystemFramework) const400 const FileEntry *DirectoryLookup::DoFrameworkLookup(
401     StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
402     SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
403     ModuleMap::KnownHeader *SuggestedModule,
404     bool &InUserSpecifiedSystemFramework) const {
405   FileManager &FileMgr = HS.getFileMgr();
406 
407   // Framework names must have a '/' in the filename.
408   size_t SlashPos = Filename.find('/');
409   if (SlashPos == StringRef::npos) return nullptr;
410 
411   // Find out if this is the home for the specified framework, by checking
412   // HeaderSearch.  Possible answers are yes/no and unknown.
413   HeaderSearch::FrameworkCacheEntry &CacheEntry =
414     HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
415 
416   // If it is known and in some other directory, fail.
417   if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
418     return nullptr;
419 
420   // Otherwise, construct the path to this framework dir.
421 
422   // FrameworkName = "/System/Library/Frameworks/"
423   SmallString<1024> FrameworkName;
424   FrameworkName += getFrameworkDir()->getName();
425   if (FrameworkName.empty() || FrameworkName.back() != '/')
426     FrameworkName.push_back('/');
427 
428   // FrameworkName = "/System/Library/Frameworks/Cocoa"
429   StringRef ModuleName(Filename.begin(), SlashPos);
430   FrameworkName += ModuleName;
431 
432   // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
433   FrameworkName += ".framework/";
434 
435   // If the cache entry was unresolved, populate it now.
436   if (!CacheEntry.Directory) {
437     HS.IncrementFrameworkLookupCount();
438 
439     // If the framework dir doesn't exist, we fail.
440     const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
441     if (!Dir) return nullptr;
442 
443     // Otherwise, if it does, remember that this is the right direntry for this
444     // framework.
445     CacheEntry.Directory = getFrameworkDir();
446 
447     // If this is a user search directory, check if the framework has been
448     // user-specified as a system framework.
449     if (getDirCharacteristic() == SrcMgr::C_User) {
450       SmallString<1024> SystemFrameworkMarker(FrameworkName);
451       SystemFrameworkMarker += ".system_framework";
452       if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
453         CacheEntry.IsUserSpecifiedSystemFramework = true;
454       }
455     }
456   }
457 
458   // Set the 'user-specified system framework' flag.
459   InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
460 
461   if (RelativePath) {
462     RelativePath->clear();
463     RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
464   }
465 
466   // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
467   unsigned OrigSize = FrameworkName.size();
468 
469   FrameworkName += "Headers/";
470 
471   if (SearchPath) {
472     SearchPath->clear();
473     // Without trailing '/'.
474     SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
475   }
476 
477   FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
478   const FileEntry *FE = FileMgr.getFile(FrameworkName,
479                                         /*openFile=*/!SuggestedModule);
480   if (!FE) {
481     // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
482     const char *Private = "Private";
483     FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
484                          Private+strlen(Private));
485     if (SearchPath)
486       SearchPath->insert(SearchPath->begin()+OrigSize, Private,
487                          Private+strlen(Private));
488 
489     FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
490   }
491 
492   // If we found the header and are allowed to suggest a module, do so now.
493   if (FE && SuggestedModule) {
494     // Find the framework in which this header occurs.
495     StringRef FrameworkPath = FE->getDir()->getName();
496     bool FoundFramework = false;
497     do {
498       // Determine whether this directory exists.
499       const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
500       if (!Dir)
501         break;
502 
503       // If this is a framework directory, then we're a subframework of this
504       // framework.
505       if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
506         FoundFramework = true;
507         break;
508       }
509 
510       // Get the parent directory name.
511       FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
512       if (FrameworkPath.empty())
513         break;
514     } while (true);
515 
516     bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
517     if (FoundFramework) {
518       if (!HS.findUsableModuleForFrameworkHeader(
519               FE, FrameworkPath, RequestingModule, SuggestedModule, IsSystem))
520         return nullptr;
521     } else {
522       if (!HS.findUsableModuleForHeader(FE, getDir(), RequestingModule,
523                                         SuggestedModule, IsSystem))
524         return nullptr;
525     }
526   }
527   return FE;
528 }
529 
setTarget(const TargetInfo & Target)530 void HeaderSearch::setTarget(const TargetInfo &Target) {
531   ModMap.setTarget(Target);
532 }
533 
534 
535 //===----------------------------------------------------------------------===//
536 // Header File Location.
537 //===----------------------------------------------------------------------===//
538 
539 /// \brief Return true with a diagnostic if the file that MSVC would have found
540 /// fails to match the one that Clang would have found with MSVC header search
541 /// disabled.
checkMSVCHeaderSearch(DiagnosticsEngine & Diags,const FileEntry * MSFE,const FileEntry * FE,SourceLocation IncludeLoc)542 static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
543                                   const FileEntry *MSFE, const FileEntry *FE,
544                                   SourceLocation IncludeLoc) {
545   if (MSFE && FE != MSFE) {
546     Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
547     return true;
548   }
549   return false;
550 }
551 
copyString(StringRef Str,llvm::BumpPtrAllocator & Alloc)552 static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
553   assert(!Str.empty());
554   char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
555   std::copy(Str.begin(), Str.end(), CopyStr);
556   CopyStr[Str.size()] = '\0';
557   return CopyStr;
558 }
559 
560 /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
561 /// return null on failure.  isAngled indicates whether the file reference is
562 /// for system \#include's or not (i.e. using <> instead of ""). Includers, if
563 /// non-empty, indicates where the \#including file(s) are, in case a relative
564 /// search is needed. Microsoft mode will pass all \#including files.
LookupFile(StringRef Filename,SourceLocation IncludeLoc,bool isAngled,const DirectoryLookup * FromDir,const DirectoryLookup * & CurDir,ArrayRef<std::pair<const FileEntry *,const DirectoryEntry * >> Includers,SmallVectorImpl<char> * SearchPath,SmallVectorImpl<char> * RelativePath,Module * RequestingModule,ModuleMap::KnownHeader * SuggestedModule,bool SkipCache)565 const FileEntry *HeaderSearch::LookupFile(
566     StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
567     const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
568     ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
569     SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
570     Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
571     bool SkipCache) {
572   if (SuggestedModule)
573     *SuggestedModule = ModuleMap::KnownHeader();
574 
575   // If 'Filename' is absolute, check to see if it exists and no searching.
576   if (llvm::sys::path::is_absolute(Filename)) {
577     CurDir = nullptr;
578 
579     // If this was an #include_next "/absolute/file", fail.
580     if (FromDir) return nullptr;
581 
582     if (SearchPath)
583       SearchPath->clear();
584     if (RelativePath) {
585       RelativePath->clear();
586       RelativePath->append(Filename.begin(), Filename.end());
587     }
588     // Otherwise, just return the file.
589     return getFileAndSuggestModule(Filename, nullptr,
590                                    /*IsSystemHeaderDir*/false,
591                                    RequestingModule, SuggestedModule);
592   }
593 
594   // This is the header that MSVC's header search would have found.
595   const FileEntry *MSFE = nullptr;
596   ModuleMap::KnownHeader MSSuggestedModule;
597 
598   // Unless disabled, check to see if the file is in the #includer's
599   // directory.  This cannot be based on CurDir, because each includer could be
600   // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
601   // include of "baz.h" should resolve to "whatever/foo/baz.h".
602   // This search is not done for <> headers.
603   if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
604     SmallString<1024> TmpDir;
605     bool First = true;
606     for (const auto &IncluderAndDir : Includers) {
607       const FileEntry *Includer = IncluderAndDir.first;
608 
609       // Concatenate the requested file onto the directory.
610       // FIXME: Portability.  Filename concatenation should be in sys::Path.
611       TmpDir = IncluderAndDir.second->getName();
612       TmpDir.push_back('/');
613       TmpDir.append(Filename.begin(), Filename.end());
614 
615       // FIXME: We don't cache the result of getFileInfo across the call to
616       // getFileAndSuggestModule, because it's a reference to an element of
617       // a container that could be reallocated across this call.
618       //
619       // FIXME: If we have no includer, that means we're processing a #include
620       // from a module build. We should treat this as a system header if we're
621       // building a [system] module.
622       bool IncluderIsSystemHeader =
623           Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User;
624       if (const FileEntry *FE = getFileAndSuggestModule(
625               TmpDir, IncluderAndDir.second, IncluderIsSystemHeader,
626               RequestingModule, SuggestedModule)) {
627         if (!Includer) {
628           assert(First && "only first includer can have no file");
629           return FE;
630         }
631 
632         // Leave CurDir unset.
633         // This file is a system header or C++ unfriendly if the old file is.
634         //
635         // Note that we only use one of FromHFI/ToHFI at once, due to potential
636         // reallocation of the underlying vector potentially making the first
637         // reference binding dangling.
638         HeaderFileInfo &FromHFI = getFileInfo(Includer);
639         unsigned DirInfo = FromHFI.DirInfo;
640         bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
641         StringRef Framework = FromHFI.Framework;
642 
643         HeaderFileInfo &ToHFI = getFileInfo(FE);
644         ToHFI.DirInfo = DirInfo;
645         ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
646         ToHFI.Framework = Framework;
647 
648         if (SearchPath) {
649           StringRef SearchPathRef(IncluderAndDir.second->getName());
650           SearchPath->clear();
651           SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
652         }
653         if (RelativePath) {
654           RelativePath->clear();
655           RelativePath->append(Filename.begin(), Filename.end());
656         }
657         if (First)
658           return FE;
659 
660         // Otherwise, we found the path via MSVC header search rules.  If
661         // -Wmsvc-include is enabled, we have to keep searching to see if we
662         // would've found this header in -I or -isystem directories.
663         if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
664           return FE;
665         } else {
666           MSFE = FE;
667           if (SuggestedModule) {
668             MSSuggestedModule = *SuggestedModule;
669             *SuggestedModule = ModuleMap::KnownHeader();
670           }
671           break;
672         }
673       }
674       First = false;
675     }
676   }
677 
678   CurDir = nullptr;
679 
680   // If this is a system #include, ignore the user #include locs.
681   unsigned i = isAngled ? AngledDirIdx : 0;
682 
683   // If this is a #include_next request, start searching after the directory the
684   // file was found in.
685   if (FromDir)
686     i = FromDir-&SearchDirs[0];
687 
688   // Cache all of the lookups performed by this method.  Many headers are
689   // multiply included, and the "pragma once" optimization prevents them from
690   // being relex/pp'd, but they would still have to search through a
691   // (potentially huge) series of SearchDirs to find it.
692   LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
693 
694   // If the entry has been previously looked up, the first value will be
695   // non-zero.  If the value is equal to i (the start point of our search), then
696   // this is a matching hit.
697   if (!SkipCache && CacheLookup.StartIdx == i+1) {
698     // Skip querying potentially lots of directories for this lookup.
699     i = CacheLookup.HitIdx;
700     if (CacheLookup.MappedName)
701       Filename = CacheLookup.MappedName;
702   } else {
703     // Otherwise, this is the first query, or the previous query didn't match
704     // our search start.  We will fill in our found location below, so prime the
705     // start point value.
706     CacheLookup.reset(/*StartIdx=*/i+1);
707   }
708 
709   SmallString<64> MappedName;
710 
711   // Check each directory in sequence to see if it contains this file.
712   for (; i != SearchDirs.size(); ++i) {
713     bool InUserSpecifiedSystemFramework = false;
714     bool HasBeenMapped = false;
715     const FileEntry *FE = SearchDirs[i].LookupFile(
716         Filename, *this, SearchPath, RelativePath, RequestingModule,
717         SuggestedModule, InUserSpecifiedSystemFramework, HasBeenMapped,
718         MappedName);
719     if (HasBeenMapped) {
720       CacheLookup.MappedName =
721           copyString(Filename, LookupFileCache.getAllocator());
722     }
723     if (!FE) continue;
724 
725     CurDir = &SearchDirs[i];
726 
727     // This file is a system header or C++ unfriendly if the dir is.
728     HeaderFileInfo &HFI = getFileInfo(FE);
729     HFI.DirInfo = CurDir->getDirCharacteristic();
730 
731     // If the directory characteristic is User but this framework was
732     // user-specified to be treated as a system framework, promote the
733     // characteristic.
734     if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
735       HFI.DirInfo = SrcMgr::C_System;
736 
737     // If the filename matches a known system header prefix, override
738     // whether the file is a system header.
739     for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
740       if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
741         HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
742                                                        : SrcMgr::C_User;
743         break;
744       }
745     }
746 
747     // If this file is found in a header map and uses the framework style of
748     // includes, then this header is part of a framework we're building.
749     if (CurDir->isIndexHeaderMap()) {
750       size_t SlashPos = Filename.find('/');
751       if (SlashPos != StringRef::npos) {
752         HFI.IndexHeaderMapHeader = 1;
753         HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
754                                                          SlashPos));
755       }
756     }
757 
758     if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
759       if (SuggestedModule)
760         *SuggestedModule = MSSuggestedModule;
761       return MSFE;
762     }
763 
764     // Remember this location for the next lookup we do.
765     CacheLookup.HitIdx = i;
766     return FE;
767   }
768 
769   // If we are including a file with a quoted include "foo.h" from inside
770   // a header in a framework that is currently being built, and we couldn't
771   // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
772   // "Foo" is the name of the framework in which the including header was found.
773   if (!Includers.empty() && Includers.front().first && !isAngled &&
774       Filename.find('/') == StringRef::npos) {
775     HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
776     if (IncludingHFI.IndexHeaderMapHeader) {
777       SmallString<128> ScratchFilename;
778       ScratchFilename += IncludingHFI.Framework;
779       ScratchFilename += '/';
780       ScratchFilename += Filename;
781 
782       const FileEntry *FE =
783           LookupFile(ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir,
784                      CurDir, Includers.front(), SearchPath, RelativePath,
785                      RequestingModule, SuggestedModule);
786 
787       if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
788         if (SuggestedModule)
789           *SuggestedModule = MSSuggestedModule;
790         return MSFE;
791       }
792 
793       LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
794       CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
795       // FIXME: SuggestedModule.
796       return FE;
797     }
798   }
799 
800   if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
801     if (SuggestedModule)
802       *SuggestedModule = MSSuggestedModule;
803     return MSFE;
804   }
805 
806   // Otherwise, didn't find it. Remember we didn't find this.
807   CacheLookup.HitIdx = SearchDirs.size();
808   return nullptr;
809 }
810 
811 /// LookupSubframeworkHeader - Look up a subframework for the specified
812 /// \#include file.  For example, if \#include'ing <HIToolbox/HIToolbox.h> from
813 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
814 /// is a subframework within Carbon.framework.  If so, return the FileEntry
815 /// for the designated file, otherwise return null.
816 const FileEntry *HeaderSearch::
LookupSubframeworkHeader(StringRef Filename,const FileEntry * ContextFileEnt,SmallVectorImpl<char> * SearchPath,SmallVectorImpl<char> * RelativePath,Module * RequestingModule,ModuleMap::KnownHeader * SuggestedModule)817 LookupSubframeworkHeader(StringRef Filename,
818                          const FileEntry *ContextFileEnt,
819                          SmallVectorImpl<char> *SearchPath,
820                          SmallVectorImpl<char> *RelativePath,
821                          Module *RequestingModule,
822                          ModuleMap::KnownHeader *SuggestedModule) {
823   assert(ContextFileEnt && "No context file?");
824 
825   // Framework names must have a '/' in the filename.  Find it.
826   // FIXME: Should we permit '\' on Windows?
827   size_t SlashPos = Filename.find('/');
828   if (SlashPos == StringRef::npos) return nullptr;
829 
830   // Look up the base framework name of the ContextFileEnt.
831   const char *ContextName = ContextFileEnt->getName();
832 
833   // If the context info wasn't a framework, couldn't be a subframework.
834   const unsigned DotFrameworkLen = 10;
835   const char *FrameworkPos = strstr(ContextName, ".framework");
836   if (FrameworkPos == nullptr ||
837       (FrameworkPos[DotFrameworkLen] != '/' &&
838        FrameworkPos[DotFrameworkLen] != '\\'))
839     return nullptr;
840 
841   SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
842 
843   // Append Frameworks/HIToolbox.framework/
844   FrameworkName += "Frameworks/";
845   FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
846   FrameworkName += ".framework/";
847 
848   auto &CacheLookup =
849       *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
850                                           FrameworkCacheEntry())).first;
851 
852   // Some other location?
853   if (CacheLookup.second.Directory &&
854       CacheLookup.first().size() == FrameworkName.size() &&
855       memcmp(CacheLookup.first().data(), &FrameworkName[0],
856              CacheLookup.first().size()) != 0)
857     return nullptr;
858 
859   // Cache subframework.
860   if (!CacheLookup.second.Directory) {
861     ++NumSubFrameworkLookups;
862 
863     // If the framework dir doesn't exist, we fail.
864     const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
865     if (!Dir) return nullptr;
866 
867     // Otherwise, if it does, remember that this is the right direntry for this
868     // framework.
869     CacheLookup.second.Directory = Dir;
870   }
871 
872   const FileEntry *FE = nullptr;
873 
874   if (RelativePath) {
875     RelativePath->clear();
876     RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
877   }
878 
879   // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
880   SmallString<1024> HeadersFilename(FrameworkName);
881   HeadersFilename += "Headers/";
882   if (SearchPath) {
883     SearchPath->clear();
884     // Without trailing '/'.
885     SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
886   }
887 
888   HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
889   if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
890 
891     // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
892     HeadersFilename = FrameworkName;
893     HeadersFilename += "PrivateHeaders/";
894     if (SearchPath) {
895       SearchPath->clear();
896       // Without trailing '/'.
897       SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
898     }
899 
900     HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
901     if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true)))
902       return nullptr;
903   }
904 
905   // This file is a system header or C++ unfriendly if the old file is.
906   //
907   // Note that the temporary 'DirInfo' is required here, as either call to
908   // getFileInfo could resize the vector and we don't want to rely on order
909   // of evaluation.
910   unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
911   getFileInfo(FE).DirInfo = DirInfo;
912 
913   FrameworkName.pop_back(); // remove the trailing '/'
914   if (!findUsableModuleForFrameworkHeader(FE, FrameworkName, RequestingModule,
915                                           SuggestedModule, /*IsSystem*/ false))
916     return nullptr;
917 
918   return FE;
919 }
920 
921 //===----------------------------------------------------------------------===//
922 // File Info Management.
923 //===----------------------------------------------------------------------===//
924 
925 /// \brief Merge the header file info provided by \p OtherHFI into the current
926 /// header file info (\p HFI)
mergeHeaderFileInfo(HeaderFileInfo & HFI,const HeaderFileInfo & OtherHFI)927 static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
928                                 const HeaderFileInfo &OtherHFI) {
929   assert(OtherHFI.External && "expected to merge external HFI");
930 
931   HFI.isImport |= OtherHFI.isImport;
932   HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
933   HFI.isModuleHeader |= OtherHFI.isModuleHeader;
934   HFI.NumIncludes += OtherHFI.NumIncludes;
935 
936   if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
937     HFI.ControllingMacro = OtherHFI.ControllingMacro;
938     HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
939   }
940 
941   HFI.DirInfo = OtherHFI.DirInfo;
942   HFI.External = (!HFI.IsValid || HFI.External);
943   HFI.IsValid = true;
944   HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
945 
946   if (HFI.Framework.empty())
947     HFI.Framework = OtherHFI.Framework;
948 }
949 
950 /// getFileInfo - Return the HeaderFileInfo structure for the specified
951 /// FileEntry.
getFileInfo(const FileEntry * FE)952 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
953   if (FE->getUID() >= FileInfo.size())
954     FileInfo.resize(FE->getUID() + 1);
955 
956   HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
957   // FIXME: Use a generation count to check whether this is really up to date.
958   if (ExternalSource && !HFI->Resolved) {
959     HFI->Resolved = true;
960     auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
961 
962     HFI = &FileInfo[FE->getUID()];
963     if (ExternalHFI.External)
964       mergeHeaderFileInfo(*HFI, ExternalHFI);
965   }
966 
967   HFI->IsValid = true;
968   // We have local information about this header file, so it's no longer
969   // strictly external.
970   HFI->External = false;
971   return *HFI;
972 }
973 
974 const HeaderFileInfo *
getExistingFileInfo(const FileEntry * FE,bool WantExternal) const975 HeaderSearch::getExistingFileInfo(const FileEntry *FE,
976                                   bool WantExternal) const {
977   // If we have an external source, ensure we have the latest information.
978   // FIXME: Use a generation count to check whether this is really up to date.
979   HeaderFileInfo *HFI;
980   if (ExternalSource) {
981     if (FE->getUID() >= FileInfo.size()) {
982       if (!WantExternal)
983         return nullptr;
984       FileInfo.resize(FE->getUID() + 1);
985     }
986 
987     HFI = &FileInfo[FE->getUID()];
988     if (!WantExternal && (!HFI->IsValid || HFI->External))
989       return nullptr;
990     if (!HFI->Resolved) {
991       HFI->Resolved = true;
992       auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
993 
994       HFI = &FileInfo[FE->getUID()];
995       if (ExternalHFI.External)
996         mergeHeaderFileInfo(*HFI, ExternalHFI);
997     }
998   } else if (FE->getUID() >= FileInfo.size()) {
999     return nullptr;
1000   } else {
1001     HFI = &FileInfo[FE->getUID()];
1002   }
1003 
1004   if (!HFI->IsValid || (HFI->External && !WantExternal))
1005     return nullptr;
1006 
1007   return HFI;
1008 }
1009 
isFileMultipleIncludeGuarded(const FileEntry * File)1010 bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1011   // Check if we've ever seen this file as a header.
1012   if (auto *HFI = getExistingFileInfo(File))
1013     return HFI->isPragmaOnce || HFI->isImport || HFI->ControllingMacro ||
1014            HFI->ControllingMacroID;
1015   return false;
1016 }
1017 
MarkFileModuleHeader(const FileEntry * FE,ModuleMap::ModuleHeaderRole Role,bool isCompilingModuleHeader)1018 void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
1019                                         ModuleMap::ModuleHeaderRole Role,
1020                                         bool isCompilingModuleHeader) {
1021   bool isModularHeader = !(Role & ModuleMap::TextualHeader);
1022 
1023   // Don't mark the file info as non-external if there's nothing to change.
1024   if (!isCompilingModuleHeader) {
1025     if (!isModularHeader)
1026       return;
1027     auto *HFI = getExistingFileInfo(FE);
1028     if (HFI && HFI->isModuleHeader)
1029       return;
1030   }
1031 
1032   auto &HFI = getFileInfo(FE);
1033   HFI.isModuleHeader |= isModularHeader;
1034   HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
1035 }
1036 
ShouldEnterIncludeFile(Preprocessor & PP,const FileEntry * File,bool isImport,Module * M)1037 bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1038                                           const FileEntry *File,
1039                                           bool isImport, Module *M) {
1040   ++NumIncluded; // Count # of attempted #includes.
1041 
1042   // Get information about this file.
1043   HeaderFileInfo &FileInfo = getFileInfo(File);
1044 
1045   // If this is a #import directive, check that we have not already imported
1046   // this header.
1047   if (isImport) {
1048     // If this has already been imported, don't import it again.
1049     FileInfo.isImport = true;
1050 
1051     // Has this already been #import'ed or #include'd?
1052     if (FileInfo.NumIncludes) return false;
1053   } else {
1054     // Otherwise, if this is a #include of a file that was previously #import'd
1055     // or if this is the second #include of a #pragma once file, ignore it.
1056     if (FileInfo.isImport)
1057       return false;
1058   }
1059 
1060   // Next, check to see if the file is wrapped with #ifndef guards.  If so, and
1061   // if the macro that guards it is defined, we know the #include has no effect.
1062   if (const IdentifierInfo *ControllingMacro
1063       = FileInfo.getControllingMacro(ExternalLookup)) {
1064     // If the header corresponds to a module, check whether the macro is already
1065     // defined in that module rather than checking in the current set of visible
1066     // modules.
1067     if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M)
1068           : PP.isMacroDefined(ControllingMacro)) {
1069       ++NumMultiIncludeFileOptzn;
1070       return false;
1071     }
1072   }
1073 
1074   // Increment the number of times this file has been included.
1075   ++FileInfo.NumIncludes;
1076 
1077   return true;
1078 }
1079 
getTotalMemory() const1080 size_t HeaderSearch::getTotalMemory() const {
1081   return SearchDirs.capacity()
1082     + llvm::capacity_in_bytes(FileInfo)
1083     + llvm::capacity_in_bytes(HeaderMaps)
1084     + LookupFileCache.getAllocator().getTotalMemory()
1085     + FrameworkMap.getAllocator().getTotalMemory();
1086 }
1087 
getUniqueFrameworkName(StringRef Framework)1088 StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
1089   return FrameworkNames.insert(Framework).first->first();
1090 }
1091 
hasModuleMap(StringRef FileName,const DirectoryEntry * Root,bool IsSystem)1092 bool HeaderSearch::hasModuleMap(StringRef FileName,
1093                                 const DirectoryEntry *Root,
1094                                 bool IsSystem) {
1095   if (!HSOpts->ImplicitModuleMaps)
1096     return false;
1097 
1098   SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
1099 
1100   StringRef DirName = FileName;
1101   do {
1102     // Get the parent directory name.
1103     DirName = llvm::sys::path::parent_path(DirName);
1104     if (DirName.empty())
1105       return false;
1106 
1107     // Determine whether this directory exists.
1108     const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
1109     if (!Dir)
1110       return false;
1111 
1112     // Try to load the module map file in this directory.
1113     switch (loadModuleMapFile(Dir, IsSystem,
1114                               llvm::sys::path::extension(Dir->getName()) ==
1115                                   ".framework")) {
1116     case LMM_NewlyLoaded:
1117     case LMM_AlreadyLoaded:
1118       // Success. All of the directories we stepped through inherit this module
1119       // map file.
1120       for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1121         DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1122       return true;
1123 
1124     case LMM_NoDirectory:
1125     case LMM_InvalidModuleMap:
1126       break;
1127     }
1128 
1129     // If we hit the top of our search, we're done.
1130     if (Dir == Root)
1131       return false;
1132 
1133     // Keep track of all of the directories we checked, so we can mark them as
1134     // having module maps if we eventually do find a module map.
1135     FixUpDirectories.push_back(Dir);
1136   } while (true);
1137 }
1138 
1139 ModuleMap::KnownHeader
findModuleForHeader(const FileEntry * File) const1140 HeaderSearch::findModuleForHeader(const FileEntry *File) const {
1141   if (ExternalSource) {
1142     // Make sure the external source has handled header info about this file,
1143     // which includes whether the file is part of a module.
1144     (void)getExistingFileInfo(File);
1145   }
1146   return ModMap.findModuleForHeader(File);
1147 }
1148 
findUsableModuleForHeader(const FileEntry * File,const DirectoryEntry * Root,Module * RequestingModule,ModuleMap::KnownHeader * SuggestedModule,bool IsSystemHeaderDir)1149 bool HeaderSearch::findUsableModuleForHeader(
1150     const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
1151     ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1152   if (File && SuggestedModule) {
1153     // If there is a module that corresponds to this header, suggest it.
1154     hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
1155     *SuggestedModule = findModuleForHeader(File);
1156   }
1157   return true;
1158 }
1159 
findUsableModuleForFrameworkHeader(const FileEntry * File,StringRef FrameworkName,Module * RequestingModule,ModuleMap::KnownHeader * SuggestedModule,bool IsSystemFramework)1160 bool HeaderSearch::findUsableModuleForFrameworkHeader(
1161     const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
1162     ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1163   // If we're supposed to suggest a module, look for one now.
1164   if (SuggestedModule) {
1165     // Find the top-level framework based on this framework.
1166     SmallVector<std::string, 4> SubmodulePath;
1167     const DirectoryEntry *TopFrameworkDir
1168       = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1169 
1170     // Determine the name of the top-level framework.
1171     StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1172 
1173     // Load this framework module. If that succeeds, find the suggested module
1174     // for this header, if any.
1175     loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1176 
1177     // FIXME: This can find a module not part of ModuleName, which is
1178     // important so that we're consistent about whether this header
1179     // corresponds to a module. Possibly we should lock down framework modules
1180     // so that this is not possible.
1181     *SuggestedModule = findModuleForHeader(File);
1182   }
1183   return true;
1184 }
1185 
getPrivateModuleMap(const FileEntry * File,FileManager & FileMgr)1186 static const FileEntry *getPrivateModuleMap(const FileEntry *File,
1187                                             FileManager &FileMgr) {
1188   StringRef Filename = llvm::sys::path::filename(File->getName());
1189   SmallString<128>  PrivateFilename(File->getDir()->getName());
1190   if (Filename == "module.map")
1191     llvm::sys::path::append(PrivateFilename, "module_private.map");
1192   else if (Filename == "module.modulemap")
1193     llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1194   else
1195     return nullptr;
1196   return FileMgr.getFile(PrivateFilename);
1197 }
1198 
loadModuleMapFile(const FileEntry * File,bool IsSystem)1199 bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
1200   // Find the directory for the module. For frameworks, that may require going
1201   // up from the 'Modules' directory.
1202   const DirectoryEntry *Dir = nullptr;
1203   if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
1204     Dir = FileMgr.getDirectory(".");
1205   else {
1206     Dir = File->getDir();
1207     StringRef DirName(Dir->getName());
1208     if (llvm::sys::path::filename(DirName) == "Modules") {
1209       DirName = llvm::sys::path::parent_path(DirName);
1210       if (DirName.endswith(".framework"))
1211         Dir = FileMgr.getDirectory(DirName);
1212       // FIXME: This assert can fail if there's a race between the above check
1213       // and the removal of the directory.
1214       assert(Dir && "parent must exist");
1215     }
1216   }
1217 
1218   switch (loadModuleMapFileImpl(File, IsSystem, Dir)) {
1219   case LMM_AlreadyLoaded:
1220   case LMM_NewlyLoaded:
1221     return false;
1222   case LMM_NoDirectory:
1223   case LMM_InvalidModuleMap:
1224     return true;
1225   }
1226   llvm_unreachable("Unknown load module map result");
1227 }
1228 
1229 HeaderSearch::LoadModuleMapResult
loadModuleMapFileImpl(const FileEntry * File,bool IsSystem,const DirectoryEntry * Dir)1230 HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1231                                     const DirectoryEntry *Dir) {
1232   assert(File && "expected FileEntry");
1233 
1234   // Check whether we've already loaded this module map, and mark it as being
1235   // loaded in case we recursively try to load it from itself.
1236   auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1237   if (!AddResult.second)
1238     return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1239 
1240   if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) {
1241     LoadedModuleMaps[File] = false;
1242     return LMM_InvalidModuleMap;
1243   }
1244 
1245   // Try to load a corresponding private module map.
1246   if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1247     if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
1248       LoadedModuleMaps[File] = false;
1249       return LMM_InvalidModuleMap;
1250     }
1251   }
1252 
1253   // This directory has a module map.
1254   return LMM_NewlyLoaded;
1255 }
1256 
1257 const FileEntry *
lookupModuleMapFile(const DirectoryEntry * Dir,bool IsFramework)1258 HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
1259   if (!HSOpts->ImplicitModuleMaps)
1260     return nullptr;
1261   // For frameworks, the preferred spelling is Modules/module.modulemap, but
1262   // module.map at the framework root is also accepted.
1263   SmallString<128> ModuleMapFileName(Dir->getName());
1264   if (IsFramework)
1265     llvm::sys::path::append(ModuleMapFileName, "Modules");
1266   llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1267   if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
1268     return F;
1269 
1270   // Continue to allow module.map
1271   ModuleMapFileName = Dir->getName();
1272   llvm::sys::path::append(ModuleMapFileName, "module.map");
1273   return FileMgr.getFile(ModuleMapFileName);
1274 }
1275 
loadFrameworkModule(StringRef Name,const DirectoryEntry * Dir,bool IsSystem)1276 Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1277                                           const DirectoryEntry *Dir,
1278                                           bool IsSystem) {
1279   if (Module *Module = ModMap.findModule(Name))
1280     return Module;
1281 
1282   // Try to load a module map file.
1283   switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
1284   case LMM_InvalidModuleMap:
1285     // Try to infer a module map from the framework directory.
1286     if (HSOpts->ImplicitModuleMaps)
1287       ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
1288     break;
1289 
1290   case LMM_AlreadyLoaded:
1291   case LMM_NoDirectory:
1292     return nullptr;
1293 
1294   case LMM_NewlyLoaded:
1295     break;
1296   }
1297 
1298   return ModMap.findModule(Name);
1299 }
1300 
1301 
1302 HeaderSearch::LoadModuleMapResult
loadModuleMapFile(StringRef DirName,bool IsSystem,bool IsFramework)1303 HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1304                                 bool IsFramework) {
1305   if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
1306     return loadModuleMapFile(Dir, IsSystem, IsFramework);
1307 
1308   return LMM_NoDirectory;
1309 }
1310 
1311 HeaderSearch::LoadModuleMapResult
loadModuleMapFile(const DirectoryEntry * Dir,bool IsSystem,bool IsFramework)1312 HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1313                                 bool IsFramework) {
1314   auto KnownDir = DirectoryHasModuleMap.find(Dir);
1315   if (KnownDir != DirectoryHasModuleMap.end())
1316     return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1317 
1318   if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
1319     LoadModuleMapResult Result =
1320         loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
1321     // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1322     // E.g. Foo.framework/Modules/module.modulemap
1323     //      ^Dir                  ^ModuleMapFile
1324     if (Result == LMM_NewlyLoaded)
1325       DirectoryHasModuleMap[Dir] = true;
1326     else if (Result == LMM_InvalidModuleMap)
1327       DirectoryHasModuleMap[Dir] = false;
1328     return Result;
1329   }
1330   return LMM_InvalidModuleMap;
1331 }
1332 
collectAllModules(SmallVectorImpl<Module * > & Modules)1333 void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
1334   Modules.clear();
1335 
1336   if (HSOpts->ImplicitModuleMaps) {
1337     // Load module maps for each of the header search directories.
1338     for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1339       bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1340       if (SearchDirs[Idx].isFramework()) {
1341         std::error_code EC;
1342         SmallString<128> DirNative;
1343         llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1344                                 DirNative);
1345 
1346         // Search each of the ".framework" directories to load them as modules.
1347         for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
1348              Dir != DirEnd && !EC; Dir.increment(EC)) {
1349           if (llvm::sys::path::extension(Dir->path()) != ".framework")
1350             continue;
1351 
1352           const DirectoryEntry *FrameworkDir =
1353               FileMgr.getDirectory(Dir->path());
1354           if (!FrameworkDir)
1355             continue;
1356 
1357           // Load this framework module.
1358           loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1359                               IsSystem);
1360         }
1361         continue;
1362       }
1363 
1364       // FIXME: Deal with header maps.
1365       if (SearchDirs[Idx].isHeaderMap())
1366         continue;
1367 
1368       // Try to load a module map file for the search directory.
1369       loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1370                         /*IsFramework*/ false);
1371 
1372       // Try to load module map files for immediate subdirectories of this
1373       // search directory.
1374       loadSubdirectoryModuleMaps(SearchDirs[Idx]);
1375     }
1376   }
1377 
1378   // Populate the list of modules.
1379   for (ModuleMap::module_iterator M = ModMap.module_begin(),
1380                                MEnd = ModMap.module_end();
1381        M != MEnd; ++M) {
1382     Modules.push_back(M->getValue());
1383   }
1384 }
1385 
loadTopLevelSystemModules()1386 void HeaderSearch::loadTopLevelSystemModules() {
1387   if (!HSOpts->ImplicitModuleMaps)
1388     return;
1389 
1390   // Load module maps for each of the header search directories.
1391   for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1392     // We only care about normal header directories.
1393     if (!SearchDirs[Idx].isNormalDir()) {
1394       continue;
1395     }
1396 
1397     // Try to load a module map file for the search directory.
1398     loadModuleMapFile(SearchDirs[Idx].getDir(),
1399                       SearchDirs[Idx].isSystemHeaderDirectory(),
1400                       SearchDirs[Idx].isFramework());
1401   }
1402 }
1403 
loadSubdirectoryModuleMaps(DirectoryLookup & SearchDir)1404 void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1405   assert(HSOpts->ImplicitModuleMaps &&
1406          "Should not be loading subdirectory module maps");
1407 
1408   if (SearchDir.haveSearchedAllModuleMaps())
1409     return;
1410 
1411   std::error_code EC;
1412   SmallString<128> DirNative;
1413   llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
1414   for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
1415        Dir != DirEnd && !EC; Dir.increment(EC)) {
1416     bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1417     if (IsFramework == SearchDir.isFramework())
1418       loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1419                         SearchDir.isFramework());
1420   }
1421 
1422   SearchDir.setSearchedAllModuleMaps(true);
1423 }
1424