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