blob: 303bfe757f5016dcb864a0614d06d0a7a460956e [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"
Zhongxing Xu20922362008-11-26 05:23:17 +000050#include "llvm/Support/PluginLoader.h"
Daniel Dunbare553a722008-10-02 01:21:33 +000051#include "llvm/System/Host.h"
Chris Lattnerdcaa0962008-03-03 03:16:03 +000052#include "llvm/System/Path.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000053#include "llvm/System/Signals.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000054using namespace clang;
55
56//===----------------------------------------------------------------------===//
57// Global options.
58//===----------------------------------------------------------------------===//
59
Daniel Dunbard3db4012008-10-16 16:54:18 +000060static bool HadErrors = false;
Daniel Dunbarb0adbba2008-10-04 23:42:49 +000061
Reid Spencer5f016e22007-07-11 17:01:13 +000062static llvm::cl::opt<bool>
63Verbose("v", llvm::cl::desc("Enable verbose output"));
64static llvm::cl::opt<bool>
Nate Begemanaabbb122007-12-30 01:38:50 +000065Stats("print-stats",
66 llvm::cl::desc("Print performance metrics and statistics"));
Daniel Dunbard3db4012008-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));
Reid Spencer5f016e22007-07-11 17:01:13 +000071
72enum ProgActions {
Steve Naroffb29b4272008-04-14 22:03:09 +000073 RewriteObjC, // ObjC->C Rewriter.
Steve Naroff13188952008-09-18 14:10:13 +000074 RewriteBlocks, // ObjC->C Rewriter for Blocks.
Chris Lattnerb57e3d42008-05-08 06:52:13 +000075 RewriteMacros, // Expand macros but not #includes.
Chris Lattnerb13c5ee2008-10-12 05:29:20 +000076 RewriteTest, // Rewriter playground
Ted Kremenek13e479b2008-03-19 07:53:42 +000077 HTMLTest, // HTML displayer testing stuff.
Daniel Dunbard69bacc2008-10-21 23:49:24 +000078 EmitAssembly, // Emit a .s file.
Reid Spencer5f016e22007-07-11 17:01:13 +000079 EmitLLVM, // Emit a .ll file.
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +000080 EmitBC, // Emit a .bc file.
Ted Kremeneka1fa3a12007-12-13 00:37:31 +000081 SerializeAST, // Emit a .ast file.
Ted Kremenek6a340832008-03-18 21:19:49 +000082 EmitHTML, // Translate input source into HTML.
Chris Lattner3b427b32007-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 Kremenekbfa82c42007-10-16 23:37:27 +000086 TestSerialization, // Run experimental serialization code.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerc106c102008-10-12 05:03:36 +000092 DumpTokens, // Dump out preprocessed tokens.
93 DumpRawTokens, // Dump out raw tokens.
Ted Kremenek85888962008-10-21 00:54:44 +000094 RunAnalysis, // Run one or more source code analyses.
Ted Kremenek7cae2f62008-10-23 23:36:29 +000095 GeneratePCH, // Generate precompiled header.
96 InheritanceView // View C++ inheritance for a specified class.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerc106c102008-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",
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek6a340832008-03-18 21:19:49 +0000117 clEnumValN(EmitHTML, "emit-html",
118 "Output input source as HTML"),
Chris Lattner3b427b32007-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 Lattnerea254db2007-10-11 00:37:43 +0000123 clEnumValN(ASTView, "ast-view",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000124 "Build ASTs and view them with GraphViz"),
Ted Kremenekbfa82c42007-10-16 23:37:27 +0000125 clEnumValN(TestSerialization, "test-pickling",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000126 "Run prototype serialization code"),
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000127 clEnumValN(EmitAssembly, "S",
128 "Emit native assembly code"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 clEnumValN(EmitLLVM, "emit-llvm",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000130 "Build ASTs then convert to LLVM, emit .ll file"),
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000131 clEnumValN(EmitBC, "emit-llvm-bc",
132 "Build ASTs then convert to LLVM, emit .bc file"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000133 clEnumValN(SerializeAST, "serialize",
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000134 "Build ASTs and emit .ast file"),
Chris Lattnerb13c5ee2008-10-12 05:29:20 +0000135 clEnumValN(RewriteTest, "rewrite-test",
136 "Rewriter playground"),
Steve Naroffb29b4272008-04-14 22:03:09 +0000137 clEnumValN(RewriteObjC, "rewrite-objc",
Chris Lattnerb57e3d42008-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 Naroff13188952008-09-18 14:10:13 +0000141 clEnumValN(RewriteBlocks, "rewrite-blocks",
142 "Rewrite Blocks to C"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 clEnumValEnd));
144
Ted Kremenekccc76472007-12-19 19:47:59 +0000145
146static llvm::cl::opt<std::string>
147OutputFile("o",
Ted Kremenek50b56412007-12-19 19:50:41 +0000148 llvm::cl::value_desc("path"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000149 llvm::cl::desc("Specify output file (for --serialize, this is a directory)"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000150
151//===----------------------------------------------------------------------===//
Sanjiv Guptae8b9f5b2008-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
Ted Kremenekc2e72992008-12-02 19:57:31 +0000158
159//===----------------------------------------------------------------------===//
160// PTH.
161//===----------------------------------------------------------------------===//
162
163static llvm::cl::opt<std::string>
164TokenCache("token-cache", llvm::cl::value_desc("path"),
165 llvm::cl::desc("Use specified token cache file"));
166
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000167//===----------------------------------------------------------------------===//
Ted Kremenek55af98c2008-04-14 18:40:58 +0000168// Diagnostic Options
169//===----------------------------------------------------------------------===//
170
Ted Kremenek41193e42007-09-26 19:42:19 +0000171static llvm::cl::opt<bool>
172VerifyDiagnostics("verify",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000173 llvm::cl::desc("Verify emitted diagnostics and warnings"));
Ted Kremenek41193e42007-09-26 19:42:19 +0000174
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000175static llvm::cl::opt<std::string>
176HTMLDiag("html-diags",
177 llvm::cl::desc("Generate HTML to report diagnostics"),
178 llvm::cl::value_desc("HTML directory"));
179
Nico Weberfd54ebc2008-08-05 23:33:20 +0000180static llvm::cl::opt<bool>
181NoShowColumn("fno-show-column",
182 llvm::cl::desc("Do not include column number on diagnostics"));
183
184static llvm::cl::opt<bool>
185NoCaretDiagnostics("fno-caret-diagnostics",
186 llvm::cl::desc("Do not include source line and caret with"
187 " diagnostics"));
188
189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000191// C++ Visualization.
192//===----------------------------------------------------------------------===//
193
194static llvm::cl::opt<std::string>
195InheritanceViewCls("cxx-inheritance-view",
196 llvm::cl::value_desc("class name"),
197 llvm::cl::desc("View C++ inhertance for a specified class"));
198
199//===----------------------------------------------------------------------===//
200// Analyzer Options.
Ted Kremenek55af98c2008-04-14 18:40:58 +0000201//===----------------------------------------------------------------------===//
202
203static llvm::cl::opt<bool>
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000204VisualizeEGDot("analyzer-viz-egraph-graphviz",
205 llvm::cl::desc("Display exploded graph using GraphViz"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000206
207static llvm::cl::opt<bool>
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000208VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
209 llvm::cl::desc("Display exploded graph using Ubigraph"));
210
211static llvm::cl::opt<bool>
212AnalyzeAll("analyzer-opt-analyze-headers",
Ted Kremenek55af98c2008-04-14 18:40:58 +0000213 llvm::cl::desc("Force the static analyzer to analyze "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000214 "functions defined in header files"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000215
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000216static llvm::cl::list<Analyses>
Ted Kremenek95c7b002008-10-24 01:04:59 +0000217AnalysisList(llvm::cl::desc("SCA Checks/Analyses:"),
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000218llvm::cl::values(
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000219#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000220clEnumValN(NAME, CMDFLAG, DESC),
221#include "Analyses.def"
222clEnumValEnd));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000223
Ted Kremenek95c7b002008-10-24 01:04:59 +0000224static llvm::cl::opt<AnalysisStores>
225AnalysisStoreOpt(llvm::cl::desc("SCA Low-Level Options (Store):"),
226 llvm::cl::init(BasicStoreModel),
227 llvm::cl::values(
228#define ANALYSIS_STORE(NAME, CMDFLAG, DESC)\
229clEnumValN(NAME##Model, "analyzer-store-" CMDFLAG, DESC),
230#include "Analyses.def"
231clEnumValEnd));
232
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000233static llvm::cl::opt<AnalysisDiagClients>
234AnalysisDiagOpt(llvm::cl::desc("SCA Output Options:"),
235 llvm::cl::init(PD_HTML),
236 llvm::cl::values(
237#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN)\
238clEnumValN(PD_##NAME, "analyzer-output-" CMDFLAG, DESC),
239#include "Analyses.def"
240clEnumValEnd));
241
Ted Kremenek55af98c2008-04-14 18:40:58 +0000242//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000243// Language Options
244//===----------------------------------------------------------------------===//
245
246enum LangKind {
247 langkind_unspecified,
248 langkind_c,
249 langkind_c_cpp,
Chris Lattnera778d7d2008-10-22 17:29:21 +0000250 langkind_asm_cpp,
Ted Kremenek85888962008-10-21 00:54:44 +0000251 langkind_c_pch,
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 langkind_cxx,
253 langkind_cxx_cpp,
254 langkind_objc,
255 langkind_objc_cpp,
Ted Kremenek85888962008-10-21 00:54:44 +0000256 langkind_objc_pch,
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 langkind_objcxx,
258 langkind_objcxx_cpp
259};
260
261/* TODO: GCC also accepts:
262 c-header c++-header objective-c-header objective-c++-header
Chris Lattnera778d7d2008-10-22 17:29:21 +0000263 assembler
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 ada, f77*, ratfor (!), f95, java, treelang
265 */
266static llvm::cl::opt<LangKind>
267BaseLang("x", llvm::cl::desc("Base language to compile"),
268 llvm::cl::init(langkind_unspecified),
269 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
270 clEnumValN(langkind_cxx, "c++", "C++"),
271 clEnumValN(langkind_objc, "objective-c", "Objective C"),
272 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
273 clEnumValN(langkind_c_cpp, "c-cpp-output",
274 "Preprocessed C"),
Chris Lattnera778d7d2008-10-22 17:29:21 +0000275 clEnumValN(langkind_asm_cpp, "assembler-with-cpp",
276 "Preprocessed asm"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
Ted Kremenek85888962008-10-21 00:54:44 +0000278 "Preprocessed C++"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
280 "Preprocessed Objective C"),
281 clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output",
282 "Preprocessed Objective C++"),
Ted Kremenek85888962008-10-21 00:54:44 +0000283 clEnumValN(langkind_c_pch,"c-header",
284 "Precompiled C header"),
285 clEnumValN(langkind_objc_pch, "objective-c-header",
286 "Precompiled Objective C header"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 clEnumValEnd));
288
289static llvm::cl::opt<bool>
290LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
291 llvm::cl::Hidden);
292static llvm::cl::opt<bool>
293LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
294 llvm::cl::Hidden);
295
Ted Kremenek8904f152007-12-05 23:49:08 +0000296/// InitializeBaseLanguage - Handle the -x foo options.
297static void InitializeBaseLanguage() {
298 if (LangObjC)
299 BaseLang = langkind_objc;
300 else if (LangObjCXX)
301 BaseLang = langkind_objcxx;
302}
303
304static LangKind GetLanguage(const std::string &Filename) {
305 if (BaseLang != langkind_unspecified)
306 return BaseLang;
307
308 std::string::size_type DotPos = Filename.rfind('.');
309
310 if (DotPos == std::string::npos) {
311 BaseLang = langkind_c; // Default to C if no extension.
Chris Lattner9b2f6c42008-01-04 19:12:28 +0000312 return langkind_c;
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 }
314
Ted Kremenek8904f152007-12-05 23:49:08 +0000315 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
316 // C header: .h
317 // C++ header: .hh or .H;
318 // assembler no preprocessing: .s
319 // assembler: .S
320 if (Ext == "c")
321 return langkind_c;
Chris Lattner1b450b02008-10-28 20:33:42 +0000322 else if (Ext == "S")
Chris Lattnera778d7d2008-10-22 17:29:21 +0000323 return langkind_asm_cpp;
Ted Kremenek8904f152007-12-05 23:49:08 +0000324 else if (Ext == "i")
325 return langkind_c_cpp;
326 else if (Ext == "ii")
327 return langkind_cxx_cpp;
328 else if (Ext == "m")
329 return langkind_objc;
330 else if (Ext == "mi")
331 return langkind_objc_cpp;
332 else if (Ext == "mm" || Ext == "M")
333 return langkind_objcxx;
334 else if (Ext == "mii")
335 return langkind_objcxx_cpp;
336 else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
337 Ext == "c++" || Ext == "cp" || Ext == "cxx")
338 return langkind_cxx;
339 else
340 return langkind_c;
341}
342
343
Ted Kremenek85888962008-10-21 00:54:44 +0000344static void InitializeCOptions(LangOptions &Options) {
345 // Do nothing.
346}
347
348static void InitializeObjCOptions(LangOptions &Options) {
349 Options.ObjC1 = Options.ObjC2 = 1;
350}
351
352
353static bool InitializeLangOptions(LangOptions &Options, LangKind LK){
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 // FIXME: implement -fpreprocessed mode.
355 bool NoPreprocess = false;
Ted Kremenek85888962008-10-21 00:54:44 +0000356 bool PCH = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000357
Ted Kremenek8904f152007-12-05 23:49:08 +0000358 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 default: assert(0 && "Unknown language kind!");
Ted Kremenek85888962008-10-21 00:54:44 +0000360 case langkind_c_pch:
361 InitializeCOptions(Options);
362 PCH = true;
363 break;
Chris Lattnera778d7d2008-10-22 17:29:21 +0000364 case langkind_asm_cpp:
Daniel Dunbarc1571452008-12-01 18:55:22 +0000365 Options.AsmPreprocessor = 1;
Chris Lattnera778d7d2008-10-22 17:29:21 +0000366 // FALLTHROUGH
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 case langkind_c_cpp:
368 NoPreprocess = true;
369 // FALLTHROUGH
370 case langkind_c:
Ted Kremenek85888962008-10-21 00:54:44 +0000371 InitializeCOptions(Options);
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 break;
373 case langkind_cxx_cpp:
374 NoPreprocess = true;
375 // FALLTHROUGH
376 case langkind_cxx:
377 Options.CPlusPlus = 1;
378 break;
Ted Kremenek85888962008-10-21 00:54:44 +0000379 case langkind_objc_pch:
380 InitializeObjCOptions(Options);
381 PCH = true;
382 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 case langkind_objc_cpp:
384 NoPreprocess = true;
385 // FALLTHROUGH
386 case langkind_objc:
Ted Kremenek85888962008-10-21 00:54:44 +0000387 InitializeObjCOptions(Options);
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 break;
389 case langkind_objcxx_cpp:
390 NoPreprocess = true;
391 // FALLTHROUGH
392 case langkind_objcxx:
393 Options.ObjC1 = Options.ObjC2 = 1;
394 Options.CPlusPlus = 1;
395 break;
396 }
Ted Kremenek85888962008-10-21 00:54:44 +0000397
398 return PCH;
Reid Spencer5f016e22007-07-11 17:01:13 +0000399}
400
401/// LangStds - Language standards we support.
402enum LangStds {
403 lang_unspecified,
404 lang_c89, lang_c94, lang_c99,
Ted Kremenekea644d82008-09-03 21:22:16 +0000405 lang_gnu_START,
406 lang_gnu89 = lang_gnu_START, lang_gnu99,
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000407 lang_cxx98, lang_gnucxx98,
408 lang_cxx0x, lang_gnucxx0x
Reid Spencer5f016e22007-07-11 17:01:13 +0000409};
410
411static llvm::cl::opt<LangStds>
412LangStd("std", llvm::cl::desc("Language standard to compile for"),
413 llvm::cl::init(lang_unspecified),
414 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
415 clEnumValN(lang_c89, "c90", "ISO C 1990"),
416 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
417 clEnumValN(lang_c94, "iso9899:199409",
418 "ISO C 1990 with amendment 1"),
419 clEnumValN(lang_c99, "c99", "ISO C 1999"),
420// clEnumValN(lang_c99, "c9x", "ISO C 1999"),
421 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
422// clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
423 clEnumValN(lang_gnu89, "gnu89",
424 "ISO C 1990 with GNU extensions (default for C)"),
425 clEnumValN(lang_gnu99, "gnu99",
426 "ISO C 1999 with GNU extensions"),
427 clEnumValN(lang_gnu99, "gnu9x",
428 "ISO C 1999 with GNU extensions"),
429 clEnumValN(lang_cxx98, "c++98",
430 "ISO C++ 1998 with amendments"),
431 clEnumValN(lang_gnucxx98, "gnu++98",
432 "ISO C++ 1998 with amendments and GNU "
433 "extensions (default for C++)"),
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000434 clEnumValN(lang_cxx0x, "c++0x",
435 "Upcoming ISO C++ 200x with amendments"),
436 clEnumValN(lang_gnucxx0x, "gnu++0x",
437 "Upcoming ISO C++ 200x with amendments and GNU "
438 "extensions (default for C++)"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 clEnumValEnd));
440
441static llvm::cl::opt<bool>
442NoOperatorNames("fno-operator-names",
443 llvm::cl::desc("Do not treat C++ operator name keywords as "
444 "synonyms for operators"));
445
Anders Carlssonee98ac52007-10-15 02:50:23 +0000446static llvm::cl::opt<bool>
447PascalStrings("fpascal-strings",
448 llvm::cl::desc("Recognize and construct Pascal-style "
449 "string literals"));
Steve Naroffd62701b2008-02-07 03:50:06 +0000450
451static llvm::cl::opt<bool>
452MSExtensions("fms-extensions",
453 llvm::cl::desc("Accept some non-standard constructs used in "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000454 "Microsoft header files "));
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000455
456static llvm::cl::opt<bool>
457WritableStrings("fwritable-strings",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000458 llvm::cl::desc("Store string literals as writable data"));
Anders Carlsson695dbb62007-11-30 04:21:22 +0000459
460static llvm::cl::opt<bool>
461LaxVectorConversions("flax-vector-conversions",
462 llvm::cl::desc("Allow implicit conversions between vectors"
463 " with a different number of elements or "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000464 "different element types"));
Chris Lattnerae0ee032008-12-04 23:20:07 +0000465static llvm::cl::opt<bool>
466EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature"));
467
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000468
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000469// FIXME: This (and all GCC -f options) really come in -f... and
470// -fno-... forms, and additionally support automagic behavior when
471// they are not defined. For example, -fexceptions defaults to on or
472// off depending on the language. We should support this behavior in
473// some form (perhaps just add a facility for distinguishing when an
474// has its default value from when it has been set to its default
475// value).
476static llvm::cl::opt<bool>
477Exceptions("fexceptions",
478 llvm::cl::desc("Enable support for exception handling."));
479
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000480static llvm::cl::opt<bool>
481GNURuntime("fgnu-runtime",
Ted Kremenek85888962008-10-21 00:54:44 +0000482 llvm::cl::desc("Generate output compatible with the standard GNU "
483 "Objective-C runtime."));
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000484
485static llvm::cl::opt<bool>
486NeXTRuntime("fnext-runtime",
Ted Kremenek85888962008-10-21 00:54:44 +0000487 llvm::cl::desc("Generate output compatible with the NeXT "
488 "runtime."));
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000489
Ted Kremenekea644d82008-09-03 21:22:16 +0000490
491
492static llvm::cl::opt<bool>
493Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences."));
494
495static llvm::cl::opt<bool>
496Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89."));
497
Reid Spencer5f016e22007-07-11 17:01:13 +0000498// FIXME: add:
Reid Spencer5f016e22007-07-11 17:01:13 +0000499// -fdollars-in-identifiers
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +0000500static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
501 TargetInfo *Target) {
Chris Lattner8fc4dfb2008-12-04 22:54:33 +0000502 // Allow the target to set the default the langauge options as it sees fit.
503 Target->getDefaultLangOptions(Options);
Ted Kremenekea644d82008-09-03 21:22:16 +0000504
505 if (Ansi) // "The -ansi option is equivalent to -std=c89."
506 LangStd = lang_c89;
507
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 if (LangStd == lang_unspecified) {
509 // Based on the base language, pick one.
Ted Kremenek8904f152007-12-05 23:49:08 +0000510 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 default: assert(0 && "Unknown base language");
512 case langkind_c:
Chris Lattnera778d7d2008-10-22 17:29:21 +0000513 case langkind_asm_cpp:
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 case langkind_c_cpp:
Ted Kremenek85888962008-10-21 00:54:44 +0000515 case langkind_c_pch:
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 case langkind_objc:
517 case langkind_objc_cpp:
Ted Kremenek85888962008-10-21 00:54:44 +0000518 case langkind_objc_pch:
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 LangStd = lang_gnu99;
520 break;
521 case langkind_cxx:
522 case langkind_cxx_cpp:
523 case langkind_objcxx:
524 case langkind_objcxx_cpp:
525 LangStd = lang_gnucxx98;
526 break;
527 }
528 }
529
530 switch (LangStd) {
531 default: assert(0 && "Unknown language standard!");
532
533 // Fall through from newer standards to older ones. This isn't really right.
534 // FIXME: Enable specifically the right features based on the language stds.
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000535 case lang_gnucxx0x:
536 case lang_cxx0x:
537 Options.CPlusPlus0x = 1;
538 // FALL THROUGH
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 case lang_gnucxx98:
540 case lang_cxx98:
541 Options.CPlusPlus = 1;
542 Options.CXXOperatorNames = !NoOperatorNames;
Nate Begeman8aebcb72007-11-15 07:30:50 +0000543 Options.Boolean = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 // FALL THROUGH.
545 case lang_gnu99:
546 case lang_c99:
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 Options.C99 = 1;
548 Options.HexFloats = 1;
549 // FALL THROUGH.
550 case lang_gnu89:
551 Options.BCPLComment = 1; // Only for C99/C++.
552 // FALL THROUGH.
553 case lang_c94:
Chris Lattner3426b9b2008-02-25 04:01:39 +0000554 Options.Digraphs = 1; // C94, C99, C++.
555 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 case lang_c89:
557 break;
558 }
Argyrios Kyrtzidisd1465522008-09-11 04:21:06 +0000559
560 if (Options.CPlusPlus) {
561 Options.C99 = 0;
562 Options.HexFloats = (LangStd == lang_gnucxx98 || LangStd==lang_gnucxx0x);
563 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000564
Chris Lattnerd658b562008-04-05 06:32:51 +0000565 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
566 Options.ImplicitInt = 1;
567 else
568 Options.ImplicitInt = 0;
Ted Kremenekea644d82008-09-03 21:22:16 +0000569
570 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi
571 // is specified, or -std is set to a conforming mode.
572 Options.Trigraphs = LangStd < lang_gnu_START || Trigraphs ? 1 : 0;
573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 Options.DollarIdents = 1; // FIXME: Really a target property.
Chris Lattnerae0ee032008-12-04 23:20:07 +0000575 if (PascalStrings.getPosition())
576 Options.PascalStrings = PascalStrings;
Steve Naroffd62701b2008-02-07 03:50:06 +0000577 Options.Microsoft = MSExtensions;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000578 Options.WritableStrings = WritableStrings;
Anders Carlsson695dbb62007-11-30 04:21:22 +0000579 Options.LaxVectorConversions = LaxVectorConversions;
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000580 Options.Exceptions = Exceptions;
Chris Lattnerae0ee032008-12-04 23:20:07 +0000581 if (EnableBlocks.getPosition())
582 Options.Blocks = EnableBlocks;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000583
Chris Lattner8fc4dfb2008-12-04 22:54:33 +0000584 // Override the default runtime if the user requested it.
585 if (NeXTRuntime)
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000586 Options.NeXTRuntime = 1;
Chris Lattner8fc4dfb2008-12-04 22:54:33 +0000587 else if (GNURuntime)
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000588 Options.NeXTRuntime = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000589}
590
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000591static llvm::cl::opt<bool>
592ObjCExclusiveGC("fobjc-gc-only",
593 llvm::cl::desc("Use GC exclusively for Objective-C related "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000594 "memory management"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000595
596static llvm::cl::opt<bool>
597ObjCEnableGC("fobjc-gc",
Nico Weberfd54ebc2008-08-05 23:33:20 +0000598 llvm::cl::desc("Enable Objective-C garbage collection"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000599
600void InitializeGCMode(LangOptions &Options) {
601 if (ObjCExclusiveGC)
602 Options.setGCMode(LangOptions::GCOnly);
603 else if (ObjCEnableGC)
604 Options.setGCMode(LangOptions::HybridGC);
605}
606
607
Reid Spencer5f016e22007-07-11 17:01:13 +0000608//===----------------------------------------------------------------------===//
609// Our DiagnosticClient implementation
610//===----------------------------------------------------------------------===//
611
612// FIXME: Werror should take a list of things, -Werror=foo,bar
613static llvm::cl::opt<bool>
614WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
615
616static llvm::cl::opt<bool>
Chris Lattner5b4681c2008-05-29 15:36:45 +0000617SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings"));
618
619static llvm::cl::opt<bool>
Reid Spencer5f016e22007-07-11 17:01:13 +0000620WarnOnExtensions("pedantic", llvm::cl::init(false),
621 llvm::cl::desc("Issue a warning on uses of GCC extensions"));
622
623static llvm::cl::opt<bool>
624ErrorOnExtensions("pedantic-errors",
625 llvm::cl::desc("Issue an error on uses of GCC extensions"));
626
627static llvm::cl::opt<bool>
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000628SuppressSystemWarnings("suppress-system-warnings",
Daniel Dunbar320a0542008-09-30 20:49:53 +0000629 llvm::cl::desc("Suppress warnings issued in system headers"),
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000630 llvm::cl::init(true));
631
632static llvm::cl::opt<bool>
Reid Spencer5f016e22007-07-11 17:01:13 +0000633WarnUnusedMacros("Wunused_macros",
634 llvm::cl::desc("Warn for unused macros in the main translation unit"));
635
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000636static llvm::cl::opt<bool>
637WarnFloatEqual("Wfloat-equal",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000638 llvm::cl::desc("Warn about equality comparisons of floating point values"));
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000639
Ted Kremenek73da5902007-12-17 17:50:07 +0000640static llvm::cl::opt<bool>
641WarnNoFormatNonLiteral("Wno-format-nonliteral",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000642 llvm::cl::desc("Do not warn about non-literal format strings"));
Ted Kremenek73da5902007-12-17 17:50:07 +0000643
Chris Lattner116a4b12008-01-23 17:19:46 +0000644static llvm::cl::opt<bool>
645WarnUndefMacros("Wundef",
646 llvm::cl::desc("Warn on use of undefined macros in #if's"));
647
Chris Lattner37d10842008-05-05 21:18:06 +0000648static llvm::cl::opt<bool>
Ted Kremenek358256d2008-05-30 16:42:02 +0000649WarnImplicitFunctionDeclaration("Wimplicit-function-declaration",
650 llvm::cl::desc("Warn about uses of implicitly defined functions"));
Chris Lattner116a4b12008-01-23 17:19:46 +0000651
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000652static llvm::cl::opt<bool>
653WarnNoStrictSelectorMatch("Wno-strict-selector-match",
654 llvm::cl::desc("Do not warn about duplicate methods that have the same size and alignment"),
655 llvm::cl::init(true));
656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657/// InitializeDiagnostics - Initialize the diagnostic object, based on the
658/// current command line option settings.
659static void InitializeDiagnostics(Diagnostic &Diags) {
Chris Lattner5b4681c2008-05-29 15:36:45 +0000660 Diags.setIgnoreAllWarnings(SilenceWarnings);
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 Diags.setWarningsAsErrors(WarningsAsErrors);
662 Diags.setWarnOnExtensions(WarnOnExtensions);
663 Diags.setErrorOnExtensions(ErrorOnExtensions);
664
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000665 // Suppress warnings in system headers unless requested not to.
666 Diags.setSuppressSystemWarnings(SuppressSystemWarnings);
667
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 // Silence the "macro is not used" warning unless requested.
669 if (!WarnUnusedMacros)
670 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000671
672 // Silence "floating point comparison" warnings unless requested.
673 if (!WarnFloatEqual)
674 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
Ted Kremenek73da5902007-12-17 17:50:07 +0000675
676 // Silence "format string is not a string literal" warnings if requested
677 if (WarnNoFormatNonLiteral)
Ted Kremenek7c1d3df2007-12-17 17:50:39 +0000678 Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant,
679 diag::MAP_IGNORE);
Chris Lattner116a4b12008-01-23 17:19:46 +0000680 if (!WarnUndefMacros)
681 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);
Steve Naroffe7a37302008-02-11 22:40:08 +0000682
Chris Lattner37d10842008-05-05 21:18:06 +0000683 if (!WarnImplicitFunctionDeclaration)
684 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
685 diag::MAP_IGNORE);
686
Steve Naroffe7a37302008-02-11 22:40:08 +0000687 if (MSExtensions) // MS allows unnamed struct/union fields.
688 Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE);
Chris Lattner8aedf192008-05-04 23:52:02 +0000689
690 // If -pedantic-errors is set, turn extensions that warn by default into
691 // errors.
692 if (ErrorOnExtensions) {
693 Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large,
694 diag::MAP_ERROR);
695 Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large,
696 diag::MAP_ERROR);
697 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000698}
699
700//===----------------------------------------------------------------------===//
Ted Kremenekcb330932008-02-18 21:21:23 +0000701// Analysis-specific options.
702//===----------------------------------------------------------------------===//
703
704static llvm::cl::opt<std::string>
705AnalyzeSpecificFunction("analyze-function",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000706 llvm::cl::desc("Run analysis on specific function"));
Ted Kremenekcb330932008-02-18 21:21:23 +0000707
Ted Kremenekffe0f432008-03-07 22:58:01 +0000708static llvm::cl::opt<bool>
Ted Kremenekd71ed262008-04-10 22:16:52 +0000709TrimGraph("trim-egraph",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000710 llvm::cl::desc("Only show error-related paths in the analysis graph"));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000711
Ted Kremenekcb330932008-02-18 21:21:23 +0000712//===----------------------------------------------------------------------===//
Ted Kremenekae360762007-12-03 22:06:55 +0000713// Target Triple Processing.
714//===----------------------------------------------------------------------===//
715
716static llvm::cl::opt<std::string>
717TargetTriple("triple",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000718 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000719
Chris Lattner42e67372008-03-05 01:18:20 +0000720static llvm::cl::opt<std::string>
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000721Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000722
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000723static llvm::cl::opt<std::string>
724MacOSVersionMin("mmacosx-version-min",
725 llvm::cl::desc("Specify target Mac OS/X version (e.g. 10.5)"));
726
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000727// If -mmacosx-version-min=10.3.9 is specified, change the triple from being
728// something like powerpc-apple-darwin9 to powerpc-apple-darwin7
729static void HandleMacOSVersionMin(std::string &Triple) {
730 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
731 if (DarwinDashIdx == std::string::npos) {
732 fprintf(stderr,
733 "-mmacosx-version-min only valid for darwin (Mac OS/X) targets\n");
734 exit(1);
735 }
736 unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin");
737
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000738 // Remove the number.
739 Triple.resize(DarwinNumIdx);
740
741 // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9]
742 bool MacOSVersionMinIsInvalid = false;
743 int VersionNum = 0;
744 if (MacOSVersionMin.size() < 4 ||
745 MacOSVersionMin.substr(0, 3) != "10." ||
746 !isdigit(MacOSVersionMin[3])) {
747 MacOSVersionMinIsInvalid = true;
748 } else {
749 const char *Start = MacOSVersionMin.c_str()+3;
750 char *End = 0;
751 VersionNum = (int)strtol(Start, &End, 10);
752
Chris Lattner079f2c462008-09-30 20:30:12 +0000753 // The version number must be in the range 0-9.
754 MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9;
755
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000756 // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7.
757 Triple += llvm::itostr(VersionNum+4);
758
Chris Lattner079f2c462008-09-30 20:30:12 +0000759 if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok.
760 // Add the period piece (.7) to the end of the triple. This gives us
761 // something like ...-darwin8.7
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000762 Triple += End;
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000763 } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not.
764 MacOSVersionMinIsInvalid = true;
765 }
766 }
767
768 if (MacOSVersionMinIsInvalid) {
769 fprintf(stderr,
770 "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n",
771 MacOSVersionMin.c_str());
772 exit(1);
773 }
774}
775
776/// CreateTargetTriple - Process the various options that affect the target
777/// triple and build a final aggregate triple that we are compiling for.
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000778static std::string CreateTargetTriple() {
Ted Kremenekae360762007-12-03 22:06:55 +0000779 // Initialize base triple. If a -triple option has been specified, use
780 // that triple. Otherwise, default to the host triple.
Chris Lattner6590d212007-12-12 05:01:48 +0000781 std::string Triple = TargetTriple;
Daniel Dunbare553a722008-10-02 01:21:33 +0000782 if (Triple.empty()) {
783 Triple = LLVM_HOSTTRIPLE;
784
785 // On darwin, we want to update the version to match that of the
786 // host.
787 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
788 if (DarwinDashIdx != std::string::npos) {
789 Triple.resize(DarwinDashIdx + strlen("-darwin"));
790
791 Triple += llvm::sys::osVersion();
792 }
793 }
Ted Kremenekae360762007-12-03 22:06:55 +0000794
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000795 // If -arch foo was specified, remove the architecture from the triple we have
796 // so far and replace it with the specified one.
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000797 if (!Arch.empty()) {
798 // Decompose the base triple into "arch" and suffix.
799 std::string::size_type FirstDashIdx = Triple.find('-');
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000800
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000801 if (FirstDashIdx == std::string::npos) {
802 fprintf(stderr,
803 "Malformed target triple: \"%s\" ('-' could not be found).\n",
804 Triple.c_str());
805 exit(1);
806 }
Ted Kremenekae360762007-12-03 22:06:55 +0000807
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000808 Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end());
809 }
810
811 // If -mmacosx-version-min=10.3.9 is specified, change the triple from being
812 // something like powerpc-apple-darwin9 to powerpc-apple-darwin7
Chris Lattnerba0f25f2008-09-30 20:16:56 +0000813 if (!MacOSVersionMin.empty())
814 HandleMacOSVersionMin(Triple);
Ted Kremenekae360762007-12-03 22:06:55 +0000815
Chris Lattner6a30c1f2008-09-30 01:13:12 +0000816 return Triple;
Ted Kremenekae360762007-12-03 22:06:55 +0000817}
818
819//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000820// Preprocessor Initialization
821//===----------------------------------------------------------------------===//
822
823// FIXME: Preprocessor builtins to support.
824// -A... - Play with #assertions
825// -undef - Undefine all predefined macros
826
827static llvm::cl::list<std::string>
828D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
829 llvm::cl::desc("Predefine the specified macro"));
830static llvm::cl::list<std::string>
831U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
832 llvm::cl::desc("Undefine the specified macro"));
833
Chris Lattner64299f82008-01-10 01:53:41 +0000834static llvm::cl::list<std::string>
835ImplicitIncludes("include", llvm::cl::value_desc("file"),
836 llvm::cl::desc("Include file before parsing"));
837
838
Reid Spencer5f016e22007-07-11 17:01:13 +0000839// Append a #define line to Buf for Macro. Macro should be of the form XXX,
840// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
841// "#define XXX Y z W". To get a #define with no value, use "XXX=".
842static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
843 const char *Command = "#define ") {
844 Buf.insert(Buf.end(), Command, Command+strlen(Command));
845 if (const char *Equal = strchr(Macro, '=')) {
846 // Turn the = into ' '.
847 Buf.insert(Buf.end(), Macro, Equal);
848 Buf.push_back(' ');
849 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
850 } else {
851 // Push "macroname 1".
852 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
853 Buf.push_back(' ');
854 Buf.push_back('1');
855 }
856 Buf.push_back('\n');
857}
858
Chris Lattner64299f82008-01-10 01:53:41 +0000859/// AddImplicitInclude - Add an implicit #include of the specified file to the
860/// predefines buffer.
861static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){
862 const char *Inc = "#include \"";
863 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
864 Buf.insert(Buf.end(), File.begin(), File.end());
865 Buf.push_back('"');
866 Buf.push_back('\n');
867}
868
Reid Spencer5f016e22007-07-11 17:01:13 +0000869
Chris Lattner53b0dab2007-10-09 22:10:18 +0000870/// InitializePreprocessor - Initialize the preprocessor getting it and the
Chris Lattner51574ea2008-04-19 23:25:44 +0000871/// environment ready to process a single file. This returns true on error.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000872///
Chris Lattner51574ea2008-04-19 23:25:44 +0000873static bool InitializePreprocessor(Preprocessor &PP,
874 bool InitializeSourceMgr,
875 const std::string &InFile) {
Chris Lattnerdee73592007-12-15 20:48:40 +0000876 FileManager &FileMgr = PP.getFileManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000877
Chris Lattner53b0dab2007-10-09 22:10:18 +0000878 // Figure out where to get and map in the main file.
Chris Lattnerdee73592007-12-15 20:48:40 +0000879 SourceManager &SourceMgr = PP.getSourceManager();
Ted Kremenek339b9c22008-04-17 22:31:54 +0000880
881 if (InitializeSourceMgr) {
882 if (InFile != "-") {
883 const FileEntry *File = FileMgr.getFile(InFile);
884 if (File) SourceMgr.createMainFileID(File, SourceLocation());
885 if (SourceMgr.getMainFileID() == 0) {
886 fprintf(stderr, "Error reading '%s'!\n",InFile.c_str());
Chris Lattner51574ea2008-04-19 23:25:44 +0000887 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000888 }
889 } else {
890 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
891 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
892 if (SourceMgr.getMainFileID() == 0) {
893 fprintf(stderr, "Error reading standard input! Empty?\n");
Chris Lattner51574ea2008-04-19 23:25:44 +0000894 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000895 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000896 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 }
Sam Bishop1102d6b2008-04-14 14:41:57 +0000898
Chris Lattneraa391972008-04-19 23:09:31 +0000899 std::vector<char> PredefineBuffer;
900
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 // Add macros from the command line.
Sam Bishop1102d6b2008-04-14 14:41:57 +0000902 unsigned d = 0, D = D_macros.size();
903 unsigned u = 0, U = U_macros.size();
904 while (d < D || u < U) {
905 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
906 DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str());
907 else
908 DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef ");
909 }
910
Chris Lattner64299f82008-01-10 01:53:41 +0000911 // FIXME: Read any files specified by -imacros.
912
913 // Add implicit #includes from -include.
914 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
915 AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000916
Chris Lattneraa391972008-04-19 23:09:31 +0000917 // Null terminate PredefinedBuffer and add it.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000918 PredefineBuffer.push_back(0);
Chris Lattneraa391972008-04-19 23:09:31 +0000919 PP.setPredefines(&PredefineBuffer[0]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000920
Ted Kremenekc2e72992008-12-02 19:57:31 +0000921 // Use PTH.
922 if (!TokenCache.empty()) {
923 PTHManager* PM = PTHManager::Create(TokenCache, PP);
924 if (PM) PP.setPTHManager(PM);
925 }
926
Chris Lattner53b0dab2007-10-09 22:10:18 +0000927 // Once we've read this, we're done.
Chris Lattner51574ea2008-04-19 23:25:44 +0000928 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000929}
930
931//===----------------------------------------------------------------------===//
932// Preprocessor include path information.
933//===----------------------------------------------------------------------===//
934
935// This tool exports a large number of command line options to control how the
936// preprocessor searches for header files. At root, however, the Preprocessor
937// object takes a very simple interface: a list of directories to search for
938//
939// FIXME: -nostdinc,-nostdinc++
Chris Lattner0c946412007-08-26 17:47:35 +0000940// FIXME: -imultilib
Reid Spencer5f016e22007-07-11 17:01:13 +0000941//
Chris Lattner64299f82008-01-10 01:53:41 +0000942// FIXME: -imacros
Reid Spencer5f016e22007-07-11 17:01:13 +0000943
944static llvm::cl::opt<bool>
945nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
946
947// Various command line options. These four add directories to each chain.
948static llvm::cl::list<std::string>
949F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
950 llvm::cl::desc("Add directory to framework include search path"));
951static llvm::cl::list<std::string>
952I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
953 llvm::cl::desc("Add directory to include search path"));
954static llvm::cl::list<std::string>
955idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
956 llvm::cl::desc("Add directory to AFTER include search path"));
957static llvm::cl::list<std::string>
958iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
959 llvm::cl::desc("Add directory to QUOTE include search path"));
960static llvm::cl::list<std::string>
961isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
962 llvm::cl::desc("Add directory to SYSTEM include search path"));
963
964// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
965static llvm::cl::list<std::string>
966iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
967 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
968static llvm::cl::list<std::string>
969iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
970 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
971static llvm::cl::list<std::string>
972iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
973 llvm::cl::Prefix,
974 llvm::cl::desc("Set directory to include search path with prefix"));
975
Chris Lattner0c946412007-08-26 17:47:35 +0000976static llvm::cl::opt<std::string>
977isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
978 llvm::cl::desc("Set the system root directory (usually /)"));
979
Reid Spencer5f016e22007-07-11 17:01:13 +0000980// Finally, implement the code that groks the options above.
Chris Lattner5f9eae52008-03-01 08:07:28 +0000981
Reid Spencer5f016e22007-07-11 17:01:13 +0000982/// InitializeIncludePaths - Process the -I options and set them in the
983/// HeaderSearch object.
Nico Weber0fca0222008-08-22 09:25:22 +0000984void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers,
985 FileManager &FM, const LangOptions &Lang) {
986 InitHeaderSearch Init(Headers, Verbose, isysroot);
987
Ted Kremenekf3721112008-05-31 00:27:00 +0000988 // Handle -I... and -F... options, walking the lists in parallel.
989 unsigned Iidx = 0, Fidx = 0;
990 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
991 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000992 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +0000993 ++Iidx;
994 } else {
Nico Weber0fca0222008-08-22 09:25:22 +0000995 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Ted Kremenekf3721112008-05-31 00:27:00 +0000996 ++Fidx;
997 }
998 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000999
Ted Kremenekf3721112008-05-31 00:27:00 +00001000 // Consume what's left from whatever list was longer.
1001 for (; Iidx != I_dirs.size(); ++Iidx)
Nico Weber0fca0222008-08-22 09:25:22 +00001002 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +00001003 for (; Fidx != F_dirs.size(); ++Fidx)
Nico Weber0fca0222008-08-22 09:25:22 +00001004 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001005
1006 // Handle -idirafter... options.
1007 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +00001008 Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After,
1009 false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001010
1011 // Handle -iquote... options.
1012 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +00001013 Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001014
1015 // Handle -isystem... options.
1016 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +00001017 Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001018
1019 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
1020 // parallel, processing the values in order of occurance to get the right
1021 // prefixes.
1022 {
1023 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
1024 unsigned iprefix_idx = 0;
1025 unsigned iwithprefix_idx = 0;
1026 unsigned iwithprefixbefore_idx = 0;
1027 bool iprefix_done = iprefix_vals.empty();
1028 bool iwithprefix_done = iwithprefix_vals.empty();
1029 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
1030 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
1031 if (!iprefix_done &&
1032 (iwithprefix_done ||
1033 iprefix_vals.getPosition(iprefix_idx) <
1034 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
1035 (iwithprefixbefore_done ||
1036 iprefix_vals.getPosition(iprefix_idx) <
1037 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
1038 Prefix = iprefix_vals[iprefix_idx];
1039 ++iprefix_idx;
1040 iprefix_done = iprefix_idx == iprefix_vals.size();
1041 } else if (!iwithprefix_done &&
1042 (iwithprefixbefore_done ||
1043 iwithprefix_vals.getPosition(iwithprefix_idx) <
1044 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
Nico Weber0fca0222008-08-22 09:25:22 +00001045 Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
1046 InitHeaderSearch::System, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 ++iwithprefix_idx;
1048 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
1049 } else {
Nico Weber0fca0222008-08-22 09:25:22 +00001050 Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
1051 InitHeaderSearch::Angled, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 ++iwithprefixbefore_idx;
1053 iwithprefixbefore_done =
1054 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
1055 }
1056 }
1057 }
Chris Lattner5f9eae52008-03-01 08:07:28 +00001058
Nico Weber0fca0222008-08-22 09:25:22 +00001059 Init.AddDefaultEnvVarPaths(Lang);
Chris Lattner5f9eae52008-03-01 08:07:28 +00001060
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001061 // Add the clang headers, which are relative to the clang driver.
1062 llvm::sys::Path MainExecutablePath =
Chris Lattner985e1822008-03-03 05:57:43 +00001063 llvm::sys::Path::GetMainExecutable(Argv0,
1064 (void*)(intptr_t)InitializeIncludePaths);
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001065 if (!MainExecutablePath.isEmpty()) {
1066 MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang
1067 MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin
1068 MainExecutablePath.appendComponent("Headers"); // Get foo/Headers
Nico Weber0fca0222008-08-22 09:25:22 +00001069 Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System,
1070 false, false, false);
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001071 }
1072
Nico Weber0fca0222008-08-22 09:25:22 +00001073 if (!nostdinc)
1074 Init.AddDefaultSystemIncludePaths(Lang);
Reid Spencer5f016e22007-07-11 17:01:13 +00001075
1076 // Now that we have collected all of the include paths, merge them all
1077 // together and tell the preprocessor about them.
1078
Nico Weber0fca0222008-08-22 09:25:22 +00001079 Init.Realize();
Reid Spencer5f016e22007-07-11 17:01:13 +00001080}
1081
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001082//===----------------------------------------------------------------------===//
1083// Driver PreprocessorFactory - For lazily generating preprocessors ...
1084//===----------------------------------------------------------------------===//
1085
1086namespace {
1087class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory {
Ted Kremenek339b9c22008-04-17 22:31:54 +00001088 const std::string &InFile;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001089 Diagnostic &Diags;
1090 const LangOptions &LangInfo;
1091 TargetInfo &Target;
1092 SourceManager &SourceMgr;
1093 HeaderSearch &HeaderInfo;
Ted Kremenek339b9c22008-04-17 22:31:54 +00001094 bool InitializeSourceMgr;
1095
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001096public:
Ted Kremenek339b9c22008-04-17 22:31:54 +00001097 DriverPreprocessorFactory(const std::string &infile,
1098 Diagnostic &diags, const LangOptions &opts,
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001099 TargetInfo &target, SourceManager &SM,
1100 HeaderSearch &Headers)
Ted Kremenek339b9c22008-04-17 22:31:54 +00001101 : InFile(infile), Diags(diags), LangInfo(opts), Target(target),
1102 SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {}
1103
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001104
1105 virtual ~DriverPreprocessorFactory() {}
1106
1107 virtual Preprocessor* CreatePreprocessor() {
Ted Kremenek339b9c22008-04-17 22:31:54 +00001108 Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target,
1109 SourceMgr, HeaderInfo);
1110
Chris Lattner51574ea2008-04-19 23:25:44 +00001111 if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) {
Ted Kremenek339b9c22008-04-17 22:31:54 +00001112 delete PP;
1113 return NULL;
1114 }
1115
Daniel Dunbar750c3582008-10-24 22:12:41 +00001116 /// FIXME: PP can only handle one callback
1117 if (ProgAction != PrintPreprocessedInput) {
1118 const char* ErrStr;
1119 bool DFG = CreateDependencyFileGen(PP, OutputFile, InFile, ErrStr);
1120 if (!DFG && ErrStr) {
Ted Kremenek9a30c242008-10-25 20:19:34 +00001121 fprintf(stderr, "%s", ErrStr);
Daniel Dunbar750c3582008-10-24 22:12:41 +00001122 return NULL;
1123 }
1124 }
1125
Ted Kremenek339b9c22008-04-17 22:31:54 +00001126 InitializeSourceMgr = false;
Ted Kremenek339b9c22008-04-17 22:31:54 +00001127 return PP;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001128 }
1129};
1130}
Reid Spencer5f016e22007-07-11 17:01:13 +00001131
Reid Spencer5f016e22007-07-11 17:01:13 +00001132//===----------------------------------------------------------------------===//
1133// Basic Parser driver
1134//===----------------------------------------------------------------------===//
1135
Chris Lattner51574ea2008-04-19 23:25:44 +00001136static void ParseFile(Preprocessor &PP, MinimalAction *PA) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 Parser P(PP, *PA);
Ted Kremenek95041a22007-12-19 22:51:13 +00001138 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001139
1140 // Parsing the specified input file.
1141 P.ParseTranslationUnit();
1142 delete PA;
1143}
1144
1145//===----------------------------------------------------------------------===//
Daniel Dunbar70f92432008-10-23 05:50:47 +00001146// Code generation options
1147//===----------------------------------------------------------------------===//
1148
1149static llvm::cl::opt<bool>
1150OptSize("Os",
1151 llvm::cl::desc("Optimize for size"));
1152
1153// It might be nice to add bounds to the CommandLine library directly.
1154struct OptLevelParser : public llvm::cl::parser<unsigned> {
1155 bool parse(llvm::cl::Option &O, const char *ArgName,
1156 const std::string &Arg, unsigned &Val) {
1157 if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
1158 return true;
1159 // FIXME: Support -O4.
1160 if (Val > 3)
1161 return O.error(": '" + Arg + "' invalid optimization level!");
1162 return false;
1163 }
1164};
1165static llvm::cl::opt<unsigned, false, OptLevelParser>
1166OptLevel("O", llvm::cl::Prefix,
1167 llvm::cl::desc("Optimization level"),
1168 llvm::cl::init(0));
1169
1170static void InitializeCompileOptions(CompileOptions &Opts) {
Daniel Dunbar70f92432008-10-23 05:50:47 +00001171 Opts.OptimizeSize = OptSize;
Daniel Dunbarac7ffe02008-10-29 07:56:11 +00001172 if (OptSize) {
1173 // -Os implies -O2
1174 // FIXME: Diagnose conflicting options.
1175 Opts.OptimizationLevel = 2;
1176 } else {
1177 Opts.OptimizationLevel = OptLevel;
1178 }
Daniel Dunbar8e8f3b72008-10-29 03:42:18 +00001179
1180 // FIXME: There are llvm-gcc options to control these selectively.
1181 Opts.InlineFunctions = (Opts.OptimizationLevel > 1);
1182 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
1183 Opts.SimplifyLibCalls = 1;
Daniel Dunbardd913e52008-10-31 09:34:21 +00001184
1185#ifdef NDEBUG
1186 Opts.VerifyModule = 0;
1187#endif
Daniel Dunbar70f92432008-10-23 05:50:47 +00001188}
1189
1190//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001191// Main driver
1192//===----------------------------------------------------------------------===//
1193
Ted Kremenekdb094a22007-12-05 18:27:04 +00001194/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
1195/// action. These consumers can operate on both ASTs that are freshly
1196/// parsed from source files as well as those deserialized from Bitcode.
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001197static ASTConsumer* CreateASTConsumer(const std::string& InFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001198 Diagnostic& Diag, FileManager& FileMgr,
Chris Lattnere66b65c2008-02-06 01:42:25 +00001199 const LangOptions& LangOpts,
Chris Lattner3245a0a2008-04-16 06:11:58 +00001200 Preprocessor *PP,
Ted Kremenek815c78f2008-08-05 18:50:11 +00001201 PreprocessorFactory *PPF) {
Ted Kremenekdb094a22007-12-05 18:27:04 +00001202 switch (ProgAction) {
1203 default:
1204 return NULL;
1205
1206 case ASTPrint:
1207 return CreateASTPrinter();
1208
1209 case ASTDump:
1210 return CreateASTDumper();
1211
1212 case ASTView:
Ted Kremenek6a340832008-03-18 21:19:49 +00001213 return CreateASTViewer();
1214
1215 case EmitHTML:
Ted Kremenek339b9c22008-04-17 22:31:54 +00001216 return CreateHTMLPrinter(OutputFile, Diag, PP, PPF);
Ted Kremenek902141f2008-07-02 18:23:21 +00001217
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001218 case InheritanceView:
1219 return CreateInheritanceViewer(InheritanceViewCls);
1220
Ted Kremenekdb094a22007-12-05 18:27:04 +00001221 case TestSerialization:
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001222 return CreateSerializationTest(Diag, FileMgr);
Ted Kremenekdb094a22007-12-05 18:27:04 +00001223
Daniel Dunbard69bacc2008-10-21 23:49:24 +00001224 case EmitAssembly:
Ted Kremenekdb094a22007-12-05 18:27:04 +00001225 case EmitLLVM:
Daniel Dunbar70f92432008-10-23 05:50:47 +00001226 case EmitBC: {
1227 BackendAction Act;
1228 if (ProgAction == EmitAssembly) {
1229 Act = Backend_EmitAssembly;
1230 } else if (ProgAction == EmitLLVM) {
1231 Act = Backend_EmitLL;
1232 } else {
1233 Act = Backend_EmitBC;
1234 }
1235 CompileOptions Opts;
1236 InitializeCompileOptions(Opts);
1237 return CreateBackendConsumer(Act, Diag, LangOpts, Opts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +00001238 InFile, OutputFile, GenerateDebugInfo);
Daniel Dunbar70f92432008-10-23 05:50:47 +00001239 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +00001240
Ted Kremenek3910c7c2007-12-19 17:25:59 +00001241 case SerializeAST:
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001242 // FIXME: Allow user to tailor where the file is written.
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001243 return CreateASTSerializer(InFile, OutputFile, Diag);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001244
Steve Naroffb29b4272008-04-14 22:03:09 +00001245 case RewriteObjC:
Chris Lattnerc68ab772008-03-22 00:08:40 +00001246 return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts);
Steve Naroff13188952008-09-18 14:10:13 +00001247
1248 case RewriteBlocks:
1249 return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts);
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001250
1251 case RunAnalysis:
1252 assert (!AnalysisList.empty());
1253 return CreateAnalysisConsumer(&AnalysisList[0],
1254 &AnalysisList[0]+AnalysisList.size(),
Ted Kremenek4fc82c82008-11-03 23:18:07 +00001255 AnalysisStoreOpt, AnalysisDiagOpt,
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001256 Diag, PP, PPF, LangOpts,
1257 AnalyzeSpecificFunction,
Ted Kremenekf8ce6992008-08-27 22:31:43 +00001258 OutputFile, VisualizeEGDot, VisualizeEGUbi,
1259 TrimGraph, AnalyzeAll);
Ted Kremenekdb094a22007-12-05 18:27:04 +00001260 }
1261}
1262
Reid Spencer5f016e22007-07-11 17:01:13 +00001263/// ProcessInputFile - Process a single input file with the specified state.
1264///
Ted Kremenek339b9c22008-04-17 22:31:54 +00001265static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
Ted Kremenek85888962008-10-21 00:54:44 +00001266 const std::string &InFile, ProgActions PA) {
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001267 llvm::OwningPtr<ASTConsumer> Consumer;
Chris Lattnerbd247762007-07-22 06:05:44 +00001268 bool ClearSourceMgr = false;
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001269
Ted Kremenek85888962008-10-21 00:54:44 +00001270 switch (PA) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 default:
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001272 Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(),
1273 PP.getFileManager(), PP.getLangOptions(),
1274 &PP, &PPF));
Ted Kremenekdb094a22007-12-05 18:27:04 +00001275
1276 if (!Consumer) {
1277 fprintf(stderr, "Unexpected program action!\n");
Daniel Dunbarb0adbba2008-10-04 23:42:49 +00001278 HadErrors = true;
Ted Kremenekdb094a22007-12-05 18:27:04 +00001279 return;
1280 }
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001281
Ted Kremenekdb094a22007-12-05 18:27:04 +00001282 break;
1283
Chris Lattnerc106c102008-10-12 05:03:36 +00001284 case DumpRawTokens: {
1285 SourceManager &SM = PP.getSourceManager();
1286 std::pair<const char*,const char*> File =
1287 SM.getBufferData(SM.getMainFileID());
1288 // Start lexing the specified input file.
1289 Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0),
1290 PP.getLangOptions(), File.first, File.second);
1291 RawLex.SetKeepWhitespaceMode(true);
1292
1293 Token RawTok;
1294
1295 RawLex.LexFromRawLexer(RawTok);
1296 while (RawTok.isNot(tok::eof)) {
1297 PP.DumpToken(RawTok, true);
1298 fprintf(stderr, "\n");
1299 RawLex.LexFromRawLexer(RawTok);
1300 }
1301 ClearSourceMgr = true;
1302 break;
1303 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 case DumpTokens: { // Token dump mode.
Chris Lattnerd2177732007-07-20 16:59:19 +00001305 Token Tok;
Chris Lattnerc106c102008-10-12 05:03:36 +00001306 // Start preprocessing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001307 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 do {
1309 PP.Lex(Tok);
1310 PP.DumpToken(Tok, true);
1311 fprintf(stderr, "\n");
Chris Lattner057aaf62007-10-09 18:03:42 +00001312 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001313 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 break;
1315 }
1316 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
Chris Lattnerd2177732007-07-20 16:59:19 +00001317 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001319 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 do {
1321 PP.Lex(Tok);
Chris Lattner057aaf62007-10-09 18:03:42 +00001322 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001323 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 break;
1325 }
Ted Kremenek85888962008-10-21 00:54:44 +00001326
1327 case GeneratePCH: {
1328 CacheTokens(PP, OutputFile);
1329 ClearSourceMgr = true;
1330 break;
1331 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001332
1333 case PrintPreprocessedInput: // -E mode.
Chris Lattnere988bc22008-01-27 23:55:11 +00001334 DoPrintPreprocessedInput(PP, OutputFile);
Chris Lattnerbd247762007-07-22 06:05:44 +00001335 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001337
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 case ParseNoop: // -parse-noop
Daniel Dunbare10b0f22008-10-31 08:56:51 +00001339 ParseFile(PP, new MinimalAction(PP));
Chris Lattnerbd247762007-07-22 06:05:44 +00001340 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 break;
1342
1343 case ParsePrintCallbacks:
Daniel Dunbare10b0f22008-10-31 08:56:51 +00001344 ParseFile(PP, CreatePrintParserActionsAction(PP));
Chris Lattnerbd247762007-07-22 06:05:44 +00001345 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001346 break;
Ted Kremenek44579782007-09-25 18:37:20 +00001347
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001348 case ParseSyntaxOnly: // -fsyntax-only
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001349 Consumer.reset(new ASTConsumer());
Ted Kremenek2bf55142007-09-17 20:49:30 +00001350 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001351
1352 case RewriteMacros:
Chris Lattner09510522008-05-09 22:43:24 +00001353 RewriteMacrosInInput(PP, InFile, OutputFile);
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001354 ClearSourceMgr = true;
1355 break;
Chris Lattnerb13c5ee2008-10-12 05:29:20 +00001356
1357 case RewriteTest:
1358 DoRewriteTest(PP, InFile, OutputFile);
1359 ClearSourceMgr = true;
1360 break;
Chris Lattner580980b2007-09-16 19:46:59 +00001361 }
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001362
Daniel Dunbar879c3ea2008-10-27 22:03:52 +00001363 if (Consumer)
Daniel Dunbard3db4012008-10-16 16:54:18 +00001364 ParseAST(PP, Consumer.get(), Stats, !DisableFree);
Daniel Dunbar879c3ea2008-10-27 22:03:52 +00001365
1366 if (VerifyDiagnostics)
Daniel Dunbar276373d2008-10-27 22:10:13 +00001367 if (CheckDiagnostics(PP))
1368 exit(1);
Chris Lattnere66b65c2008-02-06 01:42:25 +00001369
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 if (Stats) {
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001371 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +00001372 PP.PrintStats();
1373 PP.getIdentifierTable().PrintStats();
Chris Lattnerdee73592007-12-15 20:48:40 +00001374 PP.getHeaderSearchInfo().PrintStats();
Chris Lattnerbd247762007-07-22 06:05:44 +00001375 if (ClearSourceMgr)
Chris Lattnerdee73592007-12-15 20:48:40 +00001376 PP.getSourceManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 fprintf(stderr, "\n");
1378 }
Chris Lattnerbd247762007-07-22 06:05:44 +00001379
1380 // For a multi-file compilation, some things are ok with nuking the source
1381 // manager tables, other require stable fileid/macroid's across multiple
1382 // files.
Chris Lattnerdee73592007-12-15 20:48:40 +00001383 if (ClearSourceMgr)
1384 PP.getSourceManager().clearIDTables();
Daniel Dunbard68ba0e2008-11-11 06:35:39 +00001385
1386 if (DisableFree)
1387 Consumer.take();
Reid Spencer5f016e22007-07-11 17:01:13 +00001388}
1389
Ted Kremenek20e97482007-12-12 23:41:08 +00001390static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag,
1391 FileManager& FileMgr) {
1392
1393 if (VerifyDiagnostics) {
1394 fprintf(stderr, "-verify does not yet work with serialized ASTs.\n");
1395 exit (1);
1396 }
1397
1398 llvm::sys::Path Filename(InFile);
1399
1400 if (!Filename.isValid()) {
1401 fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str());
1402 exit (1);
1403 }
1404
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001405 llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr));
Ted Kremenekfe4e0152007-12-13 18:11:11 +00001406
1407 if (!TU) {
1408 fprintf(stderr, "error: file '%s' could not be deserialized\n",
1409 InFile.c_str());
1410 exit (1);
1411 }
1412
Ted Kremenek63ea8632007-12-19 19:27:38 +00001413 // Observe that we use the source file name stored in the deserialized
1414 // translation unit, rather than InFile.
Ted Kremenekee533642007-12-20 19:47:16 +00001415 llvm::OwningPtr<ASTConsumer>
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001416 Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(),
Ted Kremenek815c78f2008-08-05 18:50:11 +00001417 0, 0));
Nico Weber7bfaaae2008-08-10 19:59:06 +00001418
Ted Kremenek20e97482007-12-12 23:41:08 +00001419 if (!Consumer) {
1420 fprintf(stderr, "Unsupported program action with serialized ASTs!\n");
1421 exit (1);
1422 }
Nico Weber7bfaaae2008-08-10 19:59:06 +00001423
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001424 Consumer->Initialize(TU->getContext());
Nico Weber7bfaaae2008-08-10 19:59:06 +00001425
Chris Lattnere66b65c2008-02-06 01:42:25 +00001426 // FIXME: We need to inform Consumer about completed TagDecls as well.
Ted Kremenek20e97482007-12-12 23:41:08 +00001427 for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
1428 Consumer->HandleTopLevelDecl(*I);
Ted Kremenek20e97482007-12-12 23:41:08 +00001429}
1430
1431
Reid Spencer5f016e22007-07-11 17:01:13 +00001432static llvm::cl::list<std::string>
1433InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1434
Ted Kremenek20e97482007-12-12 23:41:08 +00001435static bool isSerializedFile(const std::string& InFile) {
1436 if (InFile.size() < 4)
1437 return false;
1438
1439 const char* s = InFile.c_str()+InFile.size()-4;
1440
1441 return s[0] == '.' &&
1442 s[1] == 'a' &&
1443 s[2] == 's' &&
1444 s[3] == 't';
1445}
1446
Reid Spencer5f016e22007-07-11 17:01:13 +00001447
1448int main(int argc, char **argv) {
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001449 llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n");
Reid Spencer5f016e22007-07-11 17:01:13 +00001450 llvm::sys::PrintStackTraceOnErrorSignal();
1451
1452 // If no input was specified, read from stdin.
1453 if (InputFilenames.empty())
1454 InputFilenames.push_back("-");
Ted Kremenek31e703b2007-12-11 23:28:38 +00001455
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 // Create a file manager object to provide access to and cache the filesystem.
1457 FileManager FileMgr;
1458
Ted Kremenek31e703b2007-12-11 23:28:38 +00001459 // Create the diagnostic client for reporting errors or for
1460 // implementing -verify.
Nico Weber7bfaaae2008-08-10 19:59:06 +00001461 DiagnosticClient* TextDiagClient = 0;
Ted Kremenek88f5cde2008-03-27 06:17:42 +00001462
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001463 if (!VerifyDiagnostics) {
1464 // Print diagnostics to stderr by default.
Chris Lattnera03a5b52008-11-19 06:56:25 +00001465 TextDiagClient = new TextDiagnosticPrinter(llvm::errs(),
1466 !NoShowColumn,
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001467 !NoCaretDiagnostics);
1468 } else {
1469 // When checking diagnostics, just buffer them up.
1470 TextDiagClient = new TextDiagnosticBuffer();
1471
1472 if (InputFilenames.size() != 1) {
1473 fprintf(stderr,
1474 "-verify only works on single input files for now.\n");
1475 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 }
1477 }
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001478
Reid Spencer5f016e22007-07-11 17:01:13 +00001479 // Configure our handling of diagnostics.
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001480 llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient);
1481 Diagnostic Diags(DiagClient.get());
Ted Kremenek31e703b2007-12-11 23:28:38 +00001482 InitializeDiagnostics(Diags);
1483
Chris Lattner4f037832007-12-05 23:24:17 +00001484 // -I- is a deprecated GCC feature, scan for it and reject it.
1485 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
1486 if (I_dirs[i] == "-") {
Chris Lattner5917fe12008-11-18 05:05:28 +00001487 Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported);
Chris Lattner4f037832007-12-05 23:24:17 +00001488 I_dirs.erase(I_dirs.begin()+i);
1489 --i;
1490 }
1491 }
Chris Lattner11215192008-03-14 06:12:05 +00001492
1493 // Get information about the target being compiled for.
1494 std::string Triple = CreateTargetTriple();
Ted Kremenek7a08e282008-08-07 18:13:12 +00001495 llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple));
1496
Chris Lattner11215192008-03-14 06:12:05 +00001497 if (Target == 0) {
1498 fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
1499 Triple.c_str());
1500 fprintf(stderr, "Please use -triple or -arch.\n");
1501 exit(1);
1502 }
Chris Lattner4f037832007-12-05 23:24:17 +00001503
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001504 // Are we invoking one or more source analyses?
1505 if (!AnalysisList.empty() && ProgAction == ParseSyntaxOnly)
1506 ProgAction = RunAnalysis;
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001507 else if (!InheritanceViewCls.empty()) // C++ visualization?
1508 ProgAction = InheritanceView;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001509
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001510 llvm::OwningPtr<SourceManager> SourceMgr;
1511
Reid Spencer5f016e22007-07-11 17:01:13 +00001512 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenek31e703b2007-12-11 23:28:38 +00001513 const std::string &InFile = InputFilenames[i];
Ted Kremenek31e703b2007-12-11 23:28:38 +00001514
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001515 if (isSerializedFile(InFile)) {
1516 Diags.setClient(TextDiagClient);
Ted Kremenek20e97482007-12-12 23:41:08 +00001517 ProcessSerializedFile(InFile,Diags,FileMgr);
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001518 }
Ted Kremenek20e97482007-12-12 23:41:08 +00001519 else {
1520 /// Create a SourceManager object. This tracks and owns all the file
1521 /// buffers allocated to a translation unit.
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001522 if (!SourceMgr)
1523 SourceMgr.reset(new SourceManager());
1524 else
1525 SourceMgr->clearIDTables();
Ted Kremenek31e703b2007-12-11 23:28:38 +00001526
Ted Kremenek20e97482007-12-12 23:41:08 +00001527 // Initialize language options, inferring file types from input filenames.
1528 LangOptions LangInfo;
1529 InitializeBaseLanguage();
1530 LangKind LK = GetLanguage(InFile);
Ted Kremenek85888962008-10-21 00:54:44 +00001531 bool PCH = InitializeLangOptions(LangInfo, LK);
Ted Kremenek01d9dbf2008-04-29 04:37:03 +00001532 InitializeGCMode(LangInfo);
Chris Lattner8fc4dfb2008-12-04 22:54:33 +00001533 InitializeLanguageStandard(LangInfo, LK, Target.get());
Ted Kremenek85888962008-10-21 00:54:44 +00001534
Ted Kremenek20e97482007-12-12 23:41:08 +00001535 // Process the -I options and set them in the HeaderInfo.
1536 HeaderSearch HeaderInfo(FileMgr);
Ted Kremenekc68ecb52008-06-06 01:47:30 +00001537
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001538 InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001539
Ted Kremenek20e97482007-12-12 23:41:08 +00001540 // Set up the preprocessor with these options.
Ted Kremenek339b9c22008-04-17 22:31:54 +00001541 DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target,
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001542 *SourceMgr.get(), HeaderInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001543
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001544 llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor());
1545
Ted Kremenek339b9c22008-04-17 22:31:54 +00001546 if (!PP)
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001547 continue;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001548
1549 // Create the HTMLDiagnosticsClient if we are using one. Otherwise,
1550 // always reset to using TextDiagClient.
1551 llvm::OwningPtr<DiagnosticClient> TmpClient;
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001552
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001553 if (!HTMLDiag.empty()) {
1554 TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(),
1555 &PPFactory));
1556 Diags.setClient(TmpClient.get());
1557 }
1558 else
1559 Diags.setClient(TextDiagClient);
1560
1561 // Process the source file.
Ted Kremenek85888962008-10-21 00:54:44 +00001562 ProcessInputFile(*PP, PPFactory, InFile, PCH ? GeneratePCH : ProgAction);
1563
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001564 HeaderInfo.ClearFileInfo();
Ted Kremenek20e97482007-12-12 23:41:08 +00001565
1566 if (Stats)
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001567 SourceMgr->PrintStats();
Ted Kremenek20e97482007-12-12 23:41:08 +00001568 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001569 }
Chris Lattner11215192008-03-14 06:12:05 +00001570
Ted Kremenek7a08e282008-08-07 18:13:12 +00001571 if (unsigned NumDiagnostics = Diags.getNumDiagnostics())
Reid Spencer5f016e22007-07-11 17:01:13 +00001572 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1573 (NumDiagnostics == 1 ? "" : "s"));
1574
1575 if (Stats) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001576 FileMgr.PrintStats();
1577 fprintf(stderr, "\n");
1578 }
1579
Daniel Dunbar276373d2008-10-27 22:10:13 +00001580 // If verifying diagnostics and we reached here, all is well.
1581 if (VerifyDiagnostics)
1582 return 0;
1583
Daniel Dunbar524b86f2008-10-28 00:38:08 +00001584 // Managed static deconstruction. Useful for making things like
1585 // -time-passes usable.
1586 llvm::llvm_shutdown();
1587
Daniel Dunbarb0adbba2008-10-04 23:42:49 +00001588 return HadErrors || (Diags.getNumErrors() != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001589}