clang 18.0.0git
Module.cpp
Go to the documentation of this file.
1//===- Module.cpp - Describe a module -------------------------------------===//
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 Module class, which describes a module in the source
10// code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Module.h"
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"
28#include <algorithm>
29#include <cassert>
30#include <functional>
31#include <string>
32#include <utility>
33#include <vector>
34
35using namespace clang;
36
37Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
38 bool IsFramework, bool IsExplicit, unsigned VisibilityID)
39 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
40 VisibilityID(VisibilityID), IsUnimportable(false),
41 HasIncompatibleModuleFile(false), IsAvailable(true),
42 IsFromModuleFile(false), IsFramework(IsFramework), IsExplicit(IsExplicit),
43 IsSystem(false), IsExternC(false), IsInferred(false),
44 InferSubmodules(false), InferExplicitSubmodules(false),
45 InferExportWildcard(false), ConfigMacrosExhaustive(false),
46 NoUndeclaredIncludes(false), ModuleMapIsPrivate(false),
47 NamedModuleHasInit(true), NameVisibility(Hidden) {
48 if (Parent) {
55
56 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
57 Parent->SubModules.push_back(this);
58 }
59}
60
62 for (auto *Submodule : SubModules) {
63 delete Submodule;
64 }
65}
66
67static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
68 StringRef Platform = Target.getPlatformName();
69 StringRef Env = Target.getTriple().getEnvironmentName();
70
71 // Attempt to match platform and environment.
72 if (Platform == Feature || Target.getTriple().getOSName() == Feature ||
73 Env == Feature)
74 return true;
75
76 auto CmpPlatformEnv = [](StringRef LHS, StringRef RHS) {
77 auto Pos = LHS.find('-');
78 if (Pos == StringRef::npos)
79 return false;
80 SmallString<128> NewLHS = LHS.slice(0, Pos);
81 NewLHS += LHS.slice(Pos+1, LHS.size());
82 return NewLHS == RHS;
83 };
84
85 SmallString<128> PlatformEnv = Target.getTriple().getOSAndEnvironmentName();
86 // Darwin has different but equivalent variants for simulators, example:
87 // 1. x86_64-apple-ios-simulator
88 // 2. x86_64-apple-iossimulator
89 // where both are valid examples of the same platform+environment but in the
90 // variant (2) the simulator is hardcoded as part of the platform name. Both
91 // forms above should match for "iossimulator" requirement.
92 if (Target.getTriple().isOSDarwin() && PlatformEnv.endswith("simulator"))
93 return PlatformEnv == Feature || CmpPlatformEnv(PlatformEnv, Feature);
94
95 return PlatformEnv == Feature;
96}
97
98/// Determine whether a translation unit built using the current
99/// language options has the given feature.
100static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
101 const TargetInfo &Target) {
102 bool HasFeature = llvm::StringSwitch<bool>(Feature)
103 .Case("altivec", LangOpts.AltiVec)
104 .Case("blocks", LangOpts.Blocks)
105 .Case("coroutines", LangOpts.Coroutines)
106 .Case("cplusplus", LangOpts.CPlusPlus)
107 .Case("cplusplus11", LangOpts.CPlusPlus11)
108 .Case("cplusplus14", LangOpts.CPlusPlus14)
109 .Case("cplusplus17", LangOpts.CPlusPlus17)
110 .Case("cplusplus20", LangOpts.CPlusPlus20)
111 .Case("cplusplus23", LangOpts.CPlusPlus23)
112 .Case("cplusplus26", LangOpts.CPlusPlus26)
113 .Case("c99", LangOpts.C99)
114 .Case("c11", LangOpts.C11)
115 .Case("c17", LangOpts.C17)
116 .Case("c23", LangOpts.C23)
117 .Case("freestanding", LangOpts.Freestanding)
118 .Case("gnuinlineasm", LangOpts.GNUAsm)
119 .Case("objc", LangOpts.ObjC)
120 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
121 .Case("opencl", LangOpts.OpenCL)
122 .Case("tls", Target.isTLSSupported())
123 .Case("zvector", LangOpts.ZVector)
124 .Default(Target.hasFeature(Feature) ||
125 isPlatformEnvironment(Target, Feature));
126 if (!HasFeature)
127 HasFeature = llvm::is_contained(LangOpts.ModuleFeatures, Feature);
128 return HasFeature;
129}
130
132 const TargetInfo &Target, Requirement &Req,
133 Module *&ShadowingModule) const {
134 if (!IsUnimportable)
135 return false;
136
137 for (const Module *Current = this; Current; Current = Current->Parent) {
138 if (Current->ShadowingModule) {
139 ShadowingModule = Current->ShadowingModule;
140 return true;
141 }
142 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
143 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
144 Current->Requirements[I].second) {
145 Req = Current->Requirements[I];
146 return true;
147 }
148 }
149 }
150
151 llvm_unreachable("could not find a reason why module is unimportable");
152}
153
154// The -fmodule-name option tells the compiler to textually include headers in
155// the specified module, meaning Clang won't build the specified module. This
156// is useful in a number of situations, for instance, when building a library
157// that vends a module map, one might want to avoid hitting intermediate build
158// products containing the module map or avoid finding the system installed
159// modulemap for that library.
160bool Module::isForBuilding(const LangOptions &LangOpts) const {
161 StringRef TopLevelName = getTopLevelModuleName();
162 StringRef CurrentModule = LangOpts.CurrentModule;
163
164 // When building the implementation of framework Foo, we want to make sure
165 // that Foo *and* Foo_Private are textually included and no modules are built
166 // for either.
167 if (!LangOpts.isCompilingModule() && getTopLevelModule()->IsFramework &&
168 CurrentModule == LangOpts.ModuleName &&
169 !CurrentModule.endswith("_Private") && TopLevelName.endswith("_Private"))
170 TopLevelName = TopLevelName.drop_back(8);
171
172 return TopLevelName == CurrentModule;
173}
174
176 Requirement &Req,
177 UnresolvedHeaderDirective &MissingHeader,
178 Module *&ShadowingModule) const {
179 if (IsAvailable)
180 return true;
181
182 if (isUnimportable(LangOpts, Target, Req, ShadowingModule))
183 return false;
184
185 // FIXME: All missing headers are listed on the top-level module. Should we
186 // just look there?
187 for (const Module *Current = this; Current; Current = Current->Parent) {
188 if (!Current->MissingHeaders.empty()) {
189 MissingHeader = Current->MissingHeaders.front();
190 return false;
191 }
192 }
193
194 llvm_unreachable("could not find a reason why module is unavailable");
195}
196
198 for (auto *Parent = this; Parent; Parent = Parent->Parent) {
199 if (Parent == Other)
200 return true;
201 }
202 return false;
203}
204
206 const Module *Result = this;
207 while (Result->Parent)
208 Result = Result->Parent;
209
210 return Result;
211}
212
214 const std::pair<std::string, SourceLocation> &IdComponent) {
215 return IdComponent.first;
216}
217
218static StringRef getModuleNameFromComponent(StringRef R) { return R; }
219
220template<typename InputIter>
221static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
222 bool AllowStringLiterals = true) {
223 for (InputIter It = Begin; It != End; ++It) {
224 if (It != Begin)
225 OS << ".";
226
227 StringRef Name = getModuleNameFromComponent(*It);
228 if (!AllowStringLiterals || isValidAsciiIdentifier(Name))
229 OS << Name;
230 else {
231 OS << '"';
232 OS.write_escaped(Name);
233 OS << '"';
234 }
235 }
236}
237
238template<typename Container>
239static void printModuleId(raw_ostream &OS, const Container &C) {
240 return printModuleId(OS, C.begin(), C.end());
241}
242
243std::string Module::getFullModuleName(bool AllowStringLiterals) const {
245
246 // Build up the set of module names (from innermost to outermost).
247 for (const Module *M = this; M; M = M->Parent)
248 Names.push_back(M->Name);
249
250 std::string Result;
251
252 llvm::raw_string_ostream Out(Result);
253 printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
254 Out.flush();
255
256 return Result;
257}
258
260 for (const Module *M = this; M; M = M->Parent) {
261 if (nameParts.empty() || M->Name != nameParts.back())
262 return false;
263 nameParts = nameParts.drop_back();
264 }
265 return nameParts.empty();
266}
267
269 if (const auto *Hdr = std::get_if<FileEntryRef>(&Umbrella))
270 return Hdr->getDir();
271 if (const auto *Dir = std::get_if<DirectoryEntryRef>(&Umbrella))
272 return *Dir;
273 return std::nullopt;
274}
275
277 assert(File);
278 TopHeaders.insert(File);
279}
280
282 if (!TopHeaderNames.empty()) {
283 for (StringRef TopHeaderName : TopHeaderNames)
284 if (auto FE = FileMgr.getOptionalFileRef(TopHeaderName))
285 TopHeaders.insert(*FE);
286 TopHeaderNames.clear();
287 }
288
289 return llvm::ArrayRef(TopHeaders.begin(), TopHeaders.end());
290}
291
292bool Module::directlyUses(const Module *Requested) {
293 auto *Top = getTopLevelModule();
294
295 // A top-level module implicitly uses itself.
296 if (Requested->isSubModuleOf(Top))
297 return true;
298
299 for (auto *Use : Top->DirectUses)
300 if (Requested->isSubModuleOf(Use))
301 return true;
302
303 // Anyone is allowed to use our builtin stdarg.h and stddef.h and their
304 // accompanying modules.
305 if (Requested->getTopLevelModuleName() == "_Builtin_stdarg" ||
306 Requested->getTopLevelModuleName() == "_Builtin_stddef")
307 return true;
308
310 UndeclaredUses.insert(Requested);
311
312 return false;
313}
314
315void Module::addRequirement(StringRef Feature, bool RequiredState,
316 const LangOptions &LangOpts,
317 const TargetInfo &Target) {
318 Requirements.push_back(Requirement(std::string(Feature), RequiredState));
319
320 // If this feature is currently available, we're done.
321 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
322 return;
323
324 markUnavailable(/*Unimportable*/true);
325}
326
327void Module::markUnavailable(bool Unimportable) {
328 auto needUpdate = [Unimportable](Module *M) {
329 return M->IsAvailable || (!M->IsUnimportable && Unimportable);
330 };
331
332 if (!needUpdate(this))
333 return;
334
336 Stack.push_back(this);
337 while (!Stack.empty()) {
338 Module *Current = Stack.back();
339 Stack.pop_back();
340
341 if (!needUpdate(Current))
342 continue;
343
344 Current->IsAvailable = false;
345 Current->IsUnimportable |= Unimportable;
346 for (auto *Submodule : Current->submodules()) {
347 if (needUpdate(Submodule))
348 Stack.push_back(Submodule);
349 }
350 }
351}
352
353Module *Module::findSubmodule(StringRef Name) const {
354 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
355 if (Pos == SubModuleIndex.end())
356 return nullptr;
357
358 return SubModules[Pos->getValue()];
359}
360
362 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
363 if (Pos != SubModuleIndex.end())
364 return SubModules[Pos->getValue()];
365 if (!InferSubmodules)
366 return nullptr;
368 Result->InferExplicitSubmodules = InferExplicitSubmodules;
369 Result->InferSubmodules = InferSubmodules;
370 Result->InferExportWildcard = InferExportWildcard;
371 if (Result->InferExportWildcard)
372 Result->Exports.push_back(Module::ExportDecl(nullptr, true));
373 return Result;
374}
375
377 assert(isNamedModuleUnit() && "We should only query the global module "
378 "fragment from the C++ 20 Named modules");
379
380 for (auto *SubModule : SubModules)
381 if (SubModule->isExplicitGlobalModule())
382 return SubModule;
383
384 return nullptr;
385}
386
388 assert(isNamedModuleUnit() && "We should only query the private module "
389 "fragment from the C++ 20 Named modules");
390
391 for (auto *SubModule : SubModules)
392 if (SubModule->isPrivateModule())
393 return SubModule;
394
395 return nullptr;
396}
397
399 // All non-explicit submodules are exported.
400 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
401 E = SubModules.end();
402 I != E; ++I) {
403 Module *Mod = *I;
404 if (!Mod->IsExplicit)
405 Exported.push_back(Mod);
406 }
407
408 // Find re-exported modules by filtering the list of imported modules.
409 bool AnyWildcard = false;
410 bool UnrestrictedWildcard = false;
411 SmallVector<Module *, 4> WildcardRestrictions;
412 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
413 Module *Mod = Exports[I].getPointer();
414 if (!Exports[I].getInt()) {
415 // Export a named module directly; no wildcards involved.
416 Exported.push_back(Mod);
417
418 continue;
419 }
420
421 // Wildcard export: export all of the imported modules that match
422 // the given pattern.
423 AnyWildcard = true;
424 if (UnrestrictedWildcard)
425 continue;
426
427 if (Module *Restriction = Exports[I].getPointer())
428 WildcardRestrictions.push_back(Restriction);
429 else {
430 WildcardRestrictions.clear();
431 UnrestrictedWildcard = true;
432 }
433 }
434
435 // If there were any wildcards, push any imported modules that were
436 // re-exported by the wildcard restriction.
437 if (!AnyWildcard)
438 return;
439
440 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
441 Module *Mod = Imports[I];
442 bool Acceptable = UnrestrictedWildcard;
443 if (!Acceptable) {
444 // Check whether this module meets one of the restrictions.
445 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
446 Module *Restriction = WildcardRestrictions[R];
447 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
448 Acceptable = true;
449 break;
450 }
451 }
452 }
453
454 if (!Acceptable)
455 continue;
456
457 Exported.push_back(Mod);
458 }
459}
460
461void Module::buildVisibleModulesCache() const {
462 assert(VisibleModulesCache.empty() && "cache does not need building");
463
464 // This module is visible to itself.
465 VisibleModulesCache.insert(this);
466
467 // Every imported module is visible.
468 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
469 while (!Stack.empty()) {
470 Module *CurrModule = Stack.pop_back_val();
471
472 // Every module transitively exported by an imported module is visible.
473 if (VisibleModulesCache.insert(CurrModule).second)
474 CurrModule->getExportedModules(Stack);
475 }
476}
477
478void Module::print(raw_ostream &OS, unsigned Indent, bool Dump) const {
479 OS.indent(Indent);
480 if (IsFramework)
481 OS << "framework ";
482 if (IsExplicit)
483 OS << "explicit ";
484 OS << "module ";
485 printModuleId(OS, &Name, &Name + 1);
486
487 if (IsSystem || IsExternC) {
488 OS.indent(Indent + 2);
489 if (IsSystem)
490 OS << " [system]";
491 if (IsExternC)
492 OS << " [extern_c]";
493 }
494
495 OS << " {\n";
496
497 if (!Requirements.empty()) {
498 OS.indent(Indent + 2);
499 OS << "requires ";
500 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
501 if (I)
502 OS << ", ";
503 if (!Requirements[I].second)
504 OS << "!";
505 OS << Requirements[I].first;
506 }
507 OS << "\n";
508 }
509
510 if (std::optional<Header> H = getUmbrellaHeaderAsWritten()) {
511 OS.indent(Indent + 2);
512 OS << "umbrella header \"";
513 OS.write_escaped(H->NameAsWritten);
514 OS << "\"\n";
515 } else if (std::optional<DirectoryName> D = getUmbrellaDirAsWritten()) {
516 OS.indent(Indent + 2);
517 OS << "umbrella \"";
518 OS.write_escaped(D->NameAsWritten);
519 OS << "\"\n";
520 }
521
522 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
523 OS.indent(Indent + 2);
524 OS << "config_macros ";
526 OS << "[exhaustive]";
527 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
528 if (I)
529 OS << ", ";
530 OS << ConfigMacros[I];
531 }
532 OS << "\n";
533 }
534
535 struct {
536 StringRef Prefix;
538 } Kinds[] = {{"", HK_Normal},
539 {"textual ", HK_Textual},
540 {"private ", HK_Private},
541 {"private textual ", HK_PrivateTextual},
542 {"exclude ", HK_Excluded}};
543
544 for (auto &K : Kinds) {
545 assert(&K == &Kinds[K.Kind] && "kinds in wrong order");
546 for (auto &H : Headers[K.Kind]) {
547 OS.indent(Indent + 2);
548 OS << K.Prefix << "header \"";
549 OS.write_escaped(H.NameAsWritten);
550 OS << "\" { size " << H.Entry.getSize()
551 << " mtime " << H.Entry.getModificationTime() << " }\n";
552 }
553 }
554 for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
555 for (auto &U : *Unresolved) {
556 OS.indent(Indent + 2);
557 OS << Kinds[U.Kind].Prefix << "header \"";
558 OS.write_escaped(U.FileName);
559 OS << "\"";
560 if (U.Size || U.ModTime) {
561 OS << " {";
562 if (U.Size)
563 OS << " size " << *U.Size;
564 if (U.ModTime)
565 OS << " mtime " << *U.ModTime;
566 OS << " }";
567 }
568 OS << "\n";
569 }
570 }
571
572 if (!ExportAsModule.empty()) {
573 OS.indent(Indent + 2);
574 OS << "export_as" << ExportAsModule << "\n";
575 }
576
577 for (auto *Submodule : submodules())
578 // Print inferred subframework modules so that we don't need to re-infer
579 // them (requires expensive directory iteration + stat calls) when we build
580 // the module. Regular inferred submodules are OK, as we need to look at all
581 // those header files anyway.
582 if (!Submodule->IsInferred || Submodule->IsFramework)
583 Submodule->print(OS, Indent + 2, Dump);
584
585 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
586 OS.indent(Indent + 2);
587 OS << "export ";
588 if (Module *Restriction = Exports[I].getPointer()) {
589 OS << Restriction->getFullModuleName(true);
590 if (Exports[I].getInt())
591 OS << ".*";
592 } else {
593 OS << "*";
594 }
595 OS << "\n";
596 }
597
598 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
599 OS.indent(Indent + 2);
600 OS << "export ";
602 if (UnresolvedExports[I].Wildcard)
603 OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*");
604 OS << "\n";
605 }
606
607 if (Dump) {
608 for (Module *M : Imports) {
609 OS.indent(Indent + 2);
610 llvm::errs() << "import " << M->getFullModuleName() << "\n";
611 }
612 }
613
614 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
615 OS.indent(Indent + 2);
616 OS << "use ";
617 OS << DirectUses[I]->getFullModuleName(true);
618 OS << "\n";
619 }
620
621 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
622 OS.indent(Indent + 2);
623 OS << "use ";
625 OS << "\n";
626 }
627
628 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
629 OS.indent(Indent + 2);
630 OS << "link ";
632 OS << "framework ";
633 OS << "\"";
634 OS.write_escaped(LinkLibraries[I].Library);
635 OS << "\"";
636 }
637
638 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
639 OS.indent(Indent + 2);
640 OS << "conflict ";
642 OS << ", \"";
643 OS.write_escaped(UnresolvedConflicts[I].Message);
644 OS << "\"\n";
645 }
646
647 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
648 OS.indent(Indent + 2);
649 OS << "conflict ";
650 OS << Conflicts[I].Other->getFullModuleName(true);
651 OS << ", \"";
652 OS.write_escaped(Conflicts[I].Message);
653 OS << "\"\n";
654 }
655
656 if (InferSubmodules) {
657 OS.indent(Indent + 2);
659 OS << "explicit ";
660 OS << "module * {\n";
662 OS.indent(Indent + 4);
663 OS << "export *\n";
664 }
665 OS.indent(Indent + 2);
666 OS << "}\n";
667 }
668
669 OS.indent(Indent);
670 OS << "}\n";
671}
672
673LLVM_DUMP_METHOD void Module::dump() const {
674 print(llvm::errs(), 0, true);
675}
676
679 // We can't import a global module fragment so the location can be invalid.
680 assert((M->isGlobalModule() || Loc.isValid()) &&
681 "setVisible expects a valid import location");
682 if (isVisible(M))
683 return;
684
685 ++Generation;
686
687 struct Visiting {
688 Module *M;
689 Visiting *ExportedBy;
690 };
691
692 std::function<void(Visiting)> VisitModule = [&](Visiting V) {
693 // Nothing to do for a module that's already visible.
694 unsigned ID = V.M->getVisibilityID();
695 if (ImportLocs.size() <= ID)
696 ImportLocs.resize(ID + 1);
697 else if (ImportLocs[ID].isValid())
698 return;
699
700 ImportLocs[ID] = Loc;
701 Vis(V.M);
702
703 // Make any exported modules visible.
705 V.M->getExportedModules(Exports);
706 for (Module *E : Exports) {
707 // Don't import non-importable modules.
708 if (!E->isUnimportable())
709 VisitModule({E, &V});
710 }
711
712 for (auto &C : V.M->Conflicts) {
713 if (isVisible(C.Other)) {
715 for (Visiting *I = &V; I; I = I->ExportedBy)
716 Path.push_back(I->M);
717 Cb(Path, C.Other, C.Message);
718 }
719 }
720 };
721 VisitModule({M, nullptr});
722}
723
725 SourceLocation Loc,
726 VisibleCallback Vis,
727 ConflictCallback Cb) {
728 for (auto *I : M->Imports)
729 setVisible(I, Loc, Vis, Cb);
730}
731
733 : Signature(M.Signature), ClangModule(&M) {
734 if (M.Directory)
735 Path = M.Directory->getName();
736 if (auto File = M.getASTFile())
737 ASTFile = File->getName();
738}
739
741 if (ClangModule)
742 return ClangModule->Name;
743 else
744 return std::string(PCHModuleName);
745}
#define V(N, I)
Definition: ASTContext.h:3241
NodeId Parent
Definition: ASTDiff.cpp:191
int Id
Definition: ASTDiff.cpp:190
Defines the clang::FileManager interface and associated types.
const Environment & Env
Definition: HTMLLogger.cpp:144
Defines the clang::LangOptions interface.
static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature)
Definition: Module.cpp:67
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.
Definition: Module.cpp:100
static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End, bool AllowStringLiterals=true)
Definition: Module.cpp:221
static StringRef getModuleNameFromComponent(const std::pair< std::string, SourceLocation > &IdComponent)
Definition: Module.cpp:213
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.
SourceLocation Begin
std::string getModuleName() const
Definition: Module.cpp:740
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Get a FileEntryRef if it exists, without doing anything on error.
Definition: FileManager.h:234
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:83
std::string ModuleName
The module currently being compiled as specified by -fmodule-name.
Definition: LangOptions.h:441
bool isCompilingModule() const
Are we compiling a module?
Definition: LangOptions.h:534
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:448
std::vector< std::string > ModuleFeatures
The names of any features to enable in module 'requires' decls in addition to the hard-coded list in ...
Definition: LangOptions.h:454
Describes a module or submodule.
Definition: Module.h:105
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition: Module.h:670
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.
Definition: Module.cpp:315
unsigned IsExplicit
Whether this is an explicit submodule.
Definition: Module.h:327
SmallVector< ExportDecl, 2 > Exports
The set of export declarations.
Definition: Module.h:414
bool isForBuilding(const LangOptions &LangOpts) const
Determine whether this module can be built in this compilation.
Definition: Module.cpp:160
std::variant< std::monostate, FileEntryRef, DirectoryEntryRef > Umbrella
The umbrella header or directory.
Definition: Module.h:166
Module * findOrInferSubmodule(StringRef Name)
Definition: Module.cpp:361
unsigned InferSubmodules
Whether we should infer submodules for this module based on the headers.
Definition: Module.h:349
Module * findSubmodule(StringRef Name) const
Find the submodule with the given name.
Definition: Module.cpp:353
OptionalDirectoryEntryRefDegradesToDirectoryEntryPtr Directory
The build directory of this module.
Definition: Module.h:159
bool directlyUses(const Module *Requested)
Determine whether this module has declared its intention to directly use another module.
Definition: Module.cpp:292
std::vector< std::string > ConfigMacros
The set of "configuration macros", which are macros that (intentionally) change how this module is bu...
Definition: Module.h:471
unsigned IsUnimportable
Whether this module has declared itself unimportable, either because it's missing a requirement from ...
Definition: Module.h:304
void print(raw_ostream &OS, unsigned Indent=0, bool Dump=false) const
Print the module map for this module to the given stream.
Definition: Module.cpp:478
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
Definition: Module.h:614
SmallVector< UnresolvedHeaderDirective, 1 > MissingHeaders
Headers that are mentioned in the module map file but could not be found on the file system.
Definition: Module.h:285
Module * Parent
The parent of this module.
Definition: Module.h:154
void markUnavailable(bool Unimportable)
Mark this module and all of its submodules as unavailable.
Definition: Module.cpp:327
SmallVector< UnresolvedHeaderDirective, 1 > UnresolvedHeaders
Headers that are mentioned in the module map file but that we have not yet attempted to resolve to a ...
Definition: Module.h:281
ModuleKind Kind
The kind of this module.
Definition: Module.h:150
@ HK_PrivateTextual
Definition: Module.h:243
bool isUnimportable() const
Determine whether this module has been declared unimportable.
Definition: Module.h:505
bool fullModuleNameIs(ArrayRef< StringRef > nameParts) const
Whether the full name of this module is equal to joining nameParts with "."s.
Definition: Module.cpp:259
Module * getPrivateModuleFragment() const
Get the Private Module Fragment (sub-module) for this module, it there is one.
Definition: Module.cpp:387
SmallVector< Header, 2 > Headers[5]
The headers that are part of this module.
Definition: Module.h:265
llvm::SmallSetVector< Module *, 2 > Imports
The set of modules imported by this module, and on which this module depends.
Definition: Module.h:401
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers).
Definition: Module.h:332
std::string Name
The name of this module.
Definition: Module.h:108
Module * getGlobalModuleFragment() const
Get the Global Module Fragment (sub-module) for this module, it there is one.
Definition: Module.cpp:376
llvm::iterator_range< submodule_iterator > submodules()
Definition: Module.h:777
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "C"...
Definition: Module.h:338
unsigned ModuleMapIsPrivate
Whether this module came from a "private" module map, found next to a regular (public) module map.
Definition: Module.h:377
llvm::SmallVector< LinkLibrary, 2 > LinkLibraries
The set of libraries or frameworks to link against when an entity from this module is used.
Definition: Module.h:463
SmallVector< UnresolvedExportDecl, 2 > UnresolvedExports
The set of export declarations that have yet to be resolved.
Definition: Module.h:432
std::optional< Header > getUmbrellaHeaderAsWritten() const
Retrieve the umbrella header as written.
Definition: Module.h:694
SmallVector< Requirement, 2 > Requirements
The set of language features required to use this module.
Definition: Module.h:295
llvm::SmallSetVector< const Module *, 2 > UndeclaredUses
When NoUndeclaredIncludes is true, the set of modules this module tried to import but didn't because ...
Definition: Module.h:442
SmallVector< ModuleId, 2 > UnresolvedDirectUses
The set of use declarations that have yet to be resolved.
Definition: Module.h:438
unsigned NoUndeclaredIncludes
Whether files in this module can only include non-modular headers and headers from used modules.
Definition: Module.h:372
SmallVector< Module *, 2 > DirectUses
The directly used modules.
Definition: Module.h:435
unsigned ConfigMacrosExhaustive
Whether the set of configuration macros is exhaustive.
Definition: Module.h:367
bool isGlobalModule() const
Does this Module scope describe a fragment of the global module within some C++ module.
Definition: Module.h:200
unsigned InferExportWildcard
Whether, when inferring submodules, the inferr submodules should export all modules they import (e....
Definition: Module.h:359
void getExportedModules(SmallVectorImpl< Module * > &Exported) const
Appends this module's list of exported modules to Exported.
Definition: Module.cpp:398
std::pair< std::string, bool > Requirement
An individual requirement: a feature name and a flag indicating the required state of that feature.
Definition: Module.h:289
std::vector< UnresolvedConflict > UnresolvedConflicts
The list of conflicts for which the module-id has not yet been resolved.
Definition: Module.h:484
bool isSubModuleOf(const Module *Other) const
Check if this module is a (possibly transitive) submodule of Other.
Definition: Module.cpp:197
ArrayRef< FileEntryRef > getTopHeaders(FileManager &FileMgr)
The top-level headers associated with this module.
Definition: Module.cpp:281
OptionalFileEntryRefDegradesToFileEntryPtr getASTFile() const
The serialized AST file for this module, if one was created.
Definition: Module.h:675
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
std::optional< DirectoryName > getUmbrellaDirAsWritten() const
Retrieve the umbrella directory as written.
Definition: Module.h:686
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.
Definition: Module.h:298
unsigned IsFramework
Whether this is a framework module.
Definition: Module.h:323
std::string ExportAsModule
The module through which entities defined in this module will eventually be exposed,...
Definition: Module.h:179
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition: Module.cpp:243
void addTopHeader(FileEntryRef File)
Add a top-level header associated with this module.
Definition: Module.cpp:276
unsigned IsAvailable
Whether this module is available in the current translation unit.
Definition: Module.h:315
unsigned InferExplicitSubmodules
Whether, when inferring submodules, the inferred submodules should be explicit.
Definition: Module.h:354
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition: Module.h:660
OptionalDirectoryEntryRef getEffectiveUmbrellaDir() const
Get the effective umbrella directory for this module: either the one explicitly written in the module...
Definition: Module.cpp:268
std::vector< Conflict > Conflicts
The list of conflicts.
Definition: Module.h:496
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
Exposes information about the current target.
Definition: TargetInfo.h:212
void makeTransitiveImportsVisible(Module *M, SourceLocation Loc, VisibleCallback Vis=[](Module *) {}, ConflictCallback Cb=[](ArrayRef< Module * >, Module *, StringRef) {})
Make transitive imports visible for [module.import]/7.
Definition: Module.cpp:724
llvm::function_ref< void(ArrayRef< Module * > Path, Module *Conflict, StringRef Message)> ConflictCallback
A callback to call when a module conflict is found.
Definition: Module.h:849
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.
Definition: Module.h:842
bool isVisible(const Module *M) const
Determine whether a module is visible.
Definition: Module.h:829
void setVisible(Module *M, SourceLocation Loc, VisibleCallback Vis=[](Module *) {}, ConflictCallback Cb=[](ArrayRef< Module * >, Module *, StringRef) {})
Make a specific module visible.
Definition: Module.cpp:677
Defines the clang::TargetInfo interface.
LLVM_READONLY bool isValidAsciiIdentifier(StringRef S, bool AllowDollar=false)
Return true if this is a valid ASCII identifier.
Definition: CharInfo.h:228
@ Result
The result type of a method or function.
@ Other
Other implicit parameter.
#define true
Definition: stdbool.h:21
#define false
Definition: stdbool.h:22
Stored information about a header directive that was found in the module map file but has not been re...
Definition: Module.h:269