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/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/Lex/HeaderSearch.h" 20 #include "clang/Lex/Preprocessor.h" 21 #include "clang/Lex/PTHManager.h" 22 #include "clang/Frontend/ChainedDiagnosticClient.h" 23 #include "clang/Frontend/FrontendAction.h" 24 #include "clang/Frontend/FrontendActions.h" 25 #include "clang/Frontend/FrontendDiagnostic.h" 26 #include "clang/Frontend/LogDiagnosticPrinter.h" 27 #include "clang/Frontend/TextDiagnosticPrinter.h" 28 #include "clang/Frontend/VerifyDiagnosticsClient.h" 29 #include "clang/Frontend/Utils.h" 30 #include "clang/Serialization/ASTReader.h" 31 #include "clang/Sema/CodeCompleteConsumer.h" 32 #include "llvm/Support/FileSystem.h" 33 #include "llvm/Support/MemoryBuffer.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include "llvm/ADT/Statistic.h" 36 #include "llvm/Support/Timer.h" 37 #include "llvm/Support/Host.h" 38 #include "llvm/Support/Path.h" 39 #include "llvm/Support/Program.h" 40 #include "llvm/Support/Signals.h" 41 #include "llvm/Support/system_error.h" 42 #include "llvm/Config/config.h" 43 using namespace clang; 44 45 CompilerInstance::CompilerInstance() 46 : Invocation(new CompilerInvocation()), ModuleManager(0) { 47 } 48 49 CompilerInstance::~CompilerInstance() { 50 } 51 52 void CompilerInstance::setInvocation(CompilerInvocation *Value) { 53 Invocation = Value; 54 } 55 56 void CompilerInstance::setDiagnostics(Diagnostic *Value) { 57 Diagnostics = Value; 58 } 59 60 void CompilerInstance::setTarget(TargetInfo *Value) { 61 Target = Value; 62 } 63 64 void CompilerInstance::setFileManager(FileManager *Value) { 65 FileMgr = Value; 66 } 67 68 void CompilerInstance::setSourceManager(SourceManager *Value) { 69 SourceMgr = Value; 70 } 71 72 void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; } 73 74 void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; } 75 76 void CompilerInstance::setSema(Sema *S) { 77 TheSema.reset(S); 78 } 79 80 void CompilerInstance::setASTConsumer(ASTConsumer *Value) { 81 Consumer.reset(Value); 82 } 83 84 void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { 85 CompletionConsumer.reset(Value); 86 } 87 88 // Diagnostics 89 static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, 90 unsigned argc, const char* const *argv, 91 Diagnostic &Diags) { 92 std::string ErrorInfo; 93 llvm::OwningPtr<raw_ostream> OS( 94 new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo)); 95 if (!ErrorInfo.empty()) { 96 Diags.Report(diag::err_fe_unable_to_open_logfile) 97 << DiagOpts.DumpBuildInformation << ErrorInfo; 98 return; 99 } 100 101 (*OS) << "clang -cc1 command line arguments: "; 102 for (unsigned i = 0; i != argc; ++i) 103 (*OS) << argv[i] << ' '; 104 (*OS) << '\n'; 105 106 // Chain in a diagnostic client which will log the diagnostics. 107 DiagnosticClient *Logger = 108 new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true); 109 Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger)); 110 } 111 112 static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts, 113 const CodeGenOptions *CodeGenOpts, 114 Diagnostic &Diags) { 115 std::string ErrorInfo; 116 bool OwnsStream = false; 117 raw_ostream *OS = &llvm::errs(); 118 if (DiagOpts.DiagnosticLogFile != "-") { 119 // Create the output stream. 120 llvm::raw_fd_ostream *FileOS( 121 new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(), 122 ErrorInfo, llvm::raw_fd_ostream::F_Append)); 123 if (!ErrorInfo.empty()) { 124 Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) 125 << DiagOpts.DumpBuildInformation << ErrorInfo; 126 } else { 127 FileOS->SetUnbuffered(); 128 FileOS->SetUseAtomicWrites(true); 129 OS = FileOS; 130 OwnsStream = true; 131 } 132 } 133 134 // Chain in the diagnostic client which will log the diagnostics. 135 LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts, 136 OwnsStream); 137 if (CodeGenOpts) 138 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); 139 Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger)); 140 } 141 142 void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv, 143 DiagnosticClient *Client, 144 bool ShouldOwnClient) { 145 Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client, 146 ShouldOwnClient, &getCodeGenOpts()); 147 } 148 149 llvm::IntrusiveRefCntPtr<Diagnostic> 150 CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts, 151 int Argc, const char* const *Argv, 152 DiagnosticClient *Client, 153 bool ShouldOwnClient, 154 const CodeGenOptions *CodeGenOpts) { 155 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 156 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID)); 157 158 // Create the diagnostic client for reporting errors or for 159 // implementing -verify. 160 if (Client) 161 Diags->setClient(Client, ShouldOwnClient); 162 else 163 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); 164 165 // Chain in -verify checker, if requested. 166 if (Opts.VerifyDiagnostics) 167 Diags->setClient(new VerifyDiagnosticsClient(*Diags)); 168 169 // Chain in -diagnostic-log-file dumper, if requested. 170 if (!Opts.DiagnosticLogFile.empty()) 171 SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); 172 173 if (!Opts.DumpBuildInformation.empty()) 174 SetUpBuildDumpLog(Opts, Argc, Argv, *Diags); 175 176 // Configure our handling of diagnostics. 177 ProcessWarningOptions(*Diags, Opts); 178 179 return Diags; 180 } 181 182 // File Manager 183 184 void CompilerInstance::createFileManager() { 185 FileMgr = new FileManager(getFileSystemOpts()); 186 } 187 188 // Source Manager 189 190 void CompilerInstance::createSourceManager(FileManager &FileMgr) { 191 SourceMgr = new SourceManager(getDiagnostics(), FileMgr); 192 } 193 194 // Preprocessor 195 196 void CompilerInstance::createPreprocessor() { 197 const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 198 199 // Create a PTH manager if we are using some form of a token cache. 200 PTHManager *PTHMgr = 0; 201 if (!PPOpts.TokenCache.empty()) 202 PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics()); 203 204 // Create the Preprocessor. 205 HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager()); 206 PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(), 207 getSourceManager(), *HeaderInfo, *this, PTHMgr, 208 /*OwnsHeaderSearch=*/true); 209 210 // Note that this is different then passing PTHMgr to Preprocessor's ctor. 211 // That argument is used as the IdentifierInfoLookup argument to 212 // IdentifierTable's ctor. 213 if (PTHMgr) { 214 PTHMgr->setPreprocessor(&*PP); 215 PP->setPTHManager(PTHMgr); 216 } 217 218 if (PPOpts.DetailedRecord) 219 PP->createPreprocessingRecord( 220 PPOpts.DetailedRecordIncludesNestedMacroExpansions); 221 222 InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts()); 223 224 // Set up the module path, including the hash for the 225 // module-creation options. 226 llvm::SmallString<256> SpecificModuleCache( 227 getHeaderSearchOpts().ModuleCachePath); 228 if (!getHeaderSearchOpts().DisableModuleHash) 229 llvm::sys::path::append(SpecificModuleCache, 230 getInvocation().getModuleHash()); 231 PP->getHeaderSearchInfo().configureModules(SpecificModuleCache, 232 getPreprocessorOpts().ModuleBuildPath.empty() 233 ? std::string() 234 : getPreprocessorOpts().ModuleBuildPath.back()); 235 236 // Handle generating dependencies, if requested. 237 const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); 238 if (!DepOpts.OutputFile.empty()) 239 AttachDependencyFileGen(*PP, DepOpts); 240 241 // Handle generating header include information, if requested. 242 if (DepOpts.ShowHeaderIncludes) 243 AttachHeaderIncludeGen(*PP); 244 if (!DepOpts.HeaderIncludeOutputFile.empty()) { 245 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; 246 if (OutputPath == "-") 247 OutputPath = ""; 248 AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, 249 /*ShowDepth=*/false); 250 } 251 } 252 253 // ASTContext 254 255 void CompilerInstance::createASTContext() { 256 Preprocessor &PP = getPreprocessor(); 257 Context = new ASTContext(getLangOpts(), PP.getSourceManager(), 258 &getTarget(), PP.getIdentifierTable(), 259 PP.getSelectorTable(), PP.getBuiltinInfo(), 260 /*size_reserve=*/ 0); 261 } 262 263 // ExternalASTSource 264 265 void CompilerInstance::createPCHExternalASTSource(StringRef Path, 266 bool DisablePCHValidation, 267 bool DisableStatCache, 268 void *DeserializationListener){ 269 llvm::OwningPtr<ExternalASTSource> Source; 270 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; 271 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, 272 DisablePCHValidation, 273 DisableStatCache, 274 getPreprocessor(), getASTContext(), 275 DeserializationListener, 276 Preamble)); 277 ModuleManager = static_cast<ASTReader*>(Source.get()); 278 getASTContext().setExternalSource(Source); 279 } 280 281 ExternalASTSource * 282 CompilerInstance::createPCHExternalASTSource(StringRef Path, 283 const std::string &Sysroot, 284 bool DisablePCHValidation, 285 bool DisableStatCache, 286 Preprocessor &PP, 287 ASTContext &Context, 288 void *DeserializationListener, 289 bool Preamble) { 290 llvm::OwningPtr<ASTReader> Reader; 291 Reader.reset(new ASTReader(PP, Context, 292 Sysroot.empty() ? "" : Sysroot.c_str(), 293 DisablePCHValidation, DisableStatCache)); 294 295 Reader->setDeserializationListener( 296 static_cast<ASTDeserializationListener *>(DeserializationListener)); 297 switch (Reader->ReadAST(Path, 298 Preamble ? serialization::MK_Preamble 299 : serialization::MK_PCH)) { 300 case ASTReader::Success: 301 // Set the predefines buffer as suggested by the PCH reader. Typically, the 302 // predefines buffer will be empty. 303 PP.setPredefines(Reader->getSuggestedPredefines()); 304 return Reader.take(); 305 306 case ASTReader::Failure: 307 // Unrecoverable failure: don't even try to process the input file. 308 break; 309 310 case ASTReader::IgnorePCH: 311 // No suitable PCH file could be found. Return an error. 312 break; 313 } 314 315 return 0; 316 } 317 318 // Code Completion 319 320 static bool EnableCodeCompletion(Preprocessor &PP, 321 const std::string &Filename, 322 unsigned Line, 323 unsigned Column) { 324 // Tell the source manager to chop off the given file at a specific 325 // line and column. 326 const FileEntry *Entry = PP.getFileManager().getFile(Filename); 327 if (!Entry) { 328 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) 329 << Filename; 330 return true; 331 } 332 333 // Truncate the named file at the given line/column. 334 PP.SetCodeCompletionPoint(Entry, Line, Column); 335 return false; 336 } 337 338 void CompilerInstance::createCodeCompletionConsumer() { 339 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; 340 if (!CompletionConsumer) { 341 CompletionConsumer.reset( 342 createCodeCompletionConsumer(getPreprocessor(), 343 Loc.FileName, Loc.Line, Loc.Column, 344 getFrontendOpts().ShowMacrosInCodeCompletion, 345 getFrontendOpts().ShowCodePatternsInCodeCompletion, 346 getFrontendOpts().ShowGlobalSymbolsInCodeCompletion, 347 llvm::outs())); 348 if (!CompletionConsumer) 349 return; 350 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, 351 Loc.Line, Loc.Column)) { 352 CompletionConsumer.reset(); 353 return; 354 } 355 356 if (CompletionConsumer->isOutputBinary() && 357 llvm::sys::Program::ChangeStdoutToBinary()) { 358 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); 359 CompletionConsumer.reset(); 360 } 361 } 362 363 void CompilerInstance::createFrontendTimer() { 364 FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); 365 } 366 367 CodeCompleteConsumer * 368 CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, 369 const std::string &Filename, 370 unsigned Line, 371 unsigned Column, 372 bool ShowMacros, 373 bool ShowCodePatterns, 374 bool ShowGlobals, 375 raw_ostream &OS) { 376 if (EnableCodeCompletion(PP, Filename, Line, Column)) 377 return 0; 378 379 // Set up the creation routine for code-completion. 380 return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns, 381 ShowGlobals, OS); 382 } 383 384 void CompilerInstance::createSema(TranslationUnitKind TUKind, 385 CodeCompleteConsumer *CompletionConsumer) { 386 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), 387 TUKind, CompletionConsumer)); 388 } 389 390 // Output Files 391 392 void CompilerInstance::addOutputFile(const OutputFile &OutFile) { 393 assert(OutFile.OS && "Attempt to add empty stream to output list!"); 394 OutputFiles.push_back(OutFile); 395 } 396 397 void CompilerInstance::clearOutputFiles(bool EraseFiles) { 398 for (std::list<OutputFile>::iterator 399 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { 400 delete it->OS; 401 if (!it->TempFilename.empty()) { 402 if (EraseFiles) { 403 bool existed; 404 llvm::sys::fs::remove(it->TempFilename, existed); 405 } else { 406 llvm::SmallString<128> NewOutFile(it->Filename); 407 408 // If '-working-directory' was passed, the output filename should be 409 // relative to that. 410 FileMgr->FixupRelativePath(NewOutFile); 411 if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename, 412 NewOutFile.str())) { 413 getDiagnostics().Report(diag::err_fe_unable_to_rename_temp) 414 << it->TempFilename << it->Filename << ec.message(); 415 416 bool existed; 417 llvm::sys::fs::remove(it->TempFilename, existed); 418 } 419 } 420 } else if (!it->Filename.empty() && EraseFiles) 421 llvm::sys::Path(it->Filename).eraseFromDisk(); 422 423 } 424 OutputFiles.clear(); 425 } 426 427 llvm::raw_fd_ostream * 428 CompilerInstance::createDefaultOutputFile(bool Binary, 429 StringRef InFile, 430 StringRef Extension) { 431 return createOutputFile(getFrontendOpts().OutputFile, Binary, 432 /*RemoveFileOnSignal=*/true, InFile, Extension); 433 } 434 435 llvm::raw_fd_ostream * 436 CompilerInstance::createOutputFile(StringRef OutputPath, 437 bool Binary, bool RemoveFileOnSignal, 438 StringRef InFile, 439 StringRef Extension, 440 bool UseTemporary) { 441 std::string Error, OutputPathName, TempPathName; 442 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, 443 RemoveFileOnSignal, 444 InFile, Extension, 445 UseTemporary, 446 &OutputPathName, 447 &TempPathName); 448 if (!OS) { 449 getDiagnostics().Report(diag::err_fe_unable_to_open_output) 450 << OutputPath << Error; 451 return 0; 452 } 453 454 // Add the output file -- but don't try to remove "-", since this means we are 455 // using stdin. 456 addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", 457 TempPathName, OS)); 458 459 return OS; 460 } 461 462 llvm::raw_fd_ostream * 463 CompilerInstance::createOutputFile(StringRef OutputPath, 464 std::string &Error, 465 bool Binary, 466 bool RemoveFileOnSignal, 467 StringRef InFile, 468 StringRef Extension, 469 bool UseTemporary, 470 std::string *ResultPathName, 471 std::string *TempPathName) { 472 std::string OutFile, TempFile; 473 if (!OutputPath.empty()) { 474 OutFile = OutputPath; 475 } else if (InFile == "-") { 476 OutFile = "-"; 477 } else if (!Extension.empty()) { 478 llvm::sys::Path Path(InFile); 479 Path.eraseSuffix(); 480 Path.appendSuffix(Extension); 481 OutFile = Path.str(); 482 } else { 483 OutFile = "-"; 484 } 485 486 llvm::OwningPtr<llvm::raw_fd_ostream> OS; 487 std::string OSFile; 488 489 if (UseTemporary && OutFile != "-") { 490 llvm::sys::Path OutPath(OutFile); 491 // Only create the temporary if we can actually write to OutPath, otherwise 492 // we want to fail early. 493 bool Exists; 494 if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) || 495 (OutPath.isRegularFile() && OutPath.canWrite())) { 496 // Create a temporary file. 497 llvm::SmallString<128> TempPath; 498 TempPath = OutFile; 499 TempPath += "-%%%%%%%%"; 500 int fd; 501 if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, 502 /*makeAbsolute=*/false) == llvm::errc::success) { 503 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); 504 OSFile = TempFile = TempPath.str(); 505 } 506 } 507 } 508 509 if (!OS) { 510 OSFile = OutFile; 511 OS.reset( 512 new llvm::raw_fd_ostream(OSFile.c_str(), Error, 513 (Binary ? llvm::raw_fd_ostream::F_Binary : 0))); 514 if (!Error.empty()) 515 return 0; 516 } 517 518 // Make sure the out stream file gets removed if we crash. 519 if (RemoveFileOnSignal) 520 llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile)); 521 522 if (ResultPathName) 523 *ResultPathName = OutFile; 524 if (TempPathName) 525 *TempPathName = TempFile; 526 527 return OS.take(); 528 } 529 530 // Initialization Utilities 531 532 bool CompilerInstance::InitializeSourceManager(StringRef InputFile) { 533 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(), 534 getSourceManager(), getFrontendOpts()); 535 } 536 537 bool CompilerInstance::InitializeSourceManager(StringRef InputFile, 538 Diagnostic &Diags, 539 FileManager &FileMgr, 540 SourceManager &SourceMgr, 541 const FrontendOptions &Opts) { 542 // Figure out where to get and map in the main file, unless it's already 543 // been created (e.g., by a precompiled preamble). 544 if (!SourceMgr.getMainFileID().isInvalid()) { 545 // Do nothing: the main file has already been set. 546 } else if (InputFile != "-") { 547 const FileEntry *File = FileMgr.getFile(InputFile); 548 if (!File) { 549 Diags.Report(diag::err_fe_error_reading) << InputFile; 550 return false; 551 } 552 SourceMgr.createMainFileID(File); 553 } else { 554 llvm::OwningPtr<llvm::MemoryBuffer> SB; 555 if (llvm::MemoryBuffer::getSTDIN(SB)) { 556 // FIXME: Give ec.message() in this diag. 557 Diags.Report(diag::err_fe_error_reading_stdin); 558 return false; 559 } 560 const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), 561 SB->getBufferSize(), 0); 562 SourceMgr.createMainFileID(File); 563 SourceMgr.overrideFileContents(File, SB.take()); 564 } 565 566 assert(!SourceMgr.getMainFileID().isInvalid() && 567 "Couldn't establish MainFileID!"); 568 return true; 569 } 570 571 // High-Level Operations 572 573 bool CompilerInstance::ExecuteAction(FrontendAction &Act) { 574 assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); 575 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); 576 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); 577 578 // FIXME: Take this as an argument, once all the APIs we used have moved to 579 // taking it as an input instead of hard-coding llvm::errs. 580 raw_ostream &OS = llvm::errs(); 581 582 // Create the target instance. 583 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts())); 584 if (!hasTarget()) 585 return false; 586 587 // Inform the target of the language options. 588 // 589 // FIXME: We shouldn't need to do this, the target should be immutable once 590 // created. This complexity should be lifted elsewhere. 591 getTarget().setForcedLangOptions(getLangOpts()); 592 593 // Validate/process some options. 594 if (getHeaderSearchOpts().Verbose) 595 OS << "clang -cc1 version " CLANG_VERSION_STRING 596 << " based upon " << PACKAGE_STRING 597 << " hosted on " << llvm::sys::getHostTriple() << "\n"; 598 599 if (getFrontendOpts().ShowTimers) 600 createFrontendTimer(); 601 602 if (getFrontendOpts().ShowStats) 603 llvm::EnableStatistics(); 604 605 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { 606 const std::string &InFile = getFrontendOpts().Inputs[i].second; 607 608 // Reset the ID tables if we are reusing the SourceManager. 609 if (hasSourceManager()) 610 getSourceManager().clearIDTables(); 611 612 if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) { 613 Act.Execute(); 614 Act.EndSourceFile(); 615 } 616 } 617 618 if (getDiagnosticOpts().ShowCarets) { 619 // We can have multiple diagnostics sharing one diagnostic client. 620 // Get the total number of warnings/errors from the client. 621 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); 622 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); 623 624 if (NumWarnings) 625 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); 626 if (NumWarnings && NumErrors) 627 OS << " and "; 628 if (NumErrors) 629 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); 630 if (NumWarnings || NumErrors) 631 OS << " generated.\n"; 632 } 633 634 if (getFrontendOpts().ShowStats && hasFileManager()) { 635 getFileManager().PrintStats(); 636 OS << "\n"; 637 } 638 639 return !getDiagnostics().getClient()->getNumErrors(); 640 } 641 642 /// \brief Determine the appropriate source input kind based on language 643 /// options. 644 static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) { 645 if (LangOpts.OpenCL) 646 return IK_OpenCL; 647 if (LangOpts.CUDA) 648 return IK_CUDA; 649 if (LangOpts.ObjC1) 650 return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC; 651 return LangOpts.CPlusPlus? IK_CXX : IK_C; 652 } 653 654 /// \brief Compile a module file for the given module name with the given 655 /// umbrella header, using the options provided by the importing compiler 656 /// instance. 657 static void compileModule(CompilerInstance &ImportingInstance, 658 StringRef ModuleName, 659 StringRef ModuleFileName, 660 StringRef UmbrellaHeader) { 661 // Construct a compiler invocation for creating this module. 662 llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation 663 (new CompilerInvocation(ImportingInstance.getInvocation())); 664 665 // For any options that aren't intended to affect how a module is built, 666 // reset them to their default values. 667 Invocation->getLangOpts().resetNonModularOptions(); 668 Invocation->getPreprocessorOpts().resetNonModularOptions(); 669 670 // Note that this module is part of the module build path, so that we 671 // can detect cycles in the module graph. 672 Invocation->getPreprocessorOpts().ModuleBuildPath.push_back(ModuleName); 673 674 // Set up the inputs/outputs so that we build the module from its umbrella 675 // header. 676 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); 677 FrontendOpts.OutputFile = ModuleFileName.str(); 678 FrontendOpts.DisableFree = false; 679 FrontendOpts.Inputs.clear(); 680 FrontendOpts.Inputs.push_back( 681 std::make_pair(getSourceInputKindFromOptions(Invocation->getLangOpts()), 682 UmbrellaHeader)); 683 684 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; 685 686 687 assert(ImportingInstance.getInvocation().getModuleHash() == 688 Invocation->getModuleHash() && "Module hash mismatch!"); 689 690 // Construct a compiler instance that will be used to actually create the 691 // module. 692 CompilerInstance Instance; 693 Instance.setInvocation(&*Invocation); 694 Instance.createDiagnostics(/*argc=*/0, /*argv=*/0, 695 &ImportingInstance.getDiagnosticClient(), 696 /*ShouldOwnClient=*/false); 697 698 // Construct a module-generating action. 699 GeneratePCHAction CreateModuleAction(true); 700 701 // Execute the action to actually build the module in-place. 702 // FIXME: Need to synchronize when multiple processes do this. 703 Instance.ExecuteAction(CreateModuleAction); 704 705 // Tell the diagnostic client that it's (re-)starting to process a source 706 // file. 707 // FIXME: This is a hack. We probably want to clone the diagnostic client. 708 ImportingInstance.getDiagnosticClient() 709 .BeginSourceFile(ImportingInstance.getLangOpts(), 710 &ImportingInstance.getPreprocessor()); 711 } 712 713 ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc, 714 IdentifierInfo &ModuleName, 715 SourceLocation ModuleNameLoc) { 716 // Determine what file we're searching from. 717 SourceManager &SourceMgr = getSourceManager(); 718 SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc); 719 const FileEntry *CurFile 720 = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc)); 721 if (!CurFile) 722 CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); 723 724 // Search for a module with the given name. 725 std::string UmbrellaHeader; 726 std::string ModuleFileName; 727 const FileEntry *ModuleFile 728 = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName(), 729 &ModuleFileName, 730 &UmbrellaHeader); 731 732 bool BuildingModule = false; 733 if (!ModuleFile && !UmbrellaHeader.empty()) { 734 // We didn't find the module, but there is an umbrella header that 735 // can be used to create the module file. Create a separate compilation 736 // module to do so. 737 738 // Check whether there is a cycle in the module graph. 739 SmallVectorImpl<std::string> &ModuleBuildPath 740 = getPreprocessorOpts().ModuleBuildPath; 741 SmallVectorImpl<std::string>::iterator Pos 742 = std::find(ModuleBuildPath.begin(), ModuleBuildPath.end(), 743 ModuleName.getName()); 744 if (Pos != ModuleBuildPath.end()) { 745 llvm::SmallString<256> CyclePath; 746 for (; Pos != ModuleBuildPath.end(); ++Pos) { 747 CyclePath += *Pos; 748 CyclePath += " -> "; 749 } 750 CyclePath += ModuleName.getName(); 751 752 getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle) 753 << ModuleName.getName() << CyclePath; 754 return 0; 755 } 756 757 BuildingModule = true; 758 compileModule(*this, ModuleName.getName(), ModuleFileName, UmbrellaHeader); 759 ModuleFile = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName()); 760 } 761 762 if (!ModuleFile) { 763 getDiagnostics().Report(ModuleNameLoc, 764 BuildingModule? diag::err_module_not_built 765 : diag::err_module_not_found) 766 << ModuleName.getName() 767 << SourceRange(ImportLoc, ModuleNameLoc); 768 return 0; 769 } 770 771 // If we don't already have an ASTReader, create one now. 772 if (!ModuleManager) { 773 if (!hasASTContext()) 774 createASTContext(); 775 776 std::string Sysroot = getHeaderSearchOpts().Sysroot; 777 const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 778 ModuleManager = new ASTReader(getPreprocessor(), *Context, 779 Sysroot.empty() ? "" : Sysroot.c_str(), 780 PPOpts.DisablePCHValidation, 781 PPOpts.DisableStatCache); 782 if (hasASTConsumer()) { 783 ModuleManager->setDeserializationListener( 784 getASTConsumer().GetASTDeserializationListener()); 785 getASTContext().setASTMutationListener( 786 getASTConsumer().GetASTMutationListener()); 787 } 788 llvm::OwningPtr<ExternalASTSource> Source; 789 Source.reset(ModuleManager); 790 getASTContext().setExternalSource(Source); 791 if (hasSema()) 792 ModuleManager->InitializeSema(getSema()); 793 if (hasASTConsumer()) 794 ModuleManager->StartTranslationUnit(&getASTConsumer()); 795 } 796 797 // Try to load the module we found. 798 switch (ModuleManager->ReadAST(ModuleFile->getName(), 799 serialization::MK_Module)) { 800 case ASTReader::Success: 801 break; 802 803 case ASTReader::IgnorePCH: 804 // FIXME: The ASTReader will already have complained, but can we showhorn 805 // that diagnostic information into a more useful form? 806 return 0; 807 808 case ASTReader::Failure: 809 // Already complained. 810 return 0; 811 } 812 813 // FIXME: The module file's FileEntry makes a poor key indeed! 814 return (ModuleKey)ModuleFile; 815 } 816 817