| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 1 | //===--- Module.cpp - Describe a module -----------------------------------===// |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the Module class, which describes a module in the source |
| 11 | // code. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 14 | |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Module.h" |
| Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 16 | #include "clang/Basic/CharInfo.h" |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LangOptions.h" |
| Douglas Gregor | 0070c0b | 2012-01-30 06:38:25 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
| Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/ArrayRef.h" |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/StringSwitch.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 25 | |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 28 | Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, |
| Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 29 | bool IsFramework, bool IsExplicit, unsigned VisibilityID) |
| Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 30 | : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), Directory(), |
| Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 31 | Umbrella(), ASTFile(nullptr), VisibilityID(VisibilityID), |
| Richard Smith | 8a308ec | 2015-11-05 00:54:55 +0000 | [diff] [blame] | 32 | IsMissingRequirement(false), HasIncompatibleModuleFile(false), |
| 33 | IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework), |
| 34 | IsExplicit(IsExplicit), IsSystem(false), IsExternC(false), |
| 35 | IsInferred(false), InferSubmodules(false), InferExplicitSubmodules(false), |
| 36 | InferExportWildcard(false), ConfigMacrosExhaustive(false), |
| David Blaikie | e6b7c28 | 2017-04-11 20:46:34 +0000 | [diff] [blame] | 37 | NoUndeclaredIncludes(false), NameVisibility(Hidden) { |
| Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 38 | if (Parent) { |
| 39 | if (!Parent->isAvailable()) |
| 40 | IsAvailable = false; |
| Douglas Gregor | 3ec6663 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 41 | if (Parent->IsSystem) |
| 42 | IsSystem = true; |
| Richard Smith | 9bca298 | 2014-03-08 00:03:56 +0000 | [diff] [blame] | 43 | if (Parent->IsExternC) |
| 44 | IsExternC = true; |
| Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 45 | if (Parent->NoUndeclaredIncludes) |
| 46 | NoUndeclaredIncludes = true; |
| Ben Langmuir | 993055f | 2014-04-18 23:51:00 +0000 | [diff] [blame] | 47 | IsMissingRequirement = Parent->IsMissingRequirement; |
| Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 48 | |
| 49 | Parent->SubModuleIndex[Name] = Parent->SubModules.size(); |
| 50 | Parent->SubModules.push_back(this); |
| 51 | } |
| 52 | } |
| 53 | |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 54 | Module::~Module() { |
| Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 55 | for (submodule_iterator I = submodule_begin(), IEnd = submodule_end(); |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 56 | I != IEnd; ++I) { |
| Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 57 | delete *I; |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 58 | } |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 61 | /// \brief Determine whether a translation unit built using the current |
| 62 | /// language options has the given feature. |
| Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 63 | static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, |
| 64 | const TargetInfo &Target) { |
| Ben Langmuir | 532d210 | 2015-02-02 21:56:15 +0000 | [diff] [blame] | 65 | bool HasFeature = llvm::StringSwitch<bool>(Feature) |
| 66 | .Case("altivec", LangOpts.AltiVec) |
| 67 | .Case("blocks", LangOpts.Blocks) |
| Eric Fiselier | e38cea0 | 2017-05-28 21:07:22 +0000 | [diff] [blame] | 68 | .Case("coroutines", LangOpts.CoroutinesTS) |
| Ben Langmuir | 532d210 | 2015-02-02 21:56:15 +0000 | [diff] [blame] | 69 | .Case("cplusplus", LangOpts.CPlusPlus) |
| 70 | .Case("cplusplus11", LangOpts.CPlusPlus11) |
| Elad Cohen | fb6358d | 2016-09-04 06:00:42 +0000 | [diff] [blame] | 71 | .Case("freestanding", LangOpts.Freestanding) |
| Bruno Cardoso Lopes | 6736e19 | 2016-08-30 21:25:42 +0000 | [diff] [blame] | 72 | .Case("gnuinlineasm", LangOpts.GNUAsm) |
| Ben Langmuir | 532d210 | 2015-02-02 21:56:15 +0000 | [diff] [blame] | 73 | .Case("objc", LangOpts.ObjC1) |
| 74 | .Case("objc_arc", LangOpts.ObjCAutoRefCount) |
| 75 | .Case("opencl", LangOpts.OpenCL) |
| 76 | .Case("tls", Target.isTLSSupported()) |
| Ulrich Weigand | 3c5038a | 2015-07-30 14:08:36 +0000 | [diff] [blame] | 77 | .Case("zvector", LangOpts.ZVector) |
| Ben Langmuir | 532d210 | 2015-02-02 21:56:15 +0000 | [diff] [blame] | 78 | .Default(Target.hasFeature(Feature)); |
| 79 | if (!HasFeature) |
| 80 | HasFeature = std::find(LangOpts.ModuleFeatures.begin(), |
| 81 | LangOpts.ModuleFeatures.end(), |
| 82 | Feature) != LangOpts.ModuleFeatures.end(); |
| 83 | return HasFeature; |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 86 | bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target, |
| 87 | Requirement &Req, |
| 88 | UnresolvedHeaderDirective &MissingHeader) const { |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 89 | if (IsAvailable) |
| 90 | return true; |
| 91 | |
| 92 | for (const Module *Current = this; Current; Current = Current->Parent) { |
| Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 93 | for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) { |
| 94 | if (hasFeature(Current->Requirements[I].first, LangOpts, Target) != |
| 95 | Current->Requirements[I].second) { |
| 96 | Req = Current->Requirements[I]; |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | } |
| Ben Langmuir | 75a7e43 | 2015-07-13 19:48:52 +0000 | [diff] [blame] | 100 | if (!Current->MissingHeaders.empty()) { |
| 101 | MissingHeader = Current->MissingHeaders.front(); |
| 102 | return false; |
| 103 | } |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | llvm_unreachable("could not find a reason why module is unavailable"); |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| Dmitri Gribenko | 62bcd92 | 2014-04-18 14:36:51 +0000 | [diff] [blame] | 109 | bool Module::isSubModuleOf(const Module *Other) const { |
| Douglas Gregor | f5eedd0 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 110 | const Module *This = this; |
| 111 | do { |
| 112 | if (This == Other) |
| 113 | return true; |
| 114 | |
| 115 | This = This->Parent; |
| 116 | } while (This); |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| Douglas Gregor | 7344109 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 121 | const Module *Module::getTopLevelModule() const { |
| 122 | const Module *Result = this; |
| 123 | while (Result->Parent) |
| 124 | Result = Result->Parent; |
| 125 | |
| 126 | return Result; |
| 127 | } |
| 128 | |
| Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 129 | static StringRef getModuleNameFromComponent( |
| 130 | const std::pair<std::string, SourceLocation> &IdComponent) { |
| 131 | return IdComponent.first; |
| 132 | } |
| 133 | static StringRef getModuleNameFromComponent(StringRef R) { return R; } |
| 134 | |
| 135 | template<typename InputIter> |
| 136 | static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End, |
| 137 | bool AllowStringLiterals = true) { |
| 138 | for (InputIter It = Begin; It != End; ++It) { |
| 139 | if (It != Begin) |
| 140 | OS << "."; |
| 141 | |
| 142 | StringRef Name = getModuleNameFromComponent(*It); |
| 143 | if (!AllowStringLiterals || isValidIdentifier(Name)) |
| 144 | OS << Name; |
| 145 | else { |
| 146 | OS << '"'; |
| 147 | OS.write_escaped(Name); |
| 148 | OS << '"'; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | template<typename Container> |
| 154 | static void printModuleId(raw_ostream &OS, const Container &C) { |
| 155 | return printModuleId(OS, C.begin(), C.end()); |
| 156 | } |
| 157 | |
| 158 | std::string Module::getFullModuleName(bool AllowStringLiterals) const { |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 159 | SmallVector<StringRef, 2> Names; |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 160 | |
| 161 | // Build up the set of module names (from innermost to outermost). |
| 162 | for (const Module *M = this; M; M = M->Parent) |
| 163 | Names.push_back(M->Name); |
| 164 | |
| 165 | std::string Result; |
| Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 166 | |
| 167 | llvm::raw_string_ostream Out(Result); |
| 168 | printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals); |
| 169 | Out.flush(); |
| 170 | |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 171 | return Result; |
| 172 | } |
| 173 | |
| Ben Langmuir | 7ff2914 | 2015-08-13 17:13:33 +0000 | [diff] [blame] | 174 | bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const { |
| 175 | for (const Module *M = this; M; M = M->Parent) { |
| 176 | if (nameParts.empty() || M->Name != nameParts.back()) |
| 177 | return false; |
| 178 | nameParts = nameParts.drop_back(); |
| 179 | } |
| 180 | return nameParts.empty(); |
| 181 | } |
| 182 | |
| Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 183 | Module::DirectoryName Module::getUmbrellaDir() const { |
| 184 | if (Header U = getUmbrellaHeader()) |
| 185 | return {"", U.Entry->getDir()}; |
| Douglas Gregor | 73141fa | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 186 | |
| Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 187 | return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()}; |
| Douglas Gregor | 73141fa | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 190 | ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) { |
| 191 | if (!TopHeaderNames.empty()) { |
| 192 | for (std::vector<std::string>::iterator |
| 193 | I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) { |
| 194 | if (const FileEntry *FE = FileMgr.getFile(*I)) |
| 195 | TopHeaders.insert(FE); |
| 196 | } |
| 197 | TopHeaderNames.clear(); |
| 198 | } |
| 199 | |
| 200 | return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end()); |
| 201 | } |
| 202 | |
| Richard Smith | 8f4d3ff | 2015-03-26 22:10:01 +0000 | [diff] [blame] | 203 | bool Module::directlyUses(const Module *Requested) const { |
| 204 | auto *Top = getTopLevelModule(); |
| 205 | |
| 206 | // A top-level module implicitly uses itself. |
| 207 | if (Requested->isSubModuleOf(Top)) |
| 208 | return true; |
| 209 | |
| 210 | for (auto *Use : Top->DirectUses) |
| 211 | if (Requested->isSubModuleOf(Use)) |
| 212 | return true; |
| Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 213 | |
| 214 | // Anyone is allowed to use our builtin stddef.h and its accompanying module. |
| 215 | if (!Requested->Parent && Requested->Name == "_Builtin_stddef_max_align_t") |
| 216 | return true; |
| 217 | |
| Richard Smith | 8f4d3ff | 2015-03-26 22:10:01 +0000 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
| Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 221 | void Module::addRequirement(StringRef Feature, bool RequiredState, |
| 222 | const LangOptions &LangOpts, |
| Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 223 | const TargetInfo &Target) { |
| Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 224 | Requirements.push_back(Requirement(Feature, RequiredState)); |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 225 | |
| 226 | // If this feature is currently available, we're done. |
| Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 227 | if (hasFeature(Feature, LangOpts, Target) == RequiredState) |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 228 | return; |
| 229 | |
| Ben Langmuir | 993055f | 2014-04-18 23:51:00 +0000 | [diff] [blame] | 230 | markUnavailable(/*MissingRequirement*/true); |
| Ben Langmuir | ec8c975 | 2014-04-18 22:07:31 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| Ben Langmuir | 993055f | 2014-04-18 23:51:00 +0000 | [diff] [blame] | 233 | void Module::markUnavailable(bool MissingRequirement) { |
| Ben Langmuir | 75a7e43 | 2015-07-13 19:48:52 +0000 | [diff] [blame] | 234 | auto needUpdate = [MissingRequirement](Module *M) { |
| 235 | return M->IsAvailable || (!M->IsMissingRequirement && MissingRequirement); |
| 236 | }; |
| 237 | |
| 238 | if (!needUpdate(this)) |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 239 | return; |
| 240 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 241 | SmallVector<Module *, 2> Stack; |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 242 | Stack.push_back(this); |
| 243 | while (!Stack.empty()) { |
| 244 | Module *Current = Stack.back(); |
| 245 | Stack.pop_back(); |
| 246 | |
| Ben Langmuir | 75a7e43 | 2015-07-13 19:48:52 +0000 | [diff] [blame] | 247 | if (!needUpdate(Current)) |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 248 | continue; |
| 249 | |
| 250 | Current->IsAvailable = false; |
| Ben Langmuir | 993055f | 2014-04-18 23:51:00 +0000 | [diff] [blame] | 251 | Current->IsMissingRequirement |= MissingRequirement; |
| Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 252 | for (submodule_iterator Sub = Current->submodule_begin(), |
| 253 | SubEnd = Current->submodule_end(); |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 254 | Sub != SubEnd; ++Sub) { |
| Ben Langmuir | 75a7e43 | 2015-07-13 19:48:52 +0000 | [diff] [blame] | 255 | if (needUpdate(*Sub)) |
| Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 256 | Stack.push_back(*Sub); |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 261 | Module *Module::findSubmodule(StringRef Name) const { |
| 262 | llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); |
| 263 | if (Pos == SubModuleIndex.end()) |
| Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 264 | return nullptr; |
| 265 | |
| Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 266 | return SubModules[Pos->getValue()]; |
| 267 | } |
| 268 | |
| Argyrios Kyrtzidis | 8739f7b | 2013-02-19 19:34:40 +0000 | [diff] [blame] | 269 | void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const { |
| Dmitri Gribenko | e9bcf5b | 2013-11-04 21:51:33 +0000 | [diff] [blame] | 270 | // All non-explicit submodules are exported. |
| 271 | for (std::vector<Module *>::const_iterator I = SubModules.begin(), |
| 272 | E = SubModules.end(); |
| 273 | I != E; ++I) { |
| 274 | Module *Mod = *I; |
| 275 | if (!Mod->IsExplicit) |
| 276 | Exported.push_back(Mod); |
| 277 | } |
| 278 | |
| 279 | // Find re-exported modules by filtering the list of imported modules. |
| Argyrios Kyrtzidis | 8739f7b | 2013-02-19 19:34:40 +0000 | [diff] [blame] | 280 | bool AnyWildcard = false; |
| 281 | bool UnrestrictedWildcard = false; |
| 282 | SmallVector<Module *, 4> WildcardRestrictions; |
| 283 | for (unsigned I = 0, N = Exports.size(); I != N; ++I) { |
| 284 | Module *Mod = Exports[I].getPointer(); |
| 285 | if (!Exports[I].getInt()) { |
| 286 | // Export a named module directly; no wildcards involved. |
| 287 | Exported.push_back(Mod); |
| 288 | |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | // Wildcard export: export all of the imported modules that match |
| 293 | // the given pattern. |
| 294 | AnyWildcard = true; |
| 295 | if (UnrestrictedWildcard) |
| 296 | continue; |
| 297 | |
| 298 | if (Module *Restriction = Exports[I].getPointer()) |
| 299 | WildcardRestrictions.push_back(Restriction); |
| 300 | else { |
| 301 | WildcardRestrictions.clear(); |
| 302 | UnrestrictedWildcard = true; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // If there were any wildcards, push any imported modules that were |
| 307 | // re-exported by the wildcard restriction. |
| 308 | if (!AnyWildcard) |
| 309 | return; |
| 310 | |
| 311 | for (unsigned I = 0, N = Imports.size(); I != N; ++I) { |
| 312 | Module *Mod = Imports[I]; |
| 313 | bool Acceptable = UnrestrictedWildcard; |
| 314 | if (!Acceptable) { |
| 315 | // Check whether this module meets one of the restrictions. |
| 316 | for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) { |
| 317 | Module *Restriction = WildcardRestrictions[R]; |
| 318 | if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) { |
| 319 | Acceptable = true; |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (!Acceptable) |
| 326 | continue; |
| 327 | |
| 328 | Exported.push_back(Mod); |
| 329 | } |
| 330 | } |
| 331 | |
| Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 332 | void Module::buildVisibleModulesCache() const { |
| 333 | assert(VisibleModulesCache.empty() && "cache does not need building"); |
| 334 | |
| 335 | // This module is visible to itself. |
| 336 | VisibleModulesCache.insert(this); |
| 337 | |
| Dmitri Gribenko | dc360d5 | 2013-10-31 22:24:10 +0000 | [diff] [blame] | 338 | // Every imported module is visible. |
| Richard Smith | dde17e7 | 2013-11-01 02:19:14 +0000 | [diff] [blame] | 339 | SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end()); |
| Dmitri Gribenko | dc360d5 | 2013-10-31 22:24:10 +0000 | [diff] [blame] | 340 | while (!Stack.empty()) { |
| 341 | Module *CurrModule = Stack.pop_back_val(); |
| Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 342 | |
| Richard Smith | dde17e7 | 2013-11-01 02:19:14 +0000 | [diff] [blame] | 343 | // Every module transitively exported by an imported module is visible. |
| 344 | if (VisibleModulesCache.insert(CurrModule).second) |
| 345 | CurrModule->getExportedModules(Stack); |
| Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 349 | void Module::print(raw_ostream &OS, unsigned Indent) const { |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 350 | OS.indent(Indent); |
| 351 | if (IsFramework) |
| 352 | OS << "framework "; |
| 353 | if (IsExplicit) |
| 354 | OS << "explicit "; |
| Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 355 | OS << "module "; |
| 356 | printModuleId(OS, &Name, &Name + 1); |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 357 | |
| Ben Langmuir | 7615f00 | 2015-01-13 17:47:38 +0000 | [diff] [blame] | 358 | if (IsSystem || IsExternC) { |
| Douglas Gregor | a686e1b | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 359 | OS.indent(Indent + 2); |
| Ben Langmuir | 7615f00 | 2015-01-13 17:47:38 +0000 | [diff] [blame] | 360 | if (IsSystem) |
| 361 | OS << " [system]"; |
| 362 | if (IsExternC) |
| 363 | OS << " [extern_c]"; |
| Douglas Gregor | a686e1b | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | OS << " {\n"; |
| 367 | |
| Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 368 | if (!Requirements.empty()) { |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 369 | OS.indent(Indent + 2); |
| 370 | OS << "requires "; |
| Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 371 | for (unsigned I = 0, N = Requirements.size(); I != N; ++I) { |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 372 | if (I) |
| 373 | OS << ", "; |
| Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 374 | if (!Requirements[I].second) |
| 375 | OS << "!"; |
| 376 | OS << Requirements[I].first; |
| Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 377 | } |
| 378 | OS << "\n"; |
| 379 | } |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 380 | |
| Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 381 | if (Header H = getUmbrellaHeader()) { |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 382 | OS.indent(Indent + 2); |
| Douglas Gregor | 322f633 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 383 | OS << "umbrella header \""; |
| Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 384 | OS.write_escaped(H.NameAsWritten); |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 385 | OS << "\"\n"; |
| Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 386 | } else if (DirectoryName D = getUmbrellaDir()) { |
| Douglas Gregor | 322f633 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 387 | OS.indent(Indent + 2); |
| 388 | OS << "umbrella \""; |
| Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 389 | OS.write_escaped(D.NameAsWritten); |
| Douglas Gregor | 322f633 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 390 | OS << "\"\n"; |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 391 | } |
| Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 392 | |
| 393 | if (!ConfigMacros.empty() || ConfigMacrosExhaustive) { |
| 394 | OS.indent(Indent + 2); |
| 395 | OS << "config_macros "; |
| 396 | if (ConfigMacrosExhaustive) |
| Douglas Gregor | 8d93242 | 2013-03-20 03:59:18 +0000 | [diff] [blame] | 397 | OS << "[exhaustive]"; |
| Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 398 | for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) { |
| 399 | if (I) |
| 400 | OS << ", "; |
| 401 | OS << ConfigMacros[I]; |
| 402 | } |
| Douglas Gregor | 8d93242 | 2013-03-20 03:59:18 +0000 | [diff] [blame] | 403 | OS << "\n"; |
| Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 406 | struct { |
| Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 407 | StringRef Prefix; |
| Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 408 | HeaderKind Kind; |
| 409 | } Kinds[] = {{"", HK_Normal}, |
| 410 | {"textual ", HK_Textual}, |
| 411 | {"private ", HK_Private}, |
| 412 | {"private textual ", HK_PrivateTextual}, |
| 413 | {"exclude ", HK_Excluded}}; |
| Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 414 | |
| 415 | for (auto &K : Kinds) { |
| Richard Smith | 040e126 | 2017-06-02 01:55:39 +0000 | [diff] [blame] | 416 | assert(&K == &Kinds[K.Kind] && "kinds in wrong order"); |
| Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 417 | for (auto &H : Headers[K.Kind]) { |
| Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 418 | OS.indent(Indent + 2); |
| 419 | OS << K.Prefix << "header \""; |
| Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 420 | OS.write_escaped(H.NameAsWritten); |
| Richard Smith | 040e126 | 2017-06-02 01:55:39 +0000 | [diff] [blame] | 421 | OS << "\" { size " << H.Entry->getSize() |
| 422 | << " mtime " << H.Entry->getModificationTime() << " }\n"; |
| 423 | } |
| 424 | } |
| 425 | for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) { |
| 426 | for (auto &U : *Unresolved) { |
| 427 | OS.indent(Indent + 2); |
| 428 | OS << Kinds[U.Kind].Prefix << "header \""; |
| 429 | OS.write_escaped(U.FileName); |
| 430 | OS << "\""; |
| 431 | if (U.Size || U.ModTime) { |
| 432 | OS << " {"; |
| 433 | if (U.Size) |
| 434 | OS << " size " << *U.Size; |
| 435 | if (U.ModTime) |
| 436 | OS << " mtime " << *U.ModTime; |
| 437 | OS << " }"; |
| 438 | } |
| 439 | OS << "\n"; |
| Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 440 | } |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 441 | } |
| Douglas Gregor | 5952766 | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 442 | |
| Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 443 | for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 444 | MI != MIEnd; ++MI) |
| Ben Langmuir | 9d6448b | 2014-08-09 00:57:23 +0000 | [diff] [blame] | 445 | // Print inferred subframework modules so that we don't need to re-infer |
| 446 | // them (requires expensive directory iteration + stat calls) when we build |
| 447 | // the module. Regular inferred submodules are OK, as we need to look at all |
| 448 | // those header files anyway. |
| 449 | if (!(*MI)->IsInferred || (*MI)->IsFramework) |
| Ben Langmuir | ffbafa2 | 2014-04-23 21:10:46 +0000 | [diff] [blame] | 450 | (*MI)->print(OS, Indent + 2); |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 451 | |
| Douglas Gregor | 24bb923 | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 452 | for (unsigned I = 0, N = Exports.size(); I != N; ++I) { |
| 453 | OS.indent(Indent + 2); |
| Douglas Gregor | 8c7c835 | 2011-12-05 17:34:59 +0000 | [diff] [blame] | 454 | OS << "export "; |
| 455 | if (Module *Restriction = Exports[I].getPointer()) { |
| Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 456 | OS << Restriction->getFullModuleName(true); |
| Douglas Gregor | 8c7c835 | 2011-12-05 17:34:59 +0000 | [diff] [blame] | 457 | if (Exports[I].getInt()) |
| 458 | OS << ".*"; |
| 459 | } else { |
| 460 | OS << "*"; |
| 461 | } |
| Douglas Gregor | 24bb923 | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 462 | OS << "\n"; |
| 463 | } |
| 464 | |
| 465 | for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { |
| 466 | OS.indent(Indent + 2); |
| 467 | OS << "export "; |
| 468 | printModuleId(OS, UnresolvedExports[I].Id); |
| Davide Italiano | 7f96b39 | 2016-03-09 21:09:51 +0000 | [diff] [blame] | 469 | if (UnresolvedExports[I].Wildcard) |
| 470 | OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*"); |
| Douglas Gregor | 24bb923 | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 471 | OS << "\n"; |
| 472 | } |
| 473 | |
| Daniel Jasper | ba7f2f7 | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 474 | for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) { |
| 475 | OS.indent(Indent + 2); |
| 476 | OS << "use "; |
| Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 477 | OS << DirectUses[I]->getFullModuleName(true); |
| Daniel Jasper | ba7f2f7 | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 478 | OS << "\n"; |
| 479 | } |
| 480 | |
| 481 | for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) { |
| 482 | OS.indent(Indent + 2); |
| 483 | OS << "use "; |
| 484 | printModuleId(OS, UnresolvedDirectUses[I]); |
| 485 | OS << "\n"; |
| 486 | } |
| 487 | |
| Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 488 | for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) { |
| 489 | OS.indent(Indent + 2); |
| 490 | OS << "link "; |
| 491 | if (LinkLibraries[I].IsFramework) |
| 492 | OS << "framework "; |
| 493 | OS << "\""; |
| 494 | OS.write_escaped(LinkLibraries[I].Library); |
| 495 | OS << "\""; |
| 496 | } |
| 497 | |
| Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 498 | for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) { |
| 499 | OS.indent(Indent + 2); |
| 500 | OS << "conflict "; |
| 501 | printModuleId(OS, UnresolvedConflicts[I].Id); |
| 502 | OS << ", \""; |
| 503 | OS.write_escaped(UnresolvedConflicts[I].Message); |
| 504 | OS << "\"\n"; |
| 505 | } |
| 506 | |
| 507 | for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) { |
| 508 | OS.indent(Indent + 2); |
| 509 | OS << "conflict "; |
| Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 510 | OS << Conflicts[I].Other->getFullModuleName(true); |
| Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 511 | OS << ", \""; |
| 512 | OS.write_escaped(Conflicts[I].Message); |
| 513 | OS << "\"\n"; |
| 514 | } |
| 515 | |
| Douglas Gregor | 7344109 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 516 | if (InferSubmodules) { |
| 517 | OS.indent(Indent + 2); |
| 518 | if (InferExplicitSubmodules) |
| 519 | OS << "explicit "; |
| 520 | OS << "module * {\n"; |
| 521 | if (InferExportWildcard) { |
| 522 | OS.indent(Indent + 4); |
| 523 | OS << "export *\n"; |
| 524 | } |
| 525 | OS.indent(Indent + 2); |
| 526 | OS << "}\n"; |
| 527 | } |
| 528 | |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 529 | OS.indent(Indent); |
| 530 | OS << "}\n"; |
| 531 | } |
| 532 | |
| Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 533 | LLVM_DUMP_METHOD void Module::dump() const { |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 534 | print(llvm::errs()); |
| 535 | } |
| 536 | |
| Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 537 | void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc, |
| 538 | VisibleCallback Vis, ConflictCallback Cb) { |
| Ben Langmuir | 6d25fdc | 2016-02-11 17:04:42 +0000 | [diff] [blame] | 539 | assert(Loc.isValid() && "setVisible expects a valid import location"); |
| Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 540 | if (isVisible(M)) |
| 541 | return; |
| Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 542 | |
| Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 543 | ++Generation; |
| 544 | |
| 545 | struct Visiting { |
| 546 | Module *M; |
| 547 | Visiting *ExportedBy; |
| 548 | }; |
| 549 | |
| 550 | std::function<void(Visiting)> VisitModule = [&](Visiting V) { |
| 551 | // Modules that aren't available cannot be made visible. |
| 552 | if (!V.M->isAvailable()) |
| 553 | return; |
| 554 | |
| 555 | // Nothing to do for a module that's already visible. |
| 556 | unsigned ID = V.M->getVisibilityID(); |
| 557 | if (ImportLocs.size() <= ID) |
| 558 | ImportLocs.resize(ID + 1); |
| 559 | else if (ImportLocs[ID].isValid()) |
| 560 | return; |
| 561 | |
| 562 | ImportLocs[ID] = Loc; |
| 563 | Vis(M); |
| 564 | |
| 565 | // Make any exported modules visible. |
| 566 | SmallVector<Module *, 16> Exports; |
| 567 | V.M->getExportedModules(Exports); |
| 568 | for (Module *E : Exports) |
| 569 | VisitModule({E, &V}); |
| 570 | |
| 571 | for (auto &C : V.M->Conflicts) { |
| 572 | if (isVisible(C.Other)) { |
| 573 | llvm::SmallVector<Module*, 8> Path; |
| 574 | for (Visiting *I = &V; I; I = I->ExportedBy) |
| 575 | Path.push_back(I->M); |
| 576 | Cb(Path, C.Other, C.Message); |
| 577 | } |
| 578 | } |
| 579 | }; |
| 580 | VisitModule({M, nullptr}); |
| 581 | } |