blob: 75640acfedb3dec836d7010f1f07847c26aa759d [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- clang.cpp - C-Language Front-end ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This utility may be invoked in the following manner:
Daniel Dunbar4fdba992009-11-14 10:53:49 +000011// clang-cc --help - Output help info.
12// clang-cc [options] - Read from stdin.
13// clang-cc [options] file - Read from "file".
14// clang-cc [options] file1 file2 - Read these files.
Reid Spencer5f016e22007-07-11 17:01:13 +000015//
16//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000017
Daniel Dunbar0498cfc2009-11-10 19:51:53 +000018#include "Options.h"
Daniel Dunbar775bee72009-11-11 10:07:22 +000019#include "clang/Basic/FileManager.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/TargetInfo.h"
22#include "clang/Basic/Version.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000023#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbare29709f2009-11-09 20:55:08 +000024#include "clang/Frontend/CompilerInvocation.h"
Daniel Dunbar4fdba992009-11-14 10:53:49 +000025#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar50f4f462009-03-12 10:14:16 +000026#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar8863b982009-11-07 04:20:15 +000027#include "clang/Frontend/PathDiagnosticClients.h"
28#include "clang/Frontend/PreprocessorOptions.h"
Daniel Dunbar775bee72009-11-11 10:07:22 +000029#include "clang/Frontend/PreprocessorOutputOptions.h"
Daniel Dunbarf79dced2009-11-14 03:24:39 +000030#include "clang/Frontend/VerifyDiagnosticsClient.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000031#include "llvm/ADT/OwningPtr.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000032#include "llvm/Config/config.h"
Daniel Dunbar4fdba992009-11-14 10:53:49 +000033#include "llvm/LLVMContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000034#include "llvm/Support/CommandLine.h"
Daniel Dunbar70121eb2009-08-10 03:40:28 +000035#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar524b86f2008-10-28 00:38:08 +000036#include "llvm/Support/ManagedStatic.h"
Zhongxing Xu20922362008-11-26 05:23:17 +000037#include "llvm/Support/PluginLoader.h"
Chris Lattner09e94a32009-03-04 21:41:39 +000038#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner47099742009-02-18 01:51:21 +000039#include "llvm/Support/Timer.h"
Chris Lattner0fa0daa2009-08-24 04:11:30 +000040#include "llvm/Support/raw_ostream.h"
Daniel Dunbare553a722008-10-02 01:21:33 +000041#include "llvm/System/Host.h"
Chris Lattnerdcaa0962008-03-03 03:16:03 +000042#include "llvm/System/Path.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000043#include "llvm/System/Signals.h"
Chris Lattner2fe11942009-06-17 17:25:50 +000044#include "llvm/Target/TargetSelect.h"
Douglas Gregor26df2f02009-04-02 19:05:20 +000045#include <cstdlib>
Douglas Gregor44cf08e2009-05-03 03:52:38 +000046#if HAVE_SYS_TYPES_H
Douglas Gregor68a0d782009-05-02 00:03:46 +000047# include <sys/types.h>
48#endif
Douglas Gregor26df2f02009-04-02 19:05:20 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050using namespace clang;
51
52//===----------------------------------------------------------------------===//
Daniel Dunbar914474c2009-11-13 01:02:10 +000053// Utility Methods
Reid Spencer5f016e22007-07-11 17:01:13 +000054//===----------------------------------------------------------------------===//
55
Daniel Dunbar750156a2009-11-07 04:19:57 +000056std::string GetBuiltinIncludePath(const char *Argv0) {
57 llvm::sys::Path P =
58 llvm::sys::Path::GetMainExecutable(Argv0,
59 (void*)(intptr_t) GetBuiltinIncludePath);
Rafael Espindola1bb15a92009-10-05 13:12:17 +000060
Daniel Dunbar750156a2009-11-07 04:19:57 +000061 if (!P.isEmpty()) {
62 P.eraseComponent(); // Remove /clang from foo/bin/clang
63 P.eraseComponent(); // Remove /bin from foo/bin
Rafael Espindola1bb15a92009-10-05 13:12:17 +000064
Daniel Dunbar750156a2009-11-07 04:19:57 +000065 // Get foo/lib/clang/<version>/include
66 P.appendComponent("lib");
67 P.appendComponent("clang");
68 P.appendComponent(CLANG_VERSION_STRING);
69 P.appendComponent("include");
70 }
Rafael Espindola1bb15a92009-10-05 13:12:17 +000071
Daniel Dunbar750156a2009-11-07 04:19:57 +000072 return P.str();
Rafael Espindola1bb15a92009-10-05 13:12:17 +000073}
74
Reid Spencer5f016e22007-07-11 17:01:13 +000075//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000076// Main driver
77//===----------------------------------------------------------------------===//
78
Daniel Dunbaraa576142009-11-12 15:23:20 +000079/// ClangFrontendTimer - The front-end activities should charge time to it with
80/// TimeRegion. The -ftime-report option controls whether this will do
81/// anything.
82llvm::Timer *ClangFrontendTimer = 0;
83
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +000084static FrontendAction *CreateFrontendAction(frontend::ActionKind AK) {
85 using namespace clang::frontend;
86
87 switch (AK) {
Daniel Dunbar4fdba992009-11-14 10:53:49 +000088 default: return 0;
89 case ASTDump: return new ASTDumpAction();
90 case ASTPrint: return new ASTPrintAction();
91 case ASTPrintXML: return new ASTPrintXMLAction();
92 case ASTView: return new ASTViewAction();
93 case DumpRawTokens: return new DumpRawTokensAction();
94 case DumpRecordLayouts: return new DumpRecordAction();
95 case DumpTokens: return new DumpTokensAction();
96 case EmitAssembly: return new EmitAssemblyAction();
97 case EmitBC: return new EmitBCAction();
98 case EmitHTML: return new HTMLPrintAction();
99 case EmitLLVM: return new EmitLLVMAction();
100 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
101 case FixIt: return new FixItAction();
102 case GeneratePCH: return new GeneratePCHAction();
103 case GeneratePTH: return new GeneratePTHAction();
104 case InheritanceView: return new InheritanceViewAction();
105 case ParseNoop: return new ParseOnlyAction();
106 case ParsePrintCallbacks: return new PrintParseAction();
107 case ParseSyntaxOnly: return new SyntaxOnlyAction();
108 case PrintDeclContext: return new DeclContextPrintAction();
109 case PrintPreprocessedInput: return new PrintPreprocessedAction();
110 case RewriteBlocks: return new RewriteBlocksAction();
111 case RewriteMacros: return new RewriteMacrosAction();
112 case RewriteObjC: return new RewriteObjCAction();
113 case RewriteTest: return new RewriteTestAction();
114 case RunAnalysis: return new AnalysisAction();
115 case RunPreprocessorOnly: return new PreprocessOnlyAction();
Eli Friedman66d6f042009-05-18 22:20:00 +0000116 }
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +0000117}
118
Daniel Dunbar70121eb2009-08-10 03:40:28 +0000119static void LLVMErrorHandler(void *UserData, const std::string &Message) {
120 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
121
Daniel Dunbar0f9fed72009-11-10 23:55:23 +0000122 Diags.Report(diag::err_fe_error_backend) << Message;
Daniel Dunbar70121eb2009-08-10 03:40:28 +0000123
124 // We cannot recover from llvm errors.
125 exit(1);
126}
127
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000128static TargetInfo *
129ConstructCompilerInvocation(CompilerInvocation &Opts, Diagnostic &Diags,
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000130 const char *Argv0, bool &IsAST) {
Daniel Dunbar26266882009-11-12 23:52:32 +0000131 // Initialize frontend options.
132 InitializeFrontendOptions(Opts.getFrontendOpts());
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000133
Daniel Dunbarf819b252009-11-13 05:52:19 +0000134 // FIXME: The target information in frontend options should be split out into
135 // TargetOptions, and the target options in codegen options should move there
136 // as well. Then we could properly initialize in layering order.
137
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000138 // Initialize base triple. If a -triple option has been specified, use
139 // that triple. Otherwise, default to the host triple.
140 llvm::Triple Triple(Opts.getFrontendOpts().TargetTriple);
141 if (Triple.getTriple().empty())
142 Triple = llvm::Triple(llvm::sys::getHostTriple());
143
144 // Get information about the target being compiled for.
145 TargetInfo *Target = TargetInfo::CreateTargetInfo(Triple.getTriple());
146 if (!Target) {
147 Diags.Report(diag::err_fe_unknown_triple) << Triple.getTriple().c_str();
148 return 0;
149 }
150
151 // Set the target ABI if specified.
152 if (!Opts.getFrontendOpts().TargetABI.empty() &&
153 !Target->setABI(Opts.getFrontendOpts().TargetABI)) {
154 Diags.Report(diag::err_fe_unknown_target_abi)
155 << Opts.getFrontendOpts().TargetABI;
156 return 0;
157 }
158
Daniel Dunbarfcb0c3b2009-11-10 18:47:35 +0000159 // Initialize backend options, which may also be used to key some language
160 // options.
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000161 InitializeCodeGenOptions(Opts.getCodeGenOpts(), *Target);
Daniel Dunbarfcb0c3b2009-11-10 18:47:35 +0000162
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000163 // Determine the input language, we currently require all files to match.
164 FrontendOptions::InputKind IK = Opts.getFrontendOpts().Inputs[0].first;
165 for (unsigned i = 1, e = Opts.getFrontendOpts().Inputs.size(); i != e; ++i) {
166 if (Opts.getFrontendOpts().Inputs[i].first != IK) {
167 llvm::errs() << "error: cannot have multiple input files of distinct "
168 << "language kinds without -x\n";
169 return 0;
170 }
171 }
172
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000173 // Initialize language options.
Daniel Dunbar5746f1f2009-11-12 00:24:10 +0000174 //
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000175 // FIXME: These aren't used during operations on ASTs. Split onto a separate
176 // code path to make this obvious.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000177 IsAST = (IK == FrontendOptions::IK_AST);
Daniel Dunbar26266882009-11-12 23:52:32 +0000178 if (!IsAST)
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000179 InitializeLangOptions(Opts.getLangOpts(), IK, *Target,
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000180 Opts.getCodeGenOpts());
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000181
Daniel Dunbar5746f1f2009-11-12 00:24:10 +0000182 // Initialize the static analyzer options.
183 InitializeAnalyzerOptions(Opts.getAnalyzerOpts());
184
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000185 // Initialize the dependency output options (-M...).
186 InitializeDependencyOutputOptions(Opts.getDependencyOutputOpts());
187
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000188 // Initialize the header search options.
Daniel Dunbarf7973292009-11-11 08:13:32 +0000189 InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(),
190 GetBuiltinIncludePath(Argv0),
Daniel Dunbarf7973292009-11-11 08:13:32 +0000191 Opts.getLangOpts());
Daniel Dunbar5fc7d342009-11-09 23:12:31 +0000192
193 // Initialize the other preprocessor options.
194 InitializePreprocessorOptions(Opts.getPreprocessorOpts());
Daniel Dunbar36f4ec32009-11-10 16:19:45 +0000195
Daniel Dunbar29cf7462009-11-11 10:07:44 +0000196 // Initialize the preprocessed output options.
197 InitializePreprocessorOutputOptions(Opts.getPreprocessorOutputOpts());
198
Daniel Dunbar26266882009-11-12 23:52:32 +0000199 // Finalize some code generation options which are derived from other places.
200 if (Opts.getLangOpts().NoBuiltin)
201 Opts.getCodeGenOpts().SimplifyLibCalls = 0;
202 if (Opts.getLangOpts().CPlusPlus)
203 Opts.getCodeGenOpts().NoCommon = 1;
204 Opts.getCodeGenOpts().TimePasses = Opts.getFrontendOpts().ShowTimers;
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000205
206 return Target;
Daniel Dunbare29709f2009-11-09 20:55:08 +0000207}
208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209int main(int argc, char **argv) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 llvm::sys::PrintStackTraceOnErrorSignal();
Chris Lattner09e94a32009-03-04 21:41:39 +0000211 llvm::PrettyStackTraceProgram X(argc, argv);
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000212 CompilerInstance Clang(&llvm::getGlobalContext(), false);
Daniel Dunbard6970812009-09-02 23:20:15 +0000213
Daniel Dunbar4d861512009-09-03 04:54:12 +0000214 // Initialize targets first, so that --version shows registered targets.
Chris Lattner2fe11942009-06-17 17:25:50 +0000215 llvm::InitializeAllTargets();
216 llvm::InitializeAllAsmPrinters();
Daniel Dunbard6970812009-09-02 23:20:15 +0000217
218 llvm::cl::ParseCommandLineOptions(argc, argv,
219 "LLVM 'Clang' Compiler: http://clang.llvm.org\n");
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Daniel Dunbar00e5b8d2009-11-12 06:48:31 +0000221 // Construct the diagnostic engine first, so that we can build a diagnostic
222 // client to use for any errors during option handling.
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000223 InitializeDiagnosticOptions(Clang.getDiagnosticOpts());
224 Clang.createDiagnostics(argc, argv);
Daniel Dunbar704e48a2009-11-13 08:20:57 +0000225 if (!Clang.hasDiagnostics())
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000226 return 1;
Ted Kremenek31e703b2007-12-11 23:28:38 +0000227
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000228 // Set an error handler, so that any LLVM backend diagnostics go through our
229 // error handler.
230 llvm::llvm_install_error_handler(LLVMErrorHandler,
231 static_cast<void*>(&Clang.getDiagnostics()));
Daniel Dunbar70121eb2009-08-10 03:40:28 +0000232
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000233 // Now that we have initialized the diagnostics engine, create the target and
234 // the compiler invocation object.
Daniel Dunbar227b2382009-11-09 22:45:57 +0000235 //
236 // FIXME: We should move .ast inputs to taking a separate path, they are
237 // really quite different.
Daniel Dunbar26266882009-11-12 23:52:32 +0000238 bool IsAST;
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000239 Clang.setTarget(
240 ConstructCompilerInvocation(Clang.getInvocation(), Clang.getDiagnostics(),
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000241 argv[0], IsAST));
Daniel Dunbar704e48a2009-11-13 08:20:57 +0000242 if (!Clang.hasTarget())
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000243 return 1;
Daniel Dunbar26266882009-11-12 23:52:32 +0000244
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000245 // Validate/process some options
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000246 if (Clang.getHeaderSearchOpts().Verbose)
Daniel Dunbar1417c742009-11-12 23:52:46 +0000247 llvm::errs() << "clang-cc version " CLANG_VERSION_STRING
248 << " based upon " << PACKAGE_STRING
249 << " hosted on " << llvm::sys::getHostTriple() << "\n";
250
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000251 if (Clang.getFrontendOpts().ShowTimers)
Daniel Dunbar26266882009-11-12 23:52:32 +0000252 ClangFrontendTimer = new llvm::Timer("Clang front-end time");
253
Daniel Dunbar79b55f92009-11-14 04:39:29 +0000254 // Enforce certain implications.
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000255 if (!Clang.getFrontendOpts().ViewClassInheritance.empty())
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000256 Clang.getFrontendOpts().ProgramAction = frontend::InheritanceView;
Daniel Dunbar79b55f92009-11-14 04:39:29 +0000257 if (!Clang.getFrontendOpts().FixItLocations.empty())
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000258 Clang.getFrontendOpts().ProgramAction = frontend::FixIt;
Daniel Dunbar227b2382009-11-09 22:45:57 +0000259
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000260 for (unsigned i = 0, e = Clang.getFrontendOpts().Inputs.size(); i != e; ++i) {
261 const std::string &InFile = Clang.getFrontendOpts().Inputs[i].second;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000263 // If we aren't using an AST file, setup the file and source managers and
264 // the preprocessor.
265 if (!IsAST) {
266 if (!i) {
267 // Create a file manager object to provide access to and cache the
268 // filesystem.
269 Clang.createFileManager();
270
271 // Create the source manager.
272 Clang.createSourceManager();
273 } else {
274 // Reset the ID tables if we are reusing the SourceManager.
275 Clang.getSourceManager().clearIDTables();
276 }
277
278 // Create the preprocessor.
279 Clang.createPreprocessor();
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +0000280 }
281
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000282 llvm::OwningPtr<FrontendAction> Act(
283 CreateFrontendAction(Clang.getFrontendOpts().ProgramAction));
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000284 assert(Act && "Invalid program action!");
285 Act->setCurrentTimer(ClangFrontendTimer);
286 if (Act->BeginSourceFile(Clang, InFile, IsAST)) {
287 Act->Execute();
288 Act->EndSourceFile();
289 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 }
Chris Lattner11215192008-03-14 06:12:05 +0000291
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000292 if (Clang.getDiagnosticOpts().ShowCarets)
293 if (unsigned NumDiagnostics = Clang.getDiagnostics().getNumDiagnostics())
Mike Stumpfc0fed32009-04-28 01:19:10 +0000294 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
295 (NumDiagnostics == 1 ? "" : "s"));
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000297 if (Clang.getFrontendOpts().ShowStats) {
Daniel Dunbar16b74492009-11-13 04:12:06 +0000298 Clang.getFileManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 fprintf(stderr, "\n");
300 }
Chris Lattner75a97cb2009-04-17 21:05:01 +0000301
302 delete ClangFrontendTimer;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000304 // Return the appropriate status when verifying diagnostics.
305 //
306 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
307 // this.
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000308 if (Clang.getDiagnosticOpts().VerifyDiagnostics)
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000309 return static_cast<VerifyDiagnosticsClient&>(
310 Clang.getDiagnosticClient()).HadErrors();
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Daniel Dunbar524b86f2008-10-28 00:38:08 +0000312 // Managed static deconstruction. Useful for making things like
313 // -time-passes usable.
314 llvm::llvm_shutdown();
315
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000316 return (Clang.getDiagnostics().getNumErrors() != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000317}