blob: aea97744208c5aeb27f5a300f2c19642dfc1038d [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 Dunbard10c5b82009-11-15 00:12:04 +000027#include "clang/Frontend/FrontendPluginRegistry.h"
Daniel Dunbar8863b982009-11-07 04:20:15 +000028#include "clang/Frontend/PathDiagnosticClients.h"
29#include "clang/Frontend/PreprocessorOptions.h"
Daniel Dunbar775bee72009-11-11 10:07:22 +000030#include "clang/Frontend/PreprocessorOutputOptions.h"
Daniel Dunbarf79dced2009-11-14 03:24:39 +000031#include "clang/Frontend/VerifyDiagnosticsClient.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000032#include "llvm/ADT/OwningPtr.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000033#include "llvm/Config/config.h"
Daniel Dunbar4fdba992009-11-14 10:53:49 +000034#include "llvm/LLVMContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000035#include "llvm/Support/CommandLine.h"
Daniel Dunbar70121eb2009-08-10 03:40:28 +000036#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar524b86f2008-10-28 00:38:08 +000037#include "llvm/Support/ManagedStatic.h"
Zhongxing Xu20922362008-11-26 05:23:17 +000038#include "llvm/Support/PluginLoader.h"
Chris Lattner09e94a32009-03-04 21:41:39 +000039#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbard10c5b82009-11-15 00:12:04 +000040#include "llvm/Support/Registry.h"
Chris Lattner47099742009-02-18 01:51:21 +000041#include "llvm/Support/Timer.h"
Chris Lattner0fa0daa2009-08-24 04:11:30 +000042#include "llvm/Support/raw_ostream.h"
Daniel Dunbare553a722008-10-02 01:21:33 +000043#include "llvm/System/Host.h"
Chris Lattnerdcaa0962008-03-03 03:16:03 +000044#include "llvm/System/Path.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000045#include "llvm/System/Signals.h"
Chris Lattner2fe11942009-06-17 17:25:50 +000046#include "llvm/Target/TargetSelect.h"
Douglas Gregor26df2f02009-04-02 19:05:20 +000047#include <cstdlib>
Douglas Gregor44cf08e2009-05-03 03:52:38 +000048#if HAVE_SYS_TYPES_H
Douglas Gregor68a0d782009-05-02 00:03:46 +000049# include <sys/types.h>
50#endif
Douglas Gregor26df2f02009-04-02 19:05:20 +000051
Reid Spencer5f016e22007-07-11 17:01:13 +000052using namespace clang;
53
54//===----------------------------------------------------------------------===//
Daniel Dunbar914474c2009-11-13 01:02:10 +000055// Utility Methods
Reid Spencer5f016e22007-07-11 17:01:13 +000056//===----------------------------------------------------------------------===//
57
Daniel Dunbar750156a2009-11-07 04:19:57 +000058std::string GetBuiltinIncludePath(const char *Argv0) {
59 llvm::sys::Path P =
60 llvm::sys::Path::GetMainExecutable(Argv0,
61 (void*)(intptr_t) GetBuiltinIncludePath);
Rafael Espindola1bb15a92009-10-05 13:12:17 +000062
Daniel Dunbar750156a2009-11-07 04:19:57 +000063 if (!P.isEmpty()) {
64 P.eraseComponent(); // Remove /clang from foo/bin/clang
65 P.eraseComponent(); // Remove /bin from foo/bin
Rafael Espindola1bb15a92009-10-05 13:12:17 +000066
Daniel Dunbar750156a2009-11-07 04:19:57 +000067 // Get foo/lib/clang/<version>/include
68 P.appendComponent("lib");
69 P.appendComponent("clang");
70 P.appendComponent(CLANG_VERSION_STRING);
71 P.appendComponent("include");
72 }
Rafael Espindola1bb15a92009-10-05 13:12:17 +000073
Daniel Dunbar750156a2009-11-07 04:19:57 +000074 return P.str();
Rafael Espindola1bb15a92009-10-05 13:12:17 +000075}
76
Reid Spencer5f016e22007-07-11 17:01:13 +000077//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000078// Main driver
79//===----------------------------------------------------------------------===//
80
Daniel Dunbaraa576142009-11-12 15:23:20 +000081/// ClangFrontendTimer - The front-end activities should charge time to it with
82/// TimeRegion. The -ftime-report option controls whether this will do
83/// anything.
84llvm::Timer *ClangFrontendTimer = 0;
85
Daniel Dunbard10c5b82009-11-15 00:12:04 +000086static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +000087 using namespace clang::frontend;
88
Daniel Dunbard10c5b82009-11-15 00:12:04 +000089 switch (CI.getFrontendOpts().ProgramAction) {
90 default:
91 llvm::llvm_unreachable("Invalid program action!");
92
Daniel Dunbar4fdba992009-11-14 10:53:49 +000093 case ASTDump: return new ASTDumpAction();
94 case ASTPrint: return new ASTPrintAction();
95 case ASTPrintXML: return new ASTPrintXMLAction();
96 case ASTView: return new ASTViewAction();
97 case DumpRawTokens: return new DumpRawTokensAction();
98 case DumpRecordLayouts: return new DumpRecordAction();
99 case DumpTokens: return new DumpTokensAction();
100 case EmitAssembly: return new EmitAssemblyAction();
101 case EmitBC: return new EmitBCAction();
102 case EmitHTML: return new HTMLPrintAction();
103 case EmitLLVM: return new EmitLLVMAction();
104 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
105 case FixIt: return new FixItAction();
106 case GeneratePCH: return new GeneratePCHAction();
107 case GeneratePTH: return new GeneratePTHAction();
108 case InheritanceView: return new InheritanceViewAction();
109 case ParseNoop: return new ParseOnlyAction();
110 case ParsePrintCallbacks: return new PrintParseAction();
111 case ParseSyntaxOnly: return new SyntaxOnlyAction();
Daniel Dunbard10c5b82009-11-15 00:12:04 +0000112
113 case PluginAction: {
114 if (CI.getFrontendOpts().ActionName == "help") {
115 llvm::errs() << "clang-cc plugins:\n";
116 for (FrontendPluginRegistry::iterator it =
117 FrontendPluginRegistry::begin(),
118 ie = FrontendPluginRegistry::end();
119 it != ie; ++it)
120 llvm::errs() << " " << it->getName() << " - " << it->getDesc() << "\n";
121 exit(1);
122 }
123
124 for (FrontendPluginRegistry::iterator it =
125 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
126 it != ie; ++it) {
127 if (it->getName() == CI.getFrontendOpts().ActionName)
128 return it->instantiate();
129 }
130
131 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
132 << CI.getFrontendOpts().ActionName;
133 return 0;
134 }
135
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000136 case PrintDeclContext: return new DeclContextPrintAction();
137 case PrintPreprocessedInput: return new PrintPreprocessedAction();
138 case RewriteBlocks: return new RewriteBlocksAction();
139 case RewriteMacros: return new RewriteMacrosAction();
140 case RewriteObjC: return new RewriteObjCAction();
141 case RewriteTest: return new RewriteTestAction();
142 case RunAnalysis: return new AnalysisAction();
143 case RunPreprocessorOnly: return new PreprocessOnlyAction();
Eli Friedman66d6f042009-05-18 22:20:00 +0000144 }
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +0000145}
146
Daniel Dunbar70121eb2009-08-10 03:40:28 +0000147static void LLVMErrorHandler(void *UserData, const std::string &Message) {
148 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
149
Daniel Dunbar0f9fed72009-11-10 23:55:23 +0000150 Diags.Report(diag::err_fe_error_backend) << Message;
Daniel Dunbar70121eb2009-08-10 03:40:28 +0000151
152 // We cannot recover from llvm errors.
153 exit(1);
154}
155
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000156static TargetInfo *
157ConstructCompilerInvocation(CompilerInvocation &Opts, Diagnostic &Diags,
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000158 const char *Argv0, bool &IsAST) {
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000159 // Initialize target options.
160 InitializeTargetOptions(Opts.getTargetOpts());
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000161
162 // Get information about the target being compiled for.
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000163 llvm::OwningPtr<TargetInfo> Target(
164 TargetInfo::CreateTargetInfo(Diags, Opts.getTargetOpts()));
165 if (!Target)
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000166 return 0;
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000167
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000168 // Initialize frontend options.
169 InitializeFrontendOptions(Opts.getFrontendOpts());
Daniel Dunbarfcb0c3b2009-11-10 18:47:35 +0000170
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000171 // Determine the input language, we currently require all files to match.
172 FrontendOptions::InputKind IK = Opts.getFrontendOpts().Inputs[0].first;
173 for (unsigned i = 1, e = Opts.getFrontendOpts().Inputs.size(); i != e; ++i) {
174 if (Opts.getFrontendOpts().Inputs[i].first != IK) {
175 llvm::errs() << "error: cannot have multiple input files of distinct "
176 << "language kinds without -x\n";
177 return 0;
178 }
179 }
180
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000181 // Initialize language options.
Daniel Dunbar5746f1f2009-11-12 00:24:10 +0000182 //
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000183 // FIXME: These aren't used during operations on ASTs. Split onto a separate
184 // code path to make this obvious.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000185 IsAST = (IK == FrontendOptions::IK_AST);
Daniel Dunbar26266882009-11-12 23:52:32 +0000186 if (!IsAST)
Daniel Dunbar6143ea22009-11-16 22:38:14 +0000187 InitializeLangOptions(Opts.getLangOpts(), IK, *Target);
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000188
Daniel Dunbar5746f1f2009-11-12 00:24:10 +0000189 // Initialize the static analyzer options.
190 InitializeAnalyzerOptions(Opts.getAnalyzerOpts());
191
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000192 // Initialize the dependency output options (-M...).
193 InitializeDependencyOutputOptions(Opts.getDependencyOutputOpts());
194
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000195 // Initialize the header search options.
Daniel Dunbarf7973292009-11-11 08:13:32 +0000196 InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(),
197 GetBuiltinIncludePath(Argv0),
Daniel Dunbarf7973292009-11-11 08:13:32 +0000198 Opts.getLangOpts());
Daniel Dunbar5fc7d342009-11-09 23:12:31 +0000199
200 // Initialize the other preprocessor options.
201 InitializePreprocessorOptions(Opts.getPreprocessorOpts());
Daniel Dunbar36f4ec32009-11-10 16:19:45 +0000202
Daniel Dunbar29cf7462009-11-11 10:07:44 +0000203 // Initialize the preprocessed output options.
204 InitializePreprocessorOutputOptions(Opts.getPreprocessorOutputOpts());
205
Daniel Dunbar6143ea22009-11-16 22:38:14 +0000206 // Initialize backend options, which may also be used to key some language
207 // options.
208 InitializeCodeGenOptions(Opts.getCodeGenOpts(), Opts.getLangOpts(),
209 Opts.getFrontendOpts().ShowTimers);
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000210
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000211 return Target.take();
Daniel Dunbare29709f2009-11-09 20:55:08 +0000212}
213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214int main(int argc, char **argv) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 llvm::sys::PrintStackTraceOnErrorSignal();
Chris Lattner09e94a32009-03-04 21:41:39 +0000216 llvm::PrettyStackTraceProgram X(argc, argv);
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000217 CompilerInstance Clang(&llvm::getGlobalContext(), false);
Daniel Dunbard6970812009-09-02 23:20:15 +0000218
Daniel Dunbar4d861512009-09-03 04:54:12 +0000219 // Initialize targets first, so that --version shows registered targets.
Chris Lattner2fe11942009-06-17 17:25:50 +0000220 llvm::InitializeAllTargets();
221 llvm::InitializeAllAsmPrinters();
Daniel Dunbard6970812009-09-02 23:20:15 +0000222
223 llvm::cl::ParseCommandLineOptions(argc, argv,
224 "LLVM 'Clang' Compiler: http://clang.llvm.org\n");
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Daniel Dunbar00e5b8d2009-11-12 06:48:31 +0000226 // Construct the diagnostic engine first, so that we can build a diagnostic
227 // client to use for any errors during option handling.
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000228 InitializeDiagnosticOptions(Clang.getDiagnosticOpts());
229 Clang.createDiagnostics(argc, argv);
Daniel Dunbar704e48a2009-11-13 08:20:57 +0000230 if (!Clang.hasDiagnostics())
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000231 return 1;
Ted Kremenek31e703b2007-12-11 23:28:38 +0000232
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000233 // Set an error handler, so that any LLVM backend diagnostics go through our
234 // error handler.
235 llvm::llvm_install_error_handler(LLVMErrorHandler,
236 static_cast<void*>(&Clang.getDiagnostics()));
Daniel Dunbar70121eb2009-08-10 03:40:28 +0000237
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000238 // Now that we have initialized the diagnostics engine, create the target and
239 // the compiler invocation object.
Daniel Dunbar227b2382009-11-09 22:45:57 +0000240 //
241 // FIXME: We should move .ast inputs to taking a separate path, they are
242 // really quite different.
Daniel Dunbar26266882009-11-12 23:52:32 +0000243 bool IsAST;
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000244 Clang.setTarget(
245 ConstructCompilerInvocation(Clang.getInvocation(), Clang.getDiagnostics(),
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000246 argv[0], IsAST));
Daniel Dunbar704e48a2009-11-13 08:20:57 +0000247 if (!Clang.hasTarget())
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000248 return 1;
Daniel Dunbar26266882009-11-12 23:52:32 +0000249
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000250 // Validate/process some options
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000251 if (Clang.getHeaderSearchOpts().Verbose)
Daniel Dunbar1417c742009-11-12 23:52:46 +0000252 llvm::errs() << "clang-cc version " CLANG_VERSION_STRING
253 << " based upon " << PACKAGE_STRING
254 << " hosted on " << llvm::sys::getHostTriple() << "\n";
255
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000256 if (Clang.getFrontendOpts().ShowTimers)
Daniel Dunbar26266882009-11-12 23:52:32 +0000257 ClangFrontendTimer = new llvm::Timer("Clang front-end time");
258
Daniel Dunbar79b55f92009-11-14 04:39:29 +0000259 // Enforce certain implications.
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000260 if (!Clang.getFrontendOpts().ViewClassInheritance.empty())
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000261 Clang.getFrontendOpts().ProgramAction = frontend::InheritanceView;
Daniel Dunbar79b55f92009-11-14 04:39:29 +0000262 if (!Clang.getFrontendOpts().FixItLocations.empty())
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000263 Clang.getFrontendOpts().ProgramAction = frontend::FixIt;
Daniel Dunbar227b2382009-11-09 22:45:57 +0000264
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000265 for (unsigned i = 0, e = Clang.getFrontendOpts().Inputs.size(); i != e; ++i) {
266 const std::string &InFile = Clang.getFrontendOpts().Inputs[i].second;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000268 // If we aren't using an AST file, setup the file and source managers and
269 // the preprocessor.
270 if (!IsAST) {
271 if (!i) {
272 // Create a file manager object to provide access to and cache the
273 // filesystem.
274 Clang.createFileManager();
275
276 // Create the source manager.
277 Clang.createSourceManager();
278 } else {
279 // Reset the ID tables if we are reusing the SourceManager.
280 Clang.getSourceManager().clearIDTables();
281 }
282
283 // Create the preprocessor.
284 Clang.createPreprocessor();
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +0000285 }
286
Daniel Dunbard10c5b82009-11-15 00:12:04 +0000287 llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(Clang));
288 if (!Act)
289 break;
290
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000291 Act->setCurrentTimer(ClangFrontendTimer);
292 if (Act->BeginSourceFile(Clang, InFile, IsAST)) {
293 Act->Execute();
294 Act->EndSourceFile();
295 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 }
Chris Lattner11215192008-03-14 06:12:05 +0000297
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000298 if (Clang.getDiagnosticOpts().ShowCarets)
299 if (unsigned NumDiagnostics = Clang.getDiagnostics().getNumDiagnostics())
Mike Stumpfc0fed32009-04-28 01:19:10 +0000300 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
301 (NumDiagnostics == 1 ? "" : "s"));
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000303 if (Clang.getFrontendOpts().ShowStats) {
Daniel Dunbar16b74492009-11-13 04:12:06 +0000304 Clang.getFileManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 fprintf(stderr, "\n");
306 }
Chris Lattner75a97cb2009-04-17 21:05:01 +0000307
308 delete ClangFrontendTimer;
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000310 // Return the appropriate status when verifying diagnostics.
311 //
312 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
313 // this.
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000314 if (Clang.getDiagnosticOpts().VerifyDiagnostics)
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000315 return static_cast<VerifyDiagnosticsClient&>(
316 Clang.getDiagnosticClient()).HadErrors();
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Daniel Dunbar524b86f2008-10-28 00:38:08 +0000318 // Managed static deconstruction. Useful for making things like
319 // -time-passes usable.
320 llvm::llvm_shutdown();
321
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000322 return (Clang.getDiagnostics().getNumErrors() != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000323}
Daniel Dunbard10c5b82009-11-15 00:12:04 +0000324