blob: bae8697e14bd0446b7881aa2e716d9d4145b9a5f [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 Dunbar9119e7e2009-11-16 22:38:48 +000019#include "clang/Basic/Diagnostic.h"
Daniel Dunbar775bee72009-11-11 10:07:22 +000020#include "clang/Basic/FileManager.h"
21#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Basic/Version.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000024#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbare29709f2009-11-09 20:55:08 +000025#include "clang/Frontend/CompilerInvocation.h"
Daniel Dunbar4fdba992009-11-14 10:53:49 +000026#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar50f4f462009-03-12 10:14:16 +000027#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbard10c5b82009-11-15 00:12:04 +000028#include "clang/Frontend/FrontendPluginRegistry.h"
Daniel Dunbarf79dced2009-11-14 03:24:39 +000029#include "clang/Frontend/VerifyDiagnosticsClient.h"
Daniel Dunbar4fdba992009-11-14 10:53:49 +000030#include "llvm/LLVMContext.h"
Daniel Dunbar9119e7e2009-11-16 22:38:48 +000031#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar70121eb2009-08-10 03:40:28 +000032#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar524b86f2008-10-28 00:38:08 +000033#include "llvm/Support/ManagedStatic.h"
Zhongxing Xu20922362008-11-26 05:23:17 +000034#include "llvm/Support/PluginLoader.h"
Chris Lattner09e94a32009-03-04 21:41:39 +000035#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner47099742009-02-18 01:51:21 +000036#include "llvm/Support/Timer.h"
Chris Lattner0fa0daa2009-08-24 04:11:30 +000037#include "llvm/Support/raw_ostream.h"
Daniel Dunbare553a722008-10-02 01:21:33 +000038#include "llvm/System/Host.h"
Chris Lattnerdcaa0962008-03-03 03:16:03 +000039#include "llvm/System/Path.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000040#include "llvm/System/Signals.h"
Chris Lattner2fe11942009-06-17 17:25:50 +000041#include "llvm/Target/TargetSelect.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000042using namespace clang;
43
44//===----------------------------------------------------------------------===//
Daniel Dunbarc363cb12009-11-16 22:38:40 +000045// Main driver
Reid Spencer5f016e22007-07-11 17:01:13 +000046//===----------------------------------------------------------------------===//
47
Daniel Dunbar750156a2009-11-07 04:19:57 +000048std::string GetBuiltinIncludePath(const char *Argv0) {
49 llvm::sys::Path P =
50 llvm::sys::Path::GetMainExecutable(Argv0,
51 (void*)(intptr_t) GetBuiltinIncludePath);
Rafael Espindola1bb15a92009-10-05 13:12:17 +000052
Daniel Dunbar750156a2009-11-07 04:19:57 +000053 if (!P.isEmpty()) {
54 P.eraseComponent(); // Remove /clang from foo/bin/clang
55 P.eraseComponent(); // Remove /bin from foo/bin
Rafael Espindola1bb15a92009-10-05 13:12:17 +000056
Daniel Dunbar750156a2009-11-07 04:19:57 +000057 // Get foo/lib/clang/<version>/include
58 P.appendComponent("lib");
59 P.appendComponent("clang");
60 P.appendComponent(CLANG_VERSION_STRING);
61 P.appendComponent("include");
62 }
Rafael Espindola1bb15a92009-10-05 13:12:17 +000063
Daniel Dunbar750156a2009-11-07 04:19:57 +000064 return P.str();
Rafael Espindola1bb15a92009-10-05 13:12:17 +000065}
66
Daniel Dunbarc363cb12009-11-16 22:38:40 +000067static void LLVMErrorHandler(void *UserData, const std::string &Message) {
68 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
69
70 Diags.Report(diag::err_fe_error_backend) << Message;
71
72 // We cannot recover from llvm errors.
73 exit(1);
74}
Reid Spencer5f016e22007-07-11 17:01:13 +000075
Daniel Dunbaraa576142009-11-12 15:23:20 +000076/// ClangFrontendTimer - The front-end activities should charge time to it with
77/// TimeRegion. The -ftime-report option controls whether this will do
78/// anything.
79llvm::Timer *ClangFrontendTimer = 0;
80
Daniel Dunbard10c5b82009-11-15 00:12:04 +000081static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +000082 using namespace clang::frontend;
83
Daniel Dunbard10c5b82009-11-15 00:12:04 +000084 switch (CI.getFrontendOpts().ProgramAction) {
85 default:
86 llvm::llvm_unreachable("Invalid program action!");
87
Daniel Dunbar4fdba992009-11-14 10:53:49 +000088 case ASTDump: return new ASTDumpAction();
89 case ASTPrint: return new ASTPrintAction();
90 case ASTPrintXML: return new ASTPrintXMLAction();
91 case ASTView: return new ASTViewAction();
92 case DumpRawTokens: return new DumpRawTokensAction();
93 case DumpRecordLayouts: return new DumpRecordAction();
94 case DumpTokens: return new DumpTokensAction();
95 case EmitAssembly: return new EmitAssemblyAction();
96 case EmitBC: return new EmitBCAction();
97 case EmitHTML: return new HTMLPrintAction();
98 case EmitLLVM: return new EmitLLVMAction();
99 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
100 case FixIt: return new FixItAction();
101 case GeneratePCH: return new GeneratePCHAction();
102 case GeneratePTH: return new GeneratePTHAction();
103 case InheritanceView: return new InheritanceViewAction();
104 case ParseNoop: return new ParseOnlyAction();
105 case ParsePrintCallbacks: return new PrintParseAction();
106 case ParseSyntaxOnly: return new SyntaxOnlyAction();
Daniel Dunbard10c5b82009-11-15 00:12:04 +0000107
108 case PluginAction: {
109 if (CI.getFrontendOpts().ActionName == "help") {
110 llvm::errs() << "clang-cc plugins:\n";
111 for (FrontendPluginRegistry::iterator it =
112 FrontendPluginRegistry::begin(),
113 ie = FrontendPluginRegistry::end();
114 it != ie; ++it)
115 llvm::errs() << " " << it->getName() << " - " << it->getDesc() << "\n";
116 exit(1);
117 }
118
119 for (FrontendPluginRegistry::iterator it =
120 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
121 it != ie; ++it) {
122 if (it->getName() == CI.getFrontendOpts().ActionName)
123 return it->instantiate();
124 }
125
126 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
127 << CI.getFrontendOpts().ActionName;
128 return 0;
129 }
130
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000131 case PrintDeclContext: return new DeclContextPrintAction();
132 case PrintPreprocessedInput: return new PrintPreprocessedAction();
133 case RewriteBlocks: return new RewriteBlocksAction();
134 case RewriteMacros: return new RewriteMacrosAction();
135 case RewriteObjC: return new RewriteObjCAction();
136 case RewriteTest: return new RewriteTestAction();
137 case RunAnalysis: return new AnalysisAction();
138 case RunPreprocessorOnly: return new PreprocessOnlyAction();
Eli Friedman66d6f042009-05-18 22:20:00 +0000139 }
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +0000140}
141
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000142static TargetInfo *
143ConstructCompilerInvocation(CompilerInvocation &Opts, Diagnostic &Diags,
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000144 const char *Argv0, bool &IsAST) {
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000145 // Initialize target options.
146 InitializeTargetOptions(Opts.getTargetOpts());
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000147
148 // Get information about the target being compiled for.
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000149 llvm::OwningPtr<TargetInfo> Target(
150 TargetInfo::CreateTargetInfo(Diags, Opts.getTargetOpts()));
151 if (!Target)
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000152 return 0;
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000153
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000154 // Initialize frontend options.
155 InitializeFrontendOptions(Opts.getFrontendOpts());
Daniel Dunbarfcb0c3b2009-11-10 18:47:35 +0000156
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000157 // Determine the input language, we currently require all files to match.
158 FrontendOptions::InputKind IK = Opts.getFrontendOpts().Inputs[0].first;
159 for (unsigned i = 1, e = Opts.getFrontendOpts().Inputs.size(); i != e; ++i) {
160 if (Opts.getFrontendOpts().Inputs[i].first != IK) {
161 llvm::errs() << "error: cannot have multiple input files of distinct "
162 << "language kinds without -x\n";
163 return 0;
164 }
165 }
166
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000167 // Initialize language options.
Daniel Dunbar5746f1f2009-11-12 00:24:10 +0000168 //
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000169 // FIXME: These aren't used during operations on ASTs. Split onto a separate
170 // code path to make this obvious.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000171 IsAST = (IK == FrontendOptions::IK_AST);
Daniel Dunbar26266882009-11-12 23:52:32 +0000172 if (!IsAST)
Daniel Dunbar6143ea22009-11-16 22:38:14 +0000173 InitializeLangOptions(Opts.getLangOpts(), IK, *Target);
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000174
Daniel Dunbar5746f1f2009-11-12 00:24:10 +0000175 // Initialize the static analyzer options.
176 InitializeAnalyzerOptions(Opts.getAnalyzerOpts());
177
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000178 // Initialize the dependency output options (-M...).
179 InitializeDependencyOutputOptions(Opts.getDependencyOutputOpts());
180
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000181 // Initialize the header search options.
Daniel Dunbarf7973292009-11-11 08:13:32 +0000182 InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(),
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000183 GetBuiltinIncludePath(Argv0));
Daniel Dunbar5fc7d342009-11-09 23:12:31 +0000184
185 // Initialize the other preprocessor options.
186 InitializePreprocessorOptions(Opts.getPreprocessorOpts());
Daniel Dunbar36f4ec32009-11-10 16:19:45 +0000187
Daniel Dunbar29cf7462009-11-11 10:07:44 +0000188 // Initialize the preprocessed output options.
189 InitializePreprocessorOutputOptions(Opts.getPreprocessorOutputOpts());
190
Daniel Dunbar6143ea22009-11-16 22:38:14 +0000191 // Initialize backend options, which may also be used to key some language
192 // options.
193 InitializeCodeGenOptions(Opts.getCodeGenOpts(), Opts.getLangOpts(),
194 Opts.getFrontendOpts().ShowTimers);
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000195
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000196 return Target.take();
Daniel Dunbare29709f2009-11-09 20:55:08 +0000197}
198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199int main(int argc, char **argv) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 llvm::sys::PrintStackTraceOnErrorSignal();
Chris Lattner09e94a32009-03-04 21:41:39 +0000201 llvm::PrettyStackTraceProgram X(argc, argv);
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000202 CompilerInstance Clang(&llvm::getGlobalContext(), false);
Daniel Dunbard6970812009-09-02 23:20:15 +0000203
Daniel Dunbar4d861512009-09-03 04:54:12 +0000204 // Initialize targets first, so that --version shows registered targets.
Chris Lattner2fe11942009-06-17 17:25:50 +0000205 llvm::InitializeAllTargets();
206 llvm::InitializeAllAsmPrinters();
Daniel Dunbard6970812009-09-02 23:20:15 +0000207
208 llvm::cl::ParseCommandLineOptions(argc, argv,
209 "LLVM 'Clang' Compiler: http://clang.llvm.org\n");
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Daniel Dunbar00e5b8d2009-11-12 06:48:31 +0000211 // Construct the diagnostic engine first, so that we can build a diagnostic
212 // client to use for any errors during option handling.
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000213 InitializeDiagnosticOptions(Clang.getDiagnosticOpts());
214 Clang.createDiagnostics(argc, argv);
Daniel Dunbar704e48a2009-11-13 08:20:57 +0000215 if (!Clang.hasDiagnostics())
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000216 return 1;
Ted Kremenek31e703b2007-12-11 23:28:38 +0000217
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000218 // Set an error handler, so that any LLVM backend diagnostics go through our
219 // error handler.
220 llvm::llvm_install_error_handler(LLVMErrorHandler,
221 static_cast<void*>(&Clang.getDiagnostics()));
Daniel Dunbar70121eb2009-08-10 03:40:28 +0000222
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000223 // Now that we have initialized the diagnostics engine, create the target and
224 // the compiler invocation object.
Daniel Dunbar227b2382009-11-09 22:45:57 +0000225 //
226 // FIXME: We should move .ast inputs to taking a separate path, they are
227 // really quite different.
Daniel Dunbar67f401b2009-11-17 21:51:03 +0000228 bool IsAST = false;
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000229 Clang.setTarget(
230 ConstructCompilerInvocation(Clang.getInvocation(), Clang.getDiagnostics(),
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000231 argv[0], IsAST));
Daniel Dunbar704e48a2009-11-13 08:20:57 +0000232 if (!Clang.hasTarget())
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000233 return 1;
Daniel Dunbar26266882009-11-12 23:52:32 +0000234
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000235 // Validate/process some options
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000236 if (Clang.getHeaderSearchOpts().Verbose)
Daniel Dunbar1417c742009-11-12 23:52:46 +0000237 llvm::errs() << "clang-cc version " CLANG_VERSION_STRING
238 << " based upon " << PACKAGE_STRING
239 << " hosted on " << llvm::sys::getHostTriple() << "\n";
240
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000241 if (Clang.getFrontendOpts().ShowTimers)
Daniel Dunbar26266882009-11-12 23:52:32 +0000242 ClangFrontendTimer = new llvm::Timer("Clang front-end time");
243
Daniel Dunbar79b55f92009-11-14 04:39:29 +0000244 // Enforce certain implications.
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000245 if (!Clang.getFrontendOpts().ViewClassInheritance.empty())
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000246 Clang.getFrontendOpts().ProgramAction = frontend::InheritanceView;
Daniel Dunbar79b55f92009-11-14 04:39:29 +0000247 if (!Clang.getFrontendOpts().FixItLocations.empty())
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000248 Clang.getFrontendOpts().ProgramAction = frontend::FixIt;
Daniel Dunbar227b2382009-11-09 22:45:57 +0000249
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000250 for (unsigned i = 0, e = Clang.getFrontendOpts().Inputs.size(); i != e; ++i) {
251 const std::string &InFile = Clang.getFrontendOpts().Inputs[i].second;
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000253 // If we aren't using an AST file, setup the file and source managers and
254 // the preprocessor.
255 if (!IsAST) {
256 if (!i) {
257 // Create a file manager object to provide access to and cache the
258 // filesystem.
259 Clang.createFileManager();
260
261 // Create the source manager.
262 Clang.createSourceManager();
263 } else {
264 // Reset the ID tables if we are reusing the SourceManager.
265 Clang.getSourceManager().clearIDTables();
266 }
267
268 // Create the preprocessor.
269 Clang.createPreprocessor();
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +0000270 }
271
Daniel Dunbard10c5b82009-11-15 00:12:04 +0000272 llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(Clang));
273 if (!Act)
274 break;
275
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000276 Act->setCurrentTimer(ClangFrontendTimer);
277 if (Act->BeginSourceFile(Clang, InFile, IsAST)) {
278 Act->Execute();
279 Act->EndSourceFile();
280 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 }
Chris Lattner11215192008-03-14 06:12:05 +0000282
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000283 if (Clang.getDiagnosticOpts().ShowCarets)
284 if (unsigned NumDiagnostics = Clang.getDiagnostics().getNumDiagnostics())
Mike Stumpfc0fed32009-04-28 01:19:10 +0000285 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
286 (NumDiagnostics == 1 ? "" : "s"));
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000288 if (Clang.getFrontendOpts().ShowStats) {
Daniel Dunbar16b74492009-11-13 04:12:06 +0000289 Clang.getFileManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 fprintf(stderr, "\n");
291 }
Chris Lattner75a97cb2009-04-17 21:05:01 +0000292
293 delete ClangFrontendTimer;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000295 // Return the appropriate status when verifying diagnostics.
296 //
297 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
298 // this.
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000299 if (Clang.getDiagnosticOpts().VerifyDiagnostics)
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000300 return static_cast<VerifyDiagnosticsClient&>(
301 Clang.getDiagnosticClient()).HadErrors();
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Daniel Dunbar524b86f2008-10-28 00:38:08 +0000303 // Managed static deconstruction. Useful for making things like
304 // -time-passes usable.
305 llvm::llvm_shutdown();
306
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000307 return (Clang.getDiagnostics().getNumErrors() != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000308}
Daniel Dunbard10c5b82009-11-15 00:12:04 +0000309