2009-11-13 11:51:44 +08:00
|
|
|
//===--- CompilerInstance.cpp ---------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2010-08-13 07:31:19 +08:00
|
|
|
#include "clang/Sema/Sema.h"
|
2009-11-14 10:47:17 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-11-13 16:20:47 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-11-13 11:51:44 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2009-11-13 12:12:06 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-11-13 11:51:44 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-01-13 08:48:06 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
2009-11-13 13:52:11 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Lex/PTHManager.h"
|
2009-11-13 13:52:34 +08:00
|
|
|
#include "clang/Frontend/ChainedDiagnosticClient.h"
|
2010-01-13 08:48:06 +08:00
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
2009-11-13 17:36:05 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2011-04-08 02:31:10 +08:00
|
|
|
#include "clang/Frontend/LogDiagnosticPrinter.h"
|
2009-11-13 13:52:34 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
2009-11-14 11:24:39 +08:00
|
|
|
#include "clang/Frontend/VerifyDiagnosticsClient.h"
|
2009-11-13 13:52:11 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2010-08-19 07:57:17 +08:00
|
|
|
#include "clang/Serialization/ASTReader.h"
|
2009-11-13 17:36:05 +08:00
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
2011-01-10 10:34:13 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2009-11-14 15:53:04 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-11-13 13:52:34 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-03-31 01:33:59 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-11-29 17:57:35 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2010-12-10 01:36:38 +08:00
|
|
|
#include "llvm/Support/system_error.h"
|
2009-11-13 11:51:44 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2010-02-16 09:54:47 +08:00
|
|
|
CompilerInstance::CompilerInstance()
|
2010-07-30 08:29:29 +08:00
|
|
|
: Invocation(new CompilerInvocation()) {
|
2010-01-31 05:47:07 +08:00
|
|
|
}
|
2009-11-13 11:51:44 +08:00
|
|
|
|
|
|
|
CompilerInstance::~CompilerInstance() {
|
2010-02-16 09:54:47 +08:00
|
|
|
}
|
|
|
|
|
2010-01-31 05:47:07 +08:00
|
|
|
void CompilerInstance::setInvocation(CompilerInvocation *Value) {
|
2011-03-22 02:40:17 +08:00
|
|
|
Invocation = Value;
|
2010-01-31 05:47:07 +08:00
|
|
|
}
|
|
|
|
|
2009-11-14 09:20:40 +08:00
|
|
|
void CompilerInstance::setDiagnostics(Diagnostic *Value) {
|
2010-04-06 07:52:57 +08:00
|
|
|
Diagnostics = Value;
|
2009-11-14 09:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerInstance::setTarget(TargetInfo *Value) {
|
2011-03-22 02:40:17 +08:00
|
|
|
Target = Value;
|
2009-11-14 09:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerInstance::setFileManager(FileManager *Value) {
|
2011-03-22 02:40:17 +08:00
|
|
|
FileMgr = Value;
|
2009-11-14 09:20:40 +08:00
|
|
|
}
|
|
|
|
|
2011-03-22 02:40:17 +08:00
|
|
|
void CompilerInstance::setSourceManager(SourceManager *Value) {
|
|
|
|
SourceMgr = Value;
|
2009-11-14 09:20:40 +08:00
|
|
|
}
|
|
|
|
|
2011-03-22 02:40:17 +08:00
|
|
|
void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
|
2009-11-14 09:20:40 +08:00
|
|
|
|
2011-03-22 02:40:17 +08:00
|
|
|
void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; }
|
2009-11-14 10:47:17 +08:00
|
|
|
|
2010-08-13 07:31:19 +08:00
|
|
|
void CompilerInstance::setSema(Sema *S) {
|
|
|
|
TheSema.reset(S);
|
|
|
|
}
|
|
|
|
|
2009-11-14 10:47:17 +08:00
|
|
|
void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
|
|
|
|
Consumer.reset(Value);
|
2009-11-14 09:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
|
|
|
|
CompletionConsumer.reset(Value);
|
|
|
|
}
|
|
|
|
|
2009-11-13 13:52:34 +08:00
|
|
|
// Diagnostics
|
|
|
|
static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
|
2010-10-11 17:13:46 +08:00
|
|
|
unsigned argc, const char* const *argv,
|
2010-03-17 17:24:48 +08:00
|
|
|
Diagnostic &Diags) {
|
2009-11-13 13:52:34 +08:00
|
|
|
std::string ErrorInfo;
|
2010-03-17 17:47:30 +08:00
|
|
|
llvm::OwningPtr<llvm::raw_ostream> OS(
|
|
|
|
new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
|
2009-11-13 13:52:34 +08:00
|
|
|
if (!ErrorInfo.empty()) {
|
2010-03-17 17:24:48 +08:00
|
|
|
Diags.Report(diag::err_fe_unable_to_open_logfile)
|
|
|
|
<< DiagOpts.DumpBuildInformation << ErrorInfo;
|
2009-11-13 13:52:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-12 07:04:35 +08:00
|
|
|
(*OS) << "clang -cc1 command line arguments: ";
|
2009-11-13 13:52:34 +08:00
|
|
|
for (unsigned i = 0; i != argc; ++i)
|
|
|
|
(*OS) << argv[i] << ' ';
|
|
|
|
(*OS) << '\n';
|
|
|
|
|
|
|
|
// Chain in a diagnostic client which will log the diagnostics.
|
|
|
|
DiagnosticClient *Logger =
|
2010-03-17 17:47:30 +08:00
|
|
|
new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
|
2010-08-19 06:29:43 +08:00
|
|
|
Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
|
2009-11-13 13:52:34 +08:00
|
|
|
}
|
|
|
|
|
2011-04-08 02:31:10 +08:00
|
|
|
static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts,
|
2011-04-08 02:59:02 +08:00
|
|
|
const CodeGenOptions *CodeGenOpts,
|
2011-04-08 02:31:10 +08:00
|
|
|
Diagnostic &Diags) {
|
|
|
|
std::string ErrorInfo;
|
|
|
|
bool OwnsStream = false;
|
|
|
|
llvm::raw_ostream *OS = &llvm::errs();
|
|
|
|
if (DiagOpts.DiagnosticLogFile != "-") {
|
|
|
|
// Create the output stream.
|
|
|
|
llvm::raw_fd_ostream *FileOS(
|
|
|
|
new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(),
|
|
|
|
ErrorInfo));
|
|
|
|
if (!ErrorInfo.empty()) {
|
|
|
|
Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
|
|
|
|
<< DiagOpts.DumpBuildInformation << ErrorInfo;
|
|
|
|
} else {
|
|
|
|
FileOS->SetUnbuffered();
|
|
|
|
FileOS->SetUseAtomicWrites(true);
|
|
|
|
OS = FileOS;
|
|
|
|
OwnsStream = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chain in the diagnostic client which will log the diagnostics.
|
2011-04-08 02:59:02 +08:00
|
|
|
LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts,
|
|
|
|
OwnsStream);
|
|
|
|
if (CodeGenOpts)
|
|
|
|
Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
|
2011-04-08 02:31:10 +08:00
|
|
|
Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
|
|
|
|
}
|
|
|
|
|
2010-11-11 08:39:14 +08:00
|
|
|
void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
|
|
|
|
DiagnosticClient *Client) {
|
2011-04-08 02:59:02 +08:00
|
|
|
Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client,
|
|
|
|
&getCodeGenOpts());
|
2009-11-13 13:52:34 +08:00
|
|
|
}
|
|
|
|
|
2010-04-06 07:52:57 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<Diagnostic>
|
|
|
|
CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
|
2010-11-11 08:39:14 +08:00
|
|
|
int Argc, const char* const *Argv,
|
2011-04-08 02:59:02 +08:00
|
|
|
DiagnosticClient *Client,
|
|
|
|
const CodeGenOptions *CodeGenOpts) {
|
2010-11-19 04:06:41 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID));
|
2009-11-14 15:53:24 +08:00
|
|
|
|
2009-11-13 13:52:34 +08:00
|
|
|
// Create the diagnostic client for reporting errors or for
|
|
|
|
// implementing -verify.
|
2010-11-11 08:39:14 +08:00
|
|
|
if (Client)
|
|
|
|
Diags->setClient(Client);
|
|
|
|
else
|
|
|
|
Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
|
2009-11-14 11:24:39 +08:00
|
|
|
|
|
|
|
// Chain in -verify checker, if requested.
|
|
|
|
if (Opts.VerifyDiagnostics)
|
2010-08-19 06:29:43 +08:00
|
|
|
Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient()));
|
2009-11-13 13:52:34 +08:00
|
|
|
|
2011-04-08 02:31:10 +08:00
|
|
|
// Chain in -diagnostic-log-file dumper, if requested.
|
|
|
|
if (!Opts.DiagnosticLogFile.empty())
|
2011-04-08 02:59:02 +08:00
|
|
|
SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
|
2011-04-08 02:31:10 +08:00
|
|
|
|
2009-11-13 13:52:34 +08:00
|
|
|
if (!Opts.DumpBuildInformation.empty())
|
2010-03-17 17:24:48 +08:00
|
|
|
SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
|
2009-11-13 13:52:34 +08:00
|
|
|
|
|
|
|
// Configure our handling of diagnostics.
|
2010-03-17 17:36:02 +08:00
|
|
|
ProcessWarningOptions(*Diags, Opts);
|
2009-11-13 13:52:34 +08:00
|
|
|
|
2010-04-06 07:52:57 +08:00
|
|
|
return Diags;
|
2009-11-13 13:52:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// File Manager
|
|
|
|
|
2009-11-13 12:12:06 +08:00
|
|
|
void CompilerInstance::createFileManager() {
|
2011-03-22 02:40:17 +08:00
|
|
|
FileMgr = new FileManager(getFileSystemOpts());
|
2009-11-13 12:12:06 +08:00
|
|
|
}
|
|
|
|
|
2009-11-13 13:52:34 +08:00
|
|
|
// Source Manager
|
|
|
|
|
2010-11-23 16:35:12 +08:00
|
|
|
void CompilerInstance::createSourceManager(FileManager &FileMgr) {
|
2011-03-22 02:40:17 +08:00
|
|
|
SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
|
2009-11-13 12:12:06 +08:00
|
|
|
}
|
2009-11-13 13:52:11 +08:00
|
|
|
|
2009-11-13 13:52:34 +08:00
|
|
|
// Preprocessor
|
|
|
|
|
2009-11-13 13:52:11 +08:00
|
|
|
void CompilerInstance::createPreprocessor() {
|
2011-03-22 02:40:17 +08:00
|
|
|
PP = createPreprocessor(getDiagnostics(), getLangOpts(),
|
|
|
|
getPreprocessorOpts(), getHeaderSearchOpts(),
|
|
|
|
getDependencyOutputOpts(), getTarget(),
|
|
|
|
getFrontendOpts(), getSourceManager(),
|
|
|
|
getFileManager());
|
2009-11-13 13:52:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Preprocessor *
|
|
|
|
CompilerInstance::createPreprocessor(Diagnostic &Diags,
|
|
|
|
const LangOptions &LangInfo,
|
|
|
|
const PreprocessorOptions &PPOpts,
|
|
|
|
const HeaderSearchOptions &HSOpts,
|
|
|
|
const DependencyOutputOptions &DepOpts,
|
|
|
|
const TargetInfo &Target,
|
2010-01-14 02:51:17 +08:00
|
|
|
const FrontendOptions &FEOpts,
|
2009-11-13 13:52:11 +08:00
|
|
|
SourceManager &SourceMgr,
|
|
|
|
FileManager &FileMgr) {
|
|
|
|
// Create a PTH manager if we are using some form of a token cache.
|
|
|
|
PTHManager *PTHMgr = 0;
|
2009-11-17 13:52:41 +08:00
|
|
|
if (!PPOpts.TokenCache.empty())
|
2010-11-23 17:01:31 +08:00
|
|
|
PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags);
|
2009-11-13 13:52:11 +08:00
|
|
|
|
|
|
|
// Create the Preprocessor.
|
2010-11-23 16:35:12 +08:00
|
|
|
HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr);
|
2009-11-13 13:52:11 +08:00
|
|
|
Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
|
|
|
|
SourceMgr, *HeaderInfo, PTHMgr,
|
|
|
|
/*OwnsHeaderSearch=*/true);
|
|
|
|
|
|
|
|
// Note that this is different then passing PTHMgr to Preprocessor's ctor.
|
|
|
|
// That argument is used as the IdentifierInfoLookup argument to
|
|
|
|
// IdentifierTable's ctor.
|
|
|
|
if (PTHMgr) {
|
|
|
|
PTHMgr->setPreprocessor(PP);
|
|
|
|
PP->setPTHManager(PTHMgr);
|
|
|
|
}
|
|
|
|
|
2010-03-20 00:15:56 +08:00
|
|
|
if (PPOpts.DetailedRecord)
|
|
|
|
PP->createPreprocessingRecord();
|
|
|
|
|
2010-11-23 16:35:12 +08:00
|
|
|
InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts);
|
2009-11-13 13:52:11 +08:00
|
|
|
|
|
|
|
// Handle generating dependencies, if requested.
|
|
|
|
if (!DepOpts.OutputFile.empty())
|
|
|
|
AttachDependencyFileGen(*PP, DepOpts);
|
|
|
|
|
2011-02-02 23:41:17 +08:00
|
|
|
// Handle generating header include information, if requested.
|
|
|
|
if (DepOpts.ShowHeaderIncludes)
|
|
|
|
AttachHeaderIncludeGen(*PP);
|
2011-02-03 05:11:31 +08:00
|
|
|
if (!DepOpts.HeaderIncludeOutputFile.empty()) {
|
|
|
|
llvm::StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
|
|
|
|
if (OutputPath == "-")
|
|
|
|
OutputPath = "";
|
2011-03-22 03:37:38 +08:00
|
|
|
AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
|
|
|
|
/*ShowDepth=*/false);
|
2011-02-03 05:11:31 +08:00
|
|
|
}
|
2011-02-02 23:41:17 +08:00
|
|
|
|
2009-11-13 13:52:11 +08:00
|
|
|
return PP;
|
|
|
|
}
|
2009-11-13 16:20:47 +08:00
|
|
|
|
|
|
|
// ASTContext
|
|
|
|
|
|
|
|
void CompilerInstance::createASTContext() {
|
|
|
|
Preprocessor &PP = getPreprocessor();
|
2011-03-22 02:40:17 +08:00
|
|
|
Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
|
|
|
|
getTarget(), PP.getIdentifierTable(),
|
|
|
|
PP.getSelectorTable(), PP.getBuiltinInfo(),
|
|
|
|
/*size_reserve=*/ 0);
|
2009-11-13 16:20:47 +08:00
|
|
|
}
|
2009-11-13 16:21:10 +08:00
|
|
|
|
|
|
|
// ExternalASTSource
|
|
|
|
|
2010-07-27 08:27:13 +08:00
|
|
|
void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
|
2010-07-30 08:29:29 +08:00
|
|
|
bool DisablePCHValidation,
|
2011-02-06 03:42:43 +08:00
|
|
|
bool DisableStatCache,
|
2010-07-30 08:29:29 +08:00
|
|
|
void *DeserializationListener){
|
2009-11-13 16:21:10 +08:00
|
|
|
llvm::OwningPtr<ExternalASTSource> Source;
|
2010-10-06 00:15:19 +08:00
|
|
|
bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
|
2009-11-13 16:21:10 +08:00
|
|
|
Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
|
2011-02-06 03:42:43 +08:00
|
|
|
DisablePCHValidation,
|
|
|
|
DisableStatCache,
|
2010-07-30 08:29:29 +08:00
|
|
|
getPreprocessor(), getASTContext(),
|
2010-10-06 00:15:19 +08:00
|
|
|
DeserializationListener,
|
|
|
|
Preamble));
|
2009-11-13 16:21:10 +08:00
|
|
|
getASTContext().setExternalSource(Source);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExternalASTSource *
|
|
|
|
CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
|
|
|
|
const std::string &Sysroot,
|
2010-07-27 08:27:13 +08:00
|
|
|
bool DisablePCHValidation,
|
2011-02-06 03:42:43 +08:00
|
|
|
bool DisableStatCache,
|
2009-11-13 16:21:10 +08:00
|
|
|
Preprocessor &PP,
|
2010-07-30 08:29:29 +08:00
|
|
|
ASTContext &Context,
|
2010-10-06 00:15:19 +08:00
|
|
|
void *DeserializationListener,
|
|
|
|
bool Preamble) {
|
2010-08-19 07:56:43 +08:00
|
|
|
llvm::OwningPtr<ASTReader> Reader;
|
|
|
|
Reader.reset(new ASTReader(PP, &Context,
|
2010-07-27 08:27:13 +08:00
|
|
|
Sysroot.empty() ? 0 : Sysroot.c_str(),
|
2011-02-06 03:42:43 +08:00
|
|
|
DisablePCHValidation, DisableStatCache));
|
2009-11-13 16:21:10 +08:00
|
|
|
|
2010-07-30 08:29:29 +08:00
|
|
|
Reader->setDeserializationListener(
|
2010-08-19 07:56:56 +08:00
|
|
|
static_cast<ASTDeserializationListener *>(DeserializationListener));
|
2010-10-06 00:15:19 +08:00
|
|
|
switch (Reader->ReadAST(Path,
|
|
|
|
Preamble ? ASTReader::Preamble : ASTReader::PCH)) {
|
2010-08-19 07:56:43 +08:00
|
|
|
case ASTReader::Success:
|
2009-11-13 16:21:10 +08:00
|
|
|
// Set the predefines buffer as suggested by the PCH reader. Typically, the
|
|
|
|
// predefines buffer will be empty.
|
|
|
|
PP.setPredefines(Reader->getSuggestedPredefines());
|
|
|
|
return Reader.take();
|
|
|
|
|
2010-08-19 07:56:43 +08:00
|
|
|
case ASTReader::Failure:
|
2009-11-13 16:21:10 +08:00
|
|
|
// Unrecoverable failure: don't even try to process the input file.
|
|
|
|
break;
|
|
|
|
|
2010-08-19 07:56:43 +08:00
|
|
|
case ASTReader::IgnorePCH:
|
2009-11-13 16:21:10 +08:00
|
|
|
// No suitable PCH file could be found. Return an error.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-13 17:36:05 +08:00
|
|
|
|
|
|
|
// Code Completion
|
|
|
|
|
2010-08-05 00:47:14 +08:00
|
|
|
static bool EnableCodeCompletion(Preprocessor &PP,
|
|
|
|
const std::string &Filename,
|
|
|
|
unsigned Line,
|
|
|
|
unsigned Column) {
|
|
|
|
// Tell the source manager to chop off the given file at a specific
|
|
|
|
// line and column.
|
2010-11-23 16:35:12 +08:00
|
|
|
const FileEntry *Entry = PP.getFileManager().getFile(Filename);
|
2010-08-05 00:47:14 +08:00
|
|
|
if (!Entry) {
|
|
|
|
PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
|
|
|
|
<< Filename;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the named file at the given line/column.
|
|
|
|
PP.SetCodeCompletionPoint(Entry, Line, Column);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-13 17:36:05 +08:00
|
|
|
void CompilerInstance::createCodeCompletionConsumer() {
|
|
|
|
const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
|
2010-08-05 00:47:14 +08:00
|
|
|
if (!CompletionConsumer) {
|
|
|
|
CompletionConsumer.reset(
|
|
|
|
createCodeCompletionConsumer(getPreprocessor(),
|
|
|
|
Loc.FileName, Loc.Line, Loc.Column,
|
|
|
|
getFrontendOpts().ShowMacrosInCodeCompletion,
|
2010-05-26 05:41:55 +08:00
|
|
|
getFrontendOpts().ShowCodePatternsInCodeCompletion,
|
2010-08-15 14:18:01 +08:00
|
|
|
getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
|
2010-08-05 00:47:14 +08:00
|
|
|
llvm::outs()));
|
|
|
|
if (!CompletionConsumer)
|
|
|
|
return;
|
|
|
|
} else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
|
|
|
|
Loc.Line, Loc.Column)) {
|
|
|
|
CompletionConsumer.reset();
|
2010-03-16 14:04:47 +08:00
|
|
|
return;
|
2010-08-05 00:47:14 +08:00
|
|
|
}
|
2009-12-01 13:55:20 +08:00
|
|
|
|
|
|
|
if (CompletionConsumer->isOutputBinary() &&
|
|
|
|
llvm::sys::Program::ChangeStdoutToBinary()) {
|
|
|
|
getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
|
|
|
|
CompletionConsumer.reset();
|
|
|
|
}
|
2009-11-13 17:36:05 +08:00
|
|
|
}
|
|
|
|
|
2009-11-29 17:57:35 +08:00
|
|
|
void CompilerInstance::createFrontendTimer() {
|
|
|
|
FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
|
|
|
|
}
|
|
|
|
|
2009-11-13 17:36:05 +08:00
|
|
|
CodeCompleteConsumer *
|
|
|
|
CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
|
|
|
|
const std::string &Filename,
|
|
|
|
unsigned Line,
|
|
|
|
unsigned Column,
|
|
|
|
bool ShowMacros,
|
2010-05-26 05:41:55 +08:00
|
|
|
bool ShowCodePatterns,
|
2010-08-15 14:18:01 +08:00
|
|
|
bool ShowGlobals,
|
2009-11-13 17:36:05 +08:00
|
|
|
llvm::raw_ostream &OS) {
|
2010-08-05 00:47:14 +08:00
|
|
|
if (EnableCodeCompletion(PP, Filename, Line, Column))
|
2009-11-13 17:36:05 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Set up the creation routine for code-completion.
|
2010-10-12 06:12:15 +08:00
|
|
|
return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
|
2010-08-15 14:18:01 +08:00
|
|
|
ShowGlobals, OS);
|
2009-11-13 17:36:05 +08:00
|
|
|
}
|
2009-11-13 18:37:48 +08:00
|
|
|
|
2010-08-13 07:31:19 +08:00
|
|
|
void CompilerInstance::createSema(bool CompleteTranslationUnit,
|
|
|
|
CodeCompleteConsumer *CompletionConsumer) {
|
|
|
|
TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
|
|
|
|
CompleteTranslationUnit, CompletionConsumer));
|
|
|
|
}
|
|
|
|
|
2009-11-13 18:37:48 +08:00
|
|
|
// Output Files
|
|
|
|
|
2010-09-18 01:38:48 +08:00
|
|
|
void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
|
|
|
|
assert(OutFile.OS && "Attempt to add empty stream to output list!");
|
|
|
|
OutputFiles.push_back(OutFile);
|
2009-11-13 18:37:48 +08:00
|
|
|
}
|
|
|
|
|
2010-03-06 20:07:48 +08:00
|
|
|
void CompilerInstance::clearOutputFiles(bool EraseFiles) {
|
2010-09-18 01:38:48 +08:00
|
|
|
for (std::list<OutputFile>::iterator
|
2009-11-13 18:37:48 +08:00
|
|
|
it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
|
2010-09-18 01:38:48 +08:00
|
|
|
delete it->OS;
|
|
|
|
if (!it->TempFilename.empty()) {
|
2011-03-07 06:25:35 +08:00
|
|
|
if (EraseFiles) {
|
|
|
|
bool existed;
|
|
|
|
llvm::sys::fs::remove(it->TempFilename, existed);
|
|
|
|
} else {
|
|
|
|
llvm::SmallString<128> NewOutFile(it->Filename);
|
|
|
|
|
2010-11-04 06:45:23 +08:00
|
|
|
// If '-working-directory' was passed, the output filename should be
|
|
|
|
// relative to that.
|
2011-03-14 09:13:54 +08:00
|
|
|
FileMgr->FixupRelativePath(NewOutFile);
|
2011-03-07 06:25:35 +08:00
|
|
|
if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
|
|
|
|
NewOutFile.str())) {
|
2010-09-18 01:38:48 +08:00
|
|
|
getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
|
2011-03-07 06:25:35 +08:00
|
|
|
<< it->TempFilename << it->Filename << ec.message();
|
|
|
|
|
|
|
|
bool existed;
|
|
|
|
llvm::sys::fs::remove(it->TempFilename, existed);
|
2010-09-18 01:38:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!it->Filename.empty() && EraseFiles)
|
|
|
|
llvm::sys::Path(it->Filename).eraseFromDisk();
|
|
|
|
|
2009-11-13 18:37:48 +08:00
|
|
|
}
|
|
|
|
OutputFiles.clear();
|
|
|
|
}
|
|
|
|
|
2009-11-14 02:32:08 +08:00
|
|
|
llvm::raw_fd_ostream *
|
|
|
|
CompilerInstance::createDefaultOutputFile(bool Binary,
|
|
|
|
llvm::StringRef InFile,
|
|
|
|
llvm::StringRef Extension) {
|
|
|
|
return createOutputFile(getFrontendOpts().OutputFile, Binary,
|
2011-02-01 06:00:42 +08:00
|
|
|
/*RemoveFileOnSignal=*/true, InFile, Extension);
|
2009-11-14 02:32:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::raw_fd_ostream *
|
|
|
|
CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
|
2011-02-01 06:00:42 +08:00
|
|
|
bool Binary, bool RemoveFileOnSignal,
|
2009-11-14 02:32:08 +08:00
|
|
|
llvm::StringRef InFile,
|
|
|
|
llvm::StringRef Extension) {
|
2010-09-18 01:38:48 +08:00
|
|
|
std::string Error, OutputPathName, TempPathName;
|
2009-11-14 02:32:08 +08:00
|
|
|
llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
|
2011-02-01 06:00:42 +08:00
|
|
|
RemoveFileOnSignal,
|
2009-11-14 02:32:08 +08:00
|
|
|
InFile, Extension,
|
2010-09-18 01:38:48 +08:00
|
|
|
&OutputPathName,
|
|
|
|
&TempPathName);
|
2009-11-14 02:32:08 +08:00
|
|
|
if (!OS) {
|
2009-12-03 17:13:30 +08:00
|
|
|
getDiagnostics().Report(diag::err_fe_unable_to_open_output)
|
|
|
|
<< OutputPath << Error;
|
|
|
|
return 0;
|
2009-11-14 02:32:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the output file -- but don't try to remove "-", since this means we are
|
|
|
|
// using stdin.
|
2010-09-18 01:38:48 +08:00
|
|
|
addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
|
|
|
|
TempPathName, OS));
|
2009-11-14 02:32:08 +08:00
|
|
|
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::raw_fd_ostream *
|
|
|
|
CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
|
|
|
|
std::string &Error,
|
|
|
|
bool Binary,
|
2011-02-01 06:00:42 +08:00
|
|
|
bool RemoveFileOnSignal,
|
2009-11-14 02:32:08 +08:00
|
|
|
llvm::StringRef InFile,
|
|
|
|
llvm::StringRef Extension,
|
2010-09-18 01:38:48 +08:00
|
|
|
std::string *ResultPathName,
|
|
|
|
std::string *TempPathName) {
|
|
|
|
std::string OutFile, TempFile;
|
2009-11-14 02:32:08 +08:00
|
|
|
if (!OutputPath.empty()) {
|
|
|
|
OutFile = OutputPath;
|
|
|
|
} else if (InFile == "-") {
|
|
|
|
OutFile = "-";
|
|
|
|
} else if (!Extension.empty()) {
|
|
|
|
llvm::sys::Path Path(InFile);
|
|
|
|
Path.eraseSuffix();
|
|
|
|
Path.appendSuffix(Extension);
|
|
|
|
OutFile = Path.str();
|
|
|
|
} else {
|
|
|
|
OutFile = "-";
|
|
|
|
}
|
2010-09-18 01:38:48 +08:00
|
|
|
|
|
|
|
if (OutFile != "-") {
|
|
|
|
llvm::sys::Path OutPath(OutFile);
|
|
|
|
// Only create the temporary if we can actually write to OutPath, otherwise
|
|
|
|
// we want to fail early.
|
2011-01-10 10:34:13 +08:00
|
|
|
bool Exists;
|
|
|
|
if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) ||
|
2010-09-18 01:38:48 +08:00
|
|
|
(OutPath.isRegularFile() && OutPath.canWrite())) {
|
|
|
|
// Create a temporary file.
|
|
|
|
llvm::sys::Path TempPath(OutFile);
|
|
|
|
if (!TempPath.createTemporaryFileOnDisk())
|
|
|
|
TempFile = TempPath.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string OSFile = OutFile;
|
|
|
|
if (!TempFile.empty())
|
|
|
|
OSFile = TempFile;
|
2009-11-14 02:32:08 +08:00
|
|
|
|
2009-11-21 06:32:38 +08:00
|
|
|
llvm::OwningPtr<llvm::raw_fd_ostream> OS(
|
2010-09-18 01:38:48 +08:00
|
|
|
new llvm::raw_fd_ostream(OSFile.c_str(), Error,
|
2009-11-21 06:32:38 +08:00
|
|
|
(Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
|
|
|
|
if (!Error.empty())
|
2009-11-14 02:32:08 +08:00
|
|
|
return 0;
|
|
|
|
|
2010-09-18 01:38:48 +08:00
|
|
|
// Make sure the out stream file gets removed if we crash.
|
2011-02-01 06:00:42 +08:00
|
|
|
if (RemoveFileOnSignal)
|
|
|
|
llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
|
2010-09-18 01:38:48 +08:00
|
|
|
|
2009-11-14 02:32:08 +08:00
|
|
|
if (ResultPathName)
|
|
|
|
*ResultPathName = OutFile;
|
2010-09-18 01:38:48 +08:00
|
|
|
if (TempPathName)
|
|
|
|
*TempPathName = TempFile;
|
2009-11-14 02:32:08 +08:00
|
|
|
|
2009-11-21 06:32:38 +08:00
|
|
|
return OS.take();
|
2009-11-14 02:32:08 +08:00
|
|
|
}
|
2009-11-14 15:53:04 +08:00
|
|
|
|
|
|
|
// Initialization Utilities
|
|
|
|
|
|
|
|
bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
|
|
|
|
return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
|
|
|
|
getSourceManager(), getFrontendOpts());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
|
|
|
|
Diagnostic &Diags,
|
|
|
|
FileManager &FileMgr,
|
|
|
|
SourceManager &SourceMgr,
|
|
|
|
const FrontendOptions &Opts) {
|
2010-11-30 13:23:00 +08:00
|
|
|
// Figure out where to get and map in the main file, unless it's already
|
|
|
|
// been created (e.g., by a precompiled preamble).
|
|
|
|
if (!SourceMgr.getMainFileID().isInvalid()) {
|
|
|
|
// Do nothing: the main file has already been set.
|
|
|
|
} else if (InputFile != "-") {
|
2010-11-23 16:35:12 +08:00
|
|
|
const FileEntry *File = FileMgr.getFile(InputFile);
|
2010-10-27 05:13:51 +08:00
|
|
|
if (!File) {
|
2009-11-14 15:53:04 +08:00
|
|
|
Diags.Report(diag::err_fe_error_reading) << InputFile;
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-27 05:13:51 +08:00
|
|
|
SourceMgr.createMainFileID(File);
|
2009-11-14 15:53:04 +08:00
|
|
|
} else {
|
2010-12-16 11:28:14 +08:00
|
|
|
llvm::OwningPtr<llvm::MemoryBuffer> SB;
|
|
|
|
if (llvm::MemoryBuffer::getSTDIN(SB)) {
|
2010-12-10 01:36:38 +08:00
|
|
|
// FIXME: Give ec.message() in this diag.
|
2009-11-14 15:53:04 +08:00
|
|
|
Diags.Report(diag::err_fe_error_reading_stdin);
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-27 07:21:25 +08:00
|
|
|
const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
|
2010-11-23 16:35:12 +08:00
|
|
|
SB->getBufferSize(), 0);
|
2010-10-27 07:21:25 +08:00
|
|
|
SourceMgr.createMainFileID(File);
|
2010-12-16 11:28:14 +08:00
|
|
|
SourceMgr.overrideFileContents(File, SB.take());
|
2009-11-14 15:53:04 +08:00
|
|
|
}
|
|
|
|
|
2010-10-27 05:13:51 +08:00
|
|
|
assert(!SourceMgr.getMainFileID().isInvalid() &&
|
|
|
|
"Couldn't establish MainFileID!");
|
2009-11-14 15:53:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-01-13 08:48:06 +08:00
|
|
|
|
|
|
|
// High-Level Operations
|
|
|
|
|
|
|
|
bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
|
|
|
|
assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
|
|
|
|
assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
|
|
|
|
assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
|
|
|
|
|
|
|
|
// FIXME: Take this as an argument, once all the APIs we used have moved to
|
|
|
|
// taking it as an input instead of hard-coding llvm::errs.
|
|
|
|
llvm::raw_ostream &OS = llvm::errs();
|
|
|
|
|
|
|
|
// Create the target instance.
|
|
|
|
setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
|
|
|
|
if (!hasTarget())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Inform the target of the language options.
|
|
|
|
//
|
|
|
|
// FIXME: We shouldn't need to do this, the target should be immutable once
|
|
|
|
// created. This complexity should be lifted elsewhere.
|
|
|
|
getTarget().setForcedLangOptions(getLangOpts());
|
|
|
|
|
|
|
|
// Validate/process some options.
|
|
|
|
if (getHeaderSearchOpts().Verbose)
|
|
|
|
OS << "clang -cc1 version " CLANG_VERSION_STRING
|
|
|
|
<< " based upon " << PACKAGE_STRING
|
|
|
|
<< " hosted on " << llvm::sys::getHostTriple() << "\n";
|
|
|
|
|
|
|
|
if (getFrontendOpts().ShowTimers)
|
|
|
|
createFrontendTimer();
|
|
|
|
|
2010-03-31 01:33:59 +08:00
|
|
|
if (getFrontendOpts().ShowStats)
|
|
|
|
llvm::EnableStatistics();
|
|
|
|
|
2010-01-13 08:48:06 +08:00
|
|
|
for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
|
|
|
|
const std::string &InFile = getFrontendOpts().Inputs[i].second;
|
|
|
|
|
2010-06-08 07:23:50 +08:00
|
|
|
// Reset the ID tables if we are reusing the SourceManager.
|
|
|
|
if (hasSourceManager())
|
|
|
|
getSourceManager().clearIDTables();
|
2010-01-13 08:48:06 +08:00
|
|
|
|
2010-06-08 07:23:06 +08:00
|
|
|
if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
|
2010-01-13 08:48:06 +08:00
|
|
|
Act.Execute();
|
|
|
|
Act.EndSourceFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-08 02:47:42 +08:00
|
|
|
if (getDiagnosticOpts().ShowCarets) {
|
2010-11-19 04:06:46 +08:00
|
|
|
// We can have multiple diagnostics sharing one diagnostic client.
|
|
|
|
// Get the total number of warnings/errors from the client.
|
|
|
|
unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
|
|
|
|
unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
|
2010-04-08 02:47:42 +08:00
|
|
|
|
|
|
|
if (NumWarnings)
|
|
|
|
OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
|
|
|
|
if (NumWarnings && NumErrors)
|
|
|
|
OS << " and ";
|
|
|
|
if (NumErrors)
|
|
|
|
OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
|
|
|
|
if (NumWarnings || NumErrors)
|
|
|
|
OS << " generated.\n";
|
|
|
|
}
|
2010-01-13 08:48:06 +08:00
|
|
|
|
2010-06-08 07:23:50 +08:00
|
|
|
if (getFrontendOpts().ShowStats && hasFileManager()) {
|
2010-01-13 08:48:06 +08:00
|
|
|
getFileManager().PrintStats();
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2010-11-19 05:13:57 +08:00
|
|
|
return !getDiagnostics().getClient()->getNumErrors();
|
2010-01-13 08:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|