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