1 //===- Module.cpp - Describe a module -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Module class, which describes a module in the source
13 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/Module.h"
16 #include "clang/Basic/CharInfo.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/StringSwitch.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
36 using namespace clang;
38 Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
39 bool IsFramework, bool IsExplicit, unsigned VisibilityID)
40 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
41 VisibilityID(VisibilityID), IsMissingRequirement(false),
42 HasIncompatibleModuleFile(false), IsAvailable(true),
43 IsFromModuleFile(false), IsFramework(IsFramework), IsExplicit(IsExplicit),
44 IsSystem(false), IsExternC(false), IsInferred(false),
45 InferSubmodules(false), InferExplicitSubmodules(false),
46 InferExportWildcard(false), ConfigMacrosExhaustive(false),
47 NoUndeclaredIncludes(false), ModuleMapIsPrivate(false),
48 NameVisibility(Hidden) {
50 if (!Parent->isAvailable())
54 if (Parent->IsExternC)
56 if (Parent->NoUndeclaredIncludes)
57 NoUndeclaredIncludes = true;
58 if (Parent->ModuleMapIsPrivate)
59 ModuleMapIsPrivate = true;
60 IsMissingRequirement = Parent->IsMissingRequirement;
62 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
63 Parent->SubModules.push_back(this);
68 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
74 /// Determine whether a translation unit built using the current
75 /// language options has the given feature.
76 static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
77 const TargetInfo &Target) {
78 bool HasFeature = llvm::StringSwitch<bool>(Feature)
79 .Case("altivec", LangOpts.AltiVec)
80 .Case("blocks", LangOpts.Blocks)
81 .Case("coroutines", LangOpts.CoroutinesTS)
82 .Case("cplusplus", LangOpts.CPlusPlus)
83 .Case("cplusplus11", LangOpts.CPlusPlus11)
84 .Case("cplusplus14", LangOpts.CPlusPlus14)
85 .Case("cplusplus17", LangOpts.CPlusPlus17)
86 .Case("c99", LangOpts.C99)
87 .Case("c11", LangOpts.C11)
88 .Case("c17", LangOpts.C17)
89 .Case("freestanding", LangOpts.Freestanding)
90 .Case("gnuinlineasm", LangOpts.GNUAsm)
91 .Case("objc", LangOpts.ObjC1)
92 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
93 .Case("opencl", LangOpts.OpenCL)
94 .Case("tls", Target.isTLSSupported())
95 .Case("zvector", LangOpts.ZVector)
96 .Default(Target.hasFeature(Feature));
98 HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
99 LangOpts.ModuleFeatures.end(),
100 Feature) != LangOpts.ModuleFeatures.end();
104 bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
106 UnresolvedHeaderDirective &MissingHeader,
107 Module *&ShadowingModule) const {
111 for (const Module *Current = this; Current; Current = Current->Parent) {
112 if (Current->ShadowingModule) {
113 ShadowingModule = Current->ShadowingModule;
116 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
117 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
118 Current->Requirements[I].second) {
119 Req = Current->Requirements[I];
123 if (!Current->MissingHeaders.empty()) {
124 MissingHeader = Current->MissingHeaders.front();
129 llvm_unreachable("could not find a reason why module is unavailable");
132 bool Module::isSubModuleOf(const Module *Other) const {
133 const Module *This = this;
144 const Module *Module::getTopLevelModule() const {
145 const Module *Result = this;
146 while (Result->Parent)
147 Result = Result->Parent;
152 static StringRef getModuleNameFromComponent(
153 const std::pair<std::string, SourceLocation> &IdComponent) {
154 return IdComponent.first;
157 static StringRef getModuleNameFromComponent(StringRef R) { return R; }
159 template<typename InputIter>
160 static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
161 bool AllowStringLiterals = true) {
162 for (InputIter It = Begin; It != End; ++It) {
166 StringRef Name = getModuleNameFromComponent(*It);
167 if (!AllowStringLiterals || isValidIdentifier(Name))
171 OS.write_escaped(Name);
177 template<typename Container>
178 static void printModuleId(raw_ostream &OS, const Container &C) {
179 return printModuleId(OS, C.begin(), C.end());
182 std::string Module::getFullModuleName(bool AllowStringLiterals) const {
183 SmallVector<StringRef, 2> Names;
185 // Build up the set of module names (from innermost to outermost).
186 for (const Module *M = this; M; M = M->Parent)
187 Names.push_back(M->Name);
191 llvm::raw_string_ostream Out(Result);
192 printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
198 bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
199 for (const Module *M = this; M; M = M->Parent) {
200 if (nameParts.empty() || M->Name != nameParts.back())
202 nameParts = nameParts.drop_back();
204 return nameParts.empty();
207 Module::DirectoryName Module::getUmbrellaDir() const {
208 if (Header U = getUmbrellaHeader())
209 return {"", U.Entry->getDir()};
211 return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()};
214 ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
215 if (!TopHeaderNames.empty()) {
216 for (std::vector<std::string>::iterator
217 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
218 if (const FileEntry *FE = FileMgr.getFile(*I))
219 TopHeaders.insert(FE);
221 TopHeaderNames.clear();
224 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
227 bool Module::directlyUses(const Module *Requested) const {
228 auto *Top = getTopLevelModule();
230 // A top-level module implicitly uses itself.
231 if (Requested->isSubModuleOf(Top))
234 for (auto *Use : Top->DirectUses)
235 if (Requested->isSubModuleOf(Use))
238 // Anyone is allowed to use our builtin stddef.h and its accompanying module.
239 if (!Requested->Parent && Requested->Name == "_Builtin_stddef_max_align_t")
245 void Module::addRequirement(StringRef Feature, bool RequiredState,
246 const LangOptions &LangOpts,
247 const TargetInfo &Target) {
248 Requirements.push_back(Requirement(Feature, RequiredState));
250 // If this feature is currently available, we're done.
251 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
254 markUnavailable(/*MissingRequirement*/true);
257 void Module::markUnavailable(bool MissingRequirement) {
258 auto needUpdate = [MissingRequirement](Module *M) {
259 return M->IsAvailable || (!M->IsMissingRequirement && MissingRequirement);
262 if (!needUpdate(this))
265 SmallVector<Module *, 2> Stack;
266 Stack.push_back(this);
267 while (!Stack.empty()) {
268 Module *Current = Stack.back();
271 if (!needUpdate(Current))
274 Current->IsAvailable = false;
275 Current->IsMissingRequirement |= MissingRequirement;
276 for (submodule_iterator Sub = Current->submodule_begin(),
277 SubEnd = Current->submodule_end();
278 Sub != SubEnd; ++Sub) {
279 if (needUpdate(*Sub))
280 Stack.push_back(*Sub);
285 Module *Module::findSubmodule(StringRef Name) const {
286 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
287 if (Pos == SubModuleIndex.end())
290 return SubModules[Pos->getValue()];
293 void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
294 // All non-explicit submodules are exported.
295 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
296 E = SubModules.end();
299 if (!Mod->IsExplicit)
300 Exported.push_back(Mod);
303 // Find re-exported modules by filtering the list of imported modules.
304 bool AnyWildcard = false;
305 bool UnrestrictedWildcard = false;
306 SmallVector<Module *, 4> WildcardRestrictions;
307 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
308 Module *Mod = Exports[I].getPointer();
309 if (!Exports[I].getInt()) {
310 // Export a named module directly; no wildcards involved.
311 Exported.push_back(Mod);
316 // Wildcard export: export all of the imported modules that match
317 // the given pattern.
319 if (UnrestrictedWildcard)
322 if (Module *Restriction = Exports[I].getPointer())
323 WildcardRestrictions.push_back(Restriction);
325 WildcardRestrictions.clear();
326 UnrestrictedWildcard = true;
330 // If there were any wildcards, push any imported modules that were
331 // re-exported by the wildcard restriction.
335 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
336 Module *Mod = Imports[I];
337 bool Acceptable = UnrestrictedWildcard;
339 // Check whether this module meets one of the restrictions.
340 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
341 Module *Restriction = WildcardRestrictions[R];
342 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
352 Exported.push_back(Mod);
356 void Module::buildVisibleModulesCache() const {
357 assert(VisibleModulesCache.empty() && "cache does not need building");
359 // This module is visible to itself.
360 VisibleModulesCache.insert(this);
362 // Every imported module is visible.
363 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
364 while (!Stack.empty()) {
365 Module *CurrModule = Stack.pop_back_val();
367 // Every module transitively exported by an imported module is visible.
368 if (VisibleModulesCache.insert(CurrModule).second)
369 CurrModule->getExportedModules(Stack);
373 void Module::print(raw_ostream &OS, unsigned Indent) const {
380 printModuleId(OS, &Name, &Name + 1);
382 if (IsSystem || IsExternC) {
383 OS.indent(Indent + 2);
392 if (!Requirements.empty()) {
393 OS.indent(Indent + 2);
395 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
398 if (!Requirements[I].second)
400 OS << Requirements[I].first;
405 if (Header H = getUmbrellaHeader()) {
406 OS.indent(Indent + 2);
407 OS << "umbrella header \"";
408 OS.write_escaped(H.NameAsWritten);
410 } else if (DirectoryName D = getUmbrellaDir()) {
411 OS.indent(Indent + 2);
413 OS.write_escaped(D.NameAsWritten);
417 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
418 OS.indent(Indent + 2);
419 OS << "config_macros ";
420 if (ConfigMacrosExhaustive)
421 OS << "[exhaustive]";
422 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
425 OS << ConfigMacros[I];
433 } Kinds[] = {{"", HK_Normal},
434 {"textual ", HK_Textual},
435 {"private ", HK_Private},
436 {"private textual ", HK_PrivateTextual},
437 {"exclude ", HK_Excluded}};
439 for (auto &K : Kinds) {
440 assert(&K == &Kinds[K.Kind] && "kinds in wrong order");
441 for (auto &H : Headers[K.Kind]) {
442 OS.indent(Indent + 2);
443 OS << K.Prefix << "header \"";
444 OS.write_escaped(H.NameAsWritten);
445 OS << "\" { size " << H.Entry->getSize()
446 << " mtime " << H.Entry->getModificationTime() << " }\n";
449 for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
450 for (auto &U : *Unresolved) {
451 OS.indent(Indent + 2);
452 OS << Kinds[U.Kind].Prefix << "header \"";
453 OS.write_escaped(U.FileName);
455 if (U.Size || U.ModTime) {
458 OS << " size " << *U.Size;
460 OS << " mtime " << *U.ModTime;
467 if (!ExportAsModule.empty()) {
468 OS.indent(Indent + 2);
469 OS << "export_as" << ExportAsModule << "\n";
472 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
474 // Print inferred subframework modules so that we don't need to re-infer
475 // them (requires expensive directory iteration + stat calls) when we build
476 // the module. Regular inferred submodules are OK, as we need to look at all
477 // those header files anyway.
478 if (!(*MI)->IsInferred || (*MI)->IsFramework)
479 (*MI)->print(OS, Indent + 2);
481 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
482 OS.indent(Indent + 2);
484 if (Module *Restriction = Exports[I].getPointer()) {
485 OS << Restriction->getFullModuleName(true);
486 if (Exports[I].getInt())
494 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
495 OS.indent(Indent + 2);
497 printModuleId(OS, UnresolvedExports[I].Id);
498 if (UnresolvedExports[I].Wildcard)
499 OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*");
503 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
504 OS.indent(Indent + 2);
506 OS << DirectUses[I]->getFullModuleName(true);
510 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
511 OS.indent(Indent + 2);
513 printModuleId(OS, UnresolvedDirectUses[I]);
517 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
518 OS.indent(Indent + 2);
520 if (LinkLibraries[I].IsFramework)
523 OS.write_escaped(LinkLibraries[I].Library);
527 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
528 OS.indent(Indent + 2);
530 printModuleId(OS, UnresolvedConflicts[I].Id);
532 OS.write_escaped(UnresolvedConflicts[I].Message);
536 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
537 OS.indent(Indent + 2);
539 OS << Conflicts[I].Other->getFullModuleName(true);
541 OS.write_escaped(Conflicts[I].Message);
545 if (InferSubmodules) {
546 OS.indent(Indent + 2);
547 if (InferExplicitSubmodules)
549 OS << "module * {\n";
550 if (InferExportWildcard) {
551 OS.indent(Indent + 4);
554 OS.indent(Indent + 2);
562 LLVM_DUMP_METHOD void Module::dump() const {
566 void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
567 VisibleCallback Vis, ConflictCallback Cb) {
568 assert(Loc.isValid() && "setVisible expects a valid import location");
576 Visiting *ExportedBy;
579 std::function<void(Visiting)> VisitModule = [&](Visiting V) {
580 // Modules that aren't available cannot be made visible.
581 if (!V.M->isAvailable())
584 // Nothing to do for a module that's already visible.
585 unsigned ID = V.M->getVisibilityID();
586 if (ImportLocs.size() <= ID)
587 ImportLocs.resize(ID + 1);
588 else if (ImportLocs[ID].isValid())
591 ImportLocs[ID] = Loc;
594 // Make any exported modules visible.
595 SmallVector<Module *, 16> Exports;
596 V.M->getExportedModules(Exports);
597 for (Module *E : Exports)
598 VisitModule({E, &V});
600 for (auto &C : V.M->Conflicts) {
601 if (isVisible(C.Other)) {
602 llvm::SmallVector<Module*, 8> Path;
603 for (Visiting *I = &V; I; I = I->ExportedBy)
604 Path.push_back(I->M);
605 Cb(Path, C.Other, C.Message);
609 VisitModule({M, nullptr});