1 //===--- Module.cpp - Describe a module -----------------------------------===// 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 //===----------------------------------------------------------------------===// 14 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" 24 25 using namespace clang; 26 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) { 36 if (Parent) { 37 if (!Parent->isAvailable()) 38 IsAvailable = false; 39 if (Parent->IsSystem) 40 IsSystem = true; 41 if (Parent->IsExternC) 42 IsExternC = true; 43 IsMissingRequirement = Parent->IsMissingRequirement; 44 45 Parent->SubModuleIndex[Name] = Parent->SubModules.size(); 46 Parent->SubModules.push_back(this); 47 } 48 } 49 50 Module::~Module() { 51 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end(); 52 I != IEnd; ++I) { 53 delete *I; 54 } 55 } 56 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)); 71 } 72 73 bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target, 74 Requirement &Req, 75 UnresolvedHeaderDirective &MissingHeader) const { 76 if (IsAvailable) 77 return true; 78 79 for (const Module *Current = this; Current; Current = Current->Parent) { 80 if (!Current->MissingHeaders.empty()) { 81 MissingHeader = Current->MissingHeaders.front(); 82 return false; 83 } 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]; 88 return false; 89 } 90 } 91 } 92 93 llvm_unreachable("could not find a reason why module is unavailable"); 94 } 95 96 bool Module::isSubModuleOf(const Module *Other) const { 97 const Module *This = this; 98 do { 99 if (This == Other) 100 return true; 101 102 This = This->Parent; 103 } while (This); 104 105 return false; 106 } 107 108 const Module *Module::getTopLevelModule() const { 109 const Module *Result = this; 110 while (Result->Parent) 111 Result = Result->Parent; 112 113 return Result; 114 } 115 116 std::string Module::getFullModuleName() const { 117 SmallVector<StringRef, 2> Names; 118 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); 122 123 std::string Result; 124 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(), 125 IEnd = Names.rend(); 126 I != IEnd; ++I) { 127 if (!Result.empty()) 128 Result += '.'; 129 130 Result += *I; 131 } 132 133 return Result; 134 } 135 136 const DirectoryEntry *Module::getUmbrellaDir() const { 137 if (const FileEntry *Header = getUmbrellaHeader()) 138 return Header->getDir(); 139 140 return Umbrella.dyn_cast<const DirectoryEntry *>(); 141 } 142 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); 149 } 150 TopHeaderNames.clear(); 151 } 152 153 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end()); 154 } 155 156 void Module::addRequirement(StringRef Feature, bool RequiredState, 157 const LangOptions &LangOpts, 158 const TargetInfo &Target) { 159 Requirements.push_back(Requirement(Feature, RequiredState)); 160 161 // If this feature is currently available, we're done. 162 if (hasFeature(Feature, LangOpts, Target) == RequiredState) 163 return; 164 165 markUnavailable(/*MissingRequirement*/true); 166 } 167 168 void Module::markUnavailable(bool MissingRequirement) { 169 if (!IsAvailable) 170 return; 171 172 SmallVector<Module *, 2> Stack; 173 Stack.push_back(this); 174 while (!Stack.empty()) { 175 Module *Current = Stack.back(); 176 Stack.pop_back(); 177 178 if (!Current->IsAvailable) 179 continue; 180 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); 188 } 189 } 190 } 191 192 Module *Module::findSubmodule(StringRef Name) const { 193 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); 194 if (Pos == SubModuleIndex.end()) 195 return nullptr; 196 197 return SubModules[Pos->getValue()]; 198 } 199 200 static void printModuleId(raw_ostream &OS, const ModuleId &Id) { 201 for (unsigned I = 0, N = Id.size(); I != N; ++I) { 202 if (I) 203 OS << "."; 204 OS << Id[I].first; 205 } 206 } 207 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(); 212 I != E; ++I) { 213 Module *Mod = *I; 214 if (!Mod->IsExplicit) 215 Exported.push_back(Mod); 216 } 217 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); 227 228 continue; 229 } 230 231 // Wildcard export: export all of the imported modules that match 232 // the given pattern. 233 AnyWildcard = true; 234 if (UnrestrictedWildcard) 235 continue; 236 237 if (Module *Restriction = Exports[I].getPointer()) 238 WildcardRestrictions.push_back(Restriction); 239 else { 240 WildcardRestrictions.clear(); 241 UnrestrictedWildcard = true; 242 } 243 } 244 245 // If there were any wildcards, push any imported modules that were 246 // re-exported by the wildcard restriction. 247 if (!AnyWildcard) 248 return; 249 250 for (unsigned I = 0, N = Imports.size(); I != N; ++I) { 251 Module *Mod = Imports[I]; 252 bool Acceptable = UnrestrictedWildcard; 253 if (!Acceptable) { 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)) { 258 Acceptable = true; 259 break; 260 } 261 } 262 } 263 264 if (!Acceptable) 265 continue; 266 267 Exported.push_back(Mod); 268 } 269 } 270 271 void Module::buildVisibleModulesCache() const { 272 assert(VisibleModulesCache.empty() && "cache does not need building"); 273 274 // This module is visible to itself. 275 VisibleModulesCache.insert(this); 276 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(); 281 282 // Every module transitively exported by an imported module is visible. 283 if (VisibleModulesCache.insert(CurrModule).second) 284 CurrModule->getExportedModules(Stack); 285 } 286 } 287 288 void Module::print(raw_ostream &OS, unsigned Indent) const { 289 OS.indent(Indent); 290 if (IsFramework) 291 OS << "framework "; 292 if (IsExplicit) 293 OS << "explicit "; 294 OS << "module " << Name; 295 296 if (IsSystem) { 297 OS.indent(Indent + 2); 298 OS << " [system]"; 299 } 300 301 OS << " {\n"; 302 303 if (!Requirements.empty()) { 304 OS.indent(Indent + 2); 305 OS << "requires "; 306 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) { 307 if (I) 308 OS << ", "; 309 if (!Requirements[I].second) 310 OS << "!"; 311 OS << Requirements[I].first; 312 } 313 OS << "\n"; 314 } 315 316 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) { 317 OS.indent(Indent + 2); 318 OS << "umbrella header \""; 319 OS.write_escaped(UmbrellaHeader->getName()); 320 OS << "\"\n"; 321 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) { 322 OS.indent(Indent + 2); 323 OS << "umbrella \""; 324 OS.write_escaped(UmbrellaDir->getName()); 325 OS << "\"\n"; 326 } 327 328 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) { 329 OS.indent(Indent + 2); 330 OS << "config_macros "; 331 if (ConfigMacrosExhaustive) 332 OS << "[exhaustive]"; 333 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) { 334 if (I) 335 OS << ", "; 336 OS << ConfigMacros[I]; 337 } 338 OS << "\n"; 339 } 340 341 struct { 342 StringRef Prefix; 343 HeaderKind Kind; 344 } Kinds[] = {{"", HK_Normal}, 345 {"textual ", HK_Textual}, 346 {"private ", HK_Private}, 347 {"private textual ", HK_PrivateTextual}, 348 {"exclude ", HK_Excluded}}; 349 350 for (auto &K : Kinds) { 351 for (auto &H : Headers[K.Kind]) { 352 OS.indent(Indent + 2); 353 OS << K.Prefix << "header \""; 354 OS.write_escaped(H.NameAsWritten); 355 OS << "\"\n"; 356 } 357 } 358 359 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); 360 MI != MIEnd; ++MI) 361 // Print inferred subframework modules so that we don't need to re-infer 362 // them (requires expensive directory iteration + stat calls) when we build 363 // the module. Regular inferred submodules are OK, as we need to look at all 364 // those header files anyway. 365 if (!(*MI)->IsInferred || (*MI)->IsFramework) 366 (*MI)->print(OS, Indent + 2); 367 368 for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 369 OS.indent(Indent + 2); 370 OS << "export "; 371 if (Module *Restriction = Exports[I].getPointer()) { 372 OS << Restriction->getFullModuleName(); 373 if (Exports[I].getInt()) 374 OS << ".*"; 375 } else { 376 OS << "*"; 377 } 378 OS << "\n"; 379 } 380 381 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { 382 OS.indent(Indent + 2); 383 OS << "export "; 384 printModuleId(OS, UnresolvedExports[I].Id); 385 if (UnresolvedExports[I].Wildcard) { 386 if (UnresolvedExports[I].Id.empty()) 387 OS << "*"; 388 else 389 OS << ".*"; 390 } 391 OS << "\n"; 392 } 393 394 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) { 395 OS.indent(Indent + 2); 396 OS << "use "; 397 OS << DirectUses[I]->getFullModuleName(); 398 OS << "\n"; 399 } 400 401 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) { 402 OS.indent(Indent + 2); 403 OS << "use "; 404 printModuleId(OS, UnresolvedDirectUses[I]); 405 OS << "\n"; 406 } 407 408 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) { 409 OS.indent(Indent + 2); 410 OS << "link "; 411 if (LinkLibraries[I].IsFramework) 412 OS << "framework "; 413 OS << "\""; 414 OS.write_escaped(LinkLibraries[I].Library); 415 OS << "\""; 416 } 417 418 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) { 419 OS.indent(Indent + 2); 420 OS << "conflict "; 421 printModuleId(OS, UnresolvedConflicts[I].Id); 422 OS << ", \""; 423 OS.write_escaped(UnresolvedConflicts[I].Message); 424 OS << "\"\n"; 425 } 426 427 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) { 428 OS.indent(Indent + 2); 429 OS << "conflict "; 430 OS << Conflicts[I].Other->getFullModuleName(); 431 OS << ", \""; 432 OS.write_escaped(Conflicts[I].Message); 433 OS << "\"\n"; 434 } 435 436 if (InferSubmodules) { 437 OS.indent(Indent + 2); 438 if (InferExplicitSubmodules) 439 OS << "explicit "; 440 OS << "module * {\n"; 441 if (InferExportWildcard) { 442 OS.indent(Indent + 4); 443 OS << "export *\n"; 444 } 445 OS.indent(Indent + 2); 446 OS << "}\n"; 447 } 448 449 OS.indent(Indent); 450 OS << "}\n"; 451 } 452 453 void Module::dump() const { 454 print(llvm::errs()); 455 } 456 457 458