blob: 6e76a4a0cd272bdca69394e26c048d0dc7a3af60 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "TextDiagnosticBuffer.h"
28#include "TextDiagnosticPrinter.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000029#include "clang/AST/TranslationUnit.h"
Chris Lattnere91c1342008-02-06 00:23:21 +000030#include "clang/Sema/ParseAST.h"
Chris Lattner556beb72007-09-15 22:56:56 +000031#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000032#include "clang/Parse/Parser.h"
33#include "clang/Lex/HeaderSearch.h"
34#include "clang/Basic/FileManager.h"
35#include "clang/Basic/SourceManager.h"
36#include "clang/Basic/TargetInfo.h"
Chris Lattnere66b65c2008-02-06 01:42:25 +000037#include "llvm/Module.h"
Chris Lattner8f3dab82007-12-15 23:20:07 +000038#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnere66b65c2008-02-06 01:42:25 +000039#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000040#include "llvm/Support/CommandLine.h"
41#include "llvm/Support/MemoryBuffer.h"
42#include "llvm/System/Signals.h"
Ted Kremenekae360762007-12-03 22:06:55 +000043#include "llvm/Config/config.h"
Ted Kremenekee533642007-12-20 19:47:16 +000044#include "llvm/ADT/OwningPtr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000045#include <memory>
Chris Lattnere66b65c2008-02-06 01:42:25 +000046#include <fstream>
Reid Spencer5f016e22007-07-11 17:01:13 +000047using namespace clang;
48
49//===----------------------------------------------------------------------===//
50// Global options.
51//===----------------------------------------------------------------------===//
52
53static llvm::cl::opt<bool>
54Verbose("v", llvm::cl::desc("Enable verbose output"));
55static llvm::cl::opt<bool>
Nate Begemanaabbb122007-12-30 01:38:50 +000056Stats("print-stats",
57 llvm::cl::desc("Print performance metrics and statistics"));
Reid Spencer5f016e22007-07-11 17:01:13 +000058
59enum ProgActions {
Chris Lattner77cd2a02007-10-11 00:43:27 +000060 RewriteTest, // Rewriter testing stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +000061 EmitLLVM, // Emit a .ll file.
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +000062 EmitBC, // Emit a .bc file.
Ted Kremeneka1fa3a12007-12-13 00:37:31 +000063 SerializeAST, // Emit a .ast file.
Chris Lattner3b427b32007-10-11 00:18:28 +000064 ASTPrint, // Parse ASTs and print them.
65 ASTDump, // Parse ASTs and dump them.
66 ASTView, // Parse ASTs and view them in Graphviz.
Ted Kremenekfddd5182007-08-21 21:42:03 +000067 ParseCFGDump, // Parse ASTS. Build CFGs. Print CFGs.
Ted Kremenek055c2752007-09-06 23:00:42 +000068 ParseCFGView, // Parse ASTS. Build CFGs. View CFGs.
Ted Kremeneke4e63342007-09-06 00:17:54 +000069 AnalysisLiveVariables, // Print results of live-variable analysis.
Ted Kremenekee985462008-01-16 18:18:48 +000070 AnalysisGRConstants, // Perform graph-reachability constant prop.
Ted Kremenek055c2752007-09-06 23:00:42 +000071 WarnDeadStores, // Run DeadStores checker on parsed ASTs.
Ted Kremenek44579782007-09-25 18:37:20 +000072 WarnDeadStoresCheck, // Check diagnostics for "DeadStores".
Ted Kremenek2bf55142007-09-17 20:49:30 +000073 WarnUninitVals, // Run UnitializedVariables checker.
Ted Kremenekbfa82c42007-10-16 23:37:27 +000074 TestSerialization, // Run experimental serialization code.
Reid Spencer5f016e22007-07-11 17:01:13 +000075 ParsePrintCallbacks, // Parse and print each callback.
76 ParseSyntaxOnly, // Parse and perform semantic analysis.
77 ParseNoop, // Parse with noop callbacks.
78 RunPreprocessorOnly, // Just lex, no output.
79 PrintPreprocessedInput, // -E mode.
80 DumpTokens // Token dump mode.
81};
82
83static llvm::cl::opt<ProgActions>
84ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
85 llvm::cl::init(ParseSyntaxOnly),
86 llvm::cl::values(
87 clEnumValN(RunPreprocessorOnly, "Eonly",
88 "Just run preprocessor, no output (for timings)"),
89 clEnumValN(PrintPreprocessedInput, "E",
90 "Run preprocessor, emit preprocessed file"),
91 clEnumValN(DumpTokens, "dumptokens",
92 "Run preprocessor, dump internal rep of tokens"),
93 clEnumValN(ParseNoop, "parse-noop",
94 "Run parser with noop callbacks (for timings)"),
95 clEnumValN(ParseSyntaxOnly, "fsyntax-only",
96 "Run parser and perform semantic analysis"),
97 clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
98 "Run parser and print each callback invoked"),
Chris Lattner3b427b32007-10-11 00:18:28 +000099 clEnumValN(ASTPrint, "ast-print",
100 "Build ASTs and then pretty-print them"),
101 clEnumValN(ASTDump, "ast-dump",
102 "Build ASTs and then debug dump them"),
Chris Lattnerea254db2007-10-11 00:37:43 +0000103 clEnumValN(ASTView, "ast-view",
Chris Lattner3b427b32007-10-11 00:18:28 +0000104 "Build ASTs and view them with GraphViz."),
Ted Kremenekfddd5182007-08-21 21:42:03 +0000105 clEnumValN(ParseCFGDump, "dump-cfg",
Ted Kremenek7dba8602007-08-29 21:56:09 +0000106 "Run parser, then build and print CFGs."),
107 clEnumValN(ParseCFGView, "view-cfg",
Ted Kremeneke4e63342007-09-06 00:17:54 +0000108 "Run parser, then build and view CFGs with Graphviz."),
109 clEnumValN(AnalysisLiveVariables, "dump-live-variables",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000110 "Print results of live variable analysis."),
Ted Kremenek786d3372007-09-25 18:05:45 +0000111 clEnumValN(WarnDeadStores, "warn-dead-stores",
Ted Kremenek055c2752007-09-06 23:00:42 +0000112 "Flag warnings of stores to dead variables."),
Ted Kremenek786d3372007-09-25 18:05:45 +0000113 clEnumValN(WarnUninitVals, "warn-uninit-values",
Ted Kremenek2bf55142007-09-17 20:49:30 +0000114 "Flag warnings of uses of unitialized variables."),
Ted Kremeneka4532552008-01-16 18:21:49 +0000115 clEnumValN(AnalysisGRConstants, "grconstants",
Chris Lattner3a2781c2008-01-10 01:41:55 +0000116 "Perform path-sensitive constant propagation."),
Ted Kremenekbfa82c42007-10-16 23:37:27 +0000117 clEnumValN(TestSerialization, "test-pickling",
118 "Run prototype serializtion code."),
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 clEnumValN(EmitLLVM, "emit-llvm",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000120 "Build ASTs then convert to LLVM, emit .ll file"),
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000121 clEnumValN(EmitBC, "emit-llvm-bc",
122 "Build ASTs then convert to LLVM, emit .bc file"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000123 clEnumValN(SerializeAST, "serialize",
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000124 "Build ASTs and emit .ast file"),
Chris Lattner77cd2a02007-10-11 00:43:27 +0000125 clEnumValN(RewriteTest, "rewrite-test",
126 "Playground for the code rewriter"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 clEnumValEnd));
128
Ted Kremenekccc76472007-12-19 19:47:59 +0000129
130static llvm::cl::opt<std::string>
131OutputFile("o",
Ted Kremenek50b56412007-12-19 19:50:41 +0000132 llvm::cl::value_desc("path"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000133 llvm::cl::desc("Specify output file (for --serialize, this is a directory)"));
134
Ted Kremenek41193e42007-09-26 19:42:19 +0000135static llvm::cl::opt<bool>
136VerifyDiagnostics("verify",
137 llvm::cl::desc("Verify emitted diagnostics and warnings."));
138
Reid Spencer5f016e22007-07-11 17:01:13 +0000139//===----------------------------------------------------------------------===//
140// Language Options
141//===----------------------------------------------------------------------===//
142
143enum LangKind {
144 langkind_unspecified,
145 langkind_c,
146 langkind_c_cpp,
147 langkind_cxx,
148 langkind_cxx_cpp,
149 langkind_objc,
150 langkind_objc_cpp,
151 langkind_objcxx,
152 langkind_objcxx_cpp
153};
154
155/* TODO: GCC also accepts:
156 c-header c++-header objective-c-header objective-c++-header
157 assembler assembler-with-cpp
158 ada, f77*, ratfor (!), f95, java, treelang
159 */
160static llvm::cl::opt<LangKind>
161BaseLang("x", llvm::cl::desc("Base language to compile"),
162 llvm::cl::init(langkind_unspecified),
163 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
164 clEnumValN(langkind_cxx, "c++", "C++"),
165 clEnumValN(langkind_objc, "objective-c", "Objective C"),
166 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
167 clEnumValN(langkind_c_cpp, "c-cpp-output",
168 "Preprocessed C"),
169 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
170 "Preprocessed C++"),
171 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
172 "Preprocessed Objective C"),
173 clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output",
174 "Preprocessed Objective C++"),
175 clEnumValEnd));
176
177static llvm::cl::opt<bool>
178LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
179 llvm::cl::Hidden);
180static llvm::cl::opt<bool>
181LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
182 llvm::cl::Hidden);
183
Ted Kremenek8904f152007-12-05 23:49:08 +0000184/// InitializeBaseLanguage - Handle the -x foo options.
185static void InitializeBaseLanguage() {
186 if (LangObjC)
187 BaseLang = langkind_objc;
188 else if (LangObjCXX)
189 BaseLang = langkind_objcxx;
190}
191
192static LangKind GetLanguage(const std::string &Filename) {
193 if (BaseLang != langkind_unspecified)
194 return BaseLang;
195
196 std::string::size_type DotPos = Filename.rfind('.');
197
198 if (DotPos == std::string::npos) {
199 BaseLang = langkind_c; // Default to C if no extension.
Chris Lattner9b2f6c42008-01-04 19:12:28 +0000200 return langkind_c;
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 }
202
Ted Kremenek8904f152007-12-05 23:49:08 +0000203 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
204 // C header: .h
205 // C++ header: .hh or .H;
206 // assembler no preprocessing: .s
207 // assembler: .S
208 if (Ext == "c")
209 return langkind_c;
210 else if (Ext == "i")
211 return langkind_c_cpp;
212 else if (Ext == "ii")
213 return langkind_cxx_cpp;
214 else if (Ext == "m")
215 return langkind_objc;
216 else if (Ext == "mi")
217 return langkind_objc_cpp;
218 else if (Ext == "mm" || Ext == "M")
219 return langkind_objcxx;
220 else if (Ext == "mii")
221 return langkind_objcxx_cpp;
222 else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
223 Ext == "c++" || Ext == "cp" || Ext == "cxx")
224 return langkind_cxx;
225 else
226 return langkind_c;
227}
228
229
230static void InitializeLangOptions(LangOptions &Options, LangKind LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 // FIXME: implement -fpreprocessed mode.
232 bool NoPreprocess = false;
233
Ted Kremenek8904f152007-12-05 23:49:08 +0000234 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 default: assert(0 && "Unknown language kind!");
236 case langkind_c_cpp:
237 NoPreprocess = true;
238 // FALLTHROUGH
239 case langkind_c:
240 break;
241 case langkind_cxx_cpp:
242 NoPreprocess = true;
243 // FALLTHROUGH
244 case langkind_cxx:
245 Options.CPlusPlus = 1;
246 break;
247 case langkind_objc_cpp:
248 NoPreprocess = true;
249 // FALLTHROUGH
250 case langkind_objc:
251 Options.ObjC1 = Options.ObjC2 = 1;
252 break;
253 case langkind_objcxx_cpp:
254 NoPreprocess = true;
255 // FALLTHROUGH
256 case langkind_objcxx:
257 Options.ObjC1 = Options.ObjC2 = 1;
258 Options.CPlusPlus = 1;
259 break;
260 }
261}
262
263/// LangStds - Language standards we support.
264enum LangStds {
265 lang_unspecified,
266 lang_c89, lang_c94, lang_c99,
267 lang_gnu89, lang_gnu99,
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000268 lang_cxx98, lang_gnucxx98,
269 lang_cxx0x, lang_gnucxx0x
Reid Spencer5f016e22007-07-11 17:01:13 +0000270};
271
272static llvm::cl::opt<LangStds>
273LangStd("std", llvm::cl::desc("Language standard to compile for"),
274 llvm::cl::init(lang_unspecified),
275 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
276 clEnumValN(lang_c89, "c90", "ISO C 1990"),
277 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
278 clEnumValN(lang_c94, "iso9899:199409",
279 "ISO C 1990 with amendment 1"),
280 clEnumValN(lang_c99, "c99", "ISO C 1999"),
281// clEnumValN(lang_c99, "c9x", "ISO C 1999"),
282 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
283// clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
284 clEnumValN(lang_gnu89, "gnu89",
285 "ISO C 1990 with GNU extensions (default for C)"),
286 clEnumValN(lang_gnu99, "gnu99",
287 "ISO C 1999 with GNU extensions"),
288 clEnumValN(lang_gnu99, "gnu9x",
289 "ISO C 1999 with GNU extensions"),
290 clEnumValN(lang_cxx98, "c++98",
291 "ISO C++ 1998 with amendments"),
292 clEnumValN(lang_gnucxx98, "gnu++98",
293 "ISO C++ 1998 with amendments and GNU "
294 "extensions (default for C++)"),
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000295 clEnumValN(lang_cxx0x, "c++0x",
296 "Upcoming ISO C++ 200x with amendments"),
297 clEnumValN(lang_gnucxx0x, "gnu++0x",
298 "Upcoming ISO C++ 200x with amendments and GNU "
299 "extensions (default for C++)"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 clEnumValEnd));
301
302static llvm::cl::opt<bool>
303NoOperatorNames("fno-operator-names",
304 llvm::cl::desc("Do not treat C++ operator name keywords as "
305 "synonyms for operators"));
306
Anders Carlssonee98ac52007-10-15 02:50:23 +0000307static llvm::cl::opt<bool>
308PascalStrings("fpascal-strings",
309 llvm::cl::desc("Recognize and construct Pascal-style "
310 "string literals"));
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000311
312static llvm::cl::opt<bool>
313WritableStrings("fwritable-strings",
314 llvm::cl::desc("Store string literals as writable data."));
Anders Carlsson695dbb62007-11-30 04:21:22 +0000315
316static llvm::cl::opt<bool>
317LaxVectorConversions("flax-vector-conversions",
318 llvm::cl::desc("Allow implicit conversions between vectors"
319 " with a different number of elements or "
320 "different element types."));
Reid Spencer5f016e22007-07-11 17:01:13 +0000321// FIXME: add:
322// -ansi
323// -trigraphs
324// -fdollars-in-identifiers
Anders Carlssonee98ac52007-10-15 02:50:23 +0000325// -fpascal-strings
Ted Kremenek8904f152007-12-05 23:49:08 +0000326static void InitializeLanguageStandard(LangOptions &Options, LangKind LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 if (LangStd == lang_unspecified) {
328 // Based on the base language, pick one.
Ted Kremenek8904f152007-12-05 23:49:08 +0000329 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 default: assert(0 && "Unknown base language");
331 case langkind_c:
332 case langkind_c_cpp:
333 case langkind_objc:
334 case langkind_objc_cpp:
335 LangStd = lang_gnu99;
336 break;
337 case langkind_cxx:
338 case langkind_cxx_cpp:
339 case langkind_objcxx:
340 case langkind_objcxx_cpp:
341 LangStd = lang_gnucxx98;
342 break;
343 }
344 }
345
346 switch (LangStd) {
347 default: assert(0 && "Unknown language standard!");
348
349 // Fall through from newer standards to older ones. This isn't really right.
350 // FIXME: Enable specifically the right features based on the language stds.
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000351 case lang_gnucxx0x:
352 case lang_cxx0x:
353 Options.CPlusPlus0x = 1;
354 // FALL THROUGH
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 case lang_gnucxx98:
356 case lang_cxx98:
357 Options.CPlusPlus = 1;
358 Options.CXXOperatorNames = !NoOperatorNames;
Nate Begeman8aebcb72007-11-15 07:30:50 +0000359 Options.Boolean = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 // FALL THROUGH.
361 case lang_gnu99:
362 case lang_c99:
363 Options.Digraphs = 1;
364 Options.C99 = 1;
365 Options.HexFloats = 1;
366 // FALL THROUGH.
367 case lang_gnu89:
368 Options.BCPLComment = 1; // Only for C99/C++.
369 // FALL THROUGH.
370 case lang_c94:
371 case lang_c89:
372 break;
373 }
374
375 Options.Trigraphs = 1; // -trigraphs or -ansi
376 Options.DollarIdents = 1; // FIXME: Really a target property.
Anders Carlssonee98ac52007-10-15 02:50:23 +0000377 Options.PascalStrings = PascalStrings;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000378 Options.WritableStrings = WritableStrings;
Anders Carlsson695dbb62007-11-30 04:21:22 +0000379 Options.LaxVectorConversions = LaxVectorConversions;
Reid Spencer5f016e22007-07-11 17:01:13 +0000380}
381
382//===----------------------------------------------------------------------===//
383// Our DiagnosticClient implementation
384//===----------------------------------------------------------------------===//
385
386// FIXME: Werror should take a list of things, -Werror=foo,bar
387static llvm::cl::opt<bool>
388WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
389
390static llvm::cl::opt<bool>
391WarnOnExtensions("pedantic", llvm::cl::init(false),
392 llvm::cl::desc("Issue a warning on uses of GCC extensions"));
393
394static llvm::cl::opt<bool>
395ErrorOnExtensions("pedantic-errors",
396 llvm::cl::desc("Issue an error on uses of GCC extensions"));
397
398static llvm::cl::opt<bool>
399WarnUnusedMacros("Wunused_macros",
400 llvm::cl::desc("Warn for unused macros in the main translation unit"));
401
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000402static llvm::cl::opt<bool>
403WarnFloatEqual("Wfloat-equal",
404 llvm::cl::desc("Warn about equality comparisons of floating point values."));
405
Ted Kremenek73da5902007-12-17 17:50:07 +0000406static llvm::cl::opt<bool>
407WarnNoFormatNonLiteral("Wno-format-nonliteral",
408 llvm::cl::desc("Do not warn about non-literal format strings."));
409
Chris Lattner116a4b12008-01-23 17:19:46 +0000410static llvm::cl::opt<bool>
411WarnUndefMacros("Wundef",
412 llvm::cl::desc("Warn on use of undefined macros in #if's"));
413
414
Reid Spencer5f016e22007-07-11 17:01:13 +0000415/// InitializeDiagnostics - Initialize the diagnostic object, based on the
416/// current command line option settings.
417static void InitializeDiagnostics(Diagnostic &Diags) {
418 Diags.setWarningsAsErrors(WarningsAsErrors);
419 Diags.setWarnOnExtensions(WarnOnExtensions);
420 Diags.setErrorOnExtensions(ErrorOnExtensions);
421
422 // Silence the "macro is not used" warning unless requested.
423 if (!WarnUnusedMacros)
424 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000425
426 // Silence "floating point comparison" warnings unless requested.
427 if (!WarnFloatEqual)
428 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
Ted Kremenek73da5902007-12-17 17:50:07 +0000429
430 // Silence "format string is not a string literal" warnings if requested
431 if (WarnNoFormatNonLiteral)
Ted Kremenek7c1d3df2007-12-17 17:50:39 +0000432 Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant,
433 diag::MAP_IGNORE);
Chris Lattner116a4b12008-01-23 17:19:46 +0000434 if (!WarnUndefMacros)
435 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000436}
437
438//===----------------------------------------------------------------------===//
Ted Kremenekae360762007-12-03 22:06:55 +0000439// Target Triple Processing.
440//===----------------------------------------------------------------------===//
441
442static llvm::cl::opt<std::string>
443TargetTriple("triple",
444 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)."));
445
446static llvm::cl::list<std::string>
447Archs("arch",
448 llvm::cl::desc("Specify target architecture (e.g. i686)."));
449
450namespace {
451 class TripleProcessor {
452 llvm::StringMap<char> TriplesProcessed;
453 std::vector<std::string>& triples;
454 public:
455 TripleProcessor(std::vector<std::string>& t) : triples(t) {}
456
457 void addTriple(const std::string& t) {
458 if (TriplesProcessed.find(t.c_str(),t.c_str()+t.size()) ==
459 TriplesProcessed.end()) {
460 triples.push_back(t);
461 TriplesProcessed.GetOrCreateValue(t.c_str(),t.c_str()+t.size());
462 }
463 }
464 };
465}
466
467static void CreateTargetTriples(std::vector<std::string>& triples) {
Ted Kremenekae360762007-12-03 22:06:55 +0000468 // Initialize base triple. If a -triple option has been specified, use
469 // that triple. Otherwise, default to the host triple.
Chris Lattner6590d212007-12-12 05:01:48 +0000470 std::string Triple = TargetTriple;
471 if (Triple.empty()) Triple = LLVM_HOSTTRIPLE;
Ted Kremenekae360762007-12-03 22:06:55 +0000472
473 // Decompose the base triple into "arch" and suffix.
Chris Lattner6590d212007-12-12 05:01:48 +0000474 std::string::size_type firstDash = Triple.find("-");
Ted Kremenekae360762007-12-03 22:06:55 +0000475
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000476 if (firstDash == std::string::npos) {
477 fprintf(stderr,
478 "Malformed target triple: \"%s\" ('-' could not be found).\n",
Chris Lattner6590d212007-12-12 05:01:48 +0000479 Triple.c_str());
480 exit(1);
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000481 }
Ted Kremenekae360762007-12-03 22:06:55 +0000482
Chris Lattner6590d212007-12-12 05:01:48 +0000483 std::string suffix(Triple, firstDash+1);
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000484
485 if (suffix.empty()) {
Chris Lattner6590d212007-12-12 05:01:48 +0000486 fprintf(stderr, "Malformed target triple: \"%s\" (no vendor or OS).\n",
487 Triple.c_str());
488 exit(1);
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000489 }
Ted Kremenekae360762007-12-03 22:06:55 +0000490
491 // Create triple cacher.
492 TripleProcessor tp(triples);
493
494 // Add the primary triple to our set of triples if we are using the
495 // host-triple with no archs or using a specified target triple.
496 if (!TargetTriple.getValue().empty() || Archs.empty())
Chris Lattner6590d212007-12-12 05:01:48 +0000497 tp.addTriple(Triple);
Ted Kremenekae360762007-12-03 22:06:55 +0000498
499 for (unsigned i = 0, e = Archs.size(); i !=e; ++i)
500 tp.addTriple(Archs[i] + "-" + suffix);
501}
502
503//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000504// Preprocessor Initialization
505//===----------------------------------------------------------------------===//
506
507// FIXME: Preprocessor builtins to support.
508// -A... - Play with #assertions
509// -undef - Undefine all predefined macros
510
511static llvm::cl::list<std::string>
512D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
513 llvm::cl::desc("Predefine the specified macro"));
514static llvm::cl::list<std::string>
515U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
516 llvm::cl::desc("Undefine the specified macro"));
517
Chris Lattner64299f82008-01-10 01:53:41 +0000518static llvm::cl::list<std::string>
519ImplicitIncludes("include", llvm::cl::value_desc("file"),
520 llvm::cl::desc("Include file before parsing"));
521
522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523// Append a #define line to Buf for Macro. Macro should be of the form XXX,
524// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
525// "#define XXX Y z W". To get a #define with no value, use "XXX=".
526static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
527 const char *Command = "#define ") {
528 Buf.insert(Buf.end(), Command, Command+strlen(Command));
529 if (const char *Equal = strchr(Macro, '=')) {
530 // Turn the = into ' '.
531 Buf.insert(Buf.end(), Macro, Equal);
532 Buf.push_back(' ');
533 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
534 } else {
535 // Push "macroname 1".
536 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
537 Buf.push_back(' ');
538 Buf.push_back('1');
539 }
540 Buf.push_back('\n');
541}
542
Chris Lattner64299f82008-01-10 01:53:41 +0000543/// AddImplicitInclude - Add an implicit #include of the specified file to the
544/// predefines buffer.
545static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){
546 const char *Inc = "#include \"";
547 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
548 Buf.insert(Buf.end(), File.begin(), File.end());
549 Buf.push_back('"');
550 Buf.push_back('\n');
551}
552
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
Chris Lattner53b0dab2007-10-09 22:10:18 +0000554/// InitializePreprocessor - Initialize the preprocessor getting it and the
555/// environment ready to process a single file. This returns the file ID for the
556/// input file. If a failure happens, it returns 0.
557///
558static unsigned InitializePreprocessor(Preprocessor &PP,
559 const std::string &InFile,
Chris Lattner53b0dab2007-10-09 22:10:18 +0000560 std::vector<char> &PredefineBuffer) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000561
Chris Lattnerdee73592007-12-15 20:48:40 +0000562 FileManager &FileMgr = PP.getFileManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000563
Chris Lattner53b0dab2007-10-09 22:10:18 +0000564 // Figure out where to get and map in the main file.
Chris Lattnerdee73592007-12-15 20:48:40 +0000565 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattner53b0dab2007-10-09 22:10:18 +0000566 if (InFile != "-") {
567 const FileEntry *File = FileMgr.getFile(InFile);
Ted Kremenek1036b682007-12-19 23:48:45 +0000568 if (File) SourceMgr.createMainFileID(File, SourceLocation());
569 if (SourceMgr.getMainFileID() == 0) {
Chris Lattner53b0dab2007-10-09 22:10:18 +0000570 fprintf(stderr, "Error reading '%s'!\n",InFile.c_str());
571 return 0;
572 }
573 } else {
574 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
Ted Kremenek1036b682007-12-19 23:48:45 +0000575 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
576 if (SourceMgr.getMainFileID() == 0) {
Chris Lattner53b0dab2007-10-09 22:10:18 +0000577 fprintf(stderr, "Error reading standard input! Empty?\n");
578 return 0;
579 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 }
581
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 // Add macros from the command line.
583 // FIXME: Should traverse the #define/#undef lists in parallel.
584 for (unsigned i = 0, e = D_macros.size(); i != e; ++i)
Chris Lattner53b0dab2007-10-09 22:10:18 +0000585 DefineBuiltinMacro(PredefineBuffer, D_macros[i].c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 for (unsigned i = 0, e = U_macros.size(); i != e; ++i)
Chris Lattner53b0dab2007-10-09 22:10:18 +0000587 DefineBuiltinMacro(PredefineBuffer, U_macros[i].c_str(), "#undef ");
588
Chris Lattner64299f82008-01-10 01:53:41 +0000589 // FIXME: Read any files specified by -imacros.
590
591 // Add implicit #includes from -include.
592 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
593 AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000594
595 // Null terminate PredefinedBuffer and add it.
596 PredefineBuffer.push_back(0);
597 PP.setPredefines(&PredefineBuffer[0]);
598
599 // Once we've read this, we're done.
Ted Kremenek1036b682007-12-19 23:48:45 +0000600 return SourceMgr.getMainFileID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000601}
602
603//===----------------------------------------------------------------------===//
604// Preprocessor include path information.
605//===----------------------------------------------------------------------===//
606
607// This tool exports a large number of command line options to control how the
608// preprocessor searches for header files. At root, however, the Preprocessor
609// object takes a very simple interface: a list of directories to search for
610//
611// FIXME: -nostdinc,-nostdinc++
Chris Lattner0c946412007-08-26 17:47:35 +0000612// FIXME: -imultilib
Reid Spencer5f016e22007-07-11 17:01:13 +0000613//
Chris Lattner64299f82008-01-10 01:53:41 +0000614// FIXME: -imacros
Reid Spencer5f016e22007-07-11 17:01:13 +0000615
616static llvm::cl::opt<bool>
617nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
618
619// Various command line options. These four add directories to each chain.
620static llvm::cl::list<std::string>
621F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
622 llvm::cl::desc("Add directory to framework include search path"));
623static llvm::cl::list<std::string>
624I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
625 llvm::cl::desc("Add directory to include search path"));
626static llvm::cl::list<std::string>
627idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
628 llvm::cl::desc("Add directory to AFTER include search path"));
629static llvm::cl::list<std::string>
630iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
631 llvm::cl::desc("Add directory to QUOTE include search path"));
632static llvm::cl::list<std::string>
633isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
634 llvm::cl::desc("Add directory to SYSTEM include search path"));
635
636// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
637static llvm::cl::list<std::string>
638iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
639 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
640static llvm::cl::list<std::string>
641iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
642 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
643static llvm::cl::list<std::string>
644iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
645 llvm::cl::Prefix,
646 llvm::cl::desc("Set directory to include search path with prefix"));
647
Chris Lattner0c946412007-08-26 17:47:35 +0000648static llvm::cl::opt<std::string>
649isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
650 llvm::cl::desc("Set the system root directory (usually /)"));
651
Reid Spencer5f016e22007-07-11 17:01:13 +0000652// Finally, implement the code that groks the options above.
653enum IncludeDirGroup {
654 Quoted = 0,
655 Angled,
656 System,
657 After
658};
659
660static std::vector<DirectoryLookup> IncludeGroup[4];
661
662/// AddPath - Add the specified path to the specified group list.
663///
664static void AddPath(const std::string &Path, IncludeDirGroup Group,
665 bool isCXXAware, bool isUserSupplied,
Chris Lattner822da612007-12-17 06:36:45 +0000666 bool isFramework, HeaderSearch &HS) {
Chris Lattnerb3de9e72007-12-09 00:39:55 +0000667 assert(!Path.empty() && "can't handle empty path here");
Chris Lattner822da612007-12-17 06:36:45 +0000668 FileManager &FM = HS.getFileMgr();
Chris Lattnerb3de9e72007-12-09 00:39:55 +0000669
Chris Lattnerd6655272007-12-17 05:59:27 +0000670 // Compute the actual path, taking into consideration -isysroot.
671 llvm::SmallString<256> MappedPath;
Chris Lattner0c946412007-08-26 17:47:35 +0000672
Chris Lattnerd6655272007-12-17 05:59:27 +0000673 // Handle isysroot.
674 if (Group == System) {
Chris Lattner60e4e2b2007-12-17 06:51:34 +0000675 // FIXME: Portability. This should be a sys::Path interface, this doesn't
676 // handle things like C:\ right, nor win32 \\network\device\blah.
Chris Lattnerd6655272007-12-17 05:59:27 +0000677 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
678 MappedPath.append(isysroot.begin(), isysroot.end());
679 if (Path[0] != '/') // If in the system group, add a /.
680 MappedPath.push_back('/');
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 }
682
Chris Lattnerd6655272007-12-17 05:59:27 +0000683 MappedPath.append(Path.begin(), Path.end());
684
685 // Compute the DirectoryLookup type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 DirectoryLookup::DirType Type;
687 if (Group == Quoted || Group == Angled)
688 Type = DirectoryLookup::NormalHeaderDir;
689 else if (isCXXAware)
690 Type = DirectoryLookup::SystemHeaderDir;
691 else
692 Type = DirectoryLookup::ExternCSystemHeaderDir;
693
Chris Lattnerd6655272007-12-17 05:59:27 +0000694
695 // If the directory exists, add it.
696 if (const DirectoryEntry *DE = FM.getDirectory(&MappedPath[0],
697 &MappedPath[0]+
698 MappedPath.size())) {
699 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
700 isFramework));
701 return;
702 }
703
Chris Lattnerdf772332007-12-17 07:52:39 +0000704 // Check to see if this is an apple-style headermap (which are not allowed to
705 // be frameworks).
706 if (!isFramework) {
707 if (const FileEntry *FE = FM.getFile(&MappedPath[0],
708 &MappedPath[0]+MappedPath.size())) {
Chris Lattner1bfd4a62007-12-17 18:34:53 +0000709 if (const HeaderMap *HM = HS.CreateHeaderMap(FE)) {
710 // It is a headermap, add it to the search path.
Chris Lattnerdf772332007-12-17 07:52:39 +0000711 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
712 return;
713 }
Chris Lattner822da612007-12-17 06:36:45 +0000714 }
715 }
716
Chris Lattnerd6655272007-12-17 05:59:27 +0000717 if (Verbose)
718 fprintf(stderr, "ignoring nonexistent directory \"%s\"\n", Path.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000719}
720
721/// RemoveDuplicates - If there are duplicate directory entries in the specified
722/// search list, remove the later (dead) ones.
723static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList) {
Chris Lattner8f3dab82007-12-15 23:20:07 +0000724 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
Chris Lattnerdf772332007-12-17 07:52:39 +0000725 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
Chris Lattnerb94c7072007-12-17 06:44:29 +0000726 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattnerb94c7072007-12-17 06:44:29 +0000728 if (SearchList[i].isNormalDir()) {
729 // If this isn't the first time we've seen this dir, remove it.
730 if (SeenDirs.insert(SearchList[i].getDir()))
731 continue;
732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 if (Verbose)
734 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
735 SearchList[i].getDir()->getName());
Chris Lattnerdf772332007-12-17 07:52:39 +0000736 } else if (SearchList[i].isFramework()) {
737 // If this isn't the first time we've seen this framework dir, remove it.
738 if (SeenFrameworkDirs.insert(SearchList[i].getFrameworkDir()))
739 continue;
740
741 if (Verbose)
742 fprintf(stderr, "ignoring duplicate framework \"%s\"\n",
743 SearchList[i].getFrameworkDir()->getName());
744
Chris Lattnerb94c7072007-12-17 06:44:29 +0000745 } else {
746 assert(SearchList[i].isHeaderMap() && "Not a headermap or normal dir?");
747 // If this isn't the first time we've seen this headermap, remove it.
748 if (SeenHeaderMaps.insert(SearchList[i].getHeaderMap()))
749 continue;
750
751 if (Verbose)
752 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
753 SearchList[i].getDir()->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 }
Chris Lattnerb94c7072007-12-17 06:44:29 +0000755
756 // This is reached if the current entry is a duplicate.
757 SearchList.erase(SearchList.begin()+i);
758 --i;
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 }
760}
761
762/// InitializeIncludePaths - Process the -I options and set them in the
763/// HeaderSearch object.
764static void InitializeIncludePaths(HeaderSearch &Headers, FileManager &FM,
Chris Lattner4f037832007-12-05 23:24:17 +0000765 const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 // Handle -F... options.
767 for (unsigned i = 0, e = F_dirs.size(); i != e; ++i)
Chris Lattner822da612007-12-17 06:36:45 +0000768 AddPath(F_dirs[i], Angled, false, true, true, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000769
770 // Handle -I... options.
Chris Lattner4f037832007-12-05 23:24:17 +0000771 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i)
Chris Lattner822da612007-12-17 06:36:45 +0000772 AddPath(I_dirs[i], Angled, false, true, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000773
774 // Handle -idirafter... options.
775 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
Chris Lattner822da612007-12-17 06:36:45 +0000776 AddPath(idirafter_dirs[i], After, false, true, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000777
778 // Handle -iquote... options.
779 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
Chris Lattner822da612007-12-17 06:36:45 +0000780 AddPath(iquote_dirs[i], Quoted, false, true, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000781
782 // Handle -isystem... options.
783 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
Chris Lattner822da612007-12-17 06:36:45 +0000784 AddPath(isystem_dirs[i], System, false, true, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000785
786 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
787 // parallel, processing the values in order of occurance to get the right
788 // prefixes.
789 {
790 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
791 unsigned iprefix_idx = 0;
792 unsigned iwithprefix_idx = 0;
793 unsigned iwithprefixbefore_idx = 0;
794 bool iprefix_done = iprefix_vals.empty();
795 bool iwithprefix_done = iwithprefix_vals.empty();
796 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
797 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
798 if (!iprefix_done &&
799 (iwithprefix_done ||
800 iprefix_vals.getPosition(iprefix_idx) <
801 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
802 (iwithprefixbefore_done ||
803 iprefix_vals.getPosition(iprefix_idx) <
804 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
805 Prefix = iprefix_vals[iprefix_idx];
806 ++iprefix_idx;
807 iprefix_done = iprefix_idx == iprefix_vals.size();
808 } else if (!iwithprefix_done &&
809 (iwithprefixbefore_done ||
810 iwithprefix_vals.getPosition(iwithprefix_idx) <
811 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
812 AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
Chris Lattner822da612007-12-17 06:36:45 +0000813 System, false, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 ++iwithprefix_idx;
815 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
816 } else {
817 AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
Chris Lattner822da612007-12-17 06:36:45 +0000818 Angled, false, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 ++iwithprefixbefore_idx;
820 iwithprefixbefore_done =
821 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
822 }
823 }
824 }
825
826 // FIXME: Add contents of the CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
827 // OBJC_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH environment variables.
828
829 // FIXME: temporary hack: hard-coded paths.
830 // FIXME: get these from the target?
831 if (!nostdinc) {
832 if (Lang.CPlusPlus) {
Chris Lattner822da612007-12-17 06:36:45 +0000833 AddPath("/usr/include/c++/4.0.0", System, true, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false,
Chris Lattner822da612007-12-17 06:36:45 +0000835 false, Headers);
836 AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false,
837 Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 }
839
Chris Lattner822da612007-12-17 06:36:45 +0000840 AddPath("/usr/local/include", System, false, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 // leopard
842 AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System,
Chris Lattner822da612007-12-17 06:36:45 +0000843 false, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 AddPath("/usr/lib/gcc/powerpc-apple-darwin9/4.0.1/include",
Chris Lattner822da612007-12-17 06:36:45 +0000845 System, false, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 AddPath("/usr/lib/gcc/powerpc-apple-darwin9/"
847 "4.0.1/../../../../powerpc-apple-darwin0/include",
Chris Lattner822da612007-12-17 06:36:45 +0000848 System, false, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000849
850 // tiger
851 AddPath("/usr/lib/gcc/i686-apple-darwin8/4.0.1/include", System,
Chris Lattner822da612007-12-17 06:36:45 +0000852 false, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 AddPath("/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include",
Chris Lattner822da612007-12-17 06:36:45 +0000854 System, false, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 AddPath("/usr/lib/gcc/powerpc-apple-darwin8/"
856 "4.0.1/../../../../powerpc-apple-darwin8/include",
Chris Lattner822da612007-12-17 06:36:45 +0000857 System, false, false, false, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000858
Lauro Ramos Venancio397cbf22008-01-21 23:08:35 +0000859 // Ubuntu 7.10 - Gutsy Gibbon
860 AddPath("/usr/lib/gcc/i486-linux-gnu/4.1.3/include", System,
861 false, false, false, Headers);
862
Chris Lattner822da612007-12-17 06:36:45 +0000863 AddPath("/usr/include", System, false, false, false, Headers);
864 AddPath("/System/Library/Frameworks", System, true, false, true, Headers);
865 AddPath("/Library/Frameworks", System, true, false, true, Headers);
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 }
867
868 // Now that we have collected all of the include paths, merge them all
869 // together and tell the preprocessor about them.
870
871 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
872 std::vector<DirectoryLookup> SearchList;
873 SearchList = IncludeGroup[Angled];
874 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
875 IncludeGroup[System].end());
876 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
877 IncludeGroup[After].end());
878 RemoveDuplicates(SearchList);
879 RemoveDuplicates(IncludeGroup[Quoted]);
880
881 // Prepend QUOTED list on the search list.
882 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
883 IncludeGroup[Quoted].end());
884
885
886 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
887 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
888 DontSearchCurDir);
889
890 // If verbose, print the list of directories that will be searched.
891 if (Verbose) {
892 fprintf(stderr, "#include \"...\" search starts here:\n");
893 unsigned QuotedIdx = IncludeGroup[Quoted].size();
894 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
895 if (i == QuotedIdx)
896 fprintf(stderr, "#include <...> search starts here:\n");
Chris Lattner3af66a92007-12-17 17:57:27 +0000897 const char *Name = SearchList[i].getName();
898 const char *Suffix;
Chris Lattner0048b512007-12-17 17:42:26 +0000899 if (SearchList[i].isNormalDir())
Chris Lattner3af66a92007-12-17 17:57:27 +0000900 Suffix = "";
Chris Lattner0048b512007-12-17 17:42:26 +0000901 else if (SearchList[i].isFramework())
Chris Lattner3af66a92007-12-17 17:57:27 +0000902 Suffix = " (framework directory)";
Chris Lattner0048b512007-12-17 17:42:26 +0000903 else {
904 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
Chris Lattner3af66a92007-12-17 17:57:27 +0000905 Suffix = " (headermap)";
Chris Lattner0048b512007-12-17 17:42:26 +0000906 }
Chris Lattner3af66a92007-12-17 17:57:27 +0000907 fprintf(stderr, " %s%s\n", Name, Suffix);
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 }
Chris Lattner80e17152007-12-15 23:11:06 +0000909 fprintf(stderr, "End of search list.\n");
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 }
911}
912
913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914//===----------------------------------------------------------------------===//
915// Basic Parser driver
916//===----------------------------------------------------------------------===//
917
Ted Kremenek95041a22007-12-19 22:51:13 +0000918static void ParseFile(Preprocessor &PP, MinimalAction *PA){
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 Parser P(PP, *PA);
Ted Kremenek95041a22007-12-19 22:51:13 +0000920 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +0000921
922 // Parsing the specified input file.
923 P.ParseTranslationUnit();
924 delete PA;
925}
926
927//===----------------------------------------------------------------------===//
928// Main driver
929//===----------------------------------------------------------------------===//
930
Ted Kremenekdb094a22007-12-05 18:27:04 +0000931/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
932/// action. These consumers can operate on both ASTs that are freshly
933/// parsed from source files as well as those deserialized from Bitcode.
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000934static ASTConsumer* CreateASTConsumer(const std::string& InFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000935 Diagnostic& Diag, FileManager& FileMgr,
Chris Lattnere66b65c2008-02-06 01:42:25 +0000936 const LangOptions& LangOpts,
937 llvm::Module *&DestModule) {
Ted Kremenekdb094a22007-12-05 18:27:04 +0000938 switch (ProgAction) {
939 default:
940 return NULL;
941
942 case ASTPrint:
943 return CreateASTPrinter();
944
945 case ASTDump:
946 return CreateASTDumper();
947
948 case ASTView:
949 return CreateASTViewer();
950
951 case ParseCFGDump:
952 case ParseCFGView:
953 return CreateCFGDumper(ProgAction == ParseCFGView);
954
955 case AnalysisLiveVariables:
956 return CreateLiveVarAnalyzer();
957
958 case WarnDeadStores:
959 return CreateDeadStoreChecker(Diag);
960
961 case WarnUninitVals:
962 return CreateUnitValsChecker(Diag);
963
Ted Kremenekee985462008-01-16 18:18:48 +0000964 case AnalysisGRConstants:
965 return CreateGRConstants();
Ted Kremeneke603df42008-01-08 18:04:06 +0000966
Ted Kremenekdb094a22007-12-05 18:27:04 +0000967 case TestSerialization:
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000968 return CreateSerializationTest(Diag, FileMgr, LangOpts);
Ted Kremenekdb094a22007-12-05 18:27:04 +0000969
970 case EmitLLVM:
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000971 case EmitBC:
Chris Lattnere66b65c2008-02-06 01:42:25 +0000972 DestModule = new llvm::Module(InFile);
973 return CreateLLVMCodeGen(Diag, LangOpts, DestModule);
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000974
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000975 case SerializeAST:
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000976 // FIXME: Allow user to tailor where the file is written.
Ted Kremenek1036b682007-12-19 23:48:45 +0000977 return CreateASTSerializer(InFile, OutputFile, Diag, LangOpts);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000978
Ted Kremenekdb094a22007-12-05 18:27:04 +0000979 case RewriteTest:
Fariborz Jahanianb4b2f0c2008-01-18 01:15:54 +0000980 return CreateCodeRewriterTest(InFile, Diag);
Ted Kremenekdb094a22007-12-05 18:27:04 +0000981 }
982}
983
Reid Spencer5f016e22007-07-11 17:01:13 +0000984/// ProcessInputFile - Process a single input file with the specified state.
985///
Ted Kremenek7dcc9682007-12-19 22:32:34 +0000986static void ProcessInputFile(Preprocessor &PP, const std::string &InFile,
Chris Lattnerdee73592007-12-15 20:48:40 +0000987 TextDiagnostics &OurDiagnosticClient) {
Ted Kremenekd39bcd82007-09-26 18:39:29 +0000988
989 ASTConsumer* Consumer = NULL;
Chris Lattnerbd247762007-07-22 06:05:44 +0000990 bool ClearSourceMgr = false;
Chris Lattnere66b65c2008-02-06 01:42:25 +0000991 llvm::Module *CodeGenModule = 0;
Ted Kremenekd39bcd82007-09-26 18:39:29 +0000992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 switch (ProgAction) {
994 default:
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000995 Consumer = CreateASTConsumer(InFile,
996 PP.getDiagnostics(),
Chris Lattnerdee73592007-12-15 20:48:40 +0000997 PP.getFileManager(),
Chris Lattnere66b65c2008-02-06 01:42:25 +0000998 PP.getLangOptions(),
999 CodeGenModule);
Ted Kremenekdb094a22007-12-05 18:27:04 +00001000
1001 if (!Consumer) {
1002 fprintf(stderr, "Unexpected program action!\n");
1003 return;
1004 }
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001005
Ted Kremenekdb094a22007-12-05 18:27:04 +00001006 break;
1007
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 case DumpTokens: { // Token dump mode.
Chris Lattnerd2177732007-07-20 16:59:19 +00001009 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001011 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 do {
1013 PP.Lex(Tok);
1014 PP.DumpToken(Tok, true);
1015 fprintf(stderr, "\n");
Chris Lattner057aaf62007-10-09 18:03:42 +00001016 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001017 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 break;
1019 }
1020 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
Chris Lattnerd2177732007-07-20 16:59:19 +00001021 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001023 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 do {
1025 PP.Lex(Tok);
Chris Lattner057aaf62007-10-09 18:03:42 +00001026 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001027 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 break;
1029 }
1030
1031 case PrintPreprocessedInput: // -E mode.
Chris Lattnere988bc22008-01-27 23:55:11 +00001032 DoPrintPreprocessedInput(PP, OutputFile);
Chris Lattnerbd247762007-07-22 06:05:44 +00001033 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 break;
1035
1036 case ParseNoop: // -parse-noop
Ted Kremenek95041a22007-12-19 22:51:13 +00001037 ParseFile(PP, new MinimalAction(PP.getIdentifierTable()));
Chris Lattnerbd247762007-07-22 06:05:44 +00001038 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 break;
1040
1041 case ParsePrintCallbacks:
Ted Kremenek95041a22007-12-19 22:51:13 +00001042 ParseFile(PP, CreatePrintParserActionsAction(PP.getIdentifierTable()));
Chris Lattnerbd247762007-07-22 06:05:44 +00001043 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 break;
Ted Kremenek44579782007-09-25 18:37:20 +00001045
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001046 case ParseSyntaxOnly: // -fsyntax-only
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001047 Consumer = new ASTConsumer();
Ted Kremenek2bf55142007-09-17 20:49:30 +00001048 break;
Chris Lattner580980b2007-09-16 19:46:59 +00001049 }
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001050
1051 if (Consumer) {
Ted Kremenek9f3d9422007-09-26 20:14:22 +00001052 if (VerifyDiagnostics)
Ted Kremenek95041a22007-12-19 22:51:13 +00001053 exit(CheckASTConsumer(PP, Consumer));
Chris Lattner31e6c7d2007-11-03 06:24:16 +00001054
1055 // This deletes Consumer.
Ted Kremenek95041a22007-12-19 22:51:13 +00001056 ParseAST(PP, Consumer, Stats);
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 }
Chris Lattnere66b65c2008-02-06 01:42:25 +00001058
1059 // If running the code generator, finish up now.
1060 if (CodeGenModule) {
1061 std::ostream *Out;
1062 if (OutputFile == "-") {
1063 Out = llvm::cout.stream();
1064 } else if (!OutputFile.empty()) {
1065 Out = new std::ofstream(OutputFile.c_str(),
1066 std::ios_base::binary|std::ios_base::out);
1067 } else if (InFile == "-") {
1068 Out = llvm::cout.stream();
1069 } else {
1070 llvm::sys::Path Path(InFile);
1071 Path.eraseSuffix();
1072 if (ProgAction == EmitLLVM)
1073 Path.appendSuffix("ll");
1074 else if (ProgAction == EmitBC)
1075 Path.appendSuffix("bc");
1076 else
1077 assert(0 && "Unknown action");
1078 Out = new std::ofstream(Path.toString().c_str(),
1079 std::ios_base::binary|std::ios_base::out);
1080 }
1081
1082 if (ProgAction == EmitLLVM) {
1083 CodeGenModule->print(*Out);
1084 } else {
1085 assert(ProgAction == EmitBC);
1086 llvm::WriteBitcodeToFile(CodeGenModule, *Out);
1087 }
1088
1089 if (Out != llvm::cout.stream())
1090 delete Out;
1091 delete CodeGenModule;
1092 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001093
1094 if (Stats) {
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001095 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 PP.PrintStats();
1097 PP.getIdentifierTable().PrintStats();
Chris Lattnerdee73592007-12-15 20:48:40 +00001098 PP.getHeaderSearchInfo().PrintStats();
Chris Lattnerbd247762007-07-22 06:05:44 +00001099 if (ClearSourceMgr)
Chris Lattnerdee73592007-12-15 20:48:40 +00001100 PP.getSourceManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 fprintf(stderr, "\n");
1102 }
Chris Lattnerbd247762007-07-22 06:05:44 +00001103
1104 // For a multi-file compilation, some things are ok with nuking the source
1105 // manager tables, other require stable fileid/macroid's across multiple
1106 // files.
Chris Lattnerdee73592007-12-15 20:48:40 +00001107 if (ClearSourceMgr)
1108 PP.getSourceManager().clearIDTables();
Reid Spencer5f016e22007-07-11 17:01:13 +00001109}
1110
Ted Kremenek20e97482007-12-12 23:41:08 +00001111static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag,
1112 FileManager& FileMgr) {
1113
1114 if (VerifyDiagnostics) {
1115 fprintf(stderr, "-verify does not yet work with serialized ASTs.\n");
1116 exit (1);
1117 }
1118
1119 llvm::sys::Path Filename(InFile);
1120
1121 if (!Filename.isValid()) {
1122 fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str());
1123 exit (1);
1124 }
1125
Ted Kremenekee533642007-12-20 19:47:16 +00001126 llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename,FileMgr));
Ted Kremenekfe4e0152007-12-13 18:11:11 +00001127
1128 if (!TU) {
1129 fprintf(stderr, "error: file '%s' could not be deserialized\n",
1130 InFile.c_str());
1131 exit (1);
1132 }
1133
Ted Kremenek63ea8632007-12-19 19:27:38 +00001134 // Observe that we use the source file name stored in the deserialized
1135 // translation unit, rather than InFile.
Chris Lattnere66b65c2008-02-06 01:42:25 +00001136 llvm::Module *DestModule;
Ted Kremenekee533642007-12-20 19:47:16 +00001137 llvm::OwningPtr<ASTConsumer>
Chris Lattnere66b65c2008-02-06 01:42:25 +00001138 Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOpts(),
1139 DestModule));
Ted Kremenek20e97482007-12-12 23:41:08 +00001140
1141 if (!Consumer) {
1142 fprintf(stderr, "Unsupported program action with serialized ASTs!\n");
1143 exit (1);
1144 }
1145
Ted Kremenek95041a22007-12-19 22:51:13 +00001146 Consumer->Initialize(*TU->getContext());
Ted Kremenek20e97482007-12-12 23:41:08 +00001147
Chris Lattnere66b65c2008-02-06 01:42:25 +00001148 // FIXME: We need to inform Consumer about completed TagDecls as well.
Ted Kremenek20e97482007-12-12 23:41:08 +00001149 for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
1150 Consumer->HandleTopLevelDecl(*I);
Ted Kremenek20e97482007-12-12 23:41:08 +00001151}
1152
1153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154static llvm::cl::list<std::string>
1155InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1156
Ted Kremenek20e97482007-12-12 23:41:08 +00001157static bool isSerializedFile(const std::string& InFile) {
1158 if (InFile.size() < 4)
1159 return false;
1160
1161 const char* s = InFile.c_str()+InFile.size()-4;
1162
1163 return s[0] == '.' &&
1164 s[1] == 'a' &&
1165 s[2] == 's' &&
1166 s[3] == 't';
1167}
1168
Reid Spencer5f016e22007-07-11 17:01:13 +00001169
1170int main(int argc, char **argv) {
1171 llvm::cl::ParseCommandLineOptions(argc, argv, " llvm cfe\n");
1172 llvm::sys::PrintStackTraceOnErrorSignal();
1173
1174 // If no input was specified, read from stdin.
1175 if (InputFilenames.empty())
1176 InputFilenames.push_back("-");
Ted Kremenek31e703b2007-12-11 23:28:38 +00001177
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 // Create a file manager object to provide access to and cache the filesystem.
1179 FileManager FileMgr;
1180
Ted Kremenek31e703b2007-12-11 23:28:38 +00001181 // Create the diagnostic client for reporting errors or for
1182 // implementing -verify.
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 std::auto_ptr<TextDiagnostics> DiagClient;
Ted Kremenek9f3d9422007-09-26 20:14:22 +00001184 if (!VerifyDiagnostics) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 // Print diagnostics to stderr by default.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001186 DiagClient.reset(new TextDiagnosticPrinter());
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 } else {
1188 // When checking diagnostics, just buffer them up.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001189 DiagClient.reset(new TextDiagnosticBuffer());
Reid Spencer5f016e22007-07-11 17:01:13 +00001190
1191 if (InputFilenames.size() != 1) {
1192 fprintf(stderr,
Ted Kremenek9f3d9422007-09-26 20:14:22 +00001193 "-verify only works on single input files for now.\n");
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 return 1;
1195 }
1196 }
1197
1198 // Configure our handling of diagnostics.
1199 Diagnostic Diags(*DiagClient);
Ted Kremenek31e703b2007-12-11 23:28:38 +00001200 InitializeDiagnostics(Diags);
1201
Chris Lattner4f037832007-12-05 23:24:17 +00001202 // -I- is a deprecated GCC feature, scan for it and reject it.
1203 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
1204 if (I_dirs[i] == "-") {
Ted Kremenek2eefd862007-12-11 22:57:35 +00001205 Diags.Report(diag::err_pp_I_dash_not_supported);
Chris Lattner4f037832007-12-05 23:24:17 +00001206 I_dirs.erase(I_dirs.begin()+i);
1207 --i;
1208 }
1209 }
1210
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenek31e703b2007-12-11 23:28:38 +00001212 const std::string &InFile = InputFilenames[i];
Ted Kremenek31e703b2007-12-11 23:28:38 +00001213
Ted Kremenek20e97482007-12-12 23:41:08 +00001214 if (isSerializedFile(InFile))
1215 ProcessSerializedFile(InFile,Diags,FileMgr);
1216 else {
1217 /// Create a SourceManager object. This tracks and owns all the file
1218 /// buffers allocated to a translation unit.
1219 SourceManager SourceMgr;
Ted Kremenek31e703b2007-12-11 23:28:38 +00001220
Ted Kremenek20e97482007-12-12 23:41:08 +00001221 // Initialize language options, inferring file types from input filenames.
1222 LangOptions LangInfo;
1223 InitializeBaseLanguage();
1224 LangKind LK = GetLanguage(InFile);
1225 InitializeLangOptions(LangInfo, LK);
1226 InitializeLanguageStandard(LangInfo, LK);
1227
1228 // Process the -I options and set them in the HeaderInfo.
1229 HeaderSearch HeaderInfo(FileMgr);
1230 DiagClient->setHeaderSearch(HeaderInfo);
1231 InitializeIncludePaths(HeaderInfo, FileMgr, LangInfo);
1232
1233 // Get information about the targets being compiled for. Note that this
1234 // pointer and the TargetInfoImpl objects are never deleted by this toy
1235 // driver.
1236 TargetInfo *Target;
1237
1238 // Create triples, and create the TargetInfo.
1239 std::vector<std::string> triples;
1240 CreateTargetTriples(triples);
1241 Target = TargetInfo::CreateTargetInfo(&triples[0],
1242 &triples[0]+triples.size(),
1243 &Diags);
1244
1245 if (Target == 0) {
1246 fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
1247 triples[0].c_str());
1248 fprintf(stderr, "Please use -triple or -arch.\n");
1249 exit(1);
1250 }
1251
1252 // Set up the preprocessor with these options.
1253 Preprocessor PP(Diags, LangInfo, *Target, SourceMgr, HeaderInfo);
1254
1255 std::vector<char> PredefineBuffer;
Ted Kremenek1036b682007-12-19 23:48:45 +00001256 if (!InitializePreprocessor(PP, InFile, PredefineBuffer))
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001257 continue;
1258
Ted Kremenek1036b682007-12-19 23:48:45 +00001259 ProcessInputFile(PP, InFile, *DiagClient);
Ted Kremenek20e97482007-12-12 23:41:08 +00001260 HeaderInfo.ClearFileInfo();
1261
1262 if (Stats)
1263 SourceMgr.PrintStats();
1264 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 }
1266
1267 unsigned NumDiagnostics = Diags.getNumDiagnostics();
1268
1269 if (NumDiagnostics)
1270 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1271 (NumDiagnostics == 1 ? "" : "s"));
1272
1273 if (Stats) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 FileMgr.PrintStats();
1275 fprintf(stderr, "\n");
1276 }
1277
Chris Lattner96f1a642007-07-21 05:40:53 +00001278 return Diags.getNumErrors() != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001279}