blob: ead873447bc50aba28858116b1a9359e1fa37694 [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:
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 Lattner97e8b6f2007-10-07 06:04:32 +000026#include "ASTConsumers.h"
Daniel Dunbar70f92432008-10-23 05:50:47 +000027#include "clang/Driver/CompileOptions.h"
Ted Kremenekad99dbf2008-11-03 22:31:48 +000028#include "clang/Driver/PathDiagnosticClients.h"
Nico Weber0fca0222008-08-22 09:25:22 +000029#include "clang/Driver/InitHeaderSearch.h"
Nico Weberfd54ebc2008-08-05 23:33:20 +000030#include "clang/Driver/TextDiagnosticBuffer.h"
31#include "clang/Driver/TextDiagnosticPrinter.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000032#include "clang/Analysis/PathDiagnostic.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000033#include "clang/AST/Decl.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000034#include "clang/AST/TranslationUnit.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000035#include "clang/CodeGen/ModuleBuilder.h"
Chris Lattnere91c1342008-02-06 00:23:21 +000036#include "clang/Sema/ParseAST.h"
Chris Lattner556beb72007-09-15 22:56:56 +000037#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerba0f25f2008-09-30 20:16:56 +000043#include "llvm/ADT/OwningPtr.h"
Chris Lattner8f3dab82007-12-15 23:20:07 +000044#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000045#include "llvm/ADT/StringExtras.h"
46#include "llvm/Config/config.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000047#include "llvm/Support/CommandLine.h"
Daniel Dunbar524b86f2008-10-28 00:38:08 +000048#include "llvm/Support/ManagedStatic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000049#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbare553a722008-10-02 01:21:33 +000050#include "llvm/System/Host.h"
Chris Lattnerdcaa0962008-03-03 03:16:03 +000051#include "llvm/System/Path.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000052#include "llvm/System/Signals.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000053using namespace clang;
54
55//===----------------------------------------------------------------------===//
56// Global options.
57//===----------------------------------------------------------------------===//
58
Daniel Dunbard3db4012008-10-16 16:54:18 +000059static bool HadErrors = false;
Daniel Dunbarb0adbba2008-10-04 23:42:49 +000060
Reid Spencer5f016e22007-07-11 17:01:13 +000061static llvm::cl::opt<bool>
62Verbose("v", llvm::cl::desc("Enable verbose output"));
63static llvm::cl::opt<bool>
Nate Begemanaabbb122007-12-30 01:38:50 +000064Stats("print-stats",
65 llvm::cl::desc("Print performance metrics and statistics"));
Daniel Dunbard3db4012008-10-16 16:54:18 +000066static llvm::cl::opt<bool>
67DisableFree("disable-free",
68 llvm::cl::desc("Disable freeing of memory on exit"),
69 llvm::cl::init(false));
Reid Spencer5f016e22007-07-11 17:01:13 +000070
71enum ProgActions {
Steve Naroffb29b4272008-04-14 22:03:09 +000072 RewriteObjC, // ObjC->C Rewriter.
Steve Naroff13188952008-09-18 14:10:13 +000073 RewriteBlocks, // ObjC->C Rewriter for Blocks.
Chris Lattnerb57e3d42008-05-08 06:52:13 +000074 RewriteMacros, // Expand macros but not #includes.
Chris Lattnerb13c5ee2008-10-12 05:29:20 +000075 RewriteTest, // Rewriter playground
Ted Kremenek13e479b2008-03-19 07:53:42 +000076 HTMLTest, // HTML displayer testing stuff.
Daniel Dunbard69bacc2008-10-21 23:49:24 +000077 EmitAssembly, // Emit a .s file.
Reid Spencer5f016e22007-07-11 17:01:13 +000078 EmitLLVM, // Emit a .ll file.
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +000079 EmitBC, // Emit a .bc file.
Ted Kremeneka1fa3a12007-12-13 00:37:31 +000080 SerializeAST, // Emit a .ast file.
Ted Kremenek6a340832008-03-18 21:19:49 +000081 EmitHTML, // Translate input source into HTML.
Chris Lattner3b427b32007-10-11 00:18:28 +000082 ASTPrint, // Parse ASTs and print them.
83 ASTDump, // Parse ASTs and dump them.
84 ASTView, // Parse ASTs and view them in Graphviz.
Ted Kremenekbfa82c42007-10-16 23:37:27 +000085 TestSerialization, // Run experimental serialization code.
Reid Spencer5f016e22007-07-11 17:01:13 +000086 ParsePrintCallbacks, // Parse and print each callback.
87 ParseSyntaxOnly, // Parse and perform semantic analysis.
88 ParseNoop, // Parse with noop callbacks.
89 RunPreprocessorOnly, // Just lex, no output.
90 PrintPreprocessedInput, // -E mode.
Chris Lattnerc106c102008-10-12 05:03:36 +000091 DumpTokens, // Dump out preprocessed tokens.
92 DumpRawTokens, // Dump out raw tokens.
Ted Kremenek85888962008-10-21 00:54:44 +000093 RunAnalysis, // Run one or more source code analyses.
Ted Kremenek7cae2f62008-10-23 23:36:29 +000094 GeneratePCH, // Generate precompiled header.
95 InheritanceView // View C++ inheritance for a specified class.
Reid Spencer5f016e22007-07-11 17:01:13 +000096};
97
98static llvm::cl::opt<ProgActions>
99ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
100 llvm::cl::init(ParseSyntaxOnly),
101 llvm::cl::values(
102 clEnumValN(RunPreprocessorOnly, "Eonly",
103 "Just run preprocessor, no output (for timings)"),
104 clEnumValN(PrintPreprocessedInput, "E",
105 "Run preprocessor, emit preprocessed file"),
Chris Lattnerc106c102008-10-12 05:03:36 +0000106 clEnumValN(DumpRawTokens, "dump-raw-tokens",
107 "Lex file in raw mode and dump raw tokens"),
108 clEnumValN(DumpTokens, "dump-tokens",
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 "Run preprocessor, dump internal rep of tokens"),
110 clEnumValN(ParseNoop, "parse-noop",
111 "Run parser with noop callbacks (for timings)"),
112 clEnumValN(ParseSyntaxOnly, "fsyntax-only",
113 "Run parser and perform semantic analysis"),
114 clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
115 "Run parser and print each callback invoked"),
Ted Kremenek6a340832008-03-18 21:19:49 +0000116 clEnumValN(EmitHTML, "emit-html",
117 "Output input source as HTML"),
Chris Lattner3b427b32007-10-11 00:18:28 +0000118 clEnumValN(ASTPrint, "ast-print",
119 "Build ASTs and then pretty-print them"),
120 clEnumValN(ASTDump, "ast-dump",
121 "Build ASTs and then debug dump them"),
Chris Lattnerea254db2007-10-11 00:37:43 +0000122 clEnumValN(ASTView, "ast-view",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000123 "Build ASTs and view them with GraphViz"),
Ted Kremenekbfa82c42007-10-16 23:37:27 +0000124 clEnumValN(TestSerialization, "test-pickling",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000125 "Run prototype serialization code"),
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000126 clEnumValN(EmitAssembly, "S",
127 "Emit native assembly code"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 clEnumValN(EmitLLVM, "emit-llvm",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000129 "Build ASTs then convert to LLVM, emit .ll file"),
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000130 clEnumValN(EmitBC, "emit-llvm-bc",
131 "Build ASTs then convert to LLVM, emit .bc file"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000132 clEnumValN(SerializeAST, "serialize",
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000133 "Build ASTs and emit .ast file"),
Chris Lattnerb13c5ee2008-10-12 05:29:20 +0000134 clEnumValN(RewriteTest, "rewrite-test",
135 "Rewriter playground"),
Steve Naroffb29b4272008-04-14 22:03:09 +0000136 clEnumValN(RewriteObjC, "rewrite-objc",
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000137 "Rewrite ObjC into C (code rewriter example)"),
138 clEnumValN(RewriteMacros, "rewrite-macros",
139 "Expand macros without full preprocessing"),
Steve Naroff13188952008-09-18 14:10:13 +0000140 clEnumValN(RewriteBlocks, "rewrite-blocks",
141 "Rewrite Blocks to C"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 clEnumValEnd));
143
Ted Kremenekccc76472007-12-19 19:47:59 +0000144
145static llvm::cl::opt<std::string>
146OutputFile("o",
Ted Kremenek50b56412007-12-19 19:50:41 +0000147 llvm::cl::value_desc("path"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000148 llvm::cl::desc("Specify output file (for --serialize, this is a directory)"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000149
150//===----------------------------------------------------------------------===//
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000151// Code Generator Options
152//===----------------------------------------------------------------------===//
153static llvm::cl::opt<bool>
154GenerateDebugInfo("g",
155 llvm::cl::desc("Generate source level debug information"));
156
157//===----------------------------------------------------------------------===//
Ted Kremenek55af98c2008-04-14 18:40:58 +0000158// Diagnostic Options
159//===----------------------------------------------------------------------===//
160
Ted Kremenek41193e42007-09-26 19:42:19 +0000161static llvm::cl::opt<bool>
162VerifyDiagnostics("verify",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000163 llvm::cl::desc("Verify emitted diagnostics and warnings"));
Ted Kremenek41193e42007-09-26 19:42:19 +0000164
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000165static llvm::cl::opt<std::string>
166HTMLDiag("html-diags",
167 llvm::cl::desc("Generate HTML to report diagnostics"),
168 llvm::cl::value_desc("HTML directory"));
169
Nico Weberfd54ebc2008-08-05 23:33:20 +0000170static llvm::cl::opt<bool>
171NoShowColumn("fno-show-column",
172 llvm::cl::desc("Do not include column number on diagnostics"));
173
174static llvm::cl::opt<bool>
175NoCaretDiagnostics("fno-caret-diagnostics",
176 llvm::cl::desc("Do not include source line and caret with"
177 " diagnostics"));
178
179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000181// C++ Visualization.
182//===----------------------------------------------------------------------===//
183
184static llvm::cl::opt<std::string>
185InheritanceViewCls("cxx-inheritance-view",
186 llvm::cl::value_desc("class name"),
187 llvm::cl::desc("View C++ inhertance for a specified class"));
188
189//===----------------------------------------------------------------------===//
190// Analyzer Options.
Ted Kremenek55af98c2008-04-14 18:40:58 +0000191//===----------------------------------------------------------------------===//
192
193static llvm::cl::opt<bool>
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000194VisualizeEGDot("analyzer-viz-egraph-graphviz",
195 llvm::cl::desc("Display exploded graph using GraphViz"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000196
197static llvm::cl::opt<bool>
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000198VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
199 llvm::cl::desc("Display exploded graph using Ubigraph"));
200
201static llvm::cl::opt<bool>
202AnalyzeAll("analyzer-opt-analyze-headers",
Ted Kremenek55af98c2008-04-14 18:40:58 +0000203 llvm::cl::desc("Force the static analyzer to analyze "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000204 "functions defined in header files"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000205
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000206static llvm::cl::list<Analyses>
Ted Kremenek95c7b002008-10-24 01:04:59 +0000207AnalysisList(llvm::cl::desc("SCA Checks/Analyses:"),
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000208llvm::cl::values(
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000209#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000210clEnumValN(NAME, CMDFLAG, DESC),
211#include "Analyses.def"
212clEnumValEnd));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000213
Ted Kremenek95c7b002008-10-24 01:04:59 +0000214static llvm::cl::opt<AnalysisStores>
215AnalysisStoreOpt(llvm::cl::desc("SCA Low-Level Options (Store):"),
216 llvm::cl::init(BasicStoreModel),
217 llvm::cl::values(
218#define ANALYSIS_STORE(NAME, CMDFLAG, DESC)\
219clEnumValN(NAME##Model, "analyzer-store-" CMDFLAG, DESC),
220#include "Analyses.def"
221clEnumValEnd));
222
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000223static llvm::cl::opt<AnalysisDiagClients>
224AnalysisDiagOpt(llvm::cl::desc("SCA Output Options:"),
225 llvm::cl::init(PD_HTML),
226 llvm::cl::values(
227#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN)\
228clEnumValN(PD_##NAME, "analyzer-output-" CMDFLAG, DESC),
229#include "Analyses.def"
230clEnumValEnd));
231
Ted Kremenek55af98c2008-04-14 18:40:58 +0000232//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000233// Language Options
234//===----------------------------------------------------------------------===//
235
236enum LangKind {
237 langkind_unspecified,
238 langkind_c,
239 langkind_c_cpp,
Chris Lattnera778d7d2008-10-22 17:29:21 +0000240 langkind_asm_cpp,
Ted Kremenek85888962008-10-21 00:54:44 +0000241 langkind_c_pch,
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 langkind_cxx,
243 langkind_cxx_cpp,
244 langkind_objc,
245 langkind_objc_cpp,
Ted Kremenek85888962008-10-21 00:54:44 +0000246 langkind_objc_pch,
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 langkind_objcxx,
248 langkind_objcxx_cpp
249};
250
251/* TODO: GCC also accepts:
252 c-header c++-header objective-c-header objective-c++-header
Chris Lattnera778d7d2008-10-22 17:29:21 +0000253 assembler
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 ada, f77*, ratfor (!), f95, java, treelang
255 */
256static llvm::cl::opt<LangKind>
257BaseLang("x", llvm::cl::desc("Base language to compile"),
258 llvm::cl::init(langkind_unspecified),
259 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
260 clEnumValN(langkind_cxx, "c++", "C++"),
261 clEnumValN(langkind_objc, "objective-c", "Objective C"),
262 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
263 clEnumValN(langkind_c_cpp, "c-cpp-output",
264 "Preprocessed C"),
Chris Lattnera778d7d2008-10-22 17:29:21 +0000265 clEnumValN(langkind_asm_cpp, "assembler-with-cpp",
266 "Preprocessed asm"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
Ted Kremenek85888962008-10-21 00:54:44 +0000268 "Preprocessed C++"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
270 "Preprocessed Objective C"),
271 clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output",
272 "Preprocessed Objective C++"),
Ted Kremenek85888962008-10-21 00:54:44 +0000273 clEnumValN(langkind_c_pch,"c-header",
274 "Precompiled C header"),
275 clEnumValN(langkind_objc_pch, "objective-c-header",
276 "Precompiled Objective C header"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 clEnumValEnd));
278
279static llvm::cl::opt<bool>
280LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
281 llvm::cl::Hidden);
282static llvm::cl::opt<bool>
283LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
284 llvm::cl::Hidden);
285
Ted Kremenek8904f152007-12-05 23:49:08 +0000286/// InitializeBaseLanguage - Handle the -x foo options.
287static void InitializeBaseLanguage() {
288 if (LangObjC)
289 BaseLang = langkind_objc;
290 else if (LangObjCXX)
291 BaseLang = langkind_objcxx;
292}
293
294static LangKind GetLanguage(const std::string &Filename) {
295 if (BaseLang != langkind_unspecified)
296 return BaseLang;
297
298 std::string::size_type DotPos = Filename.rfind('.');
299
300 if (DotPos == std::string::npos) {
301 BaseLang = langkind_c; // Default to C if no extension.
Chris Lattner9b2f6c42008-01-04 19:12:28 +0000302 return langkind_c;
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 }
304
Ted Kremenek8904f152007-12-05 23:49:08 +0000305 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
306 // C header: .h
307 // C++ header: .hh or .H;
308 // assembler no preprocessing: .s
309 // assembler: .S
310 if (Ext == "c")
311 return langkind_c;
Chris Lattner1b450b02008-10-28 20:33:42 +0000312 else if (Ext == "S")
Chris Lattnera778d7d2008-10-22 17:29:21 +0000313 return langkind_asm_cpp;
Ted Kremenek8904f152007-12-05 23:49:08 +0000314 else if (Ext == "i")
315 return langkind_c_cpp;
316 else if (Ext == "ii")
317 return langkind_cxx_cpp;
318 else if (Ext == "m")
319 return langkind_objc;
320 else if (Ext == "mi")
321 return langkind_objc_cpp;
322 else if (Ext == "mm" || Ext == "M")
323 return langkind_objcxx;
324 else if (Ext == "mii")
325 return langkind_objcxx_cpp;
326 else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
327 Ext == "c++" || Ext == "cp" || Ext == "cxx")
328 return langkind_cxx;
329 else
330 return langkind_c;
331}
332
333
Ted Kremenek85888962008-10-21 00:54:44 +0000334static void InitializeCOptions(LangOptions &Options) {
335 // Do nothing.
336}
337
338static void InitializeObjCOptions(LangOptions &Options) {
339 Options.ObjC1 = Options.ObjC2 = 1;
340}
341
342
343static bool InitializeLangOptions(LangOptions &Options, LangKind LK){
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 // FIXME: implement -fpreprocessed mode.
345 bool NoPreprocess = false;
Ted Kremenek85888962008-10-21 00:54:44 +0000346 bool PCH = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000347
Ted Kremenek8904f152007-12-05 23:49:08 +0000348 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 default: assert(0 && "Unknown language kind!");
Ted Kremenek85888962008-10-21 00:54:44 +0000350 case langkind_c_pch:
351 InitializeCOptions(Options);
352 PCH = true;
353 break;
Chris Lattnera778d7d2008-10-22 17:29:21 +0000354 case langkind_asm_cpp:
355 // FALLTHROUGH
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 case langkind_c_cpp:
357 NoPreprocess = true;
358 // FALLTHROUGH
359 case langkind_c:
Ted Kremenek85888962008-10-21 00:54:44 +0000360 InitializeCOptions(Options);
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 break;
362 case langkind_cxx_cpp:
363 NoPreprocess = true;
364 // FALLTHROUGH
365 case langkind_cxx:
366 Options.CPlusPlus = 1;
367 break;
Ted Kremenek85888962008-10-21 00:54:44 +0000368 case langkind_objc_pch:
369 InitializeObjCOptions(Options);
370 PCH = true;
371 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 case langkind_objc_cpp:
373 NoPreprocess = true;
374 // FALLTHROUGH
375 case langkind_objc:
Ted Kremenek85888962008-10-21 00:54:44 +0000376 InitializeObjCOptions(Options);
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 break;
378 case langkind_objcxx_cpp:
379 NoPreprocess = true;
380 // FALLTHROUGH
381 case langkind_objcxx:
382 Options.ObjC1 = Options.ObjC2 = 1;
383 Options.CPlusPlus = 1;
384 break;
385 }
Ted Kremenek85888962008-10-21 00:54:44 +0000386
387 return PCH;
Reid Spencer5f016e22007-07-11 17:01:13 +0000388}
389
390/// LangStds - Language standards we support.
391enum LangStds {
392 lang_unspecified,
393 lang_c89, lang_c94, lang_c99,
Ted Kremenekea644d82008-09-03 21:22:16 +0000394 lang_gnu_START,
395 lang_gnu89 = lang_gnu_START, lang_gnu99,
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000396 lang_cxx98, lang_gnucxx98,
397 lang_cxx0x, lang_gnucxx0x
Reid Spencer5f016e22007-07-11 17:01:13 +0000398};
399
400static llvm::cl::opt<LangStds>
401LangStd("std", llvm::cl::desc("Language standard to compile for"),
402 llvm::cl::init(lang_unspecified),
403 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
404 clEnumValN(lang_c89, "c90", "ISO C 1990"),
405 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
406 clEnumValN(lang_c94, "iso9899:199409",
407 "ISO C 1990 with amendment 1"),
408 clEnumValN(lang_c99, "c99", "ISO C 1999"),
409// clEnumValN(lang_c99, "c9x", "ISO C 1999"),
410 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
411// clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
412 clEnumValN(lang_gnu89, "gnu89",
413 "ISO C 1990 with GNU extensions (default for C)"),
414 clEnumValN(lang_gnu99, "gnu99",
415 "ISO C 1999 with GNU extensions"),
416 clEnumValN(lang_gnu99, "gnu9x",
417 "ISO C 1999 with GNU extensions"),
418 clEnumValN(lang_cxx98, "c++98",
419 "ISO C++ 1998 with amendments"),
420 clEnumValN(lang_gnucxx98, "gnu++98",
421 "ISO C++ 1998 with amendments and GNU "
422 "extensions (default for C++)"),
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000423 clEnumValN(lang_cxx0x, "c++0x",
424 "Upcoming ISO C++ 200x with amendments"),
425 clEnumValN(lang_gnucxx0x, "gnu++0x",
426 "Upcoming ISO C++ 200x with amendments and GNU "
427 "extensions (default for C++)"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 clEnumValEnd));
429
430static llvm::cl::opt<bool>
431NoOperatorNames("fno-operator-names",
432 llvm::cl::desc("Do not treat C++ operator name keywords as "
433 "synonyms for operators"));
434
Anders Carlssonee98ac52007-10-15 02:50:23 +0000435static llvm::cl::opt<bool>
436PascalStrings("fpascal-strings",
437 llvm::cl::desc("Recognize and construct Pascal-style "
438 "string literals"));
Steve Naroffd62701b2008-02-07 03:50:06 +0000439
440static llvm::cl::opt<bool>
441MSExtensions("fms-extensions",
442 llvm::cl::desc("Accept some non-standard constructs used in "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000443 "Microsoft header files "));
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000444
445static llvm::cl::opt<bool>
446WritableStrings("fwritable-strings",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000447 llvm::cl::desc("Store string literals as writable data"));
Anders Carlsson695dbb62007-11-30 04:21:22 +0000448
449static llvm::cl::opt<bool>
450LaxVectorConversions("flax-vector-conversions",
451 llvm::cl::desc("Allow implicit conversions between vectors"
452 " with a different number of elements or "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000453 "different element types"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000454
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000455// FIXME: This (and all GCC -f options) really come in -f... and
456// -fno-... forms, and additionally support automagic behavior when
457// they are not defined. For example, -fexceptions defaults to on or
458// off depending on the language. We should support this behavior in
459// some form (perhaps just add a facility for distinguishing when an
460// has its default value from when it has been set to its default
461// value).
462static llvm::cl::opt<bool>
463Exceptions("fexceptions",
464 llvm::cl::desc("Enable support for exception handling."));
465
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000466static llvm::cl::opt<bool>
467GNURuntime("fgnu-runtime",
Ted Kremenek85888962008-10-21 00:54:44 +0000468 llvm::cl::desc("Generate output compatible with the standard GNU "
469 "Objective-C runtime."));
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000470
471static llvm::cl::opt<bool>
472NeXTRuntime("fnext-runtime",
Ted Kremenek85888962008-10-21 00:54:44 +0000473 llvm::cl::desc("Generate output compatible with the NeXT "
474 "runtime."));
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000475
Ted Kremenekea644d82008-09-03 21:22:16 +0000476
477
478static llvm::cl::opt<bool>
479Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences."));
480
481static llvm::cl::opt<bool>
482Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89."));
483
Reid Spencer5f016e22007-07-11 17:01:13 +0000484// FIXME: add:
Reid Spencer5f016e22007-07-11 17:01:13 +0000485// -fdollars-in-identifiers
Anders Carlssonee98ac52007-10-15 02:50:23 +0000486// -fpascal-strings
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +0000487static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
488 TargetInfo *Target) {
Ted Kremenekea644d82008-09-03 21:22:16 +0000489
490 if (Ansi) // "The -ansi option is equivalent to -std=c89."
491 LangStd = lang_c89;
492
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 if (LangStd == lang_unspecified) {
494 // Based on the base language, pick one.
Ted Kremenek8904f152007-12-05 23:49:08 +0000495 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 default: assert(0 && "Unknown base language");
497 case langkind_c:
Chris Lattnera778d7d2008-10-22 17:29:21 +0000498 case langkind_asm_cpp:
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 case langkind_c_cpp:
Ted Kremenek85888962008-10-21 00:54:44 +0000500 case langkind_c_pch:
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 case langkind_objc:
502 case langkind_objc_cpp:
Ted Kremenek85888962008-10-21 00:54:44 +0000503 case langkind_objc_pch:
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 LangStd = lang_gnu99;
505 break;
506 case langkind_cxx:
507 case langkind_cxx_cpp:
508 case langkind_objcxx:
509 case langkind_objcxx_cpp:
510 LangStd = lang_gnucxx98;
511 break;
512 }
513 }
514
515 switch (LangStd) {
516 default: assert(0 && "Unknown language standard!");
517
518 // Fall through from newer standards to older ones. This isn't really right.
519 // FIXME: Enable specifically the right features based on the language stds.
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000520 case lang_gnucxx0x:
521 case lang_cxx0x:
522 Options.CPlusPlus0x = 1;
523 // FALL THROUGH
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 case lang_gnucxx98:
525 case lang_cxx98:
526 Options.CPlusPlus = 1;
527 Options.CXXOperatorNames = !NoOperatorNames;
Nate Begeman8aebcb72007-11-15 07:30:50 +0000528 Options.Boolean = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 // FALL THROUGH.
530 case lang_gnu99:
531 case lang_c99:
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 Options.C99 = 1;
533 Options.HexFloats = 1;
534 // FALL THROUGH.
535 case lang_gnu89:
536 Options.BCPLComment = 1; // Only for C99/C++.
537 // FALL THROUGH.
538 case lang_c94:
Chris Lattner3426b9b2008-02-25 04:01:39 +0000539 Options.Digraphs = 1; // C94, C99, C++.
540 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 case lang_c89:
542 break;
543 }
Argyrios Kyrtzidisd1465522008-09-11 04:21:06 +0000544
545 if (Options.CPlusPlus) {
546 Options.C99 = 0;
547 Options.HexFloats = (LangStd == lang_gnucxx98 || LangStd==lang_gnucxx0x);
548 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000549
Chris Lattnerd658b562008-04-05 06:32:51 +0000550 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
551 Options.ImplicitInt = 1;
552 else
553 Options.ImplicitInt = 0;
Ted Kremenekea644d82008-09-03 21:22:16 +0000554
555 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi
556 // is specified, or -std is set to a conforming mode.
557 Options.Trigraphs = LangStd < lang_gnu_START || Trigraphs ? 1 : 0;
558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 Options.DollarIdents = 1; // FIXME: Really a target property.
Anders Carlssonee98ac52007-10-15 02:50:23 +0000560 Options.PascalStrings = PascalStrings;
Steve Naroffd62701b2008-02-07 03:50:06 +0000561 Options.Microsoft = MSExtensions;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000562 Options.WritableStrings = WritableStrings;
Anders Carlsson695dbb62007-11-30 04:21:22 +0000563 Options.LaxVectorConversions = LaxVectorConversions;
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000564 Options.Exceptions = Exceptions;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000565
566 if (NeXTRuntime) {
567 Options.NeXTRuntime = 1;
568 } else if (GNURuntime) {
569 Options.NeXTRuntime = 0;
570 } else {
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +0000571 Options.NeXTRuntime = Target->useNeXTRuntimeAsDefault();
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000572 }
Daniel Dunbar0b0ee502008-10-10 00:20:52 +0000573
574 if (Options.CPlusPlus)
575 Options.Blocks = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000576}
577
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000578static llvm::cl::opt<bool>
579ObjCExclusiveGC("fobjc-gc-only",
580 llvm::cl::desc("Use GC exclusively for Objective-C related "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000581 "memory management"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000582
583static llvm::cl::opt<bool>
584ObjCEnableGC("fobjc-gc",
Nico Weberfd54ebc2008-08-05 23:33:20 +0000585 llvm::cl::desc("Enable Objective-C garbage collection"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000586
587void InitializeGCMode(LangOptions &Options) {
588 if (ObjCExclusiveGC)
589 Options.setGCMode(LangOptions::GCOnly);
590 else if (ObjCEnableGC)
591 Options.setGCMode(LangOptions::HybridGC);
592}
593
594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595//===----------------------------------------------------------------------===//
596// Our DiagnosticClient implementation
597//===----------------------------------------------------------------------===//
598
599// FIXME: Werror should take a list of things, -Werror=foo,bar
600static llvm::cl::opt<bool>
601WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
602
603static llvm::cl::opt<bool>
Chris Lattner5b4681c2008-05-29 15:36:45 +0000604SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings"));
605
606static llvm::cl::opt<bool>
Reid Spencer5f016e22007-07-11 17:01:13 +0000607WarnOnExtensions("pedantic", llvm::cl::init(false),
608 llvm::cl::desc("Issue a warning on uses of GCC extensions"));
609
610static llvm::cl::opt<bool>
611ErrorOnExtensions("pedantic-errors",
612 llvm::cl::desc("Issue an error on uses of GCC extensions"));
613
614static llvm::cl::opt<bool>
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000615SuppressSystemWarnings("suppress-system-warnings",
Daniel Dunbar320a0542008-09-30 20:49:53 +0000616 llvm::cl::desc("Suppress warnings issued in system headers"),
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000617 llvm::cl::init(true));
618
619static llvm::cl::opt<bool>
Reid Spencer5f016e22007-07-11 17:01:13 +0000620WarnUnusedMacros("Wunused_macros",
621 llvm::cl::desc("Warn for unused macros in the main translation unit"));
622
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000623static llvm::cl::opt<bool>
624WarnFloatEqual("Wfloat-equal",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000625 llvm::cl::desc("Warn about equality comparisons of floating point values"));
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000626
Ted Kremenek73da5902007-12-17 17:50:07 +0000627static llvm::cl::opt<bool>
628WarnNoFormatNonLiteral("Wno-format-nonliteral",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000629 llvm::cl::desc("Do not warn about non-literal format strings"));
Ted Kremenek73da5902007-12-17 17:50:07 +0000630
Chris Lattner116a4b12008-01-23 17:19:46 +0000631static llvm::cl::opt<bool>
632WarnUndefMacros("Wundef",
633 llvm::cl::desc("Warn on use of undefined macros in #if's"));
634
Chris Lattner37d10842008-05-05 21:18:06 +0000635static llvm::cl::opt<bool>
Ted Kremenek358256d2008-05-30 16:42:02 +0000636WarnImplicitFunctionDeclaration("Wimplicit-function-declaration",
637 llvm::cl::desc("Warn about uses of implicitly defined functions"));
Chris Lattner116a4b12008-01-23 17:19:46 +0000638
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000639static llvm::cl::opt<bool>
640WarnNoStrictSelectorMatch("Wno-strict-selector-match",
641 llvm::cl::desc("Do not warn about duplicate methods that have the same size and alignment"),
642 llvm::cl::init(true));
643
Reid Spencer5f016e22007-07-11 17:01:13 +0000644/// InitializeDiagnostics - Initialize the diagnostic object, based on the
645/// current command line option settings.
646static void InitializeDiagnostics(Diagnostic &Diags) {
Chris Lattner5b4681c2008-05-29 15:36:45 +0000647 Diags.setIgnoreAllWarnings(SilenceWarnings);
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 Diags.setWarningsAsErrors(WarningsAsErrors);
649 Diags.setWarnOnExtensions(WarnOnExtensions);
650 Diags.setErrorOnExtensions(ErrorOnExtensions);
651
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000652 // Suppress warnings in system headers unless requested not to.
653 Diags.setSuppressSystemWarnings(SuppressSystemWarnings);
654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 // Silence the "macro is not used" warning unless requested.
656 if (!WarnUnusedMacros)
657 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000658
659 // Silence "floating point comparison" warnings unless requested.
660 if (!WarnFloatEqual)
661 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
Ted Kremenek73da5902007-12-17 17:50:07 +0000662
663 // Silence "format string is not a string literal" warnings if requested
664 if (WarnNoFormatNonLiteral)
Ted Kremenek7c1d3df2007-12-17 17:50:39 +0000665 Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant,
666 diag::MAP_IGNORE);
Chris Lattner116a4b12008-01-23 17:19:46 +0000667 if (!WarnUndefMacros)
668 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);
Steve Naroffe7a37302008-02-11 22:40:08 +0000669
Chris Lattner37d10842008-05-05 21:18:06 +0000670 if (!WarnImplicitFunctionDeclaration)
671 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
672 diag::MAP_IGNORE);
673
Steve Naroffe7a37302008-02-11 22:40:08 +0000674 if (MSExtensions) // MS allows unnamed struct/union fields.
675 Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE);
Chris Lattner8aedf192008-05-04 23:52:02 +0000676
677 // If -pedantic-errors is set, turn extensions that warn by default into
678 // errors.
679 if (ErrorOnExtensions) {
680 Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large,
681 diag::MAP_ERROR);
682 Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large,
683 diag::MAP_ERROR);
684 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000685}
686
687//===----------------------------------------------------------------------===//
Ted Kremenekcb330932008-02-18 21:21:23 +0000688// Analysis-specific options.
689//===----------------------------------------------------------------------===//
690
691static llvm::cl::opt<std::string>
692AnalyzeSpecificFunction("analyze-function",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000693 llvm::cl::desc("Run analysis on specific function"));
Ted Kremenekcb330932008-02-18 21:21:23 +0000694
Ted Kremenekffe0f432008-03-07 22:58:01 +0000695static llvm::cl::opt<bool>
Ted Kremenekd71ed262008-04-10 22:16:52 +0000696TrimGraph("trim-egraph",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000697 llvm::cl::desc("Only show error-related paths in the analysis graph"));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000698
Ted Kremenekcb330932008-02-18 21:21:23 +0000699//===----------------------------------------------------------------------===//
Ted Kremenekae360762007-12-03 22:06:55 +0000700// Target Triple Processing.
701//===----------------------------------------------------------------------===//
702
703static llvm::cl::opt<std::string>
704TargetTriple("triple",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000705 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000706
Chris Lattner42e67372008-03-05 01:18:20 +0000707static llvm::cl::opt<std::string>
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000708Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000709
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000710static llvm::cl::opt<std::string>
711MacOSVersionMin("mmacosx-version-min",
712 llvm::cl::desc("Specify target Mac OS/X version (e.g. 10.5)"));
713
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000714// If -mmacosx-version-min=10.3.9 is specified, change the triple from being
715// something like powerpc-apple-darwin9 to powerpc-apple-darwin7
716static void HandleMacOSVersionMin(std::string &Triple) {
717 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
718 if (DarwinDashIdx == std::string::npos) {
719 fprintf(stderr,
720 "-mmacosx-version-min only valid for darwin (Mac OS/X) targets\n");
721 exit(1);
722 }
723 unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin");
724
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000725 // Remove the number.
726 Triple.resize(DarwinNumIdx);
727
728 // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9]
729 bool MacOSVersionMinIsInvalid = false;
730 int VersionNum = 0;
731 if (MacOSVersionMin.size() < 4 ||
732 MacOSVersionMin.substr(0, 3) != "10." ||
733 !isdigit(MacOSVersionMin[3])) {
734 MacOSVersionMinIsInvalid = true;
735 } else {
736 const char *Start = MacOSVersionMin.c_str()+3;
737 char *End = 0;
738 VersionNum = (int)strtol(Start, &End, 10);
739
Chris Lattner079f2c462008-09-30 20:30:12 +0000740 // The version number must be in the range 0-9.
741 MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9;
742
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000743 // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7.
744 Triple += llvm::itostr(VersionNum+4);
745
Chris Lattner079f2c462008-09-30 20:30:12 +0000746 if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok.
747 // Add the period piece (.7) to the end of the triple. This gives us
748 // something like ...-darwin8.7
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000749 Triple += End;
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000750 } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not.
751 MacOSVersionMinIsInvalid = true;
752 }
753 }
754
755 if (MacOSVersionMinIsInvalid) {
756 fprintf(stderr,
757 "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n",
758 MacOSVersionMin.c_str());
759 exit(1);
760 }
761}
762
763/// CreateTargetTriple - Process the various options that affect the target
764/// triple and build a final aggregate triple that we are compiling for.
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000765static std::string CreateTargetTriple() {
Ted Kremenekae360762007-12-03 22:06:55 +0000766 // Initialize base triple. If a -triple option has been specified, use
767 // that triple. Otherwise, default to the host triple.
Chris Lattner6590d212007-12-12 05:01:48 +0000768 std::string Triple = TargetTriple;
Daniel Dunbare553a722008-10-02 01:21:33 +0000769 if (Triple.empty()) {
770 Triple = LLVM_HOSTTRIPLE;
771
772 // On darwin, we want to update the version to match that of the
773 // host.
774 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
775 if (DarwinDashIdx != std::string::npos) {
776 Triple.resize(DarwinDashIdx + strlen("-darwin"));
777
778 Triple += llvm::sys::osVersion();
779 }
780 }
Ted Kremenekae360762007-12-03 22:06:55 +0000781
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000782 // If -arch foo was specified, remove the architecture from the triple we have
783 // so far and replace it with the specified one.
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000784 if (!Arch.empty()) {
785 // Decompose the base triple into "arch" and suffix.
786 std::string::size_type FirstDashIdx = Triple.find('-');
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000787
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000788 if (FirstDashIdx == std::string::npos) {
789 fprintf(stderr,
790 "Malformed target triple: \"%s\" ('-' could not be found).\n",
791 Triple.c_str());
792 exit(1);
793 }
Ted Kremenekae360762007-12-03 22:06:55 +0000794
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000795 Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end());
796 }
797
798 // If -mmacosx-version-min=10.3.9 is specified, change the triple from being
799 // something like powerpc-apple-darwin9 to powerpc-apple-darwin7
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000800 if (!MacOSVersionMin.empty())
801 HandleMacOSVersionMin(Triple);
Ted Kremenekae360762007-12-03 22:06:55 +0000802
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000803 return Triple;
Ted Kremenekae360762007-12-03 22:06:55 +0000804}
805
806//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000807// Preprocessor Initialization
808//===----------------------------------------------------------------------===//
809
810// FIXME: Preprocessor builtins to support.
811// -A... - Play with #assertions
812// -undef - Undefine all predefined macros
813
814static llvm::cl::list<std::string>
815D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
816 llvm::cl::desc("Predefine the specified macro"));
817static llvm::cl::list<std::string>
818U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
819 llvm::cl::desc("Undefine the specified macro"));
820
Chris Lattner64299f82008-01-10 01:53:41 +0000821static llvm::cl::list<std::string>
822ImplicitIncludes("include", llvm::cl::value_desc("file"),
823 llvm::cl::desc("Include file before parsing"));
824
825
Reid Spencer5f016e22007-07-11 17:01:13 +0000826// Append a #define line to Buf for Macro. Macro should be of the form XXX,
827// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
828// "#define XXX Y z W". To get a #define with no value, use "XXX=".
829static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
830 const char *Command = "#define ") {
831 Buf.insert(Buf.end(), Command, Command+strlen(Command));
832 if (const char *Equal = strchr(Macro, '=')) {
833 // Turn the = into ' '.
834 Buf.insert(Buf.end(), Macro, Equal);
835 Buf.push_back(' ');
836 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
837 } else {
838 // Push "macroname 1".
839 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
840 Buf.push_back(' ');
841 Buf.push_back('1');
842 }
843 Buf.push_back('\n');
844}
845
Chris Lattner64299f82008-01-10 01:53:41 +0000846/// AddImplicitInclude - Add an implicit #include of the specified file to the
847/// predefines buffer.
848static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){
849 const char *Inc = "#include \"";
850 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
851 Buf.insert(Buf.end(), File.begin(), File.end());
852 Buf.push_back('"');
853 Buf.push_back('\n');
854}
855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856
Chris Lattner53b0dab2007-10-09 22:10:18 +0000857/// InitializePreprocessor - Initialize the preprocessor getting it and the
Chris Lattner51574ea2008-04-19 23:25:44 +0000858/// environment ready to process a single file. This returns true on error.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000859///
Chris Lattner51574ea2008-04-19 23:25:44 +0000860static bool InitializePreprocessor(Preprocessor &PP,
861 bool InitializeSourceMgr,
862 const std::string &InFile) {
Chris Lattnerdee73592007-12-15 20:48:40 +0000863 FileManager &FileMgr = PP.getFileManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000864
Chris Lattner53b0dab2007-10-09 22:10:18 +0000865 // Figure out where to get and map in the main file.
Chris Lattnerdee73592007-12-15 20:48:40 +0000866 SourceManager &SourceMgr = PP.getSourceManager();
Ted Kremenek339b9c22008-04-17 22:31:54 +0000867
868 if (InitializeSourceMgr) {
869 if (InFile != "-") {
870 const FileEntry *File = FileMgr.getFile(InFile);
871 if (File) SourceMgr.createMainFileID(File, SourceLocation());
872 if (SourceMgr.getMainFileID() == 0) {
873 fprintf(stderr, "Error reading '%s'!\n",InFile.c_str());
Chris Lattner51574ea2008-04-19 23:25:44 +0000874 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000875 }
876 } else {
877 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
878 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
879 if (SourceMgr.getMainFileID() == 0) {
880 fprintf(stderr, "Error reading standard input! Empty?\n");
Chris Lattner51574ea2008-04-19 23:25:44 +0000881 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000882 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000883 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 }
Sam Bishop1102d6b2008-04-14 14:41:57 +0000885
Chris Lattneraa391972008-04-19 23:09:31 +0000886 std::vector<char> PredefineBuffer;
887
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 // Add macros from the command line.
Sam Bishop1102d6b2008-04-14 14:41:57 +0000889 unsigned d = 0, D = D_macros.size();
890 unsigned u = 0, U = U_macros.size();
891 while (d < D || u < U) {
892 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
893 DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str());
894 else
895 DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef ");
896 }
897
Chris Lattner64299f82008-01-10 01:53:41 +0000898 // FIXME: Read any files specified by -imacros.
899
900 // Add implicit #includes from -include.
901 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
902 AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000903
Chris Lattneraa391972008-04-19 23:09:31 +0000904 // Null terminate PredefinedBuffer and add it.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000905 PredefineBuffer.push_back(0);
Chris Lattneraa391972008-04-19 23:09:31 +0000906 PP.setPredefines(&PredefineBuffer[0]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000907
908 // Once we've read this, we're done.
Chris Lattner51574ea2008-04-19 23:25:44 +0000909 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000910}
911
912//===----------------------------------------------------------------------===//
913// Preprocessor include path information.
914//===----------------------------------------------------------------------===//
915
916// This tool exports a large number of command line options to control how the
917// preprocessor searches for header files. At root, however, the Preprocessor
918// object takes a very simple interface: a list of directories to search for
919//
920// FIXME: -nostdinc,-nostdinc++
Chris Lattner0c946412007-08-26 17:47:35 +0000921// FIXME: -imultilib
Reid Spencer5f016e22007-07-11 17:01:13 +0000922//
Chris Lattner64299f82008-01-10 01:53:41 +0000923// FIXME: -imacros
Reid Spencer5f016e22007-07-11 17:01:13 +0000924
925static llvm::cl::opt<bool>
926nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
927
928// Various command line options. These four add directories to each chain.
929static llvm::cl::list<std::string>
930F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
931 llvm::cl::desc("Add directory to framework include search path"));
932static llvm::cl::list<std::string>
933I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
934 llvm::cl::desc("Add directory to include search path"));
935static llvm::cl::list<std::string>
936idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
937 llvm::cl::desc("Add directory to AFTER include search path"));
938static llvm::cl::list<std::string>
939iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
940 llvm::cl::desc("Add directory to QUOTE include search path"));
941static llvm::cl::list<std::string>
942isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
943 llvm::cl::desc("Add directory to SYSTEM include search path"));
944
945// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
946static llvm::cl::list<std::string>
947iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
948 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
949static llvm::cl::list<std::string>
950iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
951 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
952static llvm::cl::list<std::string>
953iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
954 llvm::cl::Prefix,
955 llvm::cl::desc("Set directory to include search path with prefix"));
956
Chris Lattner0c946412007-08-26 17:47:35 +0000957static llvm::cl::opt<std::string>
958isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
959 llvm::cl::desc("Set the system root directory (usually /)"));
960
Reid Spencer5f016e22007-07-11 17:01:13 +0000961// Finally, implement the code that groks the options above.
Chris Lattner5f9eae52008-03-01 08:07:28 +0000962
Reid Spencer5f016e22007-07-11 17:01:13 +0000963/// InitializeIncludePaths - Process the -I options and set them in the
964/// HeaderSearch object.
Nico Weber0fca0222008-08-22 09:25:22 +0000965void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers,
966 FileManager &FM, const LangOptions &Lang) {
967 InitHeaderSearch Init(Headers, Verbose, isysroot);
968
Ted Kremenekf3721112008-05-31 00:27:00 +0000969 // Handle -I... and -F... options, walking the lists in parallel.
970 unsigned Iidx = 0, Fidx = 0;
971 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
972 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000973 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +0000974 ++Iidx;
975 } else {
Nico Weber0fca0222008-08-22 09:25:22 +0000976 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Ted Kremenekf3721112008-05-31 00:27:00 +0000977 ++Fidx;
978 }
979 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000980
Ted Kremenekf3721112008-05-31 00:27:00 +0000981 // Consume what's left from whatever list was longer.
982 for (; Iidx != I_dirs.size(); ++Iidx)
Nico Weber0fca0222008-08-22 09:25:22 +0000983 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +0000984 for (; Fidx != F_dirs.size(); ++Fidx)
Nico Weber0fca0222008-08-22 09:25:22 +0000985 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000986
987 // Handle -idirafter... options.
988 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000989 Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After,
990 false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000991
992 // Handle -iquote... options.
993 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000994 Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000995
996 // Handle -isystem... options.
997 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000998 Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000999
1000 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
1001 // parallel, processing the values in order of occurance to get the right
1002 // prefixes.
1003 {
1004 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
1005 unsigned iprefix_idx = 0;
1006 unsigned iwithprefix_idx = 0;
1007 unsigned iwithprefixbefore_idx = 0;
1008 bool iprefix_done = iprefix_vals.empty();
1009 bool iwithprefix_done = iwithprefix_vals.empty();
1010 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
1011 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
1012 if (!iprefix_done &&
1013 (iwithprefix_done ||
1014 iprefix_vals.getPosition(iprefix_idx) <
1015 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
1016 (iwithprefixbefore_done ||
1017 iprefix_vals.getPosition(iprefix_idx) <
1018 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
1019 Prefix = iprefix_vals[iprefix_idx];
1020 ++iprefix_idx;
1021 iprefix_done = iprefix_idx == iprefix_vals.size();
1022 } else if (!iwithprefix_done &&
1023 (iwithprefixbefore_done ||
1024 iwithprefix_vals.getPosition(iwithprefix_idx) <
1025 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
Nico Weber0fca0222008-08-22 09:25:22 +00001026 Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
1027 InitHeaderSearch::System, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 ++iwithprefix_idx;
1029 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
1030 } else {
Nico Weber0fca0222008-08-22 09:25:22 +00001031 Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
1032 InitHeaderSearch::Angled, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 ++iwithprefixbefore_idx;
1034 iwithprefixbefore_done =
1035 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
1036 }
1037 }
1038 }
Chris Lattner5f9eae52008-03-01 08:07:28 +00001039
Nico Weber0fca0222008-08-22 09:25:22 +00001040 Init.AddDefaultEnvVarPaths(Lang);
Chris Lattner5f9eae52008-03-01 08:07:28 +00001041
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001042 // Add the clang headers, which are relative to the clang driver.
1043 llvm::sys::Path MainExecutablePath =
Chris Lattner985e1822008-03-03 05:57:43 +00001044 llvm::sys::Path::GetMainExecutable(Argv0,
1045 (void*)(intptr_t)InitializeIncludePaths);
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001046 if (!MainExecutablePath.isEmpty()) {
1047 MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang
1048 MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin
1049 MainExecutablePath.appendComponent("Headers"); // Get foo/Headers
Nico Weber0fca0222008-08-22 09:25:22 +00001050 Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System,
1051 false, false, false);
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001052 }
1053
Nico Weber0fca0222008-08-22 09:25:22 +00001054 if (!nostdinc)
1055 Init.AddDefaultSystemIncludePaths(Lang);
Reid Spencer5f016e22007-07-11 17:01:13 +00001056
1057 // Now that we have collected all of the include paths, merge them all
1058 // together and tell the preprocessor about them.
1059
Nico Weber0fca0222008-08-22 09:25:22 +00001060 Init.Realize();
Reid Spencer5f016e22007-07-11 17:01:13 +00001061}
1062
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001063//===----------------------------------------------------------------------===//
1064// Driver PreprocessorFactory - For lazily generating preprocessors ...
1065//===----------------------------------------------------------------------===//
1066
1067namespace {
1068class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory {
Ted Kremenek339b9c22008-04-17 22:31:54 +00001069 const std::string &InFile;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001070 Diagnostic &Diags;
1071 const LangOptions &LangInfo;
1072 TargetInfo &Target;
1073 SourceManager &SourceMgr;
1074 HeaderSearch &HeaderInfo;
Ted Kremenek339b9c22008-04-17 22:31:54 +00001075 bool InitializeSourceMgr;
1076
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001077public:
Ted Kremenek339b9c22008-04-17 22:31:54 +00001078 DriverPreprocessorFactory(const std::string &infile,
1079 Diagnostic &diags, const LangOptions &opts,
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001080 TargetInfo &target, SourceManager &SM,
1081 HeaderSearch &Headers)
Ted Kremenek339b9c22008-04-17 22:31:54 +00001082 : InFile(infile), Diags(diags), LangInfo(opts), Target(target),
1083 SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {}
1084
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001085
1086 virtual ~DriverPreprocessorFactory() {}
1087
1088 virtual Preprocessor* CreatePreprocessor() {
Ted Kremenek339b9c22008-04-17 22:31:54 +00001089 Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target,
1090 SourceMgr, HeaderInfo);
1091
Chris Lattner51574ea2008-04-19 23:25:44 +00001092 if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) {
Ted Kremenek339b9c22008-04-17 22:31:54 +00001093 delete PP;
1094 return NULL;
1095 }
1096
Daniel Dunbar750c3582008-10-24 22:12:41 +00001097 /// FIXME: PP can only handle one callback
1098 if (ProgAction != PrintPreprocessedInput) {
1099 const char* ErrStr;
1100 bool DFG = CreateDependencyFileGen(PP, OutputFile, InFile, ErrStr);
1101 if (!DFG && ErrStr) {
Ted Kremenek9a30c242008-10-25 20:19:34 +00001102 fprintf(stderr, "%s", ErrStr);
Daniel Dunbar750c3582008-10-24 22:12:41 +00001103 return NULL;
1104 }
1105 }
1106
Ted Kremenek339b9c22008-04-17 22:31:54 +00001107 InitializeSourceMgr = false;
Ted Kremenek339b9c22008-04-17 22:31:54 +00001108 return PP;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001109 }
1110};
1111}
Reid Spencer5f016e22007-07-11 17:01:13 +00001112
Reid Spencer5f016e22007-07-11 17:01:13 +00001113//===----------------------------------------------------------------------===//
1114// Basic Parser driver
1115//===----------------------------------------------------------------------===//
1116
Chris Lattner51574ea2008-04-19 23:25:44 +00001117static void ParseFile(Preprocessor &PP, MinimalAction *PA) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 Parser P(PP, *PA);
Ted Kremenek95041a22007-12-19 22:51:13 +00001119 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001120
1121 // Parsing the specified input file.
1122 P.ParseTranslationUnit();
1123 delete PA;
1124}
1125
1126//===----------------------------------------------------------------------===//
Daniel Dunbar70f92432008-10-23 05:50:47 +00001127// Code generation options
1128//===----------------------------------------------------------------------===//
1129
1130static llvm::cl::opt<bool>
1131OptSize("Os",
1132 llvm::cl::desc("Optimize for size"));
1133
1134// It might be nice to add bounds to the CommandLine library directly.
1135struct OptLevelParser : public llvm::cl::parser<unsigned> {
1136 bool parse(llvm::cl::Option &O, const char *ArgName,
1137 const std::string &Arg, unsigned &Val) {
1138 if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
1139 return true;
1140 // FIXME: Support -O4.
1141 if (Val > 3)
1142 return O.error(": '" + Arg + "' invalid optimization level!");
1143 return false;
1144 }
1145};
1146static llvm::cl::opt<unsigned, false, OptLevelParser>
1147OptLevel("O", llvm::cl::Prefix,
1148 llvm::cl::desc("Optimization level"),
1149 llvm::cl::init(0));
1150
1151static void InitializeCompileOptions(CompileOptions &Opts) {
Daniel Dunbar70f92432008-10-23 05:50:47 +00001152 Opts.OptimizeSize = OptSize;
Daniel Dunbarac7ffe02008-10-29 07:56:11 +00001153 if (OptSize) {
1154 // -Os implies -O2
1155 // FIXME: Diagnose conflicting options.
1156 Opts.OptimizationLevel = 2;
1157 } else {
1158 Opts.OptimizationLevel = OptLevel;
1159 }
Daniel Dunbar8e8f3b72008-10-29 03:42:18 +00001160
1161 // FIXME: There are llvm-gcc options to control these selectively.
1162 Opts.InlineFunctions = (Opts.OptimizationLevel > 1);
1163 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
1164 Opts.SimplifyLibCalls = 1;
Daniel Dunbardd913e52008-10-31 09:34:21 +00001165
1166#ifdef NDEBUG
1167 Opts.VerifyModule = 0;
1168#endif
Daniel Dunbar70f92432008-10-23 05:50:47 +00001169}
1170
1171//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001172// Main driver
1173//===----------------------------------------------------------------------===//
1174
Ted Kremenekdb094a22007-12-05 18:27:04 +00001175/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
1176/// action. These consumers can operate on both ASTs that are freshly
1177/// parsed from source files as well as those deserialized from Bitcode.
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001178static ASTConsumer* CreateASTConsumer(const std::string& InFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001179 Diagnostic& Diag, FileManager& FileMgr,
Chris Lattnere66b65c2008-02-06 01:42:25 +00001180 const LangOptions& LangOpts,
Chris Lattner3245a0a2008-04-16 06:11:58 +00001181 Preprocessor *PP,
Ted Kremenek815c78f2008-08-05 18:50:11 +00001182 PreprocessorFactory *PPF) {
Ted Kremenekdb094a22007-12-05 18:27:04 +00001183 switch (ProgAction) {
1184 default:
1185 return NULL;
1186
1187 case ASTPrint:
1188 return CreateASTPrinter();
1189
1190 case ASTDump:
1191 return CreateASTDumper();
1192
1193 case ASTView:
Ted Kremenek6a340832008-03-18 21:19:49 +00001194 return CreateASTViewer();
1195
1196 case EmitHTML:
Ted Kremenek339b9c22008-04-17 22:31:54 +00001197 return CreateHTMLPrinter(OutputFile, Diag, PP, PPF);
Ted Kremenek902141f2008-07-02 18:23:21 +00001198
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001199 case InheritanceView:
1200 return CreateInheritanceViewer(InheritanceViewCls);
1201
Ted Kremenekdb094a22007-12-05 18:27:04 +00001202 case TestSerialization:
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001203 return CreateSerializationTest(Diag, FileMgr);
Ted Kremenekdb094a22007-12-05 18:27:04 +00001204
Daniel Dunbard69bacc2008-10-21 23:49:24 +00001205 case EmitAssembly:
Ted Kremenekdb094a22007-12-05 18:27:04 +00001206 case EmitLLVM:
Daniel Dunbar70f92432008-10-23 05:50:47 +00001207 case EmitBC: {
1208 BackendAction Act;
1209 if (ProgAction == EmitAssembly) {
1210 Act = Backend_EmitAssembly;
1211 } else if (ProgAction == EmitLLVM) {
1212 Act = Backend_EmitLL;
1213 } else {
1214 Act = Backend_EmitBC;
1215 }
1216 CompileOptions Opts;
1217 InitializeCompileOptions(Opts);
1218 return CreateBackendConsumer(Act, Diag, LangOpts, Opts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +00001219 InFile, OutputFile, GenerateDebugInfo);
Daniel Dunbar70f92432008-10-23 05:50:47 +00001220 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +00001221
Ted Kremenek3910c7c2007-12-19 17:25:59 +00001222 case SerializeAST:
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001223 // FIXME: Allow user to tailor where the file is written.
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001224 return CreateASTSerializer(InFile, OutputFile, Diag);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001225
Steve Naroffb29b4272008-04-14 22:03:09 +00001226 case RewriteObjC:
Chris Lattnerc68ab772008-03-22 00:08:40 +00001227 return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts);
Steve Naroff13188952008-09-18 14:10:13 +00001228
1229 case RewriteBlocks:
1230 return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts);
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001231
1232 case RunAnalysis:
1233 assert (!AnalysisList.empty());
1234 return CreateAnalysisConsumer(&AnalysisList[0],
1235 &AnalysisList[0]+AnalysisList.size(),
Ted Kremenek4fc82c82008-11-03 23:18:07 +00001236 AnalysisStoreOpt, AnalysisDiagOpt,
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001237 Diag, PP, PPF, LangOpts,
1238 AnalyzeSpecificFunction,
Ted Kremenekf8ce6992008-08-27 22:31:43 +00001239 OutputFile, VisualizeEGDot, VisualizeEGUbi,
1240 TrimGraph, AnalyzeAll);
Ted Kremenekdb094a22007-12-05 18:27:04 +00001241 }
1242}
1243
Reid Spencer5f016e22007-07-11 17:01:13 +00001244/// ProcessInputFile - Process a single input file with the specified state.
1245///
Ted Kremenek339b9c22008-04-17 22:31:54 +00001246static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
Ted Kremenek85888962008-10-21 00:54:44 +00001247 const std::string &InFile, ProgActions PA) {
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001248 llvm::OwningPtr<ASTConsumer> Consumer;
Chris Lattnerbd247762007-07-22 06:05:44 +00001249 bool ClearSourceMgr = false;
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001250
Ted Kremenek85888962008-10-21 00:54:44 +00001251 switch (PA) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 default:
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001253 Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(),
1254 PP.getFileManager(), PP.getLangOptions(),
1255 &PP, &PPF));
Ted Kremenekdb094a22007-12-05 18:27:04 +00001256
1257 if (!Consumer) {
1258 fprintf(stderr, "Unexpected program action!\n");
Daniel Dunbarb0adbba2008-10-04 23:42:49 +00001259 HadErrors = true;
Ted Kremenekdb094a22007-12-05 18:27:04 +00001260 return;
1261 }
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001262
Ted Kremenekdb094a22007-12-05 18:27:04 +00001263 break;
1264
Chris Lattnerc106c102008-10-12 05:03:36 +00001265 case DumpRawTokens: {
1266 SourceManager &SM = PP.getSourceManager();
1267 std::pair<const char*,const char*> File =
1268 SM.getBufferData(SM.getMainFileID());
1269 // Start lexing the specified input file.
1270 Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0),
1271 PP.getLangOptions(), File.first, File.second);
1272 RawLex.SetKeepWhitespaceMode(true);
1273
1274 Token RawTok;
1275
1276 RawLex.LexFromRawLexer(RawTok);
1277 while (RawTok.isNot(tok::eof)) {
1278 PP.DumpToken(RawTok, true);
1279 fprintf(stderr, "\n");
1280 RawLex.LexFromRawLexer(RawTok);
1281 }
1282 ClearSourceMgr = true;
1283 break;
1284 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 case DumpTokens: { // Token dump mode.
Chris Lattnerd2177732007-07-20 16:59:19 +00001286 Token Tok;
Chris Lattnerc106c102008-10-12 05:03:36 +00001287 // Start preprocessing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001288 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 do {
1290 PP.Lex(Tok);
1291 PP.DumpToken(Tok, true);
1292 fprintf(stderr, "\n");
Chris Lattner057aaf62007-10-09 18:03:42 +00001293 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001294 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 break;
1296 }
1297 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
Chris Lattnerd2177732007-07-20 16:59:19 +00001298 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001300 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 do {
1302 PP.Lex(Tok);
Chris Lattner057aaf62007-10-09 18:03:42 +00001303 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001304 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 break;
1306 }
Ted Kremenek85888962008-10-21 00:54:44 +00001307
1308 case GeneratePCH: {
1309 CacheTokens(PP, OutputFile);
1310 ClearSourceMgr = true;
1311 break;
1312 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001313
1314 case PrintPreprocessedInput: // -E mode.
Chris Lattnere988bc22008-01-27 23:55:11 +00001315 DoPrintPreprocessedInput(PP, OutputFile);
Chris Lattnerbd247762007-07-22 06:05:44 +00001316 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001318
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 case ParseNoop: // -parse-noop
Daniel Dunbare10b0f22008-10-31 08:56:51 +00001320 ParseFile(PP, new MinimalAction(PP));
Chris Lattnerbd247762007-07-22 06:05:44 +00001321 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 break;
1323
1324 case ParsePrintCallbacks:
Daniel Dunbare10b0f22008-10-31 08:56:51 +00001325 ParseFile(PP, CreatePrintParserActionsAction(PP));
Chris Lattnerbd247762007-07-22 06:05:44 +00001326 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 break;
Ted Kremenek44579782007-09-25 18:37:20 +00001328
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001329 case ParseSyntaxOnly: // -fsyntax-only
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001330 Consumer.reset(new ASTConsumer());
Ted Kremenek2bf55142007-09-17 20:49:30 +00001331 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001332
1333 case RewriteMacros:
Chris Lattner09510522008-05-09 22:43:24 +00001334 RewriteMacrosInInput(PP, InFile, OutputFile);
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001335 ClearSourceMgr = true;
1336 break;
Chris Lattnerb13c5ee2008-10-12 05:29:20 +00001337
1338 case RewriteTest:
1339 DoRewriteTest(PP, InFile, OutputFile);
1340 ClearSourceMgr = true;
1341 break;
Chris Lattner580980b2007-09-16 19:46:59 +00001342 }
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001343
Daniel Dunbar879c3ea2008-10-27 22:03:52 +00001344 if (Consumer)
Daniel Dunbard3db4012008-10-16 16:54:18 +00001345 ParseAST(PP, Consumer.get(), Stats, !DisableFree);
Daniel Dunbar879c3ea2008-10-27 22:03:52 +00001346
1347 if (VerifyDiagnostics)
Daniel Dunbar276373d2008-10-27 22:10:13 +00001348 if (CheckDiagnostics(PP))
1349 exit(1);
Chris Lattnere66b65c2008-02-06 01:42:25 +00001350
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 if (Stats) {
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001352 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 PP.PrintStats();
1354 PP.getIdentifierTable().PrintStats();
Chris Lattnerdee73592007-12-15 20:48:40 +00001355 PP.getHeaderSearchInfo().PrintStats();
Chris Lattnerbd247762007-07-22 06:05:44 +00001356 if (ClearSourceMgr)
Chris Lattnerdee73592007-12-15 20:48:40 +00001357 PP.getSourceManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +00001358 fprintf(stderr, "\n");
1359 }
Chris Lattnerbd247762007-07-22 06:05:44 +00001360
1361 // For a multi-file compilation, some things are ok with nuking the source
1362 // manager tables, other require stable fileid/macroid's across multiple
1363 // files.
Chris Lattnerdee73592007-12-15 20:48:40 +00001364 if (ClearSourceMgr)
1365 PP.getSourceManager().clearIDTables();
Daniel Dunbard68ba0e2008-11-11 06:35:39 +00001366
1367 if (DisableFree)
1368 Consumer.take();
Reid Spencer5f016e22007-07-11 17:01:13 +00001369}
1370
Ted Kremenek20e97482007-12-12 23:41:08 +00001371static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag,
1372 FileManager& FileMgr) {
1373
1374 if (VerifyDiagnostics) {
1375 fprintf(stderr, "-verify does not yet work with serialized ASTs.\n");
1376 exit (1);
1377 }
1378
1379 llvm::sys::Path Filename(InFile);
1380
1381 if (!Filename.isValid()) {
1382 fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str());
1383 exit (1);
1384 }
1385
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001386 llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr));
Ted Kremenekfe4e0152007-12-13 18:11:11 +00001387
1388 if (!TU) {
1389 fprintf(stderr, "error: file '%s' could not be deserialized\n",
1390 InFile.c_str());
1391 exit (1);
1392 }
1393
Ted Kremenek63ea8632007-12-19 19:27:38 +00001394 // Observe that we use the source file name stored in the deserialized
1395 // translation unit, rather than InFile.
Ted Kremenekee533642007-12-20 19:47:16 +00001396 llvm::OwningPtr<ASTConsumer>
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001397 Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(),
Ted Kremenek815c78f2008-08-05 18:50:11 +00001398 0, 0));
Nico Weber7bfaaae2008-08-10 19:59:06 +00001399
Ted Kremenek20e97482007-12-12 23:41:08 +00001400 if (!Consumer) {
1401 fprintf(stderr, "Unsupported program action with serialized ASTs!\n");
1402 exit (1);
1403 }
Nico Weber7bfaaae2008-08-10 19:59:06 +00001404
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001405 Consumer->Initialize(TU->getContext());
Nico Weber7bfaaae2008-08-10 19:59:06 +00001406
Chris Lattnere66b65c2008-02-06 01:42:25 +00001407 // FIXME: We need to inform Consumer about completed TagDecls as well.
Ted Kremenek20e97482007-12-12 23:41:08 +00001408 for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
1409 Consumer->HandleTopLevelDecl(*I);
Ted Kremenek20e97482007-12-12 23:41:08 +00001410}
1411
1412
Reid Spencer5f016e22007-07-11 17:01:13 +00001413static llvm::cl::list<std::string>
1414InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1415
Ted Kremenek20e97482007-12-12 23:41:08 +00001416static bool isSerializedFile(const std::string& InFile) {
1417 if (InFile.size() < 4)
1418 return false;
1419
1420 const char* s = InFile.c_str()+InFile.size()-4;
1421
1422 return s[0] == '.' &&
1423 s[1] == 'a' &&
1424 s[2] == 's' &&
1425 s[3] == 't';
1426}
1427
Reid Spencer5f016e22007-07-11 17:01:13 +00001428
1429int main(int argc, char **argv) {
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001430 llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n");
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 llvm::sys::PrintStackTraceOnErrorSignal();
1432
1433 // If no input was specified, read from stdin.
1434 if (InputFilenames.empty())
1435 InputFilenames.push_back("-");
Ted Kremenek31e703b2007-12-11 23:28:38 +00001436
Reid Spencer5f016e22007-07-11 17:01:13 +00001437 // Create a file manager object to provide access to and cache the filesystem.
1438 FileManager FileMgr;
1439
Ted Kremenek31e703b2007-12-11 23:28:38 +00001440 // Create the diagnostic client for reporting errors or for
1441 // implementing -verify.
Nico Weber7bfaaae2008-08-10 19:59:06 +00001442 DiagnosticClient* TextDiagClient = 0;
Ted Kremenek88f5cde2008-03-27 06:17:42 +00001443
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001444 if (!VerifyDiagnostics) {
1445 // Print diagnostics to stderr by default.
Chris Lattnera03a5b52008-11-19 06:56:25 +00001446 TextDiagClient = new TextDiagnosticPrinter(llvm::errs(),
1447 !NoShowColumn,
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001448 !NoCaretDiagnostics);
1449 } else {
1450 // When checking diagnostics, just buffer them up.
1451 TextDiagClient = new TextDiagnosticBuffer();
1452
1453 if (InputFilenames.size() != 1) {
1454 fprintf(stderr,
1455 "-verify only works on single input files for now.\n");
1456 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 }
1458 }
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001459
Reid Spencer5f016e22007-07-11 17:01:13 +00001460 // Configure our handling of diagnostics.
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001461 llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient);
1462 Diagnostic Diags(DiagClient.get());
Ted Kremenek31e703b2007-12-11 23:28:38 +00001463 InitializeDiagnostics(Diags);
1464
Chris Lattner4f037832007-12-05 23:24:17 +00001465 // -I- is a deprecated GCC feature, scan for it and reject it.
1466 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
1467 if (I_dirs[i] == "-") {
Chris Lattner5917fe12008-11-18 05:05:28 +00001468 Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported);
Chris Lattner4f037832007-12-05 23:24:17 +00001469 I_dirs.erase(I_dirs.begin()+i);
1470 --i;
1471 }
1472 }
Chris Lattner11215192008-03-14 06:12:05 +00001473
1474 // Get information about the target being compiled for.
1475 std::string Triple = CreateTargetTriple();
Ted Kremenek7a08e282008-08-07 18:13:12 +00001476 llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple));
1477
Chris Lattner11215192008-03-14 06:12:05 +00001478 if (Target == 0) {
1479 fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
1480 Triple.c_str());
1481 fprintf(stderr, "Please use -triple or -arch.\n");
1482 exit(1);
1483 }
Chris Lattner4f037832007-12-05 23:24:17 +00001484
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001485 // Are we invoking one or more source analyses?
1486 if (!AnalysisList.empty() && ProgAction == ParseSyntaxOnly)
1487 ProgAction = RunAnalysis;
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001488 else if (!InheritanceViewCls.empty()) // C++ visualization?
1489 ProgAction = InheritanceView;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001490
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001491 llvm::OwningPtr<SourceManager> SourceMgr;
1492
Reid Spencer5f016e22007-07-11 17:01:13 +00001493 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenek31e703b2007-12-11 23:28:38 +00001494 const std::string &InFile = InputFilenames[i];
Ted Kremenek31e703b2007-12-11 23:28:38 +00001495
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001496 if (isSerializedFile(InFile)) {
1497 Diags.setClient(TextDiagClient);
Ted Kremenek20e97482007-12-12 23:41:08 +00001498 ProcessSerializedFile(InFile,Diags,FileMgr);
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001499 }
Ted Kremenek20e97482007-12-12 23:41:08 +00001500 else {
1501 /// Create a SourceManager object. This tracks and owns all the file
1502 /// buffers allocated to a translation unit.
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001503 if (!SourceMgr)
1504 SourceMgr.reset(new SourceManager());
1505 else
1506 SourceMgr->clearIDTables();
Ted Kremenek31e703b2007-12-11 23:28:38 +00001507
Ted Kremenek20e97482007-12-12 23:41:08 +00001508 // Initialize language options, inferring file types from input filenames.
1509 LangOptions LangInfo;
1510 InitializeBaseLanguage();
1511 LangKind LK = GetLanguage(InFile);
Ted Kremenek85888962008-10-21 00:54:44 +00001512 bool PCH = InitializeLangOptions(LangInfo, LK);
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +00001513 InitializeLanguageStandard(LangInfo, LK, Target.get());
Ted Kremenek01d9dbf2008-04-29 04:37:03 +00001514 InitializeGCMode(LangInfo);
Ted Kremenek85888962008-10-21 00:54:44 +00001515
Ted Kremenek20e97482007-12-12 23:41:08 +00001516 // Process the -I options and set them in the HeaderInfo.
1517 HeaderSearch HeaderInfo(FileMgr);
Ted Kremenekc68ecb52008-06-06 01:47:30 +00001518
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001519 InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001520
Ted Kremenek20e97482007-12-12 23:41:08 +00001521 // Set up the preprocessor with these options.
Ted Kremenek339b9c22008-04-17 22:31:54 +00001522 DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target,
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001523 *SourceMgr.get(), HeaderInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001524
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001525 llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor());
1526
Ted Kremenek339b9c22008-04-17 22:31:54 +00001527 if (!PP)
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001528 continue;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001529
1530 // Create the HTMLDiagnosticsClient if we are using one. Otherwise,
1531 // always reset to using TextDiagClient.
1532 llvm::OwningPtr<DiagnosticClient> TmpClient;
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001533
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001534 if (!HTMLDiag.empty()) {
1535 TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(),
1536 &PPFactory));
1537 Diags.setClient(TmpClient.get());
1538 }
1539 else
1540 Diags.setClient(TextDiagClient);
1541
1542 // Process the source file.
Ted Kremenek85888962008-10-21 00:54:44 +00001543 ProcessInputFile(*PP, PPFactory, InFile, PCH ? GeneratePCH : ProgAction);
1544
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001545 HeaderInfo.ClearFileInfo();
Ted Kremenek20e97482007-12-12 23:41:08 +00001546
1547 if (Stats)
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001548 SourceMgr->PrintStats();
Ted Kremenek20e97482007-12-12 23:41:08 +00001549 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001550 }
Chris Lattner11215192008-03-14 06:12:05 +00001551
Ted Kremenek7a08e282008-08-07 18:13:12 +00001552 if (unsigned NumDiagnostics = Diags.getNumDiagnostics())
Reid Spencer5f016e22007-07-11 17:01:13 +00001553 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1554 (NumDiagnostics == 1 ? "" : "s"));
1555
1556 if (Stats) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001557 FileMgr.PrintStats();
1558 fprintf(stderr, "\n");
1559 }
1560
Daniel Dunbar276373d2008-10-27 22:10:13 +00001561 // If verifying diagnostics and we reached here, all is well.
1562 if (VerifyDiagnostics)
1563 return 0;
1564
Daniel Dunbar524b86f2008-10-28 00:38:08 +00001565 // Managed static deconstruction. Useful for making things like
1566 // -time-passes usable.
1567 llvm::llvm_shutdown();
1568
Daniel Dunbarb0adbba2008-10-04 23:42:49 +00001569 return HadErrors || (Diags.getNumErrors() != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001570}