20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/StringSwitch.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
36std::optional<ModuleFileKey>
38 if (ImplicitModuleSuffixLength) {
39 StringRef ModuleCachePath =
40 StringRef(Path).drop_back(ImplicitModuleSuffixLength);
41 StringRef ImplicitModuleSuffix =
42 StringRef(Path).take_back(ImplicitModuleSuffixLength);
44 ModuleCachePath,
false))
47 if (
auto ModuleFile =
FileMgr.getOptionalFileRef(Path,
true,
76 Parent->SubModules.push_back(
this);
83 StringRef Platform =
Target.getPlatformName();
84 StringRef Env =
Target.getTriple().getEnvironmentName();
91 auto CmpPlatformEnv = [](StringRef LHS, StringRef RHS) {
92 auto Pos = LHS.find(
'-');
93 if (Pos == StringRef::npos)
96 NewLHS += LHS.slice(Pos+1, LHS.size());
107 if (
Target.getTriple().isOSDarwin() && PlatformEnv.ends_with(
"simulator"))
108 return PlatformEnv ==
Feature || CmpPlatformEnv(PlatformEnv,
Feature);
118 .Case(
"altivec", LangOpts.AltiVec)
119 .Case(
"blocks", LangOpts.Blocks)
120 .Case(
"coroutines", LangOpts.Coroutines)
121 .Case(
"cplusplus", LangOpts.CPlusPlus)
122 .Case(
"cplusplus11", LangOpts.CPlusPlus11)
123 .Case(
"cplusplus14", LangOpts.CPlusPlus14)
124 .Case(
"cplusplus17", LangOpts.CPlusPlus17)
125 .Case(
"cplusplus20", LangOpts.CPlusPlus20)
126 .Case(
"cplusplus23", LangOpts.CPlusPlus23)
127 .Case(
"cplusplus26", LangOpts.CPlusPlus26)
128 .Case(
"c99", LangOpts.C99)
129 .Case(
"c11", LangOpts.C11)
130 .Case(
"c17", LangOpts.C17)
131 .Case(
"c23", LangOpts.C23)
132 .Case(
"freestanding", LangOpts.Freestanding)
133 .Case(
"gnuinlineasm", LangOpts.GNUAsm)
134 .Case(
"objc", LangOpts.ObjC)
135 .Case(
"objc_arc", LangOpts.ObjCAutoRefCount)
136 .Case(
"opencl", LangOpts.OpenCL)
137 .Case(
"tls",
Target.isTLSSupported())
138 .Case(
"zvector", LangOpts.ZVector)
152 for (
const Module *Current =
this; Current; Current = Current->Parent) {
153 if (Current->ShadowingModule) {
157 for (
unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
159 Current->Requirements[I].RequiredState) {
160 Req = Current->Requirements[I];
166 llvm_unreachable(
"could not find a reason why module is unimportable");
184 !CurrentModule.ends_with(
"_Private") &&
185 TopLevelName.ends_with(
"_Private"))
186 TopLevelName = TopLevelName.drop_back(8);
188 return TopLevelName == CurrentModule;
203 for (
const Module *Current =
this; Current; Current = Current->Parent) {
204 if (!Current->MissingHeaders.empty()) {
205 MissingHeader = Current->MissingHeaders.front();
210 llvm_unreachable(
"could not find a reason why module is unavailable");
230 const std::pair<std::string, SourceLocation> &IdComponent) {
231 return IdComponent.first;
236template<
typename InputIter>
238 bool AllowStringLiterals =
true) {
239 for (InputIter It = Begin; It != End; ++It) {
248 OS.write_escaped(Name);
254template<
typename Container>
263 for (
const Module *M =
this; M; M = M->Parent)
264 Names.push_back(M->Name);
268 llvm::raw_string_ostream Out(
Result);
269 printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
275 for (
const Module *M =
this; M; M = M->Parent) {
276 if (nameParts.empty() || M->Name != nameParts.back())
278 nameParts = nameParts.drop_back();
280 return nameParts.empty();
284 if (
const auto *Hdr = std::get_if<FileEntryRef>(&
Umbrella))
285 return Hdr->getDir();
286 if (
const auto *Dir = std::get_if<DirectoryEntryRef>(&
Umbrella))
293 TopHeaders.insert(
File);
297 if (!TopHeaderNames.empty()) {
298 for (StringRef TopHeaderName : TopHeaderNames)
299 if (
auto FE =
FileMgr.getOptionalFileRef(TopHeaderName))
300 TopHeaders.insert(*FE);
301 TopHeaderNames.clear();
314 for (
auto *Use : Top->DirectUses)
324 if (!Requested->
Parent && Requested->
Name ==
"ptrauth")
346 auto needUpdate = [Unimportable](
Module *M) {
347 return M->IsAvailable || (!M->IsUnimportable && Unimportable);
350 if (!needUpdate(
this))
354 Stack.push_back(
this);
355 while (!Stack.empty()) {
356 Module *Current = Stack.pop_back_val();
358 if (!needUpdate(Current))
363 for (
auto *Submodule : Current->
submodules()) {
364 if (needUpdate(Submodule))
365 Stack.push_back(Submodule);
372 for (
unsigned I = SubModuleIndex.size(), E = SubModules.size(); I != E; ++I)
373 SubModuleIndex[SubModules[I]->Name] = I;
375 if (
auto It = SubModuleIndex.find(
Name); It != SubModuleIndex.end())
376 return SubModules[It->second];
383 "fragment from the C++20 Named modules");
385 for (
auto *SubModule : SubModules)
386 if (SubModule->isExplicitGlobalModule())
394 "fragment from the C++20 Named modules");
396 for (
auto *SubModule : SubModules)
397 if (SubModule->isPrivateModule())
405 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
406 E = SubModules.end();
410 Exported.push_back(Mod);
414 bool AnyWildcard =
false;
415 bool UnrestrictedWildcard =
false;
417 for (
unsigned I = 0, N =
Exports.size(); I != N; ++I) {
421 Exported.push_back(Mod);
429 if (UnrestrictedWildcard)
433 WildcardRestrictions.push_back(Restriction);
435 WildcardRestrictions.clear();
436 UnrestrictedWildcard =
true;
445 for (
unsigned I = 0, N =
Imports.size(); I != N; ++I) {
447 bool Acceptable = UnrestrictedWildcard;
450 for (
unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
451 Module *Restriction = WildcardRestrictions[R];
462 Exported.push_back(Mod);
466void Module::buildVisibleModulesCache()
const {
467 assert(VisibleModulesCache.empty() &&
"cache does not need building");
470 VisibleModulesCache.insert(
this);
474 while (!Stack.empty()) {
475 Module *CurrModule = Stack.pop_back_val();
478 if (VisibleModulesCache.insert(CurrModule).second)
505 for (
unsigned I = 0, N =
Requirements.size(); I != N; ++I) {
517 OS <<
"umbrella header \"";
518 OS.write_escaped(H->NameAsWritten);
523 OS.write_escaped(D->NameAsWritten);
529 OS <<
"config_macros ";
531 OS <<
"[exhaustive]";
532 for (
unsigned I = 0, N =
ConfigMacros.size(); I != N; ++I) {
549 for (
auto &K : Kinds) {
550 assert(&K == &Kinds[K.Kind] &&
"kinds in wrong order");
553 OS << K.Prefix <<
"header \"";
554 OS.write_escaped(H.NameAsWritten);
555 OS <<
"\" { size " << H.Entry.getSize()
556 <<
" mtime " << H.Entry.getModificationTime() <<
" }\n";
562 OS << Kinds[
U.Kind].Prefix <<
"header \"";
563 OS.write_escaped(
U.FileName);
565 if (
U.Size ||
U.ModTime) {
568 OS <<
" size " << *
U.Size;
570 OS <<
" mtime " << *
U.ModTime;
587 if (!Submodule->IsInferred || Submodule->IsFramework)
588 Submodule->print(OS,
Indent + 2, Dump);
590 for (
unsigned I = 0, N =
Exports.size(); I != N; ++I) {
594 OS << Restriction->getFullModuleName(
true);
615 llvm::errs() <<
"import " << M->getFullModuleName() <<
"\n";
619 for (
unsigned I = 0, N =
DirectUses.size(); I != N; ++I) {
633 for (
unsigned I = 0, N =
LinkLibraries.size(); I != N; ++I) {
652 for (
unsigned I = 0, N =
Conflicts.size(); I != N; ++I) {
655 OS <<
Conflicts[I].Other->getFullModuleName(
true);
665 OS <<
"module * {\n";
679 print(llvm::errs(), 0,
true);
687 "setVisible expects a valid import location");
695 Visiting *ExportedBy;
700 unsigned ID =
V.M->getVisibilityID();
701 if (ImportLocs.size() <= ID)
702 ImportLocs.resize(ID + 1);
703 else if (ImportLocs[ID].isValid())
706 ImportLocs[ID] = Loc;
710 if (IncludeExports) {
712 V.M->getExportedModules(Exports);
713 for (
Module *E : Exports) {
715 if (!E->isUnimportable())
716 VisitModule({E, &
V});
720 for (
auto &
C :
V.M->Conflicts) {
723 for (Visiting *I = &
V; I; I = I->ExportedBy)
724 Path.push_back(I->M);
725 Cb(Path,
C.Other,
C.Message);
729 VisitModule({M,
nullptr});
Defines the clang::FileManager interface and associated types.
Defines the clang::LangOptions interface.
llvm::MachO::Target Target
static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature)
static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, const TargetInfo &Target)
Determine whether a translation unit built using the current language options has the given feature.
static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End, bool AllowStringLiterals=true)
static StringRef getModuleNameFromComponent(const std::pair< std::string, SourceLocation > &IdComponent)
Defines the clang::Module class, which describes a module in the source code.
static bool HasFeature(const Preprocessor &PP, StringRef Feature)
HasFeature - Return true if we recognize and implement the feature specified by the identifier as a s...
Defines the clang::SourceLocation class and associated facilities.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Implements support for file system lookup, file system caching, and directory search management.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
std::string ModuleName
The module currently being compiled as specified by -fmodule-name.
bool isCompilingModule() const
Are we compiling a module?
std::string CurrentModule
The name of the current module, of which the main source file is a part.
std::vector< std::string > ModuleFeatures
The names of any features to enable in module 'requires' decls in addition to the hard-coded list in ...
The module cache used for compiling modules implicitly.
Required to construct a Module.
Deduplication key for a loaded module file in ModuleManager.
std::optional< ModuleFileKey > makeKey(FileManager &FileMgr) const
Creates the deduplication key for use in ModuleManager.
Describes a module or submodule.
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
void addRequirement(StringRef Feature, bool RequiredState, const LangOptions &LangOpts, const TargetInfo &Target)
Add the given feature requirement to the list of features required by this module.
unsigned IsExplicit
Whether this is an explicit submodule.
SmallVector< ExportDecl, 2 > Exports
The set of export declarations.
bool isForBuilding(const LangOptions &LangOpts) const
Determine whether this module can be built in this compilation.
std::variant< std::monostate, FileEntryRef, DirectoryEntryRef > Umbrella
The umbrella header or directory.
unsigned InferSubmodules
Whether we should infer submodules for this module based on the headers.
Module * findSubmodule(StringRef Name) const
Find the submodule with the given name.
bool directlyUses(const Module *Requested)
Determine whether this module has declared its intention to directly use another module.
std::vector< std::string > ConfigMacros
The set of "configuration macros", which are macros that (intentionally) change how this module is bu...
unsigned IsUnimportable
Whether this module has declared itself unimportable, either because it's missing a requirement from ...
NameVisibilityKind NameVisibility
The visibility of names within this particular module.
@ Hidden
All of the names in this module are hidden.
void print(raw_ostream &OS, unsigned Indent=0, bool Dump=false) const
Print the module map for this module to the given stream.
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
SourceLocation DefinitionLoc
The location of the module definition.
SmallVector< UnresolvedHeaderDirective, 1 > MissingHeaders
Headers that are mentioned in the module map file but could not be found on the file system.
Module(ModuleConstructorTag, StringRef Name, SourceLocation DefinitionLoc, Module *Parent, bool IsFramework, bool IsExplicit, unsigned VisibilityID)
Construct a new module or submodule.
Module * Parent
The parent of this module.
void markUnavailable(bool Unimportable)
Mark this module and all of its submodules as unavailable.
SmallVector< UnresolvedHeaderDirective, 1 > UnresolvedHeaders
Headers that are mentioned in the module map file but that we have not yet attempted to resolve to a ...
ModuleKind Kind
The kind of this module.
bool isUnimportable() const
Determine whether this module has been declared unimportable.
bool fullModuleNameIs(ArrayRef< StringRef > nameParts) const
Whether the full name of this module is equal to joining nameParts with "."s.
Module * getPrivateModuleFragment() const
Get the Private Module Fragment (sub-module) for this module, it there is one.
unsigned IsInferred
Whether this is an inferred submodule (module * { ... }).
llvm::SmallSetVector< Module *, 2 > Imports
The set of modules imported by this module, and on which this module depends.
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers).
std::string Name
The name of this module.
Module * getGlobalModuleFragment() const
Get the Global Module Fragment (sub-module) for this module, it there is one.
llvm::iterator_range< submodule_iterator > submodules()
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "C"...
unsigned ModuleMapIsPrivate
Whether this module came from a "private" module map, found next to a regular (public) module map.
llvm::SmallVector< LinkLibrary, 2 > LinkLibraries
The set of libraries or frameworks to link against when an entity from this module is used.
SmallVector< UnresolvedExportDecl, 2 > UnresolvedExports
The set of export declarations that have yet to be resolved.
std::optional< Header > getUmbrellaHeaderAsWritten() const
Retrieve the umbrella header as written.
SmallVector< Requirement, 2 > Requirements
The set of language features required to use this module.
llvm::SmallSetVector< const Module *, 2 > UndeclaredUses
When NoUndeclaredIncludes is true, the set of modules this module tried to import but didn't because ...
SmallVector< ModuleId, 2 > UnresolvedDirectUses
The set of use declarations that have yet to be resolved.
unsigned NamedModuleHasInit
Whether this C++20 named modules doesn't need an initializer.
unsigned NoUndeclaredIncludes
Whether files in this module can only include non-modular headers and headers from used modules.
SmallVector< Module *, 2 > DirectUses
The directly used modules.
unsigned ConfigMacrosExhaustive
Whether the set of configuration macros is exhaustive.
ArrayRef< Header > getHeaders(HeaderKind HK) const
bool isGlobalModule() const
Does this Module scope describe a fragment of the global module within some C++ module.
unsigned InferExportWildcard
Whether, when inferring submodules, the inferr submodules should export all modules they import (e....
void getExportedModules(SmallVectorImpl< Module * > &Exported) const
Appends this module's list of exported modules to Exported.
std::vector< UnresolvedConflict > UnresolvedConflicts
The list of conflicts for which the module-id has not yet been resolved.
unsigned IsFromModuleFile
Whether this module was loaded from a module file.
bool isSubModuleOf(const Module *Other) const
Check if this module is a (possibly transitive) submodule of Other.
ArrayRef< FileEntryRef > getTopHeaders(FileManager &FileMgr)
The top-level headers associated with this module.
bool isAvailable() const
Determine whether this module is available for use within the current translation unit.
std::optional< DirectoryName > getUmbrellaDirAsWritten() const
Retrieve the umbrella directory as written.
unsigned HasIncompatibleModuleFile
Whether we tried and failed to load a module file for this module.
void dump() const
Dump the contents of this module to the given output stream.
Module * ShadowingModule
A module with the same name that shadows this module.
unsigned IsFramework
Whether this is a framework module.
std::string ExportAsModule
The module through which entities defined in this module will eventually be exposed,...
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
void addTopHeader(FileEntryRef File)
Add a top-level header associated with this module.
unsigned IsAvailable
Whether this module is available in the current translation unit.
unsigned InferExplicitSubmodules
Whether, when inferring submodules, the inferred submodules should be explicit.
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
OptionalDirectoryEntryRef getEffectiveUmbrellaDir() const
Get the effective umbrella directory for this module: either the one explicitly written in the module...
std::vector< Conflict > Conflicts
The list of conflicts.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
Exposes information about the current target.
void setVisible(Module *M, SourceLocation Loc, bool IncludeExports=true, VisibleCallback Vis=[](Module *) {}, ConflictCallback Cb=[](ArrayRef< Module * >, Module *, StringRef) {})
Make a specific module visible.
llvm::function_ref< void(Module *M)> VisibleCallback
A callback to call when a module is made visible (directly or indirectly) by a call to setVisible.
llvm::function_ref< void(ArrayRef< Module * > Path, Module *Conflict, StringRef Message)> ConflictCallback
A callback to call when a module conflict is found.
bool isVisible(const Module *M) const
Determine whether a module is visible.
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
LLVM_READONLY bool isValidAsciiIdentifier(StringRef S, bool AllowDollar=false)
Return true if this is a valid ASCII identifier.
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
@ Result
The result type of a method or function.
CustomizableOptional< DirectoryEntryRef > OptionalDirectoryEntryRef
@ Other
Other implicit parameter.
int const char * function