2010-08-12 10:53:12 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-24 08:31:22 +08:00
|
|
|
#include "clang/FrontendTool/Utils.h"
|
2011-06-17 00:17:05 +08:00
|
|
|
#include "clang/ARCMigrate/ARCMTActions.h"
|
2010-08-12 10:53:12 +08:00
|
|
|
#include "clang/CodeGen/CodeGenAction.h"
|
2017-07-18 16:55:03 +08:00
|
|
|
#include "clang/Config/config.h"
|
2012-05-01 22:57:16 +08:00
|
|
|
#include "clang/Driver/Options.h"
|
2010-08-12 10:53:12 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2010-08-12 10:53:12 +08:00
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/FrontendPluginRegistry.h"
|
2013-12-27 16:11:08 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Frontend/FrontendActions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
|
2013-06-15 01:17:23 +08:00
|
|
|
#include "llvm/Option/OptTable.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-08-12 10:53:12 +08:00
|
|
|
using namespace clang;
|
2013-06-15 01:17:23 +08:00
|
|
|
using namespace llvm::opt;
|
2010-08-12 10:53:12 +08:00
|
|
|
|
2016-02-08 03:28:36 +08:00
|
|
|
static std::unique_ptr<FrontendAction>
|
|
|
|
CreateFrontendBaseAction(CompilerInstance &CI) {
|
2010-08-12 10:53:12 +08:00
|
|
|
using namespace clang::frontend;
|
2013-11-28 03:44:04 +08:00
|
|
|
StringRef Action("unknown");
|
|
|
|
(void)Action;
|
2010-08-12 10:53:12 +08:00
|
|
|
|
|
|
|
switch (CI.getFrontendOpts().ProgramAction) {
|
2016-02-08 03:28:36 +08:00
|
|
|
case ASTDeclList: return llvm::make_unique<ASTDeclListAction>();
|
|
|
|
case ASTDump: return llvm::make_unique<ASTDumpAction>();
|
|
|
|
case ASTPrint: return llvm::make_unique<ASTPrintAction>();
|
|
|
|
case ASTView: return llvm::make_unique<ASTViewAction>();
|
|
|
|
case DumpRawTokens: return llvm::make_unique<DumpRawTokensAction>();
|
|
|
|
case DumpTokens: return llvm::make_unique<DumpTokensAction>();
|
|
|
|
case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
|
|
|
|
case EmitBC: return llvm::make_unique<EmitBCAction>();
|
|
|
|
case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
|
|
|
|
case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
|
|
|
|
case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
|
|
|
|
case EmitCodeGenOnly: return llvm::make_unique<EmitCodeGenOnlyAction>();
|
|
|
|
case EmitObj: return llvm::make_unique<EmitObjAction>();
|
|
|
|
case FixIt: return llvm::make_unique<FixItAction>();
|
2016-08-26 08:14:38 +08:00
|
|
|
case GenerateModule:
|
|
|
|
return llvm::make_unique<GenerateModuleFromModuleMapAction>();
|
|
|
|
case GenerateModuleInterface:
|
|
|
|
return llvm::make_unique<GenerateModuleInterfaceAction>();
|
2016-02-08 03:28:36 +08:00
|
|
|
case GeneratePCH: return llvm::make_unique<GeneratePCHAction>();
|
|
|
|
case GeneratePTH: return llvm::make_unique<GeneratePTHAction>();
|
|
|
|
case InitOnly: return llvm::make_unique<InitOnlyAction>();
|
|
|
|
case ParseSyntaxOnly: return llvm::make_unique<SyntaxOnlyAction>();
|
|
|
|
case ModuleFileInfo: return llvm::make_unique<DumpModuleInfoAction>();
|
|
|
|
case VerifyPCH: return llvm::make_unique<VerifyPCHAction>();
|
2010-08-12 10:53:12 +08:00
|
|
|
|
|
|
|
case PluginAction: {
|
|
|
|
for (FrontendPluginRegistry::iterator it =
|
|
|
|
FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
|
|
|
|
it != ie; ++it) {
|
|
|
|
if (it->getName() == CI.getFrontendOpts().ActionName) {
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<PluginASTAction> P(it->instantiate());
|
2016-03-15 20:51:40 +08:00
|
|
|
if ((P->getActionType() != PluginASTAction::ReplaceAction &&
|
|
|
|
P->getActionType() != PluginASTAction::Cmdline) ||
|
|
|
|
!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
|
2014-05-26 14:21:51 +08:00
|
|
|
return nullptr;
|
2016-02-08 03:28:36 +08:00
|
|
|
return std::move(P);
|
2010-08-12 10:53:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
|
|
|
|
<< CI.getFrontendOpts().ActionName;
|
2014-05-26 14:21:51 +08:00
|
|
|
return nullptr;
|
2010-08-12 10:53:12 +08:00
|
|
|
}
|
|
|
|
|
2016-02-08 03:28:36 +08:00
|
|
|
case PrintDeclContext: return llvm::make_unique<DeclContextPrintAction>();
|
|
|
|
case PrintPreamble: return llvm::make_unique<PrintPreambleAction>();
|
2012-06-15 01:36:09 +08:00
|
|
|
case PrintPreprocessedInput: {
|
2017-06-10 05:24:02 +08:00
|
|
|
if (CI.getPreprocessorOutputOpts().RewriteIncludes ||
|
|
|
|
CI.getPreprocessorOutputOpts().RewriteImports)
|
2016-02-08 03:28:36 +08:00
|
|
|
return llvm::make_unique<RewriteIncludesAction>();
|
|
|
|
return llvm::make_unique<PrintPreprocessedAction>();
|
2012-06-15 01:36:09 +08:00
|
|
|
}
|
|
|
|
|
2016-02-08 03:28:36 +08:00
|
|
|
case RewriteMacros: return llvm::make_unique<RewriteMacrosAction>();
|
|
|
|
case RewriteTest: return llvm::make_unique<RewriteTestAction>();
|
2014-07-17 00:48:33 +08:00
|
|
|
#ifdef CLANG_ENABLE_OBJC_REWRITER
|
2016-02-08 03:28:36 +08:00
|
|
|
case RewriteObjC: return llvm::make_unique<RewriteObjCAction>();
|
2012-12-14 00:09:42 +08:00
|
|
|
#else
|
|
|
|
case RewriteObjC: Action = "RewriteObjC"; break;
|
|
|
|
#endif
|
|
|
|
#ifdef CLANG_ENABLE_ARCMT
|
2016-02-08 03:28:36 +08:00
|
|
|
case MigrateSource:
|
|
|
|
return llvm::make_unique<arcmt::MigrateSourceAction>();
|
2012-12-14 00:09:42 +08:00
|
|
|
#else
|
|
|
|
case MigrateSource: Action = "MigrateSource"; break;
|
|
|
|
#endif
|
|
|
|
#ifdef CLANG_ENABLE_STATIC_ANALYZER
|
2016-02-08 03:28:36 +08:00
|
|
|
case RunAnalysis: return llvm::make_unique<ento::AnalysisAction>();
|
2012-12-14 00:09:42 +08:00
|
|
|
#else
|
|
|
|
case RunAnalysis: Action = "RunAnalysis"; break;
|
|
|
|
#endif
|
2016-02-08 03:28:36 +08:00
|
|
|
case RunPreprocessorOnly: return llvm::make_unique<PreprocessOnlyAction>();
|
2010-08-12 10:53:12 +08:00
|
|
|
}
|
2012-12-14 00:09:42 +08:00
|
|
|
|
|
|
|
#if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
|
2014-07-17 00:48:33 +08:00
|
|
|
|| !defined(CLANG_ENABLE_OBJC_REWRITER)
|
2012-12-14 00:09:42 +08:00
|
|
|
CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
|
|
|
|
return 0;
|
|
|
|
#else
|
2012-01-17 10:30:50 +08:00
|
|
|
llvm_unreachable("Invalid program action!");
|
2012-12-14 00:09:42 +08:00
|
|
|
#endif
|
2010-08-12 10:53:12 +08:00
|
|
|
}
|
|
|
|
|
2016-02-08 03:28:36 +08:00
|
|
|
static std::unique_ptr<FrontendAction>
|
|
|
|
CreateFrontendAction(CompilerInstance &CI) {
|
2010-08-12 10:53:12 +08:00
|
|
|
// Create the underlying action.
|
2016-02-08 03:28:36 +08:00
|
|
|
std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
|
2010-08-12 10:53:12 +08:00
|
|
|
if (!Act)
|
2014-05-26 14:21:51 +08:00
|
|
|
return nullptr;
|
2010-08-12 10:53:12 +08:00
|
|
|
|
2012-02-04 09:36:04 +08:00
|
|
|
const FrontendOptions &FEOpts = CI.getFrontendOpts();
|
|
|
|
|
|
|
|
if (FEOpts.FixAndRecompile) {
|
2016-02-08 03:28:36 +08:00
|
|
|
Act = llvm::make_unique<FixItRecompile>(std::move(Act));
|
2012-01-26 10:40:48 +08:00
|
|
|
}
|
|
|
|
|
2012-12-14 00:09:42 +08:00
|
|
|
#ifdef CLANG_ENABLE_ARCMT
|
2014-05-17 12:35:12 +08:00
|
|
|
if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
|
|
|
|
CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
|
2013-11-14 07:38:17 +08:00
|
|
|
// Potentially wrap the base FE action in an ARC Migrate Tool action.
|
|
|
|
switch (FEOpts.ARCMTAction) {
|
|
|
|
case FrontendOptions::ARCMT_None:
|
|
|
|
break;
|
|
|
|
case FrontendOptions::ARCMT_Check:
|
2016-02-08 03:28:36 +08:00
|
|
|
Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
|
2013-11-14 07:38:17 +08:00
|
|
|
break;
|
|
|
|
case FrontendOptions::ARCMT_Modify:
|
2016-02-08 03:28:36 +08:00
|
|
|
Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
|
2013-11-14 07:38:17 +08:00
|
|
|
break;
|
|
|
|
case FrontendOptions::ARCMT_Migrate:
|
2016-02-08 03:28:36 +08:00
|
|
|
Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
|
2013-11-14 07:38:17 +08:00
|
|
|
FEOpts.MTMigrateDir,
|
|
|
|
FEOpts.ARCMTMigrateReportOut,
|
|
|
|
FEOpts.ARCMTMigrateEmitARCErrors);
|
|
|
|
break;
|
|
|
|
}
|
2011-06-17 00:17:05 +08:00
|
|
|
|
2013-11-14 07:38:17 +08:00
|
|
|
if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
|
2016-02-08 03:28:36 +08:00
|
|
|
Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
|
|
|
|
FEOpts.MTMigrateDir,
|
|
|
|
FEOpts.ObjCMTAction);
|
2013-11-14 07:38:17 +08:00
|
|
|
}
|
2012-03-07 04:06:33 +08:00
|
|
|
}
|
2012-12-14 00:09:42 +08:00
|
|
|
#endif
|
2012-03-07 04:06:33 +08:00
|
|
|
|
2010-08-12 10:53:12 +08:00
|
|
|
// If there are any AST files to merge, create a frontend action
|
|
|
|
// adaptor to perform the merge.
|
2012-02-04 09:36:04 +08:00
|
|
|
if (!FEOpts.ASTMergeFiles.empty())
|
2016-02-08 03:28:36 +08:00
|
|
|
Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
|
|
|
|
FEOpts.ASTMergeFiles);
|
2010-08-12 10:53:12 +08:00
|
|
|
|
|
|
|
return Act;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
|
|
|
|
// Honor -help.
|
|
|
|
if (Clang->getFrontendOpts().ShowHelp) {
|
2017-01-14 01:34:15 +08:00
|
|
|
std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
|
2010-08-12 10:53:12 +08:00
|
|
|
Opts->PrintHelp(llvm::outs(), "clang -cc1",
|
2012-11-10 06:36:44 +08:00
|
|
|
"LLVM 'Clang' Compiler: http://clang.llvm.org",
|
2017-07-26 17:10:17 +08:00
|
|
|
/*Include=*/driver::options::CC1Option,
|
|
|
|
/*Exclude=*/0, /*ShowAllAliases=*/false);
|
2013-08-07 20:54:47 +08:00
|
|
|
return true;
|
2010-08-12 10:53:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Honor -version.
|
|
|
|
//
|
|
|
|
// FIXME: Use a better -version message?
|
|
|
|
if (Clang->getFrontendOpts().ShowVersion) {
|
|
|
|
llvm::cl::PrintVersionMessage();
|
2013-08-07 20:54:47 +08:00
|
|
|
return true;
|
2010-08-12 10:53:12 +08:00
|
|
|
}
|
|
|
|
|
2011-10-10 09:23:06 +08: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;
|
2016-02-12 00:33:20 +08:00
|
|
|
if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
|
2011-10-10 09:23:06 +08:00
|
|
|
Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
|
|
|
|
<< Path << Error;
|
|
|
|
}
|
|
|
|
|
2016-03-15 20:51:40 +08:00
|
|
|
// Check if any of the loaded plugins replaces the main AST action
|
|
|
|
for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
|
|
|
|
ie = FrontendPluginRegistry::end();
|
|
|
|
it != ie; ++it) {
|
|
|
|
std::unique_ptr<PluginASTAction> P(it->instantiate());
|
|
|
|
if (P->getActionType() == PluginASTAction::ReplaceAction) {
|
|
|
|
Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
|
|
|
|
Clang->getFrontendOpts().ActionName = it->getName();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-12 10:53:12 +08:00
|
|
|
// Honor -mllvm.
|
|
|
|
//
|
|
|
|
// FIXME: Remove this, one day.
|
2011-10-10 09:23:06 +08:00
|
|
|
// This should happen AFTER plugins have been loaded!
|
2010-08-12 10:53:12 +08:00
|
|
|
if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
|
|
|
|
unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
|
2014-05-03 16:39:35 +08:00
|
|
|
auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
|
2010-08-12 10:53:12 +08:00
|
|
|
Args[0] = "clang (LLVM option parsing)";
|
|
|
|
for (unsigned i = 0; i != NumArgs; ++i)
|
|
|
|
Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
|
2014-05-26 14:21:51 +08:00
|
|
|
Args[NumArgs + 1] = nullptr;
|
2014-05-03 16:39:35 +08:00
|
|
|
llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
|
2010-08-12 10:53:12 +08:00
|
|
|
}
|
|
|
|
|
2012-12-14 00:09:42 +08:00
|
|
|
#ifdef CLANG_ENABLE_STATIC_ANALYZER
|
2011-08-17 05:24:21 +08:00
|
|
|
// Honor -analyzer-checker-help.
|
|
|
|
// This should happen AFTER plugins have been loaded!
|
2012-08-31 12:36:05 +08:00
|
|
|
if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
|
2011-08-17 05:24:21 +08:00
|
|
|
ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
|
2013-08-07 20:54:47 +08:00
|
|
|
return true;
|
2011-08-17 05:24:21 +08:00
|
|
|
}
|
2016-08-08 21:41:04 +08:00
|
|
|
if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
|
|
|
|
ento::printEnabledCheckerList(llvm::outs(),
|
|
|
|
Clang->getFrontendOpts().Plugins,
|
|
|
|
*Clang->getAnalyzerOpts());
|
|
|
|
}
|
2012-12-14 00:09:42 +08:00
|
|
|
#endif
|
2011-08-17 05:24:21 +08:00
|
|
|
|
2010-08-12 10:53:12 +08:00
|
|
|
// If there were errors in processing arguments, don't do anything else.
|
2013-01-06 15:49:41 +08:00
|
|
|
if (Clang->getDiagnostics().hasErrorOccurred())
|
|
|
|
return false;
|
|
|
|
// Create and execute the frontend action.
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
|
2013-01-06 15:49:41 +08:00
|
|
|
if (!Act)
|
|
|
|
return false;
|
|
|
|
bool Success = Clang->ExecuteAction(*Act);
|
|
|
|
if (Clang->getFrontendOpts().DisableFree)
|
Provide a BuryPointer for unique_ptrs.
In theory, it'd be nice if we could move to a case where all buried
pointers were buried via unique_ptr to demonstrate that the program had
finished with the value (that we could really have cleanly deallocated
it) but instead chose to bury it.
I think the main reason that's not possible right now is the various
IntrusiveRefCntPtrs in the Frontend, sharing ownership for a variety of
compiler bits (see the various similar
"CompilerInstance::releaseAndLeak*" functions). I have yet to figure out
their correct ownership semantics - but perhaps, even if the
intrusiveness can be removed, the shared ownership may yet remain and
that would lead to a non-unique burying as is there today. (though we
could model that a little better - by passing in a shared_ptr, etc -
rather than needing the two step that's currently used in those other
releaseAndLeak* functions)
This might be a bit more robust if BuryPointer took the boolean:
BuryPointer(bool, unique_ptr<T>)
and the choice to bury was made internally - that way, even when
DisableFree was not set, the unique_ptr would still be null in the
caller and there'd be no chance of accidentally having a different
codepath where the value is used after burial in !DisableFree, but it
becomes null only in DisableFree, etc...
llvm-svn: 216742
2014-08-30 00:53:14 +08:00
|
|
|
BuryPointer(std::move(Act));
|
2010-08-12 10:53:12 +08:00
|
|
|
return Success;
|
|
|
|
}
|