blob: 0699b7f48dbdec9a4d890067b8f82c32db6e2d1f [file] [log] [blame]
Daniel Dunbar41b5b172010-05-20 17:49:16 +00001//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
Daniel Dunbar217acbf2009-11-19 07:37:51 +00002//
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//
Daniel Dunbar21085772009-12-11 22:20:12 +000010// This is the entry point to the clang -cc1 functionality, which implements the
11// core compiler functionality along with a number of additional tools for
12// demonstration and testing purposes.
Daniel Dunbar217acbf2009-11-19 07:37:51 +000013//
14//===----------------------------------------------------------------------===//
15
Daniel Dunbar21085772009-12-11 22:20:12 +000016#include "clang/Basic/Diagnostic.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000017#include "clang/Checker/FrontendActions.h"
18#include "clang/CodeGen/CodeGenAction.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000019#include "clang/Driver/Arg.h"
20#include "clang/Driver/ArgList.h"
21#include "clang/Driver/CC1Options.h"
22#include "clang/Driver/DriverDiagnostic.h"
23#include "clang/Driver/OptTable.h"
24#include "clang/Frontend/CompilerInstance.h"
25#include "clang/Frontend/CompilerInvocation.h"
26#include "clang/Frontend/FrontendActions.h"
27#include "clang/Frontend/FrontendDiagnostic.h"
28#include "clang/Frontend/FrontendPluginRegistry.h"
29#include "clang/Frontend/TextDiagnosticBuffer.h"
30#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000031#include "clang/Rewrite/FrontendActions.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000032#include "llvm/LLVMContext.h"
33#include "llvm/ADT/OwningPtr.h"
Douglas Gregor95dd5582010-03-30 17:33:59 +000034#include "llvm/ADT/Statistic.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/ManagedStatic.h"
Chris Lattner30bc7e82010-03-30 05:39:52 +000037#include "llvm/Support/Timer.h"
Daniel Dunbar217acbf2009-11-19 07:37:51 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000039#include "llvm/System/DynamicLibrary.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000040#include "llvm/Target/TargetSelect.h"
41#include <cstdio>
42using namespace clang;
Daniel Dunbar217acbf2009-11-19 07:37:51 +000043
Daniel Dunbar21085772009-12-11 22:20:12 +000044//===----------------------------------------------------------------------===//
45// Main driver
46//===----------------------------------------------------------------------===//
47
Daniel Dunbar41b5b172010-05-20 17:49:16 +000048static void LLVMErrorHandler(void *UserData, const std::string &Message) {
Daniel Dunbar21085772009-12-11 22:20:12 +000049 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
50
51 Diags.Report(diag::err_fe_error_backend) << Message;
52
53 // We cannot recover from llvm errors.
54 exit(1);
55}
56
Douglas Gregor9bed8792010-02-09 19:21:46 +000057static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
Daniel Dunbar21085772009-12-11 22:20:12 +000058 using namespace clang::frontend;
59
60 switch (CI.getFrontendOpts().ProgramAction) {
61 default:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000062 llvm_unreachable("Invalid program action!");
Daniel Dunbar21085772009-12-11 22:20:12 +000063
64 case ASTDump: return new ASTDumpAction();
65 case ASTPrint: return new ASTPrintAction();
66 case ASTPrintXML: return new ASTPrintXMLAction();
67 case ASTView: return new ASTViewAction();
Douglas Gregorf78cc432010-05-07 15:41:56 +000068 case BoostCon: return new BoostConAction();
Daniel Dunbar21085772009-12-11 22:20:12 +000069 case DumpRawTokens: return new DumpRawTokensAction();
Daniel Dunbar21085772009-12-11 22:20:12 +000070 case DumpTokens: return new DumpTokensAction();
71 case EmitAssembly: return new EmitAssemblyAction();
72 case EmitBC: return new EmitBCAction();
73 case EmitHTML: return new HTMLPrintAction();
74 case EmitLLVM: return new EmitLLVMAction();
75 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
Daniel Dunbar32148ce2010-05-25 18:41:01 +000076 case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
Daniel Dunbarda1573f2010-02-03 01:18:43 +000077 case EmitObj: return new EmitObjAction();
Daniel Dunbar21085772009-12-11 22:20:12 +000078 case FixIt: return new FixItAction();
79 case GeneratePCH: return new GeneratePCHAction();
80 case GeneratePTH: return new GeneratePTHAction();
81 case InheritanceView: return new InheritanceViewAction();
Daniel Dunbar27585952010-03-19 19:44:04 +000082 case InitOnly: return new InitOnlyAction();
Daniel Dunbar21085772009-12-11 22:20:12 +000083 case ParseSyntaxOnly: return new SyntaxOnlyAction();
84
85 case PluginAction: {
Daniel Dunbar21085772009-12-11 22:20:12 +000086 for (FrontendPluginRegistry::iterator it =
87 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
88 it != ie; ++it) {
Daniel Dunbar3177aae2010-06-16 16:59:23 +000089 if (it->getName() == CI.getFrontendOpts().ActionName) {
Daniel Dunbarf56a4882010-08-02 15:31:28 +000090 llvm::OwningPtr<PluginASTAction> P(it->instantiate());
91 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
92 return 0;
93 return P.take();
Daniel Dunbar3177aae2010-06-16 16:59:23 +000094 }
Daniel Dunbar21085772009-12-11 22:20:12 +000095 }
96
97 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
98 << CI.getFrontendOpts().ActionName;
99 return 0;
100 }
101
102 case PrintDeclContext: return new DeclContextPrintAction();
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000103 case PrintPreamble: return new PrintPreambleAction();
Daniel Dunbar21085772009-12-11 22:20:12 +0000104 case PrintPreprocessedInput: return new PrintPreprocessedAction();
Daniel Dunbar21085772009-12-11 22:20:12 +0000105 case RewriteMacros: return new RewriteMacrosAction();
106 case RewriteObjC: return new RewriteObjCAction();
107 case RewriteTest: return new RewriteTestAction();
108 case RunAnalysis: return new AnalysisAction();
109 case RunPreprocessorOnly: return new PreprocessOnlyAction();
110 }
111}
112
Douglas Gregor9bed8792010-02-09 19:21:46 +0000113static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
114 // Create the underlying action.
115 FrontendAction *Act = CreateFrontendBaseAction(CI);
116 if (!Act)
117 return 0;
118
119 // If there are any AST files to merge, create a frontend action
120 // adaptor to perform the merge.
121 if (!CI.getFrontendOpts().ASTMergeFiles.empty())
122 Act = new ASTMergeAction(Act, &CI.getFrontendOpts().ASTMergeFiles[0],
123 CI.getFrontendOpts().ASTMergeFiles.size());
124
125 return Act;
126}
127
Daniel Dunbar21085772009-12-11 22:20:12 +0000128// FIXME: Define the need for this testing away.
129static int cc1_test(Diagnostic &Diags,
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000130 const char **ArgBegin, const char **ArgEnd) {
Daniel Dunbar21085772009-12-11 22:20:12 +0000131 using namespace clang::driver;
132
Daniel Dunbar217acbf2009-11-19 07:37:51 +0000133 llvm::errs() << "cc1 argv:";
134 for (const char **i = ArgBegin; i != ArgEnd; ++i)
135 llvm::errs() << " \"" << *i << '"';
136 llvm::errs() << "\n";
137
Daniel Dunbar21085772009-12-11 22:20:12 +0000138 // Parse the arguments.
139 OptTable *Opts = createCC1OptTable();
140 unsigned MissingArgIndex, MissingArgCount;
141 InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd,
142 MissingArgIndex, MissingArgCount);
143
144 // Check for missing argument error.
145 if (MissingArgCount)
146 Diags.Report(clang::diag::err_drv_missing_argument)
147 << Args->getArgString(MissingArgIndex) << MissingArgCount;
148
149 // Dump the parsed arguments.
150 llvm::errs() << "cc1 parsed options:\n";
151 for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
152 it != ie; ++it)
153 (*it)->dump();
154
155 // Create a compiler invocation.
156 llvm::errs() << "cc1 creating invocation.\n";
157 CompilerInvocation Invocation;
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000158 CompilerInvocation::CreateFromArgs(Invocation, ArgBegin, ArgEnd, Diags);
Daniel Dunbar21085772009-12-11 22:20:12 +0000159
160 // Convert the invocation back to argument strings.
161 std::vector<std::string> InvocationArgs;
162 Invocation.toArgs(InvocationArgs);
163
164 // Dump the converted arguments.
165 llvm::SmallVector<const char*, 32> Invocation2Args;
166 llvm::errs() << "invocation argv :";
167 for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) {
168 Invocation2Args.push_back(InvocationArgs[i].c_str());
169 llvm::errs() << " \"" << InvocationArgs[i] << '"';
170 }
171 llvm::errs() << "\n";
172
173 // Convert those arguments to another invocation, and check that we got the
174 // same thing.
175 CompilerInvocation Invocation2;
176 CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args.begin(),
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000177 Invocation2Args.end(), Diags);
Daniel Dunbar21085772009-12-11 22:20:12 +0000178
179 // FIXME: Implement CompilerInvocation comparison.
180 if (true) {
181 //llvm::errs() << "warning: Invocations differ!\n";
182
183 std::vector<std::string> Invocation2Args;
184 Invocation2.toArgs(Invocation2Args);
185 llvm::errs() << "invocation2 argv:";
186 for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i)
187 llvm::errs() << " \"" << Invocation2Args[i] << '"';
188 llvm::errs() << "\n";
189 }
190
Daniel Dunbar217acbf2009-11-19 07:37:51 +0000191 return 0;
192}
Daniel Dunbar21085772009-12-11 22:20:12 +0000193
194int cc1_main(const char **ArgBegin, const char **ArgEnd,
195 const char *Argv0, void *MainAddr) {
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000196 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
Daniel Dunbar42e9f8e42010-02-16 01:54:47 +0000197
Chris Lattnercabae682010-04-06 17:52:14 +0000198 Clang->setLLVMContext(new llvm::LLVMContext());
Daniel Dunbar21085772009-12-11 22:20:12 +0000199
200 // Run clang -cc1 test.
201 if (ArgBegin != ArgEnd && llvm::StringRef(ArgBegin[0]) == "-cc1test") {
202 TextDiagnosticPrinter DiagClient(llvm::errs(), DiagnosticOptions());
203 Diagnostic Diags(&DiagClient);
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000204 return cc1_test(Diags, ArgBegin + 1, ArgEnd);
Daniel Dunbar21085772009-12-11 22:20:12 +0000205 }
206
207 // Initialize targets first, so that --version shows registered targets.
208 llvm::InitializeAllTargets();
209 llvm::InitializeAllAsmPrinters();
Chris Lattner01ae93f2010-04-05 23:33:20 +0000210 llvm::InitializeAllAsmParsers();
Daniel Dunbar21085772009-12-11 22:20:12 +0000211
212 // Buffer diagnostics from argument parsing so that we can output them using a
213 // well formed diagnostic object.
214 TextDiagnosticBuffer DiagsBuffer;
215 Diagnostic Diags(&DiagsBuffer);
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000216 CompilerInvocation::CreateFromArgs(Clang->getInvocation(), ArgBegin, ArgEnd,
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000217 Diags);
218
219 // Infer the builtin include path if unspecified.
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000220 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
221 Clang->getHeaderSearchOpts().ResourceDir.empty())
222 Clang->getHeaderSearchOpts().ResourceDir =
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +0000223 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
Daniel Dunbar21085772009-12-11 22:20:12 +0000224
225 // Honor -help.
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000226 if (Clang->getFrontendOpts().ShowHelp) {
Daniel Dunbar21085772009-12-11 22:20:12 +0000227 llvm::OwningPtr<driver::OptTable> Opts(driver::createCC1OptTable());
228 Opts->PrintHelp(llvm::outs(), "clang -cc1",
229 "LLVM 'Clang' Compiler: http://clang.llvm.org");
230 return 0;
231 }
232
233 // Honor -version.
234 //
235 // FIXME: Use a better -version message?
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000236 if (Clang->getFrontendOpts().ShowVersion) {
Daniel Dunbar21085772009-12-11 22:20:12 +0000237 llvm::cl::PrintVersionMessage();
238 return 0;
239 }
240
Daniel Dunbar3f87fb02010-04-15 06:09:03 +0000241 // Honor -mllvm.
242 //
243 // FIXME: Remove this, one day.
244 if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
245 unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
246 const char **Args = new const char*[NumArgs + 2];
247 Args[0] = "clang (LLVM option parsing)";
248 for (unsigned i = 0; i != NumArgs; ++i)
249 Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
250 Args[NumArgs + 1] = 0;
Dan Gohmancb421fa2010-04-19 16:39:44 +0000251 llvm::cl::ParseCommandLineOptions(NumArgs + 1, const_cast<char **>(Args));
Daniel Dunbar3f87fb02010-04-15 06:09:03 +0000252 }
253
Daniel Dunbar21085772009-12-11 22:20:12 +0000254 // Create the actual diagnostics engine.
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000255 Clang->createDiagnostics(ArgEnd - ArgBegin, const_cast<char**>(ArgBegin));
256 if (!Clang->hasDiagnostics())
Daniel Dunbar21085772009-12-11 22:20:12 +0000257 return 1;
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000258
Daniel Dunbar21085772009-12-11 22:20:12 +0000259 // Set an error handler, so that any LLVM backend diagnostics go through our
260 // error handler.
Chris Lattner1e3c6f42010-04-07 23:12:35 +0000261 llvm::install_fatal_error_handler(LLVMErrorHandler,
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000262 static_cast<void*>(&Clang->getDiagnostics()));
Daniel Dunbar21085772009-12-11 22:20:12 +0000263
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000264 DiagsBuffer.FlushDiagnostics(Clang->getDiagnostics());
Daniel Dunbar21085772009-12-11 22:20:12 +0000265
266 // Load any requested plugins.
267 for (unsigned i = 0,
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000268 e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
269 const std::string &Path = Clang->getFrontendOpts().Plugins[i];
Daniel Dunbar21085772009-12-11 22:20:12 +0000270 std::string Error;
271 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
272 Diags.Report(diag::err_fe_unable_to_load_plugin) << Path << Error;
273 }
274
Daniel Dunbar0397af22010-01-13 00:48:06 +0000275 // If there were errors in processing arguments, don't do anything else.
276 bool Success = false;
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000277 if (!Clang->getDiagnostics().getNumErrors()) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000278 // Create and execute the frontend action.
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000279 llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
280 if (Act) {
281 Success = Clang->ExecuteAction(*Act);
282 if (Clang->getFrontendOpts().DisableFree)
283 Act.take();
284 }
285 }
286
Chris Lattner30bc7e82010-03-30 05:39:52 +0000287 // If any timers were active but haven't been destroyed yet, print their
288 // results now. This happens in -disable-free mode.
289 llvm::TimerGroup::printAll(llvm::errs());
Daniel Dunbarf56a4882010-08-02 15:31:28 +0000290
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000291 // When running with -disable-free, don't do any destruction or shutdown.
292 if (Clang->getFrontendOpts().DisableFree) {
Douglas Gregor95dd5582010-03-30 17:33:59 +0000293 if (Clang->getFrontendOpts().ShowStats)
294 llvm::PrintStatistics();
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000295 Clang.take();
296 return !Success;
Daniel Dunbar21085772009-12-11 22:20:12 +0000297 }
298
Daniel Dunbar21085772009-12-11 22:20:12 +0000299 // Managed static deconstruction. Useful for making things like
300 // -time-passes usable.
301 llvm::llvm_shutdown();
302
Daniel Dunbar0397af22010-01-13 00:48:06 +0000303 return !Success;
Daniel Dunbar21085772009-12-11 22:20:12 +0000304}