blob: 0efe56a35073c58855819c5aed73f446d265b236 [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"
Zhongxing Xu81488392008-08-24 02:33:36 +000027#include "clang/Driver/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>
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000162VisualizeEGDot("analyzer-viz-egraph-graphviz",
163 llvm::cl::desc("Display exploded graph using GraphViz"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000164
165static llvm::cl::opt<bool>
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000166VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
167 llvm::cl::desc("Display exploded graph using Ubigraph"));
168
169static llvm::cl::opt<bool>
170AnalyzeAll("analyzer-opt-analyze-headers",
Ted Kremenek55af98c2008-04-14 18:40:58 +0000171 llvm::cl::desc("Force the static analyzer to analyze "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000172 "functions defined in header files"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000173
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000174static llvm::cl::list<Analyses>
175AnalysisList(llvm::cl::desc("Available Source Code Analyses:"),
176llvm::cl::values(
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000177#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000178clEnumValN(NAME, CMDFLAG, DESC),
179#include "Analyses.def"
180clEnumValEnd));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000181
Ted Kremenek55af98c2008-04-14 18:40:58 +0000182//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000183// Language Options
184//===----------------------------------------------------------------------===//
185
186enum LangKind {
187 langkind_unspecified,
188 langkind_c,
189 langkind_c_cpp,
190 langkind_cxx,
191 langkind_cxx_cpp,
192 langkind_objc,
193 langkind_objc_cpp,
194 langkind_objcxx,
195 langkind_objcxx_cpp
196};
197
198/* TODO: GCC also accepts:
199 c-header c++-header objective-c-header objective-c++-header
200 assembler assembler-with-cpp
201 ada, f77*, ratfor (!), f95, java, treelang
202 */
203static llvm::cl::opt<LangKind>
204BaseLang("x", llvm::cl::desc("Base language to compile"),
205 llvm::cl::init(langkind_unspecified),
206 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
207 clEnumValN(langkind_cxx, "c++", "C++"),
208 clEnumValN(langkind_objc, "objective-c", "Objective C"),
209 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
210 clEnumValN(langkind_c_cpp, "c-cpp-output",
211 "Preprocessed C"),
212 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
213 "Preprocessed C++"),
214 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
215 "Preprocessed Objective C"),
216 clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output",
217 "Preprocessed Objective C++"),
218 clEnumValEnd));
219
220static llvm::cl::opt<bool>
221LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
222 llvm::cl::Hidden);
223static llvm::cl::opt<bool>
224LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
225 llvm::cl::Hidden);
226
Ted Kremenek8904f152007-12-05 23:49:08 +0000227/// InitializeBaseLanguage - Handle the -x foo options.
228static void InitializeBaseLanguage() {
229 if (LangObjC)
230 BaseLang = langkind_objc;
231 else if (LangObjCXX)
232 BaseLang = langkind_objcxx;
233}
234
235static LangKind GetLanguage(const std::string &Filename) {
236 if (BaseLang != langkind_unspecified)
237 return BaseLang;
238
239 std::string::size_type DotPos = Filename.rfind('.');
240
241 if (DotPos == std::string::npos) {
242 BaseLang = langkind_c; // Default to C if no extension.
Chris Lattner9b2f6c42008-01-04 19:12:28 +0000243 return langkind_c;
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 }
245
Ted Kremenek8904f152007-12-05 23:49:08 +0000246 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
247 // C header: .h
248 // C++ header: .hh or .H;
249 // assembler no preprocessing: .s
250 // assembler: .S
251 if (Ext == "c")
252 return langkind_c;
253 else if (Ext == "i")
254 return langkind_c_cpp;
255 else if (Ext == "ii")
256 return langkind_cxx_cpp;
257 else if (Ext == "m")
258 return langkind_objc;
259 else if (Ext == "mi")
260 return langkind_objc_cpp;
261 else if (Ext == "mm" || Ext == "M")
262 return langkind_objcxx;
263 else if (Ext == "mii")
264 return langkind_objcxx_cpp;
265 else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
266 Ext == "c++" || Ext == "cp" || Ext == "cxx")
267 return langkind_cxx;
268 else
269 return langkind_c;
270}
271
272
273static void InitializeLangOptions(LangOptions &Options, LangKind LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 // FIXME: implement -fpreprocessed mode.
275 bool NoPreprocess = false;
276
Ted Kremenek8904f152007-12-05 23:49:08 +0000277 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 default: assert(0 && "Unknown language kind!");
279 case langkind_c_cpp:
280 NoPreprocess = true;
281 // FALLTHROUGH
282 case langkind_c:
283 break;
284 case langkind_cxx_cpp:
285 NoPreprocess = true;
286 // FALLTHROUGH
287 case langkind_cxx:
288 Options.CPlusPlus = 1;
289 break;
290 case langkind_objc_cpp:
291 NoPreprocess = true;
292 // FALLTHROUGH
293 case langkind_objc:
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000294 Options.ObjC1 = Options.ObjC2 = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 break;
296 case langkind_objcxx_cpp:
297 NoPreprocess = true;
298 // FALLTHROUGH
299 case langkind_objcxx:
300 Options.ObjC1 = Options.ObjC2 = 1;
301 Options.CPlusPlus = 1;
302 break;
303 }
304}
305
306/// LangStds - Language standards we support.
307enum LangStds {
308 lang_unspecified,
309 lang_c89, lang_c94, lang_c99,
Ted Kremenekea644d82008-09-03 21:22:16 +0000310 lang_gnu_START,
311 lang_gnu89 = lang_gnu_START, lang_gnu99,
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000312 lang_cxx98, lang_gnucxx98,
313 lang_cxx0x, lang_gnucxx0x
Reid Spencer5f016e22007-07-11 17:01:13 +0000314};
315
316static llvm::cl::opt<LangStds>
317LangStd("std", llvm::cl::desc("Language standard to compile for"),
318 llvm::cl::init(lang_unspecified),
319 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
320 clEnumValN(lang_c89, "c90", "ISO C 1990"),
321 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
322 clEnumValN(lang_c94, "iso9899:199409",
323 "ISO C 1990 with amendment 1"),
324 clEnumValN(lang_c99, "c99", "ISO C 1999"),
325// clEnumValN(lang_c99, "c9x", "ISO C 1999"),
326 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
327// clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
328 clEnumValN(lang_gnu89, "gnu89",
329 "ISO C 1990 with GNU extensions (default for C)"),
330 clEnumValN(lang_gnu99, "gnu99",
331 "ISO C 1999 with GNU extensions"),
332 clEnumValN(lang_gnu99, "gnu9x",
333 "ISO C 1999 with GNU extensions"),
334 clEnumValN(lang_cxx98, "c++98",
335 "ISO C++ 1998 with amendments"),
336 clEnumValN(lang_gnucxx98, "gnu++98",
337 "ISO C++ 1998 with amendments and GNU "
338 "extensions (default for C++)"),
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000339 clEnumValN(lang_cxx0x, "c++0x",
340 "Upcoming ISO C++ 200x with amendments"),
341 clEnumValN(lang_gnucxx0x, "gnu++0x",
342 "Upcoming ISO C++ 200x with amendments and GNU "
343 "extensions (default for C++)"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 clEnumValEnd));
345
346static llvm::cl::opt<bool>
347NoOperatorNames("fno-operator-names",
348 llvm::cl::desc("Do not treat C++ operator name keywords as "
349 "synonyms for operators"));
350
Anders Carlssonee98ac52007-10-15 02:50:23 +0000351static llvm::cl::opt<bool>
352PascalStrings("fpascal-strings",
353 llvm::cl::desc("Recognize and construct Pascal-style "
354 "string literals"));
Steve Naroffd62701b2008-02-07 03:50:06 +0000355
356static llvm::cl::opt<bool>
357MSExtensions("fms-extensions",
358 llvm::cl::desc("Accept some non-standard constructs used in "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000359 "Microsoft header files "));
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000360
361static llvm::cl::opt<bool>
362WritableStrings("fwritable-strings",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000363 llvm::cl::desc("Store string literals as writable data"));
Anders Carlsson695dbb62007-11-30 04:21:22 +0000364
365static llvm::cl::opt<bool>
366LaxVectorConversions("flax-vector-conversions",
367 llvm::cl::desc("Allow implicit conversions between vectors"
368 " with a different number of elements or "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000369 "different element types"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000370
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000371// FIXME: This (and all GCC -f options) really come in -f... and
372// -fno-... forms, and additionally support automagic behavior when
373// they are not defined. For example, -fexceptions defaults to on or
374// off depending on the language. We should support this behavior in
375// some form (perhaps just add a facility for distinguishing when an
376// has its default value from when it has been set to its default
377// value).
378static llvm::cl::opt<bool>
379Exceptions("fexceptions",
380 llvm::cl::desc("Enable support for exception handling."));
381
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000382static llvm::cl::opt<bool>
383GNURuntime("fgnu-runtime",
384 llvm::cl::desc("Generate output compatible with the standard GNU Objective-C runtime."));
385
386static llvm::cl::opt<bool>
387NeXTRuntime("fnext-runtime",
388 llvm::cl::desc("Generate output compatible with the NeXT runtime."));
389
Ted Kremenekea644d82008-09-03 21:22:16 +0000390
391
392static llvm::cl::opt<bool>
393Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences."));
394
395static llvm::cl::opt<bool>
396Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89."));
397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398// FIXME: add:
Reid Spencer5f016e22007-07-11 17:01:13 +0000399// -fdollars-in-identifiers
Anders Carlssonee98ac52007-10-15 02:50:23 +0000400// -fpascal-strings
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +0000401static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
402 TargetInfo *Target) {
Ted Kremenekea644d82008-09-03 21:22:16 +0000403
404 if (Ansi) // "The -ansi option is equivalent to -std=c89."
405 LangStd = lang_c89;
406
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 if (LangStd == lang_unspecified) {
408 // Based on the base language, pick one.
Ted Kremenek8904f152007-12-05 23:49:08 +0000409 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 default: assert(0 && "Unknown base language");
411 case langkind_c:
412 case langkind_c_cpp:
413 case langkind_objc:
414 case langkind_objc_cpp:
415 LangStd = lang_gnu99;
416 break;
417 case langkind_cxx:
418 case langkind_cxx_cpp:
419 case langkind_objcxx:
420 case langkind_objcxx_cpp:
421 LangStd = lang_gnucxx98;
422 break;
423 }
424 }
425
426 switch (LangStd) {
427 default: assert(0 && "Unknown language standard!");
428
429 // Fall through from newer standards to older ones. This isn't really right.
430 // FIXME: Enable specifically the right features based on the language stds.
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000431 case lang_gnucxx0x:
432 case lang_cxx0x:
433 Options.CPlusPlus0x = 1;
434 // FALL THROUGH
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 case lang_gnucxx98:
436 case lang_cxx98:
437 Options.CPlusPlus = 1;
438 Options.CXXOperatorNames = !NoOperatorNames;
Nate Begeman8aebcb72007-11-15 07:30:50 +0000439 Options.Boolean = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000440 // FALL THROUGH.
441 case lang_gnu99:
442 case lang_c99:
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 Options.C99 = 1;
444 Options.HexFloats = 1;
445 // FALL THROUGH.
446 case lang_gnu89:
447 Options.BCPLComment = 1; // Only for C99/C++.
448 // FALL THROUGH.
449 case lang_c94:
Chris Lattner3426b9b2008-02-25 04:01:39 +0000450 Options.Digraphs = 1; // C94, C99, C++.
451 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 case lang_c89:
453 break;
454 }
Argyrios Kyrtzidisd1465522008-09-11 04:21:06 +0000455
456 if (Options.CPlusPlus) {
457 Options.C99 = 0;
458 Options.HexFloats = (LangStd == lang_gnucxx98 || LangStd==lang_gnucxx0x);
459 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000460
Chris Lattnerd658b562008-04-05 06:32:51 +0000461 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
462 Options.ImplicitInt = 1;
463 else
464 Options.ImplicitInt = 0;
Ted Kremenekea644d82008-09-03 21:22:16 +0000465
466 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi
467 // is specified, or -std is set to a conforming mode.
468 Options.Trigraphs = LangStd < lang_gnu_START || Trigraphs ? 1 : 0;
469
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 Options.DollarIdents = 1; // FIXME: Really a target property.
Anders Carlssonee98ac52007-10-15 02:50:23 +0000471 Options.PascalStrings = PascalStrings;
Steve Naroffd62701b2008-02-07 03:50:06 +0000472 Options.Microsoft = MSExtensions;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000473 Options.WritableStrings = WritableStrings;
Anders Carlsson695dbb62007-11-30 04:21:22 +0000474 Options.LaxVectorConversions = LaxVectorConversions;
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000475 Options.Exceptions = Exceptions;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000476
477 if (NeXTRuntime) {
478 Options.NeXTRuntime = 1;
479 } else if (GNURuntime) {
480 Options.NeXTRuntime = 0;
481 } else {
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +0000482 Options.NeXTRuntime = Target->useNeXTRuntimeAsDefault();
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000483 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000484}
485
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000486static llvm::cl::opt<bool>
487ObjCExclusiveGC("fobjc-gc-only",
488 llvm::cl::desc("Use GC exclusively for Objective-C related "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000489 "memory management"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000490
491static llvm::cl::opt<bool>
492ObjCEnableGC("fobjc-gc",
Nico Weberfd54ebc2008-08-05 23:33:20 +0000493 llvm::cl::desc("Enable Objective-C garbage collection"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000494
495void InitializeGCMode(LangOptions &Options) {
496 if (ObjCExclusiveGC)
497 Options.setGCMode(LangOptions::GCOnly);
498 else if (ObjCEnableGC)
499 Options.setGCMode(LangOptions::HybridGC);
500}
501
502
Reid Spencer5f016e22007-07-11 17:01:13 +0000503//===----------------------------------------------------------------------===//
504// Our DiagnosticClient implementation
505//===----------------------------------------------------------------------===//
506
507// FIXME: Werror should take a list of things, -Werror=foo,bar
508static llvm::cl::opt<bool>
509WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
510
511static llvm::cl::opt<bool>
Chris Lattner5b4681c2008-05-29 15:36:45 +0000512SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings"));
513
514static llvm::cl::opt<bool>
Reid Spencer5f016e22007-07-11 17:01:13 +0000515WarnOnExtensions("pedantic", llvm::cl::init(false),
516 llvm::cl::desc("Issue a warning on uses of GCC extensions"));
517
518static llvm::cl::opt<bool>
519ErrorOnExtensions("pedantic-errors",
520 llvm::cl::desc("Issue an error on uses of GCC extensions"));
521
522static llvm::cl::opt<bool>
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000523SuppressSystemWarnings("suppress-system-warnings",
524 llvm::cl::desc("Issue an error on uses of GCC extensions"),
525 llvm::cl::init(true));
526
527static llvm::cl::opt<bool>
Reid Spencer5f016e22007-07-11 17:01:13 +0000528WarnUnusedMacros("Wunused_macros",
529 llvm::cl::desc("Warn for unused macros in the main translation unit"));
530
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000531static llvm::cl::opt<bool>
532WarnFloatEqual("Wfloat-equal",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000533 llvm::cl::desc("Warn about equality comparisons of floating point values"));
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000534
Ted Kremenek73da5902007-12-17 17:50:07 +0000535static llvm::cl::opt<bool>
536WarnNoFormatNonLiteral("Wno-format-nonliteral",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000537 llvm::cl::desc("Do not warn about non-literal format strings"));
Ted Kremenek73da5902007-12-17 17:50:07 +0000538
Chris Lattner116a4b12008-01-23 17:19:46 +0000539static llvm::cl::opt<bool>
540WarnUndefMacros("Wundef",
541 llvm::cl::desc("Warn on use of undefined macros in #if's"));
542
Chris Lattner37d10842008-05-05 21:18:06 +0000543static llvm::cl::opt<bool>
Ted Kremenek358256d2008-05-30 16:42:02 +0000544WarnImplicitFunctionDeclaration("Wimplicit-function-declaration",
545 llvm::cl::desc("Warn about uses of implicitly defined functions"));
Chris Lattner116a4b12008-01-23 17:19:46 +0000546
Reid Spencer5f016e22007-07-11 17:01:13 +0000547/// InitializeDiagnostics - Initialize the diagnostic object, based on the
548/// current command line option settings.
549static void InitializeDiagnostics(Diagnostic &Diags) {
Chris Lattner5b4681c2008-05-29 15:36:45 +0000550 Diags.setIgnoreAllWarnings(SilenceWarnings);
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 Diags.setWarningsAsErrors(WarningsAsErrors);
552 Diags.setWarnOnExtensions(WarnOnExtensions);
553 Diags.setErrorOnExtensions(ErrorOnExtensions);
554
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000555 // Suppress warnings in system headers unless requested not to.
556 Diags.setSuppressSystemWarnings(SuppressSystemWarnings);
557
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 // Silence the "macro is not used" warning unless requested.
559 if (!WarnUnusedMacros)
560 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000561
562 // Silence "floating point comparison" warnings unless requested.
563 if (!WarnFloatEqual)
564 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
Ted Kremenek73da5902007-12-17 17:50:07 +0000565
566 // Silence "format string is not a string literal" warnings if requested
567 if (WarnNoFormatNonLiteral)
Ted Kremenek7c1d3df2007-12-17 17:50:39 +0000568 Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant,
569 diag::MAP_IGNORE);
Chris Lattner116a4b12008-01-23 17:19:46 +0000570 if (!WarnUndefMacros)
571 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);
Steve Naroffe7a37302008-02-11 22:40:08 +0000572
Chris Lattner37d10842008-05-05 21:18:06 +0000573 if (!WarnImplicitFunctionDeclaration)
574 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
575 diag::MAP_IGNORE);
576
Steve Naroffe7a37302008-02-11 22:40:08 +0000577 if (MSExtensions) // MS allows unnamed struct/union fields.
578 Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE);
Chris Lattner8aedf192008-05-04 23:52:02 +0000579
580 // If -pedantic-errors is set, turn extensions that warn by default into
581 // errors.
582 if (ErrorOnExtensions) {
583 Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large,
584 diag::MAP_ERROR);
585 Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large,
586 diag::MAP_ERROR);
587 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000588}
589
590//===----------------------------------------------------------------------===//
Ted Kremenekcb330932008-02-18 21:21:23 +0000591// Analysis-specific options.
592//===----------------------------------------------------------------------===//
593
594static llvm::cl::opt<std::string>
595AnalyzeSpecificFunction("analyze-function",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000596 llvm::cl::desc("Run analysis on specific function"));
Ted Kremenekcb330932008-02-18 21:21:23 +0000597
Ted Kremenekffe0f432008-03-07 22:58:01 +0000598static llvm::cl::opt<bool>
Ted Kremenekd71ed262008-04-10 22:16:52 +0000599TrimGraph("trim-egraph",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000600 llvm::cl::desc("Only show error-related paths in the analysis graph"));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000601
Ted Kremenekcb330932008-02-18 21:21:23 +0000602//===----------------------------------------------------------------------===//
Ted Kremenekae360762007-12-03 22:06:55 +0000603// Target Triple Processing.
604//===----------------------------------------------------------------------===//
605
606static llvm::cl::opt<std::string>
607TargetTriple("triple",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000608 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000609
Chris Lattner42e67372008-03-05 01:18:20 +0000610static llvm::cl::opt<std::string>
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000611Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000612
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000613static std::string CreateTargetTriple() {
Ted Kremenekae360762007-12-03 22:06:55 +0000614 // Initialize base triple. If a -triple option has been specified, use
615 // that triple. Otherwise, default to the host triple.
Chris Lattner6590d212007-12-12 05:01:48 +0000616 std::string Triple = TargetTriple;
617 if (Triple.empty()) Triple = LLVM_HOSTTRIPLE;
Ted Kremenekae360762007-12-03 22:06:55 +0000618
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000619 // If -arch foo was specified, remove the architecture from the triple we have
620 // so far and replace it with the specified one.
621 if (Arch.empty())
622 return Triple;
623
Ted Kremenekae360762007-12-03 22:06:55 +0000624 // Decompose the base triple into "arch" and suffix.
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000625 std::string::size_type FirstDashIdx = Triple.find("-");
Ted Kremenekae360762007-12-03 22:06:55 +0000626
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000627 if (FirstDashIdx == std::string::npos) {
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000628 fprintf(stderr,
629 "Malformed target triple: \"%s\" ('-' could not be found).\n",
Chris Lattner6590d212007-12-12 05:01:48 +0000630 Triple.c_str());
631 exit(1);
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000632 }
Ted Kremenekae360762007-12-03 22:06:55 +0000633
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000634 return Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end());
Ted Kremenekae360762007-12-03 22:06:55 +0000635}
636
637//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000638// Preprocessor Initialization
639//===----------------------------------------------------------------------===//
640
641// FIXME: Preprocessor builtins to support.
642// -A... - Play with #assertions
643// -undef - Undefine all predefined macros
644
645static llvm::cl::list<std::string>
646D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
647 llvm::cl::desc("Predefine the specified macro"));
648static llvm::cl::list<std::string>
649U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
650 llvm::cl::desc("Undefine the specified macro"));
651
Chris Lattner64299f82008-01-10 01:53:41 +0000652static llvm::cl::list<std::string>
653ImplicitIncludes("include", llvm::cl::value_desc("file"),
654 llvm::cl::desc("Include file before parsing"));
655
656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657// Append a #define line to Buf for Macro. Macro should be of the form XXX,
658// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
659// "#define XXX Y z W". To get a #define with no value, use "XXX=".
660static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
661 const char *Command = "#define ") {
662 Buf.insert(Buf.end(), Command, Command+strlen(Command));
663 if (const char *Equal = strchr(Macro, '=')) {
664 // Turn the = into ' '.
665 Buf.insert(Buf.end(), Macro, Equal);
666 Buf.push_back(' ');
667 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
668 } else {
669 // Push "macroname 1".
670 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
671 Buf.push_back(' ');
672 Buf.push_back('1');
673 }
674 Buf.push_back('\n');
675}
676
Chris Lattner64299f82008-01-10 01:53:41 +0000677/// AddImplicitInclude - Add an implicit #include of the specified file to the
678/// predefines buffer.
679static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){
680 const char *Inc = "#include \"";
681 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
682 Buf.insert(Buf.end(), File.begin(), File.end());
683 Buf.push_back('"');
684 Buf.push_back('\n');
685}
686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687
Chris Lattner53b0dab2007-10-09 22:10:18 +0000688/// InitializePreprocessor - Initialize the preprocessor getting it and the
Chris Lattner51574ea2008-04-19 23:25:44 +0000689/// environment ready to process a single file. This returns true on error.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000690///
Chris Lattner51574ea2008-04-19 23:25:44 +0000691static bool InitializePreprocessor(Preprocessor &PP,
692 bool InitializeSourceMgr,
693 const std::string &InFile) {
Chris Lattnerdee73592007-12-15 20:48:40 +0000694 FileManager &FileMgr = PP.getFileManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000695
Chris Lattner53b0dab2007-10-09 22:10:18 +0000696 // Figure out where to get and map in the main file.
Chris Lattnerdee73592007-12-15 20:48:40 +0000697 SourceManager &SourceMgr = PP.getSourceManager();
Ted Kremenek339b9c22008-04-17 22:31:54 +0000698
699 if (InitializeSourceMgr) {
700 if (InFile != "-") {
701 const FileEntry *File = FileMgr.getFile(InFile);
702 if (File) SourceMgr.createMainFileID(File, SourceLocation());
703 if (SourceMgr.getMainFileID() == 0) {
704 fprintf(stderr, "Error reading '%s'!\n",InFile.c_str());
Chris Lattner51574ea2008-04-19 23:25:44 +0000705 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000706 }
707 } else {
708 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
709 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
710 if (SourceMgr.getMainFileID() == 0) {
711 fprintf(stderr, "Error reading standard input! Empty?\n");
Chris Lattner51574ea2008-04-19 23:25:44 +0000712 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000713 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000714 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 }
Sam Bishop1102d6b2008-04-14 14:41:57 +0000716
Chris Lattneraa391972008-04-19 23:09:31 +0000717 std::vector<char> PredefineBuffer;
718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 // Add macros from the command line.
Sam Bishop1102d6b2008-04-14 14:41:57 +0000720 unsigned d = 0, D = D_macros.size();
721 unsigned u = 0, U = U_macros.size();
722 while (d < D || u < U) {
723 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
724 DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str());
725 else
726 DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef ");
727 }
728
Chris Lattner64299f82008-01-10 01:53:41 +0000729 // FIXME: Read any files specified by -imacros.
730
731 // Add implicit #includes from -include.
732 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
733 AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000734
Chris Lattneraa391972008-04-19 23:09:31 +0000735 // Null terminate PredefinedBuffer and add it.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000736 PredefineBuffer.push_back(0);
Chris Lattneraa391972008-04-19 23:09:31 +0000737 PP.setPredefines(&PredefineBuffer[0]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000738
739 // Once we've read this, we're done.
Chris Lattner51574ea2008-04-19 23:25:44 +0000740 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000741}
742
743//===----------------------------------------------------------------------===//
744// Preprocessor include path information.
745//===----------------------------------------------------------------------===//
746
747// This tool exports a large number of command line options to control how the
748// preprocessor searches for header files. At root, however, the Preprocessor
749// object takes a very simple interface: a list of directories to search for
750//
751// FIXME: -nostdinc,-nostdinc++
Chris Lattner0c946412007-08-26 17:47:35 +0000752// FIXME: -imultilib
Reid Spencer5f016e22007-07-11 17:01:13 +0000753//
Chris Lattner64299f82008-01-10 01:53:41 +0000754// FIXME: -imacros
Reid Spencer5f016e22007-07-11 17:01:13 +0000755
756static llvm::cl::opt<bool>
757nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
758
759// Various command line options. These four add directories to each chain.
760static llvm::cl::list<std::string>
761F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
762 llvm::cl::desc("Add directory to framework include search path"));
763static llvm::cl::list<std::string>
764I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
765 llvm::cl::desc("Add directory to include search path"));
766static llvm::cl::list<std::string>
767idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
768 llvm::cl::desc("Add directory to AFTER include search path"));
769static llvm::cl::list<std::string>
770iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
771 llvm::cl::desc("Add directory to QUOTE include search path"));
772static llvm::cl::list<std::string>
773isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
774 llvm::cl::desc("Add directory to SYSTEM include search path"));
775
776// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
777static llvm::cl::list<std::string>
778iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
779 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
780static llvm::cl::list<std::string>
781iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
782 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
783static llvm::cl::list<std::string>
784iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
785 llvm::cl::Prefix,
786 llvm::cl::desc("Set directory to include search path with prefix"));
787
Chris Lattner0c946412007-08-26 17:47:35 +0000788static llvm::cl::opt<std::string>
789isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
790 llvm::cl::desc("Set the system root directory (usually /)"));
791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792// Finally, implement the code that groks the options above.
Chris Lattner5f9eae52008-03-01 08:07:28 +0000793
Reid Spencer5f016e22007-07-11 17:01:13 +0000794/// InitializeIncludePaths - Process the -I options and set them in the
795/// HeaderSearch object.
Nico Weber0fca0222008-08-22 09:25:22 +0000796void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers,
797 FileManager &FM, const LangOptions &Lang) {
798 InitHeaderSearch Init(Headers, Verbose, isysroot);
799
Ted Kremenekf3721112008-05-31 00:27:00 +0000800 // Handle -I... and -F... options, walking the lists in parallel.
801 unsigned Iidx = 0, Fidx = 0;
802 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
803 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000804 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +0000805 ++Iidx;
806 } else {
Nico Weber0fca0222008-08-22 09:25:22 +0000807 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Ted Kremenekf3721112008-05-31 00:27:00 +0000808 ++Fidx;
809 }
810 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000811
Ted Kremenekf3721112008-05-31 00:27:00 +0000812 // Consume what's left from whatever list was longer.
813 for (; Iidx != I_dirs.size(); ++Iidx)
Nico Weber0fca0222008-08-22 09:25:22 +0000814 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +0000815 for (; Fidx != F_dirs.size(); ++Fidx)
Nico Weber0fca0222008-08-22 09:25:22 +0000816 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000817
818 // Handle -idirafter... options.
819 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000820 Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After,
821 false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000822
823 // Handle -iquote... options.
824 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000825 Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000826
827 // Handle -isystem... options.
828 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000829 Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000830
831 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
832 // parallel, processing the values in order of occurance to get the right
833 // prefixes.
834 {
835 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
836 unsigned iprefix_idx = 0;
837 unsigned iwithprefix_idx = 0;
838 unsigned iwithprefixbefore_idx = 0;
839 bool iprefix_done = iprefix_vals.empty();
840 bool iwithprefix_done = iwithprefix_vals.empty();
841 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
842 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
843 if (!iprefix_done &&
844 (iwithprefix_done ||
845 iprefix_vals.getPosition(iprefix_idx) <
846 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
847 (iwithprefixbefore_done ||
848 iprefix_vals.getPosition(iprefix_idx) <
849 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
850 Prefix = iprefix_vals[iprefix_idx];
851 ++iprefix_idx;
852 iprefix_done = iprefix_idx == iprefix_vals.size();
853 } else if (!iwithprefix_done &&
854 (iwithprefixbefore_done ||
855 iwithprefix_vals.getPosition(iwithprefix_idx) <
856 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
Nico Weber0fca0222008-08-22 09:25:22 +0000857 Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
858 InitHeaderSearch::System, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 ++iwithprefix_idx;
860 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
861 } else {
Nico Weber0fca0222008-08-22 09:25:22 +0000862 Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
863 InitHeaderSearch::Angled, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 ++iwithprefixbefore_idx;
865 iwithprefixbefore_done =
866 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
867 }
868 }
869 }
Chris Lattner5f9eae52008-03-01 08:07:28 +0000870
Nico Weber0fca0222008-08-22 09:25:22 +0000871 Init.AddDefaultEnvVarPaths(Lang);
Chris Lattner5f9eae52008-03-01 08:07:28 +0000872
Chris Lattnerdcaa0962008-03-03 03:16:03 +0000873 // Add the clang headers, which are relative to the clang driver.
874 llvm::sys::Path MainExecutablePath =
Chris Lattner985e1822008-03-03 05:57:43 +0000875 llvm::sys::Path::GetMainExecutable(Argv0,
876 (void*)(intptr_t)InitializeIncludePaths);
Chris Lattnerdcaa0962008-03-03 03:16:03 +0000877 if (!MainExecutablePath.isEmpty()) {
878 MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang
879 MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin
880 MainExecutablePath.appendComponent("Headers"); // Get foo/Headers
Nico Weber0fca0222008-08-22 09:25:22 +0000881 Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System,
882 false, false, false);
Chris Lattnerdcaa0962008-03-03 03:16:03 +0000883 }
884
Nico Weber0fca0222008-08-22 09:25:22 +0000885 if (!nostdinc)
886 Init.AddDefaultSystemIncludePaths(Lang);
Reid Spencer5f016e22007-07-11 17:01:13 +0000887
888 // Now that we have collected all of the include paths, merge them all
889 // together and tell the preprocessor about them.
890
Nico Weber0fca0222008-08-22 09:25:22 +0000891 Init.Realize();
Reid Spencer5f016e22007-07-11 17:01:13 +0000892}
893
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000894//===----------------------------------------------------------------------===//
895// Driver PreprocessorFactory - For lazily generating preprocessors ...
896//===----------------------------------------------------------------------===//
897
898namespace {
899class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000900 const std::string &InFile;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000901 Diagnostic &Diags;
902 const LangOptions &LangInfo;
903 TargetInfo &Target;
904 SourceManager &SourceMgr;
905 HeaderSearch &HeaderInfo;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000906 bool InitializeSourceMgr;
907
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000908public:
Ted Kremenek339b9c22008-04-17 22:31:54 +0000909 DriverPreprocessorFactory(const std::string &infile,
910 Diagnostic &diags, const LangOptions &opts,
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000911 TargetInfo &target, SourceManager &SM,
912 HeaderSearch &Headers)
Ted Kremenek339b9c22008-04-17 22:31:54 +0000913 : InFile(infile), Diags(diags), LangInfo(opts), Target(target),
914 SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {}
915
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000916
917 virtual ~DriverPreprocessorFactory() {}
918
919 virtual Preprocessor* CreatePreprocessor() {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000920 Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target,
921 SourceMgr, HeaderInfo);
922
Chris Lattner51574ea2008-04-19 23:25:44 +0000923 if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000924 delete PP;
925 return NULL;
926 }
927
928 InitializeSourceMgr = false;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000929 return PP;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000930 }
931};
932}
Reid Spencer5f016e22007-07-11 17:01:13 +0000933
Reid Spencer5f016e22007-07-11 17:01:13 +0000934//===----------------------------------------------------------------------===//
935// Basic Parser driver
936//===----------------------------------------------------------------------===//
937
Chris Lattner51574ea2008-04-19 23:25:44 +0000938static void ParseFile(Preprocessor &PP, MinimalAction *PA) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 Parser P(PP, *PA);
Ted Kremenek95041a22007-12-19 22:51:13 +0000940 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +0000941
942 // Parsing the specified input file.
943 P.ParseTranslationUnit();
944 delete PA;
Argyrios Kyrtzidis14d41402008-06-13 12:15:34 +0000945
946 if (VerifyDiagnostics)
947 exit(CheckDiagnostics(PP));
Reid Spencer5f016e22007-07-11 17:01:13 +0000948}
949
950//===----------------------------------------------------------------------===//
951// Main driver
952//===----------------------------------------------------------------------===//
953
Ted Kremenekdb094a22007-12-05 18:27:04 +0000954/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
955/// action. These consumers can operate on both ASTs that are freshly
956/// parsed from source files as well as those deserialized from Bitcode.
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000957static ASTConsumer* CreateASTConsumer(const std::string& InFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000958 Diagnostic& Diag, FileManager& FileMgr,
Chris Lattnere66b65c2008-02-06 01:42:25 +0000959 const LangOptions& LangOpts,
Chris Lattner3245a0a2008-04-16 06:11:58 +0000960 Preprocessor *PP,
Ted Kremenek815c78f2008-08-05 18:50:11 +0000961 PreprocessorFactory *PPF) {
Ted Kremenekdb094a22007-12-05 18:27:04 +0000962 switch (ProgAction) {
963 default:
964 return NULL;
965
966 case ASTPrint:
967 return CreateASTPrinter();
968
969 case ASTDump:
970 return CreateASTDumper();
971
972 case ASTView:
Ted Kremenek6a340832008-03-18 21:19:49 +0000973 return CreateASTViewer();
974
975 case EmitHTML:
Ted Kremenek339b9c22008-04-17 22:31:54 +0000976 return CreateHTMLPrinter(OutputFile, Diag, PP, PPF);
Ted Kremenek902141f2008-07-02 18:23:21 +0000977
Ted Kremenekdb094a22007-12-05 18:27:04 +0000978 case TestSerialization:
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000979 return CreateSerializationTest(Diag, FileMgr);
Ted Kremenekdb094a22007-12-05 18:27:04 +0000980
981 case EmitLLVM:
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000982 case EmitBC:
Ted Kremenek815c78f2008-08-05 18:50:11 +0000983 return CreateLLVMCodeGenWriter(ProgAction == EmitBC, Diag, LangOpts,
984 InFile, OutputFile, GenerateDebugInfo);
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000985
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000986 case SerializeAST:
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000987 // FIXME: Allow user to tailor where the file is written.
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000988 return CreateASTSerializer(InFile, OutputFile, Diag);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000989
Steve Naroffb29b4272008-04-14 22:03:09 +0000990 case RewriteObjC:
Chris Lattnerc68ab772008-03-22 00:08:40 +0000991 return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000992
993 case RunAnalysis:
994 assert (!AnalysisList.empty());
995 return CreateAnalysisConsumer(&AnalysisList[0],
996 &AnalysisList[0]+AnalysisList.size(),
997 Diag, PP, PPF, LangOpts,
998 AnalyzeSpecificFunction,
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000999 OutputFile, VisualizeEGDot, VisualizeEGUbi,
1000 TrimGraph, AnalyzeAll);
Ted Kremenekdb094a22007-12-05 18:27:04 +00001001 }
1002}
1003
Reid Spencer5f016e22007-07-11 17:01:13 +00001004/// ProcessInputFile - Process a single input file with the specified state.
1005///
Ted Kremenek339b9c22008-04-17 22:31:54 +00001006static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
1007 const std::string &InFile) {
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001008
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001009 llvm::OwningPtr<ASTConsumer> Consumer;
Chris Lattnerbd247762007-07-22 06:05:44 +00001010 bool ClearSourceMgr = false;
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001011
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 switch (ProgAction) {
1013 default:
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001014 Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(),
1015 PP.getFileManager(), PP.getLangOptions(),
1016 &PP, &PPF));
Ted Kremenekdb094a22007-12-05 18:27:04 +00001017
1018 if (!Consumer) {
1019 fprintf(stderr, "Unexpected program action!\n");
1020 return;
1021 }
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001022
Ted Kremenekdb094a22007-12-05 18:27:04 +00001023 break;
1024
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 case DumpTokens: { // Token dump mode.
Chris Lattnerd2177732007-07-20 16:59:19 +00001026 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001028 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 do {
1030 PP.Lex(Tok);
1031 PP.DumpToken(Tok, true);
1032 fprintf(stderr, "\n");
Chris Lattner057aaf62007-10-09 18:03:42 +00001033 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001034 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 break;
1036 }
1037 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
Chris Lattnerd2177732007-07-20 16:59:19 +00001038 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001040 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 do {
1042 PP.Lex(Tok);
Chris Lattner057aaf62007-10-09 18:03:42 +00001043 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001044 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 break;
1046 }
1047
1048 case PrintPreprocessedInput: // -E mode.
Chris Lattnere988bc22008-01-27 23:55:11 +00001049 DoPrintPreprocessedInput(PP, OutputFile);
Chris Lattnerbd247762007-07-22 06:05:44 +00001050 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001052
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 case ParseNoop: // -parse-noop
Ted Kremenek95041a22007-12-19 22:51:13 +00001054 ParseFile(PP, new MinimalAction(PP.getIdentifierTable()));
Chris Lattnerbd247762007-07-22 06:05:44 +00001055 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 break;
1057
1058 case ParsePrintCallbacks:
Ted Kremenek95041a22007-12-19 22:51:13 +00001059 ParseFile(PP, CreatePrintParserActionsAction(PP.getIdentifierTable()));
Chris Lattnerbd247762007-07-22 06:05:44 +00001060 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 break;
Ted Kremenek44579782007-09-25 18:37:20 +00001062
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001063 case ParseSyntaxOnly: // -fsyntax-only
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001064 Consumer.reset(new ASTConsumer());
Ted Kremenek2bf55142007-09-17 20:49:30 +00001065 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001066
1067 case RewriteMacros:
Chris Lattner09510522008-05-09 22:43:24 +00001068 RewriteMacrosInInput(PP, InFile, OutputFile);
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001069 ClearSourceMgr = true;
1070 break;
Chris Lattner580980b2007-09-16 19:46:59 +00001071 }
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001072
1073 if (Consumer) {
Ted Kremenek9f3d9422007-09-26 20:14:22 +00001074 if (VerifyDiagnostics)
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001075 exit(CheckASTConsumer(PP, Consumer.get()));
Chris Lattner31e6c7d2007-11-03 06:24:16 +00001076
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001077 ParseAST(PP, Consumer.get(), Stats);
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 }
Chris Lattnere66b65c2008-02-06 01:42:25 +00001079
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 if (Stats) {
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001081 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 PP.PrintStats();
1083 PP.getIdentifierTable().PrintStats();
Chris Lattnerdee73592007-12-15 20:48:40 +00001084 PP.getHeaderSearchInfo().PrintStats();
Chris Lattnerbd247762007-07-22 06:05:44 +00001085 if (ClearSourceMgr)
Chris Lattnerdee73592007-12-15 20:48:40 +00001086 PP.getSourceManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 fprintf(stderr, "\n");
1088 }
Chris Lattnerbd247762007-07-22 06:05:44 +00001089
1090 // For a multi-file compilation, some things are ok with nuking the source
1091 // manager tables, other require stable fileid/macroid's across multiple
1092 // files.
Chris Lattnerdee73592007-12-15 20:48:40 +00001093 if (ClearSourceMgr)
1094 PP.getSourceManager().clearIDTables();
Reid Spencer5f016e22007-07-11 17:01:13 +00001095}
1096
Ted Kremenek20e97482007-12-12 23:41:08 +00001097static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag,
1098 FileManager& FileMgr) {
1099
1100 if (VerifyDiagnostics) {
1101 fprintf(stderr, "-verify does not yet work with serialized ASTs.\n");
1102 exit (1);
1103 }
1104
1105 llvm::sys::Path Filename(InFile);
1106
1107 if (!Filename.isValid()) {
1108 fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str());
1109 exit (1);
1110 }
1111
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001112 llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr));
Ted Kremenekfe4e0152007-12-13 18:11:11 +00001113
1114 if (!TU) {
1115 fprintf(stderr, "error: file '%s' could not be deserialized\n",
1116 InFile.c_str());
1117 exit (1);
1118 }
1119
Ted Kremenek63ea8632007-12-19 19:27:38 +00001120 // Observe that we use the source file name stored in the deserialized
1121 // translation unit, rather than InFile.
Ted Kremenekee533642007-12-20 19:47:16 +00001122 llvm::OwningPtr<ASTConsumer>
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001123 Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(),
Ted Kremenek815c78f2008-08-05 18:50:11 +00001124 0, 0));
Nico Weber7bfaaae2008-08-10 19:59:06 +00001125
Ted Kremenek20e97482007-12-12 23:41:08 +00001126 if (!Consumer) {
1127 fprintf(stderr, "Unsupported program action with serialized ASTs!\n");
1128 exit (1);
1129 }
Nico Weber7bfaaae2008-08-10 19:59:06 +00001130
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001131 Consumer->Initialize(TU->getContext());
Nico Weber7bfaaae2008-08-10 19:59:06 +00001132
Chris Lattnere66b65c2008-02-06 01:42:25 +00001133 // FIXME: We need to inform Consumer about completed TagDecls as well.
Ted Kremenek20e97482007-12-12 23:41:08 +00001134 for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
1135 Consumer->HandleTopLevelDecl(*I);
Ted Kremenek20e97482007-12-12 23:41:08 +00001136}
1137
1138
Reid Spencer5f016e22007-07-11 17:01:13 +00001139static llvm::cl::list<std::string>
1140InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1141
Ted Kremenek20e97482007-12-12 23:41:08 +00001142static bool isSerializedFile(const std::string& InFile) {
1143 if (InFile.size() < 4)
1144 return false;
1145
1146 const char* s = InFile.c_str()+InFile.size()-4;
1147
1148 return s[0] == '.' &&
1149 s[1] == 'a' &&
1150 s[2] == 's' &&
1151 s[3] == 't';
1152}
1153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154
1155int main(int argc, char **argv) {
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001156 llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n");
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 llvm::sys::PrintStackTraceOnErrorSignal();
1158
1159 // If no input was specified, read from stdin.
1160 if (InputFilenames.empty())
1161 InputFilenames.push_back("-");
Ted Kremenek31e703b2007-12-11 23:28:38 +00001162
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 // Create a file manager object to provide access to and cache the filesystem.
1164 FileManager FileMgr;
1165
Ted Kremenek31e703b2007-12-11 23:28:38 +00001166 // Create the diagnostic client for reporting errors or for
1167 // implementing -verify.
Nico Weber7bfaaae2008-08-10 19:59:06 +00001168 DiagnosticClient* TextDiagClient = 0;
Ted Kremenek88f5cde2008-03-27 06:17:42 +00001169
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001170 if (!VerifyDiagnostics) {
1171 // Print diagnostics to stderr by default.
1172 TextDiagClient = new TextDiagnosticPrinter(!NoShowColumn,
1173 !NoCaretDiagnostics);
1174 } else {
1175 // When checking diagnostics, just buffer them up.
1176 TextDiagClient = new TextDiagnosticBuffer();
1177
1178 if (InputFilenames.size() != 1) {
1179 fprintf(stderr,
1180 "-verify only works on single input files for now.\n");
1181 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 }
1183 }
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001184
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 // Configure our handling of diagnostics.
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001186 llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient);
1187 Diagnostic Diags(DiagClient.get());
Ted Kremenek31e703b2007-12-11 23:28:38 +00001188 InitializeDiagnostics(Diags);
1189
Chris Lattner4f037832007-12-05 23:24:17 +00001190 // -I- is a deprecated GCC feature, scan for it and reject it.
1191 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
1192 if (I_dirs[i] == "-") {
Ted Kremenek2eefd862007-12-11 22:57:35 +00001193 Diags.Report(diag::err_pp_I_dash_not_supported);
Chris Lattner4f037832007-12-05 23:24:17 +00001194 I_dirs.erase(I_dirs.begin()+i);
1195 --i;
1196 }
1197 }
Chris Lattner11215192008-03-14 06:12:05 +00001198
1199 // Get information about the target being compiled for.
1200 std::string Triple = CreateTargetTriple();
Ted Kremenek7a08e282008-08-07 18:13:12 +00001201 llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple));
1202
Chris Lattner11215192008-03-14 06:12:05 +00001203 if (Target == 0) {
1204 fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
1205 Triple.c_str());
1206 fprintf(stderr, "Please use -triple or -arch.\n");
1207 exit(1);
1208 }
Chris Lattner4f037832007-12-05 23:24:17 +00001209
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001210 // Are we invoking one or more source analyses?
1211 if (!AnalysisList.empty() && ProgAction == ParseSyntaxOnly)
1212 ProgAction = RunAnalysis;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001213
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001214 llvm::OwningPtr<SourceManager> SourceMgr;
1215
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenek31e703b2007-12-11 23:28:38 +00001217 const std::string &InFile = InputFilenames[i];
Ted Kremenek31e703b2007-12-11 23:28:38 +00001218
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001219 if (isSerializedFile(InFile)) {
1220 Diags.setClient(TextDiagClient);
Ted Kremenek20e97482007-12-12 23:41:08 +00001221 ProcessSerializedFile(InFile,Diags,FileMgr);
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001222 }
Ted Kremenek20e97482007-12-12 23:41:08 +00001223 else {
1224 /// Create a SourceManager object. This tracks and owns all the file
1225 /// buffers allocated to a translation unit.
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001226 if (!SourceMgr)
1227 SourceMgr.reset(new SourceManager());
1228 else
1229 SourceMgr->clearIDTables();
Ted Kremenek31e703b2007-12-11 23:28:38 +00001230
Ted Kremenek20e97482007-12-12 23:41:08 +00001231 // Initialize language options, inferring file types from input filenames.
1232 LangOptions LangInfo;
1233 InitializeBaseLanguage();
1234 LangKind LK = GetLanguage(InFile);
1235 InitializeLangOptions(LangInfo, LK);
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +00001236 InitializeLanguageStandard(LangInfo, LK, Target.get());
Ted Kremenek01d9dbf2008-04-29 04:37:03 +00001237 InitializeGCMode(LangInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001238
1239 // Process the -I options and set them in the HeaderInfo.
1240 HeaderSearch HeaderInfo(FileMgr);
Ted Kremenekc68ecb52008-06-06 01:47:30 +00001241
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001242 InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001243
Ted Kremenek20e97482007-12-12 23:41:08 +00001244 // Set up the preprocessor with these options.
Ted Kremenek339b9c22008-04-17 22:31:54 +00001245 DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target,
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001246 *SourceMgr.get(), HeaderInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001247
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001248 llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor());
1249
Ted Kremenek339b9c22008-04-17 22:31:54 +00001250 if (!PP)
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001251 continue;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001252
1253 // Create the HTMLDiagnosticsClient if we are using one. Otherwise,
1254 // always reset to using TextDiagClient.
1255 llvm::OwningPtr<DiagnosticClient> TmpClient;
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001256
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001257 if (!HTMLDiag.empty()) {
1258 TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(),
1259 &PPFactory));
1260 Diags.setClient(TmpClient.get());
1261 }
1262 else
1263 Diags.setClient(TextDiagClient);
1264
1265 // Process the source file.
Ted Kremenek339b9c22008-04-17 22:31:54 +00001266 ProcessInputFile(*PP, PPFactory, InFile);
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001267 HeaderInfo.ClearFileInfo();
Ted Kremenek20e97482007-12-12 23:41:08 +00001268
1269 if (Stats)
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001270 SourceMgr->PrintStats();
Ted Kremenek20e97482007-12-12 23:41:08 +00001271 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 }
Chris Lattner11215192008-03-14 06:12:05 +00001273
Ted Kremenek7a08e282008-08-07 18:13:12 +00001274 if (unsigned NumDiagnostics = Diags.getNumDiagnostics())
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1276 (NumDiagnostics == 1 ? "" : "s"));
1277
1278 if (Stats) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 FileMgr.PrintStats();
1280 fprintf(stderr, "\n");
1281 }
1282
Chris Lattner96f1a642007-07-21 05:40:53 +00001283 return Diags.getNumErrors() != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001284}