blob: ff98bff9dc8bed8f392085979c55eceb5eed8835 [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.
Steve Naroff13188952008-09-18 14:10:13 +000064 RewriteBlocks, // ObjC->C Rewriter for Blocks.
Chris Lattnerb57e3d42008-05-08 06:52:13 +000065 RewriteMacros, // Expand macros but not #includes.
Ted Kremenek13e479b2008-03-19 07:53:42 +000066 HTMLTest, // HTML displayer testing stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +000067 EmitLLVM, // Emit a .ll file.
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +000068 EmitBC, // Emit a .bc file.
Ted Kremeneka1fa3a12007-12-13 00:37:31 +000069 SerializeAST, // Emit a .ast file.
Ted Kremenek6a340832008-03-18 21:19:49 +000070 EmitHTML, // Translate input source into HTML.
Chris Lattner3b427b32007-10-11 00:18:28 +000071 ASTPrint, // Parse ASTs and print them.
72 ASTDump, // Parse ASTs and dump them.
73 ASTView, // Parse ASTs and view them in Graphviz.
Ted Kremenekbfa82c42007-10-16 23:37:27 +000074 TestSerialization, // Run experimental serialization code.
Reid Spencer5f016e22007-07-11 17:01:13 +000075 ParsePrintCallbacks, // Parse and print each callback.
76 ParseSyntaxOnly, // Parse and perform semantic analysis.
77 ParseNoop, // Parse with noop callbacks.
78 RunPreprocessorOnly, // Just lex, no output.
79 PrintPreprocessedInput, // -E mode.
Ted Kremenekf4381fd2008-07-02 00:03:09 +000080 DumpTokens, // Token dump mode.
81 RunAnalysis // Run one or more source code analyses.
Reid Spencer5f016e22007-07-11 17:01:13 +000082};
83
84static llvm::cl::opt<ProgActions>
85ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
86 llvm::cl::init(ParseSyntaxOnly),
87 llvm::cl::values(
88 clEnumValN(RunPreprocessorOnly, "Eonly",
89 "Just run preprocessor, no output (for timings)"),
90 clEnumValN(PrintPreprocessedInput, "E",
91 "Run preprocessor, emit preprocessed file"),
92 clEnumValN(DumpTokens, "dumptokens",
93 "Run preprocessor, dump internal rep of tokens"),
94 clEnumValN(ParseNoop, "parse-noop",
95 "Run parser with noop callbacks (for timings)"),
96 clEnumValN(ParseSyntaxOnly, "fsyntax-only",
97 "Run parser and perform semantic analysis"),
98 clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
99 "Run parser and print each callback invoked"),
Ted Kremenek6a340832008-03-18 21:19:49 +0000100 clEnumValN(EmitHTML, "emit-html",
101 "Output input source as HTML"),
Chris Lattner3b427b32007-10-11 00:18:28 +0000102 clEnumValN(ASTPrint, "ast-print",
103 "Build ASTs and then pretty-print them"),
104 clEnumValN(ASTDump, "ast-dump",
105 "Build ASTs and then debug dump them"),
Chris Lattnerea254db2007-10-11 00:37:43 +0000106 clEnumValN(ASTView, "ast-view",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000107 "Build ASTs and view them with GraphViz"),
Ted Kremenekbfa82c42007-10-16 23:37:27 +0000108 clEnumValN(TestSerialization, "test-pickling",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000109 "Run prototype serialization code"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 clEnumValN(EmitLLVM, "emit-llvm",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000111 "Build ASTs then convert to LLVM, emit .ll file"),
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000112 clEnumValN(EmitBC, "emit-llvm-bc",
113 "Build ASTs then convert to LLVM, emit .bc file"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000114 clEnumValN(SerializeAST, "serialize",
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000115 "Build ASTs and emit .ast file"),
Steve Naroffb29b4272008-04-14 22:03:09 +0000116 clEnumValN(RewriteObjC, "rewrite-objc",
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000117 "Rewrite ObjC into C (code rewriter example)"),
118 clEnumValN(RewriteMacros, "rewrite-macros",
119 "Expand macros without full preprocessing"),
Steve Naroff13188952008-09-18 14:10:13 +0000120 clEnumValN(RewriteBlocks, "rewrite-blocks",
121 "Rewrite Blocks to C"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 clEnumValEnd));
123
Ted Kremenekccc76472007-12-19 19:47:59 +0000124
125static llvm::cl::opt<std::string>
126OutputFile("o",
Ted Kremenek50b56412007-12-19 19:50:41 +0000127 llvm::cl::value_desc("path"),
Ted Kremenekccc76472007-12-19 19:47:59 +0000128 llvm::cl::desc("Specify output file (for --serialize, this is a directory)"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000129
130//===----------------------------------------------------------------------===//
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000131// Code Generator Options
132//===----------------------------------------------------------------------===//
133static llvm::cl::opt<bool>
134GenerateDebugInfo("g",
135 llvm::cl::desc("Generate source level debug information"));
136
137//===----------------------------------------------------------------------===//
Ted Kremenek55af98c2008-04-14 18:40:58 +0000138// Diagnostic Options
139//===----------------------------------------------------------------------===//
140
Ted Kremenek41193e42007-09-26 19:42:19 +0000141static llvm::cl::opt<bool>
142VerifyDiagnostics("verify",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000143 llvm::cl::desc("Verify emitted diagnostics and warnings"));
Ted Kremenek41193e42007-09-26 19:42:19 +0000144
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000145static llvm::cl::opt<std::string>
146HTMLDiag("html-diags",
147 llvm::cl::desc("Generate HTML to report diagnostics"),
148 llvm::cl::value_desc("HTML directory"));
149
Nico Weberfd54ebc2008-08-05 23:33:20 +0000150static llvm::cl::opt<bool>
151NoShowColumn("fno-show-column",
152 llvm::cl::desc("Do not include column number on diagnostics"));
153
154static llvm::cl::opt<bool>
155NoCaretDiagnostics("fno-caret-diagnostics",
156 llvm::cl::desc("Do not include source line and caret with"
157 " diagnostics"));
158
159
Reid Spencer5f016e22007-07-11 17:01:13 +0000160//===----------------------------------------------------------------------===//
Ted Kremenek55af98c2008-04-14 18:40:58 +0000161// Analyzer Options
162//===----------------------------------------------------------------------===//
163
164static llvm::cl::opt<bool>
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000165VisualizeEGDot("analyzer-viz-egraph-graphviz",
166 llvm::cl::desc("Display exploded graph using GraphViz"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000167
168static llvm::cl::opt<bool>
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000169VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
170 llvm::cl::desc("Display exploded graph using Ubigraph"));
171
172static llvm::cl::opt<bool>
173AnalyzeAll("analyzer-opt-analyze-headers",
Ted Kremenek55af98c2008-04-14 18:40:58 +0000174 llvm::cl::desc("Force the static analyzer to analyze "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000175 "functions defined in header files"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000176
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000177static llvm::cl::list<Analyses>
178AnalysisList(llvm::cl::desc("Available Source Code Analyses:"),
179llvm::cl::values(
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000180#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000181clEnumValN(NAME, CMDFLAG, DESC),
182#include "Analyses.def"
183clEnumValEnd));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000184
Ted Kremenek55af98c2008-04-14 18:40:58 +0000185//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000186// Language Options
187//===----------------------------------------------------------------------===//
188
189enum LangKind {
190 langkind_unspecified,
191 langkind_c,
192 langkind_c_cpp,
193 langkind_cxx,
194 langkind_cxx_cpp,
195 langkind_objc,
196 langkind_objc_cpp,
197 langkind_objcxx,
198 langkind_objcxx_cpp
199};
200
201/* TODO: GCC also accepts:
202 c-header c++-header objective-c-header objective-c++-header
203 assembler assembler-with-cpp
204 ada, f77*, ratfor (!), f95, java, treelang
205 */
206static llvm::cl::opt<LangKind>
207BaseLang("x", llvm::cl::desc("Base language to compile"),
208 llvm::cl::init(langkind_unspecified),
209 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
210 clEnumValN(langkind_cxx, "c++", "C++"),
211 clEnumValN(langkind_objc, "objective-c", "Objective C"),
212 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
213 clEnumValN(langkind_c_cpp, "c-cpp-output",
214 "Preprocessed C"),
215 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
216 "Preprocessed C++"),
217 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
218 "Preprocessed Objective C"),
219 clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output",
220 "Preprocessed Objective C++"),
221 clEnumValEnd));
222
223static llvm::cl::opt<bool>
224LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
225 llvm::cl::Hidden);
226static llvm::cl::opt<bool>
227LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
228 llvm::cl::Hidden);
229
Ted Kremenek8904f152007-12-05 23:49:08 +0000230/// InitializeBaseLanguage - Handle the -x foo options.
231static void InitializeBaseLanguage() {
232 if (LangObjC)
233 BaseLang = langkind_objc;
234 else if (LangObjCXX)
235 BaseLang = langkind_objcxx;
236}
237
238static LangKind GetLanguage(const std::string &Filename) {
239 if (BaseLang != langkind_unspecified)
240 return BaseLang;
241
242 std::string::size_type DotPos = Filename.rfind('.');
243
244 if (DotPos == std::string::npos) {
245 BaseLang = langkind_c; // Default to C if no extension.
Chris Lattner9b2f6c42008-01-04 19:12:28 +0000246 return langkind_c;
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 }
248
Ted Kremenek8904f152007-12-05 23:49:08 +0000249 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
250 // C header: .h
251 // C++ header: .hh or .H;
252 // assembler no preprocessing: .s
253 // assembler: .S
254 if (Ext == "c")
255 return langkind_c;
256 else if (Ext == "i")
257 return langkind_c_cpp;
258 else if (Ext == "ii")
259 return langkind_cxx_cpp;
260 else if (Ext == "m")
261 return langkind_objc;
262 else if (Ext == "mi")
263 return langkind_objc_cpp;
264 else if (Ext == "mm" || Ext == "M")
265 return langkind_objcxx;
266 else if (Ext == "mii")
267 return langkind_objcxx_cpp;
268 else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
269 Ext == "c++" || Ext == "cp" || Ext == "cxx")
270 return langkind_cxx;
271 else
272 return langkind_c;
273}
274
275
276static void InitializeLangOptions(LangOptions &Options, LangKind LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 // FIXME: implement -fpreprocessed mode.
278 bool NoPreprocess = false;
279
Ted Kremenek8904f152007-12-05 23:49:08 +0000280 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 default: assert(0 && "Unknown language kind!");
282 case langkind_c_cpp:
283 NoPreprocess = true;
284 // FALLTHROUGH
285 case langkind_c:
286 break;
287 case langkind_cxx_cpp:
288 NoPreprocess = true;
289 // FALLTHROUGH
290 case langkind_cxx:
291 Options.CPlusPlus = 1;
292 break;
293 case langkind_objc_cpp:
294 NoPreprocess = true;
295 // FALLTHROUGH
296 case langkind_objc:
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000297 Options.ObjC1 = Options.ObjC2 = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 break;
299 case langkind_objcxx_cpp:
300 NoPreprocess = true;
301 // FALLTHROUGH
302 case langkind_objcxx:
303 Options.ObjC1 = Options.ObjC2 = 1;
304 Options.CPlusPlus = 1;
305 break;
306 }
307}
308
309/// LangStds - Language standards we support.
310enum LangStds {
311 lang_unspecified,
312 lang_c89, lang_c94, lang_c99,
Ted Kremenekea644d82008-09-03 21:22:16 +0000313 lang_gnu_START,
314 lang_gnu89 = lang_gnu_START, lang_gnu99,
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000315 lang_cxx98, lang_gnucxx98,
316 lang_cxx0x, lang_gnucxx0x
Reid Spencer5f016e22007-07-11 17:01:13 +0000317};
318
319static llvm::cl::opt<LangStds>
320LangStd("std", llvm::cl::desc("Language standard to compile for"),
321 llvm::cl::init(lang_unspecified),
322 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
323 clEnumValN(lang_c89, "c90", "ISO C 1990"),
324 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
325 clEnumValN(lang_c94, "iso9899:199409",
326 "ISO C 1990 with amendment 1"),
327 clEnumValN(lang_c99, "c99", "ISO C 1999"),
328// clEnumValN(lang_c99, "c9x", "ISO C 1999"),
329 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
330// clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
331 clEnumValN(lang_gnu89, "gnu89",
332 "ISO C 1990 with GNU extensions (default for C)"),
333 clEnumValN(lang_gnu99, "gnu99",
334 "ISO C 1999 with GNU extensions"),
335 clEnumValN(lang_gnu99, "gnu9x",
336 "ISO C 1999 with GNU extensions"),
337 clEnumValN(lang_cxx98, "c++98",
338 "ISO C++ 1998 with amendments"),
339 clEnumValN(lang_gnucxx98, "gnu++98",
340 "ISO C++ 1998 with amendments and GNU "
341 "extensions (default for C++)"),
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000342 clEnumValN(lang_cxx0x, "c++0x",
343 "Upcoming ISO C++ 200x with amendments"),
344 clEnumValN(lang_gnucxx0x, "gnu++0x",
345 "Upcoming ISO C++ 200x with amendments and GNU "
346 "extensions (default for C++)"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 clEnumValEnd));
348
349static llvm::cl::opt<bool>
350NoOperatorNames("fno-operator-names",
351 llvm::cl::desc("Do not treat C++ operator name keywords as "
352 "synonyms for operators"));
353
Anders Carlssonee98ac52007-10-15 02:50:23 +0000354static llvm::cl::opt<bool>
355PascalStrings("fpascal-strings",
356 llvm::cl::desc("Recognize and construct Pascal-style "
357 "string literals"));
Steve Naroffd62701b2008-02-07 03:50:06 +0000358
359static llvm::cl::opt<bool>
360MSExtensions("fms-extensions",
361 llvm::cl::desc("Accept some non-standard constructs used in "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000362 "Microsoft header files "));
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000363
364static llvm::cl::opt<bool>
365WritableStrings("fwritable-strings",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000366 llvm::cl::desc("Store string literals as writable data"));
Anders Carlsson695dbb62007-11-30 04:21:22 +0000367
368static llvm::cl::opt<bool>
369LaxVectorConversions("flax-vector-conversions",
370 llvm::cl::desc("Allow implicit conversions between vectors"
371 " with a different number of elements or "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000372 "different element types"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000373
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000374// FIXME: This (and all GCC -f options) really come in -f... and
375// -fno-... forms, and additionally support automagic behavior when
376// they are not defined. For example, -fexceptions defaults to on or
377// off depending on the language. We should support this behavior in
378// some form (perhaps just add a facility for distinguishing when an
379// has its default value from when it has been set to its default
380// value).
381static llvm::cl::opt<bool>
382Exceptions("fexceptions",
383 llvm::cl::desc("Enable support for exception handling."));
384
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000385static llvm::cl::opt<bool>
386GNURuntime("fgnu-runtime",
387 llvm::cl::desc("Generate output compatible with the standard GNU Objective-C runtime."));
388
389static llvm::cl::opt<bool>
390NeXTRuntime("fnext-runtime",
391 llvm::cl::desc("Generate output compatible with the NeXT runtime."));
392
Ted Kremenekea644d82008-09-03 21:22:16 +0000393
394
395static llvm::cl::opt<bool>
396Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences."));
397
398static llvm::cl::opt<bool>
399Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89."));
400
Reid Spencer5f016e22007-07-11 17:01:13 +0000401// FIXME: add:
Reid Spencer5f016e22007-07-11 17:01:13 +0000402// -fdollars-in-identifiers
Anders Carlssonee98ac52007-10-15 02:50:23 +0000403// -fpascal-strings
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +0000404static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
405 TargetInfo *Target) {
Ted Kremenekea644d82008-09-03 21:22:16 +0000406
407 if (Ansi) // "The -ansi option is equivalent to -std=c89."
408 LangStd = lang_c89;
409
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 if (LangStd == lang_unspecified) {
411 // Based on the base language, pick one.
Ted Kremenek8904f152007-12-05 23:49:08 +0000412 switch (LK) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 default: assert(0 && "Unknown base language");
414 case langkind_c:
415 case langkind_c_cpp:
416 case langkind_objc:
417 case langkind_objc_cpp:
418 LangStd = lang_gnu99;
419 break;
420 case langkind_cxx:
421 case langkind_cxx_cpp:
422 case langkind_objcxx:
423 case langkind_objcxx_cpp:
424 LangStd = lang_gnucxx98;
425 break;
426 }
427 }
428
429 switch (LangStd) {
430 default: assert(0 && "Unknown language standard!");
431
432 // Fall through from newer standards to older ones. This isn't really right.
433 // FIXME: Enable specifically the right features based on the language stds.
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000434 case lang_gnucxx0x:
435 case lang_cxx0x:
436 Options.CPlusPlus0x = 1;
437 // FALL THROUGH
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 case lang_gnucxx98:
439 case lang_cxx98:
440 Options.CPlusPlus = 1;
441 Options.CXXOperatorNames = !NoOperatorNames;
Nate Begeman8aebcb72007-11-15 07:30:50 +0000442 Options.Boolean = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 // FALL THROUGH.
444 case lang_gnu99:
445 case lang_c99:
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 Options.C99 = 1;
447 Options.HexFloats = 1;
448 // FALL THROUGH.
449 case lang_gnu89:
450 Options.BCPLComment = 1; // Only for C99/C++.
451 // FALL THROUGH.
452 case lang_c94:
Chris Lattner3426b9b2008-02-25 04:01:39 +0000453 Options.Digraphs = 1; // C94, C99, C++.
454 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 case lang_c89:
456 break;
457 }
Argyrios Kyrtzidisd1465522008-09-11 04:21:06 +0000458
459 if (Options.CPlusPlus) {
460 Options.C99 = 0;
461 Options.HexFloats = (LangStd == lang_gnucxx98 || LangStd==lang_gnucxx0x);
462 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000463
Chris Lattnerd658b562008-04-05 06:32:51 +0000464 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
465 Options.ImplicitInt = 1;
466 else
467 Options.ImplicitInt = 0;
Ted Kremenekea644d82008-09-03 21:22:16 +0000468
469 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi
470 // is specified, or -std is set to a conforming mode.
471 Options.Trigraphs = LangStd < lang_gnu_START || Trigraphs ? 1 : 0;
472
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 Options.DollarIdents = 1; // FIXME: Really a target property.
Anders Carlssonee98ac52007-10-15 02:50:23 +0000474 Options.PascalStrings = PascalStrings;
Steve Naroffd62701b2008-02-07 03:50:06 +0000475 Options.Microsoft = MSExtensions;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000476 Options.WritableStrings = WritableStrings;
Anders Carlsson695dbb62007-11-30 04:21:22 +0000477 Options.LaxVectorConversions = LaxVectorConversions;
Daniel Dunbar6379a7a2008-08-11 17:36:14 +0000478 Options.Exceptions = Exceptions;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000479
480 if (NeXTRuntime) {
481 Options.NeXTRuntime = 1;
482 } else if (GNURuntime) {
483 Options.NeXTRuntime = 0;
484 } else {
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +0000485 Options.NeXTRuntime = Target->useNeXTRuntimeAsDefault();
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000486 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000487}
488
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000489static llvm::cl::opt<bool>
490ObjCExclusiveGC("fobjc-gc-only",
491 llvm::cl::desc("Use GC exclusively for Objective-C related "
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000492 "memory management"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000493
494static llvm::cl::opt<bool>
495ObjCEnableGC("fobjc-gc",
Nico Weberfd54ebc2008-08-05 23:33:20 +0000496 llvm::cl::desc("Enable Objective-C garbage collection"));
Ted Kremenek01d9dbf2008-04-29 04:37:03 +0000497
498void InitializeGCMode(LangOptions &Options) {
499 if (ObjCExclusiveGC)
500 Options.setGCMode(LangOptions::GCOnly);
501 else if (ObjCEnableGC)
502 Options.setGCMode(LangOptions::HybridGC);
503}
504
505
Reid Spencer5f016e22007-07-11 17:01:13 +0000506//===----------------------------------------------------------------------===//
507// Our DiagnosticClient implementation
508//===----------------------------------------------------------------------===//
509
510// FIXME: Werror should take a list of things, -Werror=foo,bar
511static llvm::cl::opt<bool>
512WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
513
514static llvm::cl::opt<bool>
Chris Lattner5b4681c2008-05-29 15:36:45 +0000515SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings"));
516
517static llvm::cl::opt<bool>
Reid Spencer5f016e22007-07-11 17:01:13 +0000518WarnOnExtensions("pedantic", llvm::cl::init(false),
519 llvm::cl::desc("Issue a warning on uses of GCC extensions"));
520
521static llvm::cl::opt<bool>
522ErrorOnExtensions("pedantic-errors",
523 llvm::cl::desc("Issue an error on uses of GCC extensions"));
524
525static llvm::cl::opt<bool>
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000526SuppressSystemWarnings("suppress-system-warnings",
527 llvm::cl::desc("Issue an error on uses of GCC extensions"),
528 llvm::cl::init(true));
529
530static llvm::cl::opt<bool>
Reid Spencer5f016e22007-07-11 17:01:13 +0000531WarnUnusedMacros("Wunused_macros",
532 llvm::cl::desc("Warn for unused macros in the main translation unit"));
533
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000534static llvm::cl::opt<bool>
535WarnFloatEqual("Wfloat-equal",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000536 llvm::cl::desc("Warn about equality comparisons of floating point values"));
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000537
Ted Kremenek73da5902007-12-17 17:50:07 +0000538static llvm::cl::opt<bool>
539WarnNoFormatNonLiteral("Wno-format-nonliteral",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000540 llvm::cl::desc("Do not warn about non-literal format strings"));
Ted Kremenek73da5902007-12-17 17:50:07 +0000541
Chris Lattner116a4b12008-01-23 17:19:46 +0000542static llvm::cl::opt<bool>
543WarnUndefMacros("Wundef",
544 llvm::cl::desc("Warn on use of undefined macros in #if's"));
545
Chris Lattner37d10842008-05-05 21:18:06 +0000546static llvm::cl::opt<bool>
Ted Kremenek358256d2008-05-30 16:42:02 +0000547WarnImplicitFunctionDeclaration("Wimplicit-function-declaration",
548 llvm::cl::desc("Warn about uses of implicitly defined functions"));
Chris Lattner116a4b12008-01-23 17:19:46 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550/// InitializeDiagnostics - Initialize the diagnostic object, based on the
551/// current command line option settings.
552static void InitializeDiagnostics(Diagnostic &Diags) {
Chris Lattner5b4681c2008-05-29 15:36:45 +0000553 Diags.setIgnoreAllWarnings(SilenceWarnings);
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 Diags.setWarningsAsErrors(WarningsAsErrors);
555 Diags.setWarnOnExtensions(WarnOnExtensions);
556 Diags.setErrorOnExtensions(ErrorOnExtensions);
557
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000558 // Suppress warnings in system headers unless requested not to.
559 Diags.setSuppressSystemWarnings(SuppressSystemWarnings);
560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 // Silence the "macro is not used" warning unless requested.
562 if (!WarnUnusedMacros)
563 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
Ted Kremenekdb87bca2007-11-13 18:37:02 +0000564
565 // Silence "floating point comparison" warnings unless requested.
566 if (!WarnFloatEqual)
567 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
Ted Kremenek73da5902007-12-17 17:50:07 +0000568
569 // Silence "format string is not a string literal" warnings if requested
570 if (WarnNoFormatNonLiteral)
Ted Kremenek7c1d3df2007-12-17 17:50:39 +0000571 Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant,
572 diag::MAP_IGNORE);
Chris Lattner116a4b12008-01-23 17:19:46 +0000573 if (!WarnUndefMacros)
574 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);
Steve Naroffe7a37302008-02-11 22:40:08 +0000575
Chris Lattner37d10842008-05-05 21:18:06 +0000576 if (!WarnImplicitFunctionDeclaration)
577 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
578 diag::MAP_IGNORE);
579
Steve Naroffe7a37302008-02-11 22:40:08 +0000580 if (MSExtensions) // MS allows unnamed struct/union fields.
581 Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE);
Chris Lattner8aedf192008-05-04 23:52:02 +0000582
583 // If -pedantic-errors is set, turn extensions that warn by default into
584 // errors.
585 if (ErrorOnExtensions) {
586 Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large,
587 diag::MAP_ERROR);
588 Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large,
589 diag::MAP_ERROR);
590 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000591}
592
593//===----------------------------------------------------------------------===//
Ted Kremenekcb330932008-02-18 21:21:23 +0000594// Analysis-specific options.
595//===----------------------------------------------------------------------===//
596
597static llvm::cl::opt<std::string>
598AnalyzeSpecificFunction("analyze-function",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000599 llvm::cl::desc("Run analysis on specific function"));
Ted Kremenekcb330932008-02-18 21:21:23 +0000600
Ted Kremenekffe0f432008-03-07 22:58:01 +0000601static llvm::cl::opt<bool>
Ted Kremenekd71ed262008-04-10 22:16:52 +0000602TrimGraph("trim-egraph",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000603 llvm::cl::desc("Only show error-related paths in the analysis graph"));
Ted Kremenekffe0f432008-03-07 22:58:01 +0000604
Ted Kremenekcb330932008-02-18 21:21:23 +0000605//===----------------------------------------------------------------------===//
Ted Kremenekae360762007-12-03 22:06:55 +0000606// Target Triple Processing.
607//===----------------------------------------------------------------------===//
608
609static llvm::cl::opt<std::string>
610TargetTriple("triple",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000611 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000612
Chris Lattner42e67372008-03-05 01:18:20 +0000613static llvm::cl::opt<std::string>
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000614Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)"));
Ted Kremenekae360762007-12-03 22:06:55 +0000615
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000616static std::string CreateTargetTriple() {
Ted Kremenekae360762007-12-03 22:06:55 +0000617 // Initialize base triple. If a -triple option has been specified, use
618 // that triple. Otherwise, default to the host triple.
Chris Lattner6590d212007-12-12 05:01:48 +0000619 std::string Triple = TargetTriple;
620 if (Triple.empty()) Triple = LLVM_HOSTTRIPLE;
Ted Kremenekae360762007-12-03 22:06:55 +0000621
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000622 // If -arch foo was specified, remove the architecture from the triple we have
623 // so far and replace it with the specified one.
624 if (Arch.empty())
625 return Triple;
626
Ted Kremenekae360762007-12-03 22:06:55 +0000627 // Decompose the base triple into "arch" and suffix.
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000628 std::string::size_type FirstDashIdx = Triple.find("-");
Ted Kremenekae360762007-12-03 22:06:55 +0000629
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000630 if (FirstDashIdx == std::string::npos) {
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000631 fprintf(stderr,
632 "Malformed target triple: \"%s\" ('-' could not be found).\n",
Chris Lattner6590d212007-12-12 05:01:48 +0000633 Triple.c_str());
634 exit(1);
Ted Kremenek9b4ebc22007-12-03 22:11:31 +0000635 }
Ted Kremenekae360762007-12-03 22:06:55 +0000636
Chris Lattner6fd9fa12008-03-09 01:35:13 +0000637 return Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end());
Ted Kremenekae360762007-12-03 22:06:55 +0000638}
639
640//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000641// Preprocessor Initialization
642//===----------------------------------------------------------------------===//
643
644// FIXME: Preprocessor builtins to support.
645// -A... - Play with #assertions
646// -undef - Undefine all predefined macros
647
648static llvm::cl::list<std::string>
649D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
650 llvm::cl::desc("Predefine the specified macro"));
651static llvm::cl::list<std::string>
652U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
653 llvm::cl::desc("Undefine the specified macro"));
654
Chris Lattner64299f82008-01-10 01:53:41 +0000655static llvm::cl::list<std::string>
656ImplicitIncludes("include", llvm::cl::value_desc("file"),
657 llvm::cl::desc("Include file before parsing"));
658
659
Reid Spencer5f016e22007-07-11 17:01:13 +0000660// Append a #define line to Buf for Macro. Macro should be of the form XXX,
661// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
662// "#define XXX Y z W". To get a #define with no value, use "XXX=".
663static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
664 const char *Command = "#define ") {
665 Buf.insert(Buf.end(), Command, Command+strlen(Command));
666 if (const char *Equal = strchr(Macro, '=')) {
667 // Turn the = into ' '.
668 Buf.insert(Buf.end(), Macro, Equal);
669 Buf.push_back(' ');
670 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
671 } else {
672 // Push "macroname 1".
673 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
674 Buf.push_back(' ');
675 Buf.push_back('1');
676 }
677 Buf.push_back('\n');
678}
679
Chris Lattner64299f82008-01-10 01:53:41 +0000680/// AddImplicitInclude - Add an implicit #include of the specified file to the
681/// predefines buffer.
682static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){
683 const char *Inc = "#include \"";
684 Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
685 Buf.insert(Buf.end(), File.begin(), File.end());
686 Buf.push_back('"');
687 Buf.push_back('\n');
688}
689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690
Chris Lattner53b0dab2007-10-09 22:10:18 +0000691/// InitializePreprocessor - Initialize the preprocessor getting it and the
Chris Lattner51574ea2008-04-19 23:25:44 +0000692/// environment ready to process a single file. This returns true on error.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000693///
Chris Lattner51574ea2008-04-19 23:25:44 +0000694static bool InitializePreprocessor(Preprocessor &PP,
695 bool InitializeSourceMgr,
696 const std::string &InFile) {
Chris Lattnerdee73592007-12-15 20:48:40 +0000697 FileManager &FileMgr = PP.getFileManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000698
Chris Lattner53b0dab2007-10-09 22:10:18 +0000699 // Figure out where to get and map in the main file.
Chris Lattnerdee73592007-12-15 20:48:40 +0000700 SourceManager &SourceMgr = PP.getSourceManager();
Ted Kremenek339b9c22008-04-17 22:31:54 +0000701
702 if (InitializeSourceMgr) {
703 if (InFile != "-") {
704 const FileEntry *File = FileMgr.getFile(InFile);
705 if (File) SourceMgr.createMainFileID(File, SourceLocation());
706 if (SourceMgr.getMainFileID() == 0) {
707 fprintf(stderr, "Error reading '%s'!\n",InFile.c_str());
Chris Lattner51574ea2008-04-19 23:25:44 +0000708 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000709 }
710 } else {
711 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
712 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
713 if (SourceMgr.getMainFileID() == 0) {
714 fprintf(stderr, "Error reading standard input! Empty?\n");
Chris Lattner51574ea2008-04-19 23:25:44 +0000715 return true;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000716 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000717 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 }
Sam Bishop1102d6b2008-04-14 14:41:57 +0000719
Chris Lattneraa391972008-04-19 23:09:31 +0000720 std::vector<char> PredefineBuffer;
721
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 // Add macros from the command line.
Sam Bishop1102d6b2008-04-14 14:41:57 +0000723 unsigned d = 0, D = D_macros.size();
724 unsigned u = 0, U = U_macros.size();
725 while (d < D || u < U) {
726 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
727 DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str());
728 else
729 DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef ");
730 }
731
Chris Lattner64299f82008-01-10 01:53:41 +0000732 // FIXME: Read any files specified by -imacros.
733
734 // Add implicit #includes from -include.
735 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
736 AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000737
Chris Lattneraa391972008-04-19 23:09:31 +0000738 // Null terminate PredefinedBuffer and add it.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000739 PredefineBuffer.push_back(0);
Chris Lattneraa391972008-04-19 23:09:31 +0000740 PP.setPredefines(&PredefineBuffer[0]);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000741
742 // Once we've read this, we're done.
Chris Lattner51574ea2008-04-19 23:25:44 +0000743 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000744}
745
746//===----------------------------------------------------------------------===//
747// Preprocessor include path information.
748//===----------------------------------------------------------------------===//
749
750// This tool exports a large number of command line options to control how the
751// preprocessor searches for header files. At root, however, the Preprocessor
752// object takes a very simple interface: a list of directories to search for
753//
754// FIXME: -nostdinc,-nostdinc++
Chris Lattner0c946412007-08-26 17:47:35 +0000755// FIXME: -imultilib
Reid Spencer5f016e22007-07-11 17:01:13 +0000756//
Chris Lattner64299f82008-01-10 01:53:41 +0000757// FIXME: -imacros
Reid Spencer5f016e22007-07-11 17:01:13 +0000758
759static llvm::cl::opt<bool>
760nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
761
762// Various command line options. These four add directories to each chain.
763static llvm::cl::list<std::string>
764F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
765 llvm::cl::desc("Add directory to framework include search path"));
766static llvm::cl::list<std::string>
767I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
768 llvm::cl::desc("Add directory to include search path"));
769static llvm::cl::list<std::string>
770idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
771 llvm::cl::desc("Add directory to AFTER include search path"));
772static llvm::cl::list<std::string>
773iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
774 llvm::cl::desc("Add directory to QUOTE include search path"));
775static llvm::cl::list<std::string>
776isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
777 llvm::cl::desc("Add directory to SYSTEM include search path"));
778
779// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
780static llvm::cl::list<std::string>
781iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
782 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
783static llvm::cl::list<std::string>
784iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
785 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
786static llvm::cl::list<std::string>
787iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
788 llvm::cl::Prefix,
789 llvm::cl::desc("Set directory to include search path with prefix"));
790
Chris Lattner0c946412007-08-26 17:47:35 +0000791static llvm::cl::opt<std::string>
792isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
793 llvm::cl::desc("Set the system root directory (usually /)"));
794
Reid Spencer5f016e22007-07-11 17:01:13 +0000795// Finally, implement the code that groks the options above.
Chris Lattner5f9eae52008-03-01 08:07:28 +0000796
Reid Spencer5f016e22007-07-11 17:01:13 +0000797/// InitializeIncludePaths - Process the -I options and set them in the
798/// HeaderSearch object.
Nico Weber0fca0222008-08-22 09:25:22 +0000799void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers,
800 FileManager &FM, const LangOptions &Lang) {
801 InitHeaderSearch Init(Headers, Verbose, isysroot);
802
Ted Kremenekf3721112008-05-31 00:27:00 +0000803 // Handle -I... and -F... options, walking the lists in parallel.
804 unsigned Iidx = 0, Fidx = 0;
805 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
806 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000807 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +0000808 ++Iidx;
809 } else {
Nico Weber0fca0222008-08-22 09:25:22 +0000810 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Ted Kremenekf3721112008-05-31 00:27:00 +0000811 ++Fidx;
812 }
813 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000814
Ted Kremenekf3721112008-05-31 00:27:00 +0000815 // Consume what's left from whatever list was longer.
816 for (; Iidx != I_dirs.size(); ++Iidx)
Nico Weber0fca0222008-08-22 09:25:22 +0000817 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekf3721112008-05-31 00:27:00 +0000818 for (; Fidx != F_dirs.size(); ++Fidx)
Nico Weber0fca0222008-08-22 09:25:22 +0000819 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000820
821 // Handle -idirafter... options.
822 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000823 Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After,
824 false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000825
826 // Handle -iquote... options.
827 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000828 Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000829
830 // Handle -isystem... options.
831 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
Nico Weber0fca0222008-08-22 09:25:22 +0000832 Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000833
834 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
835 // parallel, processing the values in order of occurance to get the right
836 // prefixes.
837 {
838 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
839 unsigned iprefix_idx = 0;
840 unsigned iwithprefix_idx = 0;
841 unsigned iwithprefixbefore_idx = 0;
842 bool iprefix_done = iprefix_vals.empty();
843 bool iwithprefix_done = iwithprefix_vals.empty();
844 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
845 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
846 if (!iprefix_done &&
847 (iwithprefix_done ||
848 iprefix_vals.getPosition(iprefix_idx) <
849 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
850 (iwithprefixbefore_done ||
851 iprefix_vals.getPosition(iprefix_idx) <
852 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
853 Prefix = iprefix_vals[iprefix_idx];
854 ++iprefix_idx;
855 iprefix_done = iprefix_idx == iprefix_vals.size();
856 } else if (!iwithprefix_done &&
857 (iwithprefixbefore_done ||
858 iwithprefix_vals.getPosition(iwithprefix_idx) <
859 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
Nico Weber0fca0222008-08-22 09:25:22 +0000860 Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
861 InitHeaderSearch::System, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 ++iwithprefix_idx;
863 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
864 } else {
Nico Weber0fca0222008-08-22 09:25:22 +0000865 Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
866 InitHeaderSearch::Angled, false, false, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 ++iwithprefixbefore_idx;
868 iwithprefixbefore_done =
869 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
870 }
871 }
872 }
Chris Lattner5f9eae52008-03-01 08:07:28 +0000873
Nico Weber0fca0222008-08-22 09:25:22 +0000874 Init.AddDefaultEnvVarPaths(Lang);
Chris Lattner5f9eae52008-03-01 08:07:28 +0000875
Chris Lattnerdcaa0962008-03-03 03:16:03 +0000876 // Add the clang headers, which are relative to the clang driver.
877 llvm::sys::Path MainExecutablePath =
Chris Lattner985e1822008-03-03 05:57:43 +0000878 llvm::sys::Path::GetMainExecutable(Argv0,
879 (void*)(intptr_t)InitializeIncludePaths);
Chris Lattnerdcaa0962008-03-03 03:16:03 +0000880 if (!MainExecutablePath.isEmpty()) {
881 MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang
882 MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin
883 MainExecutablePath.appendComponent("Headers"); // Get foo/Headers
Nico Weber0fca0222008-08-22 09:25:22 +0000884 Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System,
885 false, false, false);
Chris Lattnerdcaa0962008-03-03 03:16:03 +0000886 }
887
Nico Weber0fca0222008-08-22 09:25:22 +0000888 if (!nostdinc)
889 Init.AddDefaultSystemIncludePaths(Lang);
Reid Spencer5f016e22007-07-11 17:01:13 +0000890
891 // Now that we have collected all of the include paths, merge them all
892 // together and tell the preprocessor about them.
893
Nico Weber0fca0222008-08-22 09:25:22 +0000894 Init.Realize();
Reid Spencer5f016e22007-07-11 17:01:13 +0000895}
896
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000897//===----------------------------------------------------------------------===//
898// Driver PreprocessorFactory - For lazily generating preprocessors ...
899//===----------------------------------------------------------------------===//
900
901namespace {
902class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000903 const std::string &InFile;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000904 Diagnostic &Diags;
905 const LangOptions &LangInfo;
906 TargetInfo &Target;
907 SourceManager &SourceMgr;
908 HeaderSearch &HeaderInfo;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000909 bool InitializeSourceMgr;
910
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000911public:
Ted Kremenek339b9c22008-04-17 22:31:54 +0000912 DriverPreprocessorFactory(const std::string &infile,
913 Diagnostic &diags, const LangOptions &opts,
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000914 TargetInfo &target, SourceManager &SM,
915 HeaderSearch &Headers)
Ted Kremenek339b9c22008-04-17 22:31:54 +0000916 : InFile(infile), Diags(diags), LangInfo(opts), Target(target),
917 SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {}
918
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000919
920 virtual ~DriverPreprocessorFactory() {}
921
922 virtual Preprocessor* CreatePreprocessor() {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000923 Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target,
924 SourceMgr, HeaderInfo);
925
Chris Lattner51574ea2008-04-19 23:25:44 +0000926 if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) {
Ted Kremenek339b9c22008-04-17 22:31:54 +0000927 delete PP;
928 return NULL;
929 }
930
931 InitializeSourceMgr = false;
Ted Kremenek339b9c22008-04-17 22:31:54 +0000932 return PP;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000933 }
934};
935}
Reid Spencer5f016e22007-07-11 17:01:13 +0000936
Reid Spencer5f016e22007-07-11 17:01:13 +0000937//===----------------------------------------------------------------------===//
938// Basic Parser driver
939//===----------------------------------------------------------------------===//
940
Chris Lattner51574ea2008-04-19 23:25:44 +0000941static void ParseFile(Preprocessor &PP, MinimalAction *PA) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 Parser P(PP, *PA);
Ted Kremenek95041a22007-12-19 22:51:13 +0000943 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +0000944
945 // Parsing the specified input file.
946 P.ParseTranslationUnit();
947 delete PA;
Argyrios Kyrtzidis14d41402008-06-13 12:15:34 +0000948
949 if (VerifyDiagnostics)
950 exit(CheckDiagnostics(PP));
Reid Spencer5f016e22007-07-11 17:01:13 +0000951}
952
953//===----------------------------------------------------------------------===//
954// Main driver
955//===----------------------------------------------------------------------===//
956
Ted Kremenekdb094a22007-12-05 18:27:04 +0000957/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
958/// action. These consumers can operate on both ASTs that are freshly
959/// parsed from source files as well as those deserialized from Bitcode.
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000960static ASTConsumer* CreateASTConsumer(const std::string& InFile,
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000961 Diagnostic& Diag, FileManager& FileMgr,
Chris Lattnere66b65c2008-02-06 01:42:25 +0000962 const LangOptions& LangOpts,
Chris Lattner3245a0a2008-04-16 06:11:58 +0000963 Preprocessor *PP,
Ted Kremenek815c78f2008-08-05 18:50:11 +0000964 PreprocessorFactory *PPF) {
Ted Kremenekdb094a22007-12-05 18:27:04 +0000965 switch (ProgAction) {
966 default:
967 return NULL;
968
969 case ASTPrint:
970 return CreateASTPrinter();
971
972 case ASTDump:
973 return CreateASTDumper();
974
975 case ASTView:
Ted Kremenek6a340832008-03-18 21:19:49 +0000976 return CreateASTViewer();
977
978 case EmitHTML:
Ted Kremenek339b9c22008-04-17 22:31:54 +0000979 return CreateHTMLPrinter(OutputFile, Diag, PP, PPF);
Ted Kremenek902141f2008-07-02 18:23:21 +0000980
Ted Kremenekdb094a22007-12-05 18:27:04 +0000981 case TestSerialization:
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000982 return CreateSerializationTest(Diag, FileMgr);
Ted Kremenekdb094a22007-12-05 18:27:04 +0000983
984 case EmitLLVM:
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000985 case EmitBC:
Ted Kremenek815c78f2008-08-05 18:50:11 +0000986 return CreateLLVMCodeGenWriter(ProgAction == EmitBC, Diag, LangOpts,
987 InFile, OutputFile, GenerateDebugInfo);
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000988
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000989 case SerializeAST:
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000990 // FIXME: Allow user to tailor where the file is written.
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000991 return CreateASTSerializer(InFile, OutputFile, Diag);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000992
Steve Naroffb29b4272008-04-14 22:03:09 +0000993 case RewriteObjC:
Chris Lattnerc68ab772008-03-22 00:08:40 +0000994 return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts);
Steve Naroff13188952008-09-18 14:10:13 +0000995
996 case RewriteBlocks:
997 return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000998
999 case RunAnalysis:
1000 assert (!AnalysisList.empty());
1001 return CreateAnalysisConsumer(&AnalysisList[0],
1002 &AnalysisList[0]+AnalysisList.size(),
1003 Diag, PP, PPF, LangOpts,
1004 AnalyzeSpecificFunction,
Ted Kremenekf8ce6992008-08-27 22:31:43 +00001005 OutputFile, VisualizeEGDot, VisualizeEGUbi,
1006 TrimGraph, AnalyzeAll);
Ted Kremenekdb094a22007-12-05 18:27:04 +00001007 }
1008}
1009
Reid Spencer5f016e22007-07-11 17:01:13 +00001010/// ProcessInputFile - Process a single input file with the specified state.
1011///
Ted Kremenek339b9c22008-04-17 22:31:54 +00001012static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
1013 const std::string &InFile) {
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001014
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001015 llvm::OwningPtr<ASTConsumer> Consumer;
Chris Lattnerbd247762007-07-22 06:05:44 +00001016 bool ClearSourceMgr = false;
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001017
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 switch (ProgAction) {
1019 default:
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001020 Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(),
1021 PP.getFileManager(), PP.getLangOptions(),
1022 &PP, &PPF));
Ted Kremenekdb094a22007-12-05 18:27:04 +00001023
1024 if (!Consumer) {
1025 fprintf(stderr, "Unexpected program action!\n");
1026 return;
1027 }
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001028
Ted Kremenekdb094a22007-12-05 18:27:04 +00001029 break;
1030
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 case DumpTokens: { // Token dump mode.
Chris Lattnerd2177732007-07-20 16:59:19 +00001032 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001034 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 do {
1036 PP.Lex(Tok);
1037 PP.DumpToken(Tok, true);
1038 fprintf(stderr, "\n");
Chris Lattner057aaf62007-10-09 18:03:42 +00001039 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001040 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 break;
1042 }
1043 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
Chris Lattnerd2177732007-07-20 16:59:19 +00001044 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 // Start parsing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +00001046 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 do {
1048 PP.Lex(Tok);
Chris Lattner057aaf62007-10-09 18:03:42 +00001049 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +00001050 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 break;
1052 }
1053
1054 case PrintPreprocessedInput: // -E mode.
Chris Lattnere988bc22008-01-27 23:55:11 +00001055 DoPrintPreprocessedInput(PP, OutputFile);
Chris Lattnerbd247762007-07-22 06:05:44 +00001056 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001058
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 case ParseNoop: // -parse-noop
Ted Kremenek95041a22007-12-19 22:51:13 +00001060 ParseFile(PP, new MinimalAction(PP.getIdentifierTable()));
Chris Lattnerbd247762007-07-22 06:05:44 +00001061 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 break;
1063
1064 case ParsePrintCallbacks:
Ted Kremenek95041a22007-12-19 22:51:13 +00001065 ParseFile(PP, CreatePrintParserActionsAction(PP.getIdentifierTable()));
Chris Lattnerbd247762007-07-22 06:05:44 +00001066 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 break;
Ted Kremenek44579782007-09-25 18:37:20 +00001068
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001069 case ParseSyntaxOnly: // -fsyntax-only
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001070 Consumer.reset(new ASTConsumer());
Ted Kremenek2bf55142007-09-17 20:49:30 +00001071 break;
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001072
1073 case RewriteMacros:
Chris Lattner09510522008-05-09 22:43:24 +00001074 RewriteMacrosInInput(PP, InFile, OutputFile);
Chris Lattnerb57e3d42008-05-08 06:52:13 +00001075 ClearSourceMgr = true;
1076 break;
Chris Lattner580980b2007-09-16 19:46:59 +00001077 }
Ted Kremenekd39bcd82007-09-26 18:39:29 +00001078
1079 if (Consumer) {
Ted Kremenek9f3d9422007-09-26 20:14:22 +00001080 if (VerifyDiagnostics)
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001081 exit(CheckASTConsumer(PP, Consumer.get()));
Chris Lattner31e6c7d2007-11-03 06:24:16 +00001082
Ted Kremenek7e7e6252008-08-08 02:46:37 +00001083 ParseAST(PP, Consumer.get(), Stats);
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 }
Chris Lattnere66b65c2008-02-06 01:42:25 +00001085
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 if (Stats) {
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001087 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 PP.PrintStats();
1089 PP.getIdentifierTable().PrintStats();
Chris Lattnerdee73592007-12-15 20:48:40 +00001090 PP.getHeaderSearchInfo().PrintStats();
Chris Lattnerbd247762007-07-22 06:05:44 +00001091 if (ClearSourceMgr)
Chris Lattnerdee73592007-12-15 20:48:40 +00001092 PP.getSourceManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 fprintf(stderr, "\n");
1094 }
Chris Lattnerbd247762007-07-22 06:05:44 +00001095
1096 // For a multi-file compilation, some things are ok with nuking the source
1097 // manager tables, other require stable fileid/macroid's across multiple
1098 // files.
Chris Lattnerdee73592007-12-15 20:48:40 +00001099 if (ClearSourceMgr)
1100 PP.getSourceManager().clearIDTables();
Reid Spencer5f016e22007-07-11 17:01:13 +00001101}
1102
Ted Kremenek20e97482007-12-12 23:41:08 +00001103static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag,
1104 FileManager& FileMgr) {
1105
1106 if (VerifyDiagnostics) {
1107 fprintf(stderr, "-verify does not yet work with serialized ASTs.\n");
1108 exit (1);
1109 }
1110
1111 llvm::sys::Path Filename(InFile);
1112
1113 if (!Filename.isValid()) {
1114 fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str());
1115 exit (1);
1116 }
1117
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001118 llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr));
Ted Kremenekfe4e0152007-12-13 18:11:11 +00001119
1120 if (!TU) {
1121 fprintf(stderr, "error: file '%s' could not be deserialized\n",
1122 InFile.c_str());
1123 exit (1);
1124 }
1125
Ted Kremenek63ea8632007-12-19 19:27:38 +00001126 // Observe that we use the source file name stored in the deserialized
1127 // translation unit, rather than InFile.
Ted Kremenekee533642007-12-20 19:47:16 +00001128 llvm::OwningPtr<ASTConsumer>
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001129 Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(),
Ted Kremenek815c78f2008-08-05 18:50:11 +00001130 0, 0));
Nico Weber7bfaaae2008-08-10 19:59:06 +00001131
Ted Kremenek20e97482007-12-12 23:41:08 +00001132 if (!Consumer) {
1133 fprintf(stderr, "Unsupported program action with serialized ASTs!\n");
1134 exit (1);
1135 }
Nico Weber7bfaaae2008-08-10 19:59:06 +00001136
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001137 Consumer->Initialize(TU->getContext());
Nico Weber7bfaaae2008-08-10 19:59:06 +00001138
Chris Lattnere66b65c2008-02-06 01:42:25 +00001139 // FIXME: We need to inform Consumer about completed TagDecls as well.
Ted Kremenek20e97482007-12-12 23:41:08 +00001140 for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
1141 Consumer->HandleTopLevelDecl(*I);
Ted Kremenek20e97482007-12-12 23:41:08 +00001142}
1143
1144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145static llvm::cl::list<std::string>
1146InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1147
Ted Kremenek20e97482007-12-12 23:41:08 +00001148static bool isSerializedFile(const std::string& InFile) {
1149 if (InFile.size() < 4)
1150 return false;
1151
1152 const char* s = InFile.c_str()+InFile.size()-4;
1153
1154 return s[0] == '.' &&
1155 s[1] == 'a' &&
1156 s[2] == 's' &&
1157 s[3] == 't';
1158}
1159
Reid Spencer5f016e22007-07-11 17:01:13 +00001160
1161int main(int argc, char **argv) {
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001162 llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n");
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 llvm::sys::PrintStackTraceOnErrorSignal();
1164
1165 // If no input was specified, read from stdin.
1166 if (InputFilenames.empty())
1167 InputFilenames.push_back("-");
Ted Kremenek31e703b2007-12-11 23:28:38 +00001168
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 // Create a file manager object to provide access to and cache the filesystem.
1170 FileManager FileMgr;
1171
Ted Kremenek31e703b2007-12-11 23:28:38 +00001172 // Create the diagnostic client for reporting errors or for
1173 // implementing -verify.
Nico Weber7bfaaae2008-08-10 19:59:06 +00001174 DiagnosticClient* TextDiagClient = 0;
Ted Kremenek88f5cde2008-03-27 06:17:42 +00001175
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001176 if (!VerifyDiagnostics) {
1177 // Print diagnostics to stderr by default.
1178 TextDiagClient = new TextDiagnosticPrinter(!NoShowColumn,
1179 !NoCaretDiagnostics);
1180 } else {
1181 // When checking diagnostics, just buffer them up.
1182 TextDiagClient = new TextDiagnosticBuffer();
1183
1184 if (InputFilenames.size() != 1) {
1185 fprintf(stderr,
1186 "-verify only works on single input files for now.\n");
1187 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 }
1189 }
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001190
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 // Configure our handling of diagnostics.
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001192 llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient);
1193 Diagnostic Diags(DiagClient.get());
Ted Kremenek31e703b2007-12-11 23:28:38 +00001194 InitializeDiagnostics(Diags);
1195
Chris Lattner4f037832007-12-05 23:24:17 +00001196 // -I- is a deprecated GCC feature, scan for it and reject it.
1197 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
1198 if (I_dirs[i] == "-") {
Ted Kremenek2eefd862007-12-11 22:57:35 +00001199 Diags.Report(diag::err_pp_I_dash_not_supported);
Chris Lattner4f037832007-12-05 23:24:17 +00001200 I_dirs.erase(I_dirs.begin()+i);
1201 --i;
1202 }
1203 }
Chris Lattner11215192008-03-14 06:12:05 +00001204
1205 // Get information about the target being compiled for.
1206 std::string Triple = CreateTargetTriple();
Ted Kremenek7a08e282008-08-07 18:13:12 +00001207 llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple));
1208
Chris Lattner11215192008-03-14 06:12:05 +00001209 if (Target == 0) {
1210 fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
1211 Triple.c_str());
1212 fprintf(stderr, "Please use -triple or -arch.\n");
1213 exit(1);
1214 }
Chris Lattner4f037832007-12-05 23:24:17 +00001215
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001216 // Are we invoking one or more source analyses?
1217 if (!AnalysisList.empty() && ProgAction == ParseSyntaxOnly)
1218 ProgAction = RunAnalysis;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001219
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001220 llvm::OwningPtr<SourceManager> SourceMgr;
1221
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenek31e703b2007-12-11 23:28:38 +00001223 const std::string &InFile = InputFilenames[i];
Ted Kremenek31e703b2007-12-11 23:28:38 +00001224
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001225 if (isSerializedFile(InFile)) {
1226 Diags.setClient(TextDiagClient);
Ted Kremenek20e97482007-12-12 23:41:08 +00001227 ProcessSerializedFile(InFile,Diags,FileMgr);
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001228 }
Ted Kremenek20e97482007-12-12 23:41:08 +00001229 else {
1230 /// Create a SourceManager object. This tracks and owns all the file
1231 /// buffers allocated to a translation unit.
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001232 if (!SourceMgr)
1233 SourceMgr.reset(new SourceManager());
1234 else
1235 SourceMgr->clearIDTables();
Ted Kremenek31e703b2007-12-11 23:28:38 +00001236
Ted Kremenek20e97482007-12-12 23:41:08 +00001237 // Initialize language options, inferring file types from input filenames.
1238 LangOptions LangInfo;
1239 InitializeBaseLanguage();
1240 LangKind LK = GetLanguage(InFile);
1241 InitializeLangOptions(LangInfo, LK);
Daniel Dunbardcb4a1a2008-08-23 08:43:39 +00001242 InitializeLanguageStandard(LangInfo, LK, Target.get());
Ted Kremenek01d9dbf2008-04-29 04:37:03 +00001243 InitializeGCMode(LangInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001244
1245 // Process the -I options and set them in the HeaderInfo.
1246 HeaderSearch HeaderInfo(FileMgr);
Ted Kremenekc68ecb52008-06-06 01:47:30 +00001247
Chris Lattnerdcaa0962008-03-03 03:16:03 +00001248 InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001249
Ted Kremenek20e97482007-12-12 23:41:08 +00001250 // Set up the preprocessor with these options.
Ted Kremenek339b9c22008-04-17 22:31:54 +00001251 DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target,
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001252 *SourceMgr.get(), HeaderInfo);
Ted Kremenek20e97482007-12-12 23:41:08 +00001253
Ted Kremeneka42cf2e2008-04-17 21:38:34 +00001254 llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor());
1255
Ted Kremenek339b9c22008-04-17 22:31:54 +00001256 if (!PP)
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001257 continue;
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001258
1259 // Create the HTMLDiagnosticsClient if we are using one. Otherwise,
1260 // always reset to using TextDiagClient.
1261 llvm::OwningPtr<DiagnosticClient> TmpClient;
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001262
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001263 if (!HTMLDiag.empty()) {
1264 TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(),
1265 &PPFactory));
1266 Diags.setClient(TmpClient.get());
1267 }
1268 else
1269 Diags.setClient(TextDiagClient);
1270
1271 // Process the source file.
Ted Kremenek339b9c22008-04-17 22:31:54 +00001272 ProcessInputFile(*PP, PPFactory, InFile);
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001273 HeaderInfo.ClearFileInfo();
Ted Kremenek20e97482007-12-12 23:41:08 +00001274
1275 if (Stats)
Ted Kremenekc0c03bc2008-06-06 22:42:39 +00001276 SourceMgr->PrintStats();
Ted Kremenek20e97482007-12-12 23:41:08 +00001277 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 }
Chris Lattner11215192008-03-14 06:12:05 +00001279
Ted Kremenek7a08e282008-08-07 18:13:12 +00001280 if (unsigned NumDiagnostics = Diags.getNumDiagnostics())
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1282 (NumDiagnostics == 1 ? "" : "s"));
1283
1284 if (Stats) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 FileMgr.PrintStats();
1286 fprintf(stderr, "\n");
1287 }
1288
Chris Lattner96f1a642007-07-21 05:40:53 +00001289 return Diags.getNumErrors() != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001290}