2010-09-17 15:54:40 +00:00
|
|
|
//===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file holds ExecuteCompilerInvocation(). It is split into its own file to
|
|
|
|
// minimize the impact of pulling in essentially everything else in Clang.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/FrontendTool/Utils.h"
|
2011-02-20 13:06:31 +00:00
|
|
|
#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
|
2011-07-17 15:40:56 +00:00
|
|
|
#include "clang/ARCMigrate/ARCMTActions.h"
|
2010-09-17 15:54:40 +00:00
|
|
|
#include "clang/CodeGen/CodeGenAction.h"
|
2012-08-15 20:02:54 +00:00
|
|
|
#include "clang/Driver/Options.h"
|
2010-09-17 15:54:40 +00:00
|
|
|
#include "clang/Driver/OptTable.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/FrontendPluginRegistry.h"
|
|
|
|
#include "clang/Rewrite/FrontendActions.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-02-20 13:06:31 +00:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2010-09-17 15:54:40 +00:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
|
|
|
|
using namespace clang::frontend;
|
|
|
|
|
|
|
|
switch (CI.getFrontendOpts().ProgramAction) {
|
2012-08-15 20:02:54 +00:00
|
|
|
case ASTDeclList: return new ASTDeclListAction();
|
2010-09-17 15:54:40 +00:00
|
|
|
case ASTDump: return new ASTDumpAction();
|
2011-02-20 13:06:31 +00:00
|
|
|
case ASTDumpXML: return new ASTDumpXMLAction();
|
2010-09-17 15:54:40 +00:00
|
|
|
case ASTPrint: return new ASTPrintAction();
|
|
|
|
case ASTView: return new ASTViewAction();
|
|
|
|
case DumpRawTokens: return new DumpRawTokensAction();
|
|
|
|
case DumpTokens: return new DumpTokensAction();
|
|
|
|
case EmitAssembly: return new EmitAssemblyAction();
|
|
|
|
case EmitBC: return new EmitBCAction();
|
|
|
|
case EmitHTML: return new HTMLPrintAction();
|
|
|
|
case EmitLLVM: return new EmitLLVMAction();
|
|
|
|
case EmitLLVMOnly: return new EmitLLVMOnlyAction();
|
|
|
|
case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
|
|
|
|
case EmitObj: return new EmitObjAction();
|
|
|
|
case FixIt: return new FixItAction();
|
2012-04-14 14:01:31 +00:00
|
|
|
case GenerateModule: return new GenerateModuleAction;
|
|
|
|
case GeneratePCH: return new GeneratePCHAction;
|
2010-09-17 15:54:40 +00:00
|
|
|
case GeneratePTH: return new GeneratePTHAction();
|
|
|
|
case InitOnly: return new InitOnlyAction();
|
|
|
|
case ParseSyntaxOnly: return new SyntaxOnlyAction();
|
|
|
|
|
|
|
|
case PluginAction: {
|
|
|
|
for (FrontendPluginRegistry::iterator it =
|
|
|
|
FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
|
|
|
|
it != ie; ++it) {
|
|
|
|
if (it->getName() == CI.getFrontendOpts().ActionName) {
|
2012-04-14 14:01:31 +00:00
|
|
|
OwningPtr<PluginASTAction> P(it->instantiate());
|
2010-09-17 15:54:40 +00:00
|
|
|
if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
|
|
|
|
return 0;
|
|
|
|
return P.take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
|
|
|
|
<< CI.getFrontendOpts().ActionName;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PrintDeclContext: return new DeclContextPrintAction();
|
|
|
|
case PrintPreamble: return new PrintPreambleAction();
|
2012-08-15 20:02:54 +00:00
|
|
|
case PrintPreprocessedInput: {
|
|
|
|
if (CI.getPreprocessorOutputOpts().RewriteIncludes)
|
|
|
|
return new RewriteIncludesAction();
|
|
|
|
return new PrintPreprocessedAction();
|
|
|
|
}
|
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
case RewriteMacros: return new RewriteMacrosAction();
|
|
|
|
case RewriteObjC: return new RewriteObjCAction();
|
|
|
|
case RewriteTest: return new RewriteTestAction();
|
2011-02-20 13:06:31 +00:00
|
|
|
case RunAnalysis: return new ento::AnalysisAction();
|
2012-04-14 14:01:31 +00:00
|
|
|
case MigrateSource: return new arcmt::MigrateSourceAction();
|
2010-09-17 15:54:40 +00:00
|
|
|
case RunPreprocessorOnly: return new PreprocessOnlyAction();
|
|
|
|
}
|
2012-04-14 14:01:31 +00:00
|
|
|
llvm_unreachable("Invalid program action!");
|
2010-09-17 15:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
|
|
|
|
// Create the underlying action.
|
|
|
|
FrontendAction *Act = CreateFrontendBaseAction(CI);
|
|
|
|
if (!Act)
|
|
|
|
return 0;
|
|
|
|
|
2012-04-14 14:01:31 +00:00
|
|
|
const FrontendOptions &FEOpts = CI.getFrontendOpts();
|
|
|
|
|
|
|
|
if (FEOpts.FixAndRecompile) {
|
|
|
|
Act = new FixItRecompile(Act);
|
|
|
|
}
|
|
|
|
|
2011-07-17 15:40:56 +00:00
|
|
|
// Potentially wrap the base FE action in an ARC Migrate Tool action.
|
2012-04-14 14:01:31 +00:00
|
|
|
switch (FEOpts.ARCMTAction) {
|
2011-07-17 15:40:56 +00:00
|
|
|
case FrontendOptions::ARCMT_None:
|
|
|
|
break;
|
|
|
|
case FrontendOptions::ARCMT_Check:
|
|
|
|
Act = new arcmt::CheckAction(Act);
|
|
|
|
break;
|
|
|
|
case FrontendOptions::ARCMT_Modify:
|
|
|
|
Act = new arcmt::ModifyAction(Act);
|
|
|
|
break;
|
|
|
|
case FrontendOptions::ARCMT_Migrate:
|
2011-10-20 21:14:49 +00:00
|
|
|
Act = new arcmt::MigrateAction(Act,
|
2012-04-14 14:01:31 +00:00
|
|
|
FEOpts.MTMigrateDir,
|
|
|
|
FEOpts.ARCMTMigrateReportOut,
|
|
|
|
FEOpts.ARCMTMigrateEmitARCErrors);
|
2011-07-17 15:40:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-14 14:01:31 +00:00
|
|
|
if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
|
|
|
|
Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
|
|
|
|
FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Literals,
|
|
|
|
FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Subscripting);
|
|
|
|
}
|
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
// If there are any AST files to merge, create a frontend action
|
|
|
|
// adaptor to perform the merge.
|
2012-04-14 14:01:31 +00:00
|
|
|
if (!FEOpts.ASTMergeFiles.empty())
|
|
|
|
Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
|
2010-09-17 15:54:40 +00:00
|
|
|
|
|
|
|
return Act;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
|
|
|
|
// Honor -help.
|
|
|
|
if (Clang->getFrontendOpts().ShowHelp) {
|
2012-08-15 20:02:54 +00:00
|
|
|
OwningPtr<driver::OptTable> Opts(driver::createDriverOptTable());
|
2010-09-17 15:54:40 +00:00
|
|
|
Opts->PrintHelp(llvm::outs(), "clang -cc1",
|
|
|
|
"LLVM 'Clang' Compiler: http://clang.llvm.org");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Honor -version.
|
|
|
|
//
|
|
|
|
// FIXME: Use a better -version message?
|
|
|
|
if (Clang->getFrontendOpts().ShowVersion) {
|
|
|
|
llvm::cl::PrintVersionMessage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
// Load any requested plugins.
|
|
|
|
for (unsigned i = 0,
|
|
|
|
e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
|
|
|
|
const std::string &Path = Clang->getFrontendOpts().Plugins[i];
|
|
|
|
std::string Error;
|
|
|
|
if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
|
|
|
|
Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
|
|
|
|
<< Path << Error;
|
|
|
|
}
|
|
|
|
|
2010-09-17 15:54:40 +00:00
|
|
|
// Honor -mllvm.
|
|
|
|
//
|
|
|
|
// FIXME: Remove this, one day.
|
2011-10-20 21:14:49 +00:00
|
|
|
// This should happen AFTER plugins have been loaded!
|
2010-09-17 15:54:40 +00:00
|
|
|
if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
|
|
|
|
unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
|
|
|
|
const char **Args = new const char*[NumArgs + 2];
|
|
|
|
Args[0] = "clang (LLVM option parsing)";
|
|
|
|
for (unsigned i = 0; i != NumArgs; ++i)
|
|
|
|
Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
|
|
|
|
Args[NumArgs + 1] = 0;
|
2012-04-14 14:01:31 +00:00
|
|
|
llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
|
2010-09-17 15:54:40 +00:00
|
|
|
}
|
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
// Honor -analyzer-checker-help.
|
|
|
|
// This should happen AFTER plugins have been loaded!
|
|
|
|
if (Clang->getAnalyzerOpts().ShowCheckerHelp) {
|
|
|
|
ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
|
|
|
|
return 0;
|
2010-09-17 15:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there were errors in processing arguments, don't do anything else.
|
|
|
|
bool Success = false;
|
2011-02-20 13:06:31 +00:00
|
|
|
if (!Clang->getDiagnostics().hasErrorOccurred()) {
|
2010-09-17 15:54:40 +00:00
|
|
|
// Create and execute the frontend action.
|
2012-04-14 14:01:31 +00:00
|
|
|
OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
|
2010-09-17 15:54:40 +00:00
|
|
|
if (Act) {
|
|
|
|
Success = Clang->ExecuteAction(*Act);
|
|
|
|
if (Clang->getFrontendOpts().DisableFree)
|
|
|
|
Act.take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|