blob: 419fa980c698fa00f0b838587b1037a9be6529ff [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"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000027#include "HTMLDiagnostics.h"
Nico Weber0fca0222008-08-22 09:25:22 +000028#include "clang/Driver/InitHeaderSearch.h"
Nico Weberfd54ebc2008-08-05 23:33:20 +000029#include "clang/Driver/TextDiagnosticBuffer.h"
30#include "clang/Driver/TextDiagnosticPrinter.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000031#include "clang/Analysis/PathDiagnostic.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000032#include "clang/AST/Decl.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000033#include "clang/AST/TranslationUnit.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000034#include "clang/CodeGen/ModuleBuilder.h"
Chris Lattnere91c1342008-02-06 00:23:21 +000035#include "clang/Sema/ParseAST.h"
Chris Lattner556beb72007-09-15 22:56:56 +000036#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000037#include "clang/Parse/Parser.h"
38#include "clang/Lex/HeaderSearch.h"
39#include "clang/Basic/FileManager.h"
40#include "clang/Basic/SourceManager.h"
41#include "clang/Basic/TargetInfo.h"
Chris Lattner8f3dab82007-12-15 23:20:07 +000042#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000043#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/MemoryBuffer.h"
45#include "llvm/System/Signals.h"
Ted Kremenekae360762007-12-03 22:06:55 +000046#include "llvm/Config/config.h"
Ted Kremenekee533642007-12-20 19:47:16 +000047#include "llvm/ADT/OwningPtr.h"
Chris Lattnerdcaa0962008-03-03 03:16:03 +000048#include "llvm/System/Path.h"
Ted Kremenek57134332008-08-07 18:14:04 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050using namespace clang;
51
52//===----------------------------------------------------------------------===//
53// Global options.
54//===----------------------------------------------------------------------===//
55
56static llvm::cl::opt<bool>
57Verbose("v", llvm::cl::desc("Enable verbose output"));
58static llvm::cl::opt<bool>
Nate Begemanaabbb122007-12-30 01:38:50 +000059Stats("print-stats",
60 llvm::cl::desc("Print performance metrics and statistics"));
Reid Spencer5f016e22007-07-11 17:01:13 +000061
62enum ProgActions {
Steve Naroffb29b4272008-04-14 22:03:09 +000063 RewriteObjC, // ObjC->C Rewriter.
Chris Lattnerb57e3d42008-05-08 06:52:13 +000064 RewriteMacros, // Expand macros but not #includes.
Ted Kremenek13e479b2008-03-19 07:53:42 +000065 HTMLTest, // HTML displayer testing stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +000066 EmitLLVM, // Emit a .ll file.
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +000067 EmitBC, // Emit a .bc file.
Ted Kremeneka1fa3a12007-12-13 00:37:31 +000068 SerializeAST, // Emit a .ast file.
Ted Kremenek6a340832008-03-18 21:19:49 +000069 EmitHTML, // Translate input source into HTML.
Chris Lattner3b427b32007-10-11 00:18:28 +000070 ASTPrint, // Parse ASTs and print them.
71 ASTDump, // Parse ASTs and dump them.
72 ASTView, // Parse ASTs and view them in Graphviz.
Ted Kremenekbfa82c42007-10-16 23:37:27 +000073 TestSerialization, // Run experimental serialization code.
Reid Spencer5f016e22007-07-11 17:01:13 +000074 ParsePrintCallbacks, // Parse and print each callback.
75 ParseSyntaxOnly, // Parse and perform semantic analysis.
76 ParseNoop, // Parse with noop callbacks.
77 RunPreprocessorOnly, // Just lex, no output.
78 PrintPreprocessedInput, // -E mode.
Ted Kremenekf4381fd2008-07-02 00:03:09 +000079 DumpTokens, // Token dump mode.
80 RunAnalysis // Run one or more source code analyses.
Reid Spencer5f016e22007-07-11 17:01:13 +000081};
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"),
Ted Kremenek6a340832008-03-18 21:19:49 +000099 clEnumValN(EmitHTML, "emit-html",
100 "Output input source as HTML"),
Chris Lattner3b427b32007-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 Lattnerea254db2007-10-11 00:37:43 +0000105 clEnumValN(ASTView, "ast-view",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000106 "Build ASTs and view them with GraphViz"),
Ted Kremenekbfa82c42007-10-16 23:37:27 +0000107 clEnumValN(TestSerialization, "test-pickling",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000108 "Run prototype serialization code"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 clEnumValN(EmitLLVM, "emit-llvm",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000110 "Build ASTs then convert to LLVM, emit .ll file"),
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000111 clEnumValN(EmitBC, "emit-llvm-bc",
112 "Build ASTs then convert to LLVM, emit .bc file"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000113 clEnumValN(SerializeAST, "serialize",
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000114 "Build ASTs and emit .ast file"),
Steve Naroffb29b4272008-04-14 22:03:09 +0000115 clEnumValN(RewriteObjC, "rewrite-objc",
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000116 "Rewrite ObjC into C (code rewriter example)"),
117 clEnumValN(RewriteMacros, "rewrite-macros",
118 "Expand macros without full preprocessing"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 clEnumValEnd));
120
Ted Kremenekccc76472007-12-19 19:47:59 +0000121
122static llvm::cl::opt<std::string>
123OutputFile("o",
Ted Kremenek50b56412007-12-19 19:50:41 +0000124 llvm::cl::value_desc("path"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000125 llvm::cl::desc("Specify output file (for --serialize, this is a directory)"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000126
127//===----------------------------------------------------------------------===//
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000128// Code Generator Options
129//===----------------------------------------------------------------------===//
130static llvm::cl::opt<bool>
131GenerateDebugInfo("g",
132 llvm::cl::desc("Generate source level debug information"));
133
134//===----------------------------------------------------------------------===//
Ted Kremenek55af98c2008-04-14 18:40:58 +0000135// Diagnostic Options
136//===----------------------------------------------------------------------===//
137
Ted Kremenek41193e42007-09-26 19:42:19 +0000138static llvm::cl::opt<bool>
139VerifyDiagnostics("verify",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000140 llvm::cl::desc("Verify emitted diagnostics and warnings"));
Ted Kremenek41193e42007-09-26 19:42:19 +0000141
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000142static llvm::cl::opt<std::string>
143HTMLDiag("html-diags",
144 llvm::cl::desc("Generate HTML to report diagnostics"),
145 llvm::cl::value_desc("HTML directory"));
146
Nico Weberfd54ebc2008-08-05 23:33:20 +0000147static llvm::cl::opt<bool>
148NoShowColumn("fno-show-column",
149 llvm::cl::desc("Do not include column number on diagnostics"));
150
151static llvm::cl::opt<bool>
152NoCaretDiagnostics("fno-caret-diagnostics",
153 llvm::cl::desc("Do not include source line and caret with"
154 " diagnostics"));
155
156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157//===----------------------------------------------------------------------===//
Ted Kremenek55af98c2008-04-14 18:40:58 +0000158// Analyzer Options
159//===----------------------------------------------------------------------===//
160
161static llvm::cl::opt<bool>
162VisualizeEG("visualize-egraph",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000163 llvm::cl::desc("Display static analysis Exploded Graph"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000164
165static llvm::cl::opt<bool>
166AnalyzeAll("checker-opt-analyze-headers",
167 llvm::cl::desc("Force the static analyzer to analyze "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000168 "functions defined in header files"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000169
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000170static llvm::cl::list<Analyses>
171AnalysisList(llvm::cl::desc("Available Source Code Analyses:"),
172llvm::cl::values(
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000173#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000174clEnumValN(NAME, CMDFLAG, DESC),
175#include "Analyses.def"
176clEnumValEnd));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000177
Ted Kremenek55af98c2008-04-14 18:40:58 +0000178//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000179// Language Options
180//===----------------------------------------------------------------------===//
181
182enum LangKind {
183 langkind_unspecified,
184 langkind_c,
185 langkind_c_cpp,
186 langkind_cxx,
187 langkind_cxx_cpp,
188 langkind_objc,
189 langkind_objc_cpp,
190 langkind_objcxx,
191 langkind_objcxx_cpp
192};
193
194/* TODO: GCC also accepts:
195 c-header c++-header objective-c-header objective-c++-header
196 assembler assembler-with-cpp
197 ada, f77*, ratfor (!), f95, java, treelang
198 */
199static llvm::cl::opt<LangKind>
200BaseLang("x", llvm::cl::desc("Base language to compile"),
201 llvm::cl::init(langkind_unspecified),
202 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
203 clEnumValN(langkind_cxx, "c++", "C++"),
204 clEnumValN(langkind_objc, "objective-c", "Objective C"),
205 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
206 clEnumValN(langkind_c_cpp, "c-cpp-output",
207 "Preprocessed C"),
208 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
209 "Preprocessed C++"),
210 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
211 "Preprocessed Objective C"),
212 clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output",
213 "Preprocessed Objective C++"),
214 clEnumValEnd));
215
216static llvm::cl::opt<bool>
217LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
218 llvm::cl::Hidden);
219static llvm::cl::opt<bool>
220LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
221 llvm::cl::Hidden);
222
Ted Kremenek8904f152007-12-05 23:49:08 +0000223/// InitializeBaseLanguage - Handle the -x foo options.
224static void InitializeBaseLanguage() {
225 if (LangObjC)
226 BaseLang = langkind_objc;
227 else if (LangObjCXX)
228 BaseLang = langkind_objcxx;
229}
230
231static LangKind GetLanguage(const std::string &Filename) {
232 if (BaseLang != langkind_unspecified)
233 return BaseLang;
234
235 std::string::size_type DotPos = Filename.rfind('.');
236
237 if (DotPos == std::string::npos) {
238 BaseLang = langkind_c; // Default to C if no extension.
Chris Lattner9b2f6c42008-01-04 19:12:28 +0000239 return langkind_c;
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 }
241
Ted Kremenek8904f152007-12-05 23:49:08 +0000242 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
243 // C header: .h
244 // C++ header: .hh or .H;
245 // assembler no preprocessing: .s
246 // assembler: .S
247 if (Ext == "c")
248 return langkind_c;
249 else if (Ext == "i")
250 return langkind_c_cpp;
251 else if (Ext == "ii")
252 return langkind_cxx_cpp;
253 else if (Ext == "m")
254 return langkind_objc;
255 else if (Ext == "mi")
256 return langkind_objc_cpp;
257 else if (Ext == "mm" || Ext == "M")
258 return langkind_objcxx;
259 else if (Ext == "mii")
260 return langkind_objcxx_cpp;
261 else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
262 Ext == "c++" || Ext == "cp" || Ext == "cxx")
263 return langkind_cxx;
264 else
265 return langkind_c;
266}
267
268
269static void InitializeLangOptions(LangOptions &Options, LangKind LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 // FIXME: implement -fpreprocessed mode.
271 bool NoPreprocess = false;
272
Ted Kremenek8904f152007-12-05 23:49:08 +0000273 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 default: assert(0 && "Unknown language kind!");
275 case langkind_c_cpp:
276 NoPreprocess = true;
277 // FALLTHROUGH
278 case langkind_c:
279 break;
280 case langkind_cxx_cpp:
281 NoPreprocess = true;
282 // FALLTHROUGH
283 case langkind_cxx:
284 Options.CPlusPlus = 1;
285 break;
286 case langkind_objc_cpp:
287 NoPreprocess = true;
288 // FALLTHROUGH
289 case langkind_objc:
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000290 Options.ObjC1 = Options.ObjC2 = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 break;
292 case langkind_objcxx_cpp:
293 NoPreprocess = true;
294 // FALLTHROUGH
295 case langkind_objcxx:
296 Options.ObjC1 = Options.ObjC2 = 1;
297 Options.CPlusPlus = 1;
298 break;
299 }
300}
301
302/// LangStds - Language standards we support.
303enum LangStds {
304 lang_unspecified,
305 lang_c89, lang_c94, lang_c99,
306 lang_gnu89, lang_gnu99,
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000307 lang_cxx98, lang_gnucxx98,
308 lang_cxx0x, lang_gnucxx0x
Reid Spencer5f016e22007-07-11 17:01:13 +0000309};
310
311static llvm::cl::opt<LangStds>
312LangStd("std", llvm::cl::desc("Language standard to compile for"),
313 llvm::cl::init(lang_unspecified),
314 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
315 clEnumValN(lang_c89, "c90", "ISO C 1990"),
316 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
317 clEnumValN(lang_c94, "iso9899:199409",
318 "ISO C 1990 with amendment 1"),
319 clEnumValN(lang_c99, "c99", "ISO C 1999"),
320// clEnumValN(lang_c99, "c9x", "ISO C 1999"),
321 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
322// clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
323 clEnumValN(lang_gnu89, "gnu89",
324 "ISO C 1990 with GNU extensions (default for C)"),
325 clEnumValN(lang_gnu99, "gnu99",
326 "ISO C 1999 with GNU extensions"),
327 clEnumValN(lang_gnu99, "gnu9x",
328 "ISO C 1999 with GNU extensions"),
329 clEnumValN(lang_cxx98, "c++98",
330 "ISO C++ 1998 with amendments"),
331 clEnumValN(lang_gnucxx98, "gnu++98",
332 "ISO C++ 1998 with amendments and GNU "
333 "extensions (default for C++)"),
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000334 clEnumValN(lang_cxx0x, "c++0x",
335 "Upcoming ISO C++ 200x with amendments"),
336 clEnumValN(lang_gnucxx0x, "gnu++0x",
337 "Upcoming ISO C++ 200x with amendments and GNU "
338 "extensions (default for C++)"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 clEnumValEnd));
340
341static llvm::cl::opt<bool>
342NoOperatorNames("fno-operator-names",
343 llvm::cl::desc("Do not treat C++ operator name keywords as "
344 "synonyms for operators"));
345
Anders Carlssonee98ac52007-10-15 02:50:23 +0000346static llvm::cl::opt<bool>
347PascalStrings("fpascal-strings",
348 llvm::cl::desc("Recognize and construct Pascal-style "
349 "string literals"));
Steve Naroffd62701b2008-02-07 03:50:06 +0000350
351static llvm::cl::opt<bool>
352MSExtensions("fms-extensions",
353 llvm::cl::desc("Accept some non-standard constructs used in "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000354 "Microsoft header files "));
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000355
356static llvm::cl::opt<bool>
357WritableStrings("fwritable-strings",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000358 llvm::cl::desc("Store string literals as writable data"));
Anders Carlsson695dbb62007-11-30 04:21:22 +0000359
360static llvm::cl::opt<bool>
361LaxVectorConversions("flax-vector-conversions",
362 llvm::cl::desc("Allow implicit conversions between vectors"
363 " with a different number of elements or "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000364 "different element types"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000365
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000366// FIXME: This (and all GCC -f options) really come in -f... and
367// -fno-... forms, and additionally support automagic behavior when
368// they are not defined. For example, -fexceptions defaults to on or
369// off depending on the language. We should support this behavior in
370// some form (perhaps just add a facility for distinguishing when an
371// has its default value from when it has been set to its default
372// value).
373static llvm::cl::opt<bool>
374Exceptions("fexceptions",
375 llvm::cl::desc("Enable support for exception handling."));
376
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000377static llvm::cl::opt<bool>
378GNURuntime("fgnu-runtime",
379 llvm::cl::desc("Generate output compatible with the standard GNU Objective-C runtime."));
380
381static llvm::cl::opt<bool>
382NeXTRuntime("fnext-runtime",
383 llvm::cl::desc("Generate output compatible with the NeXT runtime."));
384
Reid Spencer5f016e22007-07-11 17:01:13 +0000385// FIXME: add:
386// -ansi
387// -trigraphs
388// -fdollars-in-identifiers
Anders Carlssonee98ac52007-10-15 02:50:23 +0000389// -fpascal-strings
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +0000390static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
391 TargetInfo *Target) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 if (LangStd == lang_unspecified) {
393 // Based on the base language, pick one.
Ted Kremenek8904f152007-12-05 23:49:08 +0000394 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 default: assert(0 && "Unknown base language");
396 case langkind_c:
397 case langkind_c_cpp:
398 case langkind_objc:
399 case langkind_objc_cpp:
400 LangStd = lang_gnu99;
401 break;
402 case langkind_cxx:
403 case langkind_cxx_cpp:
404 case langkind_objcxx:
405 case langkind_objcxx_cpp:
406 LangStd = lang_gnucxx98;
407 break;
408 }
409 }
410
411 switch (LangStd) {
412 default: assert(0 && "Unknown language standard!");
413
414 // Fall through from newer standards to older ones. This isn't really right.
415 // FIXME: Enable specifically the right features based on the language stds.
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000416 case lang_gnucxx0x:
417 case lang_cxx0x:
418 Options.CPlusPlus0x = 1;
419 // FALL THROUGH
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 case lang_gnucxx98:
421 case lang_cxx98:
422 Options.CPlusPlus = 1;
423 Options.CXXOperatorNames = !NoOperatorNames;
Nate Begeman8aebcb72007-11-15 07:30:50 +0000424 Options.Boolean = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 // FALL THROUGH.
426 case lang_gnu99:
427 case lang_c99:
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 Options.C99 = 1;
429 Options.HexFloats = 1;
430 // FALL THROUGH.
431 case lang_gnu89:
432 Options.BCPLComment = 1; // Only for C99/C++.
433 // FALL THROUGH.
434 case lang_c94:
Chris Lattner3426b9b2008-02-25 04:01:39 +0000435 Options.Digraphs = 1; // C94, C99, C++.
436 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 case lang_c89:
438 break;
439 }
440
Chris Lattnerd658b562008-04-05 06:32:51 +0000441 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
442 Options.ImplicitInt = 1;
443 else
444 Options.ImplicitInt = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 Options.Trigraphs = 1; // -trigraphs or -ansi
446 Options.DollarIdents = 1; // FIXME: Really a target property.
Anders Carlssonee98ac52007-10-15 02:50:23 +0000447 Options.PascalStrings = PascalStrings;
Steve Naroffd62701b2008-02-07 03:50:06 +0000448 Options.Microsoft = MSExtensions;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000449 Options.WritableStrings = WritableStrings;
Anders Carlsson695dbb62007-11-30 04:21:22 +0000450 Options.LaxVectorConversions = LaxVectorConversions;
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000451 Options.Exceptions = Exceptions;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000452
453 if (NeXTRuntime) {
454 Options.NeXTRuntime = 1;
455 } else if (GNURuntime) {
456 Options.NeXTRuntime = 0;
457 } else {
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +0000458 Options.NeXTRuntime = Target->useNeXTRuntimeAsDefault();
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000459 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000460}
461
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000462static llvm::cl::opt<bool>
463ObjCExclusiveGC("fobjc-gc-only",
464 llvm::cl::desc("Use GC exclusively for Objective-C related "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000465 "memory management"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000466
467static llvm::cl::opt<bool>
468ObjCEnableGC("fobjc-gc",
Nico Weberfd54ebc2008-08-05 23:33:20 +0000469 llvm::cl::desc("Enable Objective-C garbage collection"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000470
471void InitializeGCMode(LangOptions &Options) {
472 if (ObjCExclusiveGC)
473 Options.setGCMode(LangOptions::GCOnly);
474 else if (ObjCEnableGC)
475 Options.setGCMode(LangOptions::HybridGC);
476}
477
478
Reid Spencer5f016e22007-07-11 17:01:13 +0000479//===----------------------------------------------------------------------===//
480// Our DiagnosticClient implementation
481//===----------------------------------------------------------------------===//
482
483// FIXME: Werror should take a list of things, -Werror=foo,bar
484static llvm::cl::opt<bool>
485WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
486
487static llvm::cl::opt<bool>
Chris Lattner5b4681c2008-05-29 15:36:45 +0000488SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings"));
489
490static llvm::cl::opt<bool>
Reid Spencer5f016e22007-07-11 17:01:13 +0000491WarnOnExtensions("pedantic", llvm::cl::init(false),
492 llvm::cl::desc("Issue a warning on uses of GCC extensions"));
493
494static llvm::cl::opt<bool>
495ErrorOnExtensions("pedantic-errors",
496 llvm::cl::desc("Issue an error on uses of GCC extensions"));
497
498static llvm::cl::opt<bool>
499WarnUnusedMacros("Wunused_macros",
500 llvm::cl::desc("Warn for unused macros in the main translation unit"));
501
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000502static llvm::cl::opt<bool>
503WarnFloatEqual("Wfloat-equal",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000504 llvm::cl::desc("Warn about equality comparisons of floating point values"));
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000505
Ted Kremenek73da5902007-12-17 17:50:07 +0000506static llvm::cl::opt<bool>
507WarnNoFormatNonLiteral("Wno-format-nonliteral",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000508 llvm::cl::desc("Do not warn about non-literal format strings"));
Ted Kremenek73da5902007-12-17 17:50:07 +0000509
Chris Lattner116a4b12008-01-23 17:19:46 +0000510static llvm::cl::opt<bool>
511WarnUndefMacros("Wundef",
512 llvm::cl::desc("Warn on use of undefined macros in #if's"));
513
Chris Lattner37d10842008-05-05 21:18:06 +0000514static llvm::cl::opt<bool>
Ted Kremenek358256d2008-05-30 16:42:02 +0000515WarnImplicitFunctionDeclaration("Wimplicit-function-declaration",
516 llvm::cl::desc("Warn about uses of implicitly defined functions"));
Chris Lattner116a4b12008-01-23 17:19:46 +0000517
Reid Spencer5f016e22007-07-11 17:01:13 +0000518/// InitializeDiagnostics - Initialize the diagnostic object, based on the
519/// current command line option settings.
520static void InitializeDiagnostics(Diagnostic &Diags) {
Chris Lattner5b4681c2008-05-29 15:36:45 +0000521 Diags.setIgnoreAllWarnings(SilenceWarnings);
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 Diags.setWarningsAsErrors(WarningsAsErrors);
523 Diags.setWarnOnExtensions(WarnOnExtensions);
524 Diags.setErrorOnExtensions(ErrorOnExtensions);
525
526 // Silence the "macro is not used" warning unless requested.
527 if (!WarnUnusedMacros)
528 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000529
530 // Silence "floating point comparison" warnings unless requested.
531 if (!WarnFloatEqual)
532 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
Ted Kremenek73da5902007-12-17 17:50:07 +0000533
534 // Silence "format string is not a string literal" warnings if requested
535 if (WarnNoFormatNonLiteral)
Ted Kremenek7c1d3df2007-12-17 17:50:39 +0000536 Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant,
537 diag::MAP_IGNORE);
Chris Lattner116a4b12008-01-23 17:19:46 +0000538 if (!WarnUndefMacros)
539 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);
Steve Naroffe7a37302008-02-11 22:40:08 +0000540
Chris Lattner37d10842008-05-05 21:18:06 +0000541 if (!WarnImplicitFunctionDeclaration)
542 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
543 diag::MAP_IGNORE);
544
Steve Naroffe7a37302008-02-11 22:40:08 +0000545 if (MSExtensions) // MS allows unnamed struct/union fields.
546 Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE);
Chris Lattner8aedf192008-05-04 23:52:02 +0000547
548 // If -pedantic-errors is set, turn extensions that warn by default into
549 // errors.
550 if (ErrorOnExtensions) {
551 Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large,
552 diag::MAP_ERROR);
553 Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large,
554 diag::MAP_ERROR);
555 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000556}
557
558//===----------------------------------------------------------------------===//
Ted Kremenekcb330932008-02-18 21:21:23 +0000559// Analysis-specific options.
560//===----------------------------------------------------------------------===//
561
562static llvm::cl::opt<std::string>
563AnalyzeSpecificFunction("analyze-function",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000564 llvm::cl::desc("Run analysis on specific function"));
Ted Kremenekcb330932008-02-18 21:21:23 +0000565
Ted Kremenekffe0f432008-03-07 22:58:01 +0000566static llvm::cl::opt<bool>
Ted Kremenekd71ed262008-04-10 22:16:52 +0000567TrimGraph("trim-egraph",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000568 llvm::cl::desc("Only show error-related paths in the analysis graph"));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000569
Ted Kremenekcb330932008-02-18 21:21:23 +0000570//===----------------------------------------------------------------------===//
Ted Kremenekae360762007-12-03 22:06:55 +0000571// Target Triple Processing.
572//===----------------------------------------------------------------------===//
573
574static llvm::cl::opt<std::string>
575TargetTriple("triple",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000576 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000577
Chris Lattner42e67372008-03-05 01:18:20 +0000578static llvm::cl::opt<std::string>
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000579Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000580
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000581static std::string CreateTargetTriple() {
Ted Kremenekae360762007-12-03 22:06:55 +0000582 // Initialize base triple. If a -triple option has been specified, use
583 // that triple. Otherwise, default to the host triple.
Chris Lattner6590d212007-12-12 05:01:48 +0000584 std::string Triple = TargetTriple;
585 if (Triple.empty()) Triple = LLVM_HOSTTRIPLE;
Ted Kremenekae360762007-12-03 22:06:55 +0000586
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000587 // If -arch foo was specified, remove the architecture from the triple we have
588 // so far and replace it with the specified one.
589 if (Arch.empty())
590 return Triple;
591
Ted Kremenekae360762007-12-03 22:06:55 +0000592 // Decompose the base triple into "arch" and suffix.
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000593 std::string::size_type FirstDashIdx = Triple.find("-");
Ted Kremenekae360762007-12-03 22:06:55 +0000594
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000595 if (FirstDashIdx == std::string::npos) {
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000596 fprintf(stderr,
597 "Malformed target triple: \"%s\" ('-' could not be found).\n",
Chris Lattner6590d212007-12-12 05:01:48 +0000598 Triple.c_str());
599 exit(1);
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000600 }
Ted Kremenekae360762007-12-03 22:06:55 +0000601
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000602 return Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end());
Ted Kremenekae360762007-12-03 22:06:55 +0000603}
604
605//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000606// Preprocessor Initialization
607//===----------------------------------------------------------------------===//
608
609// FIXME: Preprocessor builtins to support.
610// -A... - Play with #assertions
611// -undef - Undefine all predefined macros
612
613static llvm::cl::list<std::string>
614D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
615 llvm::cl::desc("Predefine the specified macro"));
616static llvm::cl::list<std::string>
617U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
618 llvm::cl::desc("Undefine the specified macro"));
619
Chris Lattner64299f82008-01-10 01:53:41 +0000620static llvm::cl::list<std::string>
621ImplicitIncludes("include", llvm::cl::value_desc("file"),
622 llvm::cl::desc("Include file before parsing"));
623
624
Reid Spencer5f016e22007-07-11 17:01:13 +0000625// Append a #define line to Buf for Macro. Macro should be of the form XXX,
626// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
627// "#define XXX Y z W". To get a #define with no value, use "XXX=".
628static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
629 const char *Command = "#define ") {
630 Buf.insert(Buf.end(), Command, Command+strlen(Command));
631 if (const char *Equal = strchr(Macro, '=')) {
632 // Turn the = into ' '.
633 Buf.insert(Buf.end(), Macro, Equal);
634 Buf.push_back(' ');
635 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
636 } else {
637 // Push "macroname 1".
638 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
639 Buf.push_back(' ');
640 Buf.push_back('1');
641 }
642 Buf.push_back('\n');
643}
644
Chris Lattner64299f82008-01-10 01:53:41 +0000645/// AddImplicitInclude - Add an implicit #include of the specified file to the
646/// predefines buffer.
647static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){
648 const char *Inc = "#include \"";
649 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
650 Buf.insert(Buf.end(), File.begin(), File.end());
651 Buf.push_back('"');
652 Buf.push_back('\n');
653}
654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655
Chris Lattner53b0dab2007-10-09 22:10:18 +0000656/// InitializePreprocessor - Initialize the preprocessor getting it and the
Chris Lattner51574ea2008-04-19 23:25:44 +0000657/// environment ready to process a single file. This returns true on error.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000658///
Chris Lattner51574ea2008-04-19 23:25:44 +0000659static bool InitializePreprocessor(Preprocessor &PP,
660 bool InitializeSourceMgr,
661 const std::string &InFile) {
Chris Lattnerdee73592007-12-15 20:48:40 +0000662 FileManager &FileMgr = PP.getFileManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000663
Chris Lattner53b0dab2007-10-09 22:10:18 +0000664 // Figure out where to get and map in the main file.
Chris Lattnerdee73592007-12-15 20:48:40 +0000665 SourceManager &SourceMgr = PP.getSourceManager();
Ted Kremenek339b9c22008-04-17 22:31:54 +0000666
667 if (InitializeSourceMgr) {
668 if (InFile != "-") {
669 const FileEntry *File = FileMgr.getFile(InFile);
670 if (File) SourceMgr.createMainFileID(File, SourceLocation());
671 if (SourceMgr.getMainFileID() == 0) {
672 fprintf(stderr, "Error reading '%s'!\n",InFile.c_str());
Chris Lattner51574ea2008-04-19 23:25:44 +0000673 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000674 }
675 } else {
676 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
677 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
678 if (SourceMgr.getMainFileID() == 0) {
679 fprintf(stderr, "Error reading standard input! Empty?\n");
Chris Lattner51574ea2008-04-19 23:25:44 +0000680 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000681 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000682 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 }
Sam Bishop1102d6b2008-04-14 14:41:57 +0000684
Chris Lattneraa391972008-04-19 23:09:31 +0000685 std::vector<char> PredefineBuffer;
686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 // Add macros from the command line.
Sam Bishop1102d6b2008-04-14 14:41:57 +0000688 unsigned d = 0, D = D_macros.size();
689 unsigned u = 0, U = U_macros.size();
690 while (d < D || u < U) {
691 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
692 DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str());
693 else
694 DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef ");
695 }
696
Chris Lattner64299f82008-01-10 01:53:41 +0000697 // FIXME: Read any files specified by -imacros.
698
699 // Add implicit #includes from -include.
700 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
701 AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000702
Chris Lattneraa391972008-04-19 23:09:31 +0000703 // Null terminate PredefinedBuffer and add it.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000704 PredefineBuffer.push_back(0);
Chris Lattneraa391972008-04-19 23:09:31 +0000705 PP.setPredefines(&PredefineBuffer[0]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000706
707 // Once we've read this, we're done.
Chris Lattner51574ea2008-04-19 23:25:44 +0000708 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000709}
710
711//===----------------------------------------------------------------------===//
712// Preprocessor include path information.
713//===----------------------------------------------------------------------===//
714
715// This tool exports a large number of command line options to control how the
716// preprocessor searches for header files. At root, however, the Preprocessor
717// object takes a very simple interface: a list of directories to search for
718//
719// FIXME: -nostdinc,-nostdinc++
Chris Lattner0c946412007-08-26 17:47:35 +0000720// FIXME: -imultilib
Reid Spencer5f016e22007-07-11 17:01:13 +0000721//
Chris Lattner64299f82008-01-10 01:53:41 +0000722// FIXME: -imacros
Reid Spencer5f016e22007-07-11 17:01:13 +0000723
724static llvm::cl::opt<bool>
725nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
726
727// Various command line options. These four add directories to each chain.
728static llvm::cl::list<std::string>
729F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
730 llvm::cl::desc("Add directory to framework include search path"));
731static llvm::cl::list<std::string>
732I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
733 llvm::cl::desc("Add directory to include search path"));
734static llvm::cl::list<std::string>
735idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
736 llvm::cl::desc("Add directory to AFTER include search path"));
737static llvm::cl::list<std::string>
738iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
739 llvm::cl::desc("Add directory to QUOTE include search path"));
740static llvm::cl::list<std::string>
741isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
742 llvm::cl::desc("Add directory to SYSTEM include search path"));
743
744// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
745static llvm::cl::list<std::string>
746iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
747 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
748static llvm::cl::list<std::string>
749iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
750 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
751static llvm::cl::list<std::string>
752iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
753 llvm::cl::Prefix,
754 llvm::cl::desc("Set directory to include search path with prefix"));
755
Chris Lattner0c946412007-08-26 17:47:35 +0000756static llvm::cl::opt<std::string>
757isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
758 llvm::cl::desc("Set the system root directory (usually /)"));
759
Reid Spencer5f016e22007-07-11 17:01:13 +0000760// Finally, implement the code that groks the options above.
Chris Lattner5f9eae52008-03-01 08:07:28 +0000761
Reid Spencer5f016e22007-07-11 17:01:13 +0000762/// InitializeIncludePaths - Process the -I options and set them in the
763/// HeaderSearch object.
Nico Weber0fca0222008-08-22 09:25:22 +0000764void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers,
765 FileManager &FM, const LangOptions &Lang) {
766 InitHeaderSearch Init(Headers, Verbose, isysroot);
767
Ted Kremenekf3721112008-05-31 00:27:00 +0000768 // Handle -I... and -F... options, walking the lists in parallel.
769 unsigned Iidx = 0, Fidx = 0;
770 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
771 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000772 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +0000773 ++Iidx;
774 } else {
Nico Weber0fca0222008-08-22 09:25:22 +0000775 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Ted Kremenekf3721112008-05-31 00:27:00 +0000776 ++Fidx;
777 }
778 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000779
Ted Kremenekf3721112008-05-31 00:27:00 +0000780 // Consume what's left from whatever list was longer.
781 for (; Iidx != I_dirs.size(); ++Iidx)
Nico Weber0fca0222008-08-22 09:25:22 +0000782 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +0000783 for (; Fidx != F_dirs.size(); ++Fidx)
Nico Weber0fca0222008-08-22 09:25:22 +0000784 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000785
786 // Handle -idirafter... options.
787 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000788 Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After,
789 false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000790
791 // Handle -iquote... options.
792 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000793 Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000794
795 // Handle -isystem... options.
796 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000797 Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000798
799 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
800 // parallel, processing the values in order of occurance to get the right
801 // prefixes.
802 {
803 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
804 unsigned iprefix_idx = 0;
805 unsigned iwithprefix_idx = 0;
806 unsigned iwithprefixbefore_idx = 0;
807 bool iprefix_done = iprefix_vals.empty();
808 bool iwithprefix_done = iwithprefix_vals.empty();
809 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
810 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
811 if (!iprefix_done &&
812 (iwithprefix_done ||
813 iprefix_vals.getPosition(iprefix_idx) <
814 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
815 (iwithprefixbefore_done ||
816 iprefix_vals.getPosition(iprefix_idx) <
817 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
818 Prefix = iprefix_vals[iprefix_idx];
819 ++iprefix_idx;
820 iprefix_done = iprefix_idx == iprefix_vals.size();
821 } else if (!iwithprefix_done &&
822 (iwithprefixbefore_done ||
823 iwithprefix_vals.getPosition(iwithprefix_idx) <
824 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
Nico Weber0fca0222008-08-22 09:25:22 +0000825 Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
826 InitHeaderSearch::System, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 ++iwithprefix_idx;
828 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
829 } else {
Nico Weber0fca0222008-08-22 09:25:22 +0000830 Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
831 InitHeaderSearch::Angled, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 ++iwithprefixbefore_idx;
833 iwithprefixbefore_done =
834 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
835 }
836 }
837 }
Chris Lattner5f9eae52008-03-01 08:07:28 +0000838
Nico Weber0fca0222008-08-22 09:25:22 +0000839 Init.AddDefaultEnvVarPaths(Lang);
Chris Lattner5f9eae52008-03-01 08:07:28 +0000840
Chris Lattnerdcaa0962008-03-03 03:16:03 +0000841 // Add the clang headers, which are relative to the clang driver.
842 llvm::sys::Path MainExecutablePath =
Chris Lattner985e1822008-03-03 05:57:43 +0000843 llvm::sys::Path::GetMainExecutable(Argv0,
844 (void*)(intptr_t)InitializeIncludePaths);
Chris Lattnerdcaa0962008-03-03 03:16:03 +0000845 if (!MainExecutablePath.isEmpty()) {
846 MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang
847 MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin
848 MainExecutablePath.appendComponent("Headers"); // Get foo/Headers
Nico Weber0fca0222008-08-22 09:25:22 +0000849 Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System,
850 false, false, false);
Chris Lattnerdcaa0962008-03-03 03:16:03 +0000851 }
852
Nico Weber0fca0222008-08-22 09:25:22 +0000853 if (!nostdinc)
854 Init.AddDefaultSystemIncludePaths(Lang);
Reid Spencer5f016e22007-07-11 17:01:13 +0000855
856 // Now that we have collected all of the include paths, merge them all
857 // together and tell the preprocessor about them.
858
Nico Weber0fca0222008-08-22 09:25:22 +0000859 Init.Realize();
Reid Spencer5f016e22007-07-11 17:01:13 +0000860}
861
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000862//===----------------------------------------------------------------------===//
863// Driver PreprocessorFactory - For lazily generating preprocessors ...
864//===----------------------------------------------------------------------===//
865
866namespace {
867class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000868 const std::string &InFile;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000869 Diagnostic &Diags;
870 const LangOptions &LangInfo;
871 TargetInfo &Target;
872 SourceManager &SourceMgr;
873 HeaderSearch &HeaderInfo;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000874 bool InitializeSourceMgr;
875
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000876public:
Ted Kremenek339b9c22008-04-17 22:31:54 +0000877 DriverPreprocessorFactory(const std::string &infile,
878 Diagnostic &diags, const LangOptions &opts,
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000879 TargetInfo &target, SourceManager &SM,
880 HeaderSearch &Headers)
Ted Kremenek339b9c22008-04-17 22:31:54 +0000881 : InFile(infile), Diags(diags), LangInfo(opts), Target(target),
882 SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {}
883
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000884
885 virtual ~DriverPreprocessorFactory() {}
886
887 virtual Preprocessor* CreatePreprocessor() {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000888 Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target,
889 SourceMgr, HeaderInfo);
890
Chris Lattner51574ea2008-04-19 23:25:44 +0000891 if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000892 delete PP;
893 return NULL;
894 }
895
896 InitializeSourceMgr = false;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000897 return PP;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000898 }
899};
900}
Reid Spencer5f016e22007-07-11 17:01:13 +0000901
Reid Spencer5f016e22007-07-11 17:01:13 +0000902//===----------------------------------------------------------------------===//
903// Basic Parser driver
904//===----------------------------------------------------------------------===//
905
Chris Lattner51574ea2008-04-19 23:25:44 +0000906static void ParseFile(Preprocessor &PP, MinimalAction *PA) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 Parser P(PP, *PA);
Ted Kremenek95041a22007-12-19 22:51:13 +0000908 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +0000909
910 // Parsing the specified input file.
911 P.ParseTranslationUnit();
912 delete PA;
Argyrios Kyrtzidis14d41402008-06-13 12:15:34 +0000913
914 if (VerifyDiagnostics)
915 exit(CheckDiagnostics(PP));
Reid Spencer5f016e22007-07-11 17:01:13 +0000916}
917
918//===----------------------------------------------------------------------===//
919// Main driver
920//===----------------------------------------------------------------------===//
921
Ted Kremenekdb094a22007-12-05 18:27:04 +0000922/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
923/// action. These consumers can operate on both ASTs that are freshly
924/// parsed from source files as well as those deserialized from Bitcode.
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000925static ASTConsumer* CreateASTConsumer(const std::string& InFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000926 Diagnostic& Diag, FileManager& FileMgr,
Chris Lattnere66b65c2008-02-06 01:42:25 +0000927 const LangOptions& LangOpts,
Chris Lattner3245a0a2008-04-16 06:11:58 +0000928 Preprocessor *PP,
Ted Kremenek815c78f2008-08-05 18:50:11 +0000929 PreprocessorFactory *PPF) {
Ted Kremenekdb094a22007-12-05 18:27:04 +0000930 switch (ProgAction) {
931 default:
932 return NULL;
933
934 case ASTPrint:
935 return CreateASTPrinter();
936
937 case ASTDump:
938 return CreateASTDumper();
939
940 case ASTView:
Ted Kremenek6a340832008-03-18 21:19:49 +0000941 return CreateASTViewer();
942
943 case EmitHTML:
Ted Kremenek339b9c22008-04-17 22:31:54 +0000944 return CreateHTMLPrinter(OutputFile, Diag, PP, PPF);
Ted Kremenek902141f2008-07-02 18:23:21 +0000945
Ted Kremenekdb094a22007-12-05 18:27:04 +0000946 case TestSerialization:
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000947 return CreateSerializationTest(Diag, FileMgr);
Ted Kremenekdb094a22007-12-05 18:27:04 +0000948
949 case EmitLLVM:
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000950 case EmitBC:
Ted Kremenek815c78f2008-08-05 18:50:11 +0000951 return CreateLLVMCodeGenWriter(ProgAction == EmitBC, Diag, LangOpts,
952 InFile, OutputFile, GenerateDebugInfo);
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000953
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000954 case SerializeAST:
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000955 // FIXME: Allow user to tailor where the file is written.
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000956 return CreateASTSerializer(InFile, OutputFile, Diag);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000957
Steve Naroffb29b4272008-04-14 22:03:09 +0000958 case RewriteObjC:
Chris Lattnerc68ab772008-03-22 00:08:40 +0000959 return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000960
961 case RunAnalysis:
962 assert (!AnalysisList.empty());
963 return CreateAnalysisConsumer(&AnalysisList[0],
964 &AnalysisList[0]+AnalysisList.size(),
965 Diag, PP, PPF, LangOpts,
966 AnalyzeSpecificFunction,
967 OutputFile, VisualizeEG, TrimGraph,
968 AnalyzeAll);
Ted Kremenekdb094a22007-12-05 18:27:04 +0000969 }
970}
971
Reid Spencer5f016e22007-07-11 17:01:13 +0000972/// ProcessInputFile - Process a single input file with the specified state.
973///
Ted Kremenek339b9c22008-04-17 22:31:54 +0000974static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
975 const std::string &InFile) {
Ted Kremenekd39bcd82007-09-26 18:39:29 +0000976
Ted Kremenek7e7e6252008-08-08 02:46:37 +0000977 llvm::OwningPtr<ASTConsumer> Consumer;
Chris Lattnerbd247762007-07-22 06:05:44 +0000978 bool ClearSourceMgr = false;
Ted Kremenekd39bcd82007-09-26 18:39:29 +0000979
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 switch (ProgAction) {
981 default:
Ted Kremenek7e7e6252008-08-08 02:46:37 +0000982 Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(),
983 PP.getFileManager(), PP.getLangOptions(),
984 &PP, &PPF));
Ted Kremenekdb094a22007-12-05 18:27:04 +0000985
986 if (!Consumer) {
987 fprintf(stderr, "Unexpected program action!\n");
988 return;
989 }
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000990
Ted Kremenekdb094a22007-12-05 18:27:04 +0000991 break;
992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 case DumpTokens: { // Token dump mode.
Chris Lattnerd2177732007-07-20 16:59:19 +0000994 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +0000996 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 do {
998 PP.Lex(Tok);
999 PP.DumpToken(Tok, true);
1000 fprintf(stderr, "\n");
Chris Lattner057aaf62007-10-09 18:03:42 +00001001 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001002 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 break;
1004 }
1005 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
Chris Lattnerd2177732007-07-20 16:59:19 +00001006 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001008 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 do {
1010 PP.Lex(Tok);
Chris Lattner057aaf62007-10-09 18:03:42 +00001011 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001012 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 break;
1014 }
1015
1016 case PrintPreprocessedInput: // -E mode.
Chris Lattnere988bc22008-01-27 23:55:11 +00001017 DoPrintPreprocessedInput(PP, OutputFile);
Chris Lattnerbd247762007-07-22 06:05:44 +00001018 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001020
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 case ParseNoop: // -parse-noop
Ted Kremenek95041a22007-12-19 22:51:13 +00001022 ParseFile(PP, new MinimalAction(PP.getIdentifierTable()));
Chris Lattnerbd247762007-07-22 06:05:44 +00001023 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 break;
1025
1026 case ParsePrintCallbacks:
Ted Kremenek95041a22007-12-19 22:51:13 +00001027 ParseFile(PP, CreatePrintParserActionsAction(PP.getIdentifierTable()));
Chris Lattnerbd247762007-07-22 06:05:44 +00001028 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 break;
Ted Kremenek44579782007-09-25 18:37:20 +00001030
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001031 case ParseSyntaxOnly: // -fsyntax-only
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001032 Consumer.reset(new ASTConsumer());
Ted Kremenek2bf55142007-09-17 20:49:30 +00001033 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001034
1035 case RewriteMacros:
Chris Lattner09510522008-05-09 22:43:24 +00001036 RewriteMacrosInInput(PP, InFile, OutputFile);
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001037 ClearSourceMgr = true;
1038 break;
Chris Lattner580980b2007-09-16 19:46:59 +00001039 }
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001040
1041 if (Consumer) {
Ted Kremenek9f3d9422007-09-26 20:14:22 +00001042 if (VerifyDiagnostics)
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001043 exit(CheckASTConsumer(PP, Consumer.get()));
Chris Lattner31e6c7d2007-11-03 06:24:16 +00001044
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001045 ParseAST(PP, Consumer.get(), Stats);
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 }
Chris Lattnere66b65c2008-02-06 01:42:25 +00001047
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 if (Stats) {
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001049 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 PP.PrintStats();
1051 PP.getIdentifierTable().PrintStats();
Chris Lattnerdee73592007-12-15 20:48:40 +00001052 PP.getHeaderSearchInfo().PrintStats();
Chris Lattnerbd247762007-07-22 06:05:44 +00001053 if (ClearSourceMgr)
Chris Lattnerdee73592007-12-15 20:48:40 +00001054 PP.getSourceManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 fprintf(stderr, "\n");
1056 }
Chris Lattnerbd247762007-07-22 06:05:44 +00001057
1058 // For a multi-file compilation, some things are ok with nuking the source
1059 // manager tables, other require stable fileid/macroid's across multiple
1060 // files.
Chris Lattnerdee73592007-12-15 20:48:40 +00001061 if (ClearSourceMgr)
1062 PP.getSourceManager().clearIDTables();
Reid Spencer5f016e22007-07-11 17:01:13 +00001063}
1064
Ted Kremenek20e97482007-12-12 23:41:08 +00001065static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag,
1066 FileManager& FileMgr) {
1067
1068 if (VerifyDiagnostics) {
1069 fprintf(stderr, "-verify does not yet work with serialized ASTs.\n");
1070 exit (1);
1071 }
1072
1073 llvm::sys::Path Filename(InFile);
1074
1075 if (!Filename.isValid()) {
1076 fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str());
1077 exit (1);
1078 }
1079
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001080 llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr));
Ted Kremenekfe4e0152007-12-13 18:11:11 +00001081
1082 if (!TU) {
1083 fprintf(stderr, "error: file '%s' could not be deserialized\n",
1084 InFile.c_str());
1085 exit (1);
1086 }
1087
Ted Kremenek63ea8632007-12-19 19:27:38 +00001088 // Observe that we use the source file name stored in the deserialized
1089 // translation unit, rather than InFile.
Ted Kremenekee533642007-12-20 19:47:16 +00001090 llvm::OwningPtr<ASTConsumer>
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001091 Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(),
Ted Kremenek815c78f2008-08-05 18:50:11 +00001092 0, 0));
Nico Weber7bfaaae2008-08-10 19:59:06 +00001093
Ted Kremenek20e97482007-12-12 23:41:08 +00001094 if (!Consumer) {
1095 fprintf(stderr, "Unsupported program action with serialized ASTs!\n");
1096 exit (1);
1097 }
Nico Weber7bfaaae2008-08-10 19:59:06 +00001098
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001099 Consumer->Initialize(TU->getContext());
Nico Weber7bfaaae2008-08-10 19:59:06 +00001100
Chris Lattnere66b65c2008-02-06 01:42:25 +00001101 // FIXME: We need to inform Consumer about completed TagDecls as well.
Ted Kremenek20e97482007-12-12 23:41:08 +00001102 for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
1103 Consumer->HandleTopLevelDecl(*I);
Ted Kremenek20e97482007-12-12 23:41:08 +00001104}
1105
1106
Reid Spencer5f016e22007-07-11 17:01:13 +00001107static llvm::cl::list<std::string>
1108InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1109
Ted Kremenek20e97482007-12-12 23:41:08 +00001110static bool isSerializedFile(const std::string& InFile) {
1111 if (InFile.size() < 4)
1112 return false;
1113
1114 const char* s = InFile.c_str()+InFile.size()-4;
1115
1116 return s[0] == '.' &&
1117 s[1] == 'a' &&
1118 s[2] == 's' &&
1119 s[3] == 't';
1120}
1121
Reid Spencer5f016e22007-07-11 17:01:13 +00001122
1123int main(int argc, char **argv) {
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001124 llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n");
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 llvm::sys::PrintStackTraceOnErrorSignal();
1126
1127 // If no input was specified, read from stdin.
1128 if (InputFilenames.empty())
1129 InputFilenames.push_back("-");
Ted Kremenek31e703b2007-12-11 23:28:38 +00001130
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 // Create a file manager object to provide access to and cache the filesystem.
1132 FileManager FileMgr;
1133
Ted Kremenek31e703b2007-12-11 23:28:38 +00001134 // Create the diagnostic client for reporting errors or for
1135 // implementing -verify.
Nico Weber7bfaaae2008-08-10 19:59:06 +00001136 DiagnosticClient* TextDiagClient = 0;
Ted Kremenek88f5cde2008-03-27 06:17:42 +00001137
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001138 if (!VerifyDiagnostics) {
1139 // Print diagnostics to stderr by default.
1140 TextDiagClient = new TextDiagnosticPrinter(!NoShowColumn,
1141 !NoCaretDiagnostics);
1142 } else {
1143 // When checking diagnostics, just buffer them up.
1144 TextDiagClient = new TextDiagnosticBuffer();
1145
1146 if (InputFilenames.size() != 1) {
1147 fprintf(stderr,
1148 "-verify only works on single input files for now.\n");
1149 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 }
1151 }
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001152
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 // Configure our handling of diagnostics.
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001154 llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient);
1155 Diagnostic Diags(DiagClient.get());
Ted Kremenek31e703b2007-12-11 23:28:38 +00001156 InitializeDiagnostics(Diags);
1157
Chris Lattner4f037832007-12-05 23:24:17 +00001158 // -I- is a deprecated GCC feature, scan for it and reject it.
1159 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
1160 if (I_dirs[i] == "-") {
Ted Kremenek2eefd862007-12-11 22:57:35 +00001161 Diags.Report(diag::err_pp_I_dash_not_supported);
Chris Lattner4f037832007-12-05 23:24:17 +00001162 I_dirs.erase(I_dirs.begin()+i);
1163 --i;
1164 }
1165 }
Chris Lattner11215192008-03-14 06:12:05 +00001166
1167 // Get information about the target being compiled for.
1168 std::string Triple = CreateTargetTriple();
Ted Kremenek7a08e282008-08-07 18:13:12 +00001169 llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple));
1170
Chris Lattner11215192008-03-14 06:12:05 +00001171 if (Target == 0) {
1172 fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
1173 Triple.c_str());
1174 fprintf(stderr, "Please use -triple or -arch.\n");
1175 exit(1);
1176 }
Chris Lattner4f037832007-12-05 23:24:17 +00001177
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001178 // Are we invoking one or more source analyses?
1179 if (!AnalysisList.empty() && ProgAction == ParseSyntaxOnly)
1180 ProgAction = RunAnalysis;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001181
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001182 llvm::OwningPtr<SourceManager> SourceMgr;
1183
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenek31e703b2007-12-11 23:28:38 +00001185 const std::string &InFile = InputFilenames[i];
Ted Kremenek31e703b2007-12-11 23:28:38 +00001186
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001187 if (isSerializedFile(InFile)) {
1188 Diags.setClient(TextDiagClient);
Ted Kremenek20e97482007-12-12 23:41:08 +00001189 ProcessSerializedFile(InFile,Diags,FileMgr);
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001190 }
Ted Kremenek20e97482007-12-12 23:41:08 +00001191 else {
1192 /// Create a SourceManager object. This tracks and owns all the file
1193 /// buffers allocated to a translation unit.
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001194 if (!SourceMgr)
1195 SourceMgr.reset(new SourceManager());
1196 else
1197 SourceMgr->clearIDTables();
Ted Kremenek31e703b2007-12-11 23:28:38 +00001198
Ted Kremenek20e97482007-12-12 23:41:08 +00001199 // Initialize language options, inferring file types from input filenames.
1200 LangOptions LangInfo;
1201 InitializeBaseLanguage();
1202 LangKind LK = GetLanguage(InFile);
1203 InitializeLangOptions(LangInfo, LK);
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +00001204 InitializeLanguageStandard(LangInfo, LK, Target.get());
Ted Kremenek01d9dbf2008-04-29 04:37:03 +00001205 InitializeGCMode(LangInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001206
1207 // Process the -I options and set them in the HeaderInfo.
1208 HeaderSearch HeaderInfo(FileMgr);
Ted Kremenekc68ecb52008-06-06 01:47:30 +00001209
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001210 InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001211
Ted Kremenek20e97482007-12-12 23:41:08 +00001212 // Set up the preprocessor with these options.
Ted Kremenek339b9c22008-04-17 22:31:54 +00001213 DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target,
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001214 *SourceMgr.get(), HeaderInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001215
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001216 llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor());
1217
Ted Kremenek339b9c22008-04-17 22:31:54 +00001218 if (!PP)
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001219 continue;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001220
1221 // Create the HTMLDiagnosticsClient if we are using one. Otherwise,
1222 // always reset to using TextDiagClient.
1223 llvm::OwningPtr<DiagnosticClient> TmpClient;
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001224
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001225 if (!HTMLDiag.empty()) {
1226 TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(),
1227 &PPFactory));
1228 Diags.setClient(TmpClient.get());
1229 }
1230 else
1231 Diags.setClient(TextDiagClient);
1232
1233 // Process the source file.
Ted Kremenek339b9c22008-04-17 22:31:54 +00001234 ProcessInputFile(*PP, PPFactory, InFile);
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001235 HeaderInfo.ClearFileInfo();
Ted Kremenek20e97482007-12-12 23:41:08 +00001236
1237 if (Stats)
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001238 SourceMgr->PrintStats();
Ted Kremenek20e97482007-12-12 23:41:08 +00001239 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 }
Chris Lattner11215192008-03-14 06:12:05 +00001241
Ted Kremenek7a08e282008-08-07 18:13:12 +00001242 if (unsigned NumDiagnostics = Diags.getNumDiagnostics())
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1244 (NumDiagnostics == 1 ? "" : "s"));
1245
1246 if (Stats) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 FileMgr.PrintStats();
1248 fprintf(stderr, "\n");
1249 }
1250
Chris Lattner96f1a642007-07-21 05:40:53 +00001251 return Diags.getNumErrors() != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001252}