blob: 3dfe51190db023757d9a4e67ae5e99bad2633661 [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"
Daniel Dunbar1a506282009-11-25 10:53:00 +000042#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000043using namespace clang;
44
45//===----------------------------------------------------------------------===//
Daniel Dunbarc363cb12009-11-16 22:38:40 +000046// Main driver
Reid Spencer5f016e22007-07-11 17:01:13 +000047//===----------------------------------------------------------------------===//
48
Daniel Dunbar750156a2009-11-07 04:19:57 +000049std::string GetBuiltinIncludePath(const char *Argv0) {
50 llvm::sys::Path P =
51 llvm::sys::Path::GetMainExecutable(Argv0,
52 (void*)(intptr_t) GetBuiltinIncludePath);
Rafael Espindola1bb15a92009-10-05 13:12:17 +000053
Daniel Dunbar750156a2009-11-07 04:19:57 +000054 if (!P.isEmpty()) {
55 P.eraseComponent(); // Remove /clang from foo/bin/clang
56 P.eraseComponent(); // Remove /bin from foo/bin
Rafael Espindola1bb15a92009-10-05 13:12:17 +000057
Daniel Dunbar750156a2009-11-07 04:19:57 +000058 // Get foo/lib/clang/<version>/include
59 P.appendComponent("lib");
60 P.appendComponent("clang");
61 P.appendComponent(CLANG_VERSION_STRING);
62 P.appendComponent("include");
63 }
Rafael Espindola1bb15a92009-10-05 13:12:17 +000064
Daniel Dunbar750156a2009-11-07 04:19:57 +000065 return P.str();
Rafael Espindola1bb15a92009-10-05 13:12:17 +000066}
67
Daniel Dunbarc363cb12009-11-16 22:38:40 +000068static void LLVMErrorHandler(void *UserData, const std::string &Message) {
69 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
70
71 Diags.Report(diag::err_fe_error_backend) << Message;
72
73 // We cannot recover from llvm errors.
74 exit(1);
75}
Reid Spencer5f016e22007-07-11 17:01:13 +000076
Daniel Dunbaraa576142009-11-12 15:23:20 +000077/// ClangFrontendTimer - The front-end activities should charge time to it with
78/// TimeRegion. The -ftime-report option controls whether this will do
79/// anything.
80llvm::Timer *ClangFrontendTimer = 0;
81
Daniel Dunbard10c5b82009-11-15 00:12:04 +000082static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +000083 using namespace clang::frontend;
84
Daniel Dunbard10c5b82009-11-15 00:12:04 +000085 switch (CI.getFrontendOpts().ProgramAction) {
86 default:
87 llvm::llvm_unreachable("Invalid program action!");
88
Daniel Dunbar4fdba992009-11-14 10:53:49 +000089 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();
Daniel Dunbard10c5b82009-11-15 00:12:04 +0000108
109 case PluginAction: {
110 if (CI.getFrontendOpts().ActionName == "help") {
111 llvm::errs() << "clang-cc plugins:\n";
112 for (FrontendPluginRegistry::iterator it =
113 FrontendPluginRegistry::begin(),
114 ie = FrontendPluginRegistry::end();
115 it != ie; ++it)
116 llvm::errs() << " " << it->getName() << " - " << it->getDesc() << "\n";
117 exit(1);
118 }
119
120 for (FrontendPluginRegistry::iterator it =
121 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
122 it != ie; ++it) {
123 if (it->getName() == CI.getFrontendOpts().ActionName)
124 return it->instantiate();
125 }
126
127 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
128 << CI.getFrontendOpts().ActionName;
129 return 0;
130 }
131
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000132 case PrintDeclContext: return new DeclContextPrintAction();
133 case PrintPreprocessedInput: return new PrintPreprocessedAction();
134 case RewriteBlocks: return new RewriteBlocksAction();
135 case RewriteMacros: return new RewriteMacrosAction();
136 case RewriteObjC: return new RewriteObjCAction();
137 case RewriteTest: return new RewriteTestAction();
138 case RunAnalysis: return new AnalysisAction();
139 case RunPreprocessorOnly: return new PreprocessOnlyAction();
Eli Friedman66d6f042009-05-18 22:20:00 +0000140 }
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +0000141}
142
Daniel Dunbar33b26ea32009-11-20 16:55:31 +0000143static bool ConstructCompilerInvocation(CompilerInvocation &Opts,
144 Diagnostic &Diags,
145 const char *Argv0, bool &IsAST) {
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000146 // Initialize target options.
147 InitializeTargetOptions(Opts.getTargetOpts());
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000148
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000149 // Initialize frontend options.
150 InitializeFrontendOptions(Opts.getFrontendOpts());
Daniel Dunbarfcb0c3b2009-11-10 18:47:35 +0000151
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000152 // Determine the input language, we currently require all files to match.
153 FrontendOptions::InputKind IK = Opts.getFrontendOpts().Inputs[0].first;
154 for (unsigned i = 1, e = Opts.getFrontendOpts().Inputs.size(); i != e; ++i) {
155 if (Opts.getFrontendOpts().Inputs[i].first != IK) {
156 llvm::errs() << "error: cannot have multiple input files of distinct "
157 << "language kinds without -x\n";
Daniel Dunbar33b26ea32009-11-20 16:55:31 +0000158 return false;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000159 }
160 }
161
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000162 // Initialize language options.
Daniel Dunbar5746f1f2009-11-12 00:24:10 +0000163 //
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000164 // FIXME: These aren't used during operations on ASTs. Split onto a separate
165 // code path to make this obvious.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000166 IsAST = (IK == FrontendOptions::IK_AST);
Daniel Dunbar26266882009-11-12 23:52:32 +0000167 if (!IsAST)
Daniel Dunbar33b26ea32009-11-20 16:55:31 +0000168 InitializeLangOptions(Opts.getLangOpts(), IK);
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000169
Daniel Dunbar5746f1f2009-11-12 00:24:10 +0000170 // Initialize the static analyzer options.
171 InitializeAnalyzerOptions(Opts.getAnalyzerOpts());
172
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000173 // Initialize the dependency output options (-M...).
174 InitializeDependencyOutputOptions(Opts.getDependencyOutputOpts());
175
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000176 // Initialize the header search options.
Daniel Dunbarf7973292009-11-11 08:13:32 +0000177 InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(),
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000178 GetBuiltinIncludePath(Argv0));
Daniel Dunbar5fc7d342009-11-09 23:12:31 +0000179
180 // Initialize the other preprocessor options.
181 InitializePreprocessorOptions(Opts.getPreprocessorOpts());
Daniel Dunbar36f4ec32009-11-10 16:19:45 +0000182
Daniel Dunbar29cf7462009-11-11 10:07:44 +0000183 // Initialize the preprocessed output options.
184 InitializePreprocessorOutputOptions(Opts.getPreprocessorOutputOpts());
185
Daniel Dunbar33b26ea32009-11-20 16:55:31 +0000186 // Initialize backend options.
Daniel Dunbar6143ea22009-11-16 22:38:14 +0000187 InitializeCodeGenOptions(Opts.getCodeGenOpts(), Opts.getLangOpts(),
188 Opts.getFrontendOpts().ShowTimers);
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000189
Daniel Dunbar33b26ea32009-11-20 16:55:31 +0000190 return true;
Daniel Dunbare29709f2009-11-09 20:55:08 +0000191}
192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193int main(int argc, char **argv) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 llvm::sys::PrintStackTraceOnErrorSignal();
Chris Lattner09e94a32009-03-04 21:41:39 +0000195 llvm::PrettyStackTraceProgram X(argc, argv);
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000196 CompilerInstance Clang(&llvm::getGlobalContext(), false);
Daniel Dunbard6970812009-09-02 23:20:15 +0000197
Daniel Dunbar4d861512009-09-03 04:54:12 +0000198 // Initialize targets first, so that --version shows registered targets.
Chris Lattner2fe11942009-06-17 17:25:50 +0000199 llvm::InitializeAllTargets();
200 llvm::InitializeAllAsmPrinters();
Daniel Dunbard6970812009-09-02 23:20:15 +0000201
202 llvm::cl::ParseCommandLineOptions(argc, argv,
203 "LLVM 'Clang' Compiler: http://clang.llvm.org\n");
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Daniel Dunbar00e5b8d2009-11-12 06:48:31 +0000205 // Construct the diagnostic engine first, so that we can build a diagnostic
206 // client to use for any errors during option handling.
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000207 InitializeDiagnosticOptions(Clang.getDiagnosticOpts());
208 Clang.createDiagnostics(argc, argv);
Daniel Dunbar704e48a2009-11-13 08:20:57 +0000209 if (!Clang.hasDiagnostics())
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000210 return 1;
Ted Kremenek31e703b2007-12-11 23:28:38 +0000211
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000212 // Set an error handler, so that any LLVM backend diagnostics go through our
213 // error handler.
214 llvm::llvm_install_error_handler(LLVMErrorHandler,
215 static_cast<void*>(&Clang.getDiagnostics()));
Daniel Dunbar70121eb2009-08-10 03:40:28 +0000216
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000217 // Now that we have initialized the diagnostics engine, create the target and
218 // the compiler invocation object.
Daniel Dunbar227b2382009-11-09 22:45:57 +0000219 //
220 // FIXME: We should move .ast inputs to taking a separate path, they are
221 // really quite different.
Daniel Dunbar67f401b2009-11-17 21:51:03 +0000222 bool IsAST = false;
Daniel Dunbar33b26ea32009-11-20 16:55:31 +0000223 if (!ConstructCompilerInvocation(Clang.getInvocation(),
224 Clang.getDiagnostics(),
225 argv[0], IsAST))
226 return 1;
227
228 // Create the target instance.
229 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
230 Clang.getTargetOpts()));
Daniel Dunbar704e48a2009-11-13 08:20:57 +0000231 if (!Clang.hasTarget())
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000232 return 1;
Daniel Dunbar26266882009-11-12 23:52:32 +0000233
Daniel Dunbar33b26ea32009-11-20 16:55:31 +0000234 // Inform the target of the language options
235 //
236 // FIXME: We shouldn't need to do this, the target should be immutable once
237 // created. This complexity should be lifted elsewhere.
238 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
239
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000240 // Validate/process some options
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000241 if (Clang.getHeaderSearchOpts().Verbose)
Daniel Dunbar1417c742009-11-12 23:52:46 +0000242 llvm::errs() << "clang-cc version " CLANG_VERSION_STRING
243 << " based upon " << PACKAGE_STRING
244 << " hosted on " << llvm::sys::getHostTriple() << "\n";
245
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000246 if (Clang.getFrontendOpts().ShowTimers)
Daniel Dunbar26266882009-11-12 23:52:32 +0000247 ClangFrontendTimer = new llvm::Timer("Clang front-end time");
248
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000249 for (unsigned i = 0, e = Clang.getFrontendOpts().Inputs.size(); i != e; ++i) {
250 const std::string &InFile = Clang.getFrontendOpts().Inputs[i].second;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000252 // If we aren't using an AST file, setup the file and source managers and
253 // the preprocessor.
254 if (!IsAST) {
255 if (!i) {
256 // Create a file manager object to provide access to and cache the
257 // filesystem.
258 Clang.createFileManager();
259
260 // Create the source manager.
261 Clang.createSourceManager();
262 } else {
263 // Reset the ID tables if we are reusing the SourceManager.
264 Clang.getSourceManager().clearIDTables();
265 }
266
267 // Create the preprocessor.
268 Clang.createPreprocessor();
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +0000269 }
270
Daniel Dunbard10c5b82009-11-15 00:12:04 +0000271 llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(Clang));
272 if (!Act)
273 break;
274
Daniel Dunbar4fdba992009-11-14 10:53:49 +0000275 Act->setCurrentTimer(ClangFrontendTimer);
276 if (Act->BeginSourceFile(Clang, InFile, IsAST)) {
277 Act->Execute();
278 Act->EndSourceFile();
279 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 }
Chris Lattner11215192008-03-14 06:12:05 +0000281
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000282 if (Clang.getDiagnosticOpts().ShowCarets)
283 if (unsigned NumDiagnostics = Clang.getDiagnostics().getNumDiagnostics())
Mike Stumpfc0fed32009-04-28 01:19:10 +0000284 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
285 (NumDiagnostics == 1 ? "" : "s"));
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000287 if (Clang.getFrontendOpts().ShowStats) {
Daniel Dunbar16b74492009-11-13 04:12:06 +0000288 Clang.getFileManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 fprintf(stderr, "\n");
290 }
Chris Lattner75a97cb2009-04-17 21:05:01 +0000291
292 delete ClangFrontendTimer;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000294 // Return the appropriate status when verifying diagnostics.
295 //
296 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
297 // this.
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000298 if (Clang.getDiagnosticOpts().VerifyDiagnostics)
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000299 return static_cast<VerifyDiagnosticsClient&>(
300 Clang.getDiagnosticClient()).HadErrors();
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Daniel Dunbar524b86f2008-10-28 00:38:08 +0000302 // Managed static deconstruction. Useful for making things like
303 // -time-passes usable.
304 llvm::llvm_shutdown();
305
Daniel Dunbar2a79e162009-11-13 03:51:44 +0000306 return (Clang.getDiagnostics().getNumErrors() != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000307}
Daniel Dunbard10c5b82009-11-15 00:12:04 +0000308