1 //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
) {
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();
60 for (FrontendPluginRegistry::iterator it
=
61 FrontendPluginRegistry::begin(), ie
= FrontendPluginRegistry::end();
63 if (it
->getName() == CI
.getFrontendOpts().ActionName
) {
64 llvm::OwningPtr
<PluginASTAction
> P(it
->instantiate());
65 if (!P
->ParseArgs(CI
, CI
.getFrontendOpts().PluginArgs
))
71 CI
.getDiagnostics().Report(diag::err_fe_invalid_plugin_name
)
72 << CI
.getFrontendOpts().ActionName
;
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
);
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());
102 bool clang::ExecuteCompilerInvocation(CompilerInstance
*Clang
) {
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");
113 // FIXME: Use a better -version message?
114 if (Clang
->getFrontendOpts().ShowVersion
) {
115 llvm::cl::PrintVersionMessage();
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.
134 e
= Clang
->getFrontendOpts().Plugins
.size(); i
!= e
; ++i
) {
135 const std::string
&Path
= Clang
->getFrontendOpts().Plugins
[i
];
137 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path
.c_str(), &Error
))
138 Clang
->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin
)
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
));
148 Success
= Clang
->ExecuteAction(*Act
);
149 if (Clang
->getFrontendOpts().DisableFree
)