blob: 08eef52acc585d0acba8be87162108faec3d5c03 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- clang.cpp - C-Language Front-end ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This utility may be invoked in the following manner:
11// clang --help - Output help info.
12// clang [options] - Read from stdin.
13// clang [options] file - Read from "file".
14// clang [options] file1 file2 - Read these files.
15//
16//===----------------------------------------------------------------------===//
17//
18// TODO: Options to support:
19//
20// -ffatal-errors
21// -ftabstop=width
22//
23//===----------------------------------------------------------------------===//
24
25#include "clang.h"
Chris Lattnereb8c9632007-10-07 06:04:32 +000026#include "ASTConsumers.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027#include "TextDiagnosticBuffer.h"
28#include "TextDiagnosticPrinter.h"
Ted Kremenekac881932007-12-18 21:34:28 +000029#include "clang/AST/TranslationUnit.h"
Chris Lattnerf5e9db02008-02-06 02:01:47 +000030#include "clang/CodeGen/ModuleBuilder.h"
Chris Lattner0bed6ec2008-02-06 00:23:21 +000031#include "clang/Sema/ParseAST.h"
Chris Lattner1cc01712007-09-15 22:56:56 +000032#include "clang/AST/ASTConsumer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000033#include "clang/Parse/Parser.h"
34#include "clang/Lex/HeaderSearch.h"
35#include "clang/Basic/FileManager.h"
36#include "clang/Basic/SourceManager.h"
37#include "clang/Basic/TargetInfo.h"
Chris Lattner8d72ee02008-02-06 01:42:25 +000038#include "llvm/Module.h"
Chris Lattnerac139d22007-12-15 23:20:07 +000039#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner8d72ee02008-02-06 01:42:25 +000040#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner4b009652007-07-25 00:24:17 +000041#include "llvm/Support/CommandLine.h"
42#include "llvm/Support/MemoryBuffer.h"
43#include "llvm/System/Signals.h"
Ted Kremenek40499482007-12-03 22:06:55 +000044#include "llvm/Config/config.h"
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +000045#include "llvm/ADT/OwningPtr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000046#include <memory>
Chris Lattner8d72ee02008-02-06 01:42:25 +000047#include <fstream>
Chris Lattner4b009652007-07-25 00:24:17 +000048using namespace clang;
49
50//===----------------------------------------------------------------------===//
51// Global options.
52//===----------------------------------------------------------------------===//
53
54static llvm::cl::opt<bool>
55Verbose("v", llvm::cl::desc("Enable verbose output"));
56static llvm::cl::opt<bool>
Nate Begeman6acbedd2007-12-30 01:38:50 +000057Stats("print-stats",
58 llvm::cl::desc("Print performance metrics and statistics"));
Chris Lattner4b009652007-07-25 00:24:17 +000059
60enum ProgActions {
Chris Lattnerb429ae42007-10-11 00:43:27 +000061 RewriteTest, // Rewriter testing stuff.
Chris Lattner4b009652007-07-25 00:24:17 +000062 EmitLLVM, // Emit a .ll file.
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +000063 EmitBC, // Emit a .bc file.
Ted Kremenek397de012007-12-13 00:37:31 +000064 SerializeAST, // Emit a .ast file.
Chris Lattner4045a8a2007-10-11 00:18:28 +000065 ASTPrint, // Parse ASTs and print them.
66 ASTDump, // Parse ASTs and dump them.
67 ASTView, // Parse ASTs and view them in Graphviz.
Ted Kremenek97f75312007-08-21 21:42:03 +000068 ParseCFGDump, // Parse ASTS. Build CFGs. Print CFGs.
Ted Kremeneke805c4a2007-09-06 23:00:42 +000069 ParseCFGView, // Parse ASTS. Build CFGs. View CFGs.
Ted Kremenekaa04c512007-09-06 00:17:54 +000070 AnalysisLiveVariables, // Print results of live-variable analysis.
Ted Kremenek1da5b142008-02-15 00:35:38 +000071 AnalysisGRSimpleVals, // Perform graph-reachability constant prop.
72 AnalysisGRSimpleValsView, // Visualize results of path-sens. analysis.
Ted Kremeneke805c4a2007-09-06 23:00:42 +000073 WarnDeadStores, // Run DeadStores checker on parsed ASTs.
Ted Kremenek0841c702007-09-25 18:37:20 +000074 WarnDeadStoresCheck, // Check diagnostics for "DeadStores".
Ted Kremenek0a03ce62007-09-17 20:49:30 +000075 WarnUninitVals, // Run UnitializedVariables checker.
Ted Kremenek221bb8d2007-10-16 23:37:27 +000076 TestSerialization, // Run experimental serialization code.
Chris Lattner4b009652007-07-25 00:24:17 +000077 ParsePrintCallbacks, // Parse and print each callback.
78 ParseSyntaxOnly, // Parse and perform semantic analysis.
79 ParseNoop, // Parse with noop callbacks.
80 RunPreprocessorOnly, // Just lex, no output.
81 PrintPreprocessedInput, // -E mode.
82 DumpTokens // Token dump mode.
83};
84
85static llvm::cl::opt<ProgActions>
86ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
87 llvm::cl::init(ParseSyntaxOnly),
88 llvm::cl::values(
89 clEnumValN(RunPreprocessorOnly, "Eonly",
90 "Just run preprocessor, no output (for timings)"),
91 clEnumValN(PrintPreprocessedInput, "E",
92 "Run preprocessor, emit preprocessed file"),
93 clEnumValN(DumpTokens, "dumptokens",
94 "Run preprocessor, dump internal rep of tokens"),
95 clEnumValN(ParseNoop, "parse-noop",
96 "Run parser with noop callbacks (for timings)"),
97 clEnumValN(ParseSyntaxOnly, "fsyntax-only",
98 "Run parser and perform semantic analysis"),
99 clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
100 "Run parser and print each callback invoked"),
Chris Lattner4045a8a2007-10-11 00:18:28 +0000101 clEnumValN(ASTPrint, "ast-print",
102 "Build ASTs and then pretty-print them"),
103 clEnumValN(ASTDump, "ast-dump",
104 "Build ASTs and then debug dump them"),
Chris Lattner664dd082007-10-11 00:37:43 +0000105 clEnumValN(ASTView, "ast-view",
Chris Lattner4045a8a2007-10-11 00:18:28 +0000106 "Build ASTs and view them with GraphViz."),
Ted Kremenek97f75312007-08-21 21:42:03 +0000107 clEnumValN(ParseCFGDump, "dump-cfg",
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000108 "Run parser, then build and print CFGs."),
109 clEnumValN(ParseCFGView, "view-cfg",
Ted Kremenekaa04c512007-09-06 00:17:54 +0000110 "Run parser, then build and view CFGs with Graphviz."),
111 clEnumValN(AnalysisLiveVariables, "dump-live-variables",
Ted Kremenek05334682007-09-06 21:26:58 +0000112 "Print results of live variable analysis."),
Ted Kremenek945fb562007-09-25 18:05:45 +0000113 clEnumValN(WarnDeadStores, "warn-dead-stores",
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000114 "Flag warnings of stores to dead variables."),
Ted Kremenek945fb562007-09-25 18:05:45 +0000115 clEnumValN(WarnUninitVals, "warn-uninit-values",
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000116 "Flag warnings of uses of unitialized variables."),
Ted Kremenek3862eb12008-02-14 22:36:46 +0000117 clEnumValN(AnalysisGRSimpleVals, "grsimple",
Chris Lattner2528b5e2008-01-10 01:41:55 +0000118 "Perform path-sensitive constant propagation."),
Ted Kremenek1da5b142008-02-15 00:35:38 +0000119 clEnumValN(AnalysisGRSimpleValsView, "grsimple-view",
120 "View results of path-sensitive constant propagation."),
Ted Kremenek221bb8d2007-10-16 23:37:27 +0000121 clEnumValN(TestSerialization, "test-pickling",
122 "Run prototype serializtion code."),
Chris Lattner4b009652007-07-25 00:24:17 +0000123 clEnumValN(EmitLLVM, "emit-llvm",
Ted Kremenek05334682007-09-06 21:26:58 +0000124 "Build ASTs then convert to LLVM, emit .ll file"),
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000125 clEnumValN(EmitBC, "emit-llvm-bc",
126 "Build ASTs then convert to LLVM, emit .bc file"),
Ted Kremenekd01eae62007-12-19 19:47:59 +0000127 clEnumValN(SerializeAST, "serialize",
Ted Kremenek397de012007-12-13 00:37:31 +0000128 "Build ASTs and emit .ast file"),
Chris Lattnerb429ae42007-10-11 00:43:27 +0000129 clEnumValN(RewriteTest, "rewrite-test",
130 "Playground for the code rewriter"),
Chris Lattner4b009652007-07-25 00:24:17 +0000131 clEnumValEnd));
132
Ted Kremenekd01eae62007-12-19 19:47:59 +0000133
134static llvm::cl::opt<std::string>
135OutputFile("o",
Ted Kremenek09b3f0d2007-12-19 19:50:41 +0000136 llvm::cl::value_desc("path"),
Ted Kremenekd01eae62007-12-19 19:47:59 +0000137 llvm::cl::desc("Specify output file (for --serialize, this is a directory)"));
138
Ted Kremenek10389cf2007-09-26 19:42:19 +0000139static llvm::cl::opt<bool>
140VerifyDiagnostics("verify",
141 llvm::cl::desc("Verify emitted diagnostics and warnings."));
142
Chris Lattner4b009652007-07-25 00:24:17 +0000143//===----------------------------------------------------------------------===//
144// Language Options
145//===----------------------------------------------------------------------===//
146
147enum LangKind {
148 langkind_unspecified,
149 langkind_c,
150 langkind_c_cpp,
151 langkind_cxx,
152 langkind_cxx_cpp,
153 langkind_objc,
154 langkind_objc_cpp,
155 langkind_objcxx,
156 langkind_objcxx_cpp
157};
158
159/* TODO: GCC also accepts:
160 c-header c++-header objective-c-header objective-c++-header
161 assembler assembler-with-cpp
162 ada, f77*, ratfor (!), f95, java, treelang
163 */
164static llvm::cl::opt<LangKind>
165BaseLang("x", llvm::cl::desc("Base language to compile"),
166 llvm::cl::init(langkind_unspecified),
167 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
168 clEnumValN(langkind_cxx, "c++", "C++"),
169 clEnumValN(langkind_objc, "objective-c", "Objective C"),
170 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
171 clEnumValN(langkind_c_cpp, "c-cpp-output",
172 "Preprocessed C"),
173 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
174 "Preprocessed C++"),
175 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
176 "Preprocessed Objective C"),
177 clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output",
178 "Preprocessed Objective C++"),
179 clEnumValEnd));
180
181static llvm::cl::opt<bool>
182LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
183 llvm::cl::Hidden);
184static llvm::cl::opt<bool>
185LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
186 llvm::cl::Hidden);
187
Ted Kremenek11ad8952007-12-05 23:49:08 +0000188/// InitializeBaseLanguage - Handle the -x foo options.
189static void InitializeBaseLanguage() {
190 if (LangObjC)
191 BaseLang = langkind_objc;
192 else if (LangObjCXX)
193 BaseLang = langkind_objcxx;
194}
195
196static LangKind GetLanguage(const std::string &Filename) {
197 if (BaseLang != langkind_unspecified)
198 return BaseLang;
199
200 std::string::size_type DotPos = Filename.rfind('.');
201
202 if (DotPos == std::string::npos) {
203 BaseLang = langkind_c; // Default to C if no extension.
Chris Lattner4eac0502008-01-04 19:12:28 +0000204 return langkind_c;
Chris Lattner4b009652007-07-25 00:24:17 +0000205 }
206
Ted Kremenek11ad8952007-12-05 23:49:08 +0000207 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
208 // C header: .h
209 // C++ header: .hh or .H;
210 // assembler no preprocessing: .s
211 // assembler: .S
212 if (Ext == "c")
213 return langkind_c;
214 else if (Ext == "i")
215 return langkind_c_cpp;
216 else if (Ext == "ii")
217 return langkind_cxx_cpp;
218 else if (Ext == "m")
219 return langkind_objc;
220 else if (Ext == "mi")
221 return langkind_objc_cpp;
222 else if (Ext == "mm" || Ext == "M")
223 return langkind_objcxx;
224 else if (Ext == "mii")
225 return langkind_objcxx_cpp;
226 else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
227 Ext == "c++" || Ext == "cp" || Ext == "cxx")
228 return langkind_cxx;
229 else
230 return langkind_c;
231}
232
233
234static void InitializeLangOptions(LangOptions &Options, LangKind LK) {
Chris Lattner4b009652007-07-25 00:24:17 +0000235 // FIXME: implement -fpreprocessed mode.
236 bool NoPreprocess = false;
237
Ted Kremenek11ad8952007-12-05 23:49:08 +0000238 switch (LK) {
Chris Lattner4b009652007-07-25 00:24:17 +0000239 default: assert(0 && "Unknown language kind!");
240 case langkind_c_cpp:
241 NoPreprocess = true;
242 // FALLTHROUGH
243 case langkind_c:
244 break;
245 case langkind_cxx_cpp:
246 NoPreprocess = true;
247 // FALLTHROUGH
248 case langkind_cxx:
249 Options.CPlusPlus = 1;
250 break;
251 case langkind_objc_cpp:
252 NoPreprocess = true;
253 // FALLTHROUGH
254 case langkind_objc:
255 Options.ObjC1 = Options.ObjC2 = 1;
256 break;
257 case langkind_objcxx_cpp:
258 NoPreprocess = true;
259 // FALLTHROUGH
260 case langkind_objcxx:
261 Options.ObjC1 = Options.ObjC2 = 1;
262 Options.CPlusPlus = 1;
263 break;
264 }
265}
266
267/// LangStds - Language standards we support.
268enum LangStds {
269 lang_unspecified,
270 lang_c89, lang_c94, lang_c99,
271 lang_gnu89, lang_gnu99,
272 lang_cxx98, lang_gnucxx98,
273 lang_cxx0x, lang_gnucxx0x
274};
275
276static llvm::cl::opt<LangStds>
277LangStd("std", llvm::cl::desc("Language standard to compile for"),
278 llvm::cl::init(lang_unspecified),
279 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
280 clEnumValN(lang_c89, "c90", "ISO C 1990"),
281 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
282 clEnumValN(lang_c94, "iso9899:199409",
283 "ISO C 1990 with amendment 1"),
284 clEnumValN(lang_c99, "c99", "ISO C 1999"),
285// clEnumValN(lang_c99, "c9x", "ISO C 1999"),
286 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
287// clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
288 clEnumValN(lang_gnu89, "gnu89",
289 "ISO C 1990 with GNU extensions (default for C)"),
290 clEnumValN(lang_gnu99, "gnu99",
291 "ISO C 1999 with GNU extensions"),
292 clEnumValN(lang_gnu99, "gnu9x",
293 "ISO C 1999 with GNU extensions"),
294 clEnumValN(lang_cxx98, "c++98",
295 "ISO C++ 1998 with amendments"),
296 clEnumValN(lang_gnucxx98, "gnu++98",
297 "ISO C++ 1998 with amendments and GNU "
298 "extensions (default for C++)"),
299 clEnumValN(lang_cxx0x, "c++0x",
300 "Upcoming ISO C++ 200x with amendments"),
301 clEnumValN(lang_gnucxx0x, "gnu++0x",
302 "Upcoming ISO C++ 200x with amendments and GNU "
303 "extensions (default for C++)"),
304 clEnumValEnd));
305
306static llvm::cl::opt<bool>
307NoOperatorNames("fno-operator-names",
308 llvm::cl::desc("Do not treat C++ operator name keywords as "
309 "synonyms for operators"));
310
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000311static llvm::cl::opt<bool>
312PascalStrings("fpascal-strings",
313 llvm::cl::desc("Recognize and construct Pascal-style "
314 "string literals"));
Steve Naroff73a07032008-02-07 03:50:06 +0000315
316static llvm::cl::opt<bool>
317MSExtensions("fms-extensions",
318 llvm::cl::desc("Accept some non-standard constructs used in "
319 "Microsoft header files. "));
Chris Lattnerdb6be562007-11-28 05:34:05 +0000320
321static llvm::cl::opt<bool>
322WritableStrings("fwritable-strings",
323 llvm::cl::desc("Store string literals as writable data."));
Anders Carlssone87cd982007-11-30 04:21:22 +0000324
325static llvm::cl::opt<bool>
326LaxVectorConversions("flax-vector-conversions",
327 llvm::cl::desc("Allow implicit conversions between vectors"
328 " with a different number of elements or "
329 "different element types."));
Chris Lattner4b009652007-07-25 00:24:17 +0000330// FIXME: add:
331// -ansi
332// -trigraphs
333// -fdollars-in-identifiers
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000334// -fpascal-strings
Ted Kremenek11ad8952007-12-05 23:49:08 +0000335static void InitializeLanguageStandard(LangOptions &Options, LangKind LK) {
Chris Lattner4b009652007-07-25 00:24:17 +0000336 if (LangStd == lang_unspecified) {
337 // Based on the base language, pick one.
Ted Kremenek11ad8952007-12-05 23:49:08 +0000338 switch (LK) {
Chris Lattner4b009652007-07-25 00:24:17 +0000339 default: assert(0 && "Unknown base language");
340 case langkind_c:
341 case langkind_c_cpp:
342 case langkind_objc:
343 case langkind_objc_cpp:
344 LangStd = lang_gnu99;
345 break;
346 case langkind_cxx:
347 case langkind_cxx_cpp:
348 case langkind_objcxx:
349 case langkind_objcxx_cpp:
350 LangStd = lang_gnucxx98;
351 break;
352 }
353 }
354
355 switch (LangStd) {
356 default: assert(0 && "Unknown language standard!");
357
358 // Fall through from newer standards to older ones. This isn't really right.
359 // FIXME: Enable specifically the right features based on the language stds.
360 case lang_gnucxx0x:
361 case lang_cxx0x:
362 Options.CPlusPlus0x = 1;
363 // FALL THROUGH
364 case lang_gnucxx98:
365 case lang_cxx98:
366 Options.CPlusPlus = 1;
367 Options.CXXOperatorNames = !NoOperatorNames;
Nate Begemanca893342007-11-15 07:30:50 +0000368 Options.Boolean = 1;
Chris Lattner4b009652007-07-25 00:24:17 +0000369 // FALL THROUGH.
370 case lang_gnu99:
371 case lang_c99:
372 Options.Digraphs = 1;
373 Options.C99 = 1;
374 Options.HexFloats = 1;
375 // FALL THROUGH.
376 case lang_gnu89:
377 Options.BCPLComment = 1; // Only for C99/C++.
378 // FALL THROUGH.
379 case lang_c94:
380 case lang_c89:
381 break;
382 }
383
384 Options.Trigraphs = 1; // -trigraphs or -ansi
385 Options.DollarIdents = 1; // FIXME: Really a target property.
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000386 Options.PascalStrings = PascalStrings;
Steve Naroff73a07032008-02-07 03:50:06 +0000387 Options.Microsoft = MSExtensions;
Chris Lattnerdb6be562007-11-28 05:34:05 +0000388 Options.WritableStrings = WritableStrings;
Anders Carlssone87cd982007-11-30 04:21:22 +0000389 Options.LaxVectorConversions = LaxVectorConversions;
Chris Lattner4b009652007-07-25 00:24:17 +0000390}
391
392//===----------------------------------------------------------------------===//
393// Our DiagnosticClient implementation
394//===----------------------------------------------------------------------===//
395
396// FIXME: Werror should take a list of things, -Werror=foo,bar
397static llvm::cl::opt<bool>
398WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
399
400static llvm::cl::opt<bool>
401WarnOnExtensions("pedantic", llvm::cl::init(false),
402 llvm::cl::desc("Issue a warning on uses of GCC extensions"));
403
404static llvm::cl::opt<bool>
405ErrorOnExtensions("pedantic-errors",
406 llvm::cl::desc("Issue an error on uses of GCC extensions"));
407
408static llvm::cl::opt<bool>
409WarnUnusedMacros("Wunused_macros",
410 llvm::cl::desc("Warn for unused macros in the main translation unit"));
411
Ted Kremenek24f59fb2007-11-13 18:37:02 +0000412static llvm::cl::opt<bool>
413WarnFloatEqual("Wfloat-equal",
414 llvm::cl::desc("Warn about equality comparisons of floating point values."));
415
Ted Kremenek4b57bc72007-12-17 17:50:07 +0000416static llvm::cl::opt<bool>
417WarnNoFormatNonLiteral("Wno-format-nonliteral",
418 llvm::cl::desc("Do not warn about non-literal format strings."));
419
Chris Lattnera616ee32008-01-23 17:19:46 +0000420static llvm::cl::opt<bool>
421WarnUndefMacros("Wundef",
422 llvm::cl::desc("Warn on use of undefined macros in #if's"));
423
424
Chris Lattner4b009652007-07-25 00:24:17 +0000425/// InitializeDiagnostics - Initialize the diagnostic object, based on the
426/// current command line option settings.
427static void InitializeDiagnostics(Diagnostic &Diags) {
428 Diags.setWarningsAsErrors(WarningsAsErrors);
429 Diags.setWarnOnExtensions(WarnOnExtensions);
430 Diags.setErrorOnExtensions(ErrorOnExtensions);
431
432 // Silence the "macro is not used" warning unless requested.
433 if (!WarnUnusedMacros)
434 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
Ted Kremenek24f59fb2007-11-13 18:37:02 +0000435
436 // Silence "floating point comparison" warnings unless requested.
437 if (!WarnFloatEqual)
438 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
Ted Kremenek4b57bc72007-12-17 17:50:07 +0000439
440 // Silence "format string is not a string literal" warnings if requested
441 if (WarnNoFormatNonLiteral)
Ted Kremenekf0a6ae02007-12-17 17:50:39 +0000442 Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant,
443 diag::MAP_IGNORE);
Chris Lattnera616ee32008-01-23 17:19:46 +0000444 if (!WarnUndefMacros)
445 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);
Steve Naroff606f7072008-02-11 22:40:08 +0000446
447 if (MSExtensions) // MS allows unnamed struct/union fields.
448 Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE);
Chris Lattner4b009652007-07-25 00:24:17 +0000449}
450
451//===----------------------------------------------------------------------===//
Ted Kremenek0118bb52008-02-18 21:21:23 +0000452// Analysis-specific options.
453//===----------------------------------------------------------------------===//
454
455static llvm::cl::opt<std::string>
456AnalyzeSpecificFunction("analyze-function",
457 llvm::cl::desc("Run analysis on specific function."));
458
459//===----------------------------------------------------------------------===//
Ted Kremenek40499482007-12-03 22:06:55 +0000460// Target Triple Processing.
461//===----------------------------------------------------------------------===//
462
463static llvm::cl::opt<std::string>
464TargetTriple("triple",
465 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)."));
466
467static llvm::cl::list<std::string>
468Archs("arch",
469 llvm::cl::desc("Specify target architecture (e.g. i686)."));
470
471namespace {
472 class TripleProcessor {
473 llvm::StringMap<char> TriplesProcessed;
474 std::vector<std::string>& triples;
475 public:
476 TripleProcessor(std::vector<std::string>& t) : triples(t) {}
477
478 void addTriple(const std::string& t) {
479 if (TriplesProcessed.find(t.c_str(),t.c_str()+t.size()) ==
480 TriplesProcessed.end()) {
481 triples.push_back(t);
482 TriplesProcessed.GetOrCreateValue(t.c_str(),t.c_str()+t.size());
483 }
484 }
485 };
486}
487
488static void CreateTargetTriples(std::vector<std::string>& triples) {
Ted Kremenek40499482007-12-03 22:06:55 +0000489 // Initialize base triple. If a -triple option has been specified, use
490 // that triple. Otherwise, default to the host triple.
Chris Lattner210c0cc2007-12-12 05:01:48 +0000491 std::string Triple = TargetTriple;
492 if (Triple.empty()) Triple = LLVM_HOSTTRIPLE;
Ted Kremenek40499482007-12-03 22:06:55 +0000493
494 // Decompose the base triple into "arch" and suffix.
Chris Lattner210c0cc2007-12-12 05:01:48 +0000495 std::string::size_type firstDash = Triple.find("-");
Ted Kremenek40499482007-12-03 22:06:55 +0000496
Ted Kremenek0a8ce9d2007-12-03 22:11:31 +0000497 if (firstDash == std::string::npos) {
498 fprintf(stderr,
499 "Malformed target triple: \"%s\" ('-' could not be found).\n",
Chris Lattner210c0cc2007-12-12 05:01:48 +0000500 Triple.c_str());
501 exit(1);
Ted Kremenek0a8ce9d2007-12-03 22:11:31 +0000502 }
Ted Kremenek40499482007-12-03 22:06:55 +0000503
Chris Lattner210c0cc2007-12-12 05:01:48 +0000504 std::string suffix(Triple, firstDash+1);
Ted Kremenek0a8ce9d2007-12-03 22:11:31 +0000505
506 if (suffix.empty()) {
Chris Lattner210c0cc2007-12-12 05:01:48 +0000507 fprintf(stderr, "Malformed target triple: \"%s\" (no vendor or OS).\n",
508 Triple.c_str());
509 exit(1);
Ted Kremenek0a8ce9d2007-12-03 22:11:31 +0000510 }
Ted Kremenek40499482007-12-03 22:06:55 +0000511
512 // Create triple cacher.
513 TripleProcessor tp(triples);
514
515 // Add the primary triple to our set of triples if we are using the
516 // host-triple with no archs or using a specified target triple.
517 if (!TargetTriple.getValue().empty() || Archs.empty())
Chris Lattner210c0cc2007-12-12 05:01:48 +0000518 tp.addTriple(Triple);
Ted Kremenek40499482007-12-03 22:06:55 +0000519
520 for (unsigned i = 0, e = Archs.size(); i !=e; ++i)
521 tp.addTriple(Archs[i] + "-" + suffix);
522}
523
524//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000525// Preprocessor Initialization
526//===----------------------------------------------------------------------===//
527
528// FIXME: Preprocessor builtins to support.
529// -A... - Play with #assertions
530// -undef - Undefine all predefined macros
531
532static llvm::cl::list<std::string>
533D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
534 llvm::cl::desc("Predefine the specified macro"));
535static llvm::cl::list<std::string>
536U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
537 llvm::cl::desc("Undefine the specified macro"));
538
Chris Lattner008da782008-01-10 01:53:41 +0000539static llvm::cl::list<std::string>
540ImplicitIncludes("include", llvm::cl::value_desc("file"),
541 llvm::cl::desc("Include file before parsing"));
542
543
Chris Lattner4b009652007-07-25 00:24:17 +0000544// Append a #define line to Buf for Macro. Macro should be of the form XXX,
545// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
546// "#define XXX Y z W". To get a #define with no value, use "XXX=".
547static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
548 const char *Command = "#define ") {
549 Buf.insert(Buf.end(), Command, Command+strlen(Command));
550 if (const char *Equal = strchr(Macro, '=')) {
551 // Turn the = into ' '.
552 Buf.insert(Buf.end(), Macro, Equal);
553 Buf.push_back(' ');
554 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
555 } else {
556 // Push "macroname 1".
557 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
558 Buf.push_back(' ');
559 Buf.push_back('1');
560 }
561 Buf.push_back('\n');
562}
563
Chris Lattner008da782008-01-10 01:53:41 +0000564/// AddImplicitInclude - Add an implicit #include of the specified file to the
565/// predefines buffer.
566static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){
567 const char *Inc = "#include \"";
568 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
569 Buf.insert(Buf.end(), File.begin(), File.end());
570 Buf.push_back('"');
571 Buf.push_back('\n');
572}
573
Chris Lattner4b009652007-07-25 00:24:17 +0000574
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000575/// InitializePreprocessor - Initialize the preprocessor getting it and the
576/// environment ready to process a single file. This returns the file ID for the
577/// input file. If a failure happens, it returns 0.
578///
579static unsigned InitializePreprocessor(Preprocessor &PP,
580 const std::string &InFile,
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000581 std::vector<char> &PredefineBuffer) {
Chris Lattner4b009652007-07-25 00:24:17 +0000582
Chris Lattner968982d2007-12-15 20:48:40 +0000583 FileManager &FileMgr = PP.getFileManager();
Chris Lattner4b009652007-07-25 00:24:17 +0000584
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000585 // Figure out where to get and map in the main file.
Chris Lattner968982d2007-12-15 20:48:40 +0000586 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000587 if (InFile != "-") {
588 const FileEntry *File = FileMgr.getFile(InFile);
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000589 if (File) SourceMgr.createMainFileID(File, SourceLocation());
590 if (SourceMgr.getMainFileID() == 0) {
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000591 fprintf(stderr, "Error reading '%s'!\n",InFile.c_str());
592 return 0;
593 }
594 } else {
595 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000596 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
597 if (SourceMgr.getMainFileID() == 0) {
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000598 fprintf(stderr, "Error reading standard input! Empty?\n");
599 return 0;
600 }
Chris Lattner4b009652007-07-25 00:24:17 +0000601 }
602
Chris Lattner4b009652007-07-25 00:24:17 +0000603 // Add macros from the command line.
604 // FIXME: Should traverse the #define/#undef lists in parallel.
605 for (unsigned i = 0, e = D_macros.size(); i != e; ++i)
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000606 DefineBuiltinMacro(PredefineBuffer, D_macros[i].c_str());
Chris Lattner4b009652007-07-25 00:24:17 +0000607 for (unsigned i = 0, e = U_macros.size(); i != e; ++i)
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000608 DefineBuiltinMacro(PredefineBuffer, U_macros[i].c_str(), "#undef ");
609
Chris Lattner008da782008-01-10 01:53:41 +0000610 // FIXME: Read any files specified by -imacros.
611
612 // Add implicit #includes from -include.
613 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
614 AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]);
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000615
616 // Null terminate PredefinedBuffer and add it.
617 PredefineBuffer.push_back(0);
618 PP.setPredefines(&PredefineBuffer[0]);
619
620 // Once we've read this, we're done.
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000621 return SourceMgr.getMainFileID();
Chris Lattner4b009652007-07-25 00:24:17 +0000622}
623
624//===----------------------------------------------------------------------===//
625// Preprocessor include path information.
626//===----------------------------------------------------------------------===//
627
628// This tool exports a large number of command line options to control how the
629// preprocessor searches for header files. At root, however, the Preprocessor
630// object takes a very simple interface: a list of directories to search for
631//
632// FIXME: -nostdinc,-nostdinc++
Chris Lattnerae3dcc02007-08-26 17:47:35 +0000633// FIXME: -imultilib
Chris Lattner4b009652007-07-25 00:24:17 +0000634//
Chris Lattner008da782008-01-10 01:53:41 +0000635// FIXME: -imacros
Chris Lattner4b009652007-07-25 00:24:17 +0000636
637static llvm::cl::opt<bool>
638nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
639
640// Various command line options. These four add directories to each chain.
641static llvm::cl::list<std::string>
642F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
643 llvm::cl::desc("Add directory to framework include search path"));
644static llvm::cl::list<std::string>
645I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
646 llvm::cl::desc("Add directory to include search path"));
647static llvm::cl::list<std::string>
648idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
649 llvm::cl::desc("Add directory to AFTER include search path"));
650static llvm::cl::list<std::string>
651iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
652 llvm::cl::desc("Add directory to QUOTE include search path"));
653static llvm::cl::list<std::string>
654isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
655 llvm::cl::desc("Add directory to SYSTEM include search path"));
656
657// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
658static llvm::cl::list<std::string>
659iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
660 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
661static llvm::cl::list<std::string>
662iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
663 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
664static llvm::cl::list<std::string>
665iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
666 llvm::cl::Prefix,
667 llvm::cl::desc("Set directory to include search path with prefix"));
668
Chris Lattnerae3dcc02007-08-26 17:47:35 +0000669static llvm::cl::opt<std::string>
670isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
671 llvm::cl::desc("Set the system root directory (usually /)"));
672
Chris Lattner4b009652007-07-25 00:24:17 +0000673// Finally, implement the code that groks the options above.
674enum IncludeDirGroup {
675 Quoted = 0,
676 Angled,
677 System,
678 After
679};
680
681static std::vector<DirectoryLookup> IncludeGroup[4];
682
683/// AddPath - Add the specified path to the specified group list.
684///
685static void AddPath(const std::string &Path, IncludeDirGroup Group,
686 bool isCXXAware, bool isUserSupplied,
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000687 bool isFramework, HeaderSearch &HS) {
Chris Lattnerc8d80bb2007-12-09 00:39:55 +0000688 assert(!Path.empty() && "can't handle empty path here");
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000689 FileManager &FM = HS.getFileMgr();
Chris Lattnerc8d80bb2007-12-09 00:39:55 +0000690
Chris Lattner2a4e2ad2007-12-17 05:59:27 +0000691 // Compute the actual path, taking into consideration -isysroot.
692 llvm::SmallString<256> MappedPath;
Chris Lattnerae3dcc02007-08-26 17:47:35 +0000693
Chris Lattner2a4e2ad2007-12-17 05:59:27 +0000694 // Handle isysroot.
695 if (Group == System) {
Chris Lattner2a2702a2007-12-17 06:51:34 +0000696 // FIXME: Portability. This should be a sys::Path interface, this doesn't
697 // handle things like C:\ right, nor win32 \\network\device\blah.
Chris Lattner2a4e2ad2007-12-17 05:59:27 +0000698 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
699 MappedPath.append(isysroot.begin(), isysroot.end());
700 if (Path[0] != '/') // If in the system group, add a /.
701 MappedPath.push_back('/');
Chris Lattner4b009652007-07-25 00:24:17 +0000702 }
703
Chris Lattner2a4e2ad2007-12-17 05:59:27 +0000704 MappedPath.append(Path.begin(), Path.end());
705
706 // Compute the DirectoryLookup type.
Chris Lattner4b009652007-07-25 00:24:17 +0000707 DirectoryLookup::DirType Type;
708 if (Group == Quoted || Group == Angled)
709 Type = DirectoryLookup::NormalHeaderDir;
710 else if (isCXXAware)
711 Type = DirectoryLookup::SystemHeaderDir;
712 else
713 Type = DirectoryLookup::ExternCSystemHeaderDir;
714
Chris Lattner2a4e2ad2007-12-17 05:59:27 +0000715
716 // If the directory exists, add it.
717 if (const DirectoryEntry *DE = FM.getDirectory(&MappedPath[0],
718 &MappedPath[0]+
719 MappedPath.size())) {
720 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
721 isFramework));
722 return;
723 }
724
Chris Lattnerb7426782007-12-17 07:52:39 +0000725 // Check to see if this is an apple-style headermap (which are not allowed to
726 // be frameworks).
727 if (!isFramework) {
728 if (const FileEntry *FE = FM.getFile(&MappedPath[0],
729 &MappedPath[0]+MappedPath.size())) {
Chris Lattner9af36d32007-12-17 18:34:53 +0000730 if (const HeaderMap *HM = HS.CreateHeaderMap(FE)) {
731 // It is a headermap, add it to the search path.
Chris Lattnerb7426782007-12-17 07:52:39 +0000732 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
733 return;
734 }
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000735 }
736 }
737
Chris Lattner2a4e2ad2007-12-17 05:59:27 +0000738 if (Verbose)
739 fprintf(stderr, "ignoring nonexistent directory \"%s\"\n", Path.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +0000740}
741
742/// RemoveDuplicates - If there are duplicate directory entries in the specified
743/// search list, remove the later (dead) ones.
744static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList) {
Chris Lattnerac139d22007-12-15 23:20:07 +0000745 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
Chris Lattnerb7426782007-12-17 07:52:39 +0000746 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
Chris Lattnercf33e932007-12-17 06:44:29 +0000747 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Chris Lattner4b009652007-07-25 00:24:17 +0000748 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattnercf33e932007-12-17 06:44:29 +0000749 if (SearchList[i].isNormalDir()) {
750 // If this isn't the first time we've seen this dir, remove it.
751 if (SeenDirs.insert(SearchList[i].getDir()))
752 continue;
753
Chris Lattner4b009652007-07-25 00:24:17 +0000754 if (Verbose)
755 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
756 SearchList[i].getDir()->getName());
Chris Lattnerb7426782007-12-17 07:52:39 +0000757 } else if (SearchList[i].isFramework()) {
758 // If this isn't the first time we've seen this framework dir, remove it.
759 if (SeenFrameworkDirs.insert(SearchList[i].getFrameworkDir()))
760 continue;
761
762 if (Verbose)
763 fprintf(stderr, "ignoring duplicate framework \"%s\"\n",
764 SearchList[i].getFrameworkDir()->getName());
765
Chris Lattnercf33e932007-12-17 06:44:29 +0000766 } else {
767 assert(SearchList[i].isHeaderMap() && "Not a headermap or normal dir?");
768 // If this isn't the first time we've seen this headermap, remove it.
769 if (SeenHeaderMaps.insert(SearchList[i].getHeaderMap()))
770 continue;
771
772 if (Verbose)
773 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
774 SearchList[i].getDir()->getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000775 }
Chris Lattnercf33e932007-12-17 06:44:29 +0000776
777 // This is reached if the current entry is a duplicate.
778 SearchList.erase(SearchList.begin()+i);
779 --i;
Chris Lattner4b009652007-07-25 00:24:17 +0000780 }
781}
782
783/// InitializeIncludePaths - Process the -I options and set them in the
784/// HeaderSearch object.
785static void InitializeIncludePaths(HeaderSearch &Headers, FileManager &FM,
Chris Lattner45a56e02007-12-05 23:24:17 +0000786 const LangOptions &Lang) {
Chris Lattner4b009652007-07-25 00:24:17 +0000787 // Handle -F... options.
788 for (unsigned i = 0, e = F_dirs.size(); i != e; ++i)
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000789 AddPath(F_dirs[i], Angled, false, true, true, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000790
791 // Handle -I... options.
Chris Lattner45a56e02007-12-05 23:24:17 +0000792 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i)
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000793 AddPath(I_dirs[i], Angled, false, true, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000794
795 // Handle -idirafter... options.
796 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000797 AddPath(idirafter_dirs[i], After, false, true, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000798
799 // Handle -iquote... options.
800 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000801 AddPath(iquote_dirs[i], Quoted, false, true, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000802
803 // Handle -isystem... options.
804 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000805 AddPath(isystem_dirs[i], System, false, true, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000806
807 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
808 // parallel, processing the values in order of occurance to get the right
809 // prefixes.
810 {
811 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
812 unsigned iprefix_idx = 0;
813 unsigned iwithprefix_idx = 0;
814 unsigned iwithprefixbefore_idx = 0;
815 bool iprefix_done = iprefix_vals.empty();
816 bool iwithprefix_done = iwithprefix_vals.empty();
817 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
818 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
819 if (!iprefix_done &&
820 (iwithprefix_done ||
821 iprefix_vals.getPosition(iprefix_idx) <
822 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
823 (iwithprefixbefore_done ||
824 iprefix_vals.getPosition(iprefix_idx) <
825 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
826 Prefix = iprefix_vals[iprefix_idx];
827 ++iprefix_idx;
828 iprefix_done = iprefix_idx == iprefix_vals.size();
829 } else if (!iwithprefix_done &&
830 (iwithprefixbefore_done ||
831 iwithprefix_vals.getPosition(iwithprefix_idx) <
832 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
833 AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000834 System, false, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000835 ++iwithprefix_idx;
836 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
837 } else {
838 AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000839 Angled, false, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000840 ++iwithprefixbefore_idx;
841 iwithprefixbefore_done =
842 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
843 }
844 }
845 }
846
847 // FIXME: Add contents of the CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
848 // OBJC_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH environment variables.
849
850 // FIXME: temporary hack: hard-coded paths.
851 // FIXME: get these from the target?
852 if (!nostdinc) {
853 if (Lang.CPlusPlus) {
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000854 AddPath("/usr/include/c++/4.0.0", System, true, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000855 AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false,
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000856 false, Headers);
857 AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false,
858 Headers);
Lauro Ramos Venanciof6a66272008-02-15 22:36:38 +0000859
860 // Ubuntu 7.10 - Gutsy Gibbon
861 AddPath("/usr/include/c++/4.1.3", System, true, false, false, Headers);
862 AddPath("/usr/include/c++/4.1.3/i486-linux-gnu", System, true, false,
863 false, Headers);
864 AddPath("/usr/include/c++/4.1.3/backward", System, true, false, false,
865 Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000866 }
867
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000868 AddPath("/usr/local/include", System, false, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000869 // leopard
870 AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System,
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000871 false, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000872 AddPath("/usr/lib/gcc/powerpc-apple-darwin9/4.0.1/include",
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000873 System, false, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000874 AddPath("/usr/lib/gcc/powerpc-apple-darwin9/"
875 "4.0.1/../../../../powerpc-apple-darwin0/include",
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000876 System, false, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000877
878 // tiger
879 AddPath("/usr/lib/gcc/i686-apple-darwin8/4.0.1/include", System,
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000880 false, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000881 AddPath("/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include",
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000882 System, false, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000883 AddPath("/usr/lib/gcc/powerpc-apple-darwin8/"
884 "4.0.1/../../../../powerpc-apple-darwin8/include",
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000885 System, false, false, false, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000886
Lauro Ramos Venanciod0515f22008-01-21 23:08:35 +0000887 // Ubuntu 7.10 - Gutsy Gibbon
888 AddPath("/usr/lib/gcc/i486-linux-gnu/4.1.3/include", System,
889 false, false, false, Headers);
890
Chris Lattnerc2043bf2007-12-17 06:36:45 +0000891 AddPath("/usr/include", System, false, false, false, Headers);
892 AddPath("/System/Library/Frameworks", System, true, false, true, Headers);
893 AddPath("/Library/Frameworks", System, true, false, true, Headers);
Chris Lattner4b009652007-07-25 00:24:17 +0000894 }
895
896 // Now that we have collected all of the include paths, merge them all
897 // together and tell the preprocessor about them.
898
899 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
900 std::vector<DirectoryLookup> SearchList;
901 SearchList = IncludeGroup[Angled];
902 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
903 IncludeGroup[System].end());
904 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
905 IncludeGroup[After].end());
906 RemoveDuplicates(SearchList);
907 RemoveDuplicates(IncludeGroup[Quoted]);
908
909 // Prepend QUOTED list on the search list.
910 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
911 IncludeGroup[Quoted].end());
912
913
914 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
915 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
916 DontSearchCurDir);
917
918 // If verbose, print the list of directories that will be searched.
919 if (Verbose) {
920 fprintf(stderr, "#include \"...\" search starts here:\n");
921 unsigned QuotedIdx = IncludeGroup[Quoted].size();
922 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
923 if (i == QuotedIdx)
924 fprintf(stderr, "#include <...> search starts here:\n");
Chris Lattner1df68f92007-12-17 17:57:27 +0000925 const char *Name = SearchList[i].getName();
926 const char *Suffix;
Chris Lattner0f64f652007-12-17 17:42:26 +0000927 if (SearchList[i].isNormalDir())
Chris Lattner1df68f92007-12-17 17:57:27 +0000928 Suffix = "";
Chris Lattner0f64f652007-12-17 17:42:26 +0000929 else if (SearchList[i].isFramework())
Chris Lattner1df68f92007-12-17 17:57:27 +0000930 Suffix = " (framework directory)";
Chris Lattner0f64f652007-12-17 17:42:26 +0000931 else {
932 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
Chris Lattner1df68f92007-12-17 17:57:27 +0000933 Suffix = " (headermap)";
Chris Lattner0f64f652007-12-17 17:42:26 +0000934 }
Chris Lattner1df68f92007-12-17 17:57:27 +0000935 fprintf(stderr, " %s%s\n", Name, Suffix);
Chris Lattner4b009652007-07-25 00:24:17 +0000936 }
Chris Lattnerac553842007-12-15 23:11:06 +0000937 fprintf(stderr, "End of search list.\n");
Chris Lattner4b009652007-07-25 00:24:17 +0000938 }
939}
940
941
Chris Lattner4b009652007-07-25 00:24:17 +0000942//===----------------------------------------------------------------------===//
943// Basic Parser driver
944//===----------------------------------------------------------------------===//
945
Ted Kremenek17861c52007-12-19 22:51:13 +0000946static void ParseFile(Preprocessor &PP, MinimalAction *PA){
Chris Lattner4b009652007-07-25 00:24:17 +0000947 Parser P(PP, *PA);
Ted Kremenek17861c52007-12-19 22:51:13 +0000948 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +0000949
950 // Parsing the specified input file.
951 P.ParseTranslationUnit();
952 delete PA;
953}
954
955//===----------------------------------------------------------------------===//
956// Main driver
957//===----------------------------------------------------------------------===//
958
Ted Kremeneka36aaef2007-12-05 18:27:04 +0000959/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
960/// action. These consumers can operate on both ASTs that are freshly
961/// parsed from source files as well as those deserialized from Bitcode.
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000962static ASTConsumer* CreateASTConsumer(const std::string& InFile,
Ted Kremenek397de012007-12-13 00:37:31 +0000963 Diagnostic& Diag, FileManager& FileMgr,
Chris Lattner8d72ee02008-02-06 01:42:25 +0000964 const LangOptions& LangOpts,
965 llvm::Module *&DestModule) {
Ted Kremeneka36aaef2007-12-05 18:27:04 +0000966 switch (ProgAction) {
967 default:
968 return NULL;
969
970 case ASTPrint:
971 return CreateASTPrinter();
972
973 case ASTDump:
974 return CreateASTDumper();
975
976 case ASTView:
977 return CreateASTViewer();
978
979 case ParseCFGDump:
980 case ParseCFGView:
981 return CreateCFGDumper(ProgAction == ParseCFGView);
982
983 case AnalysisLiveVariables:
984 return CreateLiveVarAnalyzer();
985
986 case WarnDeadStores:
987 return CreateDeadStoreChecker(Diag);
988
989 case WarnUninitVals:
990 return CreateUnitValsChecker(Diag);
991
Ted Kremenek3862eb12008-02-14 22:36:46 +0000992 case AnalysisGRSimpleVals:
Ted Kremenek0118bb52008-02-18 21:21:23 +0000993 return CreateGRSimpleVals(Diag, AnalyzeSpecificFunction);
Ted Kremenek3b451132008-01-08 18:04:06 +0000994
Ted Kremenek1da5b142008-02-15 00:35:38 +0000995 case AnalysisGRSimpleValsView:
Ted Kremenek0118bb52008-02-18 21:21:23 +0000996 return CreateGRSimpleVals(Diag, AnalyzeSpecificFunction, true);
Ted Kremenek1da5b142008-02-15 00:35:38 +0000997
Ted Kremeneka36aaef2007-12-05 18:27:04 +0000998 case TestSerialization:
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000999 return CreateSerializationTest(Diag, FileMgr, LangOpts);
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001000
1001 case EmitLLVM:
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +00001002 case EmitBC:
Chris Lattner8d72ee02008-02-06 01:42:25 +00001003 DestModule = new llvm::Module(InFile);
1004 return CreateLLVMCodeGen(Diag, LangOpts, DestModule);
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +00001005
Ted Kremenekbde30332007-12-19 17:25:59 +00001006 case SerializeAST:
Ted Kremenek397de012007-12-13 00:37:31 +00001007 // FIXME: Allow user to tailor where the file is written.
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +00001008 return CreateASTSerializer(InFile, OutputFile, Diag, LangOpts);
Ted Kremenek397de012007-12-13 00:37:31 +00001009
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001010 case RewriteTest:
Fariborz Jahanian8d2080c2008-01-18 01:15:54 +00001011 return CreateCodeRewriterTest(InFile, Diag);
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001012 }
1013}
1014
Chris Lattner4b009652007-07-25 00:24:17 +00001015/// ProcessInputFile - Process a single input file with the specified state.
1016///
Ted Kremenek0fd6e492007-12-19 22:32:34 +00001017static void ProcessInputFile(Preprocessor &PP, const std::string &InFile,
Chris Lattner968982d2007-12-15 20:48:40 +00001018 TextDiagnostics &OurDiagnosticClient) {
Ted Kremenek6856c632007-09-26 18:39:29 +00001019
1020 ASTConsumer* Consumer = NULL;
Chris Lattner4b009652007-07-25 00:24:17 +00001021 bool ClearSourceMgr = false;
Chris Lattner8d72ee02008-02-06 01:42:25 +00001022 llvm::Module *CodeGenModule = 0;
Ted Kremenek6856c632007-09-26 18:39:29 +00001023
Chris Lattner4b009652007-07-25 00:24:17 +00001024 switch (ProgAction) {
1025 default:
Ted Kremenekd890f6a2007-12-19 22:24:34 +00001026 Consumer = CreateASTConsumer(InFile,
1027 PP.getDiagnostics(),
Chris Lattner968982d2007-12-15 20:48:40 +00001028 PP.getFileManager(),
Chris Lattner8d72ee02008-02-06 01:42:25 +00001029 PP.getLangOptions(),
1030 CodeGenModule);
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001031
1032 if (!Consumer) {
1033 fprintf(stderr, "Unexpected program action!\n");
1034 return;
1035 }
Ted Kremenekd890f6a2007-12-19 22:24:34 +00001036
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001037 break;
1038
Chris Lattner4b009652007-07-25 00:24:17 +00001039 case DumpTokens: { // Token dump mode.
1040 Token Tok;
1041 // Start parsing the specified input file.
Ted Kremenek17861c52007-12-19 22:51:13 +00001042 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +00001043 do {
1044 PP.Lex(Tok);
1045 PP.DumpToken(Tok, true);
1046 fprintf(stderr, "\n");
Chris Lattner3b494152007-10-09 18:03:42 +00001047 } while (Tok.isNot(tok::eof));
Chris Lattner4b009652007-07-25 00:24:17 +00001048 ClearSourceMgr = true;
1049 break;
1050 }
1051 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
1052 Token Tok;
1053 // Start parsing the specified input file.
Ted Kremenek17861c52007-12-19 22:51:13 +00001054 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +00001055 do {
1056 PP.Lex(Tok);
Chris Lattner3b494152007-10-09 18:03:42 +00001057 } while (Tok.isNot(tok::eof));
Chris Lattner4b009652007-07-25 00:24:17 +00001058 ClearSourceMgr = true;
1059 break;
1060 }
1061
1062 case PrintPreprocessedInput: // -E mode.
Chris Lattnerefd02a32008-01-27 23:55:11 +00001063 DoPrintPreprocessedInput(PP, OutputFile);
Chris Lattner4b009652007-07-25 00:24:17 +00001064 ClearSourceMgr = true;
1065 break;
1066
1067 case ParseNoop: // -parse-noop
Ted Kremenek17861c52007-12-19 22:51:13 +00001068 ParseFile(PP, new MinimalAction(PP.getIdentifierTable()));
Chris Lattner4b009652007-07-25 00:24:17 +00001069 ClearSourceMgr = true;
1070 break;
1071
1072 case ParsePrintCallbacks:
Ted Kremenek17861c52007-12-19 22:51:13 +00001073 ParseFile(PP, CreatePrintParserActionsAction(PP.getIdentifierTable()));
Chris Lattner4b009652007-07-25 00:24:17 +00001074 ClearSourceMgr = true;
1075 break;
Ted Kremenek0841c702007-09-25 18:37:20 +00001076
Ted Kremenek6856c632007-09-26 18:39:29 +00001077 case ParseSyntaxOnly: // -fsyntax-only
Ted Kremenek6856c632007-09-26 18:39:29 +00001078 Consumer = new ASTConsumer();
Ted Kremenek0a03ce62007-09-17 20:49:30 +00001079 break;
Chris Lattner129758d2007-09-16 19:46:59 +00001080 }
Ted Kremenek6856c632007-09-26 18:39:29 +00001081
1082 if (Consumer) {
Ted Kremenek56b70862007-09-26 20:14:22 +00001083 if (VerifyDiagnostics)
Ted Kremenek17861c52007-12-19 22:51:13 +00001084 exit(CheckASTConsumer(PP, Consumer));
Chris Lattner8593cbf2007-11-03 06:24:16 +00001085
1086 // This deletes Consumer.
Ted Kremenek17861c52007-12-19 22:51:13 +00001087 ParseAST(PP, Consumer, Stats);
Chris Lattner4b009652007-07-25 00:24:17 +00001088 }
Chris Lattner8d72ee02008-02-06 01:42:25 +00001089
1090 // If running the code generator, finish up now.
1091 if (CodeGenModule) {
1092 std::ostream *Out;
1093 if (OutputFile == "-") {
1094 Out = llvm::cout.stream();
1095 } else if (!OutputFile.empty()) {
1096 Out = new std::ofstream(OutputFile.c_str(),
1097 std::ios_base::binary|std::ios_base::out);
1098 } else if (InFile == "-") {
1099 Out = llvm::cout.stream();
1100 } else {
1101 llvm::sys::Path Path(InFile);
1102 Path.eraseSuffix();
1103 if (ProgAction == EmitLLVM)
1104 Path.appendSuffix("ll");
1105 else if (ProgAction == EmitBC)
1106 Path.appendSuffix("bc");
1107 else
1108 assert(0 && "Unknown action");
1109 Out = new std::ofstream(Path.toString().c_str(),
1110 std::ios_base::binary|std::ios_base::out);
1111 }
1112
1113 if (ProgAction == EmitLLVM) {
1114 CodeGenModule->print(*Out);
1115 } else {
1116 assert(ProgAction == EmitBC);
1117 llvm::WriteBitcodeToFile(CodeGenModule, *Out);
1118 }
1119
1120 if (Out != llvm::cout.stream())
1121 delete Out;
1122 delete CodeGenModule;
1123 }
Chris Lattner4b009652007-07-25 00:24:17 +00001124
1125 if (Stats) {
Ted Kremenekd890f6a2007-12-19 22:24:34 +00001126 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +00001127 PP.PrintStats();
1128 PP.getIdentifierTable().PrintStats();
Chris Lattner968982d2007-12-15 20:48:40 +00001129 PP.getHeaderSearchInfo().PrintStats();
Chris Lattner4b009652007-07-25 00:24:17 +00001130 if (ClearSourceMgr)
Chris Lattner968982d2007-12-15 20:48:40 +00001131 PP.getSourceManager().PrintStats();
Chris Lattner4b009652007-07-25 00:24:17 +00001132 fprintf(stderr, "\n");
1133 }
1134
1135 // For a multi-file compilation, some things are ok with nuking the source
1136 // manager tables, other require stable fileid/macroid's across multiple
1137 // files.
Chris Lattner968982d2007-12-15 20:48:40 +00001138 if (ClearSourceMgr)
1139 PP.getSourceManager().clearIDTables();
Chris Lattner4b009652007-07-25 00:24:17 +00001140}
1141
Ted Kremenek80d53372007-12-12 23:41:08 +00001142static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag,
1143 FileManager& FileMgr) {
1144
1145 if (VerifyDiagnostics) {
1146 fprintf(stderr, "-verify does not yet work with serialized ASTs.\n");
1147 exit (1);
1148 }
1149
1150 llvm::sys::Path Filename(InFile);
1151
1152 if (!Filename.isValid()) {
1153 fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str());
1154 exit (1);
1155 }
1156
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +00001157 llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename,FileMgr));
Ted Kremenek2bd42412007-12-13 18:11:11 +00001158
1159 if (!TU) {
1160 fprintf(stderr, "error: file '%s' could not be deserialized\n",
1161 InFile.c_str());
1162 exit (1);
1163 }
1164
Ted Kremenekab749372007-12-19 19:27:38 +00001165 // Observe that we use the source file name stored in the deserialized
1166 // translation unit, rather than InFile.
Chris Lattner8d72ee02008-02-06 01:42:25 +00001167 llvm::Module *DestModule;
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +00001168 llvm::OwningPtr<ASTConsumer>
Chris Lattner8d72ee02008-02-06 01:42:25 +00001169 Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOpts(),
1170 DestModule));
Ted Kremenek80d53372007-12-12 23:41:08 +00001171
1172 if (!Consumer) {
1173 fprintf(stderr, "Unsupported program action with serialized ASTs!\n");
1174 exit (1);
1175 }
1176
Ted Kremenek17861c52007-12-19 22:51:13 +00001177 Consumer->Initialize(*TU->getContext());
Ted Kremenek80d53372007-12-12 23:41:08 +00001178
Chris Lattner8d72ee02008-02-06 01:42:25 +00001179 // FIXME: We need to inform Consumer about completed TagDecls as well.
Ted Kremenek80d53372007-12-12 23:41:08 +00001180 for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
1181 Consumer->HandleTopLevelDecl(*I);
Ted Kremenek80d53372007-12-12 23:41:08 +00001182}
1183
1184
Chris Lattner4b009652007-07-25 00:24:17 +00001185static llvm::cl::list<std::string>
1186InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1187
Ted Kremenek80d53372007-12-12 23:41:08 +00001188static bool isSerializedFile(const std::string& InFile) {
1189 if (InFile.size() < 4)
1190 return false;
1191
1192 const char* s = InFile.c_str()+InFile.size()-4;
1193
1194 return s[0] == '.' &&
1195 s[1] == 'a' &&
1196 s[2] == 's' &&
1197 s[3] == 't';
1198}
1199
Chris Lattner4b009652007-07-25 00:24:17 +00001200
1201int main(int argc, char **argv) {
1202 llvm::cl::ParseCommandLineOptions(argc, argv, " llvm cfe\n");
1203 llvm::sys::PrintStackTraceOnErrorSignal();
1204
1205 // If no input was specified, read from stdin.
1206 if (InputFilenames.empty())
1207 InputFilenames.push_back("-");
Ted Kremenekb240e822007-12-11 23:28:38 +00001208
Chris Lattner4b009652007-07-25 00:24:17 +00001209 // Create a file manager object to provide access to and cache the filesystem.
1210 FileManager FileMgr;
1211
Ted Kremenekb240e822007-12-11 23:28:38 +00001212 // Create the diagnostic client for reporting errors or for
1213 // implementing -verify.
Chris Lattner4b009652007-07-25 00:24:17 +00001214 std::auto_ptr<TextDiagnostics> DiagClient;
Ted Kremenek56b70862007-09-26 20:14:22 +00001215 if (!VerifyDiagnostics) {
Chris Lattner4b009652007-07-25 00:24:17 +00001216 // Print diagnostics to stderr by default.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001217 DiagClient.reset(new TextDiagnosticPrinter());
Chris Lattner4b009652007-07-25 00:24:17 +00001218 } else {
1219 // When checking diagnostics, just buffer them up.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001220 DiagClient.reset(new TextDiagnosticBuffer());
Chris Lattner4b009652007-07-25 00:24:17 +00001221
1222 if (InputFilenames.size() != 1) {
1223 fprintf(stderr,
Ted Kremenek56b70862007-09-26 20:14:22 +00001224 "-verify only works on single input files for now.\n");
Chris Lattner4b009652007-07-25 00:24:17 +00001225 return 1;
1226 }
1227 }
1228
1229 // Configure our handling of diagnostics.
1230 Diagnostic Diags(*DiagClient);
Ted Kremenekb240e822007-12-11 23:28:38 +00001231 InitializeDiagnostics(Diags);
1232
Chris Lattner45a56e02007-12-05 23:24:17 +00001233 // -I- is a deprecated GCC feature, scan for it and reject it.
1234 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
1235 if (I_dirs[i] == "-") {
Ted Kremenekde79f792007-12-11 22:57:35 +00001236 Diags.Report(diag::err_pp_I_dash_not_supported);
Chris Lattner45a56e02007-12-05 23:24:17 +00001237 I_dirs.erase(I_dirs.begin()+i);
1238 --i;
1239 }
1240 }
1241
Chris Lattner4b009652007-07-25 00:24:17 +00001242 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenekb240e822007-12-11 23:28:38 +00001243 const std::string &InFile = InputFilenames[i];
Ted Kremenekb240e822007-12-11 23:28:38 +00001244
Ted Kremenek80d53372007-12-12 23:41:08 +00001245 if (isSerializedFile(InFile))
1246 ProcessSerializedFile(InFile,Diags,FileMgr);
1247 else {
1248 /// Create a SourceManager object. This tracks and owns all the file
1249 /// buffers allocated to a translation unit.
1250 SourceManager SourceMgr;
Ted Kremenekb240e822007-12-11 23:28:38 +00001251
Ted Kremenek80d53372007-12-12 23:41:08 +00001252 // Initialize language options, inferring file types from input filenames.
1253 LangOptions LangInfo;
1254 InitializeBaseLanguage();
1255 LangKind LK = GetLanguage(InFile);
1256 InitializeLangOptions(LangInfo, LK);
1257 InitializeLanguageStandard(LangInfo, LK);
1258
1259 // Process the -I options and set them in the HeaderInfo.
1260 HeaderSearch HeaderInfo(FileMgr);
1261 DiagClient->setHeaderSearch(HeaderInfo);
1262 InitializeIncludePaths(HeaderInfo, FileMgr, LangInfo);
1263
1264 // Get information about the targets being compiled for. Note that this
1265 // pointer and the TargetInfoImpl objects are never deleted by this toy
1266 // driver.
1267 TargetInfo *Target;
1268
1269 // Create triples, and create the TargetInfo.
1270 std::vector<std::string> triples;
1271 CreateTargetTriples(triples);
1272 Target = TargetInfo::CreateTargetInfo(&triples[0],
1273 &triples[0]+triples.size(),
1274 &Diags);
1275
1276 if (Target == 0) {
1277 fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
1278 triples[0].c_str());
1279 fprintf(stderr, "Please use -triple or -arch.\n");
1280 exit(1);
1281 }
1282
1283 // Set up the preprocessor with these options.
1284 Preprocessor PP(Diags, LangInfo, *Target, SourceMgr, HeaderInfo);
1285
1286 std::vector<char> PredefineBuffer;
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +00001287 if (!InitializePreprocessor(PP, InFile, PredefineBuffer))
Ted Kremenek2578dd02007-12-19 22:29:55 +00001288 continue;
1289
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +00001290 ProcessInputFile(PP, InFile, *DiagClient);
Ted Kremenek80d53372007-12-12 23:41:08 +00001291 HeaderInfo.ClearFileInfo();
1292
1293 if (Stats)
1294 SourceMgr.PrintStats();
1295 }
Chris Lattner4b009652007-07-25 00:24:17 +00001296 }
1297
1298 unsigned NumDiagnostics = Diags.getNumDiagnostics();
1299
1300 if (NumDiagnostics)
1301 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1302 (NumDiagnostics == 1 ? "" : "s"));
1303
1304 if (Stats) {
Chris Lattner4b009652007-07-25 00:24:17 +00001305 FileMgr.PrintStats();
1306 fprintf(stderr, "\n");
1307 }
1308
1309 return Diags.getNumErrors() != 0;
1310}