blob: dacdca75a9d2ad2cc5d4d77c5b057f25dff54ecd [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- clang.cpp - C-Language Front-end ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This utility may be invoked in the following manner:
11// clang --help - Output help info.
12// clang [options] - Read from stdin.
13// clang [options] file - Read from "file".
14// clang [options] file1 file2 - Read these files.
15//
16//===----------------------------------------------------------------------===//
17//
18// TODO: Options to support:
19//
20// -ffatal-errors
21// -ftabstop=width
22//
23//===----------------------------------------------------------------------===//
24
25#include "clang.h"
Chris Lattnereb8c9632007-10-07 06:04:32 +000026#include "ASTConsumers.h"
Daniel Dunbaraa7a0662008-10-23 05:50:47 +000027#include "clang/Driver/CompileOptions.h"
Ted Kremenek21b88b72008-11-03 22:31:48 +000028#include "clang/Driver/PathDiagnosticClients.h"
Nico Weber770e3882008-08-22 09:25:22 +000029#include "clang/Driver/InitHeaderSearch.h"
Nico Weber0e13eaa2008-08-05 23:33:20 +000030#include "clang/Driver/TextDiagnosticBuffer.h"
31#include "clang/Driver/TextDiagnosticPrinter.h"
Ted Kremenekfd75e312008-03-27 06:17:42 +000032#include "clang/Analysis/PathDiagnostic.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000033#include "clang/AST/Decl.h"
Ted Kremenekac881932007-12-18 21:34:28 +000034#include "clang/AST/TranslationUnit.h"
Chris Lattnerf5e9db02008-02-06 02:01:47 +000035#include "clang/CodeGen/ModuleBuilder.h"
Chris Lattner0bed6ec2008-02-06 00:23:21 +000036#include "clang/Sema/ParseAST.h"
Chris Lattner1cc01712007-09-15 22:56:56 +000037#include "clang/AST/ASTConsumer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000038#include "clang/Parse/Parser.h"
39#include "clang/Lex/HeaderSearch.h"
40#include "clang/Basic/FileManager.h"
41#include "clang/Basic/SourceManager.h"
42#include "clang/Basic/TargetInfo.h"
Chris Lattner01de9c82008-09-30 20:16:56 +000043#include "llvm/ADT/OwningPtr.h"
Chris Lattnerac139d22007-12-15 23:20:07 +000044#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner01de9c82008-09-30 20:16:56 +000045#include "llvm/ADT/StringExtras.h"
46#include "llvm/Config/config.h"
Chris Lattner4b009652007-07-25 00:24:17 +000047#include "llvm/Support/CommandLine.h"
Daniel Dunbarbb298c02008-10-28 00:38:08 +000048#include "llvm/Support/ManagedStatic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000049#include "llvm/Support/MemoryBuffer.h"
Zhongxing Xuf4ec4d92008-11-26 05:23:17 +000050#include "llvm/Support/PluginLoader.h"
Daniel Dunbarabe2e542008-10-02 01:21:33 +000051#include "llvm/System/Host.h"
Chris Lattner3ee4a2f2008-03-03 03:16:03 +000052#include "llvm/System/Path.h"
Chris Lattner01de9c82008-09-30 20:16:56 +000053#include "llvm/System/Signals.h"
Chris Lattner4b009652007-07-25 00:24:17 +000054using namespace clang;
55
56//===----------------------------------------------------------------------===//
57// Global options.
58//===----------------------------------------------------------------------===//
59
Daniel Dunbar4efedde2008-10-16 16:54:18 +000060static bool HadErrors = false;
Daniel Dunbar70a66b12008-10-04 23:42:49 +000061
Chris Lattner4b009652007-07-25 00:24:17 +000062static llvm::cl::opt<bool>
63Verbose("v", llvm::cl::desc("Enable verbose output"));
64static llvm::cl::opt<bool>
Nate Begeman6acbedd2007-12-30 01:38:50 +000065Stats("print-stats",
66 llvm::cl::desc("Print performance metrics and statistics"));
Daniel Dunbar4efedde2008-10-16 16:54:18 +000067static llvm::cl::opt<bool>
68DisableFree("disable-free",
69 llvm::cl::desc("Disable freeing of memory on exit"),
70 llvm::cl::init(false));
Chris Lattner4b009652007-07-25 00:24:17 +000071
72enum ProgActions {
Steve Naroff44e81222008-04-14 22:03:09 +000073 RewriteObjC, // ObjC->C Rewriter.
Steve Naroff93c18352008-09-18 14:10:13 +000074 RewriteBlocks, // ObjC->C Rewriter for Blocks.
Chris Lattner1665a9f2008-05-08 06:52:13 +000075 RewriteMacros, // Expand macros but not #includes.
Chris Lattnerc3fbf392008-10-12 05:29:20 +000076 RewriteTest, // Rewriter playground
Ted Kremeneke1a79d82008-03-19 07:53:42 +000077 HTMLTest, // HTML displayer testing stuff.
Daniel Dunbar85e44e22008-10-21 23:49:24 +000078 EmitAssembly, // Emit a .s file.
Chris Lattner4b009652007-07-25 00:24:17 +000079 EmitLLVM, // Emit a .ll file.
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +000080 EmitBC, // Emit a .bc file.
Ted Kremenek397de012007-12-13 00:37:31 +000081 SerializeAST, // Emit a .ast file.
Ted Kremenek24612ae2008-03-18 21:19:49 +000082 EmitHTML, // Translate input source into HTML.
Chris Lattner4045a8a2007-10-11 00:18:28 +000083 ASTPrint, // Parse ASTs and print them.
84 ASTDump, // Parse ASTs and dump them.
85 ASTView, // Parse ASTs and view them in Graphviz.
Ted Kremenek221bb8d2007-10-16 23:37:27 +000086 TestSerialization, // Run experimental serialization code.
Chris Lattner4b009652007-07-25 00:24:17 +000087 ParsePrintCallbacks, // Parse and print each callback.
88 ParseSyntaxOnly, // Parse and perform semantic analysis.
89 ParseNoop, // Parse with noop callbacks.
90 RunPreprocessorOnly, // Just lex, no output.
91 PrintPreprocessedInput, // -E mode.
Chris Lattneraf669fb2008-10-12 05:03:36 +000092 DumpTokens, // Dump out preprocessed tokens.
93 DumpRawTokens, // Dump out raw tokens.
Ted Kremenek71c6cc62008-10-21 00:54:44 +000094 RunAnalysis, // Run one or more source code analyses.
Ted Kremenekd9ceb3d2008-10-23 23:36:29 +000095 GeneratePCH, // Generate precompiled header.
96 InheritanceView // View C++ inheritance for a specified class.
Chris Lattner4b009652007-07-25 00:24:17 +000097};
98
99static llvm::cl::opt<ProgActions>
100ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
101 llvm::cl::init(ParseSyntaxOnly),
102 llvm::cl::values(
103 clEnumValN(RunPreprocessorOnly, "Eonly",
104 "Just run preprocessor, no output (for timings)"),
105 clEnumValN(PrintPreprocessedInput, "E",
106 "Run preprocessor, emit preprocessed file"),
Chris Lattneraf669fb2008-10-12 05:03:36 +0000107 clEnumValN(DumpRawTokens, "dump-raw-tokens",
108 "Lex file in raw mode and dump raw tokens"),
109 clEnumValN(DumpTokens, "dump-tokens",
Chris Lattner4b009652007-07-25 00:24:17 +0000110 "Run preprocessor, dump internal rep of tokens"),
111 clEnumValN(ParseNoop, "parse-noop",
112 "Run parser with noop callbacks (for timings)"),
113 clEnumValN(ParseSyntaxOnly, "fsyntax-only",
114 "Run parser and perform semantic analysis"),
115 clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
116 "Run parser and print each callback invoked"),
Ted Kremenek24612ae2008-03-18 21:19:49 +0000117 clEnumValN(EmitHTML, "emit-html",
118 "Output input source as HTML"),
Chris Lattner4045a8a2007-10-11 00:18:28 +0000119 clEnumValN(ASTPrint, "ast-print",
120 "Build ASTs and then pretty-print them"),
121 clEnumValN(ASTDump, "ast-dump",
122 "Build ASTs and then debug dump them"),
Chris Lattner664dd082007-10-11 00:37:43 +0000123 clEnumValN(ASTView, "ast-view",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000124 "Build ASTs and view them with GraphViz"),
Ted Kremenek221bb8d2007-10-16 23:37:27 +0000125 clEnumValN(TestSerialization, "test-pickling",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000126 "Run prototype serialization code"),
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000127 clEnumValN(EmitAssembly, "S",
128 "Emit native assembly code"),
Chris Lattner4b009652007-07-25 00:24:17 +0000129 clEnumValN(EmitLLVM, "emit-llvm",
Ted Kremenek05334682007-09-06 21:26:58 +0000130 "Build ASTs then convert to LLVM, emit .ll file"),
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000131 clEnumValN(EmitBC, "emit-llvm-bc",
132 "Build ASTs then convert to LLVM, emit .bc file"),
Ted Kremenekd01eae62007-12-19 19:47:59 +0000133 clEnumValN(SerializeAST, "serialize",
Ted Kremenek397de012007-12-13 00:37:31 +0000134 "Build ASTs and emit .ast file"),
Chris Lattnerc3fbf392008-10-12 05:29:20 +0000135 clEnumValN(RewriteTest, "rewrite-test",
136 "Rewriter playground"),
Steve Naroff44e81222008-04-14 22:03:09 +0000137 clEnumValN(RewriteObjC, "rewrite-objc",
Chris Lattner1665a9f2008-05-08 06:52:13 +0000138 "Rewrite ObjC into C (code rewriter example)"),
139 clEnumValN(RewriteMacros, "rewrite-macros",
140 "Expand macros without full preprocessing"),
Steve Naroff93c18352008-09-18 14:10:13 +0000141 clEnumValN(RewriteBlocks, "rewrite-blocks",
142 "Rewrite Blocks to C"),
Chris Lattner4b009652007-07-25 00:24:17 +0000143 clEnumValEnd));
144
Ted Kremenekd01eae62007-12-19 19:47:59 +0000145
146static llvm::cl::opt<std::string>
147OutputFile("o",
Ted Kremenek09b3f0d2007-12-19 19:50:41 +0000148 llvm::cl::value_desc("path"),
Ted Kremenekd01eae62007-12-19 19:47:59 +0000149 llvm::cl::desc("Specify output file (for --serialize, this is a directory)"));
Ted Kremenek517cb512008-04-14 18:40:58 +0000150
151//===----------------------------------------------------------------------===//
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000152// Code Generator Options
153//===----------------------------------------------------------------------===//
154static llvm::cl::opt<bool>
155GenerateDebugInfo("g",
156 llvm::cl::desc("Generate source level debug information"));
157
158//===----------------------------------------------------------------------===//
Ted Kremenek517cb512008-04-14 18:40:58 +0000159// Diagnostic Options
160//===----------------------------------------------------------------------===//
161
Ted Kremenek10389cf2007-09-26 19:42:19 +0000162static llvm::cl::opt<bool>
163VerifyDiagnostics("verify",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000164 llvm::cl::desc("Verify emitted diagnostics and warnings"));
Ted Kremenek10389cf2007-09-26 19:42:19 +0000165
Ted Kremenekfd75e312008-03-27 06:17:42 +0000166static llvm::cl::opt<std::string>
167HTMLDiag("html-diags",
168 llvm::cl::desc("Generate HTML to report diagnostics"),
169 llvm::cl::value_desc("HTML directory"));
170
Nico Weber0e13eaa2008-08-05 23:33:20 +0000171static llvm::cl::opt<bool>
172NoShowColumn("fno-show-column",
173 llvm::cl::desc("Do not include column number on diagnostics"));
174
175static llvm::cl::opt<bool>
176NoCaretDiagnostics("fno-caret-diagnostics",
177 llvm::cl::desc("Do not include source line and caret with"
178 " diagnostics"));
179
180
Chris Lattner4b009652007-07-25 00:24:17 +0000181//===----------------------------------------------------------------------===//
Ted Kremenekd9ceb3d2008-10-23 23:36:29 +0000182// C++ Visualization.
183//===----------------------------------------------------------------------===//
184
185static llvm::cl::opt<std::string>
186InheritanceViewCls("cxx-inheritance-view",
187 llvm::cl::value_desc("class name"),
188 llvm::cl::desc("View C++ inhertance for a specified class"));
189
190//===----------------------------------------------------------------------===//
191// Analyzer Options.
Ted Kremenek517cb512008-04-14 18:40:58 +0000192//===----------------------------------------------------------------------===//
193
194static llvm::cl::opt<bool>
Ted Kremenekcf262252008-08-27 22:31:43 +0000195VisualizeEGDot("analyzer-viz-egraph-graphviz",
196 llvm::cl::desc("Display exploded graph using GraphViz"));
Ted Kremenek517cb512008-04-14 18:40:58 +0000197
198static llvm::cl::opt<bool>
Ted Kremenekcf262252008-08-27 22:31:43 +0000199VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
200 llvm::cl::desc("Display exploded graph using Ubigraph"));
201
202static llvm::cl::opt<bool>
203AnalyzeAll("analyzer-opt-analyze-headers",
Ted Kremenek517cb512008-04-14 18:40:58 +0000204 llvm::cl::desc("Force the static analyzer to analyze "
Sanjiv Gupta69683532008-05-08 08:28:14 +0000205 "functions defined in header files"));
Ted Kremenek517cb512008-04-14 18:40:58 +0000206
Ted Kremenek81ea7992008-07-02 00:03:09 +0000207static llvm::cl::list<Analyses>
Ted Kremenekc3803992008-10-24 01:04:59 +0000208AnalysisList(llvm::cl::desc("SCA Checks/Analyses:"),
Ted Kremenek81ea7992008-07-02 00:03:09 +0000209llvm::cl::values(
Ted Kremenekfbda0ef2008-07-15 00:46:02 +0000210#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000211clEnumValN(NAME, CMDFLAG, DESC),
212#include "Analyses.def"
213clEnumValEnd));
Ted Kremenek81ea7992008-07-02 00:03:09 +0000214
Ted Kremenekc3803992008-10-24 01:04:59 +0000215static llvm::cl::opt<AnalysisStores>
216AnalysisStoreOpt(llvm::cl::desc("SCA Low-Level Options (Store):"),
217 llvm::cl::init(BasicStoreModel),
218 llvm::cl::values(
219#define ANALYSIS_STORE(NAME, CMDFLAG, DESC)\
220clEnumValN(NAME##Model, "analyzer-store-" CMDFLAG, DESC),
221#include "Analyses.def"
222clEnumValEnd));
223
Ted Kremeneka3f825e2008-11-03 23:18:07 +0000224static llvm::cl::opt<AnalysisDiagClients>
225AnalysisDiagOpt(llvm::cl::desc("SCA Output Options:"),
226 llvm::cl::init(PD_HTML),
227 llvm::cl::values(
228#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN)\
229clEnumValN(PD_##NAME, "analyzer-output-" CMDFLAG, DESC),
230#include "Analyses.def"
231clEnumValEnd));
232
Ted Kremenek517cb512008-04-14 18:40:58 +0000233//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000234// Language Options
235//===----------------------------------------------------------------------===//
236
237enum LangKind {
238 langkind_unspecified,
239 langkind_c,
240 langkind_c_cpp,
Chris Lattnera19689a2008-10-22 17:29:21 +0000241 langkind_asm_cpp,
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000242 langkind_c_pch,
Chris Lattner4b009652007-07-25 00:24:17 +0000243 langkind_cxx,
244 langkind_cxx_cpp,
245 langkind_objc,
246 langkind_objc_cpp,
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000247 langkind_objc_pch,
Chris Lattner4b009652007-07-25 00:24:17 +0000248 langkind_objcxx,
249 langkind_objcxx_cpp
250};
251
252/* TODO: GCC also accepts:
253 c-header c++-header objective-c-header objective-c++-header
Chris Lattnera19689a2008-10-22 17:29:21 +0000254 assembler
Chris Lattner4b009652007-07-25 00:24:17 +0000255 ada, f77*, ratfor (!), f95, java, treelang
256 */
257static llvm::cl::opt<LangKind>
258BaseLang("x", llvm::cl::desc("Base language to compile"),
259 llvm::cl::init(langkind_unspecified),
260 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
261 clEnumValN(langkind_cxx, "c++", "C++"),
262 clEnumValN(langkind_objc, "objective-c", "Objective C"),
263 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
264 clEnumValN(langkind_c_cpp, "c-cpp-output",
265 "Preprocessed C"),
Chris Lattnera19689a2008-10-22 17:29:21 +0000266 clEnumValN(langkind_asm_cpp, "assembler-with-cpp",
267 "Preprocessed asm"),
Chris Lattner4b009652007-07-25 00:24:17 +0000268 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000269 "Preprocessed C++"),
Chris Lattner4b009652007-07-25 00:24:17 +0000270 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
271 "Preprocessed Objective C"),
272 clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output",
273 "Preprocessed Objective C++"),
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000274 clEnumValN(langkind_c_pch,"c-header",
275 "Precompiled C header"),
276 clEnumValN(langkind_objc_pch, "objective-c-header",
277 "Precompiled Objective C header"),
Chris Lattner4b009652007-07-25 00:24:17 +0000278 clEnumValEnd));
279
280static llvm::cl::opt<bool>
281LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
282 llvm::cl::Hidden);
283static llvm::cl::opt<bool>
284LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
285 llvm::cl::Hidden);
286
Ted Kremenek11ad8952007-12-05 23:49:08 +0000287/// InitializeBaseLanguage - Handle the -x foo options.
288static void InitializeBaseLanguage() {
289 if (LangObjC)
290 BaseLang = langkind_objc;
291 else if (LangObjCXX)
292 BaseLang = langkind_objcxx;
293}
294
295static LangKind GetLanguage(const std::string &Filename) {
296 if (BaseLang != langkind_unspecified)
297 return BaseLang;
298
299 std::string::size_type DotPos = Filename.rfind('.');
300
301 if (DotPos == std::string::npos) {
302 BaseLang = langkind_c; // Default to C if no extension.
Chris Lattner4eac0502008-01-04 19:12:28 +0000303 return langkind_c;
Chris Lattner4b009652007-07-25 00:24:17 +0000304 }
305
Ted Kremenek11ad8952007-12-05 23:49:08 +0000306 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
307 // C header: .h
308 // C++ header: .hh or .H;
309 // assembler no preprocessing: .s
310 // assembler: .S
311 if (Ext == "c")
312 return langkind_c;
Chris Lattner1cbb3032008-10-28 20:33:42 +0000313 else if (Ext == "S")
Chris Lattnera19689a2008-10-22 17:29:21 +0000314 return langkind_asm_cpp;
Ted Kremenek11ad8952007-12-05 23:49:08 +0000315 else if (Ext == "i")
316 return langkind_c_cpp;
317 else if (Ext == "ii")
318 return langkind_cxx_cpp;
319 else if (Ext == "m")
320 return langkind_objc;
321 else if (Ext == "mi")
322 return langkind_objc_cpp;
323 else if (Ext == "mm" || Ext == "M")
324 return langkind_objcxx;
325 else if (Ext == "mii")
326 return langkind_objcxx_cpp;
327 else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
328 Ext == "c++" || Ext == "cp" || Ext == "cxx")
329 return langkind_cxx;
330 else
331 return langkind_c;
332}
333
334
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000335static void InitializeCOptions(LangOptions &Options) {
336 // Do nothing.
337}
338
339static void InitializeObjCOptions(LangOptions &Options) {
340 Options.ObjC1 = Options.ObjC2 = 1;
341}
342
343
344static bool InitializeLangOptions(LangOptions &Options, LangKind LK){
Chris Lattner4b009652007-07-25 00:24:17 +0000345 // FIXME: implement -fpreprocessed mode.
346 bool NoPreprocess = false;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000347 bool PCH = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000348
Ted Kremenek11ad8952007-12-05 23:49:08 +0000349 switch (LK) {
Chris Lattner4b009652007-07-25 00:24:17 +0000350 default: assert(0 && "Unknown language kind!");
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000351 case langkind_c_pch:
352 InitializeCOptions(Options);
353 PCH = true;
354 break;
Chris Lattnera19689a2008-10-22 17:29:21 +0000355 case langkind_asm_cpp:
356 // FALLTHROUGH
Chris Lattner4b009652007-07-25 00:24:17 +0000357 case langkind_c_cpp:
358 NoPreprocess = true;
359 // FALLTHROUGH
360 case langkind_c:
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000361 InitializeCOptions(Options);
Chris Lattner4b009652007-07-25 00:24:17 +0000362 break;
363 case langkind_cxx_cpp:
364 NoPreprocess = true;
365 // FALLTHROUGH
366 case langkind_cxx:
367 Options.CPlusPlus = 1;
368 break;
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000369 case langkind_objc_pch:
370 InitializeObjCOptions(Options);
371 PCH = true;
372 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000373 case langkind_objc_cpp:
374 NoPreprocess = true;
375 // FALLTHROUGH
376 case langkind_objc:
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000377 InitializeObjCOptions(Options);
Chris Lattner4b009652007-07-25 00:24:17 +0000378 break;
379 case langkind_objcxx_cpp:
380 NoPreprocess = true;
381 // FALLTHROUGH
382 case langkind_objcxx:
383 Options.ObjC1 = Options.ObjC2 = 1;
384 Options.CPlusPlus = 1;
385 break;
386 }
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000387
388 return PCH;
Chris Lattner4b009652007-07-25 00:24:17 +0000389}
390
391/// LangStds - Language standards we support.
392enum LangStds {
393 lang_unspecified,
394 lang_c89, lang_c94, lang_c99,
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000395 lang_gnu_START,
396 lang_gnu89 = lang_gnu_START, lang_gnu99,
Chris Lattner4b009652007-07-25 00:24:17 +0000397 lang_cxx98, lang_gnucxx98,
398 lang_cxx0x, lang_gnucxx0x
399};
400
401static llvm::cl::opt<LangStds>
402LangStd("std", llvm::cl::desc("Language standard to compile for"),
403 llvm::cl::init(lang_unspecified),
404 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
405 clEnumValN(lang_c89, "c90", "ISO C 1990"),
406 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
407 clEnumValN(lang_c94, "iso9899:199409",
408 "ISO C 1990 with amendment 1"),
409 clEnumValN(lang_c99, "c99", "ISO C 1999"),
410// clEnumValN(lang_c99, "c9x", "ISO C 1999"),
411 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
412// clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
413 clEnumValN(lang_gnu89, "gnu89",
414 "ISO C 1990 with GNU extensions (default for C)"),
415 clEnumValN(lang_gnu99, "gnu99",
416 "ISO C 1999 with GNU extensions"),
417 clEnumValN(lang_gnu99, "gnu9x",
418 "ISO C 1999 with GNU extensions"),
419 clEnumValN(lang_cxx98, "c++98",
420 "ISO C++ 1998 with amendments"),
421 clEnumValN(lang_gnucxx98, "gnu++98",
422 "ISO C++ 1998 with amendments and GNU "
423 "extensions (default for C++)"),
424 clEnumValN(lang_cxx0x, "c++0x",
425 "Upcoming ISO C++ 200x with amendments"),
426 clEnumValN(lang_gnucxx0x, "gnu++0x",
427 "Upcoming ISO C++ 200x with amendments and GNU "
428 "extensions (default for C++)"),
429 clEnumValEnd));
430
431static llvm::cl::opt<bool>
432NoOperatorNames("fno-operator-names",
433 llvm::cl::desc("Do not treat C++ operator name keywords as "
434 "synonyms for operators"));
435
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000436static llvm::cl::opt<bool>
437PascalStrings("fpascal-strings",
438 llvm::cl::desc("Recognize and construct Pascal-style "
439 "string literals"));
Steve Naroff73a07032008-02-07 03:50:06 +0000440
441static llvm::cl::opt<bool>
442MSExtensions("fms-extensions",
443 llvm::cl::desc("Accept some non-standard constructs used in "
Sanjiv Gupta69683532008-05-08 08:28:14 +0000444 "Microsoft header files "));
Chris Lattnerdb6be562007-11-28 05:34:05 +0000445
446static llvm::cl::opt<bool>
447WritableStrings("fwritable-strings",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000448 llvm::cl::desc("Store string literals as writable data"));
Anders Carlssone87cd982007-11-30 04:21:22 +0000449
450static llvm::cl::opt<bool>
451LaxVectorConversions("flax-vector-conversions",
452 llvm::cl::desc("Allow implicit conversions between vectors"
453 " with a different number of elements or "
Sanjiv Gupta69683532008-05-08 08:28:14 +0000454 "different element types"));
Ted Kremenek2658c4a2008-04-29 04:37:03 +0000455
Daniel Dunbar91692d92008-08-11 17:36:14 +0000456// FIXME: This (and all GCC -f options) really come in -f... and
457// -fno-... forms, and additionally support automagic behavior when
458// they are not defined. For example, -fexceptions defaults to on or
459// off depending on the language. We should support this behavior in
460// some form (perhaps just add a facility for distinguishing when an
461// has its default value from when it has been set to its default
462// value).
463static llvm::cl::opt<bool>
464Exceptions("fexceptions",
465 llvm::cl::desc("Enable support for exception handling."));
466
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000467static llvm::cl::opt<bool>
468GNURuntime("fgnu-runtime",
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000469 llvm::cl::desc("Generate output compatible with the standard GNU "
470 "Objective-C runtime."));
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000471
472static llvm::cl::opt<bool>
473NeXTRuntime("fnext-runtime",
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000474 llvm::cl::desc("Generate output compatible with the NeXT "
475 "runtime."));
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000476
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000477
478
479static llvm::cl::opt<bool>
480Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences."));
481
482static llvm::cl::opt<bool>
483Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89."));
484
Chris Lattner4b009652007-07-25 00:24:17 +0000485// FIXME: add:
Chris Lattner4b009652007-07-25 00:24:17 +0000486// -fdollars-in-identifiers
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000487// -fpascal-strings
Daniel Dunbar34542952008-08-23 08:43:39 +0000488static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
489 TargetInfo *Target) {
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000490
491 if (Ansi) // "The -ansi option is equivalent to -std=c89."
492 LangStd = lang_c89;
493
Chris Lattner4b009652007-07-25 00:24:17 +0000494 if (LangStd == lang_unspecified) {
495 // Based on the base language, pick one.
Ted Kremenek11ad8952007-12-05 23:49:08 +0000496 switch (LK) {
Chris Lattner4b009652007-07-25 00:24:17 +0000497 default: assert(0 && "Unknown base language");
498 case langkind_c:
Chris Lattnera19689a2008-10-22 17:29:21 +0000499 case langkind_asm_cpp:
Chris Lattner4b009652007-07-25 00:24:17 +0000500 case langkind_c_cpp:
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000501 case langkind_c_pch:
Chris Lattner4b009652007-07-25 00:24:17 +0000502 case langkind_objc:
503 case langkind_objc_cpp:
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000504 case langkind_objc_pch:
Chris Lattner4b009652007-07-25 00:24:17 +0000505 LangStd = lang_gnu99;
506 break;
507 case langkind_cxx:
508 case langkind_cxx_cpp:
509 case langkind_objcxx:
510 case langkind_objcxx_cpp:
511 LangStd = lang_gnucxx98;
512 break;
513 }
514 }
515
516 switch (LangStd) {
517 default: assert(0 && "Unknown language standard!");
518
519 // Fall through from newer standards to older ones. This isn't really right.
520 // FIXME: Enable specifically the right features based on the language stds.
521 case lang_gnucxx0x:
522 case lang_cxx0x:
523 Options.CPlusPlus0x = 1;
524 // FALL THROUGH
525 case lang_gnucxx98:
526 case lang_cxx98:
527 Options.CPlusPlus = 1;
528 Options.CXXOperatorNames = !NoOperatorNames;
Nate Begemanca893342007-11-15 07:30:50 +0000529 Options.Boolean = 1;
Chris Lattner4b009652007-07-25 00:24:17 +0000530 // FALL THROUGH.
531 case lang_gnu99:
532 case lang_c99:
Chris Lattner4b009652007-07-25 00:24:17 +0000533 Options.C99 = 1;
534 Options.HexFloats = 1;
535 // FALL THROUGH.
536 case lang_gnu89:
537 Options.BCPLComment = 1; // Only for C99/C++.
538 // FALL THROUGH.
539 case lang_c94:
Chris Lattner0297c762008-02-25 04:01:39 +0000540 Options.Digraphs = 1; // C94, C99, C++.
541 // FALL THROUGH.
Chris Lattner4b009652007-07-25 00:24:17 +0000542 case lang_c89:
543 break;
544 }
Argiris Kirtzidisb1519552008-09-11 04:21:06 +0000545
546 if (Options.CPlusPlus) {
547 Options.C99 = 0;
548 Options.HexFloats = (LangStd == lang_gnucxx98 || LangStd==lang_gnucxx0x);
549 }
Chris Lattner4b009652007-07-25 00:24:17 +0000550
Chris Lattner6ab935b2008-04-05 06:32:51 +0000551 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
552 Options.ImplicitInt = 1;
553 else
554 Options.ImplicitInt = 0;
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000555
556 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi
557 // is specified, or -std is set to a conforming mode.
558 Options.Trigraphs = LangStd < lang_gnu_START || Trigraphs ? 1 : 0;
559
Chris Lattner4b009652007-07-25 00:24:17 +0000560 Options.DollarIdents = 1; // FIXME: Really a target property.
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000561 Options.PascalStrings = PascalStrings;
Steve Naroff73a07032008-02-07 03:50:06 +0000562 Options.Microsoft = MSExtensions;
Chris Lattnerdb6be562007-11-28 05:34:05 +0000563 Options.WritableStrings = WritableStrings;
Anders Carlssone87cd982007-11-30 04:21:22 +0000564 Options.LaxVectorConversions = LaxVectorConversions;
Daniel Dunbar91692d92008-08-11 17:36:14 +0000565 Options.Exceptions = Exceptions;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000566
567 if (NeXTRuntime) {
568 Options.NeXTRuntime = 1;
569 } else if (GNURuntime) {
570 Options.NeXTRuntime = 0;
571 } else {
Daniel Dunbar34542952008-08-23 08:43:39 +0000572 Options.NeXTRuntime = Target->useNeXTRuntimeAsDefault();
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000573 }
Ted Kremenek1a05e6d2008-11-22 01:04:48 +0000574
575 if (Options.CPlusPlus)
576 Options.Blocks = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000577}
578
Ted Kremenek2658c4a2008-04-29 04:37:03 +0000579static llvm::cl::opt<bool>
580ObjCExclusiveGC("fobjc-gc-only",
581 llvm::cl::desc("Use GC exclusively for Objective-C related "
Sanjiv Gupta69683532008-05-08 08:28:14 +0000582 "memory management"));
Ted Kremenek2658c4a2008-04-29 04:37:03 +0000583
584static llvm::cl::opt<bool>
585ObjCEnableGC("fobjc-gc",
Nico Weber0e13eaa2008-08-05 23:33:20 +0000586 llvm::cl::desc("Enable Objective-C garbage collection"));
Ted Kremenek2658c4a2008-04-29 04:37:03 +0000587
588void InitializeGCMode(LangOptions &Options) {
589 if (ObjCExclusiveGC)
590 Options.setGCMode(LangOptions::GCOnly);
591 else if (ObjCEnableGC)
592 Options.setGCMode(LangOptions::HybridGC);
593}
594
595
Chris Lattner4b009652007-07-25 00:24:17 +0000596//===----------------------------------------------------------------------===//
597// Our DiagnosticClient implementation
598//===----------------------------------------------------------------------===//
599
600// FIXME: Werror should take a list of things, -Werror=foo,bar
601static llvm::cl::opt<bool>
602WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
603
604static llvm::cl::opt<bool>
Chris Lattner3a22b7c2008-05-29 15:36:45 +0000605SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings"));
606
607static llvm::cl::opt<bool>
Chris Lattner4b009652007-07-25 00:24:17 +0000608WarnOnExtensions("pedantic", llvm::cl::init(false),
609 llvm::cl::desc("Issue a warning on uses of GCC extensions"));
610
611static llvm::cl::opt<bool>
612ErrorOnExtensions("pedantic-errors",
613 llvm::cl::desc("Issue an error on uses of GCC extensions"));
614
615static llvm::cl::opt<bool>
Daniel Dunbar4dbd8572008-09-12 18:10:20 +0000616SuppressSystemWarnings("suppress-system-warnings",
Daniel Dunbar35e50942008-09-30 20:49:53 +0000617 llvm::cl::desc("Suppress warnings issued in system headers"),
Daniel Dunbar4dbd8572008-09-12 18:10:20 +0000618 llvm::cl::init(true));
619
620static llvm::cl::opt<bool>
Chris Lattner4b009652007-07-25 00:24:17 +0000621WarnUnusedMacros("Wunused_macros",
622 llvm::cl::desc("Warn for unused macros in the main translation unit"));
623
Ted Kremenek24f59fb2007-11-13 18:37:02 +0000624static llvm::cl::opt<bool>
625WarnFloatEqual("Wfloat-equal",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000626 llvm::cl::desc("Warn about equality comparisons of floating point values"));
Ted Kremenek24f59fb2007-11-13 18:37:02 +0000627
Ted Kremenek4b57bc72007-12-17 17:50:07 +0000628static llvm::cl::opt<bool>
629WarnNoFormatNonLiteral("Wno-format-nonliteral",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000630 llvm::cl::desc("Do not warn about non-literal format strings"));
Ted Kremenek4b57bc72007-12-17 17:50:07 +0000631
Chris Lattnera616ee32008-01-23 17:19:46 +0000632static llvm::cl::opt<bool>
633WarnUndefMacros("Wundef",
634 llvm::cl::desc("Warn on use of undefined macros in #if's"));
635
Chris Lattnerdea31bf2008-05-05 21:18:06 +0000636static llvm::cl::opt<bool>
Ted Kremenekc6e16692008-05-30 16:42:02 +0000637WarnImplicitFunctionDeclaration("Wimplicit-function-declaration",
638 llvm::cl::desc("Warn about uses of implicitly defined functions"));
Chris Lattnera616ee32008-01-23 17:19:46 +0000639
Steve Naroffb91afca2008-10-21 10:37:50 +0000640static llvm::cl::opt<bool>
641WarnNoStrictSelectorMatch("Wno-strict-selector-match",
642 llvm::cl::desc("Do not warn about duplicate methods that have the same size and alignment"),
643 llvm::cl::init(true));
644
Chris Lattner4b009652007-07-25 00:24:17 +0000645/// InitializeDiagnostics - Initialize the diagnostic object, based on the
646/// current command line option settings.
647static void InitializeDiagnostics(Diagnostic &Diags) {
Chris Lattner3a22b7c2008-05-29 15:36:45 +0000648 Diags.setIgnoreAllWarnings(SilenceWarnings);
Chris Lattner4b009652007-07-25 00:24:17 +0000649 Diags.setWarningsAsErrors(WarningsAsErrors);
650 Diags.setWarnOnExtensions(WarnOnExtensions);
651 Diags.setErrorOnExtensions(ErrorOnExtensions);
652
Daniel Dunbar4dbd8572008-09-12 18:10:20 +0000653 // Suppress warnings in system headers unless requested not to.
654 Diags.setSuppressSystemWarnings(SuppressSystemWarnings);
655
Chris Lattner4b009652007-07-25 00:24:17 +0000656 // Silence the "macro is not used" warning unless requested.
657 if (!WarnUnusedMacros)
658 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
Ted Kremenek24f59fb2007-11-13 18:37:02 +0000659
660 // Silence "floating point comparison" warnings unless requested.
661 if (!WarnFloatEqual)
662 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
Ted Kremenek4b57bc72007-12-17 17:50:07 +0000663
664 // Silence "format string is not a string literal" warnings if requested
665 if (WarnNoFormatNonLiteral)
Ted Kremenekf0a6ae02007-12-17 17:50:39 +0000666 Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant,
667 diag::MAP_IGNORE);
Chris Lattnera616ee32008-01-23 17:19:46 +0000668 if (!WarnUndefMacros)
669 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);
Steve Naroff606f7072008-02-11 22:40:08 +0000670
Chris Lattnerdea31bf2008-05-05 21:18:06 +0000671 if (!WarnImplicitFunctionDeclaration)
672 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
673 diag::MAP_IGNORE);
674
Steve Naroff606f7072008-02-11 22:40:08 +0000675 if (MSExtensions) // MS allows unnamed struct/union fields.
676 Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE);
Chris Lattner057a2f52008-05-04 23:52:02 +0000677
678 // If -pedantic-errors is set, turn extensions that warn by default into
679 // errors.
680 if (ErrorOnExtensions) {
681 Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large,
682 diag::MAP_ERROR);
683 Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large,
684 diag::MAP_ERROR);
685 }
Chris Lattner4b009652007-07-25 00:24:17 +0000686}
687
688//===----------------------------------------------------------------------===//
Ted Kremenek0118bb52008-02-18 21:21:23 +0000689// Analysis-specific options.
690//===----------------------------------------------------------------------===//
691
692static llvm::cl::opt<std::string>
693AnalyzeSpecificFunction("analyze-function",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000694 llvm::cl::desc("Run analysis on specific function"));
Ted Kremenek0118bb52008-02-18 21:21:23 +0000695
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000696static llvm::cl::opt<bool>
Ted Kremenekb1983ba2008-04-10 22:16:52 +0000697TrimGraph("trim-egraph",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000698 llvm::cl::desc("Only show error-related paths in the analysis graph"));
Ted Kremenek5e1e05c2008-03-07 22:58:01 +0000699
Ted Kremenek0118bb52008-02-18 21:21:23 +0000700//===----------------------------------------------------------------------===//
Ted Kremenek40499482007-12-03 22:06:55 +0000701// Target Triple Processing.
702//===----------------------------------------------------------------------===//
703
704static llvm::cl::opt<std::string>
705TargetTriple("triple",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000706 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
Ted Kremenek40499482007-12-03 22:06:55 +0000707
Chris Lattnerfc457002008-03-05 01:18:20 +0000708static llvm::cl::opt<std::string>
Sanjiv Gupta69683532008-05-08 08:28:14 +0000709Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)"));
Ted Kremenek40499482007-12-03 22:06:55 +0000710
Chris Lattner2b168e02008-09-30 01:13:12 +0000711static llvm::cl::opt<std::string>
712MacOSVersionMin("mmacosx-version-min",
713 llvm::cl::desc("Specify target Mac OS/X version (e.g. 10.5)"));
714
Chris Lattner01de9c82008-09-30 20:16:56 +0000715// If -mmacosx-version-min=10.3.9 is specified, change the triple from being
716// something like powerpc-apple-darwin9 to powerpc-apple-darwin7
717static void HandleMacOSVersionMin(std::string &Triple) {
718 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
719 if (DarwinDashIdx == std::string::npos) {
720 fprintf(stderr,
721 "-mmacosx-version-min only valid for darwin (Mac OS/X) targets\n");
722 exit(1);
723 }
724 unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin");
725
Chris Lattner01de9c82008-09-30 20:16:56 +0000726 // Remove the number.
727 Triple.resize(DarwinNumIdx);
728
729 // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9]
730 bool MacOSVersionMinIsInvalid = false;
731 int VersionNum = 0;
732 if (MacOSVersionMin.size() < 4 ||
733 MacOSVersionMin.substr(0, 3) != "10." ||
734 !isdigit(MacOSVersionMin[3])) {
735 MacOSVersionMinIsInvalid = true;
736 } else {
737 const char *Start = MacOSVersionMin.c_str()+3;
738 char *End = 0;
739 VersionNum = (int)strtol(Start, &End, 10);
740
Chris Lattnerd376f6d2008-09-30 20:30:12 +0000741 // The version number must be in the range 0-9.
742 MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9;
743
Chris Lattner01de9c82008-09-30 20:16:56 +0000744 // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7.
745 Triple += llvm::itostr(VersionNum+4);
746
Chris Lattnerd376f6d2008-09-30 20:30:12 +0000747 if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok.
748 // Add the period piece (.7) to the end of the triple. This gives us
749 // something like ...-darwin8.7
Chris Lattner01de9c82008-09-30 20:16:56 +0000750 Triple += End;
Chris Lattner01de9c82008-09-30 20:16:56 +0000751 } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not.
752 MacOSVersionMinIsInvalid = true;
753 }
754 }
755
756 if (MacOSVersionMinIsInvalid) {
757 fprintf(stderr,
758 "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n",
759 MacOSVersionMin.c_str());
760 exit(1);
761 }
762}
763
764/// CreateTargetTriple - Process the various options that affect the target
765/// triple and build a final aggregate triple that we are compiling for.
Chris Lattnerf3d79c32008-03-09 01:35:13 +0000766static std::string CreateTargetTriple() {
Ted Kremenek40499482007-12-03 22:06:55 +0000767 // Initialize base triple. If a -triple option has been specified, use
768 // that triple. Otherwise, default to the host triple.
Chris Lattner210c0cc2007-12-12 05:01:48 +0000769 std::string Triple = TargetTriple;
Daniel Dunbarabe2e542008-10-02 01:21:33 +0000770 if (Triple.empty()) {
771 Triple = LLVM_HOSTTRIPLE;
772
773 // On darwin, we want to update the version to match that of the
774 // host.
775 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
776 if (DarwinDashIdx != std::string::npos) {
777 Triple.resize(DarwinDashIdx + strlen("-darwin"));
778
779 Triple += llvm::sys::osVersion();
780 }
781 }
Ted Kremenek40499482007-12-03 22:06:55 +0000782
Chris Lattnerf3d79c32008-03-09 01:35:13 +0000783 // If -arch foo was specified, remove the architecture from the triple we have
784 // so far and replace it with the specified one.
Chris Lattner2b168e02008-09-30 01:13:12 +0000785 if (!Arch.empty()) {
786 // Decompose the base triple into "arch" and suffix.
787 std::string::size_type FirstDashIdx = Triple.find('-');
Chris Lattnerf3d79c32008-03-09 01:35:13 +0000788
Chris Lattner2b168e02008-09-30 01:13:12 +0000789 if (FirstDashIdx == std::string::npos) {
790 fprintf(stderr,
791 "Malformed target triple: \"%s\" ('-' could not be found).\n",
792 Triple.c_str());
793 exit(1);
794 }
Ted Kremenek40499482007-12-03 22:06:55 +0000795
Chris Lattner2b168e02008-09-30 01:13:12 +0000796 Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end());
797 }
798
799 // If -mmacosx-version-min=10.3.9 is specified, change the triple from being
800 // something like powerpc-apple-darwin9 to powerpc-apple-darwin7
Chris Lattner01de9c82008-09-30 20:16:56 +0000801 if (!MacOSVersionMin.empty())
802 HandleMacOSVersionMin(Triple);
Ted Kremenek40499482007-12-03 22:06:55 +0000803
Chris Lattner2b168e02008-09-30 01:13:12 +0000804 return Triple;
Ted Kremenek40499482007-12-03 22:06:55 +0000805}
806
807//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000808// Preprocessor Initialization
809//===----------------------------------------------------------------------===//
810
811// FIXME: Preprocessor builtins to support.
812// -A... - Play with #assertions
813// -undef - Undefine all predefined macros
814
815static llvm::cl::list<std::string>
816D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
817 llvm::cl::desc("Predefine the specified macro"));
818static llvm::cl::list<std::string>
819U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
820 llvm::cl::desc("Undefine the specified macro"));
821
Chris Lattner008da782008-01-10 01:53:41 +0000822static llvm::cl::list<std::string>
823ImplicitIncludes("include", llvm::cl::value_desc("file"),
824 llvm::cl::desc("Include file before parsing"));
825
826
Chris Lattner4b009652007-07-25 00:24:17 +0000827// Append a #define line to Buf for Macro. Macro should be of the form XXX,
828// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
829// "#define XXX Y z W". To get a #define with no value, use "XXX=".
830static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
831 const char *Command = "#define ") {
832 Buf.insert(Buf.end(), Command, Command+strlen(Command));
833 if (const char *Equal = strchr(Macro, '=')) {
834 // Turn the = into ' '.
835 Buf.insert(Buf.end(), Macro, Equal);
836 Buf.push_back(' ');
837 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
838 } else {
839 // Push "macroname 1".
840 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
841 Buf.push_back(' ');
842 Buf.push_back('1');
843 }
844 Buf.push_back('\n');
845}
846
Chris Lattner008da782008-01-10 01:53:41 +0000847/// AddImplicitInclude - Add an implicit #include of the specified file to the
848/// predefines buffer.
849static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){
850 const char *Inc = "#include \"";
851 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
852 Buf.insert(Buf.end(), File.begin(), File.end());
853 Buf.push_back('"');
854 Buf.push_back('\n');
855}
856
Chris Lattner4b009652007-07-25 00:24:17 +0000857
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000858/// InitializePreprocessor - Initialize the preprocessor getting it and the
Chris Lattner9d818a22008-04-19 23:25:44 +0000859/// environment ready to process a single file. This returns true on error.
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000860///
Chris Lattner9d818a22008-04-19 23:25:44 +0000861static bool InitializePreprocessor(Preprocessor &PP,
862 bool InitializeSourceMgr,
863 const std::string &InFile) {
Chris Lattner968982d2007-12-15 20:48:40 +0000864 FileManager &FileMgr = PP.getFileManager();
Chris Lattner4b009652007-07-25 00:24:17 +0000865
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000866 // Figure out where to get and map in the main file.
Chris Lattner968982d2007-12-15 20:48:40 +0000867 SourceManager &SourceMgr = PP.getSourceManager();
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000868
869 if (InitializeSourceMgr) {
870 if (InFile != "-") {
871 const FileEntry *File = FileMgr.getFile(InFile);
872 if (File) SourceMgr.createMainFileID(File, SourceLocation());
873 if (SourceMgr.getMainFileID() == 0) {
874 fprintf(stderr, "Error reading '%s'!\n",InFile.c_str());
Chris Lattner9d818a22008-04-19 23:25:44 +0000875 return true;
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000876 }
877 } else {
878 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
879 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
880 if (SourceMgr.getMainFileID() == 0) {
881 fprintf(stderr, "Error reading standard input! Empty?\n");
Chris Lattner9d818a22008-04-19 23:25:44 +0000882 return true;
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000883 }
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000884 }
Chris Lattner4b009652007-07-25 00:24:17 +0000885 }
Sam Bishop61a20782008-04-14 14:41:57 +0000886
Chris Lattner47b6a162008-04-19 23:09:31 +0000887 std::vector<char> PredefineBuffer;
888
Chris Lattner4b009652007-07-25 00:24:17 +0000889 // Add macros from the command line.
Sam Bishop61a20782008-04-14 14:41:57 +0000890 unsigned d = 0, D = D_macros.size();
891 unsigned u = 0, U = U_macros.size();
892 while (d < D || u < U) {
893 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
894 DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str());
895 else
896 DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef ");
897 }
898
Chris Lattner008da782008-01-10 01:53:41 +0000899 // FIXME: Read any files specified by -imacros.
900
901 // Add implicit #includes from -include.
902 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
903 AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]);
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000904
Chris Lattner47b6a162008-04-19 23:09:31 +0000905 // Null terminate PredefinedBuffer and add it.
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000906 PredefineBuffer.push_back(0);
Chris Lattner47b6a162008-04-19 23:09:31 +0000907 PP.setPredefines(&PredefineBuffer[0]);
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000908
909 // Once we've read this, we're done.
Chris Lattner9d818a22008-04-19 23:25:44 +0000910 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000911}
912
913//===----------------------------------------------------------------------===//
914// Preprocessor include path information.
915//===----------------------------------------------------------------------===//
916
917// This tool exports a large number of command line options to control how the
918// preprocessor searches for header files. At root, however, the Preprocessor
919// object takes a very simple interface: a list of directories to search for
920//
921// FIXME: -nostdinc,-nostdinc++
Chris Lattnerae3dcc02007-08-26 17:47:35 +0000922// FIXME: -imultilib
Chris Lattner4b009652007-07-25 00:24:17 +0000923//
Chris Lattner008da782008-01-10 01:53:41 +0000924// FIXME: -imacros
Chris Lattner4b009652007-07-25 00:24:17 +0000925
926static llvm::cl::opt<bool>
927nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
928
929// Various command line options. These four add directories to each chain.
930static llvm::cl::list<std::string>
931F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
932 llvm::cl::desc("Add directory to framework include search path"));
933static llvm::cl::list<std::string>
934I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
935 llvm::cl::desc("Add directory to include search path"));
936static llvm::cl::list<std::string>
937idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
938 llvm::cl::desc("Add directory to AFTER include search path"));
939static llvm::cl::list<std::string>
940iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
941 llvm::cl::desc("Add directory to QUOTE include search path"));
942static llvm::cl::list<std::string>
943isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
944 llvm::cl::desc("Add directory to SYSTEM include search path"));
945
946// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
947static llvm::cl::list<std::string>
948iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
949 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
950static llvm::cl::list<std::string>
951iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
952 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
953static llvm::cl::list<std::string>
954iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
955 llvm::cl::Prefix,
956 llvm::cl::desc("Set directory to include search path with prefix"));
957
Chris Lattnerae3dcc02007-08-26 17:47:35 +0000958static llvm::cl::opt<std::string>
959isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
960 llvm::cl::desc("Set the system root directory (usually /)"));
961
Chris Lattner4b009652007-07-25 00:24:17 +0000962// Finally, implement the code that groks the options above.
Chris Lattner4f022a72008-03-01 08:07:28 +0000963
Chris Lattner4b009652007-07-25 00:24:17 +0000964/// InitializeIncludePaths - Process the -I options and set them in the
965/// HeaderSearch object.
Nico Weber770e3882008-08-22 09:25:22 +0000966void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers,
967 FileManager &FM, const LangOptions &Lang) {
968 InitHeaderSearch Init(Headers, Verbose, isysroot);
969
Ted Kremenekb4d41e12008-05-31 00:27:00 +0000970 // Handle -I... and -F... options, walking the lists in parallel.
971 unsigned Iidx = 0, Fidx = 0;
972 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
973 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
Nico Weber770e3882008-08-22 09:25:22 +0000974 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekb4d41e12008-05-31 00:27:00 +0000975 ++Iidx;
976 } else {
Nico Weber770e3882008-08-22 09:25:22 +0000977 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Ted Kremenekb4d41e12008-05-31 00:27:00 +0000978 ++Fidx;
979 }
980 }
Chris Lattner4b009652007-07-25 00:24:17 +0000981
Ted Kremenekb4d41e12008-05-31 00:27:00 +0000982 // Consume what's left from whatever list was longer.
983 for (; Iidx != I_dirs.size(); ++Iidx)
Nico Weber770e3882008-08-22 09:25:22 +0000984 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekb4d41e12008-05-31 00:27:00 +0000985 for (; Fidx != F_dirs.size(); ++Fidx)
Nico Weber770e3882008-08-22 09:25:22 +0000986 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000987
988 // Handle -idirafter... options.
989 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
Nico Weber770e3882008-08-22 09:25:22 +0000990 Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After,
991 false, true, false);
Chris Lattner4b009652007-07-25 00:24:17 +0000992
993 // Handle -iquote... options.
994 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
Nico Weber770e3882008-08-22 09:25:22 +0000995 Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false);
Chris Lattner4b009652007-07-25 00:24:17 +0000996
997 // Handle -isystem... options.
998 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
Nico Weber770e3882008-08-22 09:25:22 +0000999 Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001000
1001 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
1002 // parallel, processing the values in order of occurance to get the right
1003 // prefixes.
1004 {
1005 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
1006 unsigned iprefix_idx = 0;
1007 unsigned iwithprefix_idx = 0;
1008 unsigned iwithprefixbefore_idx = 0;
1009 bool iprefix_done = iprefix_vals.empty();
1010 bool iwithprefix_done = iwithprefix_vals.empty();
1011 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
1012 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
1013 if (!iprefix_done &&
1014 (iwithprefix_done ||
1015 iprefix_vals.getPosition(iprefix_idx) <
1016 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
1017 (iwithprefixbefore_done ||
1018 iprefix_vals.getPosition(iprefix_idx) <
1019 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
1020 Prefix = iprefix_vals[iprefix_idx];
1021 ++iprefix_idx;
1022 iprefix_done = iprefix_idx == iprefix_vals.size();
1023 } else if (!iwithprefix_done &&
1024 (iwithprefixbefore_done ||
1025 iwithprefix_vals.getPosition(iwithprefix_idx) <
1026 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
Nico Weber770e3882008-08-22 09:25:22 +00001027 Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
1028 InitHeaderSearch::System, false, false, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001029 ++iwithprefix_idx;
1030 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
1031 } else {
Nico Weber770e3882008-08-22 09:25:22 +00001032 Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
1033 InitHeaderSearch::Angled, false, false, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001034 ++iwithprefixbefore_idx;
1035 iwithprefixbefore_done =
1036 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
1037 }
1038 }
1039 }
Chris Lattner4f022a72008-03-01 08:07:28 +00001040
Nico Weber770e3882008-08-22 09:25:22 +00001041 Init.AddDefaultEnvVarPaths(Lang);
Chris Lattner4f022a72008-03-01 08:07:28 +00001042
Chris Lattner3ee4a2f2008-03-03 03:16:03 +00001043 // Add the clang headers, which are relative to the clang driver.
1044 llvm::sys::Path MainExecutablePath =
Chris Lattner716a0542008-03-03 05:57:43 +00001045 llvm::sys::Path::GetMainExecutable(Argv0,
1046 (void*)(intptr_t)InitializeIncludePaths);
Chris Lattner3ee4a2f2008-03-03 03:16:03 +00001047 if (!MainExecutablePath.isEmpty()) {
1048 MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang
1049 MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin
1050 MainExecutablePath.appendComponent("Headers"); // Get foo/Headers
Nico Weber770e3882008-08-22 09:25:22 +00001051 Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System,
1052 false, false, false);
Chris Lattner3ee4a2f2008-03-03 03:16:03 +00001053 }
1054
Nico Weber770e3882008-08-22 09:25:22 +00001055 if (!nostdinc)
1056 Init.AddDefaultSystemIncludePaths(Lang);
Chris Lattner4b009652007-07-25 00:24:17 +00001057
1058 // Now that we have collected all of the include paths, merge them all
1059 // together and tell the preprocessor about them.
1060
Nico Weber770e3882008-08-22 09:25:22 +00001061 Init.Realize();
Chris Lattner4b009652007-07-25 00:24:17 +00001062}
1063
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001064//===----------------------------------------------------------------------===//
1065// Driver PreprocessorFactory - For lazily generating preprocessors ...
1066//===----------------------------------------------------------------------===//
1067
1068namespace {
1069class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory {
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001070 const std::string &InFile;
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001071 Diagnostic &Diags;
1072 const LangOptions &LangInfo;
1073 TargetInfo &Target;
1074 SourceManager &SourceMgr;
1075 HeaderSearch &HeaderInfo;
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001076 bool InitializeSourceMgr;
1077
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001078public:
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001079 DriverPreprocessorFactory(const std::string &infile,
1080 Diagnostic &diags, const LangOptions &opts,
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001081 TargetInfo &target, SourceManager &SM,
1082 HeaderSearch &Headers)
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001083 : InFile(infile), Diags(diags), LangInfo(opts), Target(target),
1084 SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {}
1085
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001086
1087 virtual ~DriverPreprocessorFactory() {}
1088
1089 virtual Preprocessor* CreatePreprocessor() {
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001090 Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target,
1091 SourceMgr, HeaderInfo);
1092
Chris Lattner9d818a22008-04-19 23:25:44 +00001093 if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) {
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001094 delete PP;
1095 return NULL;
1096 }
1097
Daniel Dunbar35fe5de2008-10-24 22:12:41 +00001098 /// FIXME: PP can only handle one callback
1099 if (ProgAction != PrintPreprocessedInput) {
1100 const char* ErrStr;
1101 bool DFG = CreateDependencyFileGen(PP, OutputFile, InFile, ErrStr);
1102 if (!DFG && ErrStr) {
Ted Kremenekdf349f32008-10-25 20:19:34 +00001103 fprintf(stderr, "%s", ErrStr);
Daniel Dunbar35fe5de2008-10-24 22:12:41 +00001104 return NULL;
1105 }
1106 }
1107
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001108 InitializeSourceMgr = false;
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001109 return PP;
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001110 }
1111};
1112}
Chris Lattner4b009652007-07-25 00:24:17 +00001113
Chris Lattner4b009652007-07-25 00:24:17 +00001114//===----------------------------------------------------------------------===//
1115// Basic Parser driver
1116//===----------------------------------------------------------------------===//
1117
Chris Lattner9d818a22008-04-19 23:25:44 +00001118static void ParseFile(Preprocessor &PP, MinimalAction *PA) {
Chris Lattner4b009652007-07-25 00:24:17 +00001119 Parser P(PP, *PA);
Ted Kremenek17861c52007-12-19 22:51:13 +00001120 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +00001121
1122 // Parsing the specified input file.
1123 P.ParseTranslationUnit();
1124 delete PA;
1125}
1126
1127//===----------------------------------------------------------------------===//
Daniel Dunbaraa7a0662008-10-23 05:50:47 +00001128// Code generation options
1129//===----------------------------------------------------------------------===//
1130
1131static llvm::cl::opt<bool>
1132OptSize("Os",
1133 llvm::cl::desc("Optimize for size"));
1134
1135// It might be nice to add bounds to the CommandLine library directly.
1136struct OptLevelParser : public llvm::cl::parser<unsigned> {
1137 bool parse(llvm::cl::Option &O, const char *ArgName,
1138 const std::string &Arg, unsigned &Val) {
1139 if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
1140 return true;
1141 // FIXME: Support -O4.
1142 if (Val > 3)
1143 return O.error(": '" + Arg + "' invalid optimization level!");
1144 return false;
1145 }
1146};
1147static llvm::cl::opt<unsigned, false, OptLevelParser>
1148OptLevel("O", llvm::cl::Prefix,
1149 llvm::cl::desc("Optimization level"),
1150 llvm::cl::init(0));
1151
1152static void InitializeCompileOptions(CompileOptions &Opts) {
Daniel Dunbaraa7a0662008-10-23 05:50:47 +00001153 Opts.OptimizeSize = OptSize;
Daniel Dunbard725b162008-10-29 07:56:11 +00001154 if (OptSize) {
1155 // -Os implies -O2
1156 // FIXME: Diagnose conflicting options.
1157 Opts.OptimizationLevel = 2;
1158 } else {
1159 Opts.OptimizationLevel = OptLevel;
1160 }
Daniel Dunbar721cbf12008-10-29 03:42:18 +00001161
1162 // FIXME: There are llvm-gcc options to control these selectively.
1163 Opts.InlineFunctions = (Opts.OptimizationLevel > 1);
1164 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
1165 Opts.SimplifyLibCalls = 1;
Daniel Dunbare8d0ba72008-10-31 09:34:21 +00001166
1167#ifdef NDEBUG
1168 Opts.VerifyModule = 0;
1169#endif
Daniel Dunbaraa7a0662008-10-23 05:50:47 +00001170}
1171
1172//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00001173// Main driver
1174//===----------------------------------------------------------------------===//
1175
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001176/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
1177/// action. These consumers can operate on both ASTs that are freshly
1178/// parsed from source files as well as those deserialized from Bitcode.
Ted Kremenekd890f6a2007-12-19 22:24:34 +00001179static ASTConsumer* CreateASTConsumer(const std::string& InFile,
Ted Kremenek397de012007-12-13 00:37:31 +00001180 Diagnostic& Diag, FileManager& FileMgr,
Chris Lattner8d72ee02008-02-06 01:42:25 +00001181 const LangOptions& LangOpts,
Chris Lattner21f72d62008-04-16 06:11:58 +00001182 Preprocessor *PP,
Ted Kremenek7c65b6c2008-08-05 18:50:11 +00001183 PreprocessorFactory *PPF) {
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001184 switch (ProgAction) {
1185 default:
1186 return NULL;
1187
1188 case ASTPrint:
1189 return CreateASTPrinter();
1190
1191 case ASTDump:
1192 return CreateASTDumper();
1193
1194 case ASTView:
Ted Kremenek24612ae2008-03-18 21:19:49 +00001195 return CreateASTViewer();
1196
1197 case EmitHTML:
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001198 return CreateHTMLPrinter(OutputFile, Diag, PP, PPF);
Ted Kremeneke972d852008-07-02 18:23:21 +00001199
Ted Kremenekd9ceb3d2008-10-23 23:36:29 +00001200 case InheritanceView:
1201 return CreateInheritanceViewer(InheritanceViewCls);
1202
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001203 case TestSerialization:
Ted Kremenek842126e2008-06-04 15:55:15 +00001204 return CreateSerializationTest(Diag, FileMgr);
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001205
Daniel Dunbar85e44e22008-10-21 23:49:24 +00001206 case EmitAssembly:
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001207 case EmitLLVM:
Daniel Dunbaraa7a0662008-10-23 05:50:47 +00001208 case EmitBC: {
1209 BackendAction Act;
1210 if (ProgAction == EmitAssembly) {
1211 Act = Backend_EmitAssembly;
1212 } else if (ProgAction == EmitLLVM) {
1213 Act = Backend_EmitLL;
1214 } else {
1215 Act = Backend_EmitBC;
1216 }
1217 CompileOptions Opts;
1218 InitializeCompileOptions(Opts);
1219 return CreateBackendConsumer(Act, Diag, LangOpts, Opts,
Daniel Dunbar85e44e22008-10-21 23:49:24 +00001220 InFile, OutputFile, GenerateDebugInfo);
Daniel Dunbaraa7a0662008-10-23 05:50:47 +00001221 }
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +00001222
Ted Kremenekbde30332007-12-19 17:25:59 +00001223 case SerializeAST:
Ted Kremenek397de012007-12-13 00:37:31 +00001224 // FIXME: Allow user to tailor where the file is written.
Ted Kremenek842126e2008-06-04 15:55:15 +00001225 return CreateASTSerializer(InFile, OutputFile, Diag);
Ted Kremenek397de012007-12-13 00:37:31 +00001226
Steve Naroff44e81222008-04-14 22:03:09 +00001227 case RewriteObjC:
Chris Lattner673f2bd2008-03-22 00:08:40 +00001228 return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts);
Steve Naroff93c18352008-09-18 14:10:13 +00001229
1230 case RewriteBlocks:
1231 return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts);
Ted Kremenek81ea7992008-07-02 00:03:09 +00001232
1233 case RunAnalysis:
1234 assert (!AnalysisList.empty());
1235 return CreateAnalysisConsumer(&AnalysisList[0],
1236 &AnalysisList[0]+AnalysisList.size(),
Ted Kremeneka3f825e2008-11-03 23:18:07 +00001237 AnalysisStoreOpt, AnalysisDiagOpt,
Ted Kremenek81ea7992008-07-02 00:03:09 +00001238 Diag, PP, PPF, LangOpts,
1239 AnalyzeSpecificFunction,
Ted Kremenekcf262252008-08-27 22:31:43 +00001240 OutputFile, VisualizeEGDot, VisualizeEGUbi,
1241 TrimGraph, AnalyzeAll);
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001242 }
1243}
1244
Chris Lattner4b009652007-07-25 00:24:17 +00001245/// ProcessInputFile - Process a single input file with the specified state.
1246///
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001247static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001248 const std::string &InFile, ProgActions PA) {
Ted Kremenek50aab982008-08-08 02:46:37 +00001249 llvm::OwningPtr<ASTConsumer> Consumer;
Chris Lattner4b009652007-07-25 00:24:17 +00001250 bool ClearSourceMgr = false;
Ted Kremenek6856c632007-09-26 18:39:29 +00001251
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001252 switch (PA) {
Chris Lattner4b009652007-07-25 00:24:17 +00001253 default:
Ted Kremenek50aab982008-08-08 02:46:37 +00001254 Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(),
1255 PP.getFileManager(), PP.getLangOptions(),
1256 &PP, &PPF));
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001257
1258 if (!Consumer) {
1259 fprintf(stderr, "Unexpected program action!\n");
Daniel Dunbar70a66b12008-10-04 23:42:49 +00001260 HadErrors = true;
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001261 return;
1262 }
Ted Kremenekd890f6a2007-12-19 22:24:34 +00001263
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001264 break;
1265
Chris Lattneraf669fb2008-10-12 05:03:36 +00001266 case DumpRawTokens: {
1267 SourceManager &SM = PP.getSourceManager();
1268 std::pair<const char*,const char*> File =
1269 SM.getBufferData(SM.getMainFileID());
1270 // Start lexing the specified input file.
1271 Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0),
1272 PP.getLangOptions(), File.first, File.second);
1273 RawLex.SetKeepWhitespaceMode(true);
1274
1275 Token RawTok;
1276
1277 RawLex.LexFromRawLexer(RawTok);
1278 while (RawTok.isNot(tok::eof)) {
1279 PP.DumpToken(RawTok, true);
1280 fprintf(stderr, "\n");
1281 RawLex.LexFromRawLexer(RawTok);
1282 }
1283 ClearSourceMgr = true;
1284 break;
1285 }
Chris Lattner4b009652007-07-25 00:24:17 +00001286 case DumpTokens: { // Token dump mode.
1287 Token Tok;
Chris Lattneraf669fb2008-10-12 05:03:36 +00001288 // Start preprocessing the specified input file.
Ted Kremenek17861c52007-12-19 22:51:13 +00001289 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +00001290 do {
1291 PP.Lex(Tok);
1292 PP.DumpToken(Tok, true);
1293 fprintf(stderr, "\n");
Chris Lattner3b494152007-10-09 18:03:42 +00001294 } while (Tok.isNot(tok::eof));
Chris Lattner4b009652007-07-25 00:24:17 +00001295 ClearSourceMgr = true;
1296 break;
1297 }
1298 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
1299 Token Tok;
1300 // Start parsing the specified input file.
Ted Kremenek17861c52007-12-19 22:51:13 +00001301 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +00001302 do {
1303 PP.Lex(Tok);
Chris Lattner3b494152007-10-09 18:03:42 +00001304 } while (Tok.isNot(tok::eof));
Chris Lattner4b009652007-07-25 00:24:17 +00001305 ClearSourceMgr = true;
1306 break;
1307 }
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001308
1309 case GeneratePCH: {
1310 CacheTokens(PP, OutputFile);
1311 ClearSourceMgr = true;
1312 break;
1313 }
Chris Lattner4b009652007-07-25 00:24:17 +00001314
1315 case PrintPreprocessedInput: // -E mode.
Chris Lattnerefd02a32008-01-27 23:55:11 +00001316 DoPrintPreprocessedInput(PP, OutputFile);
Chris Lattner4b009652007-07-25 00:24:17 +00001317 ClearSourceMgr = true;
1318 break;
Chris Lattner1665a9f2008-05-08 06:52:13 +00001319
Chris Lattner4b009652007-07-25 00:24:17 +00001320 case ParseNoop: // -parse-noop
Daniel Dunbar747a95e2008-10-31 08:56:51 +00001321 ParseFile(PP, new MinimalAction(PP));
Chris Lattner4b009652007-07-25 00:24:17 +00001322 ClearSourceMgr = true;
1323 break;
1324
1325 case ParsePrintCallbacks:
Daniel Dunbar747a95e2008-10-31 08:56:51 +00001326 ParseFile(PP, CreatePrintParserActionsAction(PP));
Chris Lattner4b009652007-07-25 00:24:17 +00001327 ClearSourceMgr = true;
1328 break;
Ted Kremenek0841c702007-09-25 18:37:20 +00001329
Ted Kremenek6856c632007-09-26 18:39:29 +00001330 case ParseSyntaxOnly: // -fsyntax-only
Ted Kremenek50aab982008-08-08 02:46:37 +00001331 Consumer.reset(new ASTConsumer());
Ted Kremenek0a03ce62007-09-17 20:49:30 +00001332 break;
Chris Lattner1665a9f2008-05-08 06:52:13 +00001333
1334 case RewriteMacros:
Chris Lattner302b0622008-05-09 22:43:24 +00001335 RewriteMacrosInInput(PP, InFile, OutputFile);
Chris Lattner1665a9f2008-05-08 06:52:13 +00001336 ClearSourceMgr = true;
1337 break;
Chris Lattnerc3fbf392008-10-12 05:29:20 +00001338
1339 case RewriteTest:
1340 DoRewriteTest(PP, InFile, OutputFile);
1341 ClearSourceMgr = true;
1342 break;
Chris Lattner129758d2007-09-16 19:46:59 +00001343 }
Ted Kremenek6856c632007-09-26 18:39:29 +00001344
Daniel Dunbar849bfc62008-10-27 22:03:52 +00001345 if (Consumer)
Daniel Dunbar4efedde2008-10-16 16:54:18 +00001346 ParseAST(PP, Consumer.get(), Stats, !DisableFree);
Daniel Dunbar849bfc62008-10-27 22:03:52 +00001347
1348 if (VerifyDiagnostics)
Daniel Dunbar8d4dff52008-10-27 22:10:13 +00001349 if (CheckDiagnostics(PP))
1350 exit(1);
Chris Lattner8d72ee02008-02-06 01:42:25 +00001351
Chris Lattner4b009652007-07-25 00:24:17 +00001352 if (Stats) {
Ted Kremenekd890f6a2007-12-19 22:24:34 +00001353 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +00001354 PP.PrintStats();
1355 PP.getIdentifierTable().PrintStats();
Chris Lattner968982d2007-12-15 20:48:40 +00001356 PP.getHeaderSearchInfo().PrintStats();
Chris Lattner4b009652007-07-25 00:24:17 +00001357 if (ClearSourceMgr)
Chris Lattner968982d2007-12-15 20:48:40 +00001358 PP.getSourceManager().PrintStats();
Chris Lattner4b009652007-07-25 00:24:17 +00001359 fprintf(stderr, "\n");
1360 }
1361
1362 // For a multi-file compilation, some things are ok with nuking the source
1363 // manager tables, other require stable fileid/macroid's across multiple
1364 // files.
Chris Lattner968982d2007-12-15 20:48:40 +00001365 if (ClearSourceMgr)
1366 PP.getSourceManager().clearIDTables();
Daniel Dunbar622d6d02008-11-11 06:35:39 +00001367
1368 if (DisableFree)
1369 Consumer.take();
Chris Lattner4b009652007-07-25 00:24:17 +00001370}
1371
Ted Kremenek80d53372007-12-12 23:41:08 +00001372static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag,
1373 FileManager& FileMgr) {
1374
1375 if (VerifyDiagnostics) {
1376 fprintf(stderr, "-verify does not yet work with serialized ASTs.\n");
1377 exit (1);
1378 }
1379
1380 llvm::sys::Path Filename(InFile);
1381
1382 if (!Filename.isValid()) {
1383 fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str());
1384 exit (1);
1385 }
1386
Ted Kremenek863b01f2008-04-23 16:25:39 +00001387 llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr));
Ted Kremenek2bd42412007-12-13 18:11:11 +00001388
1389 if (!TU) {
1390 fprintf(stderr, "error: file '%s' could not be deserialized\n",
1391 InFile.c_str());
1392 exit (1);
1393 }
1394
Ted Kremenekab749372007-12-19 19:27:38 +00001395 // Observe that we use the source file name stored in the deserialized
1396 // translation unit, rather than InFile.
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +00001397 llvm::OwningPtr<ASTConsumer>
Ted Kremenek842126e2008-06-04 15:55:15 +00001398 Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(),
Ted Kremenek7c65b6c2008-08-05 18:50:11 +00001399 0, 0));
Nico Weberd2a6ac92008-08-10 19:59:06 +00001400
Ted Kremenek80d53372007-12-12 23:41:08 +00001401 if (!Consumer) {
1402 fprintf(stderr, "Unsupported program action with serialized ASTs!\n");
1403 exit (1);
1404 }
Nico Weberd2a6ac92008-08-10 19:59:06 +00001405
Ted Kremenek863b01f2008-04-23 16:25:39 +00001406 Consumer->Initialize(TU->getContext());
Nico Weberd2a6ac92008-08-10 19:59:06 +00001407
Chris Lattner8d72ee02008-02-06 01:42:25 +00001408 // FIXME: We need to inform Consumer about completed TagDecls as well.
Ted Kremenek80d53372007-12-12 23:41:08 +00001409 for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
1410 Consumer->HandleTopLevelDecl(*I);
Ted Kremenek80d53372007-12-12 23:41:08 +00001411}
1412
1413
Chris Lattner4b009652007-07-25 00:24:17 +00001414static llvm::cl::list<std::string>
1415InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1416
Ted Kremenek80d53372007-12-12 23:41:08 +00001417static bool isSerializedFile(const std::string& InFile) {
1418 if (InFile.size() < 4)
1419 return false;
1420
1421 const char* s = InFile.c_str()+InFile.size()-4;
1422
1423 return s[0] == '.' &&
1424 s[1] == 'a' &&
1425 s[2] == 's' &&
1426 s[3] == 't';
1427}
1428
Chris Lattner4b009652007-07-25 00:24:17 +00001429
1430int main(int argc, char **argv) {
Chris Lattner3ee4a2f2008-03-03 03:16:03 +00001431 llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n");
Chris Lattner4b009652007-07-25 00:24:17 +00001432 llvm::sys::PrintStackTraceOnErrorSignal();
1433
1434 // If no input was specified, read from stdin.
1435 if (InputFilenames.empty())
1436 InputFilenames.push_back("-");
Ted Kremenekb240e822007-12-11 23:28:38 +00001437
Chris Lattner4b009652007-07-25 00:24:17 +00001438 // Create a file manager object to provide access to and cache the filesystem.
1439 FileManager FileMgr;
1440
Ted Kremenekb240e822007-12-11 23:28:38 +00001441 // Create the diagnostic client for reporting errors or for
1442 // implementing -verify.
Nico Weberd2a6ac92008-08-10 19:59:06 +00001443 DiagnosticClient* TextDiagClient = 0;
Ted Kremenekfd75e312008-03-27 06:17:42 +00001444
Ted Kremenek5c341732008-08-07 17:49:57 +00001445 if (!VerifyDiagnostics) {
1446 // Print diagnostics to stderr by default.
Chris Lattner92a33532008-11-19 06:56:25 +00001447 TextDiagClient = new TextDiagnosticPrinter(llvm::errs(),
1448 !NoShowColumn,
Ted Kremenek5c341732008-08-07 17:49:57 +00001449 !NoCaretDiagnostics);
1450 } else {
1451 // When checking diagnostics, just buffer them up.
1452 TextDiagClient = new TextDiagnosticBuffer();
1453
1454 if (InputFilenames.size() != 1) {
1455 fprintf(stderr,
1456 "-verify only works on single input files for now.\n");
1457 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001458 }
1459 }
Ted Kremenek5c341732008-08-07 17:49:57 +00001460
Chris Lattner4b009652007-07-25 00:24:17 +00001461 // Configure our handling of diagnostics.
Ted Kremenek5c341732008-08-07 17:49:57 +00001462 llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient);
1463 Diagnostic Diags(DiagClient.get());
Ted Kremenekb240e822007-12-11 23:28:38 +00001464 InitializeDiagnostics(Diags);
1465
Chris Lattner45a56e02007-12-05 23:24:17 +00001466 // -I- is a deprecated GCC feature, scan for it and reject it.
1467 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
1468 if (I_dirs[i] == "-") {
Chris Lattnera1433472008-11-18 05:05:28 +00001469 Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported);
Chris Lattner45a56e02007-12-05 23:24:17 +00001470 I_dirs.erase(I_dirs.begin()+i);
1471 --i;
1472 }
1473 }
Chris Lattner2c77d852008-03-14 06:12:05 +00001474
1475 // Get information about the target being compiled for.
1476 std::string Triple = CreateTargetTriple();
Ted Kremenekec6c5252008-08-07 18:13:12 +00001477 llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple));
1478
Chris Lattner2c77d852008-03-14 06:12:05 +00001479 if (Target == 0) {
1480 fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
1481 Triple.c_str());
1482 fprintf(stderr, "Please use -triple or -arch.\n");
1483 exit(1);
1484 }
Chris Lattner45a56e02007-12-05 23:24:17 +00001485
Ted Kremenek81ea7992008-07-02 00:03:09 +00001486 // Are we invoking one or more source analyses?
1487 if (!AnalysisList.empty() && ProgAction == ParseSyntaxOnly)
1488 ProgAction = RunAnalysis;
Ted Kremenekd9ceb3d2008-10-23 23:36:29 +00001489 else if (!InheritanceViewCls.empty()) // C++ visualization?
1490 ProgAction = InheritanceView;
Ted Kremenek5c341732008-08-07 17:49:57 +00001491
Ted Kremenek2a4224a2008-06-06 22:42:39 +00001492 llvm::OwningPtr<SourceManager> SourceMgr;
1493
Chris Lattner4b009652007-07-25 00:24:17 +00001494 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenekb240e822007-12-11 23:28:38 +00001495 const std::string &InFile = InputFilenames[i];
Ted Kremenekb240e822007-12-11 23:28:38 +00001496
Ted Kremenek5c341732008-08-07 17:49:57 +00001497 if (isSerializedFile(InFile)) {
1498 Diags.setClient(TextDiagClient);
Ted Kremenek80d53372007-12-12 23:41:08 +00001499 ProcessSerializedFile(InFile,Diags,FileMgr);
Ted Kremenek5c341732008-08-07 17:49:57 +00001500 }
Ted Kremenek80d53372007-12-12 23:41:08 +00001501 else {
1502 /// Create a SourceManager object. This tracks and owns all the file
1503 /// buffers allocated to a translation unit.
Ted Kremenek2a4224a2008-06-06 22:42:39 +00001504 if (!SourceMgr)
1505 SourceMgr.reset(new SourceManager());
1506 else
1507 SourceMgr->clearIDTables();
Ted Kremenekb240e822007-12-11 23:28:38 +00001508
Ted Kremenek80d53372007-12-12 23:41:08 +00001509 // Initialize language options, inferring file types from input filenames.
1510 LangOptions LangInfo;
1511 InitializeBaseLanguage();
1512 LangKind LK = GetLanguage(InFile);
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001513 bool PCH = InitializeLangOptions(LangInfo, LK);
Daniel Dunbar34542952008-08-23 08:43:39 +00001514 InitializeLanguageStandard(LangInfo, LK, Target.get());
Ted Kremenek2658c4a2008-04-29 04:37:03 +00001515 InitializeGCMode(LangInfo);
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001516
Ted Kremenek80d53372007-12-12 23:41:08 +00001517 // Process the -I options and set them in the HeaderInfo.
1518 HeaderSearch HeaderInfo(FileMgr);
Ted Kremenek649465c2008-06-06 01:47:30 +00001519
Chris Lattner3ee4a2f2008-03-03 03:16:03 +00001520 InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo);
Ted Kremenek80d53372007-12-12 23:41:08 +00001521
Ted Kremenek80d53372007-12-12 23:41:08 +00001522 // Set up the preprocessor with these options.
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001523 DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target,
Ted Kremenek2a4224a2008-06-06 22:42:39 +00001524 *SourceMgr.get(), HeaderInfo);
Ted Kremenek80d53372007-12-12 23:41:08 +00001525
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001526 llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor());
1527
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001528 if (!PP)
Ted Kremenek2578dd02007-12-19 22:29:55 +00001529 continue;
Ted Kremenek5c341732008-08-07 17:49:57 +00001530
1531 // Create the HTMLDiagnosticsClient if we are using one. Otherwise,
1532 // always reset to using TextDiagClient.
1533 llvm::OwningPtr<DiagnosticClient> TmpClient;
Ted Kremenek2578dd02007-12-19 22:29:55 +00001534
Ted Kremenek5c341732008-08-07 17:49:57 +00001535 if (!HTMLDiag.empty()) {
1536 TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(),
1537 &PPFactory));
1538 Diags.setClient(TmpClient.get());
1539 }
1540 else
1541 Diags.setClient(TextDiagClient);
1542
1543 // Process the source file.
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001544 ProcessInputFile(*PP, PPFactory, InFile, PCH ? GeneratePCH : ProgAction);
1545
Ted Kremenek2a4224a2008-06-06 22:42:39 +00001546 HeaderInfo.ClearFileInfo();
Ted Kremenek80d53372007-12-12 23:41:08 +00001547
1548 if (Stats)
Ted Kremenek2a4224a2008-06-06 22:42:39 +00001549 SourceMgr->PrintStats();
Ted Kremenek80d53372007-12-12 23:41:08 +00001550 }
Chris Lattner4b009652007-07-25 00:24:17 +00001551 }
Chris Lattner2c77d852008-03-14 06:12:05 +00001552
Ted Kremenekec6c5252008-08-07 18:13:12 +00001553 if (unsigned NumDiagnostics = Diags.getNumDiagnostics())
Chris Lattner4b009652007-07-25 00:24:17 +00001554 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1555 (NumDiagnostics == 1 ? "" : "s"));
1556
1557 if (Stats) {
Chris Lattner4b009652007-07-25 00:24:17 +00001558 FileMgr.PrintStats();
1559 fprintf(stderr, "\n");
1560 }
1561
Daniel Dunbar8d4dff52008-10-27 22:10:13 +00001562 // If verifying diagnostics and we reached here, all is well.
1563 if (VerifyDiagnostics)
1564 return 0;
1565
Daniel Dunbarbb298c02008-10-28 00:38:08 +00001566 // Managed static deconstruction. Useful for making things like
1567 // -time-passes usable.
1568 llvm::llvm_shutdown();
1569
Daniel Dunbar70a66b12008-10-04 23:42:49 +00001570 return HadErrors || (Diags.getNumErrors() != 0);
Chris Lattner4b009652007-07-25 00:24:17 +00001571}