CompilerInstance.cpp revision 4f7121ffc46611335a36c15ea42d6591b8c288ab
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/FrontendDiagnostic.h" 25#include "clang/Frontend/TextDiagnosticPrinter.h" 26#include "clang/Frontend/VerifyDiagnosticsClient.h" 27#include "clang/Frontend/Utils.h" 28#include "clang/Serialization/PCHReader.h" 29#include "clang/Sema/CodeCompleteConsumer.h" 30#include "llvm/LLVMContext.h" 31#include "llvm/Support/MemoryBuffer.h" 32#include "llvm/Support/raw_ostream.h" 33#include "llvm/ADT/Statistic.h" 34#include "llvm/Support/Timer.h" 35#include "llvm/System/Host.h" 36#include "llvm/System/Path.h" 37#include "llvm/System/Program.h" 38using namespace clang; 39 40CompilerInstance::CompilerInstance() 41 : Invocation(new CompilerInvocation()) { 42} 43 44CompilerInstance::~CompilerInstance() { 45} 46 47void CompilerInstance::setLLVMContext(llvm::LLVMContext *Value) { 48 LLVMContext.reset(Value); 49} 50 51void CompilerInstance::setInvocation(CompilerInvocation *Value) { 52 Invocation.reset(Value); 53} 54 55void CompilerInstance::setDiagnostics(Diagnostic *Value) { 56 Diagnostics = Value; 57} 58 59void CompilerInstance::setDiagnosticClient(DiagnosticClient *Value) { 60 DiagClient.reset(Value); 61} 62 63void CompilerInstance::setTarget(TargetInfo *Value) { 64 Target.reset(Value); 65} 66 67void CompilerInstance::setFileManager(FileManager *Value) { 68 FileMgr.reset(Value); 69} 70 71void CompilerInstance::setSourceManager(SourceManager *Value) { 72 SourceMgr.reset(Value); 73} 74 75void CompilerInstance::setPreprocessor(Preprocessor *Value) { 76 PP.reset(Value); 77} 78 79void CompilerInstance::setASTContext(ASTContext *Value) { 80 Context.reset(Value); 81} 82 83void CompilerInstance::setSema(Sema *S) { 84 TheSema.reset(S); 85} 86 87void CompilerInstance::setASTConsumer(ASTConsumer *Value) { 88 Consumer.reset(Value); 89} 90 91void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { 92 CompletionConsumer.reset(Value); 93} 94 95// Diagnostics 96namespace { 97 class BinaryDiagnosticSerializer : public DiagnosticClient { 98 llvm::raw_ostream &OS; 99 SourceManager *SourceMgr; 100 public: 101 explicit BinaryDiagnosticSerializer(llvm::raw_ostream &OS) 102 : OS(OS), SourceMgr(0) { } 103 104 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel, 105 const DiagnosticInfo &Info); 106 }; 107} 108 109void BinaryDiagnosticSerializer::HandleDiagnostic(Diagnostic::Level DiagLevel, 110 const DiagnosticInfo &Info) { 111 StoredDiagnostic(DiagLevel, Info).Serialize(OS); 112} 113 114static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, 115 unsigned argc, char **argv, 116 Diagnostic &Diags) { 117 std::string ErrorInfo; 118 llvm::OwningPtr<llvm::raw_ostream> OS( 119 new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo)); 120 if (!ErrorInfo.empty()) { 121 Diags.Report(diag::err_fe_unable_to_open_logfile) 122 << DiagOpts.DumpBuildInformation << ErrorInfo; 123 return; 124 } 125 126 (*OS) << "clang -cc1 command line arguments: "; 127 for (unsigned i = 0; i != argc; ++i) 128 (*OS) << argv[i] << ' '; 129 (*OS) << '\n'; 130 131 // Chain in a diagnostic client which will log the diagnostics. 132 DiagnosticClient *Logger = 133 new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true); 134 Diags.setClient(new ChainedDiagnosticClient(Diags.getClient(), Logger)); 135} 136 137void CompilerInstance::createDiagnostics(int Argc, char **Argv) { 138 Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv); 139 140 if (Diagnostics) 141 DiagClient.reset(Diagnostics->getClient()); 142} 143 144llvm::IntrusiveRefCntPtr<Diagnostic> 145CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts, 146 int Argc, char **Argv) { 147 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic()); 148 149 // Create the diagnostic client for reporting errors or for 150 // implementing -verify. 151 llvm::OwningPtr<DiagnosticClient> DiagClient; 152 if (Opts.BinaryOutput) { 153 if (llvm::sys::Program::ChangeStderrToBinary()) { 154 // We weren't able to set standard error to binary, which is a 155 // bit of a problem. So, just create a text diagnostic printer 156 // to complain about this problem, and pretend that the user 157 // didn't try to use binary output. 158 DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), Opts)); 159 Diags->setClient(DiagClient.take()); 160 Diags->Report(diag::err_fe_stderr_binary); 161 return Diags; 162 } else { 163 DiagClient.reset(new BinaryDiagnosticSerializer(llvm::errs())); 164 } 165 } else { 166 DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), Opts)); 167 } 168 169 // Chain in -verify checker, if requested. 170 if (Opts.VerifyDiagnostics) 171 DiagClient.reset(new VerifyDiagnosticsClient(*Diags, DiagClient.take())); 172 173 Diags->setClient(DiagClient.take()); 174 if (!Opts.DumpBuildInformation.empty()) 175 SetUpBuildDumpLog(Opts, Argc, Argv, *Diags); 176 177 // Configure our handling of diagnostics. 178 ProcessWarningOptions(*Diags, Opts); 179 180 return Diags; 181} 182 183// File Manager 184 185void CompilerInstance::createFileManager() { 186 FileMgr.reset(new FileManager()); 187} 188 189// Source Manager 190 191void CompilerInstance::createSourceManager() { 192 SourceMgr.reset(new SourceManager(getDiagnostics())); 193} 194 195// Preprocessor 196 197void CompilerInstance::createPreprocessor() { 198 PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(), 199 getPreprocessorOpts(), getHeaderSearchOpts(), 200 getDependencyOutputOpts(), getTarget(), 201 getFrontendOpts(), getSourceManager(), 202 getFileManager())); 203} 204 205Preprocessor * 206CompilerInstance::createPreprocessor(Diagnostic &Diags, 207 const LangOptions &LangInfo, 208 const PreprocessorOptions &PPOpts, 209 const HeaderSearchOptions &HSOpts, 210 const DependencyOutputOptions &DepOpts, 211 const TargetInfo &Target, 212 const FrontendOptions &FEOpts, 213 SourceManager &SourceMgr, 214 FileManager &FileMgr) { 215 // Create a PTH manager if we are using some form of a token cache. 216 PTHManager *PTHMgr = 0; 217 if (!PPOpts.TokenCache.empty()) 218 PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags); 219 220 // Create the Preprocessor. 221 HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr); 222 Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target, 223 SourceMgr, *HeaderInfo, 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, HSOpts, FEOpts); 238 239 // Handle generating dependencies, if requested. 240 if (!DepOpts.OutputFile.empty()) 241 AttachDependencyFileGen(*PP, DepOpts); 242 243 return PP; 244} 245 246// ASTContext 247 248void CompilerInstance::createASTContext() { 249 Preprocessor &PP = getPreprocessor(); 250 Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(), 251 getTarget(), PP.getIdentifierTable(), 252 PP.getSelectorTable(), PP.getBuiltinInfo(), 253 /*size_reserve=*/ 0)); 254} 255 256// ExternalASTSource 257 258void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path, 259 bool DisablePCHValidation, 260 void *DeserializationListener){ 261 llvm::OwningPtr<ExternalASTSource> Source; 262 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, 263 DisablePCHValidation, 264 getPreprocessor(), getASTContext(), 265 DeserializationListener)); 266 getASTContext().setExternalSource(Source); 267} 268 269ExternalASTSource * 270CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path, 271 const std::string &Sysroot, 272 bool DisablePCHValidation, 273 Preprocessor &PP, 274 ASTContext &Context, 275 void *DeserializationListener) { 276 llvm::OwningPtr<PCHReader> Reader; 277 Reader.reset(new PCHReader(PP, &Context, 278 Sysroot.empty() ? 0 : Sysroot.c_str(), 279 DisablePCHValidation)); 280 281 Reader->setDeserializationListener( 282 static_cast<PCHDeserializationListener *>(DeserializationListener)); 283 switch (Reader->ReadPCH(Path)) { 284 case PCHReader::Success: 285 // Set the predefines buffer as suggested by the PCH reader. Typically, the 286 // predefines buffer will be empty. 287 PP.setPredefines(Reader->getSuggestedPredefines()); 288 return Reader.take(); 289 290 case PCHReader::Failure: 291 // Unrecoverable failure: don't even try to process the input file. 292 break; 293 294 case PCHReader::IgnorePCH: 295 // No suitable PCH file could be found. Return an error. 296 break; 297 } 298 299 return 0; 300} 301 302// Code Completion 303 304static bool EnableCodeCompletion(Preprocessor &PP, 305 const std::string &Filename, 306 unsigned Line, 307 unsigned Column) { 308 // Tell the source manager to chop off the given file at a specific 309 // line and column. 310 const FileEntry *Entry = PP.getFileManager().getFile(Filename); 311 if (!Entry) { 312 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) 313 << Filename; 314 return true; 315 } 316 317 // Truncate the named file at the given line/column. 318 PP.SetCodeCompletionPoint(Entry, Line, Column); 319 return false; 320} 321 322void CompilerInstance::createCodeCompletionConsumer() { 323 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; 324 if (!CompletionConsumer) { 325 CompletionConsumer.reset( 326 createCodeCompletionConsumer(getPreprocessor(), 327 Loc.FileName, Loc.Line, Loc.Column, 328 getFrontendOpts().DebugCodeCompletionPrinter, 329 getFrontendOpts().ShowMacrosInCodeCompletion, 330 getFrontendOpts().ShowCodePatternsInCodeCompletion, 331 getFrontendOpts().ShowGlobalSymbolsInCodeCompletion, 332 llvm::outs())); 333 if (!CompletionConsumer) 334 return; 335 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, 336 Loc.Line, Loc.Column)) { 337 CompletionConsumer.reset(); 338 return; 339 } 340 341 if (CompletionConsumer->isOutputBinary() && 342 llvm::sys::Program::ChangeStdoutToBinary()) { 343 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); 344 CompletionConsumer.reset(); 345 } 346} 347 348void CompilerInstance::createFrontendTimer() { 349 FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); 350} 351 352CodeCompleteConsumer * 353CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, 354 const std::string &Filename, 355 unsigned Line, 356 unsigned Column, 357 bool UseDebugPrinter, 358 bool ShowMacros, 359 bool ShowCodePatterns, 360 bool ShowGlobals, 361 llvm::raw_ostream &OS) { 362 if (EnableCodeCompletion(PP, Filename, Line, Column)) 363 return 0; 364 365 // Set up the creation routine for code-completion. 366 if (UseDebugPrinter) 367 return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns, 368 ShowGlobals, OS); 369 else 370 return new CIndexCodeCompleteConsumer(ShowMacros, ShowCodePatterns, 371 ShowGlobals, OS); 372} 373 374void CompilerInstance::createSema(bool CompleteTranslationUnit, 375 CodeCompleteConsumer *CompletionConsumer) { 376 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), 377 CompleteTranslationUnit, CompletionConsumer)); 378} 379 380// Output Files 381 382void CompilerInstance::addOutputFile(llvm::StringRef Path, 383 llvm::raw_ostream *OS) { 384 assert(OS && "Attempt to add empty stream to output list!"); 385 OutputFiles.push_back(std::make_pair(Path, OS)); 386} 387 388void CompilerInstance::clearOutputFiles(bool EraseFiles) { 389 for (std::list< std::pair<std::string, llvm::raw_ostream*> >::iterator 390 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { 391 delete it->second; 392 if (EraseFiles && !it->first.empty()) 393 llvm::sys::Path(it->first).eraseFromDisk(); 394 } 395 OutputFiles.clear(); 396} 397 398llvm::raw_fd_ostream * 399CompilerInstance::createDefaultOutputFile(bool Binary, 400 llvm::StringRef InFile, 401 llvm::StringRef Extension) { 402 return createOutputFile(getFrontendOpts().OutputFile, Binary, 403 InFile, Extension); 404} 405 406llvm::raw_fd_ostream * 407CompilerInstance::createOutputFile(llvm::StringRef OutputPath, 408 bool Binary, 409 llvm::StringRef InFile, 410 llvm::StringRef Extension) { 411 std::string Error, OutputPathName; 412 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, 413 InFile, Extension, 414 &OutputPathName); 415 if (!OS) { 416 getDiagnostics().Report(diag::err_fe_unable_to_open_output) 417 << OutputPath << Error; 418 return 0; 419 } 420 421 // Add the output file -- but don't try to remove "-", since this means we are 422 // using stdin. 423 addOutputFile((OutputPathName != "-") ? OutputPathName : "", OS); 424 425 return OS; 426} 427 428llvm::raw_fd_ostream * 429CompilerInstance::createOutputFile(llvm::StringRef OutputPath, 430 std::string &Error, 431 bool Binary, 432 llvm::StringRef InFile, 433 llvm::StringRef Extension, 434 std::string *ResultPathName) { 435 std::string OutFile; 436 if (!OutputPath.empty()) { 437 OutFile = OutputPath; 438 } else if (InFile == "-") { 439 OutFile = "-"; 440 } else if (!Extension.empty()) { 441 llvm::sys::Path Path(InFile); 442 Path.eraseSuffix(); 443 Path.appendSuffix(Extension); 444 OutFile = Path.str(); 445 } else { 446 OutFile = "-"; 447 } 448 449 llvm::OwningPtr<llvm::raw_fd_ostream> OS( 450 new llvm::raw_fd_ostream(OutFile.c_str(), Error, 451 (Binary ? llvm::raw_fd_ostream::F_Binary : 0))); 452 if (!Error.empty()) 453 return 0; 454 455 if (ResultPathName) 456 *ResultPathName = OutFile; 457 458 return OS.take(); 459} 460 461// Initialization Utilities 462 463bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) { 464 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(), 465 getSourceManager(), getFrontendOpts()); 466} 467 468bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile, 469 Diagnostic &Diags, 470 FileManager &FileMgr, 471 SourceManager &SourceMgr, 472 const FrontendOptions &Opts) { 473 // Figure out where to get and map in the main file. 474 if (InputFile != "-") { 475 const FileEntry *File = FileMgr.getFile(InputFile); 476 if (File) SourceMgr.createMainFileID(File, SourceLocation()); 477 if (SourceMgr.getMainFileID().isInvalid()) { 478 Diags.Report(diag::err_fe_error_reading) << InputFile; 479 return false; 480 } 481 } else { 482 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); 483 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB); 484 if (SourceMgr.getMainFileID().isInvalid()) { 485 Diags.Report(diag::err_fe_error_reading_stdin); 486 return false; 487 } 488 } 489 490 return true; 491} 492 493// High-Level Operations 494 495bool CompilerInstance::ExecuteAction(FrontendAction &Act) { 496 assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); 497 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); 498 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); 499 500 // FIXME: Take this as an argument, once all the APIs we used have moved to 501 // taking it as an input instead of hard-coding llvm::errs. 502 llvm::raw_ostream &OS = llvm::errs(); 503 504 // Create the target instance. 505 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts())); 506 if (!hasTarget()) 507 return false; 508 509 // Inform the target of the language options. 510 // 511 // FIXME: We shouldn't need to do this, the target should be immutable once 512 // created. This complexity should be lifted elsewhere. 513 getTarget().setForcedLangOptions(getLangOpts()); 514 515 // Validate/process some options. 516 if (getHeaderSearchOpts().Verbose) 517 OS << "clang -cc1 version " CLANG_VERSION_STRING 518 << " based upon " << PACKAGE_STRING 519 << " hosted on " << llvm::sys::getHostTriple() << "\n"; 520 521 if (getFrontendOpts().ShowTimers) 522 createFrontendTimer(); 523 524 if (getFrontendOpts().ShowStats) 525 llvm::EnableStatistics(); 526 527 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { 528 const std::string &InFile = getFrontendOpts().Inputs[i].second; 529 530 // Reset the ID tables if we are reusing the SourceManager. 531 if (hasSourceManager()) 532 getSourceManager().clearIDTables(); 533 534 if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) { 535 Act.Execute(); 536 Act.EndSourceFile(); 537 } 538 } 539 540 if (getDiagnosticOpts().ShowCarets) { 541 unsigned NumWarnings = getDiagnostics().getNumWarnings(); 542 unsigned NumErrors = getDiagnostics().getNumErrors() - 543 getDiagnostics().getNumErrorsSuppressed(); 544 545 if (NumWarnings) 546 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); 547 if (NumWarnings && NumErrors) 548 OS << " and "; 549 if (NumErrors) 550 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); 551 if (NumWarnings || NumErrors) 552 OS << " generated.\n"; 553 } 554 555 if (getFrontendOpts().ShowStats && hasFileManager()) { 556 getFileManager().PrintStats(); 557 OS << "\n"; 558 } 559 560 // Return the appropriate status when verifying diagnostics. 561 // 562 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need 563 // this. 564 if (getDiagnosticOpts().VerifyDiagnostics) 565 return !static_cast<VerifyDiagnosticsClient&>( 566 getDiagnosticClient()).HadErrors(); 567 568 return !getDiagnostics().getNumErrors(); 569} 570 571 572