1 //===--- CompilerInstance.cpp ---------------------------------------------===// 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 #include "clang/Frontend/CompilerInstance.h" 11 #include "clang/AST/ASTConsumer.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/AST/Decl.h" 14 #include "clang/Basic/Diagnostic.h" 15 #include "clang/Basic/FileManager.h" 16 #include "clang/Basic/SourceManager.h" 17 #include "clang/Basic/TargetInfo.h" 18 #include "clang/Basic/Version.h" 19 #include "clang/Frontend/ChainedDiagnosticConsumer.h" 20 #include "clang/Frontend/FrontendAction.h" 21 #include "clang/Frontend/FrontendActions.h" 22 #include "clang/Frontend/FrontendDiagnostic.h" 23 #include "clang/Frontend/LogDiagnosticPrinter.h" 24 #include "clang/Frontend/SerializedDiagnosticPrinter.h" 25 #include "clang/Frontend/TextDiagnosticPrinter.h" 26 #include "clang/Frontend/Utils.h" 27 #include "clang/Frontend/VerifyDiagnosticConsumer.h" 28 #include "clang/Lex/HeaderSearch.h" 29 #include "clang/Lex/PTHManager.h" 30 #include "clang/Lex/Preprocessor.h" 31 #include "clang/Sema/CodeCompleteConsumer.h" 32 #include "clang/Sema/Sema.h" 33 #include "clang/Serialization/ASTReader.h" 34 #include "llvm/ADT/Statistic.h" 35 #include "llvm/Config/config.h" 36 #include "llvm/Support/CrashRecoveryContext.h" 37 #include "llvm/Support/FileSystem.h" 38 #include "llvm/Support/Host.h" 39 #include "llvm/Support/LockFileManager.h" 40 #include "llvm/Support/MemoryBuffer.h" 41 #include "llvm/Support/Path.h" 42 #include "llvm/Support/Program.h" 43 #include "llvm/Support/Signals.h" 44 #include "llvm/Support/Timer.h" 45 #include "llvm/Support/raw_ostream.h" 46 #include "llvm/Support/system_error.h" 47 #include <sys/stat.h> 48 #include <time.h> 49 50 using namespace clang; 51 52 CompilerInstance::CompilerInstance() 53 : Invocation(new CompilerInvocation()), ModuleManager(0), 54 BuildGlobalModuleIndex(false), ModuleBuildFailed(false) { 55 } 56 57 CompilerInstance::~CompilerInstance() { 58 assert(OutputFiles.empty() && "Still output files in flight?"); 59 } 60 61 void CompilerInstance::setInvocation(CompilerInvocation *Value) { 62 Invocation = Value; 63 } 64 65 bool CompilerInstance::shouldBuildGlobalModuleIndex() const { 66 return (BuildGlobalModuleIndex || 67 (ModuleManager && ModuleManager->isGlobalIndexUnavailable() && 68 getFrontendOpts().GenerateGlobalModuleIndex)) && 69 !ModuleBuildFailed; 70 } 71 72 void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) { 73 Diagnostics = Value; 74 } 75 76 void CompilerInstance::setTarget(TargetInfo *Value) { 77 Target = Value; 78 } 79 80 void CompilerInstance::setFileManager(FileManager *Value) { 81 FileMgr = Value; 82 if (Value) 83 VirtualFileSystem = Value->getVirtualFileSystem(); 84 else 85 VirtualFileSystem.reset(); 86 } 87 88 void CompilerInstance::setSourceManager(SourceManager *Value) { 89 SourceMgr = Value; 90 } 91 92 void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; } 93 94 void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; } 95 96 void CompilerInstance::setSema(Sema *S) { 97 TheSema.reset(S); 98 } 99 100 void CompilerInstance::setASTConsumer(ASTConsumer *Value) { 101 Consumer.reset(Value); 102 } 103 104 void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { 105 CompletionConsumer.reset(Value); 106 } 107 108 // Diagnostics 109 static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts, 110 const CodeGenOptions *CodeGenOpts, 111 DiagnosticsEngine &Diags) { 112 std::string ErrorInfo; 113 bool OwnsStream = false; 114 raw_ostream *OS = &llvm::errs(); 115 if (DiagOpts->DiagnosticLogFile != "-") { 116 // Create the output stream. 117 llvm::raw_fd_ostream *FileOS( 118 new llvm::raw_fd_ostream(DiagOpts->DiagnosticLogFile.c_str(), ErrorInfo, 119 llvm::sys::fs::F_Append)); 120 if (!ErrorInfo.empty()) { 121 Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) 122 << DiagOpts->DiagnosticLogFile << ErrorInfo; 123 } else { 124 FileOS->SetUnbuffered(); 125 FileOS->SetUseAtomicWrites(true); 126 OS = FileOS; 127 OwnsStream = true; 128 } 129 } 130 131 // Chain in the diagnostic client which will log the diagnostics. 132 LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts, 133 OwnsStream); 134 if (CodeGenOpts) 135 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); 136 Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger)); 137 } 138 139 static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, 140 DiagnosticsEngine &Diags, 141 StringRef OutputFile) { 142 std::string ErrorInfo; 143 OwningPtr<llvm::raw_fd_ostream> OS; 144 OS.reset(new llvm::raw_fd_ostream(OutputFile.str().c_str(), ErrorInfo, 145 llvm::sys::fs::F_Binary)); 146 147 if (!ErrorInfo.empty()) { 148 Diags.Report(diag::warn_fe_serialized_diag_failure) 149 << OutputFile << ErrorInfo; 150 return; 151 } 152 153 DiagnosticConsumer *SerializedConsumer = 154 clang::serialized_diags::create(OS.take(), DiagOpts); 155 156 157 Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), 158 SerializedConsumer)); 159 } 160 161 void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client, 162 bool ShouldOwnClient) { 163 Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client, 164 ShouldOwnClient, &getCodeGenOpts()); 165 } 166 167 IntrusiveRefCntPtr<DiagnosticsEngine> 168 CompilerInstance::createDiagnostics(DiagnosticOptions *Opts, 169 DiagnosticConsumer *Client, 170 bool ShouldOwnClient, 171 const CodeGenOptions *CodeGenOpts) { 172 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 173 IntrusiveRefCntPtr<DiagnosticsEngine> 174 Diags(new DiagnosticsEngine(DiagID, Opts)); 175 176 // Create the diagnostic client for reporting errors or for 177 // implementing -verify. 178 if (Client) { 179 Diags->setClient(Client, ShouldOwnClient); 180 } else 181 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); 182 183 // Chain in -verify checker, if requested. 184 if (Opts->VerifyDiagnostics) 185 Diags->setClient(new VerifyDiagnosticConsumer(*Diags)); 186 187 // Chain in -diagnostic-log-file dumper, if requested. 188 if (!Opts->DiagnosticLogFile.empty()) 189 SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); 190 191 if (!Opts->DiagnosticSerializationFile.empty()) 192 SetupSerializedDiagnostics(Opts, *Diags, 193 Opts->DiagnosticSerializationFile); 194 195 // Configure our handling of diagnostics. 196 ProcessWarningOptions(*Diags, *Opts); 197 198 return Diags; 199 } 200 201 // File Manager 202 203 void CompilerInstance::createFileManager() { 204 if (!hasVirtualFileSystem()) { 205 // TODO: choose the virtual file system based on the CompilerInvocation. 206 setVirtualFileSystem(vfs::getRealFileSystem()); 207 } 208 FileMgr = new FileManager(getFileSystemOpts(), VirtualFileSystem); 209 } 210 211 // Source Manager 212 213 void CompilerInstance::createSourceManager(FileManager &FileMgr) { 214 SourceMgr = new SourceManager(getDiagnostics(), FileMgr); 215 } 216 217 // Preprocessor 218 219 void CompilerInstance::createPreprocessor() { 220 const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 221 222 // Create a PTH manager if we are using some form of a token cache. 223 PTHManager *PTHMgr = 0; 224 if (!PPOpts.TokenCache.empty()) 225 PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics()); 226 227 // Create the Preprocessor. 228 HeaderSearch *HeaderInfo = new HeaderSearch(&getHeaderSearchOpts(), 229 getSourceManager(), 230 getDiagnostics(), 231 getLangOpts(), 232 &getTarget()); 233 PP = new Preprocessor(&getPreprocessorOpts(), 234 getDiagnostics(), getLangOpts(), &getTarget(), 235 getSourceManager(), *HeaderInfo, *this, PTHMgr, 236 /*OwnsHeaderSearch=*/true); 237 238 // Note that this is different then passing PTHMgr to Preprocessor's ctor. 239 // That argument is used as the IdentifierInfoLookup argument to 240 // IdentifierTable's ctor. 241 if (PTHMgr) { 242 PTHMgr->setPreprocessor(&*PP); 243 PP->setPTHManager(PTHMgr); 244 } 245 246 if (PPOpts.DetailedRecord) 247 PP->createPreprocessingRecord(); 248 249 InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts()); 250 251 PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP); 252 253 // Set up the module path, including the hash for the 254 // module-creation options. 255 SmallString<256> SpecificModuleCache( 256 getHeaderSearchOpts().ModuleCachePath); 257 if (!getHeaderSearchOpts().DisableModuleHash) 258 llvm::sys::path::append(SpecificModuleCache, 259 getInvocation().getModuleHash()); 260 PP->getHeaderSearchInfo().setModuleCachePath(SpecificModuleCache); 261 262 // Handle generating dependencies, if requested. 263 const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); 264 if (!DepOpts.OutputFile.empty()) 265 AttachDependencyFileGen(*PP, DepOpts); 266 if (!DepOpts.DOTOutputFile.empty()) 267 AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile, 268 getHeaderSearchOpts().Sysroot); 269 270 271 // Handle generating header include information, if requested. 272 if (DepOpts.ShowHeaderIncludes) 273 AttachHeaderIncludeGen(*PP); 274 if (!DepOpts.HeaderIncludeOutputFile.empty()) { 275 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; 276 if (OutputPath == "-") 277 OutputPath = ""; 278 AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, 279 /*ShowDepth=*/false); 280 } 281 282 if (DepOpts.PrintShowIncludes) { 283 AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/false, /*OutputPath=*/"", 284 /*ShowDepth=*/true, /*MSStyle=*/true); 285 } 286 } 287 288 // ASTContext 289 290 void CompilerInstance::createASTContext() { 291 Preprocessor &PP = getPreprocessor(); 292 Context = new ASTContext(getLangOpts(), PP.getSourceManager(), 293 &getTarget(), PP.getIdentifierTable(), 294 PP.getSelectorTable(), PP.getBuiltinInfo(), 295 /*size_reserve=*/ 0); 296 } 297 298 // ExternalASTSource 299 300 void CompilerInstance::createPCHExternalASTSource(StringRef Path, 301 bool DisablePCHValidation, 302 bool AllowPCHWithCompilerErrors, 303 void *DeserializationListener){ 304 OwningPtr<ExternalASTSource> Source; 305 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; 306 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, 307 DisablePCHValidation, 308 AllowPCHWithCompilerErrors, 309 getPreprocessor(), getASTContext(), 310 DeserializationListener, 311 Preamble, 312 getFrontendOpts().UseGlobalModuleIndex)); 313 ModuleManager = static_cast<ASTReader*>(Source.get()); 314 getASTContext().setExternalSource(Source); 315 } 316 317 ExternalASTSource * 318 CompilerInstance::createPCHExternalASTSource(StringRef Path, 319 const std::string &Sysroot, 320 bool DisablePCHValidation, 321 bool AllowPCHWithCompilerErrors, 322 Preprocessor &PP, 323 ASTContext &Context, 324 void *DeserializationListener, 325 bool Preamble, 326 bool UseGlobalModuleIndex) { 327 OwningPtr<ASTReader> Reader; 328 Reader.reset(new ASTReader(PP, Context, 329 Sysroot.empty() ? "" : Sysroot.c_str(), 330 DisablePCHValidation, 331 AllowPCHWithCompilerErrors, 332 /*AllowConfigurationMismatch*/false, 333 /*ValidateSystemInputs*/false, 334 UseGlobalModuleIndex)); 335 336 Reader->setDeserializationListener( 337 static_cast<ASTDeserializationListener *>(DeserializationListener)); 338 switch (Reader->ReadAST(Path, 339 Preamble ? serialization::MK_Preamble 340 : serialization::MK_PCH, 341 SourceLocation(), 342 ASTReader::ARR_None)) { 343 case ASTReader::Success: 344 // Set the predefines buffer as suggested by the PCH reader. Typically, the 345 // predefines buffer will be empty. 346 PP.setPredefines(Reader->getSuggestedPredefines()); 347 return Reader.take(); 348 349 case ASTReader::Failure: 350 // Unrecoverable failure: don't even try to process the input file. 351 break; 352 353 case ASTReader::Missing: 354 case ASTReader::OutOfDate: 355 case ASTReader::VersionMismatch: 356 case ASTReader::ConfigurationMismatch: 357 case ASTReader::HadErrors: 358 // No suitable PCH file could be found. Return an error. 359 break; 360 } 361 362 return 0; 363 } 364 365 // Code Completion 366 367 static bool EnableCodeCompletion(Preprocessor &PP, 368 const std::string &Filename, 369 unsigned Line, 370 unsigned Column) { 371 // Tell the source manager to chop off the given file at a specific 372 // line and column. 373 const FileEntry *Entry = PP.getFileManager().getFile(Filename); 374 if (!Entry) { 375 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) 376 << Filename; 377 return true; 378 } 379 380 // Truncate the named file at the given line/column. 381 PP.SetCodeCompletionPoint(Entry, Line, Column); 382 return false; 383 } 384 385 void CompilerInstance::createCodeCompletionConsumer() { 386 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; 387 if (!CompletionConsumer) { 388 setCodeCompletionConsumer( 389 createCodeCompletionConsumer(getPreprocessor(), 390 Loc.FileName, Loc.Line, Loc.Column, 391 getFrontendOpts().CodeCompleteOpts, 392 llvm::outs())); 393 if (!CompletionConsumer) 394 return; 395 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, 396 Loc.Line, Loc.Column)) { 397 setCodeCompletionConsumer(0); 398 return; 399 } 400 401 if (CompletionConsumer->isOutputBinary() && 402 llvm::sys::ChangeStdoutToBinary()) { 403 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); 404 setCodeCompletionConsumer(0); 405 } 406 } 407 408 void CompilerInstance::createFrontendTimer() { 409 FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); 410 } 411 412 CodeCompleteConsumer * 413 CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, 414 const std::string &Filename, 415 unsigned Line, 416 unsigned Column, 417 const CodeCompleteOptions &Opts, 418 raw_ostream &OS) { 419 if (EnableCodeCompletion(PP, Filename, Line, Column)) 420 return 0; 421 422 // Set up the creation routine for code-completion. 423 return new PrintingCodeCompleteConsumer(Opts, OS); 424 } 425 426 void CompilerInstance::createSema(TranslationUnitKind TUKind, 427 CodeCompleteConsumer *CompletionConsumer) { 428 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), 429 TUKind, CompletionConsumer)); 430 } 431 432 // Output Files 433 434 void CompilerInstance::addOutputFile(const OutputFile &OutFile) { 435 assert(OutFile.OS && "Attempt to add empty stream to output list!"); 436 OutputFiles.push_back(OutFile); 437 } 438 439 void CompilerInstance::clearOutputFiles(bool EraseFiles) { 440 for (std::list<OutputFile>::iterator 441 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { 442 delete it->OS; 443 if (!it->TempFilename.empty()) { 444 if (EraseFiles) { 445 llvm::sys::fs::remove(it->TempFilename); 446 } else { 447 SmallString<128> NewOutFile(it->Filename); 448 449 // If '-working-directory' was passed, the output filename should be 450 // relative to that. 451 FileMgr->FixupRelativePath(NewOutFile); 452 if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename, 453 NewOutFile.str())) { 454 getDiagnostics().Report(diag::err_unable_to_rename_temp) 455 << it->TempFilename << it->Filename << ec.message(); 456 457 llvm::sys::fs::remove(it->TempFilename); 458 } 459 } 460 } else if (!it->Filename.empty() && EraseFiles) 461 llvm::sys::fs::remove(it->Filename); 462 463 } 464 OutputFiles.clear(); 465 } 466 467 llvm::raw_fd_ostream * 468 CompilerInstance::createDefaultOutputFile(bool Binary, 469 StringRef InFile, 470 StringRef Extension) { 471 return createOutputFile(getFrontendOpts().OutputFile, Binary, 472 /*RemoveFileOnSignal=*/true, InFile, Extension, 473 /*UseTemporary=*/true); 474 } 475 476 llvm::raw_fd_ostream * 477 CompilerInstance::createOutputFile(StringRef OutputPath, 478 bool Binary, bool RemoveFileOnSignal, 479 StringRef InFile, 480 StringRef Extension, 481 bool UseTemporary, 482 bool CreateMissingDirectories) { 483 std::string Error, OutputPathName, TempPathName; 484 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, 485 RemoveFileOnSignal, 486 InFile, Extension, 487 UseTemporary, 488 CreateMissingDirectories, 489 &OutputPathName, 490 &TempPathName); 491 if (!OS) { 492 getDiagnostics().Report(diag::err_fe_unable_to_open_output) 493 << OutputPath << Error; 494 return 0; 495 } 496 497 // Add the output file -- but don't try to remove "-", since this means we are 498 // using stdin. 499 addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", 500 TempPathName, OS)); 501 502 return OS; 503 } 504 505 llvm::raw_fd_ostream * 506 CompilerInstance::createOutputFile(StringRef OutputPath, 507 std::string &Error, 508 bool Binary, 509 bool RemoveFileOnSignal, 510 StringRef InFile, 511 StringRef Extension, 512 bool UseTemporary, 513 bool CreateMissingDirectories, 514 std::string *ResultPathName, 515 std::string *TempPathName) { 516 assert((!CreateMissingDirectories || UseTemporary) && 517 "CreateMissingDirectories is only allowed when using temporary files"); 518 519 std::string OutFile, TempFile; 520 if (!OutputPath.empty()) { 521 OutFile = OutputPath; 522 } else if (InFile == "-") { 523 OutFile = "-"; 524 } else if (!Extension.empty()) { 525 SmallString<128> Path(InFile); 526 llvm::sys::path::replace_extension(Path, Extension); 527 OutFile = Path.str(); 528 } else { 529 OutFile = "-"; 530 } 531 532 OwningPtr<llvm::raw_fd_ostream> OS; 533 std::string OSFile; 534 535 if (UseTemporary) { 536 if (OutFile == "-") 537 UseTemporary = false; 538 else { 539 llvm::sys::fs::file_status Status; 540 llvm::sys::fs::status(OutputPath, Status); 541 if (llvm::sys::fs::exists(Status)) { 542 // Fail early if we can't write to the final destination. 543 if (!llvm::sys::fs::can_write(OutputPath)) 544 return 0; 545 546 // Don't use a temporary if the output is a special file. This handles 547 // things like '-o /dev/null' 548 if (!llvm::sys::fs::is_regular_file(Status)) 549 UseTemporary = false; 550 } 551 } 552 } 553 554 if (UseTemporary) { 555 // Create a temporary file. 556 SmallString<128> TempPath; 557 TempPath = OutFile; 558 TempPath += "-%%%%%%%%"; 559 int fd; 560 llvm::error_code EC = 561 llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath); 562 563 if (CreateMissingDirectories && 564 EC == llvm::errc::no_such_file_or_directory) { 565 StringRef Parent = llvm::sys::path::parent_path(OutputPath); 566 EC = llvm::sys::fs::create_directories(Parent); 567 if (!EC) { 568 EC = llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath); 569 } 570 } 571 572 if (!EC) { 573 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); 574 OSFile = TempFile = TempPath.str(); 575 } 576 // If we failed to create the temporary, fallback to writing to the file 577 // directly. This handles the corner case where we cannot write to the 578 // directory, but can write to the file. 579 } 580 581 if (!OS) { 582 OSFile = OutFile; 583 OS.reset(new llvm::raw_fd_ostream( 584 OSFile.c_str(), Error, 585 (Binary ? llvm::sys::fs::F_Binary : llvm::sys::fs::F_None))); 586 if (!Error.empty()) 587 return 0; 588 } 589 590 // Make sure the out stream file gets removed if we crash. 591 if (RemoveFileOnSignal) 592 llvm::sys::RemoveFileOnSignal(OSFile); 593 594 if (ResultPathName) 595 *ResultPathName = OutFile; 596 if (TempPathName) 597 *TempPathName = TempFile; 598 599 return OS.take(); 600 } 601 602 // Initialization Utilities 603 604 bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){ 605 return InitializeSourceManager(Input, getDiagnostics(), 606 getFileManager(), getSourceManager(), 607 getFrontendOpts()); 608 } 609 610 bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input, 611 DiagnosticsEngine &Diags, 612 FileManager &FileMgr, 613 SourceManager &SourceMgr, 614 const FrontendOptions &Opts) { 615 SrcMgr::CharacteristicKind 616 Kind = Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User; 617 618 if (Input.isBuffer()) { 619 SourceMgr.createMainFileIDForMemBuffer(Input.getBuffer(), Kind); 620 assert(!SourceMgr.getMainFileID().isInvalid() && 621 "Couldn't establish MainFileID!"); 622 return true; 623 } 624 625 StringRef InputFile = Input.getFile(); 626 627 // Figure out where to get and map in the main file. 628 if (InputFile != "-") { 629 const FileEntry *File = FileMgr.getFile(InputFile, /*OpenFile=*/true); 630 if (!File) { 631 Diags.Report(diag::err_fe_error_reading) << InputFile; 632 return false; 633 } 634 635 // The natural SourceManager infrastructure can't currently handle named 636 // pipes, but we would at least like to accept them for the main 637 // file. Detect them here, read them with the volatile flag so FileMgr will 638 // pick up the correct size, and simply override their contents as we do for 639 // STDIN. 640 if (File->isNamedPipe()) { 641 std::string ErrorStr; 642 if (llvm::MemoryBuffer *MB = 643 FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true)) { 644 // Create a new virtual file that will have the correct size. 645 File = FileMgr.getVirtualFile(InputFile, MB->getBufferSize(), 0); 646 SourceMgr.overrideFileContents(File, MB); 647 } else { 648 Diags.Report(diag::err_cannot_open_file) << InputFile << ErrorStr; 649 return false; 650 } 651 } 652 653 SourceMgr.createMainFileID(File, Kind); 654 } else { 655 OwningPtr<llvm::MemoryBuffer> SB; 656 if (llvm::error_code ec = llvm::MemoryBuffer::getSTDIN(SB)) { 657 Diags.Report(diag::err_fe_error_reading_stdin) << ec.message(); 658 return false; 659 } 660 const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), 661 SB->getBufferSize(), 0); 662 SourceMgr.createMainFileID(File, Kind); 663 SourceMgr.overrideFileContents(File, SB.take()); 664 } 665 666 assert(!SourceMgr.getMainFileID().isInvalid() && 667 "Couldn't establish MainFileID!"); 668 return true; 669 } 670 671 // High-Level Operations 672 673 bool CompilerInstance::ExecuteAction(FrontendAction &Act) { 674 assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); 675 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); 676 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); 677 678 // FIXME: Take this as an argument, once all the APIs we used have moved to 679 // taking it as an input instead of hard-coding llvm::errs. 680 raw_ostream &OS = llvm::errs(); 681 682 // Create the target instance. 683 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), &getTargetOpts())); 684 if (!hasTarget()) 685 return false; 686 687 // Inform the target of the language options. 688 // 689 // FIXME: We shouldn't need to do this, the target should be immutable once 690 // created. This complexity should be lifted elsewhere. 691 getTarget().setForcedLangOptions(getLangOpts()); 692 693 // rewriter project will change target built-in bool type from its default. 694 if (getFrontendOpts().ProgramAction == frontend::RewriteObjC) 695 getTarget().noSignedCharForObjCBool(); 696 697 // Validate/process some options. 698 if (getHeaderSearchOpts().Verbose) 699 OS << "clang -cc1 version " CLANG_VERSION_STRING 700 << " based upon " << PACKAGE_STRING 701 << " default target " << llvm::sys::getDefaultTargetTriple() << "\n"; 702 703 if (getFrontendOpts().ShowTimers) 704 createFrontendTimer(); 705 706 if (getFrontendOpts().ShowStats) 707 llvm::EnableStatistics(); 708 709 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { 710 // Reset the ID tables if we are reusing the SourceManager. 711 if (hasSourceManager()) 712 getSourceManager().clearIDTables(); 713 714 if (Act.BeginSourceFile(*this, getFrontendOpts().Inputs[i])) { 715 Act.Execute(); 716 Act.EndSourceFile(); 717 } 718 } 719 720 // Notify the diagnostic client that all files were processed. 721 getDiagnostics().getClient()->finish(); 722 723 if (getDiagnosticOpts().ShowCarets) { 724 // We can have multiple diagnostics sharing one diagnostic client. 725 // Get the total number of warnings/errors from the client. 726 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); 727 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); 728 729 if (NumWarnings) 730 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); 731 if (NumWarnings && NumErrors) 732 OS << " and "; 733 if (NumErrors) 734 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); 735 if (NumWarnings || NumErrors) 736 OS << " generated.\n"; 737 } 738 739 if (getFrontendOpts().ShowStats && hasFileManager()) { 740 getFileManager().PrintStats(); 741 OS << "\n"; 742 } 743 744 return !getDiagnostics().getClient()->getNumErrors(); 745 } 746 747 /// \brief Determine the appropriate source input kind based on language 748 /// options. 749 static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) { 750 if (LangOpts.OpenCL) 751 return IK_OpenCL; 752 if (LangOpts.CUDA) 753 return IK_CUDA; 754 if (LangOpts.ObjC1) 755 return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC; 756 return LangOpts.CPlusPlus? IK_CXX : IK_C; 757 } 758 759 namespace { 760 struct CompileModuleMapData { 761 CompilerInstance &Instance; 762 GenerateModuleAction &CreateModuleAction; 763 }; 764 } 765 766 /// \brief Helper function that executes the module-generating action under 767 /// a crash recovery context. 768 static void doCompileMapModule(void *UserData) { 769 CompileModuleMapData &Data 770 = *reinterpret_cast<CompileModuleMapData *>(UserData); 771 Data.Instance.ExecuteAction(Data.CreateModuleAction); 772 } 773 774 namespace { 775 /// \brief Function object that checks with the given macro definition should 776 /// be removed, because it is one of the ignored macros. 777 class RemoveIgnoredMacro { 778 const HeaderSearchOptions &HSOpts; 779 780 public: 781 explicit RemoveIgnoredMacro(const HeaderSearchOptions &HSOpts) 782 : HSOpts(HSOpts) { } 783 784 bool operator()(const std::pair<std::string, bool> &def) const { 785 StringRef MacroDef = def.first; 786 return HSOpts.ModulesIgnoreMacros.count(MacroDef.split('=').first) > 0; 787 } 788 }; 789 } 790 791 /// \brief Compile a module file for the given module, using the options 792 /// provided by the importing compiler instance. 793 static void compileModule(CompilerInstance &ImportingInstance, 794 SourceLocation ImportLoc, 795 Module *Module, 796 StringRef ModuleFileName) { 797 // FIXME: have LockFileManager return an error_code so that we can 798 // avoid the mkdir when the directory already exists. 799 StringRef Dir = llvm::sys::path::parent_path(ModuleFileName); 800 llvm::sys::fs::create_directories(Dir); 801 802 llvm::LockFileManager Locked(ModuleFileName); 803 switch (Locked) { 804 case llvm::LockFileManager::LFS_Error: 805 return; 806 807 case llvm::LockFileManager::LFS_Owned: 808 // We're responsible for building the module ourselves. Do so below. 809 break; 810 811 case llvm::LockFileManager::LFS_Shared: 812 // Someone else is responsible for building the module. Wait for them to 813 // finish. 814 Locked.waitForUnlock(); 815 return; 816 } 817 818 ModuleMap &ModMap 819 = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 820 821 // Construct a compiler invocation for creating this module. 822 IntrusiveRefCntPtr<CompilerInvocation> Invocation 823 (new CompilerInvocation(ImportingInstance.getInvocation())); 824 825 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); 826 827 // For any options that aren't intended to affect how a module is built, 828 // reset them to their default values. 829 Invocation->getLangOpts()->resetNonModularOptions(); 830 PPOpts.resetNonModularOptions(); 831 832 // Remove any macro definitions that are explicitly ignored by the module. 833 // They aren't supposed to affect how the module is built anyway. 834 const HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts(); 835 PPOpts.Macros.erase(std::remove_if(PPOpts.Macros.begin(), PPOpts.Macros.end(), 836 RemoveIgnoredMacro(HSOpts)), 837 PPOpts.Macros.end()); 838 839 840 // Note the name of the module we're building. 841 Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName(); 842 843 // Make sure that the failed-module structure has been allocated in 844 // the importing instance, and propagate the pointer to the newly-created 845 // instance. 846 PreprocessorOptions &ImportingPPOpts 847 = ImportingInstance.getInvocation().getPreprocessorOpts(); 848 if (!ImportingPPOpts.FailedModules) 849 ImportingPPOpts.FailedModules = new PreprocessorOptions::FailedModulesSet; 850 PPOpts.FailedModules = ImportingPPOpts.FailedModules; 851 852 // If there is a module map file, build the module using the module map. 853 // Set up the inputs/outputs so that we build the module from its umbrella 854 // header. 855 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); 856 FrontendOpts.OutputFile = ModuleFileName.str(); 857 FrontendOpts.DisableFree = false; 858 FrontendOpts.GenerateGlobalModuleIndex = false; 859 FrontendOpts.Inputs.clear(); 860 InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts()); 861 862 // Don't free the remapped file buffers; they are owned by our caller. 863 PPOpts.RetainRemappedFileBuffers = true; 864 865 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; 866 assert(ImportingInstance.getInvocation().getModuleHash() == 867 Invocation->getModuleHash() && "Module hash mismatch!"); 868 869 // Construct a compiler instance that will be used to actually create the 870 // module. 871 CompilerInstance Instance; 872 Instance.setInvocation(&*Invocation); 873 874 Instance.createDiagnostics(new ForwardingDiagnosticConsumer( 875 ImportingInstance.getDiagnosticClient()), 876 /*ShouldOwnClient=*/true); 877 878 Instance.setVirtualFileSystem(&ImportingInstance.getVirtualFileSystem()); 879 880 // Note that this module is part of the module build stack, so that we 881 // can detect cycles in the module graph. 882 Instance.createFileManager(); // FIXME: Adopt file manager from importer? 883 Instance.createSourceManager(Instance.getFileManager()); 884 SourceManager &SourceMgr = Instance.getSourceManager(); 885 SourceMgr.setModuleBuildStack( 886 ImportingInstance.getSourceManager().getModuleBuildStack()); 887 SourceMgr.pushModuleBuildStack(Module->getTopLevelModuleName(), 888 FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager())); 889 890 // Get or create the module map that we'll use to build this module. 891 std::string InferredModuleMapContent; 892 if (const FileEntry *ModuleMapFile = 893 ModMap.getContainingModuleMapFile(Module)) { 894 // Use the module map where this module resides. 895 FrontendOpts.Inputs.push_back( 896 FrontendInputFile(ModuleMapFile->getName(), IK)); 897 } else { 898 llvm::raw_string_ostream OS(InferredModuleMapContent); 899 Module->print(OS); 900 OS.flush(); 901 FrontendOpts.Inputs.push_back( 902 FrontendInputFile("__inferred_module.map", IK)); 903 904 const llvm::MemoryBuffer *ModuleMapBuffer = 905 llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent); 906 ModuleMapFile = Instance.getFileManager().getVirtualFile( 907 "__inferred_module.map", InferredModuleMapContent.size(), 0); 908 SourceMgr.overrideFileContents(ModuleMapFile, ModuleMapBuffer); 909 } 910 911 // Construct a module-generating action. 912 GenerateModuleAction CreateModuleAction(Module->IsSystem); 913 914 // Execute the action to actually build the module in-place. Use a separate 915 // thread so that we get a stack large enough. 916 const unsigned ThreadStackSize = 8 << 20; 917 llvm::CrashRecoveryContext CRC; 918 CompileModuleMapData Data = { Instance, CreateModuleAction }; 919 CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize); 920 921 922 // Delete the temporary module map file. 923 // FIXME: Even though we're executing under crash protection, it would still 924 // be nice to do this with RemoveFileOnSignal when we can. However, that 925 // doesn't make sense for all clients, so clean this up manually. 926 Instance.clearOutputFiles(/*EraseFiles=*/true); 927 928 // We've rebuilt a module. If we're allowed to generate or update the global 929 // module index, record that fact in the importing compiler instance. 930 if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) { 931 ImportingInstance.setBuildGlobalModuleIndex(true); 932 } 933 } 934 935 /// \brief Diagnose differences between the current definition of the given 936 /// configuration macro and the definition provided on the command line. 937 static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro, 938 Module *Mod, SourceLocation ImportLoc) { 939 IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro); 940 SourceManager &SourceMgr = PP.getSourceManager(); 941 942 // If this identifier has never had a macro definition, then it could 943 // not have changed. 944 if (!Id->hadMacroDefinition()) 945 return; 946 947 // If this identifier does not currently have a macro definition, 948 // check whether it had one on the command line. 949 if (!Id->hasMacroDefinition()) { 950 MacroDirective::DefInfo LatestDef = 951 PP.getMacroDirectiveHistory(Id)->getDefinition(); 952 for (MacroDirective::DefInfo Def = LatestDef; Def; 953 Def = Def.getPreviousDefinition()) { 954 FileID FID = SourceMgr.getFileID(Def.getLocation()); 955 if (FID.isInvalid()) 956 continue; 957 958 // We only care about the predefines buffer. 959 if (FID != PP.getPredefinesFileID()) 960 continue; 961 962 // This macro was defined on the command line, then #undef'd later. 963 // Complain. 964 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) 965 << true << ConfigMacro << Mod->getFullModuleName(); 966 if (LatestDef.isUndefined()) 967 PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here) 968 << true; 969 return; 970 } 971 972 // Okay: no definition in the predefines buffer. 973 return; 974 } 975 976 // This identifier has a macro definition. Check whether we had a definition 977 // on the command line. 978 MacroDirective::DefInfo LatestDef = 979 PP.getMacroDirectiveHistory(Id)->getDefinition(); 980 MacroDirective::DefInfo PredefinedDef; 981 for (MacroDirective::DefInfo Def = LatestDef; Def; 982 Def = Def.getPreviousDefinition()) { 983 FileID FID = SourceMgr.getFileID(Def.getLocation()); 984 if (FID.isInvalid()) 985 continue; 986 987 // We only care about the predefines buffer. 988 if (FID != PP.getPredefinesFileID()) 989 continue; 990 991 PredefinedDef = Def; 992 break; 993 } 994 995 // If there was no definition for this macro in the predefines buffer, 996 // complain. 997 if (!PredefinedDef || 998 (!PredefinedDef.getLocation().isValid() && 999 PredefinedDef.getUndefLocation().isValid())) { 1000 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) 1001 << false << ConfigMacro << Mod->getFullModuleName(); 1002 PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here) 1003 << false; 1004 return; 1005 } 1006 1007 // If the current macro definition is the same as the predefined macro 1008 // definition, it's okay. 1009 if (LatestDef.getMacroInfo() == PredefinedDef.getMacroInfo() || 1010 LatestDef.getMacroInfo()->isIdenticalTo(*PredefinedDef.getMacroInfo(),PP, 1011 /*Syntactically=*/true)) 1012 return; 1013 1014 // The macro definitions differ. 1015 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) 1016 << false << ConfigMacro << Mod->getFullModuleName(); 1017 PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here) 1018 << false; 1019 } 1020 1021 /// \brief Write a new timestamp file with the given path. 1022 static void writeTimestampFile(StringRef TimestampFile) { 1023 std::string ErrorInfo; 1024 llvm::raw_fd_ostream Out(TimestampFile.str().c_str(), ErrorInfo, 1025 llvm::sys::fs::F_Binary); 1026 } 1027 1028 /// \brief Prune the module cache of modules that haven't been accessed in 1029 /// a long time. 1030 static void pruneModuleCache(const HeaderSearchOptions &HSOpts) { 1031 struct stat StatBuf; 1032 llvm::SmallString<128> TimestampFile; 1033 TimestampFile = HSOpts.ModuleCachePath; 1034 llvm::sys::path::append(TimestampFile, "modules.timestamp"); 1035 1036 // Try to stat() the timestamp file. 1037 if (::stat(TimestampFile.c_str(), &StatBuf)) { 1038 // If the timestamp file wasn't there, create one now. 1039 if (errno == ENOENT) { 1040 writeTimestampFile(TimestampFile); 1041 } 1042 return; 1043 } 1044 1045 // Check whether the time stamp is older than our pruning interval. 1046 // If not, do nothing. 1047 time_t TimeStampModTime = StatBuf.st_mtime; 1048 time_t CurrentTime = time(0); 1049 if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval)) 1050 return; 1051 1052 // Write a new timestamp file so that nobody else attempts to prune. 1053 // There is a benign race condition here, if two Clang instances happen to 1054 // notice at the same time that the timestamp is out-of-date. 1055 writeTimestampFile(TimestampFile); 1056 1057 // Walk the entire module cache, looking for unused module files and module 1058 // indices. 1059 llvm::error_code EC; 1060 SmallString<128> ModuleCachePathNative; 1061 llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative); 1062 for (llvm::sys::fs::directory_iterator 1063 Dir(ModuleCachePathNative.str(), EC), DirEnd; 1064 Dir != DirEnd && !EC; Dir.increment(EC)) { 1065 // If we don't have a directory, there's nothing to look into. 1066 if (!llvm::sys::fs::is_directory(Dir->path())) 1067 continue; 1068 1069 // Walk all of the files within this directory. 1070 for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd; 1071 File != FileEnd && !EC; File.increment(EC)) { 1072 // We only care about module and global module index files. 1073 StringRef Extension = llvm::sys::path::extension(File->path()); 1074 if (Extension != ".pcm" && Extension != ".timestamp" && 1075 llvm::sys::path::filename(File->path()) != "modules.idx") 1076 continue; 1077 1078 // Look at this file. If we can't stat it, there's nothing interesting 1079 // there. 1080 if (::stat(File->path().c_str(), &StatBuf)) 1081 continue; 1082 1083 // If the file has been used recently enough, leave it there. 1084 time_t FileAccessTime = StatBuf.st_atime; 1085 if (CurrentTime - FileAccessTime <= 1086 time_t(HSOpts.ModuleCachePruneAfter)) { 1087 continue; 1088 } 1089 1090 // Remove the file. 1091 llvm::sys::fs::remove(File->path()); 1092 1093 // Remove the timestamp file. 1094 std::string TimpestampFilename = File->path() + ".timestamp"; 1095 llvm::sys::fs::remove(TimpestampFilename); 1096 } 1097 1098 // If we removed all of the files in the directory, remove the directory 1099 // itself. 1100 if (llvm::sys::fs::directory_iterator(Dir->path(), EC) == 1101 llvm::sys::fs::directory_iterator() && !EC) 1102 llvm::sys::fs::remove(Dir->path()); 1103 } 1104 } 1105 1106 ModuleLoadResult 1107 CompilerInstance::loadModule(SourceLocation ImportLoc, 1108 ModuleIdPath Path, 1109 Module::NameVisibilityKind Visibility, 1110 bool IsInclusionDirective) { 1111 // Determine what file we're searching from. 1112 StringRef ModuleName = Path[0].first->getName(); 1113 SourceLocation ModuleNameLoc = Path[0].second; 1114 1115 // If we've already handled this import, just return the cached result. 1116 // This one-element cache is important to eliminate redundant diagnostics 1117 // when both the preprocessor and parser see the same import declaration. 1118 if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) { 1119 // Make the named module visible. 1120 if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule) 1121 ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility, 1122 ImportLoc, /*Complain=*/false); 1123 return LastModuleImportResult; 1124 } 1125 1126 clang::Module *Module = 0; 1127 1128 // If we don't already have information on this module, load the module now. 1129 llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known 1130 = KnownModules.find(Path[0].first); 1131 if (Known != KnownModules.end()) { 1132 // Retrieve the cached top-level module. 1133 Module = Known->second; 1134 } else if (ModuleName == getLangOpts().CurrentModule) { 1135 // This is the module we're building. 1136 Module = PP->getHeaderSearchInfo().getModuleMap().findModule(ModuleName); 1137 Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first; 1138 } else { 1139 // Search for a module with the given name. 1140 Module = PP->getHeaderSearchInfo().lookupModule(ModuleName); 1141 if (!Module) { 1142 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found) 1143 << ModuleName 1144 << SourceRange(ImportLoc, ModuleNameLoc); 1145 ModuleBuildFailed = true; 1146 return ModuleLoadResult(); 1147 } 1148 1149 std::string ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(Module); 1150 1151 // If we don't already have an ASTReader, create one now. 1152 if (!ModuleManager) { 1153 if (!hasASTContext()) 1154 createASTContext(); 1155 1156 // If we're not recursively building a module, check whether we 1157 // need to prune the module cache. 1158 if (getSourceManager().getModuleBuildStack().empty() && 1159 getHeaderSearchOpts().ModuleCachePruneInterval > 0 && 1160 getHeaderSearchOpts().ModuleCachePruneAfter > 0) { 1161 pruneModuleCache(getHeaderSearchOpts()); 1162 } 1163 1164 std::string Sysroot = getHeaderSearchOpts().Sysroot; 1165 const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 1166 ModuleManager = new ASTReader(getPreprocessor(), *Context, 1167 Sysroot.empty() ? "" : Sysroot.c_str(), 1168 PPOpts.DisablePCHValidation, 1169 /*AllowASTWithCompilerErrors=*/false, 1170 /*AllowConfigurationMismatch=*/false, 1171 /*ValidateSystemInputs=*/false, 1172 getFrontendOpts().UseGlobalModuleIndex); 1173 if (hasASTConsumer()) { 1174 ModuleManager->setDeserializationListener( 1175 getASTConsumer().GetASTDeserializationListener()); 1176 getASTContext().setASTMutationListener( 1177 getASTConsumer().GetASTMutationListener()); 1178 } 1179 OwningPtr<ExternalASTSource> Source; 1180 Source.reset(ModuleManager); 1181 getASTContext().setExternalSource(Source); 1182 if (hasSema()) 1183 ModuleManager->InitializeSema(getSema()); 1184 if (hasASTConsumer()) 1185 ModuleManager->StartTranslationUnit(&getASTConsumer()); 1186 } 1187 1188 // Try to load the module file. 1189 unsigned ARRFlags = ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing; 1190 switch (ModuleManager->ReadAST(ModuleFileName, serialization::MK_Module, 1191 ImportLoc, ARRFlags)) { 1192 case ASTReader::Success: 1193 break; 1194 1195 case ASTReader::OutOfDate: 1196 case ASTReader::Missing: { 1197 // The module file is missing or out-of-date. Build it. 1198 assert(Module && "missing module file"); 1199 // Check whether there is a cycle in the module graph. 1200 ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack(); 1201 ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end(); 1202 for (; Pos != PosEnd; ++Pos) { 1203 if (Pos->first == ModuleName) 1204 break; 1205 } 1206 1207 if (Pos != PosEnd) { 1208 SmallString<256> CyclePath; 1209 for (; Pos != PosEnd; ++Pos) { 1210 CyclePath += Pos->first; 1211 CyclePath += " -> "; 1212 } 1213 CyclePath += ModuleName; 1214 1215 getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle) 1216 << ModuleName << CyclePath; 1217 return ModuleLoadResult(); 1218 } 1219 1220 // Check whether we have already attempted to build this module (but 1221 // failed). 1222 if (getPreprocessorOpts().FailedModules && 1223 getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) { 1224 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built) 1225 << ModuleName 1226 << SourceRange(ImportLoc, ModuleNameLoc); 1227 ModuleBuildFailed = true; 1228 return ModuleLoadResult(); 1229 } 1230 1231 // Try to compile the module. 1232 compileModule(*this, ModuleNameLoc, Module, ModuleFileName); 1233 1234 // Try to read the module file, now that we've compiled it. 1235 ASTReader::ASTReadResult ReadResult 1236 = ModuleManager->ReadAST(ModuleFileName, 1237 serialization::MK_Module, ImportLoc, 1238 ASTReader::ARR_Missing); 1239 if (ReadResult != ASTReader::Success) { 1240 if (ReadResult == ASTReader::Missing) { 1241 getDiagnostics().Report(ModuleNameLoc, 1242 Module? diag::err_module_not_built 1243 : diag::err_module_not_found) 1244 << ModuleName 1245 << SourceRange(ImportLoc, ModuleNameLoc); 1246 } 1247 1248 if (getPreprocessorOpts().FailedModules) 1249 getPreprocessorOpts().FailedModules->addFailed(ModuleName); 1250 KnownModules[Path[0].first] = 0; 1251 ModuleBuildFailed = true; 1252 return ModuleLoadResult(); 1253 } 1254 1255 // Okay, we've rebuilt and now loaded the module. 1256 break; 1257 } 1258 1259 case ASTReader::VersionMismatch: 1260 case ASTReader::ConfigurationMismatch: 1261 case ASTReader::HadErrors: 1262 ModuleLoader::HadFatalFailure = true; 1263 // FIXME: The ASTReader will already have complained, but can we showhorn 1264 // that diagnostic information into a more useful form? 1265 KnownModules[Path[0].first] = 0; 1266 return ModuleLoadResult(); 1267 1268 case ASTReader::Failure: 1269 ModuleLoader::HadFatalFailure = true; 1270 // Already complained, but note now that we failed. 1271 KnownModules[Path[0].first] = 0; 1272 ModuleBuildFailed = true; 1273 return ModuleLoadResult(); 1274 } 1275 1276 // Cache the result of this top-level module lookup for later. 1277 Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first; 1278 } 1279 1280 // If we never found the module, fail. 1281 if (!Module) 1282 return ModuleLoadResult(); 1283 1284 // Verify that the rest of the module path actually corresponds to 1285 // a submodule. 1286 if (Path.size() > 1) { 1287 for (unsigned I = 1, N = Path.size(); I != N; ++I) { 1288 StringRef Name = Path[I].first->getName(); 1289 clang::Module *Sub = Module->findSubmodule(Name); 1290 1291 if (!Sub) { 1292 // Attempt to perform typo correction to find a module name that works. 1293 SmallVector<StringRef, 2> Best; 1294 unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)(); 1295 1296 for (clang::Module::submodule_iterator J = Module->submodule_begin(), 1297 JEnd = Module->submodule_end(); 1298 J != JEnd; ++J) { 1299 unsigned ED = Name.edit_distance((*J)->Name, 1300 /*AllowReplacements=*/true, 1301 BestEditDistance); 1302 if (ED <= BestEditDistance) { 1303 if (ED < BestEditDistance) { 1304 Best.clear(); 1305 BestEditDistance = ED; 1306 } 1307 1308 Best.push_back((*J)->Name); 1309 } 1310 } 1311 1312 // If there was a clear winner, user it. 1313 if (Best.size() == 1) { 1314 getDiagnostics().Report(Path[I].second, 1315 diag::err_no_submodule_suggest) 1316 << Path[I].first << Module->getFullModuleName() << Best[0] 1317 << SourceRange(Path[0].second, Path[I-1].second) 1318 << FixItHint::CreateReplacement(SourceRange(Path[I].second), 1319 Best[0]); 1320 1321 Sub = Module->findSubmodule(Best[0]); 1322 } 1323 } 1324 1325 if (!Sub) { 1326 // No submodule by this name. Complain, and don't look for further 1327 // submodules. 1328 getDiagnostics().Report(Path[I].second, diag::err_no_submodule) 1329 << Path[I].first << Module->getFullModuleName() 1330 << SourceRange(Path[0].second, Path[I-1].second); 1331 break; 1332 } 1333 1334 Module = Sub; 1335 } 1336 } 1337 1338 // Make the named module visible, if it's not already part of the module 1339 // we are parsing. 1340 if (ModuleName != getLangOpts().CurrentModule) { 1341 if (!Module->IsFromModuleFile) { 1342 // We have an umbrella header or directory that doesn't actually include 1343 // all of the headers within the directory it covers. Complain about 1344 // this missing submodule and recover by forgetting that we ever saw 1345 // this submodule. 1346 // FIXME: Should we detect this at module load time? It seems fairly 1347 // expensive (and rare). 1348 getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule) 1349 << Module->getFullModuleName() 1350 << SourceRange(Path.front().second, Path.back().second); 1351 1352 return ModuleLoadResult(0, true); 1353 } 1354 1355 // Check whether this module is available. 1356 clang::Module::Requirement Requirement; 1357 clang::Module::HeaderDirective MissingHeader; 1358 if (!Module->isAvailable(getLangOpts(), getTarget(), Requirement, 1359 MissingHeader)) { 1360 if (MissingHeader.FileNameLoc.isValid()) { 1361 getDiagnostics().Report(MissingHeader.FileNameLoc, 1362 diag::err_module_header_missing) 1363 << MissingHeader.IsUmbrella << MissingHeader.FileName; 1364 } else { 1365 getDiagnostics().Report(ImportLoc, diag::err_module_unavailable) 1366 << Module->getFullModuleName() 1367 << Requirement.second << Requirement.first 1368 << SourceRange(Path.front().second, Path.back().second); 1369 } 1370 LastModuleImportLoc = ImportLoc; 1371 LastModuleImportResult = ModuleLoadResult(); 1372 return ModuleLoadResult(); 1373 } 1374 1375 ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc, 1376 /*Complain=*/true); 1377 } 1378 1379 // Check for any configuration macros that have changed. 1380 clang::Module *TopModule = Module->getTopLevelModule(); 1381 for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) { 1382 checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I], 1383 Module, ImportLoc); 1384 } 1385 1386 // If this module import was due to an inclusion directive, create an 1387 // implicit import declaration to capture it in the AST. 1388 if (IsInclusionDirective && hasASTContext()) { 1389 TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); 1390 ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, 1391 ImportLoc, Module, 1392 Path.back().second); 1393 TU->addDecl(ImportD); 1394 if (Consumer) 1395 Consumer->HandleImplicitImportDecl(ImportD); 1396 } 1397 1398 LastModuleImportLoc = ImportLoc; 1399 LastModuleImportResult = ModuleLoadResult(Module, false); 1400 return LastModuleImportResult; 1401 } 1402 1403 void CompilerInstance::makeModuleVisible(Module *Mod, 1404 Module::NameVisibilityKind Visibility, 1405 SourceLocation ImportLoc, 1406 bool Complain){ 1407 ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc, Complain); 1408 } 1409 1410