[Heikki Kultala] This patch contains the ABI changes for the TCE target.
[clang.git] / lib / FrontendTool / ExecuteCompilerInvocation.cpp
blob4bb85e7e6340bd6fedc876d429f352f94b00dfcf
1 //===--- ExecuteCompilerInvocation.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 // This file holds ExecuteCompilerInvocation(). It is split into its own file to
11 // minimize the impact of pulling in essentially everything else in Clang.
13 //===----------------------------------------------------------------------===//
15 #include "clang/FrontendTool/Utils.h"
16 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
17 #include "clang/CodeGen/CodeGenAction.h"
18 #include "clang/Driver/CC1Options.h"
19 #include "clang/Driver/OptTable.h"
20 #include "clang/Frontend/CompilerInvocation.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/FrontendActions.h"
23 #include "clang/Frontend/FrontendDiagnostic.h"
24 #include "clang/Frontend/FrontendPluginRegistry.h"
25 #include "clang/Rewrite/FrontendActions.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/DynamicLibrary.h"
28 using namespace clang;
30 static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
31 using namespace clang::frontend;
33 switch (CI.getFrontendOpts().ProgramAction) {
34 default:
35 llvm_unreachable("Invalid program action!");
37 case ASTDump: return new ASTDumpAction();
38 case ASTDumpXML: return new ASTDumpXMLAction();
39 case ASTPrint: return new ASTPrintAction();
40 case ASTPrintXML: return new ASTPrintXMLAction();
41 case ASTView: return new ASTViewAction();
42 case BoostCon: return new BoostConAction();
43 case CreateModule: return 0;
44 case DumpRawTokens: return new DumpRawTokensAction();
45 case DumpTokens: return new DumpTokensAction();
46 case EmitAssembly: return new EmitAssemblyAction();
47 case EmitBC: return new EmitBCAction();
48 case EmitHTML: return new HTMLPrintAction();
49 case EmitLLVM: return new EmitLLVMAction();
50 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
51 case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
52 case EmitObj: return new EmitObjAction();
53 case FixIt: return new FixItAction();
54 case GeneratePCH: return new GeneratePCHAction();
55 case GeneratePTH: return new GeneratePTHAction();
56 case InitOnly: return new InitOnlyAction();
57 case ParseSyntaxOnly: return new SyntaxOnlyAction();
59 case PluginAction: {
60 for (FrontendPluginRegistry::iterator it =
61 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
62 it != ie; ++it) {
63 if (it->getName() == CI.getFrontendOpts().ActionName) {
64 llvm::OwningPtr<PluginASTAction> P(it->instantiate());
65 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
66 return 0;
67 return P.take();
71 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
72 << CI.getFrontendOpts().ActionName;
73 return 0;
76 case PrintDeclContext: return new DeclContextPrintAction();
77 case PrintPreamble: return new PrintPreambleAction();
78 case PrintPreprocessedInput: return new PrintPreprocessedAction();
79 case RewriteMacros: return new RewriteMacrosAction();
80 case RewriteObjC: return new RewriteObjCAction();
81 case RewriteTest: return new RewriteTestAction();
82 case RunAnalysis: return new ento::AnalysisAction();
83 case RunPreprocessorOnly: return new PreprocessOnlyAction();
87 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
88 // Create the underlying action.
89 FrontendAction *Act = CreateFrontendBaseAction(CI);
90 if (!Act)
91 return 0;
93 // If there are any AST files to merge, create a frontend action
94 // adaptor to perform the merge.
95 if (!CI.getFrontendOpts().ASTMergeFiles.empty())
96 Act = new ASTMergeAction(Act, &CI.getFrontendOpts().ASTMergeFiles[0],
97 CI.getFrontendOpts().ASTMergeFiles.size());
99 return Act;
102 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
103 // Honor -help.
104 if (Clang->getFrontendOpts().ShowHelp) {
105 llvm::OwningPtr<driver::OptTable> Opts(driver::createCC1OptTable());
106 Opts->PrintHelp(llvm::outs(), "clang -cc1",
107 "LLVM 'Clang' Compiler: http://clang.llvm.org");
108 return 0;
111 // Honor -version.
113 // FIXME: Use a better -version message?
114 if (Clang->getFrontendOpts().ShowVersion) {
115 llvm::cl::PrintVersionMessage();
116 return 0;
119 // Honor -mllvm.
121 // FIXME: Remove this, one day.
122 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
123 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
124 const char **Args = new const char*[NumArgs + 2];
125 Args[0] = "clang (LLVM option parsing)";
126 for (unsigned i = 0; i != NumArgs; ++i)
127 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
128 Args[NumArgs + 1] = 0;
129 llvm::cl::ParseCommandLineOptions(NumArgs + 1, const_cast<char **>(Args));
132 // Load any requested plugins.
133 for (unsigned i = 0,
134 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
135 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
136 std::string Error;
137 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
138 Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
139 << Path << Error;
142 // If there were errors in processing arguments, don't do anything else.
143 bool Success = false;
144 if (!Clang->getDiagnostics().hasErrorOccurred()) {
145 // Create and execute the frontend action.
146 llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
147 if (Act) {
148 Success = Clang->ExecuteAction(*Act);
149 if (Clang->getFrontendOpts().DisableFree)
150 Act.take();
154 return Success;