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