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/FileManager.h"
17 #include "clang/Basic/LangOptions.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
27 Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
28 bool IsFramework, bool IsExplicit)
29 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), Directory(),
30 Umbrella(), ASTFile(nullptr), IsMissingRequirement(false),
31 IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework),
32 IsExplicit(IsExplicit), IsSystem(false), IsExternC(false),
33 IsInferred(false), InferSubmodules(false), InferExplicitSubmodules(false),
34 InferExportWildcard(false), ConfigMacrosExhaustive(false),
35 NameVisibility(Hidden) {
37 if (!Parent->isAvailable())
41 if (Parent->IsExternC)
43 IsMissingRequirement = Parent->IsMissingRequirement;
45 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
46 Parent->SubModules.push_back(this);
51 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
57 /// \brief Determine whether a translation unit built using the current
58 /// language options has the given feature.
59 static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
60 const TargetInfo &Target) {
61 return llvm::StringSwitch<bool>(Feature)
62 .Case("altivec", LangOpts.AltiVec)
63 .Case("blocks", LangOpts.Blocks)
64 .Case("cplusplus", LangOpts.CPlusPlus)
65 .Case("cplusplus11", LangOpts.CPlusPlus11)
66 .Case("objc", LangOpts.ObjC1)
67 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
68 .Case("opencl", LangOpts.OpenCL)
69 .Case("tls", Target.isTLSSupported())
70 .Default(Target.hasFeature(Feature));
73 bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
75 UnresolvedHeaderDirective &MissingHeader) const {
79 for (const Module *Current = this; Current; Current = Current->Parent) {
80 if (!Current->MissingHeaders.empty()) {
81 MissingHeader = Current->MissingHeaders.front();
84 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
85 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
86 Current->Requirements[I].second) {
87 Req = Current->Requirements[I];
93 llvm_unreachable("could not find a reason why module is unavailable");
96 bool Module::isSubModuleOf(const Module *Other) const {
97 const Module *This = this;
108 const Module *Module::getTopLevelModule() const {
109 const Module *Result = this;
110 while (Result->Parent)
111 Result = Result->Parent;
116 std::string Module::getFullModuleName() const {
117 SmallVector<StringRef, 2> Names;
119 // Build up the set of module names (from innermost to outermost).
120 for (const Module *M = this; M; M = M->Parent)
121 Names.push_back(M->Name);
124 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
136 const DirectoryEntry *Module::getUmbrellaDir() const {
137 if (const FileEntry *Header = getUmbrellaHeader())
138 return Header->getDir();
140 return Umbrella.dyn_cast<const DirectoryEntry *>();
143 ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
144 if (!TopHeaderNames.empty()) {
145 for (std::vector<std::string>::iterator
146 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
147 if (const FileEntry *FE = FileMgr.getFile(*I))
148 TopHeaders.insert(FE);
150 TopHeaderNames.clear();
153 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
156 void Module::addRequirement(StringRef Feature, bool RequiredState,
157 const LangOptions &LangOpts,
158 const TargetInfo &Target) {
159 Requirements.push_back(Requirement(Feature, RequiredState));
161 // If this feature is currently available, we're done.
162 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
165 markUnavailable(/*MissingRequirement*/true);
168 void Module::markUnavailable(bool MissingRequirement) {
172 SmallVector<Module *, 2> Stack;
173 Stack.push_back(this);
174 while (!Stack.empty()) {
175 Module *Current = Stack.back();
178 if (!Current->IsAvailable)
181 Current->IsAvailable = false;
182 Current->IsMissingRequirement |= MissingRequirement;
183 for (submodule_iterator Sub = Current->submodule_begin(),
184 SubEnd = Current->submodule_end();
185 Sub != SubEnd; ++Sub) {
186 if ((*Sub)->IsAvailable)
187 Stack.push_back(*Sub);
192 Module *Module::findSubmodule(StringRef Name) const {
193 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
194 if (Pos == SubModuleIndex.end())
197 return SubModules[Pos->getValue()];
200 static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
201 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
208 void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
209 // All non-explicit submodules are exported.
210 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
211 E = SubModules.end();
214 if (!Mod->IsExplicit)
215 Exported.push_back(Mod);
218 // Find re-exported modules by filtering the list of imported modules.
219 bool AnyWildcard = false;
220 bool UnrestrictedWildcard = false;
221 SmallVector<Module *, 4> WildcardRestrictions;
222 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
223 Module *Mod = Exports[I].getPointer();
224 if (!Exports[I].getInt()) {
225 // Export a named module directly; no wildcards involved.
226 Exported.push_back(Mod);
231 // Wildcard export: export all of the imported modules that match
232 // the given pattern.
234 if (UnrestrictedWildcard)
237 if (Module *Restriction = Exports[I].getPointer())
238 WildcardRestrictions.push_back(Restriction);
240 WildcardRestrictions.clear();
241 UnrestrictedWildcard = true;
245 // If there were any wildcards, push any imported modules that were
246 // re-exported by the wildcard restriction.
250 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
251 Module *Mod = Imports[I];
252 bool Acceptable = UnrestrictedWildcard;
254 // Check whether this module meets one of the restrictions.
255 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
256 Module *Restriction = WildcardRestrictions[R];
257 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
267 Exported.push_back(Mod);
271 void Module::buildVisibleModulesCache() const {
272 assert(VisibleModulesCache.empty() && "cache does not need building");
274 // This module is visible to itself.
275 VisibleModulesCache.insert(this);
277 // Every imported module is visible.
278 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
279 while (!Stack.empty()) {
280 Module *CurrModule = Stack.pop_back_val();
282 // Every module transitively exported by an imported module is visible.
283 if (VisibleModulesCache.insert(CurrModule).second)
284 CurrModule->getExportedModules(Stack);
288 void Module::print(raw_ostream &OS, unsigned Indent) const {
294 OS << "module " << Name;
296 if (IsSystem || IsExternC) {
297 OS.indent(Indent + 2);
306 if (!Requirements.empty()) {
307 OS.indent(Indent + 2);
309 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
312 if (!Requirements[I].second)
314 OS << Requirements[I].first;
319 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
320 OS.indent(Indent + 2);
321 OS << "umbrella header \"";
322 OS.write_escaped(UmbrellaHeader->getName());
324 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
325 OS.indent(Indent + 2);
327 OS.write_escaped(UmbrellaDir->getName());
331 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
332 OS.indent(Indent + 2);
333 OS << "config_macros ";
334 if (ConfigMacrosExhaustive)
335 OS << "[exhaustive]";
336 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
339 OS << ConfigMacros[I];
347 } Kinds[] = {{"", HK_Normal},
348 {"textual ", HK_Textual},
349 {"private ", HK_Private},
350 {"private textual ", HK_PrivateTextual},
351 {"exclude ", HK_Excluded}};
353 for (auto &K : Kinds) {
354 for (auto &H : Headers[K.Kind]) {
355 OS.indent(Indent + 2);
356 OS << K.Prefix << "header \"";
357 OS.write_escaped(H.NameAsWritten);
362 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
364 // Print inferred subframework modules so that we don't need to re-infer
365 // them (requires expensive directory iteration + stat calls) when we build
366 // the module. Regular inferred submodules are OK, as we need to look at all
367 // those header files anyway.
368 if (!(*MI)->IsInferred || (*MI)->IsFramework)
369 (*MI)->print(OS, Indent + 2);
371 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
372 OS.indent(Indent + 2);
374 if (Module *Restriction = Exports[I].getPointer()) {
375 OS << Restriction->getFullModuleName();
376 if (Exports[I].getInt())
384 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
385 OS.indent(Indent + 2);
387 printModuleId(OS, UnresolvedExports[I].Id);
388 if (UnresolvedExports[I].Wildcard) {
389 if (UnresolvedExports[I].Id.empty())
397 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
398 OS.indent(Indent + 2);
400 OS << DirectUses[I]->getFullModuleName();
404 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
405 OS.indent(Indent + 2);
407 printModuleId(OS, UnresolvedDirectUses[I]);
411 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
412 OS.indent(Indent + 2);
414 if (LinkLibraries[I].IsFramework)
417 OS.write_escaped(LinkLibraries[I].Library);
421 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
422 OS.indent(Indent + 2);
424 printModuleId(OS, UnresolvedConflicts[I].Id);
426 OS.write_escaped(UnresolvedConflicts[I].Message);
430 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
431 OS.indent(Indent + 2);
433 OS << Conflicts[I].Other->getFullModuleName();
435 OS.write_escaped(Conflicts[I].Message);
439 if (InferSubmodules) {
440 OS.indent(Indent + 2);
441 if (InferExplicitSubmodules)
443 OS << "module * {\n";
444 if (InferExportWildcard) {
445 OS.indent(Indent + 4);
448 OS.indent(Indent + 2);
456 void Module::dump() const {