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" 15 #include "clang/Basic/CharInfo.h" 16 #include "clang/Basic/FileManager.h" 17 #include "clang/Basic/LangOptions.h" 18 #include "clang/Basic/SourceLocation.h" 19 #include "clang/Basic/TargetInfo.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 35 using namespace clang; 36 37 Module::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) { 49 IsAvailable = Parent->isAvailable(); 50 IsUnimportable = Parent->isUnimportable(); 51 IsSystem = Parent->IsSystem; 52 IsExternC = Parent->IsExternC; 53 NoUndeclaredIncludes = Parent->NoUndeclaredIncludes; 54 ModuleMapIsPrivate = Parent->ModuleMapIsPrivate; 55 56 Parent->SubModuleIndex[Name] = Parent->SubModules.size(); 57 Parent->SubModules.push_back(this); 58 } 59 } 60 61 Module::~Module() { 62 for (auto *Submodule : SubModules) { 63 delete Submodule; 64 } 65 } 66 67 static 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. 100 static 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 131 bool Module::isUnimportable(const LangOptions &LangOpts, 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. 160 bool Module::isForBuilding(const LangOptions &LangOpts) const { 161 StringRef TopLevelName = getTopLevelModuleName(); 162 StringRef CurrentModule = LangOpts.CurrentModule; 163 164 // When building framework Foo, we want to make sure that Foo *and* 165 // Foo_Private are textually included and no modules are built for both. 166 if (getTopLevelModule()->IsFramework && 167 CurrentModule == LangOpts.ModuleName && 168 !CurrentModule.endswith("_Private") && TopLevelName.endswith("_Private")) 169 TopLevelName = TopLevelName.drop_back(8); 170 171 return TopLevelName == CurrentModule; 172 } 173 174 bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target, 175 Requirement &Req, 176 UnresolvedHeaderDirective &MissingHeader, 177 Module *&ShadowingModule) const { 178 if (IsAvailable) 179 return true; 180 181 if (isUnimportable(LangOpts, Target, Req, ShadowingModule)) 182 return false; 183 184 // FIXME: All missing headers are listed on the top-level module. Should we 185 // just look there? 186 for (const Module *Current = this; Current; Current = Current->Parent) { 187 if (!Current->MissingHeaders.empty()) { 188 MissingHeader = Current->MissingHeaders.front(); 189 return false; 190 } 191 } 192 193 llvm_unreachable("could not find a reason why module is unavailable"); 194 } 195 196 bool Module::isSubModuleOf(const Module *Other) const { 197 for (auto *Parent = this; Parent; Parent = Parent->Parent) { 198 if (Parent == Other) 199 return true; 200 } 201 return false; 202 } 203 204 const Module *Module::getTopLevelModule() const { 205 const Module *Result = this; 206 while (Result->Parent) 207 Result = Result->Parent; 208 209 return Result; 210 } 211 212 static StringRef getModuleNameFromComponent( 213 const std::pair<std::string, SourceLocation> &IdComponent) { 214 return IdComponent.first; 215 } 216 217 static StringRef getModuleNameFromComponent(StringRef R) { return R; } 218 219 template<typename InputIter> 220 static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End, 221 bool AllowStringLiterals = true) { 222 for (InputIter It = Begin; It != End; ++It) { 223 if (It != Begin) 224 OS << "."; 225 226 StringRef Name = getModuleNameFromComponent(*It); 227 if (!AllowStringLiterals || isValidAsciiIdentifier(Name)) 228 OS << Name; 229 else { 230 OS << '"'; 231 OS.write_escaped(Name); 232 OS << '"'; 233 } 234 } 235 } 236 237 template<typename Container> 238 static void printModuleId(raw_ostream &OS, const Container &C) { 239 return printModuleId(OS, C.begin(), C.end()); 240 } 241 242 std::string Module::getFullModuleName(bool AllowStringLiterals) const { 243 SmallVector<StringRef, 2> Names; 244 245 // Build up the set of module names (from innermost to outermost). 246 for (const Module *M = this; M; M = M->Parent) 247 Names.push_back(M->Name); 248 249 std::string Result; 250 251 llvm::raw_string_ostream Out(Result); 252 printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals); 253 Out.flush(); 254 255 return Result; 256 } 257 258 bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const { 259 for (const Module *M = this; M; M = M->Parent) { 260 if (nameParts.empty() || M->Name != nameParts.back()) 261 return false; 262 nameParts = nameParts.drop_back(); 263 } 264 return nameParts.empty(); 265 } 266 267 OptionalDirectoryEntryRef Module::getEffectiveUmbrellaDir() const { 268 if (Umbrella && Umbrella.is<FileEntryRef>()) 269 return Umbrella.get<FileEntryRef>().getDir(); 270 if (Umbrella && Umbrella.is<DirectoryEntryRef>()) 271 return Umbrella.get<DirectoryEntryRef>(); 272 return std::nullopt; 273 } 274 275 void Module::addTopHeader(FileEntryRef File) { 276 assert(File); 277 TopHeaders.insert(File); 278 } 279 280 ArrayRef<FileEntryRef> Module::getTopHeaders(FileManager &FileMgr) { 281 if (!TopHeaderNames.empty()) { 282 for (StringRef TopHeaderName : TopHeaderNames) 283 if (auto FE = FileMgr.getOptionalFileRef(TopHeaderName)) 284 TopHeaders.insert(*FE); 285 TopHeaderNames.clear(); 286 } 287 288 return llvm::ArrayRef(TopHeaders.begin(), TopHeaders.end()); 289 } 290 291 bool Module::directlyUses(const Module *Requested) { 292 auto *Top = getTopLevelModule(); 293 294 // A top-level module implicitly uses itself. 295 if (Requested->isSubModuleOf(Top)) 296 return true; 297 298 for (auto *Use : Top->DirectUses) 299 if (Requested->isSubModuleOf(Use)) 300 return true; 301 302 // Anyone is allowed to use our builtin stdarg.h and stddef.h and their 303 // accompanying modules. 304 if (!Requested->Parent && (Requested->Name == "_Builtin_stdarg" || Requested->Name == "_Builtin_stddef")) 305 return true; 306 307 if (NoUndeclaredIncludes) 308 UndeclaredUses.insert(Requested); 309 310 return false; 311 } 312 313 void Module::addRequirement(StringRef Feature, bool RequiredState, 314 const LangOptions &LangOpts, 315 const TargetInfo &Target) { 316 Requirements.push_back(Requirement(std::string(Feature), RequiredState)); 317 318 // If this feature is currently available, we're done. 319 if (hasFeature(Feature, LangOpts, Target) == RequiredState) 320 return; 321 322 markUnavailable(/*Unimportable*/true); 323 } 324 325 void Module::markUnavailable(bool Unimportable) { 326 auto needUpdate = [Unimportable](Module *M) { 327 return M->IsAvailable || (!M->IsUnimportable && Unimportable); 328 }; 329 330 if (!needUpdate(this)) 331 return; 332 333 SmallVector<Module *, 2> Stack; 334 Stack.push_back(this); 335 while (!Stack.empty()) { 336 Module *Current = Stack.back(); 337 Stack.pop_back(); 338 339 if (!needUpdate(Current)) 340 continue; 341 342 Current->IsAvailable = false; 343 Current->IsUnimportable |= Unimportable; 344 for (auto *Submodule : Current->submodules()) { 345 if (needUpdate(Submodule)) 346 Stack.push_back(Submodule); 347 } 348 } 349 } 350 351 Module *Module::findSubmodule(StringRef Name) const { 352 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); 353 if (Pos == SubModuleIndex.end()) 354 return nullptr; 355 356 return SubModules[Pos->getValue()]; 357 } 358 359 Module *Module::findOrInferSubmodule(StringRef Name) { 360 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); 361 if (Pos != SubModuleIndex.end()) 362 return SubModules[Pos->getValue()]; 363 if (!InferSubmodules) 364 return nullptr; 365 Module *Result = new Module(Name, SourceLocation(), this, false, InferExplicitSubmodules, 0); 366 Result->InferExplicitSubmodules = InferExplicitSubmodules; 367 Result->InferSubmodules = InferSubmodules; 368 Result->InferExportWildcard = InferExportWildcard; 369 if (Result->InferExportWildcard) 370 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 371 return Result; 372 } 373 374 Module *Module::getGlobalModuleFragment() const { 375 assert(isNamedModuleUnit() && "We should only query the global module " 376 "fragment from the C++ 20 Named modules"); 377 378 for (auto *SubModule : SubModules) 379 if (SubModule->isExplicitGlobalModule()) 380 return SubModule; 381 382 return nullptr; 383 } 384 385 Module *Module::getPrivateModuleFragment() const { 386 assert(isNamedModuleUnit() && "We should only query the private module " 387 "fragment from the C++ 20 Named modules"); 388 389 for (auto *SubModule : SubModules) 390 if (SubModule->isPrivateModule()) 391 return SubModule; 392 393 return nullptr; 394 } 395 396 void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const { 397 // All non-explicit submodules are exported. 398 for (std::vector<Module *>::const_iterator I = SubModules.begin(), 399 E = SubModules.end(); 400 I != E; ++I) { 401 Module *Mod = *I; 402 if (!Mod->IsExplicit) 403 Exported.push_back(Mod); 404 } 405 406 // Find re-exported modules by filtering the list of imported modules. 407 bool AnyWildcard = false; 408 bool UnrestrictedWildcard = false; 409 SmallVector<Module *, 4> WildcardRestrictions; 410 for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 411 Module *Mod = Exports[I].getPointer(); 412 if (!Exports[I].getInt()) { 413 // Export a named module directly; no wildcards involved. 414 Exported.push_back(Mod); 415 416 continue; 417 } 418 419 // Wildcard export: export all of the imported modules that match 420 // the given pattern. 421 AnyWildcard = true; 422 if (UnrestrictedWildcard) 423 continue; 424 425 if (Module *Restriction = Exports[I].getPointer()) 426 WildcardRestrictions.push_back(Restriction); 427 else { 428 WildcardRestrictions.clear(); 429 UnrestrictedWildcard = true; 430 } 431 } 432 433 // If there were any wildcards, push any imported modules that were 434 // re-exported by the wildcard restriction. 435 if (!AnyWildcard) 436 return; 437 438 for (unsigned I = 0, N = Imports.size(); I != N; ++I) { 439 Module *Mod = Imports[I]; 440 bool Acceptable = UnrestrictedWildcard; 441 if (!Acceptable) { 442 // Check whether this module meets one of the restrictions. 443 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) { 444 Module *Restriction = WildcardRestrictions[R]; 445 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) { 446 Acceptable = true; 447 break; 448 } 449 } 450 } 451 452 if (!Acceptable) 453 continue; 454 455 Exported.push_back(Mod); 456 } 457 } 458 459 void Module::buildVisibleModulesCache() const { 460 assert(VisibleModulesCache.empty() && "cache does not need building"); 461 462 // This module is visible to itself. 463 VisibleModulesCache.insert(this); 464 465 // Every imported module is visible. 466 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end()); 467 while (!Stack.empty()) { 468 Module *CurrModule = Stack.pop_back_val(); 469 470 // Every module transitively exported by an imported module is visible. 471 if (VisibleModulesCache.insert(CurrModule).second) 472 CurrModule->getExportedModules(Stack); 473 } 474 } 475 476 void Module::print(raw_ostream &OS, unsigned Indent, bool Dump) const { 477 OS.indent(Indent); 478 if (IsFramework) 479 OS << "framework "; 480 if (IsExplicit) 481 OS << "explicit "; 482 OS << "module "; 483 printModuleId(OS, &Name, &Name + 1); 484 485 if (IsSystem || IsExternC) { 486 OS.indent(Indent + 2); 487 if (IsSystem) 488 OS << " [system]"; 489 if (IsExternC) 490 OS << " [extern_c]"; 491 } 492 493 OS << " {\n"; 494 495 if (!Requirements.empty()) { 496 OS.indent(Indent + 2); 497 OS << "requires "; 498 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) { 499 if (I) 500 OS << ", "; 501 if (!Requirements[I].second) 502 OS << "!"; 503 OS << Requirements[I].first; 504 } 505 OS << "\n"; 506 } 507 508 if (std::optional<Header> H = getUmbrellaHeaderAsWritten()) { 509 OS.indent(Indent + 2); 510 OS << "umbrella header \""; 511 OS.write_escaped(H->NameAsWritten); 512 OS << "\"\n"; 513 } else if (std::optional<DirectoryName> D = getUmbrellaDirAsWritten()) { 514 OS.indent(Indent + 2); 515 OS << "umbrella \""; 516 OS.write_escaped(D->NameAsWritten); 517 OS << "\"\n"; 518 } 519 520 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) { 521 OS.indent(Indent + 2); 522 OS << "config_macros "; 523 if (ConfigMacrosExhaustive) 524 OS << "[exhaustive]"; 525 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) { 526 if (I) 527 OS << ", "; 528 OS << ConfigMacros[I]; 529 } 530 OS << "\n"; 531 } 532 533 struct { 534 StringRef Prefix; 535 HeaderKind Kind; 536 } Kinds[] = {{"", HK_Normal}, 537 {"textual ", HK_Textual}, 538 {"private ", HK_Private}, 539 {"private textual ", HK_PrivateTextual}, 540 {"exclude ", HK_Excluded}}; 541 542 for (auto &K : Kinds) { 543 assert(&K == &Kinds[K.Kind] && "kinds in wrong order"); 544 for (auto &H : Headers[K.Kind]) { 545 OS.indent(Indent + 2); 546 OS << K.Prefix << "header \""; 547 OS.write_escaped(H.NameAsWritten); 548 OS << "\" { size " << H.Entry.getSize() 549 << " mtime " << H.Entry.getModificationTime() << " }\n"; 550 } 551 } 552 for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) { 553 for (auto &U : *Unresolved) { 554 OS.indent(Indent + 2); 555 OS << Kinds[U.Kind].Prefix << "header \""; 556 OS.write_escaped(U.FileName); 557 OS << "\""; 558 if (U.Size || U.ModTime) { 559 OS << " {"; 560 if (U.Size) 561 OS << " size " << *U.Size; 562 if (U.ModTime) 563 OS << " mtime " << *U.ModTime; 564 OS << " }"; 565 } 566 OS << "\n"; 567 } 568 } 569 570 if (!ExportAsModule.empty()) { 571 OS.indent(Indent + 2); 572 OS << "export_as" << ExportAsModule << "\n"; 573 } 574 575 for (auto *Submodule : submodules()) 576 // Print inferred subframework modules so that we don't need to re-infer 577 // them (requires expensive directory iteration + stat calls) when we build 578 // the module. Regular inferred submodules are OK, as we need to look at all 579 // those header files anyway. 580 if (!Submodule->IsInferred || Submodule->IsFramework) 581 Submodule->print(OS, Indent + 2, Dump); 582 583 for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 584 OS.indent(Indent + 2); 585 OS << "export "; 586 if (Module *Restriction = Exports[I].getPointer()) { 587 OS << Restriction->getFullModuleName(true); 588 if (Exports[I].getInt()) 589 OS << ".*"; 590 } else { 591 OS << "*"; 592 } 593 OS << "\n"; 594 } 595 596 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { 597 OS.indent(Indent + 2); 598 OS << "export "; 599 printModuleId(OS, UnresolvedExports[I].Id); 600 if (UnresolvedExports[I].Wildcard) 601 OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*"); 602 OS << "\n"; 603 } 604 605 if (Dump) { 606 for (Module *M : Imports) { 607 OS.indent(Indent + 2); 608 llvm::errs() << "import " << M->getFullModuleName() << "\n"; 609 } 610 } 611 612 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) { 613 OS.indent(Indent + 2); 614 OS << "use "; 615 OS << DirectUses[I]->getFullModuleName(true); 616 OS << "\n"; 617 } 618 619 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) { 620 OS.indent(Indent + 2); 621 OS << "use "; 622 printModuleId(OS, UnresolvedDirectUses[I]); 623 OS << "\n"; 624 } 625 626 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) { 627 OS.indent(Indent + 2); 628 OS << "link "; 629 if (LinkLibraries[I].IsFramework) 630 OS << "framework "; 631 OS << "\""; 632 OS.write_escaped(LinkLibraries[I].Library); 633 OS << "\""; 634 } 635 636 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) { 637 OS.indent(Indent + 2); 638 OS << "conflict "; 639 printModuleId(OS, UnresolvedConflicts[I].Id); 640 OS << ", \""; 641 OS.write_escaped(UnresolvedConflicts[I].Message); 642 OS << "\"\n"; 643 } 644 645 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) { 646 OS.indent(Indent + 2); 647 OS << "conflict "; 648 OS << Conflicts[I].Other->getFullModuleName(true); 649 OS << ", \""; 650 OS.write_escaped(Conflicts[I].Message); 651 OS << "\"\n"; 652 } 653 654 if (InferSubmodules) { 655 OS.indent(Indent + 2); 656 if (InferExplicitSubmodules) 657 OS << "explicit "; 658 OS << "module * {\n"; 659 if (InferExportWildcard) { 660 OS.indent(Indent + 4); 661 OS << "export *\n"; 662 } 663 OS.indent(Indent + 2); 664 OS << "}\n"; 665 } 666 667 OS.indent(Indent); 668 OS << "}\n"; 669 } 670 671 LLVM_DUMP_METHOD void Module::dump() const { 672 print(llvm::errs(), 0, true); 673 } 674 675 void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc, 676 VisibleCallback Vis, ConflictCallback Cb) { 677 // We can't import a global module fragment so the location can be invalid. 678 assert((M->isGlobalModule() || Loc.isValid()) && 679 "setVisible expects a valid import location"); 680 if (isVisible(M)) 681 return; 682 683 ++Generation; 684 685 struct Visiting { 686 Module *M; 687 Visiting *ExportedBy; 688 }; 689 690 std::function<void(Visiting)> VisitModule = [&](Visiting V) { 691 // Nothing to do for a module that's already visible. 692 unsigned ID = V.M->getVisibilityID(); 693 if (ImportLocs.size() <= ID) 694 ImportLocs.resize(ID + 1); 695 else if (ImportLocs[ID].isValid()) 696 return; 697 698 ImportLocs[ID] = Loc; 699 Vis(V.M); 700 701 // Make any exported modules visible. 702 SmallVector<Module *, 16> Exports; 703 V.M->getExportedModules(Exports); 704 for (Module *E : Exports) { 705 // Don't import non-importable modules. 706 if (!E->isUnimportable()) 707 VisitModule({E, &V}); 708 } 709 710 for (auto &C : V.M->Conflicts) { 711 if (isVisible(C.Other)) { 712 llvm::SmallVector<Module*, 8> Path; 713 for (Visiting *I = &V; I; I = I->ExportedBy) 714 Path.push_back(I->M); 715 Cb(Path, C.Other, C.Message); 716 } 717 } 718 }; 719 VisitModule({M, nullptr}); 720 } 721 722 void VisibleModuleSet::makeTransitiveImportsVisible(Module *M, 723 SourceLocation Loc, 724 VisibleCallback Vis, 725 ConflictCallback Cb) { 726 for (auto *I : M->Imports) 727 setVisible(I, Loc, Vis, Cb); 728 } 729 730 ASTSourceDescriptor::ASTSourceDescriptor(Module &M) 731 : Signature(M.Signature), ClangModule(&M) { 732 if (M.Directory) 733 Path = M.Directory->getName(); 734 if (auto File = M.getASTFile()) 735 ASTFile = File->getName(); 736 } 737 738 std::string ASTSourceDescriptor::getModuleName() const { 739 if (ClangModule) 740 return ClangModule->Name; 741 else 742 return std::string(PCHModuleName); 743 } 744