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