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