1 //===--- CompilerInstance.cpp ---------------------------------------------===// 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 #include "clang/Frontend/CompilerInstance.h" 10 #include "clang/AST/ASTConsumer.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/AST/Decl.h" 13 #include "clang/Basic/CharInfo.h" 14 #include "clang/Basic/Diagnostic.h" 15 #include "clang/Basic/DiagnosticOptions.h" 16 #include "clang/Basic/FileManager.h" 17 #include "clang/Basic/LangStandard.h" 18 #include "clang/Basic/SourceManager.h" 19 #include "clang/Basic/Stack.h" 20 #include "clang/Basic/TargetInfo.h" 21 #include "clang/Basic/Version.h" 22 #include "clang/Config/config.h" 23 #include "clang/Frontend/ChainedDiagnosticConsumer.h" 24 #include "clang/Frontend/FrontendAction.h" 25 #include "clang/Frontend/FrontendActions.h" 26 #include "clang/Frontend/FrontendDiagnostic.h" 27 #include "clang/Frontend/FrontendPluginRegistry.h" 28 #include "clang/Frontend/LogDiagnosticPrinter.h" 29 #include "clang/Frontend/SARIFDiagnosticPrinter.h" 30 #include "clang/Frontend/SerializedDiagnosticPrinter.h" 31 #include "clang/Frontend/TextDiagnosticPrinter.h" 32 #include "clang/Frontend/Utils.h" 33 #include "clang/Frontend/VerifyDiagnosticConsumer.h" 34 #include "clang/Lex/HeaderSearch.h" 35 #include "clang/Lex/Preprocessor.h" 36 #include "clang/Lex/PreprocessorOptions.h" 37 #include "clang/Sema/CodeCompleteConsumer.h" 38 #include "clang/Sema/Sema.h" 39 #include "clang/Serialization/ASTReader.h" 40 #include "clang/Serialization/GlobalModuleIndex.h" 41 #include "clang/Serialization/InMemoryModuleCache.h" 42 #include "llvm/ADT/ScopeExit.h" 43 #include "llvm/ADT/Statistic.h" 44 #include "llvm/Config/llvm-config.h" 45 #include "llvm/Support/BuryPointer.h" 46 #include "llvm/Support/CrashRecoveryContext.h" 47 #include "llvm/Support/Errc.h" 48 #include "llvm/Support/FileSystem.h" 49 #include "llvm/Support/LockFileManager.h" 50 #include "llvm/Support/MemoryBuffer.h" 51 #include "llvm/Support/Path.h" 52 #include "llvm/Support/Program.h" 53 #include "llvm/Support/Signals.h" 54 #include "llvm/Support/TimeProfiler.h" 55 #include "llvm/Support/Timer.h" 56 #include "llvm/Support/raw_ostream.h" 57 #include "llvm/TargetParser/Host.h" 58 #include <optional> 59 #include <time.h> 60 #include <utility> 61 62 using namespace clang; 63 64 CompilerInstance::CompilerInstance( 65 std::shared_ptr<PCHContainerOperations> PCHContainerOps, 66 InMemoryModuleCache *SharedModuleCache) 67 : ModuleLoader(/* BuildingModule = */ SharedModuleCache), 68 Invocation(new CompilerInvocation()), 69 ModuleCache(SharedModuleCache ? SharedModuleCache 70 : new InMemoryModuleCache), 71 ThePCHContainerOperations(std::move(PCHContainerOps)) {} 72 73 CompilerInstance::~CompilerInstance() { 74 assert(OutputFiles.empty() && "Still output files in flight?"); 75 } 76 77 void CompilerInstance::setInvocation( 78 std::shared_ptr<CompilerInvocation> Value) { 79 Invocation = std::move(Value); 80 } 81 82 bool CompilerInstance::shouldBuildGlobalModuleIndex() const { 83 return (BuildGlobalModuleIndex || 84 (TheASTReader && TheASTReader->isGlobalIndexUnavailable() && 85 getFrontendOpts().GenerateGlobalModuleIndex)) && 86 !DisableGeneratingGlobalModuleIndex; 87 } 88 89 void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) { 90 Diagnostics = Value; 91 } 92 93 void CompilerInstance::setVerboseOutputStream(raw_ostream &Value) { 94 OwnedVerboseOutputStream.reset(); 95 VerboseOutputStream = &Value; 96 } 97 98 void CompilerInstance::setVerboseOutputStream(std::unique_ptr<raw_ostream> Value) { 99 OwnedVerboseOutputStream.swap(Value); 100 VerboseOutputStream = OwnedVerboseOutputStream.get(); 101 } 102 103 void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; } 104 void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; } 105 106 bool CompilerInstance::createTarget() { 107 // Create the target instance. 108 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), 109 getInvocation().TargetOpts)); 110 if (!hasTarget()) 111 return false; 112 113 // Check whether AuxTarget exists, if not, then create TargetInfo for the 114 // other side of CUDA/OpenMP/SYCL compilation. 115 if (!getAuxTarget() && 116 (getLangOpts().CUDA || getLangOpts().OpenMPIsDevice || 117 getLangOpts().SYCLIsDevice) && 118 !getFrontendOpts().AuxTriple.empty()) { 119 auto TO = std::make_shared<TargetOptions>(); 120 TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple); 121 if (getFrontendOpts().AuxTargetCPU) 122 TO->CPU = *getFrontendOpts().AuxTargetCPU; 123 if (getFrontendOpts().AuxTargetFeatures) 124 TO->FeaturesAsWritten = *getFrontendOpts().AuxTargetFeatures; 125 TO->HostTriple = getTarget().getTriple().str(); 126 setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO)); 127 } 128 129 if (!getTarget().hasStrictFP() && !getLangOpts().ExpStrictFP) { 130 if (getLangOpts().RoundingMath) { 131 getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_rounding); 132 getLangOpts().RoundingMath = false; 133 } 134 auto FPExc = getLangOpts().getFPExceptionMode(); 135 if (FPExc != LangOptions::FPE_Default && FPExc != LangOptions::FPE_Ignore) { 136 getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_exceptions); 137 getLangOpts().setFPExceptionMode(LangOptions::FPE_Ignore); 138 } 139 // FIXME: can we disable FEnvAccess? 140 } 141 142 // We should do it here because target knows nothing about 143 // language options when it's being created. 144 if (getLangOpts().OpenCL && 145 !getTarget().validateOpenCLTarget(getLangOpts(), getDiagnostics())) 146 return false; 147 148 // Inform the target of the language options. 149 // FIXME: We shouldn't need to do this, the target should be immutable once 150 // created. This complexity should be lifted elsewhere. 151 getTarget().adjust(getDiagnostics(), getLangOpts()); 152 153 // Adjust target options based on codegen options. 154 getTarget().adjustTargetOptions(getCodeGenOpts(), getTargetOpts()); 155 156 if (auto *Aux = getAuxTarget()) 157 getTarget().setAuxTarget(Aux); 158 159 return true; 160 } 161 162 llvm::vfs::FileSystem &CompilerInstance::getVirtualFileSystem() const { 163 return getFileManager().getVirtualFileSystem(); 164 } 165 166 void CompilerInstance::setFileManager(FileManager *Value) { 167 FileMgr = Value; 168 } 169 170 void CompilerInstance::setSourceManager(SourceManager *Value) { 171 SourceMgr = Value; 172 } 173 174 void CompilerInstance::setPreprocessor(std::shared_ptr<Preprocessor> Value) { 175 PP = std::move(Value); 176 } 177 178 void CompilerInstance::setASTContext(ASTContext *Value) { 179 Context = Value; 180 181 if (Context && Consumer) 182 getASTConsumer().Initialize(getASTContext()); 183 } 184 185 void CompilerInstance::setSema(Sema *S) { 186 TheSema.reset(S); 187 } 188 189 void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) { 190 Consumer = std::move(Value); 191 192 if (Context && Consumer) 193 getASTConsumer().Initialize(getASTContext()); 194 } 195 196 void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { 197 CompletionConsumer.reset(Value); 198 } 199 200 std::unique_ptr<Sema> CompilerInstance::takeSema() { 201 return std::move(TheSema); 202 } 203 204 IntrusiveRefCntPtr<ASTReader> CompilerInstance::getASTReader() const { 205 return TheASTReader; 206 } 207 void CompilerInstance::setASTReader(IntrusiveRefCntPtr<ASTReader> Reader) { 208 assert(ModuleCache.get() == &Reader->getModuleManager().getModuleCache() && 209 "Expected ASTReader to use the same PCM cache"); 210 TheASTReader = std::move(Reader); 211 } 212 213 std::shared_ptr<ModuleDependencyCollector> 214 CompilerInstance::getModuleDepCollector() const { 215 return ModuleDepCollector; 216 } 217 218 void CompilerInstance::setModuleDepCollector( 219 std::shared_ptr<ModuleDependencyCollector> Collector) { 220 ModuleDepCollector = std::move(Collector); 221 } 222 223 static void collectHeaderMaps(const HeaderSearch &HS, 224 std::shared_ptr<ModuleDependencyCollector> MDC) { 225 SmallVector<std::string, 4> HeaderMapFileNames; 226 HS.getHeaderMapFileNames(HeaderMapFileNames); 227 for (auto &Name : HeaderMapFileNames) 228 MDC->addFile(Name); 229 } 230 231 static void collectIncludePCH(CompilerInstance &CI, 232 std::shared_ptr<ModuleDependencyCollector> MDC) { 233 const PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); 234 if (PPOpts.ImplicitPCHInclude.empty()) 235 return; 236 237 StringRef PCHInclude = PPOpts.ImplicitPCHInclude; 238 FileManager &FileMgr = CI.getFileManager(); 239 auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude); 240 if (!PCHDir) { 241 MDC->addFile(PCHInclude); 242 return; 243 } 244 245 std::error_code EC; 246 SmallString<128> DirNative; 247 llvm::sys::path::native(PCHDir->getName(), DirNative); 248 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); 249 SimpleASTReaderListener Validator(CI.getPreprocessor()); 250 for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd; 251 Dir != DirEnd && !EC; Dir.increment(EC)) { 252 // Check whether this is an AST file. ASTReader::isAcceptableASTFile is not 253 // used here since we're not interested in validating the PCH at this time, 254 // but only to check whether this is a file containing an AST. 255 if (!ASTReader::readASTFileControlBlock( 256 Dir->path(), FileMgr, CI.getModuleCache(), 257 CI.getPCHContainerReader(), 258 /*FindModuleFileExtensions=*/false, Validator, 259 /*ValidateDiagnosticOptions=*/false)) 260 MDC->addFile(Dir->path()); 261 } 262 } 263 264 static void collectVFSEntries(CompilerInstance &CI, 265 std::shared_ptr<ModuleDependencyCollector> MDC) { 266 if (CI.getHeaderSearchOpts().VFSOverlayFiles.empty()) 267 return; 268 269 // Collect all VFS found. 270 SmallVector<llvm::vfs::YAMLVFSEntry, 16> VFSEntries; 271 for (const std::string &VFSFile : CI.getHeaderSearchOpts().VFSOverlayFiles) { 272 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer = 273 llvm::MemoryBuffer::getFile(VFSFile); 274 if (!Buffer) 275 return; 276 llvm::vfs::collectVFSFromYAML(std::move(Buffer.get()), 277 /*DiagHandler*/ nullptr, VFSFile, VFSEntries); 278 } 279 280 for (auto &E : VFSEntries) 281 MDC->addFile(E.VPath, E.RPath); 282 } 283 284 // Diagnostics 285 static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts, 286 const CodeGenOptions *CodeGenOpts, 287 DiagnosticsEngine &Diags) { 288 std::error_code EC; 289 std::unique_ptr<raw_ostream> StreamOwner; 290 raw_ostream *OS = &llvm::errs(); 291 if (DiagOpts->DiagnosticLogFile != "-") { 292 // Create the output stream. 293 auto FileOS = std::make_unique<llvm::raw_fd_ostream>( 294 DiagOpts->DiagnosticLogFile, EC, 295 llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF); 296 if (EC) { 297 Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) 298 << DiagOpts->DiagnosticLogFile << EC.message(); 299 } else { 300 FileOS->SetUnbuffered(); 301 OS = FileOS.get(); 302 StreamOwner = std::move(FileOS); 303 } 304 } 305 306 // Chain in the diagnostic client which will log the diagnostics. 307 auto Logger = std::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts, 308 std::move(StreamOwner)); 309 if (CodeGenOpts) 310 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); 311 if (Diags.ownsClient()) { 312 Diags.setClient( 313 new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger))); 314 } else { 315 Diags.setClient( 316 new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger))); 317 } 318 } 319 320 static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, 321 DiagnosticsEngine &Diags, 322 StringRef OutputFile) { 323 auto SerializedConsumer = 324 clang::serialized_diags::create(OutputFile, DiagOpts); 325 326 if (Diags.ownsClient()) { 327 Diags.setClient(new ChainedDiagnosticConsumer( 328 Diags.takeClient(), std::move(SerializedConsumer))); 329 } else { 330 Diags.setClient(new ChainedDiagnosticConsumer( 331 Diags.getClient(), std::move(SerializedConsumer))); 332 } 333 } 334 335 void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client, 336 bool ShouldOwnClient) { 337 Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client, 338 ShouldOwnClient, &getCodeGenOpts()); 339 } 340 341 IntrusiveRefCntPtr<DiagnosticsEngine> 342 CompilerInstance::createDiagnostics(DiagnosticOptions *Opts, 343 DiagnosticConsumer *Client, 344 bool ShouldOwnClient, 345 const CodeGenOptions *CodeGenOpts) { 346 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 347 IntrusiveRefCntPtr<DiagnosticsEngine> 348 Diags(new DiagnosticsEngine(DiagID, Opts)); 349 350 // Create the diagnostic client for reporting errors or for 351 // implementing -verify. 352 if (Client) { 353 Diags->setClient(Client, ShouldOwnClient); 354 } else if (Opts->getFormat() == DiagnosticOptions::SARIF) { 355 Diags->setClient(new SARIFDiagnosticPrinter(llvm::errs(), Opts)); 356 } else 357 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); 358 359 // Chain in -verify checker, if requested. 360 if (Opts->VerifyDiagnostics) 361 Diags->setClient(new VerifyDiagnosticConsumer(*Diags)); 362 363 // Chain in -diagnostic-log-file dumper, if requested. 364 if (!Opts->DiagnosticLogFile.empty()) 365 SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); 366 367 if (!Opts->DiagnosticSerializationFile.empty()) 368 SetupSerializedDiagnostics(Opts, *Diags, 369 Opts->DiagnosticSerializationFile); 370 371 // Configure our handling of diagnostics. 372 ProcessWarningOptions(*Diags, *Opts); 373 374 return Diags; 375 } 376 377 // File Manager 378 379 FileManager *CompilerInstance::createFileManager( 380 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { 381 if (!VFS) 382 VFS = FileMgr ? &FileMgr->getVirtualFileSystem() 383 : createVFSFromCompilerInvocation(getInvocation(), 384 getDiagnostics()); 385 assert(VFS && "FileManager has no VFS?"); 386 FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS)); 387 return FileMgr.get(); 388 } 389 390 // Source Manager 391 392 void CompilerInstance::createSourceManager(FileManager &FileMgr) { 393 SourceMgr = new SourceManager(getDiagnostics(), FileMgr); 394 } 395 396 // Initialize the remapping of files to alternative contents, e.g., 397 // those specified through other files. 398 static void InitializeFileRemapping(DiagnosticsEngine &Diags, 399 SourceManager &SourceMgr, 400 FileManager &FileMgr, 401 const PreprocessorOptions &InitOpts) { 402 // Remap files in the source manager (with buffers). 403 for (const auto &RB : InitOpts.RemappedFileBuffers) { 404 // Create the file entry for the file that we're mapping from. 405 const FileEntry *FromFile = 406 FileMgr.getVirtualFile(RB.first, RB.second->getBufferSize(), 0); 407 if (!FromFile) { 408 Diags.Report(diag::err_fe_remap_missing_from_file) << RB.first; 409 if (!InitOpts.RetainRemappedFileBuffers) 410 delete RB.second; 411 continue; 412 } 413 414 // Override the contents of the "from" file with the contents of the 415 // "to" file. If the caller owns the buffers, then pass a MemoryBufferRef; 416 // otherwise, pass as a std::unique_ptr<MemoryBuffer> to transfer ownership 417 // to the SourceManager. 418 if (InitOpts.RetainRemappedFileBuffers) 419 SourceMgr.overrideFileContents(FromFile, RB.second->getMemBufferRef()); 420 else 421 SourceMgr.overrideFileContents( 422 FromFile, std::unique_ptr<llvm::MemoryBuffer>( 423 const_cast<llvm::MemoryBuffer *>(RB.second))); 424 } 425 426 // Remap files in the source manager (with other files). 427 for (const auto &RF : InitOpts.RemappedFiles) { 428 // Find the file that we're mapping to. 429 OptionalFileEntryRef ToFile = FileMgr.getOptionalFileRef(RF.second); 430 if (!ToFile) { 431 Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second; 432 continue; 433 } 434 435 // Create the file entry for the file that we're mapping from. 436 const FileEntry *FromFile = 437 FileMgr.getVirtualFile(RF.first, ToFile->getSize(), 0); 438 if (!FromFile) { 439 Diags.Report(diag::err_fe_remap_missing_from_file) << RF.first; 440 continue; 441 } 442 443 // Override the contents of the "from" file with the contents of 444 // the "to" file. 445 SourceMgr.overrideFileContents(FromFile, *ToFile); 446 } 447 448 SourceMgr.setOverridenFilesKeepOriginalName( 449 InitOpts.RemappedFilesKeepOriginalName); 450 } 451 452 // Preprocessor 453 454 void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { 455 const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 456 457 // The AST reader holds a reference to the old preprocessor (if any). 458 TheASTReader.reset(); 459 460 // Create the Preprocessor. 461 HeaderSearch *HeaderInfo = 462 new HeaderSearch(getHeaderSearchOptsPtr(), getSourceManager(), 463 getDiagnostics(), getLangOpts(), &getTarget()); 464 PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOptsPtr(), 465 getDiagnostics(), getLangOpts(), 466 getSourceManager(), *HeaderInfo, *this, 467 /*IdentifierInfoLookup=*/nullptr, 468 /*OwnsHeaderSearch=*/true, TUKind); 469 getTarget().adjust(getDiagnostics(), getLangOpts()); 470 PP->Initialize(getTarget(), getAuxTarget()); 471 472 if (PPOpts.DetailedRecord) 473 PP->createPreprocessingRecord(); 474 475 // Apply remappings to the source manager. 476 InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(), 477 PP->getFileManager(), PPOpts); 478 479 // Predefine macros and configure the preprocessor. 480 InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(), 481 getFrontendOpts()); 482 483 // Initialize the header search object. In CUDA compilations, we use the aux 484 // triple (the host triple) to initialize our header search, since we need to 485 // find the host headers in order to compile the CUDA code. 486 const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple(); 487 if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA && 488 PP->getAuxTargetInfo()) 489 HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple(); 490 491 ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(), 492 PP->getLangOpts(), *HeaderSearchTriple); 493 494 PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP); 495 496 if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules) { 497 std::string ModuleHash = getInvocation().getModuleHash(); 498 PP->getHeaderSearchInfo().setModuleHash(ModuleHash); 499 PP->getHeaderSearchInfo().setModuleCachePath( 500 getSpecificModuleCachePath(ModuleHash)); 501 } 502 503 // Handle generating dependencies, if requested. 504 const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); 505 if (!DepOpts.OutputFile.empty()) 506 addDependencyCollector(std::make_shared<DependencyFileGenerator>(DepOpts)); 507 if (!DepOpts.DOTOutputFile.empty()) 508 AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile, 509 getHeaderSearchOpts().Sysroot); 510 511 // If we don't have a collector, but we are collecting module dependencies, 512 // then we're the top level compiler instance and need to create one. 513 if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) { 514 ModuleDepCollector = std::make_shared<ModuleDependencyCollector>( 515 DepOpts.ModuleDependencyOutputDir); 516 } 517 518 // If there is a module dep collector, register with other dep collectors 519 // and also (a) collect header maps and (b) TODO: input vfs overlay files. 520 if (ModuleDepCollector) { 521 addDependencyCollector(ModuleDepCollector); 522 collectHeaderMaps(PP->getHeaderSearchInfo(), ModuleDepCollector); 523 collectIncludePCH(*this, ModuleDepCollector); 524 collectVFSEntries(*this, ModuleDepCollector); 525 } 526 527 for (auto &Listener : DependencyCollectors) 528 Listener->attachToPreprocessor(*PP); 529 530 // Handle generating header include information, if requested. 531 if (DepOpts.ShowHeaderIncludes) 532 AttachHeaderIncludeGen(*PP, DepOpts); 533 if (!DepOpts.HeaderIncludeOutputFile.empty()) { 534 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; 535 if (OutputPath == "-") 536 OutputPath = ""; 537 AttachHeaderIncludeGen(*PP, DepOpts, 538 /*ShowAllHeaders=*/true, OutputPath, 539 /*ShowDepth=*/false); 540 } 541 542 if (DepOpts.ShowIncludesDest != ShowIncludesDestination::None) { 543 AttachHeaderIncludeGen(*PP, DepOpts, 544 /*ShowAllHeaders=*/true, /*OutputPath=*/"", 545 /*ShowDepth=*/true, /*MSStyle=*/true); 546 } 547 } 548 549 std::string CompilerInstance::getSpecificModuleCachePath(StringRef ModuleHash) { 550 // Set up the module path, including the hash for the module-creation options. 551 SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath); 552 if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash) 553 llvm::sys::path::append(SpecificModuleCache, ModuleHash); 554 return std::string(SpecificModuleCache.str()); 555 } 556 557 // ASTContext 558 559 void CompilerInstance::createASTContext() { 560 Preprocessor &PP = getPreprocessor(); 561 auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(), 562 PP.getIdentifierTable(), PP.getSelectorTable(), 563 PP.getBuiltinInfo(), PP.TUKind); 564 Context->InitBuiltinTypes(getTarget(), getAuxTarget()); 565 setASTContext(Context); 566 } 567 568 // ExternalASTSource 569 570 namespace { 571 // Helper to recursively read the module names for all modules we're adding. 572 // We mark these as known and redirect any attempt to load that module to 573 // the files we were handed. 574 struct ReadModuleNames : ASTReaderListener { 575 Preprocessor &PP; 576 llvm::SmallVector<std::string, 8> LoadedModules; 577 578 ReadModuleNames(Preprocessor &PP) : PP(PP) {} 579 580 void ReadModuleName(StringRef ModuleName) override { 581 // Keep the module name as a string for now. It's not safe to create a new 582 // IdentifierInfo from an ASTReader callback. 583 LoadedModules.push_back(ModuleName.str()); 584 } 585 586 void registerAll() { 587 ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap(); 588 for (const std::string &LoadedModule : LoadedModules) 589 MM.cacheModuleLoad(*PP.getIdentifierInfo(LoadedModule), 590 MM.findModule(LoadedModule)); 591 LoadedModules.clear(); 592 } 593 594 void markAllUnavailable() { 595 for (const std::string &LoadedModule : LoadedModules) { 596 if (Module *M = PP.getHeaderSearchInfo().getModuleMap().findModule( 597 LoadedModule)) { 598 M->HasIncompatibleModuleFile = true; 599 600 // Mark module as available if the only reason it was unavailable 601 // was missing headers. 602 SmallVector<Module *, 2> Stack; 603 Stack.push_back(M); 604 while (!Stack.empty()) { 605 Module *Current = Stack.pop_back_val(); 606 if (Current->IsUnimportable) continue; 607 Current->IsAvailable = true; 608 auto SubmodulesRange = Current->submodules(); 609 Stack.insert(Stack.end(), SubmodulesRange.begin(), 610 SubmodulesRange.end()); 611 } 612 } 613 } 614 LoadedModules.clear(); 615 } 616 }; 617 } // namespace 618 619 void CompilerInstance::createPCHExternalASTSource( 620 StringRef Path, DisableValidationForModuleKind DisableValidation, 621 bool AllowPCHWithCompilerErrors, void *DeserializationListener, 622 bool OwnDeserializationListener) { 623 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; 624 TheASTReader = createPCHExternalASTSource( 625 Path, getHeaderSearchOpts().Sysroot, DisableValidation, 626 AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(), 627 getASTContext(), getPCHContainerReader(), 628 getFrontendOpts().ModuleFileExtensions, DependencyCollectors, 629 DeserializationListener, OwnDeserializationListener, Preamble, 630 getFrontendOpts().UseGlobalModuleIndex); 631 } 632 633 IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource( 634 StringRef Path, StringRef Sysroot, 635 DisableValidationForModuleKind DisableValidation, 636 bool AllowPCHWithCompilerErrors, Preprocessor &PP, 637 InMemoryModuleCache &ModuleCache, ASTContext &Context, 638 const PCHContainerReader &PCHContainerRdr, 639 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 640 ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors, 641 void *DeserializationListener, bool OwnDeserializationListener, 642 bool Preamble, bool UseGlobalModuleIndex) { 643 HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts(); 644 645 IntrusiveRefCntPtr<ASTReader> Reader(new ASTReader( 646 PP, ModuleCache, &Context, PCHContainerRdr, Extensions, 647 Sysroot.empty() ? "" : Sysroot.data(), DisableValidation, 648 AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false, 649 HSOpts.ModulesValidateSystemHeaders, HSOpts.ValidateASTInputFilesContent, 650 UseGlobalModuleIndex)); 651 652 // We need the external source to be set up before we read the AST, because 653 // eagerly-deserialized declarations may use it. 654 Context.setExternalSource(Reader.get()); 655 656 Reader->setDeserializationListener( 657 static_cast<ASTDeserializationListener *>(DeserializationListener), 658 /*TakeOwnership=*/OwnDeserializationListener); 659 660 for (auto &Listener : DependencyCollectors) 661 Listener->attachToASTReader(*Reader); 662 663 auto Listener = std::make_unique<ReadModuleNames>(PP); 664 auto &ListenerRef = *Listener; 665 ASTReader::ListenerScope ReadModuleNamesListener(*Reader, 666 std::move(Listener)); 667 668 switch (Reader->ReadAST(Path, 669 Preamble ? serialization::MK_Preamble 670 : serialization::MK_PCH, 671 SourceLocation(), 672 ASTReader::ARR_None)) { 673 case ASTReader::Success: 674 // Set the predefines buffer as suggested by the PCH reader. Typically, the 675 // predefines buffer will be empty. 676 PP.setPredefines(Reader->getSuggestedPredefines()); 677 ListenerRef.registerAll(); 678 return Reader; 679 680 case ASTReader::Failure: 681 // Unrecoverable failure: don't even try to process the input file. 682 break; 683 684 case ASTReader::Missing: 685 case ASTReader::OutOfDate: 686 case ASTReader::VersionMismatch: 687 case ASTReader::ConfigurationMismatch: 688 case ASTReader::HadErrors: 689 // No suitable PCH file could be found. Return an error. 690 break; 691 } 692 693 ListenerRef.markAllUnavailable(); 694 Context.setExternalSource(nullptr); 695 return nullptr; 696 } 697 698 // Code Completion 699 700 static bool EnableCodeCompletion(Preprocessor &PP, 701 StringRef Filename, 702 unsigned Line, 703 unsigned Column) { 704 // Tell the source manager to chop off the given file at a specific 705 // line and column. 706 auto Entry = PP.getFileManager().getFile(Filename); 707 if (!Entry) { 708 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) 709 << Filename; 710 return true; 711 } 712 713 // Truncate the named file at the given line/column. 714 PP.SetCodeCompletionPoint(*Entry, Line, Column); 715 return false; 716 } 717 718 void CompilerInstance::createCodeCompletionConsumer() { 719 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; 720 if (!CompletionConsumer) { 721 setCodeCompletionConsumer(createCodeCompletionConsumer( 722 getPreprocessor(), Loc.FileName, Loc.Line, Loc.Column, 723 getFrontendOpts().CodeCompleteOpts, llvm::outs())); 724 return; 725 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, 726 Loc.Line, Loc.Column)) { 727 setCodeCompletionConsumer(nullptr); 728 return; 729 } 730 } 731 732 void CompilerInstance::createFrontendTimer() { 733 FrontendTimerGroup.reset( 734 new llvm::TimerGroup("frontend", "Clang front-end time report")); 735 FrontendTimer.reset( 736 new llvm::Timer("frontend", "Clang front-end timer", 737 *FrontendTimerGroup)); 738 } 739 740 CodeCompleteConsumer * 741 CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, 742 StringRef Filename, 743 unsigned Line, 744 unsigned Column, 745 const CodeCompleteOptions &Opts, 746 raw_ostream &OS) { 747 if (EnableCodeCompletion(PP, Filename, Line, Column)) 748 return nullptr; 749 750 // Set up the creation routine for code-completion. 751 return new PrintingCodeCompleteConsumer(Opts, OS); 752 } 753 754 void CompilerInstance::createSema(TranslationUnitKind TUKind, 755 CodeCompleteConsumer *CompletionConsumer) { 756 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), 757 TUKind, CompletionConsumer)); 758 // Attach the external sema source if there is any. 759 if (ExternalSemaSrc) { 760 TheSema->addExternalSource(ExternalSemaSrc.get()); 761 ExternalSemaSrc->InitializeSema(*TheSema); 762 } 763 } 764 765 // Output Files 766 767 void CompilerInstance::clearOutputFiles(bool EraseFiles) { 768 // The ASTConsumer can own streams that write to the output files. 769 assert(!hasASTConsumer() && "ASTConsumer should be reset"); 770 // Ignore errors that occur when trying to discard the temp file. 771 for (OutputFile &OF : OutputFiles) { 772 if (EraseFiles) { 773 if (OF.File) 774 consumeError(OF.File->discard()); 775 if (!OF.Filename.empty()) 776 llvm::sys::fs::remove(OF.Filename); 777 continue; 778 } 779 780 if (!OF.File) 781 continue; 782 783 if (OF.File->TmpName.empty()) { 784 consumeError(OF.File->discard()); 785 continue; 786 } 787 788 llvm::Error E = OF.File->keep(OF.Filename); 789 if (!E) 790 continue; 791 792 getDiagnostics().Report(diag::err_unable_to_rename_temp) 793 << OF.File->TmpName << OF.Filename << std::move(E); 794 795 llvm::sys::fs::remove(OF.File->TmpName); 796 } 797 OutputFiles.clear(); 798 if (DeleteBuiltModules) { 799 for (auto &Module : BuiltModules) 800 llvm::sys::fs::remove(Module.second); 801 BuiltModules.clear(); 802 } 803 } 804 805 std::unique_ptr<raw_pwrite_stream> CompilerInstance::createDefaultOutputFile( 806 bool Binary, StringRef InFile, StringRef Extension, bool RemoveFileOnSignal, 807 bool CreateMissingDirectories, bool ForceUseTemporary) { 808 StringRef OutputPath = getFrontendOpts().OutputFile; 809 std::optional<SmallString<128>> PathStorage; 810 if (OutputPath.empty()) { 811 if (InFile == "-" || Extension.empty()) { 812 OutputPath = "-"; 813 } else { 814 PathStorage.emplace(InFile); 815 llvm::sys::path::replace_extension(*PathStorage, Extension); 816 OutputPath = *PathStorage; 817 } 818 } 819 820 return createOutputFile(OutputPath, Binary, RemoveFileOnSignal, 821 getFrontendOpts().UseTemporary || ForceUseTemporary, 822 CreateMissingDirectories); 823 } 824 825 std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() { 826 return std::make_unique<llvm::raw_null_ostream>(); 827 } 828 829 std::unique_ptr<raw_pwrite_stream> 830 CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary, 831 bool RemoveFileOnSignal, bool UseTemporary, 832 bool CreateMissingDirectories) { 833 Expected<std::unique_ptr<raw_pwrite_stream>> OS = 834 createOutputFileImpl(OutputPath, Binary, RemoveFileOnSignal, UseTemporary, 835 CreateMissingDirectories); 836 if (OS) 837 return std::move(*OS); 838 getDiagnostics().Report(diag::err_fe_unable_to_open_output) 839 << OutputPath << errorToErrorCode(OS.takeError()).message(); 840 return nullptr; 841 } 842 843 Expected<std::unique_ptr<llvm::raw_pwrite_stream>> 844 CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary, 845 bool RemoveFileOnSignal, 846 bool UseTemporary, 847 bool CreateMissingDirectories) { 848 assert((!CreateMissingDirectories || UseTemporary) && 849 "CreateMissingDirectories is only allowed when using temporary files"); 850 851 // If '-working-directory' was passed, the output filename should be 852 // relative to that. 853 std::optional<SmallString<128>> AbsPath; 854 if (OutputPath != "-" && !llvm::sys::path::is_absolute(OutputPath)) { 855 assert(hasFileManager() && 856 "File Manager is required to fix up relative path.\n"); 857 858 AbsPath.emplace(OutputPath); 859 FileMgr->FixupRelativePath(*AbsPath); 860 OutputPath = *AbsPath; 861 } 862 863 std::unique_ptr<llvm::raw_fd_ostream> OS; 864 std::optional<StringRef> OSFile; 865 866 if (UseTemporary) { 867 if (OutputPath == "-") 868 UseTemporary = false; 869 else { 870 llvm::sys::fs::file_status Status; 871 llvm::sys::fs::status(OutputPath, Status); 872 if (llvm::sys::fs::exists(Status)) { 873 // Fail early if we can't write to the final destination. 874 if (!llvm::sys::fs::can_write(OutputPath)) 875 return llvm::errorCodeToError( 876 make_error_code(llvm::errc::operation_not_permitted)); 877 878 // Don't use a temporary if the output is a special file. This handles 879 // things like '-o /dev/null' 880 if (!llvm::sys::fs::is_regular_file(Status)) 881 UseTemporary = false; 882 } 883 } 884 } 885 886 std::optional<llvm::sys::fs::TempFile> Temp; 887 if (UseTemporary) { 888 // Create a temporary file. 889 // Insert -%%%%%%%% before the extension (if any), and because some tools 890 // (noticeable, clang's own GlobalModuleIndex.cpp) glob for build 891 // artifacts, also append .tmp. 892 StringRef OutputExtension = llvm::sys::path::extension(OutputPath); 893 SmallString<128> TempPath = 894 StringRef(OutputPath).drop_back(OutputExtension.size()); 895 TempPath += "-%%%%%%%%"; 896 TempPath += OutputExtension; 897 TempPath += ".tmp"; 898 Expected<llvm::sys::fs::TempFile> ExpectedFile = 899 llvm::sys::fs::TempFile::create( 900 TempPath, llvm::sys::fs::all_read | llvm::sys::fs::all_write, 901 Binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_Text); 902 903 llvm::Error E = handleErrors( 904 ExpectedFile.takeError(), [&](const llvm::ECError &E) -> llvm::Error { 905 std::error_code EC = E.convertToErrorCode(); 906 if (CreateMissingDirectories && 907 EC == llvm::errc::no_such_file_or_directory) { 908 StringRef Parent = llvm::sys::path::parent_path(OutputPath); 909 EC = llvm::sys::fs::create_directories(Parent); 910 if (!EC) { 911 ExpectedFile = llvm::sys::fs::TempFile::create(TempPath); 912 if (!ExpectedFile) 913 return llvm::errorCodeToError( 914 llvm::errc::no_such_file_or_directory); 915 } 916 } 917 return llvm::errorCodeToError(EC); 918 }); 919 920 if (E) { 921 consumeError(std::move(E)); 922 } else { 923 Temp = std::move(ExpectedFile.get()); 924 OS.reset(new llvm::raw_fd_ostream(Temp->FD, /*shouldClose=*/false)); 925 OSFile = Temp->TmpName; 926 } 927 // If we failed to create the temporary, fallback to writing to the file 928 // directly. This handles the corner case where we cannot write to the 929 // directory, but can write to the file. 930 } 931 932 if (!OS) { 933 OSFile = OutputPath; 934 std::error_code EC; 935 OS.reset(new llvm::raw_fd_ostream( 936 *OSFile, EC, 937 (Binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_TextWithCRLF))); 938 if (EC) 939 return llvm::errorCodeToError(EC); 940 } 941 942 // Add the output file -- but don't try to remove "-", since this means we are 943 // using stdin. 944 OutputFiles.emplace_back(((OutputPath != "-") ? OutputPath : "").str(), 945 std::move(Temp)); 946 947 if (!Binary || OS->supportsSeeking()) 948 return std::move(OS); 949 950 return std::make_unique<llvm::buffer_unique_ostream>(std::move(OS)); 951 } 952 953 // Initialization Utilities 954 955 bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){ 956 return InitializeSourceManager(Input, getDiagnostics(), getFileManager(), 957 getSourceManager()); 958 } 959 960 // static 961 bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input, 962 DiagnosticsEngine &Diags, 963 FileManager &FileMgr, 964 SourceManager &SourceMgr) { 965 SrcMgr::CharacteristicKind Kind = 966 Input.getKind().getFormat() == InputKind::ModuleMap 967 ? Input.isSystem() ? SrcMgr::C_System_ModuleMap 968 : SrcMgr::C_User_ModuleMap 969 : Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User; 970 971 if (Input.isBuffer()) { 972 SourceMgr.setMainFileID(SourceMgr.createFileID(Input.getBuffer(), Kind)); 973 assert(SourceMgr.getMainFileID().isValid() && 974 "Couldn't establish MainFileID!"); 975 return true; 976 } 977 978 StringRef InputFile = Input.getFile(); 979 980 // Figure out where to get and map in the main file. 981 auto FileOrErr = InputFile == "-" 982 ? FileMgr.getSTDIN() 983 : FileMgr.getFileRef(InputFile, /*OpenFile=*/true); 984 if (!FileOrErr) { 985 auto EC = llvm::errorToErrorCode(FileOrErr.takeError()); 986 if (InputFile != "-") 987 Diags.Report(diag::err_fe_error_reading) << InputFile << EC.message(); 988 else 989 Diags.Report(diag::err_fe_error_reading_stdin) << EC.message(); 990 return false; 991 } 992 993 SourceMgr.setMainFileID( 994 SourceMgr.createFileID(*FileOrErr, SourceLocation(), Kind)); 995 996 assert(SourceMgr.getMainFileID().isValid() && 997 "Couldn't establish MainFileID!"); 998 return true; 999 } 1000 1001 // High-Level Operations 1002 1003 bool CompilerInstance::ExecuteAction(FrontendAction &Act) { 1004 assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); 1005 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); 1006 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); 1007 1008 // Mark this point as the bottom of the stack if we don't have somewhere 1009 // better. We generally expect frontend actions to be invoked with (nearly) 1010 // DesiredStackSpace available. 1011 noteBottomOfStack(); 1012 1013 auto FinishDiagnosticClient = llvm::make_scope_exit([&]() { 1014 // Notify the diagnostic client that all files were processed. 1015 getDiagnosticClient().finish(); 1016 }); 1017 1018 raw_ostream &OS = getVerboseOutputStream(); 1019 1020 if (!Act.PrepareToExecute(*this)) 1021 return false; 1022 1023 if (!createTarget()) 1024 return false; 1025 1026 // rewriter project will change target built-in bool type from its default. 1027 if (getFrontendOpts().ProgramAction == frontend::RewriteObjC) 1028 getTarget().noSignedCharForObjCBool(); 1029 1030 // Validate/process some options. 1031 if (getHeaderSearchOpts().Verbose) 1032 OS << "clang -cc1 version " CLANG_VERSION_STRING << " based upon LLVM " 1033 << LLVM_VERSION_STRING << " default target " 1034 << llvm::sys::getDefaultTargetTriple() << "\n"; 1035 1036 if (getCodeGenOpts().TimePasses) 1037 createFrontendTimer(); 1038 1039 if (getFrontendOpts().ShowStats || !getFrontendOpts().StatsFile.empty()) 1040 llvm::EnableStatistics(false); 1041 1042 for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) { 1043 // Reset the ID tables if we are reusing the SourceManager and parsing 1044 // regular files. 1045 if (hasSourceManager() && !Act.isModelParsingAction()) 1046 getSourceManager().clearIDTables(); 1047 1048 if (Act.BeginSourceFile(*this, FIF)) { 1049 if (llvm::Error Err = Act.Execute()) { 1050 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 1051 } 1052 Act.EndSourceFile(); 1053 } 1054 } 1055 1056 if (getDiagnosticOpts().ShowCarets) { 1057 // We can have multiple diagnostics sharing one diagnostic client. 1058 // Get the total number of warnings/errors from the client. 1059 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); 1060 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); 1061 1062 if (NumWarnings) 1063 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); 1064 if (NumWarnings && NumErrors) 1065 OS << " and "; 1066 if (NumErrors) 1067 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); 1068 if (NumWarnings || NumErrors) { 1069 OS << " generated"; 1070 if (getLangOpts().CUDA) { 1071 if (!getLangOpts().CUDAIsDevice) { 1072 OS << " when compiling for host"; 1073 } else { 1074 OS << " when compiling for " << getTargetOpts().CPU; 1075 } 1076 } 1077 OS << ".\n"; 1078 } 1079 } 1080 1081 if (getFrontendOpts().ShowStats) { 1082 if (hasFileManager()) { 1083 getFileManager().PrintStats(); 1084 OS << '\n'; 1085 } 1086 llvm::PrintStatistics(OS); 1087 } 1088 StringRef StatsFile = getFrontendOpts().StatsFile; 1089 if (!StatsFile.empty()) { 1090 llvm::sys::fs::OpenFlags FileFlags = llvm::sys::fs::OF_TextWithCRLF; 1091 if (getFrontendOpts().AppendStats) 1092 FileFlags |= llvm::sys::fs::OF_Append; 1093 std::error_code EC; 1094 auto StatS = 1095 std::make_unique<llvm::raw_fd_ostream>(StatsFile, EC, FileFlags); 1096 if (EC) { 1097 getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file) 1098 << StatsFile << EC.message(); 1099 } else { 1100 llvm::PrintStatisticsJSON(*StatS); 1101 } 1102 } 1103 1104 return !getDiagnostics().getClient()->getNumErrors(); 1105 } 1106 1107 void CompilerInstance::LoadRequestedPlugins() { 1108 // Load any requested plugins. 1109 for (const std::string &Path : getFrontendOpts().Plugins) { 1110 std::string Error; 1111 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error)) 1112 getDiagnostics().Report(diag::err_fe_unable_to_load_plugin) 1113 << Path << Error; 1114 } 1115 1116 // Check if any of the loaded plugins replaces the main AST action 1117 for (const FrontendPluginRegistry::entry &Plugin : 1118 FrontendPluginRegistry::entries()) { 1119 std::unique_ptr<PluginASTAction> P(Plugin.instantiate()); 1120 if (P->getActionType() == PluginASTAction::ReplaceAction) { 1121 getFrontendOpts().ProgramAction = clang::frontend::PluginAction; 1122 getFrontendOpts().ActionName = Plugin.getName().str(); 1123 break; 1124 } 1125 } 1126 } 1127 1128 /// Determine the appropriate source input kind based on language 1129 /// options. 1130 static Language getLanguageFromOptions(const LangOptions &LangOpts) { 1131 if (LangOpts.OpenCL) 1132 return Language::OpenCL; 1133 if (LangOpts.CUDA) 1134 return Language::CUDA; 1135 if (LangOpts.ObjC) 1136 return LangOpts.CPlusPlus ? Language::ObjCXX : Language::ObjC; 1137 return LangOpts.CPlusPlus ? Language::CXX : Language::C; 1138 } 1139 1140 /// Compile a module file for the given module, using the options 1141 /// provided by the importing compiler instance. Returns true if the module 1142 /// was built without errors. 1143 static bool 1144 compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, 1145 StringRef ModuleName, FrontendInputFile Input, 1146 StringRef OriginalModuleMapFile, StringRef ModuleFileName, 1147 llvm::function_ref<void(CompilerInstance &)> PreBuildStep = 1148 [](CompilerInstance &) {}, 1149 llvm::function_ref<void(CompilerInstance &)> PostBuildStep = 1150 [](CompilerInstance &) {}) { 1151 llvm::TimeTraceScope TimeScope("Module Compile", ModuleName); 1152 1153 // Never compile a module that's already finalized - this would cause the 1154 // existing module to be freed, causing crashes if it is later referenced 1155 if (ImportingInstance.getModuleCache().isPCMFinal(ModuleFileName)) { 1156 ImportingInstance.getDiagnostics().Report( 1157 ImportLoc, diag::err_module_rebuild_finalized) 1158 << ModuleName; 1159 return false; 1160 } 1161 1162 // Construct a compiler invocation for creating this module. 1163 auto Invocation = 1164 std::make_shared<CompilerInvocation>(ImportingInstance.getInvocation()); 1165 1166 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); 1167 1168 // For any options that aren't intended to affect how a module is built, 1169 // reset them to their default values. 1170 Invocation->resetNonModularOptions(); 1171 1172 // Remove any macro definitions that are explicitly ignored by the module. 1173 // They aren't supposed to affect how the module is built anyway. 1174 HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts(); 1175 llvm::erase_if(PPOpts.Macros, 1176 [&HSOpts](const std::pair<std::string, bool> &def) { 1177 StringRef MacroDef = def.first; 1178 return HSOpts.ModulesIgnoreMacros.contains( 1179 llvm::CachedHashString(MacroDef.split('=').first)); 1180 }); 1181 1182 // If the original compiler invocation had -fmodule-name, pass it through. 1183 Invocation->getLangOpts()->ModuleName = 1184 ImportingInstance.getInvocation().getLangOpts()->ModuleName; 1185 1186 // Note the name of the module we're building. 1187 Invocation->getLangOpts()->CurrentModule = std::string(ModuleName); 1188 1189 // Make sure that the failed-module structure has been allocated in 1190 // the importing instance, and propagate the pointer to the newly-created 1191 // instance. 1192 PreprocessorOptions &ImportingPPOpts 1193 = ImportingInstance.getInvocation().getPreprocessorOpts(); 1194 if (!ImportingPPOpts.FailedModules) 1195 ImportingPPOpts.FailedModules = 1196 std::make_shared<PreprocessorOptions::FailedModulesSet>(); 1197 PPOpts.FailedModules = ImportingPPOpts.FailedModules; 1198 1199 // If there is a module map file, build the module using the module map. 1200 // Set up the inputs/outputs so that we build the module from its umbrella 1201 // header. 1202 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); 1203 FrontendOpts.OutputFile = ModuleFileName.str(); 1204 FrontendOpts.DisableFree = false; 1205 FrontendOpts.GenerateGlobalModuleIndex = false; 1206 FrontendOpts.BuildingImplicitModule = true; 1207 FrontendOpts.OriginalModuleMap = std::string(OriginalModuleMapFile); 1208 // Force implicitly-built modules to hash the content of the module file. 1209 HSOpts.ModulesHashContent = true; 1210 FrontendOpts.Inputs = {Input}; 1211 1212 // Don't free the remapped file buffers; they are owned by our caller. 1213 PPOpts.RetainRemappedFileBuffers = true; 1214 1215 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; 1216 assert(ImportingInstance.getInvocation().getModuleHash() == 1217 Invocation->getModuleHash() && "Module hash mismatch!"); 1218 1219 // Construct a compiler instance that will be used to actually create the 1220 // module. Since we're sharing an in-memory module cache, 1221 // CompilerInstance::CompilerInstance is responsible for finalizing the 1222 // buffers to prevent use-after-frees. 1223 CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(), 1224 &ImportingInstance.getModuleCache()); 1225 auto &Inv = *Invocation; 1226 Instance.setInvocation(std::move(Invocation)); 1227 1228 Instance.createDiagnostics(new ForwardingDiagnosticConsumer( 1229 ImportingInstance.getDiagnosticClient()), 1230 /*ShouldOwnClient=*/true); 1231 1232 if (FrontendOpts.ModulesShareFileManager) { 1233 Instance.setFileManager(&ImportingInstance.getFileManager()); 1234 } else { 1235 Instance.createFileManager(&ImportingInstance.getVirtualFileSystem()); 1236 } 1237 Instance.createSourceManager(Instance.getFileManager()); 1238 SourceManager &SourceMgr = Instance.getSourceManager(); 1239 1240 // Note that this module is part of the module build stack, so that we 1241 // can detect cycles in the module graph. 1242 SourceMgr.setModuleBuildStack( 1243 ImportingInstance.getSourceManager().getModuleBuildStack()); 1244 SourceMgr.pushModuleBuildStack(ModuleName, 1245 FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager())); 1246 1247 // If we're collecting module dependencies, we need to share a collector 1248 // between all of the module CompilerInstances. Other than that, we don't 1249 // want to produce any dependency output from the module build. 1250 Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector()); 1251 Inv.getDependencyOutputOpts() = DependencyOutputOptions(); 1252 1253 ImportingInstance.getDiagnostics().Report(ImportLoc, 1254 diag::remark_module_build) 1255 << ModuleName << ModuleFileName; 1256 1257 PreBuildStep(Instance); 1258 1259 // Execute the action to actually build the module in-place. Use a separate 1260 // thread so that we get a stack large enough. 1261 bool Crashed = !llvm::CrashRecoveryContext().RunSafelyOnThread( 1262 [&]() { 1263 GenerateModuleFromModuleMapAction Action; 1264 Instance.ExecuteAction(Action); 1265 }, 1266 DesiredStackSize); 1267 1268 PostBuildStep(Instance); 1269 1270 ImportingInstance.getDiagnostics().Report(ImportLoc, 1271 diag::remark_module_build_done) 1272 << ModuleName; 1273 1274 if (Crashed) { 1275 // Clear the ASTConsumer if it hasn't been already, in case it owns streams 1276 // that must be closed before clearing output files. 1277 Instance.setSema(nullptr); 1278 Instance.setASTConsumer(nullptr); 1279 1280 // Delete any remaining temporary files related to Instance. 1281 Instance.clearOutputFiles(/*EraseFiles=*/true); 1282 } 1283 1284 // If \p AllowPCMWithCompilerErrors is set return 'success' even if errors 1285 // occurred. 1286 return !Instance.getDiagnostics().hasErrorOccurred() || 1287 Instance.getFrontendOpts().AllowPCMWithCompilerErrors; 1288 } 1289 1290 static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File, 1291 FileManager &FileMgr) { 1292 StringRef Filename = llvm::sys::path::filename(File.getName()); 1293 SmallString<128> PublicFilename(File.getDir().getName()); 1294 if (Filename == "module_private.map") 1295 llvm::sys::path::append(PublicFilename, "module.map"); 1296 else if (Filename == "module.private.modulemap") 1297 llvm::sys::path::append(PublicFilename, "module.modulemap"); 1298 else 1299 return std::nullopt; 1300 return FileMgr.getOptionalFileRef(PublicFilename); 1301 } 1302 1303 /// Compile a module file for the given module in a separate compiler instance, 1304 /// using the options provided by the importing compiler instance. Returns true 1305 /// if the module was built without errors. 1306 static bool compileModule(CompilerInstance &ImportingInstance, 1307 SourceLocation ImportLoc, Module *Module, 1308 StringRef ModuleFileName) { 1309 InputKind IK(getLanguageFromOptions(ImportingInstance.getLangOpts()), 1310 InputKind::ModuleMap); 1311 1312 // Get or create the module map that we'll use to build this module. 1313 ModuleMap &ModMap 1314 = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1315 bool Result; 1316 if (OptionalFileEntryRef ModuleMapFile = 1317 ModMap.getContainingModuleMapFile(Module)) { 1318 // Canonicalize compilation to start with the public module map. This is 1319 // vital for submodules declarations in the private module maps to be 1320 // correctly parsed when depending on a top level module in the public one. 1321 if (OptionalFileEntryRef PublicMMFile = getPublicModuleMap( 1322 *ModuleMapFile, ImportingInstance.getFileManager())) 1323 ModuleMapFile = PublicMMFile; 1324 1325 StringRef ModuleMapFilePath = ModuleMapFile->getNameAsRequested(); 1326 1327 // Use the module map where this module resides. 1328 Result = compileModuleImpl( 1329 ImportingInstance, ImportLoc, Module->getTopLevelModuleName(), 1330 FrontendInputFile(ModuleMapFilePath, IK, +Module->IsSystem), 1331 ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName); 1332 } else { 1333 // FIXME: We only need to fake up an input file here as a way of 1334 // transporting the module's directory to the module map parser. We should 1335 // be able to do that more directly, and parse from a memory buffer without 1336 // inventing this file. 1337 SmallString<128> FakeModuleMapFile(Module->Directory->getName()); 1338 llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map"); 1339 1340 std::string InferredModuleMapContent; 1341 llvm::raw_string_ostream OS(InferredModuleMapContent); 1342 Module->print(OS); 1343 OS.flush(); 1344 1345 Result = compileModuleImpl( 1346 ImportingInstance, ImportLoc, Module->getTopLevelModuleName(), 1347 FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem), 1348 ModMap.getModuleMapFileForUniquing(Module)->getName(), 1349 ModuleFileName, 1350 [&](CompilerInstance &Instance) { 1351 std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer = 1352 llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent); 1353 const FileEntry *ModuleMapFile = Instance.getFileManager().getVirtualFile( 1354 FakeModuleMapFile, InferredModuleMapContent.size(), 0); 1355 Instance.getSourceManager().overrideFileContents( 1356 ModuleMapFile, std::move(ModuleMapBuffer)); 1357 }); 1358 } 1359 1360 // We've rebuilt a module. If we're allowed to generate or update the global 1361 // module index, record that fact in the importing compiler instance. 1362 if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) { 1363 ImportingInstance.setBuildGlobalModuleIndex(true); 1364 } 1365 1366 return Result; 1367 } 1368 1369 /// Read the AST right after compiling the module. 1370 static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance, 1371 SourceLocation ImportLoc, 1372 SourceLocation ModuleNameLoc, 1373 Module *Module, StringRef ModuleFileName, 1374 bool *OutOfDate) { 1375 DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics(); 1376 1377 unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing; 1378 if (OutOfDate) 1379 ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate; 1380 1381 // Try to read the module file, now that we've compiled it. 1382 ASTReader::ASTReadResult ReadResult = 1383 ImportingInstance.getASTReader()->ReadAST( 1384 ModuleFileName, serialization::MK_ImplicitModule, ImportLoc, 1385 ModuleLoadCapabilities); 1386 if (ReadResult == ASTReader::Success) 1387 return true; 1388 1389 // The caller wants to handle out-of-date failures. 1390 if (OutOfDate && ReadResult == ASTReader::OutOfDate) { 1391 *OutOfDate = true; 1392 return false; 1393 } 1394 1395 // The ASTReader didn't diagnose the error, so conservatively report it. 1396 if (ReadResult == ASTReader::Missing || !Diags.hasErrorOccurred()) 1397 Diags.Report(ModuleNameLoc, diag::err_module_not_built) 1398 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc); 1399 1400 return false; 1401 } 1402 1403 /// Compile a module in a separate compiler instance and read the AST, 1404 /// returning true if the module compiles without errors. 1405 static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance, 1406 SourceLocation ImportLoc, 1407 SourceLocation ModuleNameLoc, 1408 Module *Module, 1409 StringRef ModuleFileName) { 1410 if (!compileModule(ImportingInstance, ModuleNameLoc, Module, 1411 ModuleFileName)) { 1412 ImportingInstance.getDiagnostics().Report(ModuleNameLoc, 1413 diag::err_module_not_built) 1414 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc); 1415 return false; 1416 } 1417 1418 return readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc, 1419 Module, ModuleFileName, 1420 /*OutOfDate=*/nullptr); 1421 } 1422 1423 /// Compile a module in a separate compiler instance and read the AST, 1424 /// returning true if the module compiles without errors, using a lock manager 1425 /// to avoid building the same module in multiple compiler instances. 1426 /// 1427 /// Uses a lock file manager and exponential backoff to reduce the chances that 1428 /// multiple instances will compete to create the same module. On timeout, 1429 /// deletes the lock file in order to avoid deadlock from crashing processes or 1430 /// bugs in the lock file manager. 1431 static bool compileModuleAndReadASTBehindLock( 1432 CompilerInstance &ImportingInstance, SourceLocation ImportLoc, 1433 SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName) { 1434 DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics(); 1435 1436 Diags.Report(ModuleNameLoc, diag::remark_module_lock) 1437 << ModuleFileName << Module->Name; 1438 1439 // FIXME: have LockFileManager return an error_code so that we can 1440 // avoid the mkdir when the directory already exists. 1441 StringRef Dir = llvm::sys::path::parent_path(ModuleFileName); 1442 llvm::sys::fs::create_directories(Dir); 1443 1444 while (true) { 1445 llvm::LockFileManager Locked(ModuleFileName); 1446 switch (Locked) { 1447 case llvm::LockFileManager::LFS_Error: 1448 // ModuleCache takes care of correctness and locks are only necessary for 1449 // performance. Fallback to building the module in case of any lock 1450 // related errors. 1451 Diags.Report(ModuleNameLoc, diag::remark_module_lock_failure) 1452 << Module->Name << Locked.getErrorMessage(); 1453 // Clear out any potential leftover. 1454 Locked.unsafeRemoveLockFile(); 1455 [[fallthrough]]; 1456 case llvm::LockFileManager::LFS_Owned: 1457 // We're responsible for building the module ourselves. 1458 return compileModuleAndReadASTImpl(ImportingInstance, ImportLoc, 1459 ModuleNameLoc, Module, ModuleFileName); 1460 1461 case llvm::LockFileManager::LFS_Shared: 1462 break; // The interesting case. 1463 } 1464 1465 // Someone else is responsible for building the module. Wait for them to 1466 // finish. 1467 switch (Locked.waitForUnlock()) { 1468 case llvm::LockFileManager::Res_Success: 1469 break; // The interesting case. 1470 case llvm::LockFileManager::Res_OwnerDied: 1471 continue; // try again to get the lock. 1472 case llvm::LockFileManager::Res_Timeout: 1473 // Since ModuleCache takes care of correctness, we try waiting for 1474 // another process to complete the build so clang does not do it done 1475 // twice. If case of timeout, build it ourselves. 1476 Diags.Report(ModuleNameLoc, diag::remark_module_lock_timeout) 1477 << Module->Name; 1478 // Clear the lock file so that future invocations can make progress. 1479 Locked.unsafeRemoveLockFile(); 1480 continue; 1481 } 1482 1483 // Read the module that was just written by someone else. 1484 bool OutOfDate = false; 1485 if (readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc, 1486 Module, ModuleFileName, &OutOfDate)) 1487 return true; 1488 if (!OutOfDate) 1489 return false; 1490 1491 // The module may be out of date in the presence of file system races, 1492 // or if one of its imports depends on header search paths that are not 1493 // consistent with this ImportingInstance. Try again... 1494 } 1495 } 1496 1497 /// Compile a module in a separate compiler instance and read the AST, 1498 /// returning true if the module compiles without errors, potentially using a 1499 /// lock manager to avoid building the same module in multiple compiler 1500 /// instances. 1501 static bool compileModuleAndReadAST(CompilerInstance &ImportingInstance, 1502 SourceLocation ImportLoc, 1503 SourceLocation ModuleNameLoc, 1504 Module *Module, StringRef ModuleFileName) { 1505 return ImportingInstance.getInvocation() 1506 .getFrontendOpts() 1507 .BuildingImplicitModuleUsesLock 1508 ? compileModuleAndReadASTBehindLock(ImportingInstance, ImportLoc, 1509 ModuleNameLoc, Module, 1510 ModuleFileName) 1511 : compileModuleAndReadASTImpl(ImportingInstance, ImportLoc, 1512 ModuleNameLoc, Module, 1513 ModuleFileName); 1514 } 1515 1516 /// Diagnose differences between the current definition of the given 1517 /// configuration macro and the definition provided on the command line. 1518 static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro, 1519 Module *Mod, SourceLocation ImportLoc) { 1520 IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro); 1521 SourceManager &SourceMgr = PP.getSourceManager(); 1522 1523 // If this identifier has never had a macro definition, then it could 1524 // not have changed. 1525 if (!Id->hadMacroDefinition()) 1526 return; 1527 auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id); 1528 1529 // Find the macro definition from the command line. 1530 MacroInfo *CmdLineDefinition = nullptr; 1531 for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) { 1532 // We only care about the predefines buffer. 1533 FileID FID = SourceMgr.getFileID(MD->getLocation()); 1534 if (FID.isInvalid() || FID != PP.getPredefinesFileID()) 1535 continue; 1536 if (auto *DMD = dyn_cast<DefMacroDirective>(MD)) 1537 CmdLineDefinition = DMD->getMacroInfo(); 1538 break; 1539 } 1540 1541 auto *CurrentDefinition = PP.getMacroInfo(Id); 1542 if (CurrentDefinition == CmdLineDefinition) { 1543 // Macro matches. Nothing to do. 1544 } else if (!CurrentDefinition) { 1545 // This macro was defined on the command line, then #undef'd later. 1546 // Complain. 1547 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) 1548 << true << ConfigMacro << Mod->getFullModuleName(); 1549 auto LatestDef = LatestLocalMD->getDefinition(); 1550 assert(LatestDef.isUndefined() && 1551 "predefined macro went away with no #undef?"); 1552 PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here) 1553 << true; 1554 return; 1555 } else if (!CmdLineDefinition) { 1556 // There was no definition for this macro in the predefines buffer, 1557 // but there was a local definition. Complain. 1558 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) 1559 << false << ConfigMacro << Mod->getFullModuleName(); 1560 PP.Diag(CurrentDefinition->getDefinitionLoc(), 1561 diag::note_module_def_undef_here) 1562 << false; 1563 } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP, 1564 /*Syntactically=*/true)) { 1565 // The macro definitions differ. 1566 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) 1567 << false << ConfigMacro << Mod->getFullModuleName(); 1568 PP.Diag(CurrentDefinition->getDefinitionLoc(), 1569 diag::note_module_def_undef_here) 1570 << false; 1571 } 1572 } 1573 1574 /// Write a new timestamp file with the given path. 1575 static void writeTimestampFile(StringRef TimestampFile) { 1576 std::error_code EC; 1577 llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::OF_None); 1578 } 1579 1580 /// Prune the module cache of modules that haven't been accessed in 1581 /// a long time. 1582 static void pruneModuleCache(const HeaderSearchOptions &HSOpts) { 1583 llvm::sys::fs::file_status StatBuf; 1584 llvm::SmallString<128> TimestampFile; 1585 TimestampFile = HSOpts.ModuleCachePath; 1586 assert(!TimestampFile.empty()); 1587 llvm::sys::path::append(TimestampFile, "modules.timestamp"); 1588 1589 // Try to stat() the timestamp file. 1590 if (std::error_code EC = llvm::sys::fs::status(TimestampFile, StatBuf)) { 1591 // If the timestamp file wasn't there, create one now. 1592 if (EC == std::errc::no_such_file_or_directory) { 1593 writeTimestampFile(TimestampFile); 1594 } 1595 return; 1596 } 1597 1598 // Check whether the time stamp is older than our pruning interval. 1599 // If not, do nothing. 1600 time_t TimeStampModTime = 1601 llvm::sys::toTimeT(StatBuf.getLastModificationTime()); 1602 time_t CurrentTime = time(nullptr); 1603 if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval)) 1604 return; 1605 1606 // Write a new timestamp file so that nobody else attempts to prune. 1607 // There is a benign race condition here, if two Clang instances happen to 1608 // notice at the same time that the timestamp is out-of-date. 1609 writeTimestampFile(TimestampFile); 1610 1611 // Walk the entire module cache, looking for unused module files and module 1612 // indices. 1613 std::error_code EC; 1614 SmallString<128> ModuleCachePathNative; 1615 llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative); 1616 for (llvm::sys::fs::directory_iterator Dir(ModuleCachePathNative, EC), DirEnd; 1617 Dir != DirEnd && !EC; Dir.increment(EC)) { 1618 // If we don't have a directory, there's nothing to look into. 1619 if (!llvm::sys::fs::is_directory(Dir->path())) 1620 continue; 1621 1622 // Walk all of the files within this directory. 1623 for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd; 1624 File != FileEnd && !EC; File.increment(EC)) { 1625 // We only care about module and global module index files. 1626 StringRef Extension = llvm::sys::path::extension(File->path()); 1627 if (Extension != ".pcm" && Extension != ".timestamp" && 1628 llvm::sys::path::filename(File->path()) != "modules.idx") 1629 continue; 1630 1631 // Look at this file. If we can't stat it, there's nothing interesting 1632 // there. 1633 if (llvm::sys::fs::status(File->path(), StatBuf)) 1634 continue; 1635 1636 // If the file has been used recently enough, leave it there. 1637 time_t FileAccessTime = llvm::sys::toTimeT(StatBuf.getLastAccessedTime()); 1638 if (CurrentTime - FileAccessTime <= 1639 time_t(HSOpts.ModuleCachePruneAfter)) { 1640 continue; 1641 } 1642 1643 // Remove the file. 1644 llvm::sys::fs::remove(File->path()); 1645 1646 // Remove the timestamp file. 1647 std::string TimpestampFilename = File->path() + ".timestamp"; 1648 llvm::sys::fs::remove(TimpestampFilename); 1649 } 1650 1651 // If we removed all of the files in the directory, remove the directory 1652 // itself. 1653 if (llvm::sys::fs::directory_iterator(Dir->path(), EC) == 1654 llvm::sys::fs::directory_iterator() && !EC) 1655 llvm::sys::fs::remove(Dir->path()); 1656 } 1657 } 1658 1659 void CompilerInstance::createASTReader() { 1660 if (TheASTReader) 1661 return; 1662 1663 if (!hasASTContext()) 1664 createASTContext(); 1665 1666 // If we're implicitly building modules but not currently recursively 1667 // building a module, check whether we need to prune the module cache. 1668 if (getSourceManager().getModuleBuildStack().empty() && 1669 !getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty() && 1670 getHeaderSearchOpts().ModuleCachePruneInterval > 0 && 1671 getHeaderSearchOpts().ModuleCachePruneAfter > 0) { 1672 pruneModuleCache(getHeaderSearchOpts()); 1673 } 1674 1675 HeaderSearchOptions &HSOpts = getHeaderSearchOpts(); 1676 std::string Sysroot = HSOpts.Sysroot; 1677 const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 1678 const FrontendOptions &FEOpts = getFrontendOpts(); 1679 std::unique_ptr<llvm::Timer> ReadTimer; 1680 1681 if (FrontendTimerGroup) 1682 ReadTimer = std::make_unique<llvm::Timer>("reading_modules", 1683 "Reading modules", 1684 *FrontendTimerGroup); 1685 TheASTReader = new ASTReader( 1686 getPreprocessor(), getModuleCache(), &getASTContext(), 1687 getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions, 1688 Sysroot.empty() ? "" : Sysroot.c_str(), 1689 PPOpts.DisablePCHOrModuleValidation, 1690 /*AllowASTWithCompilerErrors=*/FEOpts.AllowPCMWithCompilerErrors, 1691 /*AllowConfigurationMismatch=*/false, HSOpts.ModulesValidateSystemHeaders, 1692 HSOpts.ValidateASTInputFilesContent, 1693 getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer)); 1694 if (hasASTConsumer()) { 1695 TheASTReader->setDeserializationListener( 1696 getASTConsumer().GetASTDeserializationListener()); 1697 getASTContext().setASTMutationListener( 1698 getASTConsumer().GetASTMutationListener()); 1699 } 1700 getASTContext().setExternalSource(TheASTReader); 1701 if (hasSema()) 1702 TheASTReader->InitializeSema(getSema()); 1703 if (hasASTConsumer()) 1704 TheASTReader->StartTranslationUnit(&getASTConsumer()); 1705 1706 for (auto &Listener : DependencyCollectors) 1707 Listener->attachToASTReader(*TheASTReader); 1708 } 1709 1710 bool CompilerInstance::loadModuleFile(StringRef FileName) { 1711 llvm::Timer Timer; 1712 if (FrontendTimerGroup) 1713 Timer.init("preloading." + FileName.str(), "Preloading " + FileName.str(), 1714 *FrontendTimerGroup); 1715 llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr); 1716 1717 // If we don't already have an ASTReader, create one now. 1718 if (!TheASTReader) 1719 createASTReader(); 1720 1721 // If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the 1722 // ASTReader to diagnose it, since it can produce better errors that we can. 1723 bool ConfigMismatchIsRecoverable = 1724 getDiagnostics().getDiagnosticLevel(diag::warn_module_config_mismatch, 1725 SourceLocation()) 1726 <= DiagnosticsEngine::Warning; 1727 1728 auto Listener = std::make_unique<ReadModuleNames>(*PP); 1729 auto &ListenerRef = *Listener; 1730 ASTReader::ListenerScope ReadModuleNamesListener(*TheASTReader, 1731 std::move(Listener)); 1732 1733 // Try to load the module file. 1734 switch (TheASTReader->ReadAST( 1735 FileName, serialization::MK_ExplicitModule, SourceLocation(), 1736 ConfigMismatchIsRecoverable ? ASTReader::ARR_ConfigurationMismatch : 0)) { 1737 case ASTReader::Success: 1738 // We successfully loaded the module file; remember the set of provided 1739 // modules so that we don't try to load implicit modules for them. 1740 ListenerRef.registerAll(); 1741 return true; 1742 1743 case ASTReader::ConfigurationMismatch: 1744 // Ignore unusable module files. 1745 getDiagnostics().Report(SourceLocation(), diag::warn_module_config_mismatch) 1746 << FileName; 1747 // All modules provided by any files we tried and failed to load are now 1748 // unavailable; includes of those modules should now be handled textually. 1749 ListenerRef.markAllUnavailable(); 1750 return true; 1751 1752 default: 1753 return false; 1754 } 1755 } 1756 1757 namespace { 1758 enum ModuleSource { 1759 MS_ModuleNotFound, 1760 MS_ModuleCache, 1761 MS_PrebuiltModulePath, 1762 MS_ModuleBuildPragma 1763 }; 1764 } // end namespace 1765 1766 /// Select a source for loading the named module and compute the filename to 1767 /// load it from. 1768 static ModuleSource selectModuleSource( 1769 Module *M, StringRef ModuleName, std::string &ModuleFilename, 1770 const std::map<std::string, std::string, std::less<>> &BuiltModules, 1771 HeaderSearch &HS) { 1772 assert(ModuleFilename.empty() && "Already has a module source?"); 1773 1774 // Check to see if the module has been built as part of this compilation 1775 // via a module build pragma. 1776 auto BuiltModuleIt = BuiltModules.find(ModuleName); 1777 if (BuiltModuleIt != BuiltModules.end()) { 1778 ModuleFilename = BuiltModuleIt->second; 1779 return MS_ModuleBuildPragma; 1780 } 1781 1782 // Try to load the module from the prebuilt module path. 1783 const HeaderSearchOptions &HSOpts = HS.getHeaderSearchOpts(); 1784 if (!HSOpts.PrebuiltModuleFiles.empty() || 1785 !HSOpts.PrebuiltModulePaths.empty()) { 1786 ModuleFilename = HS.getPrebuiltModuleFileName(ModuleName); 1787 if (HSOpts.EnablePrebuiltImplicitModules && ModuleFilename.empty()) 1788 ModuleFilename = HS.getPrebuiltImplicitModuleFileName(M); 1789 if (!ModuleFilename.empty()) 1790 return MS_PrebuiltModulePath; 1791 } 1792 1793 // Try to load the module from the module cache. 1794 if (M) { 1795 ModuleFilename = HS.getCachedModuleFileName(M); 1796 return MS_ModuleCache; 1797 } 1798 1799 return MS_ModuleNotFound; 1800 } 1801 1802 ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST( 1803 StringRef ModuleName, SourceLocation ImportLoc, 1804 SourceLocation ModuleNameLoc, bool IsInclusionDirective) { 1805 // Search for a module with the given name. 1806 HeaderSearch &HS = PP->getHeaderSearchInfo(); 1807 Module *M = 1808 HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective); 1809 1810 // Select the source and filename for loading the named module. 1811 std::string ModuleFilename; 1812 ModuleSource Source = 1813 selectModuleSource(M, ModuleName, ModuleFilename, BuiltModules, HS); 1814 if (Source == MS_ModuleNotFound) { 1815 // We can't find a module, error out here. 1816 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found) 1817 << ModuleName << SourceRange(ImportLoc, ModuleNameLoc); 1818 return nullptr; 1819 } 1820 if (ModuleFilename.empty()) { 1821 if (M && M->HasIncompatibleModuleFile) { 1822 // We tried and failed to load a module file for this module. Fall 1823 // back to textual inclusion for its headers. 1824 return ModuleLoadResult::ConfigMismatch; 1825 } 1826 1827 getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled) 1828 << ModuleName; 1829 return nullptr; 1830 } 1831 1832 // Create an ASTReader on demand. 1833 if (!getASTReader()) 1834 createASTReader(); 1835 1836 // Time how long it takes to load the module. 1837 llvm::Timer Timer; 1838 if (FrontendTimerGroup) 1839 Timer.init("loading." + ModuleFilename, "Loading " + ModuleFilename, 1840 *FrontendTimerGroup); 1841 llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr); 1842 llvm::TimeTraceScope TimeScope("Module Load", ModuleName); 1843 1844 // Try to load the module file. If we are not trying to load from the 1845 // module cache, we don't know how to rebuild modules. 1846 unsigned ARRFlags = Source == MS_ModuleCache 1847 ? ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing | 1848 ASTReader::ARR_TreatModuleWithErrorsAsOutOfDate 1849 : Source == MS_PrebuiltModulePath 1850 ? 0 1851 : ASTReader::ARR_ConfigurationMismatch; 1852 switch (getASTReader()->ReadAST(ModuleFilename, 1853 Source == MS_PrebuiltModulePath 1854 ? serialization::MK_PrebuiltModule 1855 : Source == MS_ModuleBuildPragma 1856 ? serialization::MK_ExplicitModule 1857 : serialization::MK_ImplicitModule, 1858 ImportLoc, ARRFlags)) { 1859 case ASTReader::Success: { 1860 if (M) 1861 return M; 1862 assert(Source != MS_ModuleCache && 1863 "missing module, but file loaded from cache"); 1864 1865 // A prebuilt module is indexed as a ModuleFile; the Module does not exist 1866 // until the first call to ReadAST. Look it up now. 1867 M = HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective); 1868 1869 // Check whether M refers to the file in the prebuilt module path. 1870 if (M && M->getASTFile()) 1871 if (auto ModuleFile = FileMgr->getFile(ModuleFilename)) 1872 if (*ModuleFile == M->getASTFile()) 1873 return M; 1874 1875 getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt) 1876 << ModuleName; 1877 return ModuleLoadResult(); 1878 } 1879 1880 case ASTReader::OutOfDate: 1881 case ASTReader::Missing: 1882 // The most interesting case. 1883 break; 1884 1885 case ASTReader::ConfigurationMismatch: 1886 if (Source == MS_PrebuiltModulePath) 1887 // FIXME: We shouldn't be setting HadFatalFailure below if we only 1888 // produce a warning here! 1889 getDiagnostics().Report(SourceLocation(), 1890 diag::warn_module_config_mismatch) 1891 << ModuleFilename; 1892 // Fall through to error out. 1893 [[fallthrough]]; 1894 case ASTReader::VersionMismatch: 1895 case ASTReader::HadErrors: 1896 ModuleLoader::HadFatalFailure = true; 1897 // FIXME: The ASTReader will already have complained, but can we shoehorn 1898 // that diagnostic information into a more useful form? 1899 return ModuleLoadResult(); 1900 1901 case ASTReader::Failure: 1902 ModuleLoader::HadFatalFailure = true; 1903 return ModuleLoadResult(); 1904 } 1905 1906 // ReadAST returned Missing or OutOfDate. 1907 if (Source != MS_ModuleCache) { 1908 // We don't know the desired configuration for this module and don't 1909 // necessarily even have a module map. Since ReadAST already produces 1910 // diagnostics for these two cases, we simply error out here. 1911 return ModuleLoadResult(); 1912 } 1913 1914 // The module file is missing or out-of-date. Build it. 1915 assert(M && "missing module, but trying to compile for cache"); 1916 1917 // Check whether there is a cycle in the module graph. 1918 ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack(); 1919 ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end(); 1920 for (; Pos != PosEnd; ++Pos) { 1921 if (Pos->first == ModuleName) 1922 break; 1923 } 1924 1925 if (Pos != PosEnd) { 1926 SmallString<256> CyclePath; 1927 for (; Pos != PosEnd; ++Pos) { 1928 CyclePath += Pos->first; 1929 CyclePath += " -> "; 1930 } 1931 CyclePath += ModuleName; 1932 1933 getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle) 1934 << ModuleName << CyclePath; 1935 return nullptr; 1936 } 1937 1938 // Check whether we have already attempted to build this module (but 1939 // failed). 1940 if (getPreprocessorOpts().FailedModules && 1941 getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) { 1942 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built) 1943 << ModuleName << SourceRange(ImportLoc, ModuleNameLoc); 1944 return nullptr; 1945 } 1946 1947 // Try to compile and then read the AST. 1948 if (!compileModuleAndReadAST(*this, ImportLoc, ModuleNameLoc, M, 1949 ModuleFilename)) { 1950 assert(getDiagnostics().hasErrorOccurred() && 1951 "undiagnosed error in compileModuleAndReadAST"); 1952 if (getPreprocessorOpts().FailedModules) 1953 getPreprocessorOpts().FailedModules->addFailed(ModuleName); 1954 return nullptr; 1955 } 1956 1957 // Okay, we've rebuilt and now loaded the module. 1958 return M; 1959 } 1960 1961 ModuleLoadResult 1962 CompilerInstance::loadModule(SourceLocation ImportLoc, 1963 ModuleIdPath Path, 1964 Module::NameVisibilityKind Visibility, 1965 bool IsInclusionDirective) { 1966 // Determine what file we're searching from. 1967 StringRef ModuleName = Path[0].first->getName(); 1968 SourceLocation ModuleNameLoc = Path[0].second; 1969 1970 // If we've already handled this import, just return the cached result. 1971 // This one-element cache is important to eliminate redundant diagnostics 1972 // when both the preprocessor and parser see the same import declaration. 1973 if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) { 1974 // Make the named module visible. 1975 if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule) 1976 TheASTReader->makeModuleVisible(LastModuleImportResult, Visibility, 1977 ImportLoc); 1978 return LastModuleImportResult; 1979 } 1980 1981 // If we don't already have information on this module, load the module now. 1982 Module *Module = nullptr; 1983 ModuleMap &MM = getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1984 if (auto MaybeModule = MM.getCachedModuleLoad(*Path[0].first)) { 1985 // Use the cached result, which may be nullptr. 1986 Module = *MaybeModule; 1987 } else if (ModuleName == getLangOpts().CurrentModule) { 1988 // This is the module we're building. 1989 Module = PP->getHeaderSearchInfo().lookupModule( 1990 ModuleName, ImportLoc, /*AllowSearch*/ true, 1991 /*AllowExtraModuleMapSearch*/ !IsInclusionDirective); 1992 1993 MM.cacheModuleLoad(*Path[0].first, Module); 1994 } else { 1995 ModuleLoadResult Result = findOrCompileModuleAndReadAST( 1996 ModuleName, ImportLoc, ModuleNameLoc, IsInclusionDirective); 1997 if (!Result.isNormal()) 1998 return Result; 1999 if (!Result) 2000 DisableGeneratingGlobalModuleIndex = true; 2001 Module = Result; 2002 MM.cacheModuleLoad(*Path[0].first, Module); 2003 } 2004 2005 // If we never found the module, fail. Otherwise, verify the module and link 2006 // it up. 2007 if (!Module) 2008 return ModuleLoadResult(); 2009 2010 // Verify that the rest of the module path actually corresponds to 2011 // a submodule. 2012 bool MapPrivateSubModToTopLevel = false; 2013 for (unsigned I = 1, N = Path.size(); I != N; ++I) { 2014 StringRef Name = Path[I].first->getName(); 2015 clang::Module *Sub = Module->findSubmodule(Name); 2016 2017 // If the user is requesting Foo.Private and it doesn't exist, try to 2018 // match Foo_Private and emit a warning asking for the user to write 2019 // @import Foo_Private instead. FIXME: remove this when existing clients 2020 // migrate off of Foo.Private syntax. 2021 if (!Sub && Name == "Private" && Module == Module->getTopLevelModule()) { 2022 SmallString<128> PrivateModule(Module->Name); 2023 PrivateModule.append("_Private"); 2024 2025 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> PrivPath; 2026 auto &II = PP->getIdentifierTable().get( 2027 PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID()); 2028 PrivPath.push_back(std::make_pair(&II, Path[0].second)); 2029 2030 std::string FileName; 2031 // If there is a modulemap module or prebuilt module, load it. 2032 if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc, true, 2033 !IsInclusionDirective) || 2034 selectModuleSource(nullptr, PrivateModule, FileName, BuiltModules, 2035 PP->getHeaderSearchInfo()) != MS_ModuleNotFound) 2036 Sub = loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective); 2037 if (Sub) { 2038 MapPrivateSubModToTopLevel = true; 2039 PP->markClangModuleAsAffecting(Module); 2040 if (!getDiagnostics().isIgnored( 2041 diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) { 2042 getDiagnostics().Report(Path[I].second, 2043 diag::warn_no_priv_submodule_use_toplevel) 2044 << Path[I].first << Module->getFullModuleName() << PrivateModule 2045 << SourceRange(Path[0].second, Path[I].second) 2046 << FixItHint::CreateReplacement(SourceRange(Path[0].second), 2047 PrivateModule); 2048 getDiagnostics().Report(Sub->DefinitionLoc, 2049 diag::note_private_top_level_defined); 2050 } 2051 } 2052 } 2053 2054 if (!Sub) { 2055 // Attempt to perform typo correction to find a module name that works. 2056 SmallVector<StringRef, 2> Best; 2057 unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)(); 2058 2059 for (class Module *SubModule : Module->submodules()) { 2060 unsigned ED = 2061 Name.edit_distance(SubModule->Name, 2062 /*AllowReplacements=*/true, BestEditDistance); 2063 if (ED <= BestEditDistance) { 2064 if (ED < BestEditDistance) { 2065 Best.clear(); 2066 BestEditDistance = ED; 2067 } 2068 2069 Best.push_back(SubModule->Name); 2070 } 2071 } 2072 2073 // If there was a clear winner, user it. 2074 if (Best.size() == 1) { 2075 getDiagnostics().Report(Path[I].second, diag::err_no_submodule_suggest) 2076 << Path[I].first << Module->getFullModuleName() << Best[0] 2077 << SourceRange(Path[0].second, Path[I - 1].second) 2078 << FixItHint::CreateReplacement(SourceRange(Path[I].second), 2079 Best[0]); 2080 2081 Sub = Module->findSubmodule(Best[0]); 2082 } 2083 } 2084 2085 if (!Sub) { 2086 // No submodule by this name. Complain, and don't look for further 2087 // submodules. 2088 getDiagnostics().Report(Path[I].second, diag::err_no_submodule) 2089 << Path[I].first << Module->getFullModuleName() 2090 << SourceRange(Path[0].second, Path[I - 1].second); 2091 break; 2092 } 2093 2094 Module = Sub; 2095 } 2096 2097 // Make the named module visible, if it's not already part of the module 2098 // we are parsing. 2099 if (ModuleName != getLangOpts().CurrentModule) { 2100 if (!Module->IsFromModuleFile && !MapPrivateSubModToTopLevel) { 2101 // We have an umbrella header or directory that doesn't actually include 2102 // all of the headers within the directory it covers. Complain about 2103 // this missing submodule and recover by forgetting that we ever saw 2104 // this submodule. 2105 // FIXME: Should we detect this at module load time? It seems fairly 2106 // expensive (and rare). 2107 getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule) 2108 << Module->getFullModuleName() 2109 << SourceRange(Path.front().second, Path.back().second); 2110 2111 return ModuleLoadResult(Module, ModuleLoadResult::MissingExpected); 2112 } 2113 2114 // Check whether this module is available. 2115 if (Preprocessor::checkModuleIsAvailable(getLangOpts(), getTarget(), 2116 getDiagnostics(), Module)) { 2117 getDiagnostics().Report(ImportLoc, diag::note_module_import_here) 2118 << SourceRange(Path.front().second, Path.back().second); 2119 LastModuleImportLoc = ImportLoc; 2120 LastModuleImportResult = ModuleLoadResult(); 2121 return ModuleLoadResult(); 2122 } 2123 2124 TheASTReader->makeModuleVisible(Module, Visibility, ImportLoc); 2125 } 2126 2127 // Check for any configuration macros that have changed. 2128 clang::Module *TopModule = Module->getTopLevelModule(); 2129 for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) { 2130 checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I], 2131 Module, ImportLoc); 2132 } 2133 2134 // Resolve any remaining module using export_as for this one. 2135 getPreprocessor() 2136 .getHeaderSearchInfo() 2137 .getModuleMap() 2138 .resolveLinkAsDependencies(TopModule); 2139 2140 LastModuleImportLoc = ImportLoc; 2141 LastModuleImportResult = ModuleLoadResult(Module); 2142 return LastModuleImportResult; 2143 } 2144 2145 void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc, 2146 StringRef ModuleName, 2147 StringRef Source) { 2148 // Avoid creating filenames with special characters. 2149 SmallString<128> CleanModuleName(ModuleName); 2150 for (auto &C : CleanModuleName) 2151 if (!isAlphanumeric(C)) 2152 C = '_'; 2153 2154 // FIXME: Using a randomized filename here means that our intermediate .pcm 2155 // output is nondeterministic (as .pcm files refer to each other by name). 2156 // Can this affect the output in any way? 2157 SmallString<128> ModuleFileName; 2158 if (std::error_code EC = llvm::sys::fs::createTemporaryFile( 2159 CleanModuleName, "pcm", ModuleFileName)) { 2160 getDiagnostics().Report(ImportLoc, diag::err_fe_unable_to_open_output) 2161 << ModuleFileName << EC.message(); 2162 return; 2163 } 2164 std::string ModuleMapFileName = (CleanModuleName + ".map").str(); 2165 2166 FrontendInputFile Input( 2167 ModuleMapFileName, 2168 InputKind(getLanguageFromOptions(*Invocation->getLangOpts()), 2169 InputKind::ModuleMap, /*Preprocessed*/true)); 2170 2171 std::string NullTerminatedSource(Source.str()); 2172 2173 auto PreBuildStep = [&](CompilerInstance &Other) { 2174 // Create a virtual file containing our desired source. 2175 // FIXME: We shouldn't need to do this. 2176 const FileEntry *ModuleMapFile = Other.getFileManager().getVirtualFile( 2177 ModuleMapFileName, NullTerminatedSource.size(), 0); 2178 Other.getSourceManager().overrideFileContents( 2179 ModuleMapFile, llvm::MemoryBuffer::getMemBuffer(NullTerminatedSource)); 2180 2181 Other.BuiltModules = std::move(BuiltModules); 2182 Other.DeleteBuiltModules = false; 2183 }; 2184 2185 auto PostBuildStep = [this](CompilerInstance &Other) { 2186 BuiltModules = std::move(Other.BuiltModules); 2187 }; 2188 2189 // Build the module, inheriting any modules that we've built locally. 2190 if (compileModuleImpl(*this, ImportLoc, ModuleName, Input, StringRef(), 2191 ModuleFileName, PreBuildStep, PostBuildStep)) { 2192 BuiltModules[std::string(ModuleName)] = std::string(ModuleFileName.str()); 2193 llvm::sys::RemoveFileOnSignal(ModuleFileName); 2194 } 2195 } 2196 2197 void CompilerInstance::makeModuleVisible(Module *Mod, 2198 Module::NameVisibilityKind Visibility, 2199 SourceLocation ImportLoc) { 2200 if (!TheASTReader) 2201 createASTReader(); 2202 if (!TheASTReader) 2203 return; 2204 2205 TheASTReader->makeModuleVisible(Mod, Visibility, ImportLoc); 2206 } 2207 2208 GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex( 2209 SourceLocation TriggerLoc) { 2210 if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty()) 2211 return nullptr; 2212 if (!TheASTReader) 2213 createASTReader(); 2214 // Can't do anything if we don't have the module manager. 2215 if (!TheASTReader) 2216 return nullptr; 2217 // Get an existing global index. This loads it if not already 2218 // loaded. 2219 TheASTReader->loadGlobalIndex(); 2220 GlobalModuleIndex *GlobalIndex = TheASTReader->getGlobalIndex(); 2221 // If the global index doesn't exist, create it. 2222 if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() && 2223 hasPreprocessor()) { 2224 llvm::sys::fs::create_directories( 2225 getPreprocessor().getHeaderSearchInfo().getModuleCachePath()); 2226 if (llvm::Error Err = GlobalModuleIndex::writeIndex( 2227 getFileManager(), getPCHContainerReader(), 2228 getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) { 2229 // FIXME this drops the error on the floor. This code is only used for 2230 // typo correction and drops more than just this one source of errors 2231 // (such as the directory creation failure above). It should handle the 2232 // error. 2233 consumeError(std::move(Err)); 2234 return nullptr; 2235 } 2236 TheASTReader->resetForReload(); 2237 TheASTReader->loadGlobalIndex(); 2238 GlobalIndex = TheASTReader->getGlobalIndex(); 2239 } 2240 // For finding modules needing to be imported for fixit messages, 2241 // we need to make the global index cover all modules, so we do that here. 2242 if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) { 2243 ModuleMap &MMap = getPreprocessor().getHeaderSearchInfo().getModuleMap(); 2244 bool RecreateIndex = false; 2245 for (ModuleMap::module_iterator I = MMap.module_begin(), 2246 E = MMap.module_end(); I != E; ++I) { 2247 Module *TheModule = I->second; 2248 const FileEntry *Entry = TheModule->getASTFile(); 2249 if (!Entry) { 2250 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; 2251 Path.push_back(std::make_pair( 2252 getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc)); 2253 std::reverse(Path.begin(), Path.end()); 2254 // Load a module as hidden. This also adds it to the global index. 2255 loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false); 2256 RecreateIndex = true; 2257 } 2258 } 2259 if (RecreateIndex) { 2260 if (llvm::Error Err = GlobalModuleIndex::writeIndex( 2261 getFileManager(), getPCHContainerReader(), 2262 getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) { 2263 // FIXME As above, this drops the error on the floor. 2264 consumeError(std::move(Err)); 2265 return nullptr; 2266 } 2267 TheASTReader->resetForReload(); 2268 TheASTReader->loadGlobalIndex(); 2269 GlobalIndex = TheASTReader->getGlobalIndex(); 2270 } 2271 HaveFullGlobalModuleIndex = true; 2272 } 2273 return GlobalIndex; 2274 } 2275 2276 // Check global module index for missing imports. 2277 bool 2278 CompilerInstance::lookupMissingImports(StringRef Name, 2279 SourceLocation TriggerLoc) { 2280 // Look for the symbol in non-imported modules, but only if an error 2281 // actually occurred. 2282 if (!buildingModule()) { 2283 // Load global module index, or retrieve a previously loaded one. 2284 GlobalModuleIndex *GlobalIndex = loadGlobalModuleIndex( 2285 TriggerLoc); 2286 2287 // Only if we have a global index. 2288 if (GlobalIndex) { 2289 GlobalModuleIndex::HitSet FoundModules; 2290 2291 // Find the modules that reference the identifier. 2292 // Note that this only finds top-level modules. 2293 // We'll let diagnoseTypo find the actual declaration module. 2294 if (GlobalIndex->lookupIdentifier(Name, FoundModules)) 2295 return true; 2296 } 2297 } 2298 2299 return false; 2300 } 2301 void CompilerInstance::resetAndLeakSema() { llvm::BuryPointer(takeSema()); } 2302 2303 void CompilerInstance::setExternalSemaSource( 2304 IntrusiveRefCntPtr<ExternalSemaSource> ESS) { 2305 ExternalSemaSrc = std::move(ESS); 2306 } 2307