1 //===- HeaderSearch.h - Resolve Header File Locations -----------*- C++ -*-===// 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 defines the HeaderSearch interface. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_LEX_HEADERSEARCH_H 14 #define LLVM_CLANG_LEX_HEADERSEARCH_H 15 16 #include "clang/Basic/SourceLocation.h" 17 #include "clang/Basic/SourceManager.h" 18 #include "clang/Lex/DirectoryLookup.h" 19 #include "clang/Lex/ExternalPreprocessorSource.h" 20 #include "clang/Lex/HeaderMap.h" 21 #include "clang/Lex/ModuleMap.h" 22 #include "llvm/ADT/ArrayRef.h" 23 #include "llvm/ADT/DenseMap.h" 24 #include "llvm/ADT/SmallString.h" 25 #include "llvm/ADT/StringMap.h" 26 #include "llvm/ADT/StringRef.h" 27 #include "llvm/ADT/StringSet.h" 28 #include "llvm/Support/Allocator.h" 29 #include <cassert> 30 #include <cstddef> 31 #include <memory> 32 #include <string> 33 #include <utility> 34 #include <vector> 35 36 namespace llvm { 37 38 class Triple; 39 40 } // namespace llvm 41 42 namespace clang { 43 44 class DiagnosticsEngine; 45 class DirectoryEntry; 46 class ExternalPreprocessorSource; 47 class FileEntry; 48 class FileManager; 49 class HeaderSearch; 50 class HeaderSearchOptions; 51 class IdentifierInfo; 52 class LangOptions; 53 class Module; 54 class Preprocessor; 55 class TargetInfo; 56 57 /// The preprocessor keeps track of this information for each 58 /// file that is \#included. 59 struct HeaderFileInfo { 60 // TODO: Whether the file was included is not a property of the file itself. 61 // It's a preprocessor state, move it there. 62 /// True if this file has been included (or imported) **locally**. 63 LLVM_PREFERRED_TYPE(bool) 64 unsigned IsLocallyIncluded : 1; 65 66 // TODO: Whether the file was imported is not a property of the file itself. 67 // It's a preprocessor state, move it there. 68 /// True if this is a \#import'd file. 69 LLVM_PREFERRED_TYPE(bool) 70 unsigned isImport : 1; 71 72 /// True if this is a \#pragma once file. 73 LLVM_PREFERRED_TYPE(bool) 74 unsigned isPragmaOnce : 1; 75 76 /// Keep track of whether this is a system header, and if so, 77 /// whether it is C++ clean or not. This can be set by the include paths or 78 /// by \#pragma gcc system_header. This is an instance of 79 /// SrcMgr::CharacteristicKind. 80 LLVM_PREFERRED_TYPE(SrcMgr::CharacteristicKind) 81 unsigned DirInfo : 3; 82 83 /// Whether this header file info was supplied by an external source, 84 /// and has not changed since. 85 LLVM_PREFERRED_TYPE(bool) 86 unsigned External : 1; 87 88 /// Whether this header is part of and built with a module. i.e. it is listed 89 /// in a module map, and is not `excluded` or `textual`. (same meaning as 90 /// `ModuleMap::isModular()`). 91 LLVM_PREFERRED_TYPE(bool) 92 unsigned isModuleHeader : 1; 93 94 /// Whether this header is a `textual header` in a module. If a header is 95 /// textual in one module and normal in another module, this bit will not be 96 /// set, only `isModuleHeader`. 97 LLVM_PREFERRED_TYPE(bool) 98 unsigned isTextualModuleHeader : 1; 99 100 /// Whether this header is part of the module that we are building, even if it 101 /// doesn't build with the module. i.e. this will include `excluded` and 102 /// `textual` headers as well as normal headers. 103 LLVM_PREFERRED_TYPE(bool) 104 unsigned isCompilingModuleHeader : 1; 105 106 /// Whether this structure is considered to already have been 107 /// "resolved", meaning that it was loaded from the external source. 108 LLVM_PREFERRED_TYPE(bool) 109 unsigned Resolved : 1; 110 111 /// Whether this is a header inside a framework that is currently 112 /// being built. 113 /// 114 /// When a framework is being built, the headers have not yet been placed 115 /// into the appropriate framework subdirectories, and therefore are 116 /// provided via a header map. This bit indicates when this is one of 117 /// those framework headers. 118 LLVM_PREFERRED_TYPE(bool) 119 unsigned IndexHeaderMapHeader : 1; 120 121 /// Whether this file has been looked up as a header. 122 LLVM_PREFERRED_TYPE(bool) 123 unsigned IsValid : 1; 124 125 /// If this file has a \#ifndef XXX (or equivalent) guard that 126 /// protects the entire contents of the file, this is the identifier 127 /// for the macro that controls whether or not it has any effect. 128 /// 129 /// Note: Most clients should use getControllingMacro() to access 130 /// the controlling macro of this header, since 131 /// getControllingMacro() is able to load a controlling macro from 132 /// external storage. 133 LazyIdentifierInfoPtr LazyControllingMacro; 134 135 /// If this header came from a framework include, this is the name 136 /// of the framework. 137 StringRef Framework; 138 HeaderFileInfoHeaderFileInfo139 HeaderFileInfo() 140 : IsLocallyIncluded(false), isImport(false), isPragmaOnce(false), 141 DirInfo(SrcMgr::C_User), External(false), isModuleHeader(false), 142 isTextualModuleHeader(false), isCompilingModuleHeader(false), 143 Resolved(false), IndexHeaderMapHeader(false), IsValid(false) {} 144 145 /// Retrieve the controlling macro for this header file, if 146 /// any. 147 const IdentifierInfo * 148 getControllingMacro(ExternalPreprocessorSource *External); 149 150 /// Update the module membership bits based on the header role. 151 /// 152 /// isModuleHeader will potentially be set, but not cleared. 153 /// isTextualModuleHeader will be set or cleared based on the role update. 154 void mergeModuleMembership(ModuleMap::ModuleHeaderRole Role); 155 }; 156 157 /// An external source of header file information, which may supply 158 /// information about header files already included. 159 class ExternalHeaderFileInfoSource { 160 public: 161 virtual ~ExternalHeaderFileInfoSource(); 162 163 /// Retrieve the header file information for the given file entry. 164 /// 165 /// \returns Header file information for the given file entry, with the 166 /// \c External bit set. If the file entry is not known, return a 167 /// default-constructed \c HeaderFileInfo. 168 virtual HeaderFileInfo GetHeaderFileInfo(FileEntryRef FE) = 0; 169 }; 170 171 /// This structure is used to record entries in our framework cache. 172 struct FrameworkCacheEntry { 173 /// The directory entry which should be used for the cached framework. 174 OptionalDirectoryEntryRef Directory; 175 176 /// Whether this framework has been "user-specified" to be treated as if it 177 /// were a system framework (even if it was found outside a system framework 178 /// directory). 179 bool IsUserSpecifiedSystemFramework; 180 }; 181 182 namespace detail { 183 template <bool Const, typename T> 184 using Qualified = std::conditional_t<Const, const T, T>; 185 186 /// Forward iterator over the search directories of \c HeaderSearch. 187 template <bool IsConst> 188 struct SearchDirIteratorImpl 189 : llvm::iterator_facade_base<SearchDirIteratorImpl<IsConst>, 190 std::forward_iterator_tag, 191 Qualified<IsConst, DirectoryLookup>> { 192 /// Const -> non-const iterator conversion. 193 template <typename Enable = std::enable_if<IsConst, bool>> SearchDirIteratorImplSearchDirIteratorImpl194 SearchDirIteratorImpl(const SearchDirIteratorImpl<false> &Other) 195 : HS(Other.HS), Idx(Other.Idx) {} 196 197 SearchDirIteratorImpl(const SearchDirIteratorImpl &) = default; 198 199 SearchDirIteratorImpl &operator=(const SearchDirIteratorImpl &) = default; 200 201 bool operator==(const SearchDirIteratorImpl &RHS) const { 202 return HS == RHS.HS && Idx == RHS.Idx; 203 } 204 205 SearchDirIteratorImpl &operator++() { 206 assert(*this && "Invalid iterator."); 207 ++Idx; 208 return *this; 209 } 210 211 Qualified<IsConst, DirectoryLookup> &operator*() const { 212 assert(*this && "Invalid iterator."); 213 return HS->SearchDirs[Idx]; 214 } 215 216 /// Creates an invalid iterator. SearchDirIteratorImplSearchDirIteratorImpl217 SearchDirIteratorImpl(std::nullptr_t) : HS(nullptr), Idx(0) {} 218 219 /// Checks whether the iterator is valid. 220 explicit operator bool() const { return HS != nullptr; } 221 222 private: 223 /// The parent \c HeaderSearch. This is \c nullptr for invalid iterator. 224 Qualified<IsConst, HeaderSearch> *HS; 225 226 /// The index of the current element. 227 size_t Idx; 228 229 /// The constructor that creates a valid iterator. SearchDirIteratorImplSearchDirIteratorImpl230 SearchDirIteratorImpl(Qualified<IsConst, HeaderSearch> &HS, size_t Idx) 231 : HS(&HS), Idx(Idx) {} 232 233 /// Only HeaderSearch is allowed to instantiate valid iterators. 234 friend HeaderSearch; 235 236 /// Enables const -> non-const conversion. 237 friend SearchDirIteratorImpl<!IsConst>; 238 }; 239 } // namespace detail 240 241 using ConstSearchDirIterator = detail::SearchDirIteratorImpl<true>; 242 using SearchDirIterator = detail::SearchDirIteratorImpl<false>; 243 244 using ConstSearchDirRange = llvm::iterator_range<ConstSearchDirIterator>; 245 using SearchDirRange = llvm::iterator_range<SearchDirIterator>; 246 247 /// Encapsulates the information needed to find the file referenced 248 /// by a \#include or \#include_next, (sub-)framework lookup, etc. 249 class HeaderSearch { 250 friend class DirectoryLookup; 251 252 friend ConstSearchDirIterator; 253 friend SearchDirIterator; 254 255 /// Header-search options used to initialize this header search. 256 std::shared_ptr<HeaderSearchOptions> HSOpts; 257 258 /// Mapping from SearchDir to HeaderSearchOptions::UserEntries indices. 259 llvm::DenseMap<unsigned, unsigned> SearchDirToHSEntry; 260 261 DiagnosticsEngine &Diags; 262 FileManager &FileMgr; 263 264 /// \#include search path information. Requests for \#include "x" search the 265 /// directory of the \#including file first, then each directory in SearchDirs 266 /// consecutively. Requests for <x> search the current dir first, then each 267 /// directory in SearchDirs, starting at AngledDirIdx, consecutively. 268 std::vector<DirectoryLookup> SearchDirs; 269 /// Whether the DirectoryLookup at the corresponding index in SearchDirs has 270 /// been successfully used to lookup a file. 271 std::vector<bool> SearchDirsUsage; 272 unsigned AngledDirIdx = 0; 273 unsigned SystemDirIdx = 0; 274 275 /// Maps HeaderMap keys to SearchDir indices. When HeaderMaps are used 276 /// heavily, SearchDirs can start with thousands of HeaderMaps, so this Index 277 /// lets us avoid scanning them all to find a match. 278 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> SearchDirHeaderMapIndex; 279 280 /// The index of the first SearchDir that isn't a header map. 281 unsigned FirstNonHeaderMapSearchDirIdx = 0; 282 283 /// \#include prefixes for which the 'system header' property is 284 /// overridden. 285 /// 286 /// For a \#include "x" or \#include \<x> directive, the last string in this 287 /// list which is a prefix of 'x' determines whether the file is treated as 288 /// a system header. 289 std::vector<std::pair<std::string, bool>> SystemHeaderPrefixes; 290 291 /// The hash used for module cache paths. 292 std::string ModuleHash; 293 294 /// The path to the module cache. 295 std::string ModuleCachePath; 296 297 /// All of the preprocessor-specific data about files that are 298 /// included, indexed by the FileEntry's UID. 299 mutable std::vector<HeaderFileInfo> FileInfo; 300 301 /// Keeps track of each lookup performed by LookupFile. 302 struct LookupFileCacheInfo { 303 // The requesting module for the lookup we cached. 304 const Module *RequestingModule = nullptr; 305 306 /// Starting search directory iterator that the cached search was performed 307 /// from. If there is a hit and this value doesn't match the current query, 308 /// the cache has to be ignored. 309 ConstSearchDirIterator StartIt = nullptr; 310 311 /// The search directory iterator that satisfied the query. 312 ConstSearchDirIterator HitIt = nullptr; 313 314 /// This is non-null if the original filename was mapped to a framework 315 /// include via a headermap. 316 const char *MappedName = nullptr; 317 318 /// Default constructor -- Initialize all members with zero. 319 LookupFileCacheInfo() = default; 320 resetLookupFileCacheInfo321 void reset(const Module *NewRequestingModule, 322 ConstSearchDirIterator NewStartIt) { 323 RequestingModule = NewRequestingModule; 324 StartIt = NewStartIt; 325 MappedName = nullptr; 326 } 327 }; 328 llvm::StringMap<LookupFileCacheInfo, llvm::BumpPtrAllocator> LookupFileCache; 329 330 /// Collection mapping a framework or subframework 331 /// name like "Carbon" to the Carbon.framework directory. 332 llvm::StringMap<FrameworkCacheEntry, llvm::BumpPtrAllocator> FrameworkMap; 333 334 /// Maps include file names (including the quotes or 335 /// angle brackets) to other include file names. This is used to support the 336 /// include_alias pragma for Microsoft compatibility. 337 using IncludeAliasMap = 338 llvm::StringMap<std::string, llvm::BumpPtrAllocator>; 339 std::unique_ptr<IncludeAliasMap> IncludeAliases; 340 341 /// This is a mapping from FileEntry -> HeaderMap, uniquing headermaps. 342 std::vector<std::pair<FileEntryRef, std::unique_ptr<HeaderMap>>> HeaderMaps; 343 344 /// The mapping between modules and headers. 345 mutable ModuleMap ModMap; 346 347 /// Describes whether a given directory has a module map in it. 348 llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap; 349 350 /// Set of module map files we've already loaded, and a flag indicating 351 /// whether they were valid or not. 352 llvm::DenseMap<const FileEntry *, bool> LoadedModuleMaps; 353 354 // A map of discovered headers with their associated include file name. 355 llvm::DenseMap<const FileEntry *, llvm::SmallString<64>> IncludeNames; 356 357 /// Uniqued set of framework names, which is used to track which 358 /// headers were included as framework headers. 359 llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames; 360 361 /// Entity used to resolve the identifier IDs of controlling 362 /// macros into IdentifierInfo pointers, and keep the identifire up to date, 363 /// as needed. 364 ExternalPreprocessorSource *ExternalLookup = nullptr; 365 366 /// Entity used to look up stored header file information. 367 ExternalHeaderFileInfoSource *ExternalSource = nullptr; 368 369 /// Scan all of the header maps at the beginning of SearchDirs and 370 /// map their keys to the SearchDir index of their header map. 371 void indexInitialHeaderMaps(); 372 373 public: 374 HeaderSearch(std::shared_ptr<HeaderSearchOptions> HSOpts, 375 SourceManager &SourceMgr, DiagnosticsEngine &Diags, 376 const LangOptions &LangOpts, const TargetInfo *Target); 377 HeaderSearch(const HeaderSearch &) = delete; 378 HeaderSearch &operator=(const HeaderSearch &) = delete; 379 380 /// Retrieve the header-search options with which this header search 381 /// was initialized. getHeaderSearchOpts()382 HeaderSearchOptions &getHeaderSearchOpts() const { return *HSOpts; } 383 getFileMgr()384 FileManager &getFileMgr() const { return FileMgr; } 385 getDiags()386 DiagnosticsEngine &getDiags() const { return Diags; } 387 388 /// Interface for setting the file search paths. 389 void SetSearchPaths(std::vector<DirectoryLookup> dirs, unsigned angledDirIdx, 390 unsigned systemDirIdx, 391 llvm::DenseMap<unsigned, unsigned> searchDirToHSEntry); 392 393 /// Add an additional search path. 394 void AddSearchPath(const DirectoryLookup &dir, bool isAngled); 395 396 /// Add an additional system search path. AddSystemSearchPath(const DirectoryLookup & dir)397 void AddSystemSearchPath(const DirectoryLookup &dir) { 398 SearchDirs.push_back(dir); 399 SearchDirsUsage.push_back(false); 400 } 401 402 /// Set the list of system header prefixes. SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string,bool>> P)403 void SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string, bool>> P) { 404 SystemHeaderPrefixes.assign(P.begin(), P.end()); 405 } 406 407 /// Checks whether the map exists or not. HasIncludeAliasMap()408 bool HasIncludeAliasMap() const { return (bool)IncludeAliases; } 409 410 /// Map the source include name to the dest include name. 411 /// 412 /// The Source should include the angle brackets or quotes, the dest 413 /// should not. This allows for distinction between <> and "" headers. AddIncludeAlias(StringRef Source,StringRef Dest)414 void AddIncludeAlias(StringRef Source, StringRef Dest) { 415 if (!IncludeAliases) 416 IncludeAliases.reset(new IncludeAliasMap); 417 (*IncludeAliases)[Source] = std::string(Dest); 418 } 419 420 /// Maps one header file name to a different header 421 /// file name, for use with the include_alias pragma. Note that the source 422 /// file name should include the angle brackets or quotes. Returns StringRef 423 /// as null if the header cannot be mapped. MapHeaderToIncludeAlias(StringRef Source)424 StringRef MapHeaderToIncludeAlias(StringRef Source) { 425 assert(IncludeAliases && "Trying to map headers when there's no map"); 426 427 // Do any filename replacements before anything else 428 IncludeAliasMap::const_iterator Iter = IncludeAliases->find(Source); 429 if (Iter != IncludeAliases->end()) 430 return Iter->second; 431 return {}; 432 } 433 434 /// Set the hash to use for module cache paths. setModuleHash(StringRef Hash)435 void setModuleHash(StringRef Hash) { ModuleHash = std::string(Hash); } 436 437 /// Set the path to the module cache. setModuleCachePath(StringRef CachePath)438 void setModuleCachePath(StringRef CachePath) { 439 ModuleCachePath = std::string(CachePath); 440 } 441 442 /// Retrieve the module hash. getModuleHash()443 StringRef getModuleHash() const { return ModuleHash; } 444 445 /// Retrieve the path to the module cache. getModuleCachePath()446 StringRef getModuleCachePath() const { return ModuleCachePath; } 447 448 /// Consider modules when including files from this directory. setDirectoryHasModuleMap(const DirectoryEntry * Dir)449 void setDirectoryHasModuleMap(const DirectoryEntry* Dir) { 450 DirectoryHasModuleMap[Dir] = true; 451 } 452 453 /// Forget everything we know about headers so far. ClearFileInfo()454 void ClearFileInfo() { 455 FileInfo.clear(); 456 } 457 SetExternalLookup(ExternalPreprocessorSource * EPS)458 void SetExternalLookup(ExternalPreprocessorSource *EPS) { 459 ExternalLookup = EPS; 460 } 461 getExternalLookup()462 ExternalPreprocessorSource *getExternalLookup() const { 463 return ExternalLookup; 464 } 465 466 /// Set the external source of header information. SetExternalSource(ExternalHeaderFileInfoSource * ES)467 void SetExternalSource(ExternalHeaderFileInfoSource *ES) { 468 ExternalSource = ES; 469 } 470 471 /// Set the target information for the header search, if not 472 /// already known. 473 void setTarget(const TargetInfo &Target); 474 475 /// Given a "foo" or \<foo> reference, look up the indicated file, 476 /// return null on failure. 477 /// 478 /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member 479 /// the file was found in, or null if not applicable. 480 /// 481 /// \param IncludeLoc Used for diagnostics if valid. 482 /// 483 /// \param isAngled indicates whether the file reference is a <> reference. 484 /// 485 /// \param CurDir If non-null, the file was found in the specified directory 486 /// search location. This is used to implement \#include_next. 487 /// 488 /// \param Includers Indicates where the \#including file(s) are, in case 489 /// relative searches are needed. In reverse order of inclusion. 490 /// 491 /// \param SearchPath If non-null, will be set to the search path relative 492 /// to which the file was found. If the include path is absolute, SearchPath 493 /// will be set to an empty string. 494 /// 495 /// \param RelativePath If non-null, will be set to the path relative to 496 /// SearchPath at which the file was found. This only differs from the 497 /// Filename for framework includes. 498 /// 499 /// \param SuggestedModule If non-null, and the file found is semantically 500 /// part of a known module, this will be set to the module that should 501 /// be imported instead of preprocessing/parsing the file found. 502 /// 503 /// \param IsMapped If non-null, and the search involved header maps, set to 504 /// true. 505 /// 506 /// \param IsFrameworkFound If non-null, will be set to true if a framework is 507 /// found in any of searched SearchDirs. Will be set to false if a framework 508 /// is found only through header maps. Doesn't guarantee the requested file is 509 /// found. 510 OptionalFileEntryRef LookupFile( 511 StringRef Filename, SourceLocation IncludeLoc, bool isAngled, 512 ConstSearchDirIterator FromDir, ConstSearchDirIterator *CurDir, 513 ArrayRef<std::pair<OptionalFileEntryRef, DirectoryEntryRef>> Includers, 514 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath, 515 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule, 516 bool *IsMapped, bool *IsFrameworkFound, bool SkipCache = false, 517 bool BuildSystemModule = false, bool OpenFile = true, 518 bool CacheFailures = true); 519 520 /// Look up a subframework for the specified \#include file. 521 /// 522 /// For example, if \#include'ing <HIToolbox/HIToolbox.h> from 523 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if 524 /// HIToolbox is a subframework within Carbon.framework. If so, return 525 /// the FileEntry for the designated file, otherwise return null. 526 OptionalFileEntryRef LookupSubframeworkHeader( 527 StringRef Filename, FileEntryRef ContextFileEnt, 528 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath, 529 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule); 530 531 /// Look up the specified framework name in our framework cache. 532 /// \returns The DirectoryEntry it is in if we know, null otherwise. LookupFrameworkCache(StringRef FWName)533 FrameworkCacheEntry &LookupFrameworkCache(StringRef FWName) { 534 return FrameworkMap[FWName]; 535 } 536 537 /// Mark the specified file as a target of a \#include, 538 /// \#include_next, or \#import directive. 539 /// 540 /// \return false if \#including the file will have no effect or true 541 /// if we should include it. 542 /// 543 /// \param M The module to which `File` belongs (this should usually be the 544 /// SuggestedModule returned by LookupFile/LookupSubframeworkHeader) 545 bool ShouldEnterIncludeFile(Preprocessor &PP, FileEntryRef File, 546 bool isImport, bool ModulesEnabled, Module *M, 547 bool &IsFirstIncludeOfFile); 548 549 /// Return whether the specified file is a normal header, 550 /// a system header, or a C++ friendly system header. getFileDirFlavor(FileEntryRef File)551 SrcMgr::CharacteristicKind getFileDirFlavor(FileEntryRef File) { 552 if (const HeaderFileInfo *HFI = getExistingFileInfo(File)) 553 return (SrcMgr::CharacteristicKind)HFI->DirInfo; 554 return (SrcMgr::CharacteristicKind)HeaderFileInfo().DirInfo; 555 } 556 557 /// Mark the specified file as a "once only" file due to 558 /// \#pragma once. MarkFileIncludeOnce(FileEntryRef File)559 void MarkFileIncludeOnce(FileEntryRef File) { 560 getFileInfo(File).isPragmaOnce = true; 561 } 562 563 /// Mark the specified file as a system header, e.g. due to 564 /// \#pragma GCC system_header. MarkFileSystemHeader(FileEntryRef File)565 void MarkFileSystemHeader(FileEntryRef File) { 566 getFileInfo(File).DirInfo = SrcMgr::C_System; 567 } 568 569 /// Mark the specified file as part of a module. 570 void MarkFileModuleHeader(FileEntryRef FE, ModuleMap::ModuleHeaderRole Role, 571 bool isCompilingModuleHeader); 572 573 /// Mark the specified file as having a controlling macro. 574 /// 575 /// This is used by the multiple-include optimization to eliminate 576 /// no-op \#includes. SetFileControllingMacro(FileEntryRef File,const IdentifierInfo * ControllingMacro)577 void SetFileControllingMacro(FileEntryRef File, 578 const IdentifierInfo *ControllingMacro) { 579 getFileInfo(File).LazyControllingMacro = ControllingMacro; 580 } 581 582 /// Determine whether this file is intended to be safe from 583 /// multiple inclusions, e.g., it has \#pragma once or a controlling 584 /// macro. 585 /// 586 /// This routine does not consider the effect of \#import 587 bool isFileMultipleIncludeGuarded(FileEntryRef File) const; 588 589 /// Determine whether the given file is known to have ever been \#imported. hasFileBeenImported(FileEntryRef File)590 bool hasFileBeenImported(FileEntryRef File) const { 591 const HeaderFileInfo *FI = getExistingFileInfo(File); 592 return FI && FI->isImport; 593 } 594 595 /// Determine which HeaderSearchOptions::UserEntries have been successfully 596 /// used so far and mark their index with 'true' in the resulting bit vector. 597 /// Note: implicit module maps don't contribute to entry usage. 598 std::vector<bool> computeUserEntryUsage() const; 599 600 /// Collect which HeaderSearchOptions::VFSOverlayFiles have been meaningfully 601 /// used so far and mark their index with 'true' in the resulting bit vector. 602 /// 603 /// Note: this ignores VFSs that redirect non-affecting files such as unused 604 /// modulemaps. 605 std::vector<bool> collectVFSUsageAndClear() const; 606 607 /// This method returns a HeaderMap for the specified 608 /// FileEntry, uniquing them through the 'HeaderMaps' datastructure. 609 const HeaderMap *CreateHeaderMap(FileEntryRef FE); 610 611 /// Get filenames for all registered header maps. 612 void getHeaderMapFileNames(SmallVectorImpl<std::string> &Names) const; 613 614 /// Retrieve the name of the cached module file that should be used 615 /// to load the given module. 616 /// 617 /// \param Module The module whose module file name will be returned. 618 /// 619 /// \returns The name of the module file that corresponds to this module, 620 /// or an empty string if this module does not correspond to any module file. 621 std::string getCachedModuleFileName(Module *Module); 622 623 /// Retrieve the name of the prebuilt module file that should be used 624 /// to load a module with the given name. 625 /// 626 /// \param ModuleName The module whose module file name will be returned. 627 /// 628 /// \param FileMapOnly If true, then only look in the explicit module name 629 // to file name map and skip the directory search. 630 /// 631 /// \returns The name of the module file that corresponds to this module, 632 /// or an empty string if this module does not correspond to any module file. 633 std::string getPrebuiltModuleFileName(StringRef ModuleName, 634 bool FileMapOnly = false); 635 636 /// Retrieve the name of the prebuilt module file that should be used 637 /// to load the given module. 638 /// 639 /// \param Module The module whose module file name will be returned. 640 /// 641 /// \returns The name of the module file that corresponds to this module, 642 /// or an empty string if this module does not correspond to any module file. 643 std::string getPrebuiltImplicitModuleFileName(Module *Module); 644 645 /// Retrieve the name of the (to-be-)cached module file that should 646 /// be used to load a module with the given name. 647 /// 648 /// \param ModuleName The module whose module file name will be returned. 649 /// 650 /// \param ModuleMapPath A path that when combined with \c ModuleName 651 /// uniquely identifies this module. See Module::ModuleMap. 652 /// 653 /// \returns The name of the module file that corresponds to this module, 654 /// or an empty string if this module does not correspond to any module file. 655 std::string getCachedModuleFileName(StringRef ModuleName, 656 StringRef ModuleMapPath); 657 658 /// Lookup a module Search for a module with the given name. 659 /// 660 /// \param ModuleName The name of the module we're looking for. 661 /// 662 /// \param ImportLoc Location of the module include/import. 663 /// 664 /// \param AllowSearch Whether we are allowed to search in the various 665 /// search directories to produce a module definition. If not, this lookup 666 /// will only return an already-known module. 667 /// 668 /// \param AllowExtraModuleMapSearch Whether we allow to search modulemaps 669 /// in subdirectories. 670 /// 671 /// \returns The module with the given name. 672 Module *lookupModule(StringRef ModuleName, 673 SourceLocation ImportLoc = SourceLocation(), 674 bool AllowSearch = true, 675 bool AllowExtraModuleMapSearch = false); 676 677 /// Try to find a module map file in the given directory, returning 678 /// \c nullopt if none is found. 679 OptionalFileEntryRef lookupModuleMapFile(DirectoryEntryRef Dir, 680 bool IsFramework); 681 682 /// Determine whether there is a module map that may map the header 683 /// with the given file name to a (sub)module. 684 /// Always returns false if modules are disabled. 685 /// 686 /// \param Filename The name of the file. 687 /// 688 /// \param Root The "root" directory, at which we should stop looking for 689 /// module maps. 690 /// 691 /// \param IsSystem Whether the directories we're looking at are system 692 /// header directories. 693 bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root, 694 bool IsSystem); 695 696 /// Retrieve the module that corresponds to the given file, if any. 697 /// 698 /// \param File The header that we wish to map to a module. 699 /// \param AllowTextual Whether we want to find textual headers too. 700 ModuleMap::KnownHeader findModuleForHeader(FileEntryRef File, 701 bool AllowTextual = false, 702 bool AllowExcluded = false) const; 703 704 /// Retrieve all the modules corresponding to the given file. 705 /// 706 /// \ref findModuleForHeader should typically be used instead of this. 707 ArrayRef<ModuleMap::KnownHeader> 708 findAllModulesForHeader(FileEntryRef File) const; 709 710 /// Like \ref findAllModulesForHeader, but do not attempt to infer module 711 /// ownership from umbrella headers if we've not already done so. 712 ArrayRef<ModuleMap::KnownHeader> 713 findResolvedModulesForHeader(FileEntryRef File) const; 714 715 /// Read the contents of the given module map file. 716 /// 717 /// \param File The module map file. 718 /// \param IsSystem Whether this file is in a system header directory. 719 /// \param ID If the module map file is already mapped (perhaps as part of 720 /// processing a preprocessed module), the ID of the file. 721 /// \param Offset [inout] An offset within ID to start parsing. On exit, 722 /// filled by the end of the parsed contents (either EOF or the 723 /// location of an end-of-module-map pragma). 724 /// \param OriginalModuleMapFile The original path to the module map file, 725 /// used to resolve paths within the module (this is required when 726 /// building the module from preprocessed source). 727 /// \returns true if an error occurred, false otherwise. 728 bool loadModuleMapFile(FileEntryRef File, bool IsSystem, FileID ID = FileID(), 729 unsigned *Offset = nullptr, 730 StringRef OriginalModuleMapFile = StringRef()); 731 732 /// Collect the set of all known, top-level modules. 733 /// 734 /// \param Modules Will be filled with the set of known, top-level modules. 735 void collectAllModules(SmallVectorImpl<Module *> &Modules); 736 737 /// Load all known, top-level system modules. 738 void loadTopLevelSystemModules(); 739 740 private: 741 /// Lookup a module with the given module name and search-name. 742 /// 743 /// \param ModuleName The name of the module we're looking for. 744 /// 745 /// \param SearchName The "search-name" to derive filesystem paths from 746 /// when looking for the module map; this is usually equal to ModuleName, 747 /// but for compatibility with some buggy frameworks, additional attempts 748 /// may be made to find the module under a related-but-different search-name. 749 /// 750 /// \param ImportLoc Location of the module include/import. 751 /// 752 /// \param AllowExtraModuleMapSearch Whether we allow to search modulemaps 753 /// in subdirectories. 754 /// 755 /// \returns The module named ModuleName. 756 Module *lookupModule(StringRef ModuleName, StringRef SearchName, 757 SourceLocation ImportLoc, 758 bool AllowExtraModuleMapSearch = false); 759 760 /// Retrieve the name of the (to-be-)cached module file that should 761 /// be used to load a module with the given name. 762 /// 763 /// \param ModuleName The module whose module file name will be returned. 764 /// 765 /// \param ModuleMapPath A path that when combined with \c ModuleName 766 /// uniquely identifies this module. See Module::ModuleMap. 767 /// 768 /// \param CachePath A path to the module cache. 769 /// 770 /// \returns The name of the module file that corresponds to this module, 771 /// or an empty string if this module does not correspond to any module file. 772 std::string getCachedModuleFileNameImpl(StringRef ModuleName, 773 StringRef ModuleMapPath, 774 StringRef CachePath); 775 776 /// Retrieve a module with the given name, which may be part of the 777 /// given framework. 778 /// 779 /// \param Name The name of the module to retrieve. 780 /// 781 /// \param Dir The framework directory (e.g., ModuleName.framework). 782 /// 783 /// \param IsSystem Whether the framework directory is part of the system 784 /// frameworks. 785 /// 786 /// \returns The module, if found; otherwise, null. 787 Module *loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir, 788 bool IsSystem); 789 790 /// Load all of the module maps within the immediate subdirectories 791 /// of the given search directory. 792 void loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir); 793 794 /// Find and suggest a usable module for the given file. 795 /// 796 /// \return \c true if the file can be used, \c false if we are not permitted to 797 /// find this file due to requirements from \p RequestingModule. 798 bool findUsableModuleForHeader(FileEntryRef File, const DirectoryEntry *Root, 799 Module *RequestingModule, 800 ModuleMap::KnownHeader *SuggestedModule, 801 bool IsSystemHeaderDir); 802 803 /// Find and suggest a usable module for the given file, which is part of 804 /// the specified framework. 805 /// 806 /// \return \c true if the file can be used, \c false if we are not permitted to 807 /// find this file due to requirements from \p RequestingModule. 808 bool findUsableModuleForFrameworkHeader( 809 FileEntryRef File, StringRef FrameworkName, Module *RequestingModule, 810 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework); 811 812 /// Look up the file with the specified name and determine its owning 813 /// module. 814 OptionalFileEntryRef 815 getFileAndSuggestModule(StringRef FileName, SourceLocation IncludeLoc, 816 const DirectoryEntry *Dir, bool IsSystemHeaderDir, 817 Module *RequestingModule, 818 ModuleMap::KnownHeader *SuggestedModule, 819 bool OpenFile = true, bool CacheFailures = true); 820 821 /// Cache the result of a successful lookup at the given include location 822 /// using the search path at \c HitIt. 823 void cacheLookupSuccess(LookupFileCacheInfo &CacheLookup, 824 ConstSearchDirIterator HitIt, 825 SourceLocation IncludeLoc); 826 827 /// Note that a lookup at the given include location was successful using the 828 /// search path at index `HitIdx`. 829 void noteLookupUsage(unsigned HitIdx, SourceLocation IncludeLoc); 830 831 public: 832 /// Retrieve the module map. getModuleMap()833 ModuleMap &getModuleMap() { return ModMap; } 834 835 /// Retrieve the module map. getModuleMap()836 const ModuleMap &getModuleMap() const { return ModMap; } 837 header_file_size()838 unsigned header_file_size() const { return FileInfo.size(); } 839 840 /// Return the HeaderFileInfo structure for the specified FileEntry, in 841 /// preparation for updating it in some way. 842 HeaderFileInfo &getFileInfo(FileEntryRef FE); 843 844 /// Return the HeaderFileInfo structure for the specified FileEntry, if it has 845 /// ever been filled in (either locally or externally). 846 const HeaderFileInfo *getExistingFileInfo(FileEntryRef FE) const; 847 848 /// Return the headerFileInfo structure for the specified FileEntry, if it has 849 /// ever been filled in locally. 850 const HeaderFileInfo *getExistingLocalFileInfo(FileEntryRef FE) const; 851 search_dir_begin()852 SearchDirIterator search_dir_begin() { return {*this, 0}; } search_dir_end()853 SearchDirIterator search_dir_end() { return {*this, SearchDirs.size()}; } search_dir_range()854 SearchDirRange search_dir_range() { 855 return {search_dir_begin(), search_dir_end()}; 856 } 857 search_dir_begin()858 ConstSearchDirIterator search_dir_begin() const { return quoted_dir_begin(); } search_dir_nth(size_t n)859 ConstSearchDirIterator search_dir_nth(size_t n) const { 860 assert(n < SearchDirs.size()); 861 return {*this, n}; 862 } search_dir_end()863 ConstSearchDirIterator search_dir_end() const { return system_dir_end(); } search_dir_range()864 ConstSearchDirRange search_dir_range() const { 865 return {search_dir_begin(), search_dir_end()}; 866 } 867 search_dir_size()868 unsigned search_dir_size() const { return SearchDirs.size(); } 869 quoted_dir_begin()870 ConstSearchDirIterator quoted_dir_begin() const { return {*this, 0}; } quoted_dir_end()871 ConstSearchDirIterator quoted_dir_end() const { return angled_dir_begin(); } 872 angled_dir_begin()873 ConstSearchDirIterator angled_dir_begin() const { 874 return {*this, AngledDirIdx}; 875 } angled_dir_end()876 ConstSearchDirIterator angled_dir_end() const { return system_dir_begin(); } 877 system_dir_begin()878 ConstSearchDirIterator system_dir_begin() const { 879 return {*this, SystemDirIdx}; 880 } system_dir_end()881 ConstSearchDirIterator system_dir_end() const { 882 return {*this, SearchDirs.size()}; 883 } 884 885 /// Get the index of the given search directory. 886 unsigned searchDirIdx(const DirectoryLookup &DL) const; 887 888 /// Retrieve a uniqued framework name. 889 StringRef getUniqueFrameworkName(StringRef Framework); 890 891 /// Retrieve the include name for the header. 892 /// 893 /// \param File The entry for a given header. 894 /// \returns The name of how the file was included when the header's location 895 /// was resolved. 896 StringRef getIncludeNameForHeader(const FileEntry *File) const; 897 898 /// Suggest a path by which the specified file could be found, for use in 899 /// diagnostics to suggest a #include. Returned path will only contain forward 900 /// slashes as separators. MainFile is the absolute path of the file that we 901 /// are generating the diagnostics for. It will try to shorten the path using 902 /// MainFile location, if none of the include search directories were prefix 903 /// of File. 904 /// 905 /// \param IsAngled If non-null, filled in to indicate whether the suggested 906 /// path should be referenced as <Header.h> instead of "Header.h". 907 std::string suggestPathToFileForDiagnostics(FileEntryRef File, 908 llvm::StringRef MainFile, 909 bool *IsAngled = nullptr) const; 910 911 /// Suggest a path by which the specified file could be found, for use in 912 /// diagnostics to suggest a #include. Returned path will only contain forward 913 /// slashes as separators. MainFile is the absolute path of the file that we 914 /// are generating the diagnostics for. It will try to shorten the path using 915 /// MainFile location, if none of the include search directories were prefix 916 /// of File. 917 /// 918 /// \param WorkingDir If non-empty, this will be prepended to search directory 919 /// paths that are relative. 920 std::string suggestPathToFileForDiagnostics(llvm::StringRef File, 921 llvm::StringRef WorkingDir, 922 llvm::StringRef MainFile, 923 bool *IsAngled = nullptr) const; 924 925 void PrintStats(); 926 927 size_t getTotalMemory() const; 928 929 private: 930 /// Describes what happened when we tried to load a module map file. 931 enum LoadModuleMapResult { 932 /// The module map file had already been loaded. 933 LMM_AlreadyLoaded, 934 935 /// The module map file was loaded by this invocation. 936 LMM_NewlyLoaded, 937 938 /// There is was directory with the given name. 939 LMM_NoDirectory, 940 941 /// There was either no module map file or the module map file was 942 /// invalid. 943 LMM_InvalidModuleMap 944 }; 945 946 LoadModuleMapResult loadModuleMapFileImpl(FileEntryRef File, bool IsSystem, 947 DirectoryEntryRef Dir, 948 FileID ID = FileID(), 949 unsigned *Offset = nullptr); 950 951 /// Try to load the module map file in the given directory. 952 /// 953 /// \param DirName The name of the directory where we will look for a module 954 /// map file. 955 /// \param IsSystem Whether this is a system header directory. 956 /// \param IsFramework Whether this is a framework directory. 957 /// 958 /// \returns The result of attempting to load the module map file from the 959 /// named directory. 960 LoadModuleMapResult loadModuleMapFile(StringRef DirName, bool IsSystem, 961 bool IsFramework); 962 963 /// Try to load the module map file in the given directory. 964 /// 965 /// \param Dir The directory where we will look for a module map file. 966 /// \param IsSystem Whether this is a system header directory. 967 /// \param IsFramework Whether this is a framework directory. 968 /// 969 /// \returns The result of attempting to load the module map file from the 970 /// named directory. 971 LoadModuleMapResult loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem, 972 bool IsFramework); 973 }; 974 975 /// Apply the header search options to get given HeaderSearch object. 976 void ApplyHeaderSearchOptions(HeaderSearch &HS, 977 const HeaderSearchOptions &HSOpts, 978 const LangOptions &Lang, 979 const llvm::Triple &triple); 980 981 } // namespace clang 982 983 #endif // LLVM_CLANG_LEX_HEADERSEARCH_H 984