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