clang 17.0.0git
ModuleMap.h
Go to the documentation of this file.
1//===- ModuleMap.h - Describe the layout of modules -------------*- 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 ModuleMap interface, which describes the layout of a
10// module as it relates to headers.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LEX_MODULEMAP_H
15#define LLVM_CLANG_LEX_MODULEMAP_H
16
19#include "clang/Basic/Module.h"
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/PointerIntPair.h"
24#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringMap.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/ADT/StringSet.h"
29#include "llvm/ADT/TinyPtrVector.h"
30#include "llvm/ADT/Twine.h"
31#include <ctime>
32#include <memory>
33#include <optional>
34#include <string>
35#include <utility>
36
37namespace clang {
38
39class DiagnosticsEngine;
40class DirectoryEntry;
41class FileEntry;
42class FileManager;
43class HeaderSearch;
44class SourceManager;
45
46/// A mechanism to observe the actions of the module map parser as it
47/// reads module map files.
49 virtual void anchor();
50
51public:
52 virtual ~ModuleMapCallbacks() = default;
53
54 /// Called when a module map file has been read.
55 ///
56 /// \param FileStart A SourceLocation referring to the start of the file's
57 /// contents.
58 /// \param File The file itself.
59 /// \param IsSystem Whether this is a module map from a system include path.
60 virtual void moduleMapFileRead(SourceLocation FileStart,
61 const FileEntry &File, bool IsSystem) {}
62
63 /// Called when a header is added during module map parsing.
64 ///
65 /// \param Filename The header file itself.
66 virtual void moduleMapAddHeader(StringRef Filename) {}
67
68 /// Called when an umbrella header is added during module map parsing.
69 ///
70 /// \param FileMgr FileManager instance
71 /// \param Header The umbrella header to collect.
73 const FileEntry *Header) {}
74};
75
76class ModuleMap {
77 SourceManager &SourceMgr;
78 DiagnosticsEngine &Diags;
79 const LangOptions &LangOpts;
80 const TargetInfo *Target;
81 HeaderSearch &HeaderInfo;
82
84
85 /// The directory used for Clang-supplied, builtin include headers,
86 /// such as "stdint.h".
87 const DirectoryEntry *BuiltinIncludeDir = nullptr;
88
89 /// Language options used to parse the module map itself.
90 ///
91 /// These are always simple C language options.
92 LangOptions MMapLangOpts;
93
94 /// The module that the main source file is associated with (the module
95 /// named LangOpts::CurrentModule, if we've loaded it).
96 Module *SourceModule = nullptr;
97
98 /// Submodules of the current module that have not yet been attached to it.
99 /// (Ownership is transferred if/when we create an enclosing module.)
100 llvm::SmallVector<std::unique_ptr<Module>, 8> PendingSubmodules;
101
102 /// The top-level modules that are known.
103 llvm::StringMap<Module *> Modules;
104
105 /// Module loading cache that includes submodules, indexed by IdentifierInfo.
106 /// nullptr is stored for modules that are known to fail to load.
107 llvm::DenseMap<const IdentifierInfo *, Module *> CachedModuleLoads;
108
109 /// Shadow modules created while building this module map.
110 llvm::SmallVector<Module*, 2> ShadowModules;
111
112 /// The number of modules we have created in total.
113 unsigned NumCreatedModules = 0;
114
115 /// In case a module has a export_as entry, it might have a pending link
116 /// name to be determined if that module is imported.
117 llvm::StringMap<llvm::StringSet<>> PendingLinkAsModule;
118
119public:
120 /// Use PendingLinkAsModule information to mark top level link names that
121 /// are going to be replaced by export_as aliases.
123
124 /// Make module to use export_as as the link dependency name if enough
125 /// information is available or add it to a pending list otherwise.
126 void addLinkAsDependency(Module *Mod);
127
128 /// Flags describing the role of a module header.
130 /// This header is normally included in the module.
132
133 /// This header is included but private.
135
136 /// This header is part of the module (for layering purposes) but
137 /// should be textually included.
139
140 /// This header is explicitly excluded from the module.
142
143 // Caution: Adding an enumerator needs other changes.
144 // Adjust the number of bits for KnownHeader::Storage.
145 // Adjust the HeaderFileInfoTrait::ReadData streaming.
146 // Adjust the HeaderFileInfoTrait::EmitData streaming.
147 // Adjust ModuleMap::addHeader.
148 };
149
150 /// Convert a header kind to a role. Requires Kind to not be HK_Excluded.
152
153 /// Convert a header role to a kind.
155
156 /// Check if the header with the given role is a modular one.
157 static bool isModular(ModuleHeaderRole Role);
158
159 /// A header that is known to reside within a given module,
160 /// whether it was included or excluded.
162 llvm::PointerIntPair<Module *, 3, ModuleHeaderRole> Storage;
163
164 public:
165 KnownHeader() : Storage(nullptr, NormalHeader) {}
166 KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) {}
167
168 friend bool operator==(const KnownHeader &A, const KnownHeader &B) {
169 return A.Storage == B.Storage;
170 }
171 friend bool operator!=(const KnownHeader &A, const KnownHeader &B) {
172 return A.Storage != B.Storage;
173 }
174
175 /// Retrieve the module the header is stored in.
176 Module *getModule() const { return Storage.getPointer(); }
177
178 /// The role of this header within the module.
179 ModuleHeaderRole getRole() const { return Storage.getInt(); }
180
181 /// Whether this header is available in the module.
182 bool isAvailable() const {
183 return getRole() != ExcludedHeader && getModule()->isAvailable();
184 }
185
186 /// Whether this header is accessible from the specified module.
187 bool isAccessibleFrom(Module *M) const {
188 return !(getRole() & PrivateHeader) ||
190 }
191
192 // Whether this known header is valid (i.e., it has an
193 // associated module).
194 explicit operator bool() const {
195 return Storage.getPointer() != nullptr;
196 }
197 };
198
200
201private:
202 friend class ModuleMapParser;
203
204 using HeadersMap =
205 llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1>>;
206
207 /// Mapping from each header to the module that owns the contents of
208 /// that header.
209 HeadersMap Headers;
210
211 /// Map from file sizes to modules with lazy header directives of that size.
212 mutable llvm::DenseMap<off_t, llvm::TinyPtrVector<Module*>> LazyHeadersBySize;
213
214 /// Map from mtimes to modules with lazy header directives with those mtimes.
215 mutable llvm::DenseMap<time_t, llvm::TinyPtrVector<Module*>>
216 LazyHeadersByModTime;
217
218 /// Mapping from directories with umbrella headers to the module
219 /// that is generated from the umbrella header.
220 ///
221 /// This mapping is used to map headers that haven't explicitly been named
222 /// in the module map over to the module that includes them via its umbrella
223 /// header.
224 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
225
226 /// A generation counter that is used to test whether modules of the
227 /// same name may shadow or are illegal redefinitions.
228 ///
229 /// Modules from earlier scopes may shadow modules from later ones.
230 /// Modules from the same scope may not have the same name.
231 unsigned CurrentModuleScopeID = 0;
232
233 llvm::DenseMap<Module *, unsigned> ModuleScopeIDs;
234
235 /// The set of attributes that can be attached to a module.
236 struct Attributes {
237 /// Whether this is a system module.
238 unsigned IsSystem : 1;
239
240 /// Whether this is an extern "C" module.
241 unsigned IsExternC : 1;
242
243 /// Whether this is an exhaustive set of configuration macros.
244 unsigned IsExhaustive : 1;
245
246 /// Whether files in this module can only include non-modular headers
247 /// and headers from used modules.
248 unsigned NoUndeclaredIncludes : 1;
249
250 Attributes()
251 : IsSystem(false), IsExternC(false), IsExhaustive(false),
252 NoUndeclaredIncludes(false) {}
253 };
254
255 /// A directory for which framework modules can be inferred.
256 struct InferredDirectory {
257 /// Whether to infer modules from this directory.
258 unsigned InferModules : 1;
259
260 /// The attributes to use for inferred modules.
261 Attributes Attrs;
262
263 /// If \c InferModules is non-zero, the module map file that allowed
264 /// inferred modules. Otherwise, nullptr.
265 const FileEntry *ModuleMapFile;
266
267 /// The names of modules that cannot be inferred within this
268 /// directory.
269 SmallVector<std::string, 2> ExcludedModules;
270
271 InferredDirectory() : InferModules(false) {}
272 };
273
274 /// A mapping from directories to information about inferring
275 /// framework modules from within those directories.
276 llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
277
278 /// A mapping from an inferred module to the module map that allowed the
279 /// inference.
280 llvm::DenseMap<const Module *, const FileEntry *> InferredModuleAllowedBy;
281
282 llvm::DenseMap<const Module *, AdditionalModMapsSet> AdditionalModMaps;
283
284 /// Describes whether we haved parsed a particular file as a module
285 /// map.
286 llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap;
287
288 /// Resolve the given export declaration into an actual export
289 /// declaration.
290 ///
291 /// \param Mod The module in which we're resolving the export declaration.
292 ///
293 /// \param Unresolved The export declaration to resolve.
294 ///
295 /// \param Complain Whether this routine should complain about unresolvable
296 /// exports.
297 ///
298 /// \returns The resolved export declaration, which will have a NULL pointer
299 /// if the export could not be resolved.
301 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
302 bool Complain) const;
303
304 /// Resolve the given module id to an actual module.
305 ///
306 /// \param Id The module-id to resolve.
307 ///
308 /// \param Mod The module in which we're resolving the module-id.
309 ///
310 /// \param Complain Whether this routine should complain about unresolvable
311 /// module-ids.
312 ///
313 /// \returns The resolved module, or null if the module-id could not be
314 /// resolved.
315 Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const;
316
317 /// Add an unresolved header to a module.
318 ///
319 /// \param Mod The module in which we're adding the unresolved header
320 /// directive.
321 /// \param Header The unresolved header directive.
322 /// \param NeedsFramework If Mod is not a framework but a missing header would
323 /// be found in case Mod was, set it to true. False otherwise.
324 void addUnresolvedHeader(Module *Mod,
325 Module::UnresolvedHeaderDirective Header,
326 bool &NeedsFramework);
327
328 /// Look up the given header directive to find an actual header file.
329 ///
330 /// \param M The module in which we're resolving the header directive.
331 /// \param Header The header directive to resolve.
332 /// \param RelativePathName Filled in with the relative path name from the
333 /// module to the resolved header.
334 /// \param NeedsFramework If M is not a framework but a missing header would
335 /// be found in case M was, set it to true. False otherwise.
336 /// \return The resolved file, if any.
338 findHeader(Module *M, const Module::UnresolvedHeaderDirective &Header,
339 SmallVectorImpl<char> &RelativePathName, bool &NeedsFramework);
340
341 /// Resolve the given header directive.
342 ///
343 /// \param M The module in which we're resolving the header directive.
344 /// \param Header The header directive to resolve.
345 /// \param NeedsFramework If M is not a framework but a missing header would
346 /// be found in case M was, set it to true. False otherwise.
347 void resolveHeader(Module *M, const Module::UnresolvedHeaderDirective &Header,
348 bool &NeedsFramework);
349
350 /// Attempt to resolve the specified header directive as naming a builtin
351 /// header.
352 /// \return \c true if a corresponding builtin header was found.
353 bool resolveAsBuiltinHeader(Module *M,
354 const Module::UnresolvedHeaderDirective &Header);
355
356 /// Looks up the modules that \p File corresponds to.
357 ///
358 /// If \p File represents a builtin header within Clang's builtin include
359 /// directory, this also loads all of the module maps to see if it will get
360 /// associated with a specific module (e.g. in /usr/include).
361 HeadersMap::iterator findKnownHeader(const FileEntry *File);
362
363 /// Searches for a module whose umbrella directory contains \p File.
364 ///
365 /// \param File The header to search for.
366 ///
367 /// \param IntermediateDirs On success, contains the set of directories
368 /// searched before finding \p File.
369 KnownHeader findHeaderInUmbrellaDirs(const FileEntry *File,
370 SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs);
371
372 /// Given that \p File is not in the Headers map, look it up within
373 /// umbrella directories and find or create a module for it.
374 KnownHeader findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File);
375
376 /// A convenience method to determine if \p File is (possibly nested)
377 /// in an umbrella directory.
378 bool isHeaderInUmbrellaDirs(const FileEntry *File) {
379 SmallVector<const DirectoryEntry *, 2> IntermediateDirs;
380 return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs));
381 }
382
383 Module *inferFrameworkModule(const DirectoryEntry *FrameworkDir,
384 Attributes Attrs, Module *Parent);
385
386public:
387 /// Construct a new module map.
388 ///
389 /// \param SourceMgr The source manager used to find module files and headers.
390 /// This source manager should be shared with the header-search mechanism,
391 /// since they will refer to the same headers.
392 ///
393 /// \param Diags A diagnostic engine used for diagnostics.
394 ///
395 /// \param LangOpts Language options for this translation unit.
396 ///
397 /// \param Target The target for this translation unit.
398 ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
399 const LangOptions &LangOpts, const TargetInfo *Target,
400 HeaderSearch &HeaderInfo);
401
402 /// Destroy the module map.
403 ~ModuleMap();
404
405 /// Set the target information.
406 void setTarget(const TargetInfo &Target);
407
408 /// Set the directory that contains Clang-supplied include
409 /// files, such as our stdarg.h or tgmath.h.
411 BuiltinIncludeDir = Dir;
412 }
413
414 /// Get the directory that contains Clang-supplied include files.
416 return BuiltinIncludeDir;
417 }
418
419 /// Is this a compiler builtin header?
420 static bool isBuiltinHeader(StringRef FileName);
421 bool isBuiltinHeader(const FileEntry *File);
422
423 /// Add a module map callback.
424 void addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback) {
425 Callbacks.push_back(std::move(Callback));
426 }
427
428 /// Retrieve the module that owns the given header file, if any. Note that
429 /// this does not implicitly load module maps, except for builtin headers,
430 /// and does not consult the external source. (Those checks are the
431 /// responsibility of \ref HeaderSearch.)
432 ///
433 /// \param File The header file that is likely to be included.
434 ///
435 /// \param AllowTextual If \c true and \p File is a textual header, return
436 /// its owning module. Otherwise, no KnownHeader will be returned if the
437 /// file is only known as a textual header.
438 ///
439 /// \returns The module KnownHeader, which provides the module that owns the
440 /// given header file. The KnownHeader is default constructed to indicate
441 /// that no module owns this header file.
442 KnownHeader findModuleForHeader(const FileEntry *File,
443 bool AllowTextual = false,
444 bool AllowExcluded = false);
445
446 /// Retrieve all the modules that contain the given header file. Note that
447 /// this does not implicitly load module maps, except for builtin headers,
448 /// and does not consult the external source. (Those checks are the
449 /// responsibility of \ref HeaderSearch.)
450 ///
451 /// Typically, \ref findModuleForHeader should be used instead, as it picks
452 /// the preferred module for the header.
454
455 /// Like \ref findAllModulesForHeader, but do not attempt to infer module
456 /// ownership from umbrella headers if we've not already done so.
459
460 /// Resolve all lazy header directives for the specified file.
461 ///
462 /// This ensures that the HeaderFileInfo on HeaderSearch is up to date. This
463 /// is effectively internal, but is exposed so HeaderSearch can call it.
464 void resolveHeaderDirectives(const FileEntry *File) const;
465
466 /// Resolve lazy header directives for the specified module. If File is
467 /// provided, only headers with same size and modtime are resolved. If File
468 /// is not set, all headers are resolved.
470 std::optional<const FileEntry *> File) const;
471
472 /// Reports errors if a module must not include a specific file.
473 ///
474 /// \param RequestingModule The module including a file.
475 ///
476 /// \param RequestingModuleIsModuleInterface \c true if the inclusion is in
477 /// the interface of RequestingModule, \c false if it's in the
478 /// implementation of RequestingModule. Value is ignored and
479 /// meaningless if RequestingModule is nullptr.
480 ///
481 /// \param FilenameLoc The location of the inclusion's filename.
482 ///
483 /// \param Filename The included filename as written.
484 ///
485 /// \param File The included file.
486 void diagnoseHeaderInclusion(Module *RequestingModule,
487 bool RequestingModuleIsModuleInterface,
488 SourceLocation FilenameLoc, StringRef Filename,
490
491 /// Determine whether the given header is part of a module
492 /// marked 'unavailable'.
493 bool isHeaderInUnavailableModule(const FileEntry *Header) const;
494
495 /// Determine whether the given header is unavailable as part
496 /// of the specified module.
497 bool isHeaderUnavailableInModule(const FileEntry *Header,
498 const Module *RequestingModule) const;
499
500 /// Retrieve a module with the given name.
501 ///
502 /// \param Name The name of the module to look up.
503 ///
504 /// \returns The named module, if known; otherwise, returns null.
505 Module *findModule(StringRef Name) const;
506
507 /// Retrieve a module with the given name using lexical name lookup,
508 /// starting at the given context.
509 ///
510 /// \param Name The name of the module to look up.
511 ///
512 /// \param Context The module context, from which we will perform lexical
513 /// name lookup.
514 ///
515 /// \returns The named module, if known; otherwise, returns null.
516 Module *lookupModuleUnqualified(StringRef Name, Module *Context) const;
517
518 /// Retrieve a module with the given name within the given context,
519 /// using direct (qualified) name lookup.
520 ///
521 /// \param Name The name of the module to look up.
522 ///
523 /// \param Context The module for which we will look for a submodule. If
524 /// null, we will look for a top-level module.
525 ///
526 /// \returns The named submodule, if known; otherwose, returns null.
527 Module *lookupModuleQualified(StringRef Name, Module *Context) const;
528
529 /// Find a new module or submodule, or create it if it does not already
530 /// exist.
531 ///
532 /// \param Name The name of the module to find or create.
533 ///
534 /// \param Parent The module that will act as the parent of this submodule,
535 /// or nullptr to indicate that this is a top-level module.
536 ///
537 /// \param IsFramework Whether this is a framework module.
538 ///
539 /// \param IsExplicit Whether this is an explicit submodule.
540 ///
541 /// \returns The found or newly-created module, along with a boolean value
542 /// that will be true if the module is newly-created.
543 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
544 bool IsFramework,
545 bool IsExplicit);
546
547 /// Create a global module fragment for a C++ module unit.
548 ///
549 /// We model the global module fragment as a submodule of the module
550 /// interface unit. Unfortunately, we can't create the module interface
551 /// unit's Module until later, because we don't know what it will be called
552 /// usually. See C++20 [module.unit]/7.2 for the case we could know its
553 /// parent.
555 Module *Parent = nullptr);
557 SourceLocation Loc, bool IsExported, Module *Parent = nullptr);
558
559 /// Create a global module fragment for a C++ module interface unit.
561 SourceLocation Loc);
562
563 /// Create a new C++ module with the specified kind, and reparent any pending
564 /// global module fragment(s) to it.
566 Module::ModuleKind Kind);
567
568 /// Create a new module for a C++ module interface unit.
569 /// The module must not already exist, and will be configured for the current
570 /// compilation.
571 ///
572 /// Note that this also sets the current module to the newly-created module.
573 ///
574 /// \returns The newly-created module.
576
577 /// Create a new module for a C++ module implementation unit.
578 /// The interface module for this implementation (implicitly imported) must
579 /// exist and be loaded and present in the modules map.
580 ///
581 /// \returns The newly-created module.
583
584 /// Create a C++20 header unit.
585 Module *createHeaderUnit(SourceLocation Loc, StringRef Name,
587
588 /// Infer the contents of a framework module map from the given
589 /// framework directory.
590 Module *inferFrameworkModule(const DirectoryEntry *FrameworkDir,
591 bool IsSystem, Module *Parent);
592
593 /// Create a new top-level module that is shadowed by
594 /// \p ShadowingModule.
595 Module *createShadowedModule(StringRef Name, bool IsFramework,
596 Module *ShadowingModule);
597
598 /// Creates a new declaration scope for module names, allowing
599 /// previously defined modules to shadow definitions from the new scope.
600 ///
601 /// \note Module names from earlier scopes will shadow names from the new
602 /// scope, which is the opposite of how shadowing works for variables.
603 void finishModuleDeclarationScope() { CurrentModuleScopeID += 1; }
604
605 bool mayShadowNewModule(Module *ExistingModule) {
606 assert(!ExistingModule->Parent && "expected top-level module");
607 assert(ModuleScopeIDs.count(ExistingModule) && "unknown module");
608 return ModuleScopeIDs[ExistingModule] < CurrentModuleScopeID;
609 }
610
611 /// Check whether a framework module can be inferred in the given directory.
613 auto It = InferredDirectories.find(Dir);
614 return It != InferredDirectories.end() && It->getSecond().InferModules;
615 }
616
617 /// Retrieve the module map file containing the definition of the given
618 /// module.
619 ///
620 /// \param Module The module whose module map file will be returned, if known.
621 ///
622 /// \returns The file entry for the module map file containing the given
623 /// module, or nullptr if the module definition was inferred.
625
626 /// Get the module map file that (along with the module name) uniquely
627 /// identifies this module.
628 ///
629 /// The particular module that \c Name refers to may depend on how the module
630 /// was found in header search. However, the combination of \c Name and
631 /// this module map will be globally unique for top-level modules. In the case
632 /// of inferred modules, returns the module map that allowed the inference
633 /// (e.g. contained 'module *'). Otherwise, returns
634 /// getContainingModuleMapFile().
636
637 void setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap);
638
639 /// Canonicalize \p Path in a manner suitable for a module map file. In
640 /// particular, this canonicalizes the parent directory separately from the
641 /// filename so that it does not affect header resolution relative to the
642 /// modulemap.
643 ///
644 /// \returns an error code if any filesystem operations failed. In this case
645 /// \p Path is not modified.
647
648 /// Get any module map files other than getModuleMapFileForUniquing(M)
649 /// that define submodules of a top-level module \p M. This is cheaper than
650 /// getting the module map file for each submodule individually, since the
651 /// expected number of results is very small.
653 auto I = AdditionalModMaps.find(M);
654 if (I == AdditionalModMaps.end())
655 return nullptr;
656 return &I->second;
657 }
658
660
661 /// Resolve all of the unresolved exports in the given module.
662 ///
663 /// \param Mod The module whose exports should be resolved.
664 ///
665 /// \param Complain Whether to emit diagnostics for failures.
666 ///
667 /// \returns true if any errors were encountered while resolving exports,
668 /// false otherwise.
669 bool resolveExports(Module *Mod, bool Complain);
670
671 /// Resolve all of the unresolved uses in the given module.
672 ///
673 /// \param Mod The module whose uses should be resolved.
674 ///
675 /// \param Complain Whether to emit diagnostics for failures.
676 ///
677 /// \returns true if any errors were encountered while resolving uses,
678 /// false otherwise.
679 bool resolveUses(Module *Mod, bool Complain);
680
681 /// Resolve all of the unresolved conflicts in the given module.
682 ///
683 /// \param Mod The module whose conflicts should be resolved.
684 ///
685 /// \param Complain Whether to emit diagnostics for failures.
686 ///
687 /// \returns true if any errors were encountered while resolving conflicts,
688 /// false otherwise.
689 bool resolveConflicts(Module *Mod, bool Complain);
690
691 /// Sets the umbrella header of the given module to the given
692 /// header.
693 void setUmbrellaHeader(Module *Mod, FileEntryRef UmbrellaHeader,
694 const Twine &NameAsWritten,
695 const Twine &PathRelativeToRootModuleDirectory);
696
697 /// Sets the umbrella directory of the given module to the given
698 /// directory.
699 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
700 const Twine &NameAsWritten,
701 const Twine &PathRelativeToRootModuleDirectory);
702
703 /// Adds this header to the given module.
704 /// \param Role The role of the header wrt the module.
705 void addHeader(Module *Mod, Module::Header Header,
706 ModuleHeaderRole Role, bool Imported = false);
707
708 /// Parse the given module map file, and record any modules we
709 /// encounter.
710 ///
711 /// \param File The file to be parsed.
712 ///
713 /// \param IsSystem Whether this module map file is in a system header
714 /// directory, and therefore should be considered a system module.
715 ///
716 /// \param HomeDir The directory in which relative paths within this module
717 /// map file will be resolved.
718 ///
719 /// \param ID The FileID of the file to process, if we've already entered it.
720 ///
721 /// \param Offset [inout] On input the offset at which to start parsing. On
722 /// output, the offset at which the module map terminated.
723 ///
724 /// \param ExternModuleLoc The location of the "extern module" declaration
725 /// that caused us to load this module map file, if any.
726 ///
727 /// \returns true if an error occurred, false otherwise.
728 bool parseModuleMapFile(const FileEntry *File, bool IsSystem,
729 const DirectoryEntry *HomeDir,
730 FileID ID = FileID(), unsigned *Offset = nullptr,
731 SourceLocation ExternModuleLoc = SourceLocation());
732
733 /// Dump the contents of the module map, for debugging purposes.
734 void dump();
735
736 using module_iterator = llvm::StringMap<Module *>::const_iterator;
737
738 module_iterator module_begin() const { return Modules.begin(); }
739 module_iterator module_end() const { return Modules.end(); }
740 llvm::iterator_range<module_iterator> modules() const {
741 return {module_begin(), module_end()};
742 }
743
744 /// Cache a module load. M might be nullptr.
746 CachedModuleLoads[&II] = M;
747 }
748
749 /// Return a cached module load.
750 std::optional<Module *> getCachedModuleLoad(const IdentifierInfo &II) {
751 auto I = CachedModuleLoads.find(&II);
752 if (I == CachedModuleLoads.end())
753 return std::nullopt;
754 return I->second;
755 }
756};
757
758} // namespace clang
759
760#endif // LLVM_CLANG_LEX_MODULEMAP_H
NodeId Parent
Definition: ASTDiff.cpp:191
int Id
Definition: ASTDiff.cpp:190
unsigned Offset
Definition: Format.cpp:2778
StringRef Filename
Definition: Format.cpp:2776
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Defines the clang::LangOptions interface.
Defines the clang::Module class, which describes a module in the source code.
Defines the clang::SourceLocation class and associated facilities.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
Cached information about one directory (either on disk or in the virtual file system).
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:353
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
Definition: HeaderSearch.h:223
One of these records is kept for each identifier that is lexed.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:82
A mechanism to observe the actions of the module map parser as it reads module map files.
Definition: ModuleMap.h:48
virtual void moduleMapFileRead(SourceLocation FileStart, const FileEntry &File, bool IsSystem)
Called when a module map file has been read.
Definition: ModuleMap.h:60
virtual ~ModuleMapCallbacks()=default
virtual void moduleMapAddUmbrellaHeader(FileManager *FileMgr, const FileEntry *Header)
Called when an umbrella header is added during module map parsing.
Definition: ModuleMap.h:72
virtual void moduleMapAddHeader(StringRef Filename)
Called when a header is added during module map parsing.
Definition: ModuleMap.h:66
A header that is known to reside within a given module, whether it was included or excluded.
Definition: ModuleMap.h:161
KnownHeader(Module *M, ModuleHeaderRole Role)
Definition: ModuleMap.h:166
bool isAccessibleFrom(Module *M) const
Whether this header is accessible from the specified module.
Definition: ModuleMap.h:187
ModuleHeaderRole getRole() const
The role of this header within the module.
Definition: ModuleMap.h:179
friend bool operator!=(const KnownHeader &A, const KnownHeader &B)
Definition: ModuleMap.h:171
friend bool operator==(const KnownHeader &A, const KnownHeader &B)
Definition: ModuleMap.h:168
Module * getModule() const
Retrieve the module the header is stored in.
Definition: ModuleMap.h:176
bool isAvailable() const
Whether this header is available in the module.
Definition: ModuleMap.h:182
Module * createShadowedModule(StringRef Name, bool IsFramework, Module *ShadowingModule)
Create a new top-level module that is shadowed by ShadowingModule.
Definition: ModuleMap.cpp:1155
bool resolveExports(Module *Mod, bool Complain)
Resolve all of the unresolved exports in the given module.
Definition: ModuleMap.cpp:1388
void setUmbrellaHeader(Module *Mod, FileEntryRef UmbrellaHeader, const Twine &NameAsWritten, const Twine &PathRelativeToRootModuleDirectory)
Sets the umbrella header of the given module to the given header.
Definition: ModuleMap.cpp:1170
void addLinkAsDependency(Module *Mod)
Make module to use export_as as the link dependency name if enough information is available or add it...
Definition: ModuleMap.cpp:69
void dump()
Dump the contents of the module map, for debugging purposes.
Definition: ModuleMap.cpp:1366
std::pair< Module *, bool > findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, bool IsExplicit)
Find a new module or submodule, or create it if it does not already exist.
Definition: ModuleMap.cpp:834
llvm::StringMap< Module * >::const_iterator module_iterator
Definition: ModuleMap.h:736
void diagnoseHeaderInclusion(Module *RequestingModule, bool RequestingModuleIsModuleInterface, SourceLocation FilenameLoc, StringRef Filename, FileEntryRef File)
Reports errors if a module must not include a specific file.
Definition: ModuleMap.cpp:474
OptionalFileEntryRef getContainingModuleMapFile(const Module *Module) const
Retrieve the module map file containing the definition of the given module.
Definition: ModuleMap.cpp:1295
static Module::HeaderKind headerRoleToKind(ModuleHeaderRole Role)
Convert a header role to a kind.
Definition: ModuleMap.cpp:76
Module * findModule(StringRef Name) const
Retrieve a module with the given name.
Definition: ModuleMap.cpp:809
void finishModuleDeclarationScope()
Creates a new declaration scope for module names, allowing previously defined modules to shadow defin...
Definition: ModuleMap.h:603
bool canInferFrameworkModule(const DirectoryEntry *Dir) const
Check whether a framework module can be inferred in the given directory.
Definition: ModuleMap.h:612
bool mayShadowNewModule(Module *ExistingModule)
Definition: ModuleMap.h:605
static bool isBuiltinHeader(StringRef FileName)
Is this a compiler builtin header?
Definition: ModuleMap.cpp:378
Module * createHeaderUnit(SourceLocation Loc, StringRef Name, Module::Header H)
Create a C++20 header unit.
Definition: ModuleMap.cpp:949
void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir, const Twine &NameAsWritten, const Twine &PathRelativeToRootModuleDirectory)
Sets the umbrella directory of the given module to the given directory.
Definition: ModuleMap.cpp:1185
void addModuleMapCallbacks(std::unique_ptr< ModuleMapCallbacks > Callback)
Add a module map callback.
Definition: ModuleMap.h:424
bool parseModuleMapFile(const FileEntry *File, bool IsSystem, const DirectoryEntry *HomeDir, FileID ID=FileID(), unsigned *Offset=nullptr, SourceLocation ExternModuleLoc=SourceLocation())
Parse the given module map file, and record any modules we encounter.
Definition: ModuleMap.cpp:3083
static bool isModular(ModuleHeaderRole Role)
Check if the header with the given role is a modular one.
Definition: ModuleMap.cpp:109
bool resolveConflicts(Module *Mod, bool Complain)
Resolve all of the unresolved conflicts in the given module.
Definition: ModuleMap.cpp:1414
void resolveHeaderDirectives(const FileEntry *File) const
Resolve all lazy header directives for the specified file.
Definition: ModuleMap.cpp:1231
void setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap)
Definition: ModuleMap.cpp:1313
const DirectoryEntry * getBuiltinDir() const
Get the directory that contains Clang-supplied include files.
Definition: ModuleMap.h:415
module_iterator module_begin() const
Definition: ModuleMap.h:738
void setBuiltinIncludeDir(const DirectoryEntry *Dir)
Set the directory that contains Clang-supplied include files, such as our stdarg.h or tgmath....
Definition: ModuleMap.h:410
OptionalFileEntryRef getModuleMapFileForUniquing(const Module *M) const
Get the module map file that (along with the module name) uniquely identifies this module.
Definition: ModuleMap.cpp:1304
void addAdditionalModuleMapFile(const Module *M, const FileEntry *ModuleMap)
Definition: ModuleMap.cpp:1361
ArrayRef< KnownHeader > findResolvedModulesForHeader(const FileEntry *File) const
Like findAllModulesForHeader, but do not attempt to infer module ownership from umbrella headers if w...
Definition: ModuleMap.cpp:698
Module * createModuleForImplementationUnit(SourceLocation Loc, StringRef Name)
Create a new module for a C++ module implementation unit.
Definition: ModuleMap.cpp:925
ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, const LangOptions &LangOpts, const TargetInfo *Target, HeaderSearch &HeaderInfo)
Construct a new module map.
Definition: ModuleMap.cpp:317
std::optional< Module * > getCachedModuleLoad(const IdentifierInfo &II)
Return a cached module load.
Definition: ModuleMap.h:750
std::error_code canonicalizeModuleMapPath(SmallVectorImpl< char > &Path)
Canonicalize Path in a manner suitable for a module map file.
Definition: ModuleMap.cpp:1319
bool isHeaderUnavailableInModule(const FileEntry *Header, const Module *RequestingModule) const
Determine whether the given header is unavailable as part of the specified module.
Definition: ModuleMap.cpp:712
ArrayRef< KnownHeader > findAllModulesForHeader(const FileEntry *File)
Retrieve all the modules that contain the given header file.
Definition: ModuleMap.cpp:686
Module * lookupModuleUnqualified(StringRef Name, Module *Context) const
Retrieve a module with the given name using lexical name lookup, starting at the given context.
Definition: ModuleMap.cpp:817
module_iterator module_end() const
Definition: ModuleMap.h:739
AdditionalModMapsSet * getAdditionalModuleMapFiles(const Module *M)
Get any module map files other than getModuleMapFileForUniquing(M) that define submodules of a top-le...
Definition: ModuleMap.h:652
Module * createImplicitGlobalModuleFragmentForModuleUnit(SourceLocation Loc, bool IsExported, Module *Parent=nullptr)
Definition: ModuleMap.cpp:866
~ModuleMap()
Destroy the module map.
Definition: ModuleMap.cpp:325
Module * createGlobalModuleFragmentForModuleUnit(SourceLocation Loc, Module *Parent=nullptr)
Create a global module fragment for a C++ module unit.
Definition: ModuleMap.cpp:854
void setTarget(const TargetInfo &Target)
Set the target information.
Definition: ModuleMap.cpp:332
KnownHeader findModuleForHeader(const FileEntry *File, bool AllowTextual=false, bool AllowExcluded=false)
Retrieve the module that owns the given header file, if any.
Definition: ModuleMap.cpp:584
Module * lookupModuleQualified(StringRef Name, Module *Context) const
Retrieve a module with the given name within the given context, using direct (qualified) name lookup.
Definition: ModuleMap.cpp:827
void resolveLinkAsDependencies(Module *Mod)
Use PendingLinkAsModule information to mark top level link names that are going to be replaced by exp...
Definition: ModuleMap.cpp:58
void cacheModuleLoad(const IdentifierInfo &II, Module *M)
Cache a module load. M might be nullptr.
Definition: ModuleMap.h:745
ModuleHeaderRole
Flags describing the role of a module header.
Definition: ModuleMap.h:129
@ PrivateHeader
This header is included but private.
Definition: ModuleMap.h:134
@ ExcludedHeader
This header is explicitly excluded from the module.
Definition: ModuleMap.h:141
@ NormalHeader
This header is normally included in the module.
Definition: ModuleMap.h:131
@ TextualHeader
This header is part of the module (for layering purposes) but should be textually included.
Definition: ModuleMap.h:138
Module * createModuleForInterfaceUnit(SourceLocation Loc, StringRef Name)
Create a new module for a C++ module interface unit.
Definition: ModuleMap.cpp:907
void addHeader(Module *Mod, Module::Header Header, ModuleHeaderRole Role, bool Imported=false)
Adds this header to the given module.
Definition: ModuleMap.cpp:1266
Module * createPrivateModuleFragmentForInterfaceUnit(Module *Parent, SourceLocation Loc)
Create a global module fragment for a C++ module interface unit.
Definition: ModuleMap.cpp:882
Module * createModuleUnitWithKind(SourceLocation Loc, StringRef Name, Module::ModuleKind Kind)
Create a new C++ module with the specified kind, and reparent any pending global module fragment(s) t...
Definition: ModuleMap.cpp:891
static ModuleHeaderRole headerKindToRole(Module::HeaderKind Kind)
Convert a header kind to a role. Requires Kind to not be HK_Excluded.
Definition: ModuleMap.cpp:93
bool isHeaderInUnavailableModule(const FileEntry *Header) const
Determine whether the given header is part of a module marked 'unavailable'.
Definition: ModuleMap.cpp:707
llvm::iterator_range< module_iterator > modules() const
Definition: ModuleMap.h:740
bool resolveUses(Module *Mod, bool Complain)
Resolve all of the unresolved uses in the given module.
Definition: ModuleMap.cpp:1401
Describes a module or submodule.
Definition: Module.h:98
Module * Parent
The parent of this module.
Definition: Module.h:147
bool isAvailable() const
Determine whether this module is available for use within the current translation unit.
Definition: Module.h:504
llvm::PointerIntPair< Module *, 1, bool > ExportDecl
Describes an exported module.
Definition: Module.h:387
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition: Module.h:629
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Exposes information about the current target.
Definition: TargetInfo.h:206
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition: FileEntry.h:202
SmallVector< std::pair< std::string, SourceLocation >, 2 > ModuleId
Describes the name of a module.
Definition: Module.h:53
#define false
Definition: stdbool.h:22
#define bool
Definition: stdbool.h:20
Information about a header directive as found in the module map file.
Definition: Module.h:242