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 || IsExternC) { 297 OS.indent(Indent + 2); 298 if (IsSystem) 299 OS << " [system]"; 300 if (IsExternC) 301 OS << " [extern_c]"; 302 } 303 304 OS << " {\n"; 305 306 if (!Requirements.empty()) { 307 OS.indent(Indent + 2); 308 OS << "requires "; 309 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) { 310 if (I) 311 OS << ", "; 312 if (!Requirements[I].second) 313 OS << "!"; 314 OS << Requirements[I].first; 315 } 316 OS << "\n"; 317 } 318 319 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) { 320 OS.indent(Indent + 2); 321 OS << "umbrella header \""; 322 OS.write_escaped(UmbrellaHeader->getName()); 323 OS << "\"\n"; 324 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) { 325 OS.indent(Indent + 2); 326 OS << "umbrella \""; 327 OS.write_escaped(UmbrellaDir->getName()); 328 OS << "\"\n"; 329 } 330 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) { 337 if (I) 338 OS << ", "; 339 OS << ConfigMacros[I]; 340 } 341 OS << "\n"; 342 } 343 344 struct { 345 StringRef Prefix; 346 HeaderKind Kind; 347 } Kinds[] = {{"", HK_Normal}, 348 {"textual ", HK_Textual}, 349 {"private ", HK_Private}, 350 {"private textual ", HK_PrivateTextual}, 351 {"exclude ", HK_Excluded}}; 352 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); 358 OS << "\"\n"; 359 } 360 } 361 362 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); 363 MI != MIEnd; ++MI) 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); 370 371 for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 372 OS.indent(Indent + 2); 373 OS << "export "; 374 if (Module *Restriction = Exports[I].getPointer()) { 375 OS << Restriction->getFullModuleName(); 376 if (Exports[I].getInt()) 377 OS << ".*"; 378 } else { 379 OS << "*"; 380 } 381 OS << "\n"; 382 } 383 384 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { 385 OS.indent(Indent + 2); 386 OS << "export "; 387 printModuleId(OS, UnresolvedExports[I].Id); 388 if (UnresolvedExports[I].Wildcard) { 389 if (UnresolvedExports[I].Id.empty()) 390 OS << "*"; 391 else 392 OS << ".*"; 393 } 394 OS << "\n"; 395 } 396 397 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) { 398 OS.indent(Indent + 2); 399 OS << "use "; 400 OS << DirectUses[I]->getFullModuleName(); 401 OS << "\n"; 402 } 403 404 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) { 405 OS.indent(Indent + 2); 406 OS << "use "; 407 printModuleId(OS, UnresolvedDirectUses[I]); 408 OS << "\n"; 409 } 410 411 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) { 412 OS.indent(Indent + 2); 413 OS << "link "; 414 if (LinkLibraries[I].IsFramework) 415 OS << "framework "; 416 OS << "\""; 417 OS.write_escaped(LinkLibraries[I].Library); 418 OS << "\""; 419 } 420 421 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) { 422 OS.indent(Indent + 2); 423 OS << "conflict "; 424 printModuleId(OS, UnresolvedConflicts[I].Id); 425 OS << ", \""; 426 OS.write_escaped(UnresolvedConflicts[I].Message); 427 OS << "\"\n"; 428 } 429 430 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) { 431 OS.indent(Indent + 2); 432 OS << "conflict "; 433 OS << Conflicts[I].Other->getFullModuleName(); 434 OS << ", \""; 435 OS.write_escaped(Conflicts[I].Message); 436 OS << "\"\n"; 437 } 438 439 if (InferSubmodules) { 440 OS.indent(Indent + 2); 441 if (InferExplicitSubmodules) 442 OS << "explicit "; 443 OS << "module * {\n"; 444 if (InferExportWildcard) { 445 OS.indent(Indent + 4); 446 OS << "export *\n"; 447 } 448 OS.indent(Indent + 2); 449 OS << "}\n"; 450 } 451 452 OS.indent(Indent); 453 OS << "}\n"; 454 } 455 456 void Module::dump() const { 457 print(llvm::errs()); 458 } 459 460 461