clang 19.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/DenseSet.h"
24#include "llvm/ADT/PointerIntPair.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.
61 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 Header The umbrella header to collect.
72};
73
74class ModuleMap {
75 SourceManager &SourceMgr;
76 DiagnosticsEngine &Diags;
77 const LangOptions &LangOpts;
78 const TargetInfo *Target;
79 HeaderSearch &HeaderInfo;
80
82
83 /// The directory used for Clang-supplied, builtin include headers,
84 /// such as "stdint.h".
85 OptionalDirectoryEntryRef BuiltinIncludeDir;
86
87 /// Language options used to parse the module map itself.
88 ///
89 /// These are always simple C language options.
90 LangOptions MMapLangOpts;
91
92 /// The module that the main source file is associated with (the module
93 /// named LangOpts::CurrentModule, if we've loaded it).
94 Module *SourceModule = nullptr;
95
96 /// Submodules of the current module that have not yet been attached to it.
97 /// (Ownership is transferred if/when we create an enclosing module.)
99
100 /// The top-level modules that are known.
101 llvm::StringMap<Module *> Modules;
102
103 /// Module loading cache that includes submodules, indexed by IdentifierInfo.
104 /// nullptr is stored for modules that are known to fail to load.
105 llvm::DenseMap<const IdentifierInfo *, Module *> CachedModuleLoads;
106
107 /// Shadow modules created while building this module map.
108 llvm::SmallVector<Module*, 2> ShadowModules;
109
110 /// The number of modules we have created in total.
111 unsigned NumCreatedModules = 0;
112
113 /// In case a module has a export_as entry, it might have a pending link
114 /// name to be determined if that module is imported.
115 llvm::StringMap<llvm::StringSet<>> PendingLinkAsModule;
116
117public:
118 /// Use PendingLinkAsModule information to mark top level link names that
119 /// are going to be replaced by export_as aliases.
121
122 /// Make module to use export_as as the link dependency name if enough
123 /// information is available or add it to a pending list otherwise.
124 void addLinkAsDependency(Module *Mod);
125
126 /// Flags describing the role of a module header.
128 /// This header is normally included in the module.
130
131 /// This header is included but private.
133
134 /// This header is part of the module (for layering purposes) but
135 /// should be textually included.
137
138 /// This header is explicitly excluded from the module.
140
141 // Caution: Adding an enumerator needs other changes.
142 // Adjust the number of bits for KnownHeader::Storage.
143 // Adjust the HeaderFileInfoTrait::ReadData streaming.
144 // Adjust the HeaderFileInfoTrait::EmitData streaming.
145 // Adjust ModuleMap::addHeader.
146 };
147
148 /// Convert a header kind to a role. Requires Kind to not be HK_Excluded.
150
151 /// Convert a header role to a kind.
153
154 /// Check if the header with the given role is a modular one.
155 static bool isModular(ModuleHeaderRole Role);
156
157 /// A header that is known to reside within a given module,
158 /// whether it was included or excluded.
160 llvm::PointerIntPair<Module *, 3, ModuleHeaderRole> Storage;
161
162 public:
163 KnownHeader() : Storage(nullptr, NormalHeader) {}
164 KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) {}
165
166 friend bool operator==(const KnownHeader &A, const KnownHeader &B) {
167 return A.Storage == B.Storage;
168 }
169 friend bool operator!=(const KnownHeader &A, const KnownHeader &B) {
170 return A.Storage != B.Storage;
171 }
172
173 /// Retrieve the module the header is stored in.
174 Module *getModule() const { return Storage.getPointer(); }
175
176 /// The role of this header within the module.
177 ModuleHeaderRole getRole() const { return Storage.getInt(); }
178
179 /// Whether this header is available in the module.
180 bool isAvailable() const {
181 return getRole() != ExcludedHeader && getModule()->isAvailable();
182 }
183
184 /// Whether this header is accessible from the specified module.
185 bool isAccessibleFrom(Module *M) const {
186 return !(getRole() & PrivateHeader) ||
188 }
189
190 // Whether this known header is valid (i.e., it has an
191 // associated module).
192 explicit operator bool() const {
193 return Storage.getPointer() != nullptr;
194 }
195 };
196
198
199private:
200 friend class ModuleMapParser;
201
202 using HeadersMap = llvm::DenseMap<FileEntryRef, SmallVector<KnownHeader, 1>>;
203
204 /// Mapping from each header to the module that owns the contents of
205 /// that header.
206 HeadersMap Headers;
207
208 /// Map from file sizes to modules with lazy header directives of that size.
209 mutable llvm::DenseMap<off_t, llvm::TinyPtrVector<Module*>> LazyHeadersBySize;
210
211 /// Map from mtimes to modules with lazy header directives with those mtimes.
212 mutable llvm::DenseMap<time_t, llvm::TinyPtrVector<Module*>>
213 LazyHeadersByModTime;
214
215 /// Mapping from directories with umbrella headers to the module
216 /// that is generated from the umbrella header.
217 ///
218 /// This mapping is used to map headers that haven't explicitly been named
219 /// in the module map over to the module that includes them via its umbrella
220 /// header.
221 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
222
223 /// A generation counter that is used to test whether modules of the
224 /// same name may shadow or are illegal redefinitions.
225 ///
226 /// Modules from earlier scopes may shadow modules from later ones.
227 /// Modules from the same scope may not have the same name.
228 unsigned CurrentModuleScopeID = 0;
229
230 llvm::DenseMap<Module *, unsigned> ModuleScopeIDs;
231
232 /// The set of attributes that can be attached to a module.
233 struct Attributes {
234 /// Whether this is a system module.
235 LLVM_PREFERRED_TYPE(bool)
236 unsigned IsSystem : 1;
237
238 /// Whether this is an extern "C" module.
239 LLVM_PREFERRED_TYPE(bool)
240 unsigned IsExternC : 1;
241
242 /// Whether this is an exhaustive set of configuration macros.
243 LLVM_PREFERRED_TYPE(bool)
244 unsigned IsExhaustive : 1;
245
246 /// Whether files in this module can only include non-modular headers
247 /// and headers from used modules.
248 LLVM_PREFERRED_TYPE(bool)
249 unsigned NoUndeclaredIncludes : 1;
250
251 Attributes()
252 : IsSystem(false), IsExternC(false), IsExhaustive(false),
253 NoUndeclaredIncludes(false) {}
254 };
255
256 /// A directory for which framework modules can be inferred.
257 struct InferredDirectory {
258 /// Whether to infer modules from this directory.
259 LLVM_PREFERRED_TYPE(bool)
260 unsigned InferModules : 1;
261
262 /// The attributes to use for inferred modules.
263 Attributes Attrs;
264
265 /// If \c InferModules is non-zero, the module map file that allowed
266 /// inferred modules. Otherwise, invalid.
267 FileID ModuleMapFID;
268
269 /// The names of modules that cannot be inferred within this
270 /// directory.
271 SmallVector<std::string, 2> ExcludedModules;
272
273 InferredDirectory() : InferModules(false) {}
274 };
275
276 /// A mapping from directories to information about inferring
277 /// framework modules from within those directories.
278 llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
279
280 /// A mapping from an inferred module to the module map that allowed the
281 /// inference.
282 llvm::DenseMap<const Module *, FileID> InferredModuleAllowedBy;
283
284 llvm::DenseMap<const Module *, AdditionalModMapsSet> AdditionalModMaps;
285
286 /// Describes whether we haved parsed a particular file as a module
287 /// map.
288 llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap;
289
290 /// Resolve the given export declaration into an actual export
291 /// declaration.
292 ///
293 /// \param Mod The module in which we're resolving the export declaration.
294 ///
295 /// \param Unresolved The export declaration to resolve.
296 ///
297 /// \param Complain Whether this routine should complain about unresolvable
298 /// exports.
299 ///
300 /// \returns The resolved export declaration, which will have a NULL pointer
301 /// if the export could not be resolved.
303 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
304 bool Complain) const;
305
306 /// Resolve the given module id to an actual module.
307 ///
308 /// \param Id The module-id to resolve.
309 ///
310 /// \param Mod The module in which we're resolving the module-id.
311 ///
312 /// \param Complain Whether this routine should complain about unresolvable
313 /// module-ids.
314 ///
315 /// \returns The resolved module, or null if the module-id could not be
316 /// resolved.
317 Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const;
318
319 /// Add an unresolved header to a module.
320 ///
321 /// \param Mod The module in which we're adding the unresolved header
322 /// directive.
323 /// \param Header The unresolved header directive.
324 /// \param NeedsFramework If Mod is not a framework but a missing header would
325 /// be found in case Mod was, set it to true. False otherwise.
326 void addUnresolvedHeader(Module *Mod,
327 Module::UnresolvedHeaderDirective Header,
328 bool &NeedsFramework);
329
330 /// Look up the given header directive to find an actual header file.
331 ///
332 /// \param M The module in which we're resolving the header directive.
333 /// \param Header The header directive to resolve.
334 /// \param RelativePathName Filled in with the relative path name from the
335 /// module to the resolved header.
336 /// \param NeedsFramework If M is not a framework but a missing header would
337 /// be found in case M was, set it to true. False otherwise.
338 /// \return The resolved file, if any.
340 findHeader(Module *M, const Module::UnresolvedHeaderDirective &Header,
341 SmallVectorImpl<char> &RelativePathName, bool &NeedsFramework);
342
343 /// Resolve the given header directive.
344 ///
345 /// \param M The module in which we're resolving the header directive.
346 /// \param Header The header directive to resolve.
347 /// \param NeedsFramework If M is not a framework but a missing header would
348 /// be found in case M was, set it to true. False otherwise.
349 void resolveHeader(Module *M, const Module::UnresolvedHeaderDirective &Header,
350 bool &NeedsFramework);
351
352 /// Attempt to resolve the specified header directive as naming a builtin
353 /// header.
354 /// \return \c true if a corresponding builtin header was found.
355 bool resolveAsBuiltinHeader(Module *M,
356 const Module::UnresolvedHeaderDirective &Header);
357
358 /// Looks up the modules that \p File corresponds to.
359 ///
360 /// If \p File represents a builtin header within Clang's builtin include
361 /// directory, this also loads all of the module maps to see if it will get
362 /// associated with a specific module (e.g. in /usr/include).
363 HeadersMap::iterator findKnownHeader(FileEntryRef File);
364
365 /// Searches for a module whose umbrella directory contains \p File.
366 ///
367 /// \param File The header to search for.
368 ///
369 /// \param IntermediateDirs On success, contains the set of directories
370 /// searched before finding \p File.
371 KnownHeader findHeaderInUmbrellaDirs(
372 FileEntryRef File, SmallVectorImpl<DirectoryEntryRef> &IntermediateDirs);
373
374 /// Given that \p File is not in the Headers map, look it up within
375 /// umbrella directories and find or create a module for it.
376 KnownHeader findOrCreateModuleForHeaderInUmbrellaDir(FileEntryRef File);
377
378 /// A convenience method to determine if \p File is (possibly nested)
379 /// in an umbrella directory.
380 bool isHeaderInUmbrellaDirs(FileEntryRef File) {
381 SmallVector<DirectoryEntryRef, 2> IntermediateDirs;
382 return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs));
383 }
384
385 Module *inferFrameworkModule(DirectoryEntryRef FrameworkDir, Attributes Attrs,
386 Module *Parent);
387
388public:
389 /// Construct a new module map.
390 ///
391 /// \param SourceMgr The source manager used to find module files and headers.
392 /// This source manager should be shared with the header-search mechanism,
393 /// since they will refer to the same headers.
394 ///
395 /// \param Diags A diagnostic engine used for diagnostics.
396 ///
397 /// \param LangOpts Language options for this translation unit.
398 ///
399 /// \param Target The target for this translation unit.
400 ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
401 const LangOptions &LangOpts, const TargetInfo *Target,
402 HeaderSearch &HeaderInfo);
403
404 /// Destroy the module map.
405 ~ModuleMap();
406
407 /// Set the target information.
408 void setTarget(const TargetInfo &Target);
409
410 /// Set the directory that contains Clang-supplied include files, such as our
411 /// stdarg.h or tgmath.h.
412 void setBuiltinIncludeDir(DirectoryEntryRef Dir) { BuiltinIncludeDir = Dir; }
413
414 /// Get the directory that contains Clang-supplied include files.
415 OptionalDirectoryEntryRef getBuiltinDir() const { return BuiltinIncludeDir; }
416
417 /// Is this a compiler builtin header?
419
421 Module *Module) const;
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(FileEntryRef File, bool AllowTextual = false,
443 bool AllowExcluded = false);
444
445 /// Retrieve all the modules that contain the given header file. Note that
446 /// this does not implicitly load module maps, except for builtin headers,
447 /// and does not consult the external source. (Those checks are the
448 /// responsibility of \ref HeaderSearch.)
449 ///
450 /// Typically, \ref findModuleForHeader should be used instead, as it picks
451 /// the preferred module for the header.
453
454 /// Like \ref findAllModulesForHeader, but do not attempt to infer module
455 /// ownership from umbrella headers if we've not already done so.
457
458 /// Resolve all lazy header directives for the specified file.
459 ///
460 /// This ensures that the HeaderFileInfo on HeaderSearch is up to date. This
461 /// is effectively internal, but is exposed so HeaderSearch can call it.
462 void resolveHeaderDirectives(const FileEntry *File) const;
463
464 /// Resolve lazy header directives for the specified module. If File is
465 /// provided, only headers with same size and modtime are resolved. If File
466 /// is not set, all headers are resolved.
468 std::optional<const FileEntry *> File) const;
469
470 /// Reports errors if a module must not include a specific file.
471 ///
472 /// \param RequestingModule The module including a file.
473 ///
474 /// \param RequestingModuleIsModuleInterface \c true if the inclusion is in
475 /// the interface of RequestingModule, \c false if it's in the
476 /// implementation of RequestingModule. Value is ignored and
477 /// meaningless if RequestingModule is nullptr.
478 ///
479 /// \param FilenameLoc The location of the inclusion's filename.
480 ///
481 /// \param Filename The included filename as written.
482 ///
483 /// \param File The included file.
484 void diagnoseHeaderInclusion(Module *RequestingModule,
485 bool RequestingModuleIsModuleInterface,
486 SourceLocation FilenameLoc, StringRef Filename,
488
489 /// Determine whether the given header is part of a module
490 /// marked 'unavailable'.
491 bool isHeaderInUnavailableModule(FileEntryRef Header) const;
492
493 /// Determine whether the given header is unavailable as part
494 /// of the specified module.
496 const Module *RequestingModule) const;
497
498 /// Retrieve a module with the given name.
499 ///
500 /// \param Name The name of the module to look up.
501 ///
502 /// \returns The named module, if known; otherwise, returns null.
503 Module *findModule(StringRef Name) const;
504
505 /// Retrieve a module with the given name using lexical name lookup,
506 /// starting at the given context.
507 ///
508 /// \param Name The name of the module to look up.
509 ///
510 /// \param Context The module context, from which we will perform lexical
511 /// name lookup.
512 ///
513 /// \returns The named module, if known; otherwise, returns null.
514 Module *lookupModuleUnqualified(StringRef Name, Module *Context) const;
515
516 /// Retrieve a module with the given name within the given context,
517 /// using direct (qualified) name lookup.
518 ///
519 /// \param Name The name of the module to look up.
520 ///
521 /// \param Context The module for which we will look for a submodule. If
522 /// null, we will look for a top-level module.
523 ///
524 /// \returns The named submodule, if known; otherwose, returns null.
525 Module *lookupModuleQualified(StringRef Name, Module *Context) const;
526
527 /// Find a new module or submodule, or create it if it does not already
528 /// exist.
529 ///
530 /// \param Name The name of the module to find or create.
531 ///
532 /// \param Parent The module that will act as the parent of this submodule,
533 /// or nullptr to indicate that this is a top-level module.
534 ///
535 /// \param IsFramework Whether this is a framework module.
536 ///
537 /// \param IsExplicit Whether this is an explicit submodule.
538 ///
539 /// \returns The found or newly-created module, along with a boolean value
540 /// that will be true if the module is newly-created.
541 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
542 bool IsFramework,
543 bool IsExplicit);
544
545 /// Create a global module fragment for a C++ module unit.
546 ///
547 /// We model the global module fragment as a submodule of the module
548 /// interface unit. Unfortunately, we can't create the module interface
549 /// unit's Module until later, because we don't know what it will be called
550 /// usually. See C++20 [module.unit]/7.2 for the case we could know its
551 /// parent.
553 Module *Parent = nullptr);
555 Module *Parent);
556
557 /// Create a global module fragment for a C++ module interface unit.
559 SourceLocation Loc);
560
561 /// Create a new C++ module with the specified kind, and reparent any pending
562 /// global module fragment(s) to it.
564 Module::ModuleKind Kind);
565
566 /// Create a new module for a C++ module interface unit.
567 /// The module must not already exist, and will be configured for the current
568 /// compilation.
569 ///
570 /// Note that this also sets the current module to the newly-created module.
571 ///
572 /// \returns The newly-created module.
574
575 /// Create a new module for a C++ module implementation unit.
576 /// The interface module for this implementation (implicitly imported) must
577 /// exist and be loaded and present in the modules map.
578 ///
579 /// \returns The newly-created module.
581
582 /// Create a C++20 header unit.
583 Module *createHeaderUnit(SourceLocation Loc, StringRef Name,
585
586 /// Infer the contents of a framework module map from the given
587 /// framework directory.
588 Module *inferFrameworkModule(DirectoryEntryRef FrameworkDir, bool IsSystem,
589 Module *Parent);
590
591 /// Create a new top-level module that is shadowed by
592 /// \p ShadowingModule.
593 Module *createShadowedModule(StringRef Name, bool IsFramework,
594 Module *ShadowingModule);
595
596 /// Creates a new declaration scope for module names, allowing
597 /// previously defined modules to shadow definitions from the new scope.
598 ///
599 /// \note Module names from earlier scopes will shadow names from the new
600 /// scope, which is the opposite of how shadowing works for variables.
601 void finishModuleDeclarationScope() { CurrentModuleScopeID += 1; }
602
603 bool mayShadowNewModule(Module *ExistingModule) {
604 assert(!ExistingModule->Parent && "expected top-level module");
605 assert(ModuleScopeIDs.count(ExistingModule) && "unknown module");
606 return ModuleScopeIDs[ExistingModule] < CurrentModuleScopeID;
607 }
608
609 /// Check whether a framework module can be inferred in the given directory.
611 auto It = InferredDirectories.find(Dir);
612 return It != InferredDirectories.end() && It->getSecond().InferModules;
613 }
614
615 /// Retrieve the module map file containing the definition of the given
616 /// module.
617 ///
618 /// \param Module The module whose module map file will be returned, if known.
619 ///
620 /// \returns The FileID for the module map file containing the given module,
621 /// invalid if the module definition was inferred.
624
625 /// Get the module map file that (along with the module name) uniquely
626 /// identifies this module.
627 ///
628 /// The particular module that \c Name refers to may depend on how the module
629 /// was found in header search. However, the combination of \c Name and
630 /// this module map will be globally unique for top-level modules. In the case
631 /// of inferred modules, returns the module map that allowed the inference
632 /// (e.g. contained 'module *'). Otherwise, returns
633 /// getContainingModuleMapFile().
636
637 void setInferredModuleAllowedBy(Module *M, FileID ModMapFID);
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 header.
692 void
694 const Twine &NameAsWritten,
695 const Twine &PathRelativeToRootModuleDirectory);
696
697 /// Sets the umbrella directory of the given module to the given directory.
698 void setUmbrellaDirAsWritten(Module *Mod, DirectoryEntryRef UmbrellaDir,
699 const Twine &NameAsWritten,
700 const Twine &PathRelativeToRootModuleDirectory);
701
702 /// Adds this header to the given module.
703 /// \param Role The role of the header wrt the module.
704 void addHeader(Module *Mod, Module::Header Header,
705 ModuleHeaderRole Role, bool Imported = false);
706
707 /// Parse the given module map file, and record any modules we
708 /// encounter.
709 ///
710 /// \param File The file to be parsed.
711 ///
712 /// \param IsSystem Whether this module map file is in a system header
713 /// directory, and therefore should be considered a system module.
714 ///
715 /// \param HomeDir The directory in which relative paths within this module
716 /// map file will be resolved.
717 ///
718 /// \param ID The FileID of the file to process, if we've already entered it.
719 ///
720 /// \param Offset [inout] On input the offset at which to start parsing. On
721 /// output, the offset at which the module map terminated.
722 ///
723 /// \param ExternModuleLoc The location of the "extern module" declaration
724 /// that caused us to load this module map file, if any.
725 ///
726 /// \returns true if an error occurred, false otherwise.
727 bool parseModuleMapFile(FileEntryRef File, bool IsSystem,
728 DirectoryEntryRef HomeDir, FileID ID = FileID(),
729 unsigned *Offset = nullptr,
730 SourceLocation ExternModuleLoc = SourceLocation());
731
732 /// Dump the contents of the module map, for debugging purposes.
733 void dump();
734
735 using module_iterator = llvm::StringMap<Module *>::const_iterator;
736
737 module_iterator module_begin() const { return Modules.begin(); }
738 module_iterator module_end() const { return Modules.end(); }
739 llvm::iterator_range<module_iterator> modules() const {
740 return {module_begin(), module_end()};
741 }
742
743 /// Cache a module load. M might be nullptr.
745 CachedModuleLoads[&II] = M;
746 }
747
748 /// Return a cached module load.
749 std::optional<Module *> getCachedModuleLoad(const IdentifierInfo &II) {
750 auto I = CachedModuleLoads.find(&II);
751 if (I == CachedModuleLoads.end())
752 return std::nullopt;
753 return I->second;
754 }
755};
756
757} // namespace clang
758
759#endif // LLVM_CLANG_LEX_MODULEMAP_H
NodeId Parent
Definition: ASTDiff.cpp:191
int Id
Definition: ASTDiff.cpp:190
static char ID
Definition: Arena.cpp:183
StringRef Filename
Definition: Format.cpp:2971
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Defines the clang::LangOptions interface.
llvm::MachO::Target Target
Definition: MachO.h:48
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
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
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:300
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
Definition: HeaderSearch.h:253
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:461
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, FileEntryRef File, bool IsSystem)
Called when a module map file has been read.
Definition: ModuleMap.h:60
virtual ~ModuleMapCallbacks()=default
virtual void moduleMapAddHeader(StringRef Filename)
Called when a header is added during module map parsing.
Definition: ModuleMap.h:66
virtual void moduleMapAddUmbrellaHeader(FileEntryRef Header)
Called when an umbrella header is added during module map parsing.
Definition: ModuleMap.h:71
A header that is known to reside within a given module, whether it was included or excluded.
Definition: ModuleMap.h:159
KnownHeader(Module *M, ModuleHeaderRole Role)
Definition: ModuleMap.h:164
bool isAccessibleFrom(Module *M) const
Whether this header is accessible from the specified module.
Definition: ModuleMap.h:185
ModuleHeaderRole getRole() const
The role of this header within the module.
Definition: ModuleMap.h:177
friend bool operator!=(const KnownHeader &A, const KnownHeader &B)
Definition: ModuleMap.h:169
friend bool operator==(const KnownHeader &A, const KnownHeader &B)
Definition: ModuleMap.h:166
Module * getModule() const
Retrieve the module the header is stored in.
Definition: ModuleMap.h:174
bool isAvailable() const
Whether this header is available in the module.
Definition: ModuleMap.h:180
Module * createShadowedModule(StringRef Name, bool IsFramework, Module *ShadowingModule)
Create a new top-level module that is shadowed by ShadowingModule.
Definition: ModuleMap.cpp:1171
bool resolveExports(Module *Mod, bool Complain)
Resolve all of the unresolved exports in the given module.
Definition: ModuleMap.cpp:1399
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:1377
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:851
llvm::StringMap< Module * >::const_iterator module_iterator
Definition: ModuleMap.h:735
void setUmbrellaDirAsWritten(Module *Mod, DirectoryEntryRef UmbrellaDir, const Twine &NameAsWritten, const Twine &PathRelativeToRootModuleDirectory)
Sets the umbrella directory of the given module to the given directory.
Definition: ModuleMap.cpp:1201
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:496
void addAdditionalModuleMapFile(const Module *M, FileEntryRef ModuleMap)
Definition: ModuleMap.cpp:1372
OptionalFileEntryRef getContainingModuleMapFile(const Module *Module) const
Definition: ModuleMap.cpp:1317
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:826
void finishModuleDeclarationScope()
Creates a new declaration scope for module names, allowing previously defined modules to shadow defin...
Definition: ModuleMap.h:601
bool canInferFrameworkModule(const DirectoryEntry *Dir) const
Check whether a framework module can be inferred in the given directory.
Definition: ModuleMap.h:610
bool mayShadowNewModule(Module *ExistingModule)
Definition: ModuleMap.h:603
KnownHeader findModuleForHeader(FileEntryRef File, bool AllowTextual=false, bool AllowExcluded=false)
Retrieve the module that owns the given header file, if any.
Definition: ModuleMap.cpp:606
Module * createHeaderUnit(SourceLocation Loc, StringRef Name, Module::Header H)
Create a C++20 header unit.
Definition: ModuleMap.cpp:966
void addModuleMapCallbacks(std::unique_ptr< ModuleMapCallbacks > Callback)
Add a module map callback.
Definition: ModuleMap.h:424
void setBuiltinIncludeDir(DirectoryEntryRef Dir)
Set the directory that contains Clang-supplied include files, such as our stdarg.h or tgmath....
Definition: ModuleMap.h:412
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:1426
bool isHeaderUnavailableInModule(FileEntryRef Header, const Module *RequestingModule) const
Determine whether the given header is unavailable as part of the specified module.
Definition: ModuleMap.cpp:732
void resolveHeaderDirectives(const FileEntry *File) const
Resolve all lazy header directives for the specified file.
Definition: ModuleMap.cpp:1247
module_iterator module_begin() const
Definition: ModuleMap.h:737
ArrayRef< KnownHeader > findResolvedModulesForHeader(FileEntryRef File) const
Like findAllModulesForHeader, but do not attempt to infer module ownership from umbrella headers if w...
Definition: ModuleMap.cpp:719
OptionalFileEntryRef getModuleMapFileForUniquing(const Module *M) const
Definition: ModuleMap.cpp:1330
bool shouldImportRelativeToBuiltinIncludeDir(StringRef FileName, Module *Module) const
Definition: ModuleMap.cpp:420
Module * createModuleForImplementationUnit(SourceLocation Loc, StringRef Name)
Create a new module for a C++ module implementation unit.
Definition: ModuleMap.cpp:942
ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, const LangOptions &LangOpts, const TargetInfo *Target, HeaderSearch &HeaderInfo)
Construct a new module map.
Definition: ModuleMap.cpp:357
std::optional< Module * > getCachedModuleLoad(const IdentifierInfo &II)
Return a cached module load.
Definition: ModuleMap.h:749
std::error_code canonicalizeModuleMapPath(SmallVectorImpl< char > &Path)
Canonicalize Path in a manner suitable for a module map file.
Definition: ModuleMap.cpp:1340
FileID getModuleMapFileIDForUniquing(const Module *M) const
Get the module map file that (along with the module name) uniquely identifies this module.
Definition: ModuleMap.cpp:1321
void setInferredModuleAllowedBy(Module *M, FileID ModMapFID)
Definition: ModuleMap.cpp:1334
void setUmbrellaHeaderAsWritten(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:1186
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:834
bool isBuiltinHeader(FileEntryRef File)
Is this a compiler builtin header?
Definition: ModuleMap.cpp:415
module_iterator module_end() const
Definition: ModuleMap.h:738
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
bool isHeaderInUnavailableModule(FileEntryRef Header) const
Determine whether the given header is part of a module marked 'unavailable'.
Definition: ModuleMap.cpp:728
FileID getContainingModuleMapFileID(const Module *Module) const
Retrieve the module map file containing the definition of the given module.
Definition: ModuleMap.cpp:1309
OptionalDirectoryEntryRef getBuiltinDir() const
Get the directory that contains Clang-supplied include files.
Definition: ModuleMap.h:415
bool parseModuleMapFile(FileEntryRef File, bool IsSystem, DirectoryEntryRef 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:3119
~ModuleMap()
Destroy the module map.
Definition: ModuleMap.cpp:365
Module * createGlobalModuleFragmentForModuleUnit(SourceLocation Loc, Module *Parent=nullptr)
Create a global module fragment for a C++ module unit.
Definition: ModuleMap.cpp:871
void setTarget(const TargetInfo &Target)
Set the target information.
Definition: ModuleMap.cpp:372
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:844
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:744
ModuleHeaderRole
Flags describing the role of a module header.
Definition: ModuleMap.h:127
@ PrivateHeader
This header is included but private.
Definition: ModuleMap.h:132
@ ExcludedHeader
This header is explicitly excluded from the module.
Definition: ModuleMap.h:139
@ NormalHeader
This header is normally included in the module.
Definition: ModuleMap.h:129
@ TextualHeader
This header is part of the module (for layering purposes) but should be textually included.
Definition: ModuleMap.h:136
Module * createModuleForInterfaceUnit(SourceLocation Loc, StringRef Name)
Create a new module for a C++ module interface unit.
Definition: ModuleMap.cpp:924
void addHeader(Module *Mod, Module::Header Header, ModuleHeaderRole Role, bool Imported=false)
Adds this header to the given module.
Definition: ModuleMap.cpp:1282
Module * createPrivateModuleFragmentForInterfaceUnit(Module *Parent, SourceLocation Loc)
Create a global module fragment for a C++ module interface unit.
Definition: ModuleMap.cpp:899
ArrayRef< KnownHeader > findAllModulesForHeader(FileEntryRef File)
Retrieve all the modules that contain the given header file.
Definition: ModuleMap.cpp:707
Module * createImplicitGlobalModuleFragmentForModuleUnit(SourceLocation Loc, Module *Parent)
Definition: ModuleMap.cpp:884
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:908
static ModuleHeaderRole headerKindToRole(Module::HeaderKind Kind)
Convert a header kind to a role. Requires Kind to not be HK_Excluded.
Definition: ModuleMap.cpp:93
llvm::iterator_range< module_iterator > modules() const
Definition: ModuleMap.h:739
bool resolveUses(Module *Mod, bool Complain)
Resolve all of the unresolved uses in the given module.
Definition: ModuleMap.cpp:1412
Describes a module or submodule.
Definition: Module.h:105
Module * Parent
The parent of this module.
Definition: Module.h:154
bool isAvailable() const
Determine whether this module is available for use within the current translation unit.
Definition: Module.h:528
llvm::PointerIntPair< Module *, 1, bool > ExportDecl
Describes an exported module.
Definition: Module.h:411
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition: Module.h:665
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:214
@ HeaderSearch
Remove unused header search paths including header maps.
The JSON file list parser is used to communicate input to InstallAPI.
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition: FileEntry.h:202
SmallVector< std::pair< std::string, SourceLocation >, 2 > ModuleId
Describes the name of a module.
Definition: Module.h:54
#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:250