blob: af8b5b62437b910d891946d4337157dbd7a84bdd [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- clang.cpp - C-Language Front-end ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This utility may be invoked in the following manner:
11// clang --help - Output help info.
12// clang [options] - Read from stdin.
13// clang [options] file - Read from "file".
14// clang [options] file1 file2 - Read these files.
15//
16//===----------------------------------------------------------------------===//
17//
18// TODO: Options to support:
19//
Chris Lattner39fb14e2009-02-18 01:17:01 +000020// -Wfatal-errors
Chris Lattner4b009652007-07-25 00:24:17 +000021// -ftabstop=width
22//
23//===----------------------------------------------------------------------===//
24
Ted Kremenek377a3192009-03-31 18:58:14 +000025#include "clang-cc.h"
Chris Lattnereb8c9632007-10-07 06:04:32 +000026#include "ASTConsumers.h"
Daniel Dunbar68952de2009-03-02 06:16:29 +000027#include "clang/Frontend/CompileOptions.h"
Douglas Gregor133d2552009-04-02 01:08:08 +000028#include "clang/Frontend/FixItRewriter.h"
Daniel Dunbarf45afe62009-03-12 10:14:16 +000029#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar68952de2009-03-02 06:16:29 +000030#include "clang/Frontend/InitHeaderSearch.h"
Chris Lattner94269d72009-04-21 05:40:52 +000031#include "clang/Frontend/InitPreprocessor.h"
Daniel Dunbarf45afe62009-03-12 10:14:16 +000032#include "clang/Frontend/PathDiagnosticClients.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000033#include "clang/Frontend/PCHReader.h"
Daniel Dunbar68952de2009-03-02 06:16:29 +000034#include "clang/Frontend/TextDiagnosticBuffer.h"
35#include "clang/Frontend/TextDiagnosticPrinter.h"
Ted Kremenekfd75e312008-03-27 06:17:42 +000036#include "clang/Analysis/PathDiagnostic.h"
Chris Lattnerf5e9db02008-02-06 02:01:47 +000037#include "clang/CodeGen/ModuleBuilder.h"
Chris Lattner0bed6ec2008-02-06 00:23:21 +000038#include "clang/Sema/ParseAST.h"
Chris Lattner86a24842009-01-29 06:55:46 +000039#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1cc01712007-09-15 22:56:56 +000040#include "clang/AST/ASTConsumer.h"
Chris Lattnerfc318192009-03-28 04:31:31 +000041#include "clang/AST/ASTContext.h"
42#include "clang/AST/Decl.h"
Chris Lattnera17991f2009-03-29 16:50:03 +000043#include "clang/AST/DeclGroup.h"
Chris Lattner4b009652007-07-25 00:24:17 +000044#include "clang/Parse/Parser.h"
45#include "clang/Lex/HeaderSearch.h"
Chris Lattner71af6d62009-02-06 04:16:41 +000046#include "clang/Lex/LexDiagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000047#include "clang/Basic/FileManager.h"
48#include "clang/Basic/SourceManager.h"
49#include "clang/Basic/TargetInfo.h"
Chris Lattner01de9c82008-09-30 20:16:56 +000050#include "llvm/ADT/OwningPtr.h"
Chris Lattnerac139d22007-12-15 23:20:07 +000051#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner01de9c82008-09-30 20:16:56 +000052#include "llvm/ADT/StringExtras.h"
Chris Lattner0e004a12009-04-08 18:24:34 +000053#include "llvm/ADT/STLExtras.h"
Chris Lattner01de9c82008-09-30 20:16:56 +000054#include "llvm/Config/config.h"
Chris Lattner4b009652007-07-25 00:24:17 +000055#include "llvm/Support/CommandLine.h"
Daniel Dunbarbb298c02008-10-28 00:38:08 +000056#include "llvm/Support/ManagedStatic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000057#include "llvm/Support/MemoryBuffer.h"
Zhongxing Xuf4ec4d92008-11-26 05:23:17 +000058#include "llvm/Support/PluginLoader.h"
Chris Lattner2b2d0c42009-03-04 21:41:39 +000059#include "llvm/Support/PrettyStackTrace.h"
Chris Lattnerefe33382009-02-18 01:51:21 +000060#include "llvm/Support/Timer.h"
Daniel Dunbarabe2e542008-10-02 01:21:33 +000061#include "llvm/System/Host.h"
Chris Lattner3ee4a2f2008-03-03 03:16:03 +000062#include "llvm/System/Path.h"
Chris Lattner01de9c82008-09-30 20:16:56 +000063#include "llvm/System/Signals.h"
Douglas Gregor24b48b02009-04-02 19:05:20 +000064#include <cstdlib>
65
Chris Lattner4b009652007-07-25 00:24:17 +000066using namespace clang;
67
68//===----------------------------------------------------------------------===//
Douglas Gregor24b48b02009-04-02 19:05:20 +000069// Source Location Parser
70//===----------------------------------------------------------------------===//
71
72/// \brief A source location that has been parsed on the command line.
73struct ParsedSourceLocation {
74 std::string FileName;
75 unsigned Line;
76 unsigned Column;
77
78 /// \brief Try to resolve the file name of a parsed source location.
79 ///
80 /// \returns true if there was an error, false otherwise.
81 bool ResolveLocation(FileManager &FileMgr, RequestedSourceLocation &Result);
82};
83
84bool
85ParsedSourceLocation::ResolveLocation(FileManager &FileMgr,
86 RequestedSourceLocation &Result) {
87 const FileEntry *File = FileMgr.getFile(FileName);
88 if (!File)
89 return true;
90
91 Result.File = File;
92 Result.Line = Line;
93 Result.Column = Column;
94 return false;
95}
96
97namespace llvm {
98 namespace cl {
99 /// \brief Command-line option parser that parses source locations.
100 ///
101 /// Source locations are of the form filename:line:column.
102 template<>
103 class parser<ParsedSourceLocation>
104 : public basic_parser<ParsedSourceLocation> {
105 public:
106 bool parse(Option &O, const char *ArgName,
107 const std::string &ArgValue,
108 ParsedSourceLocation &Val);
109 };
110
111 bool
112 parser<ParsedSourceLocation>::
113 parse(Option &O, const char *ArgName, const std::string &ArgValue,
114 ParsedSourceLocation &Val) {
115 using namespace clang;
116
117 const char *ExpectedFormat
118 = "source location must be of the form filename:line:column";
119 std::string::size_type SecondColon = ArgValue.rfind(':');
120 if (SecondColon == std::string::npos) {
121 std::fprintf(stderr, "%s\n", ExpectedFormat);
122 return true;
123 }
124 char *EndPtr;
125 long Column
126 = std::strtol(ArgValue.c_str() + SecondColon + 1, &EndPtr, 10);
127 if (EndPtr != ArgValue.c_str() + ArgValue.size()) {
128 std::fprintf(stderr, "%s\n", ExpectedFormat);
129 return true;
130 }
131
132 std::string::size_type FirstColon = ArgValue.rfind(':', SecondColon-1);
133 if (SecondColon == std::string::npos) {
134 std::fprintf(stderr, "%s\n", ExpectedFormat);
135 return true;
136 }
137 long Line = std::strtol(ArgValue.c_str() + FirstColon + 1, &EndPtr, 10);
138 if (EndPtr != ArgValue.c_str() + SecondColon) {
139 std::fprintf(stderr, "%s\n", ExpectedFormat);
140 return true;
141 }
142
143 Val.FileName = ArgValue.substr(0, FirstColon);
144 Val.Line = Line;
145 Val.Column = Column;
146 return false;
147 }
148 }
149}
150
151//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000152// Global options.
153//===----------------------------------------------------------------------===//
154
Chris Lattnerefe33382009-02-18 01:51:21 +0000155/// ClangFrontendTimer - The front-end activities should charge time to it with
156/// TimeRegion. The -ftime-report option controls whether this will do
157/// anything.
158llvm::Timer *ClangFrontendTimer = 0;
159
Daniel Dunbar4efedde2008-10-16 16:54:18 +0000160static bool HadErrors = false;
Daniel Dunbar70a66b12008-10-04 23:42:49 +0000161
Chris Lattner4b009652007-07-25 00:24:17 +0000162static llvm::cl::opt<bool>
163Verbose("v", llvm::cl::desc("Enable verbose output"));
164static llvm::cl::opt<bool>
Nate Begeman6acbedd2007-12-30 01:38:50 +0000165Stats("print-stats",
166 llvm::cl::desc("Print performance metrics and statistics"));
Daniel Dunbar4efedde2008-10-16 16:54:18 +0000167static llvm::cl::opt<bool>
168DisableFree("disable-free",
169 llvm::cl::desc("Disable freeing of memory on exit"),
170 llvm::cl::init(false));
Chris Lattner4b009652007-07-25 00:24:17 +0000171
172enum ProgActions {
Steve Naroff44e81222008-04-14 22:03:09 +0000173 RewriteObjC, // ObjC->C Rewriter.
Steve Naroff93c18352008-09-18 14:10:13 +0000174 RewriteBlocks, // ObjC->C Rewriter for Blocks.
Chris Lattner1665a9f2008-05-08 06:52:13 +0000175 RewriteMacros, // Expand macros but not #includes.
Chris Lattnerc3fbf392008-10-12 05:29:20 +0000176 RewriteTest, // Rewriter playground
Douglas Gregor133d2552009-04-02 01:08:08 +0000177 FixIt, // Fix-It Rewriter
Ted Kremeneke1a79d82008-03-19 07:53:42 +0000178 HTMLTest, // HTML displayer testing stuff.
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000179 EmitAssembly, // Emit a .s file.
Chris Lattner4b009652007-07-25 00:24:17 +0000180 EmitLLVM, // Emit a .ll file.
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000181 EmitBC, // Emit a .bc file.
Daniel Dunbard999a8e2009-02-26 22:39:37 +0000182 EmitLLVMOnly, // Generate LLVM IR, but do not
Ted Kremenek24612ae2008-03-18 21:19:49 +0000183 EmitHTML, // Translate input source into HTML.
Chris Lattner4045a8a2007-10-11 00:18:28 +0000184 ASTPrint, // Parse ASTs and print them.
185 ASTDump, // Parse ASTs and dump them.
186 ASTView, // Parse ASTs and view them in Graphviz.
Zhongxing Xu6036bbe2009-01-13 01:29:24 +0000187 PrintDeclContext, // Print DeclContext and their Decls.
Chris Lattner4b009652007-07-25 00:24:17 +0000188 ParsePrintCallbacks, // Parse and print each callback.
189 ParseSyntaxOnly, // Parse and perform semantic analysis.
190 ParseNoop, // Parse with noop callbacks.
191 RunPreprocessorOnly, // Just lex, no output.
192 PrintPreprocessedInput, // -E mode.
Chris Lattneraf669fb2008-10-12 05:03:36 +0000193 DumpTokens, // Dump out preprocessed tokens.
194 DumpRawTokens, // Dump out raw tokens.
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000195 RunAnalysis, // Run one or more source code analyses.
Douglas Gregor2a0e8742009-04-02 23:43:50 +0000196 GeneratePTH, // Generate pre-tokenized header.
Douglas Gregorc34897d2009-04-09 22:27:44 +0000197 GeneratePCH, // Generate pre-compiled header.
Ted Kremenekd9ceb3d2008-10-23 23:36:29 +0000198 InheritanceView // View C++ inheritance for a specified class.
Chris Lattner4b009652007-07-25 00:24:17 +0000199};
200
201static llvm::cl::opt<ProgActions>
202ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
203 llvm::cl::init(ParseSyntaxOnly),
204 llvm::cl::values(
205 clEnumValN(RunPreprocessorOnly, "Eonly",
206 "Just run preprocessor, no output (for timings)"),
207 clEnumValN(PrintPreprocessedInput, "E",
208 "Run preprocessor, emit preprocessed file"),
Chris Lattneraf669fb2008-10-12 05:03:36 +0000209 clEnumValN(DumpRawTokens, "dump-raw-tokens",
210 "Lex file in raw mode and dump raw tokens"),
Daniel Dunbar9c321102009-01-20 23:17:32 +0000211 clEnumValN(RunAnalysis, "analyze",
212 "Run static analysis engine"),
Chris Lattneraf669fb2008-10-12 05:03:36 +0000213 clEnumValN(DumpTokens, "dump-tokens",
Chris Lattner4b009652007-07-25 00:24:17 +0000214 "Run preprocessor, dump internal rep of tokens"),
215 clEnumValN(ParseNoop, "parse-noop",
216 "Run parser with noop callbacks (for timings)"),
217 clEnumValN(ParseSyntaxOnly, "fsyntax-only",
218 "Run parser and perform semantic analysis"),
219 clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
220 "Run parser and print each callback invoked"),
Ted Kremenek24612ae2008-03-18 21:19:49 +0000221 clEnumValN(EmitHTML, "emit-html",
222 "Output input source as HTML"),
Chris Lattner4045a8a2007-10-11 00:18:28 +0000223 clEnumValN(ASTPrint, "ast-print",
224 "Build ASTs and then pretty-print them"),
225 clEnumValN(ASTDump, "ast-dump",
226 "Build ASTs and then debug dump them"),
Chris Lattner664dd082007-10-11 00:37:43 +0000227 clEnumValN(ASTView, "ast-view",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000228 "Build ASTs and view them with GraphViz"),
Zhongxing Xu6036bbe2009-01-13 01:29:24 +0000229 clEnumValN(PrintDeclContext, "print-decl-contexts",
Ted Kremenek4787dcf2009-04-01 00:23:28 +0000230 "Print DeclContexts and their Decls"),
Douglas Gregor2a0e8742009-04-02 23:43:50 +0000231 clEnumValN(GeneratePTH, "emit-pth",
Ted Kremenek4787dcf2009-04-01 00:23:28 +0000232 "Generate pre-tokenized header file"),
Douglas Gregorc34897d2009-04-09 22:27:44 +0000233 clEnumValN(GeneratePCH, "emit-pch",
234 "Generate pre-compiled header file"),
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000235 clEnumValN(EmitAssembly, "S",
236 "Emit native assembly code"),
Chris Lattner4b009652007-07-25 00:24:17 +0000237 clEnumValN(EmitLLVM, "emit-llvm",
Ted Kremenek05334682007-09-06 21:26:58 +0000238 "Build ASTs then convert to LLVM, emit .ll file"),
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +0000239 clEnumValN(EmitBC, "emit-llvm-bc",
240 "Build ASTs then convert to LLVM, emit .bc file"),
Daniel Dunbard999a8e2009-02-26 22:39:37 +0000241 clEnumValN(EmitLLVMOnly, "emit-llvm-only",
242 "Build ASTs and convert to LLVM, discarding output"),
Chris Lattnerc3fbf392008-10-12 05:29:20 +0000243 clEnumValN(RewriteTest, "rewrite-test",
244 "Rewriter playground"),
Steve Naroff44e81222008-04-14 22:03:09 +0000245 clEnumValN(RewriteObjC, "rewrite-objc",
Chris Lattner1665a9f2008-05-08 06:52:13 +0000246 "Rewrite ObjC into C (code rewriter example)"),
247 clEnumValN(RewriteMacros, "rewrite-macros",
248 "Expand macros without full preprocessing"),
Steve Naroff93c18352008-09-18 14:10:13 +0000249 clEnumValN(RewriteBlocks, "rewrite-blocks",
250 "Rewrite Blocks to C"),
Douglas Gregor133d2552009-04-02 01:08:08 +0000251 clEnumValN(FixIt, "fixit",
252 "Apply fix-it advice to the input source"),
Chris Lattner4b009652007-07-25 00:24:17 +0000253 clEnumValEnd));
254
Ted Kremenekd01eae62007-12-19 19:47:59 +0000255
256static llvm::cl::opt<std::string>
257OutputFile("o",
Ted Kremenek09b3f0d2007-12-19 19:50:41 +0000258 llvm::cl::value_desc("path"),
Douglas Gregor608ff622009-04-22 21:45:53 +0000259 llvm::cl::desc("Specify output file"));
Ted Kremenek517cb512008-04-14 18:40:58 +0000260
Ted Kremenek57f25b22008-12-02 19:57:31 +0000261
262//===----------------------------------------------------------------------===//
263// PTH.
264//===----------------------------------------------------------------------===//
265
266static llvm::cl::opt<std::string>
267TokenCache("token-cache", llvm::cl::value_desc("path"),
268 llvm::cl::desc("Use specified token cache file"));
269
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000270//===----------------------------------------------------------------------===//
Ted Kremenek517cb512008-04-14 18:40:58 +0000271// Diagnostic Options
272//===----------------------------------------------------------------------===//
273
Ted Kremenek10389cf2007-09-26 19:42:19 +0000274static llvm::cl::opt<bool>
275VerifyDiagnostics("verify",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000276 llvm::cl::desc("Verify emitted diagnostics and warnings"));
Ted Kremenek10389cf2007-09-26 19:42:19 +0000277
Ted Kremenekfd75e312008-03-27 06:17:42 +0000278static llvm::cl::opt<std::string>
279HTMLDiag("html-diags",
280 llvm::cl::desc("Generate HTML to report diagnostics"),
281 llvm::cl::value_desc("HTML directory"));
282
Nico Weber0e13eaa2008-08-05 23:33:20 +0000283static llvm::cl::opt<bool>
284NoShowColumn("fno-show-column",
285 llvm::cl::desc("Do not include column number on diagnostics"));
286
287static llvm::cl::opt<bool>
Chris Lattnerb96a04f2009-01-30 19:01:41 +0000288NoShowLocation("fno-show-source-location",
289 llvm::cl::desc("Do not include source location information with"
290 " diagnostics"));
291
292static llvm::cl::opt<bool>
Nico Weber0e13eaa2008-08-05 23:33:20 +0000293NoCaretDiagnostics("fno-caret-diagnostics",
294 llvm::cl::desc("Do not include source line and caret with"
295 " diagnostics"));
296
Chris Lattner695a4f52009-03-13 01:08:23 +0000297static llvm::cl::opt<bool>
Chris Lattner041bd732009-04-19 07:44:08 +0000298NoDiagnosticsFixIt("fno-diagnostics-fixit-info",
299 llvm::cl::desc("Do not include fixit information in"
300 " diagnostics"));
301
302static llvm::cl::opt<bool>
Chris Lattner13bac9e2009-04-21 05:34:31 +0000303PrintSourceRangeInfo("fdiagnostics-print-source-range-info",
304 llvm::cl::desc("Print source range spans in numeric form"));
305
306static llvm::cl::alias
307PrintSourceRangeInfo2("fprint-source-range-info",
308 llvm::cl::desc("Print source range spans in numeric form [deprecated]"),
309 llvm::cl::aliasopt(PrintSourceRangeInfo));
Chris Lattner695a4f52009-03-13 01:08:23 +0000310
Chris Lattnera96ec3b2009-04-16 05:44:38 +0000311static llvm::cl::opt<bool>
312PrintDiagnosticOption("fdiagnostics-show-option",
313 llvm::cl::desc("Print diagnostic name with mappable diagnostics"));
Nico Weber0e13eaa2008-08-05 23:33:20 +0000314
Chris Lattner4b009652007-07-25 00:24:17 +0000315//===----------------------------------------------------------------------===//
Ted Kremenekd9ceb3d2008-10-23 23:36:29 +0000316// C++ Visualization.
317//===----------------------------------------------------------------------===//
318
319static llvm::cl::opt<std::string>
320InheritanceViewCls("cxx-inheritance-view",
321 llvm::cl::value_desc("class name"),
Daniel Dunbar47f0b0f2009-01-14 18:56:36 +0000322 llvm::cl::desc("View C++ inheritance for a specified class"));
Ted Kremenekd9ceb3d2008-10-23 23:36:29 +0000323
324//===----------------------------------------------------------------------===//
Douglas Gregor23d23262009-02-14 20:49:29 +0000325// Builtin Options
326//===----------------------------------------------------------------------===//
Chris Lattner93d4d982009-02-18 01:12:43 +0000327
328static llvm::cl::opt<bool>
329TimeReport("ftime-report",
330 llvm::cl::desc("Print the amount of time each "
331 "phase of compilation takes"));
332
Douglas Gregor23d23262009-02-14 20:49:29 +0000333static llvm::cl::opt<bool>
334Freestanding("ffreestanding",
Daniel Dunbarfd46ea22009-02-16 22:43:43 +0000335 llvm::cl::desc("Assert that the compilation takes place in a "
Douglas Gregor23d23262009-02-14 20:49:29 +0000336 "freestanding environment"));
337
Daniel Dunbarfd46ea22009-02-16 22:43:43 +0000338static llvm::cl::opt<bool>
Daniel Dunbar9d805ce2009-04-07 21:16:11 +0000339AllowBuiltins("fbuiltin", llvm::cl::init(true),
340 llvm::cl::desc("Disable implicit builtin knowledge of functions"));
Chris Lattner911b8672009-03-13 22:38:49 +0000341
342
343static llvm::cl::opt<bool>
Daniel Dunbar9d805ce2009-04-07 21:16:11 +0000344MathErrno("fmath-errno", llvm::cl::init(true),
345 llvm::cl::desc("Require math functions to respect errno"));
Daniel Dunbarfd46ea22009-02-16 22:43:43 +0000346
Douglas Gregor23d23262009-02-14 20:49:29 +0000347//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000348// Language Options
349//===----------------------------------------------------------------------===//
350
351enum LangKind {
352 langkind_unspecified,
353 langkind_c,
354 langkind_c_cpp,
Chris Lattnera19689a2008-10-22 17:29:21 +0000355 langkind_asm_cpp,
Chris Lattner4b009652007-07-25 00:24:17 +0000356 langkind_cxx,
357 langkind_cxx_cpp,
358 langkind_objc,
359 langkind_objc_cpp,
360 langkind_objcxx,
Daniel Dunbardb6126e2009-04-01 05:09:09 +0000361 langkind_objcxx_cpp
Chris Lattner4b009652007-07-25 00:24:17 +0000362};
363
Chris Lattner4b009652007-07-25 00:24:17 +0000364static llvm::cl::opt<LangKind>
365BaseLang("x", llvm::cl::desc("Base language to compile"),
366 llvm::cl::init(langkind_unspecified),
367 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
368 clEnumValN(langkind_cxx, "c++", "C++"),
369 clEnumValN(langkind_objc, "objective-c", "Objective C"),
370 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
Daniel Dunbarb1488592009-01-29 23:50:47 +0000371 clEnumValN(langkind_c_cpp, "cpp-output",
Chris Lattner4b009652007-07-25 00:24:17 +0000372 "Preprocessed C"),
Chris Lattnera19689a2008-10-22 17:29:21 +0000373 clEnumValN(langkind_asm_cpp, "assembler-with-cpp",
374 "Preprocessed asm"),
Chris Lattner4b009652007-07-25 00:24:17 +0000375 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
Chris Lattnerb5555ea2009-02-06 06:19:20 +0000376 "Preprocessed C++"),
Chris Lattner4b009652007-07-25 00:24:17 +0000377 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
378 "Preprocessed Objective C"),
Chris Lattnerb5555ea2009-02-06 06:19:20 +0000379 clEnumValN(langkind_objcxx_cpp, "objective-c++-cpp-output",
Chris Lattner4b009652007-07-25 00:24:17 +0000380 "Preprocessed Objective C++"),
Daniel Dunbardb6126e2009-04-01 05:09:09 +0000381 clEnumValN(langkind_c, "c-header",
382 "C header"),
383 clEnumValN(langkind_objc, "objective-c-header",
384 "Objective-C header"),
385 clEnumValN(langkind_cxx, "c++-header",
386 "C++ header"),
387 clEnumValN(langkind_objcxx, "objective-c++-header",
388 "Objective-C++ header"),
Chris Lattner4b009652007-07-25 00:24:17 +0000389 clEnumValEnd));
390
391static llvm::cl::opt<bool>
392LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
393 llvm::cl::Hidden);
394static llvm::cl::opt<bool>
395LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
396 llvm::cl::Hidden);
397
Chris Lattnere1be6022009-04-14 23:22:57 +0000398static llvm::cl::opt<bool>
399ObjCExclusiveGC("fobjc-gc-only",
400 llvm::cl::desc("Use GC exclusively for Objective-C related "
401 "memory management"));
402
403static llvm::cl::opt<bool>
404ObjCEnableGC("fobjc-gc",
405 llvm::cl::desc("Enable Objective-C garbage collection"));
406
Fariborz Jahanian2ccf6172009-04-17 03:04:15 +0000407static llvm::cl::opt<bool>
408ObjCEnableGCBitmapPrint("print-ivar-layout",
409 llvm::cl::desc("Enable Objective-C Ivar layout bitmap print trace"));
410
Chris Lattnere1be6022009-04-14 23:22:57 +0000411static llvm::cl::opt<LangOptions::VisibilityMode>
412SymbolVisibility("fvisibility",
413 llvm::cl::desc("Set the default symbol visibility:"),
414 llvm::cl::init(LangOptions::Default),
415 llvm::cl::values(clEnumValN(LangOptions::Default, "default",
416 "Use default symbol visibility"),
417 clEnumValN(LangOptions::Hidden, "hidden",
418 "Use hidden symbol visibility"),
419 clEnumValN(LangOptions::Protected,"protected",
420 "Use protected symbol visibility"),
421 clEnumValEnd));
422
423static llvm::cl::opt<bool>
424OverflowChecking("ftrapv",
425 llvm::cl::desc("Trap on integer overflow"),
426 llvm::cl::init(false));
427
428
Ted Kremenek11ad8952007-12-05 23:49:08 +0000429/// InitializeBaseLanguage - Handle the -x foo options.
430static void InitializeBaseLanguage() {
431 if (LangObjC)
432 BaseLang = langkind_objc;
433 else if (LangObjCXX)
434 BaseLang = langkind_objcxx;
435}
436
437static LangKind GetLanguage(const std::string &Filename) {
438 if (BaseLang != langkind_unspecified)
439 return BaseLang;
440
441 std::string::size_type DotPos = Filename.rfind('.');
442
443 if (DotPos == std::string::npos) {
444 BaseLang = langkind_c; // Default to C if no extension.
Chris Lattner4eac0502008-01-04 19:12:28 +0000445 return langkind_c;
Chris Lattner4b009652007-07-25 00:24:17 +0000446 }
447
Ted Kremenek11ad8952007-12-05 23:49:08 +0000448 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
449 // C header: .h
450 // C++ header: .hh or .H;
451 // assembler no preprocessing: .s
452 // assembler: .S
453 if (Ext == "c")
454 return langkind_c;
Chris Lattner5dc0c1b2009-03-23 16:24:37 +0000455 else if (Ext == "S" ||
456 // If the compiler is run on a .s file, preprocess it as .S
457 Ext == "s")
Chris Lattnera19689a2008-10-22 17:29:21 +0000458 return langkind_asm_cpp;
Ted Kremenek11ad8952007-12-05 23:49:08 +0000459 else if (Ext == "i")
460 return langkind_c_cpp;
461 else if (Ext == "ii")
462 return langkind_cxx_cpp;
463 else if (Ext == "m")
464 return langkind_objc;
465 else if (Ext == "mi")
466 return langkind_objc_cpp;
467 else if (Ext == "mm" || Ext == "M")
468 return langkind_objcxx;
469 else if (Ext == "mii")
470 return langkind_objcxx_cpp;
471 else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
472 Ext == "c++" || Ext == "cp" || Ext == "cxx")
473 return langkind_cxx;
474 else
475 return langkind_c;
476}
477
478
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000479static void InitializeCOptions(LangOptions &Options) {
480 // Do nothing.
481}
482
483static void InitializeObjCOptions(LangOptions &Options) {
484 Options.ObjC1 = Options.ObjC2 = 1;
485}
486
487
Daniel Dunbardb6126e2009-04-01 05:09:09 +0000488static void InitializeLangOptions(LangOptions &Options, LangKind LK){
Chris Lattner4b009652007-07-25 00:24:17 +0000489 // FIXME: implement -fpreprocessed mode.
490 bool NoPreprocess = false;
491
Ted Kremenek11ad8952007-12-05 23:49:08 +0000492 switch (LK) {
Chris Lattner4b009652007-07-25 00:24:17 +0000493 default: assert(0 && "Unknown language kind!");
Chris Lattnera19689a2008-10-22 17:29:21 +0000494 case langkind_asm_cpp:
Daniel Dunbar20b88022008-12-01 18:55:22 +0000495 Options.AsmPreprocessor = 1;
Chris Lattnera19689a2008-10-22 17:29:21 +0000496 // FALLTHROUGH
Chris Lattner4b009652007-07-25 00:24:17 +0000497 case langkind_c_cpp:
498 NoPreprocess = true;
499 // FALLTHROUGH
500 case langkind_c:
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000501 InitializeCOptions(Options);
Chris Lattner4b009652007-07-25 00:24:17 +0000502 break;
503 case langkind_cxx_cpp:
504 NoPreprocess = true;
505 // FALLTHROUGH
506 case langkind_cxx:
507 Options.CPlusPlus = 1;
508 break;
509 case langkind_objc_cpp:
510 NoPreprocess = true;
511 // FALLTHROUGH
512 case langkind_objc:
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000513 InitializeObjCOptions(Options);
Chris Lattner4b009652007-07-25 00:24:17 +0000514 break;
515 case langkind_objcxx_cpp:
516 NoPreprocess = true;
517 // FALLTHROUGH
518 case langkind_objcxx:
519 Options.ObjC1 = Options.ObjC2 = 1;
520 Options.CPlusPlus = 1;
521 break;
522 }
Chris Lattnere1be6022009-04-14 23:22:57 +0000523
524 if (ObjCExclusiveGC)
525 Options.setGCMode(LangOptions::GCOnly);
526 else if (ObjCEnableGC)
527 Options.setGCMode(LangOptions::HybridGC);
528
Fariborz Jahanian2ccf6172009-04-17 03:04:15 +0000529 if (ObjCEnableGCBitmapPrint)
530 Options.ObjCGCBitmapPrint = 1;
531
Chris Lattnere1be6022009-04-14 23:22:57 +0000532 Options.setVisibilityMode(SymbolVisibility);
533 Options.OverflowChecking = OverflowChecking;
Chris Lattner4b009652007-07-25 00:24:17 +0000534}
535
536/// LangStds - Language standards we support.
537enum LangStds {
538 lang_unspecified,
539 lang_c89, lang_c94, lang_c99,
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000540 lang_gnu_START,
541 lang_gnu89 = lang_gnu_START, lang_gnu99,
Chris Lattner4b009652007-07-25 00:24:17 +0000542 lang_cxx98, lang_gnucxx98,
543 lang_cxx0x, lang_gnucxx0x
544};
545
546static llvm::cl::opt<LangStds>
547LangStd("std", llvm::cl::desc("Language standard to compile for"),
548 llvm::cl::init(lang_unspecified),
549 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
550 clEnumValN(lang_c89, "c90", "ISO C 1990"),
551 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
552 clEnumValN(lang_c94, "iso9899:199409",
553 "ISO C 1990 with amendment 1"),
554 clEnumValN(lang_c99, "c99", "ISO C 1999"),
Chris Lattnerddeb7402009-04-06 17:17:55 +0000555 clEnumValN(lang_c99, "c9x", "ISO C 1999"),
Chris Lattner4b009652007-07-25 00:24:17 +0000556 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
Chris Lattnerddeb7402009-04-06 17:17:55 +0000557 clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
Chris Lattner4b009652007-07-25 00:24:17 +0000558 clEnumValN(lang_gnu89, "gnu89",
Gabor Greifbe1618a2009-02-28 09:22:15 +0000559 "ISO C 1990 with GNU extensions"),
Chris Lattner4b009652007-07-25 00:24:17 +0000560 clEnumValN(lang_gnu99, "gnu99",
Gabor Greifbe1618a2009-02-28 09:22:15 +0000561 "ISO C 1999 with GNU extensions (default for C)"),
Chris Lattner4b009652007-07-25 00:24:17 +0000562 clEnumValN(lang_gnu99, "gnu9x",
563 "ISO C 1999 with GNU extensions"),
564 clEnumValN(lang_cxx98, "c++98",
565 "ISO C++ 1998 with amendments"),
566 clEnumValN(lang_gnucxx98, "gnu++98",
567 "ISO C++ 1998 with amendments and GNU "
568 "extensions (default for C++)"),
569 clEnumValN(lang_cxx0x, "c++0x",
570 "Upcoming ISO C++ 200x with amendments"),
571 clEnumValN(lang_gnucxx0x, "gnu++0x",
572 "Upcoming ISO C++ 200x with amendments and GNU "
Gabor Greif46612282009-03-11 23:07:18 +0000573 "extensions"),
Chris Lattner4b009652007-07-25 00:24:17 +0000574 clEnumValEnd));
575
576static llvm::cl::opt<bool>
577NoOperatorNames("fno-operator-names",
578 llvm::cl::desc("Do not treat C++ operator name keywords as "
579 "synonyms for operators"));
580
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000581static llvm::cl::opt<bool>
582PascalStrings("fpascal-strings",
583 llvm::cl::desc("Recognize and construct Pascal-style "
584 "string literals"));
Steve Naroff73a07032008-02-07 03:50:06 +0000585
586static llvm::cl::opt<bool>
587MSExtensions("fms-extensions",
588 llvm::cl::desc("Accept some non-standard constructs used in "
Sanjiv Gupta69683532008-05-08 08:28:14 +0000589 "Microsoft header files "));
Chris Lattnerdb6be562007-11-28 05:34:05 +0000590
591static llvm::cl::opt<bool>
592WritableStrings("fwritable-strings",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000593 llvm::cl::desc("Store string literals as writable data"));
Anders Carlssone87cd982007-11-30 04:21:22 +0000594
595static llvm::cl::opt<bool>
Anders Carlsson6cf61c92009-01-30 23:26:40 +0000596NoLaxVectorConversions("fno-lax-vector-conversions",
Anders Carlsson355ed052009-01-30 23:17:46 +0000597 llvm::cl::desc("Disallow implicit conversions between "
598 "vectors with a different number of "
599 "elements or different element types"));
Chris Lattnerd7bc88b2008-12-04 23:20:07 +0000600
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000601static llvm::cl::opt<bool>
Daniel Dunbar9d805ce2009-04-07 21:16:11 +0000602EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature"));
Mike Stump9093c742009-02-02 22:57:57 +0000603
604static llvm::cl::opt<bool>
Chris Lattner1e3eedb2009-03-13 17:38:01 +0000605EnableHeinousExtensions("fheinous-gnu-extensions",
606 llvm::cl::desc("enable GNU extensions that you really really shouldn't use"),
607 llvm::cl::ValueDisallowed, llvm::cl::Hidden);
608
609static llvm::cl::opt<bool>
Mike Stump9093c742009-02-02 22:57:57 +0000610ObjCNonFragileABI("fobjc-nonfragile-abi",
611 llvm::cl::desc("enable objective-c's nonfragile abi"));
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000612
Daniel Dunbar9bae8652009-02-04 21:19:06 +0000613static llvm::cl::opt<bool>
614EmitAllDecls("femit-all-decls",
615 llvm::cl::desc("Emit all declarations, even if unused"));
Ted Kremenek2658c4a2008-04-29 04:37:03 +0000616
Daniel Dunbar91692d92008-08-11 17:36:14 +0000617// FIXME: This (and all GCC -f options) really come in -f... and
618// -fno-... forms, and additionally support automagic behavior when
619// they are not defined. For example, -fexceptions defaults to on or
620// off depending on the language. We should support this behavior in
621// some form (perhaps just add a facility for distinguishing when an
622// has its default value from when it has been set to its default
623// value).
624static llvm::cl::opt<bool>
625Exceptions("fexceptions",
Chris Lattner77c04832009-04-19 07:00:02 +0000626 llvm::cl::desc("Enable support for exception handling"));
Daniel Dunbar91692d92008-08-11 17:36:14 +0000627
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000628static llvm::cl::opt<bool>
629GNURuntime("fgnu-runtime",
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000630 llvm::cl::desc("Generate output compatible with the standard GNU "
Chris Lattner77c04832009-04-19 07:00:02 +0000631 "Objective-C runtime"));
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000632
633static llvm::cl::opt<bool>
634NeXTRuntime("fnext-runtime",
Ted Kremenek71c6cc62008-10-21 00:54:44 +0000635 llvm::cl::desc("Generate output compatible with the NeXT "
Chris Lattner77c04832009-04-19 07:00:02 +0000636 "runtime"));
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000637
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000638
639
640static llvm::cl::opt<bool>
Chris Lattner77c04832009-04-19 07:00:02 +0000641Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences"));
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000642
Chris Lattner738d3f62009-03-02 22:11:07 +0000643static llvm::cl::list<std::string>
Chris Lattnerf6790a82009-03-03 19:56:18 +0000644TargetFeatures("mattr", llvm::cl::CommaSeparated,
645 llvm::cl::desc("Target specific attributes (-mattr=help for details)"));
646
Douglas Gregor375733c2009-03-10 00:06:19 +0000647static llvm::cl::opt<unsigned>
648TemplateDepth("ftemplate-depth", llvm::cl::init(99),
649 llvm::cl::desc("Maximum depth of recursive template "
650 "instantiation"));
Chris Lattner77c04832009-04-19 07:00:02 +0000651static llvm::cl::opt<bool>
652DollarsInIdents("fdollars-in-identifiers",
653 llvm::cl::desc("Allow '$' in identifiers"));
Chris Lattner738d3f62009-03-02 22:11:07 +0000654
Anders Carlssondfd77642009-04-06 17:37:10 +0000655
656static llvm::cl::opt<bool>
657OptSize("Os", llvm::cl::desc("Optimize for size"));
658
659static llvm::cl::opt<bool>
660NoCommon("fno-common",
661 llvm::cl::desc("Compile common globals like normal definitions"),
662 llvm::cl::ValueDisallowed);
663
Daniel Dunbardedc94c2009-04-08 05:11:16 +0000664static llvm::cl::opt<std::string>
665MainFileName("main-file-name",
666 llvm::cl::desc("Main file name to use for debug info"));
Anders Carlssondfd77642009-04-06 17:37:10 +0000667
668// It might be nice to add bounds to the CommandLine library directly.
669struct OptLevelParser : public llvm::cl::parser<unsigned> {
670 bool parse(llvm::cl::Option &O, const char *ArgName,
671 const std::string &Arg, unsigned &Val) {
672 if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
673 return true;
Anders Carlssondfd77642009-04-06 17:37:10 +0000674 if (Val > 3)
675 return O.error(": '" + Arg + "' invalid optimization level!");
676 return false;
677 }
678};
679static llvm::cl::opt<unsigned, false, OptLevelParser>
680OptLevel("O", llvm::cl::Prefix,
681 llvm::cl::desc("Optimization level"),
682 llvm::cl::init(0));
683
Daniel Dunbare079c712009-04-08 03:03:23 +0000684static llvm::cl::opt<unsigned>
Daniel Dunbarcdd3c762009-04-08 18:03:55 +0000685PICLevel("pic-level", llvm::cl::desc("Value for __PIC__"));
686
687static llvm::cl::opt<bool>
688StaticDefine("static-define", llvm::cl::desc("Should __STATIC__ be defined"));
Daniel Dunbare079c712009-04-08 03:03:23 +0000689
Daniel Dunbar34542952008-08-23 08:43:39 +0000690static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
691 TargetInfo *Target) {
Chris Lattnerddae7102008-12-04 22:54:33 +0000692 // Allow the target to set the default the langauge options as it sees fit.
693 Target->getDefaultLangOptions(Options);
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000694
Chris Lattnerf6790a82009-03-03 19:56:18 +0000695 // If there are any -mattr options, pass them to the target for validation and
696 // processing. The driver should have already consolidated all the
697 // target-feature settings and passed them to us in the -mattr list. The
698 // -mattr list is treated by the code generator as a diff against the -mcpu
699 // setting, but the driver should pass all enabled options as "+" settings.
700 // This means that the target should only look at + settings.
Chris Lattner880d38e2009-04-13 06:33:49 +0000701 if (!TargetFeatures.empty()) {
Chris Lattner738d3f62009-03-02 22:11:07 +0000702 std::string ErrorStr;
Chris Lattnerf6790a82009-03-03 19:56:18 +0000703 int Opt = Target->HandleTargetFeatures(&TargetFeatures[0],
704 TargetFeatures.size(), ErrorStr);
Chris Lattner738d3f62009-03-02 22:11:07 +0000705 if (Opt != -1) {
706 if (ErrorStr.empty())
Chris Lattnerf6790a82009-03-03 19:56:18 +0000707 fprintf(stderr, "invalid feature '%s'\n",
708 TargetFeatures[Opt].c_str());
Chris Lattner738d3f62009-03-02 22:11:07 +0000709 else
Chris Lattnerf6790a82009-03-03 19:56:18 +0000710 fprintf(stderr, "feature '%s': %s\n",
711 TargetFeatures[Opt].c_str(), ErrorStr.c_str());
Chris Lattner738d3f62009-03-02 22:11:07 +0000712 exit(1);
713 }
714 }
715
Chris Lattner4b009652007-07-25 00:24:17 +0000716 if (LangStd == lang_unspecified) {
717 // Based on the base language, pick one.
Ted Kremenek11ad8952007-12-05 23:49:08 +0000718 switch (LK) {
Ted Kremenek9bd34312009-03-19 19:02:20 +0000719 case lang_unspecified: assert(0 && "Unknown base language");
Chris Lattner4b009652007-07-25 00:24:17 +0000720 case langkind_c:
Chris Lattnera19689a2008-10-22 17:29:21 +0000721 case langkind_asm_cpp:
Chris Lattner4b009652007-07-25 00:24:17 +0000722 case langkind_c_cpp:
723 case langkind_objc:
724 case langkind_objc_cpp:
725 LangStd = lang_gnu99;
726 break;
727 case langkind_cxx:
728 case langkind_cxx_cpp:
729 case langkind_objcxx:
730 case langkind_objcxx_cpp:
731 LangStd = lang_gnucxx98;
732 break;
733 }
734 }
735
736 switch (LangStd) {
737 default: assert(0 && "Unknown language standard!");
738
739 // Fall through from newer standards to older ones. This isn't really right.
740 // FIXME: Enable specifically the right features based on the language stds.
741 case lang_gnucxx0x:
742 case lang_cxx0x:
743 Options.CPlusPlus0x = 1;
744 // FALL THROUGH
745 case lang_gnucxx98:
746 case lang_cxx98:
747 Options.CPlusPlus = 1;
748 Options.CXXOperatorNames = !NoOperatorNames;
Nate Begemanca893342007-11-15 07:30:50 +0000749 Options.Boolean = 1;
Chris Lattner4b009652007-07-25 00:24:17 +0000750 // FALL THROUGH.
751 case lang_gnu99:
752 case lang_c99:
Chris Lattner4b009652007-07-25 00:24:17 +0000753 Options.C99 = 1;
754 Options.HexFloats = 1;
755 // FALL THROUGH.
756 case lang_gnu89:
757 Options.BCPLComment = 1; // Only for C99/C++.
758 // FALL THROUGH.
759 case lang_c94:
Chris Lattner0297c762008-02-25 04:01:39 +0000760 Options.Digraphs = 1; // C94, C99, C++.
761 // FALL THROUGH.
Chris Lattner4b009652007-07-25 00:24:17 +0000762 case lang_c89:
763 break;
764 }
Argiris Kirtzidisb1519552008-09-11 04:21:06 +0000765
Chris Lattner31e7f6f2009-03-20 15:44:26 +0000766 // GNUMode - Set if we're in gnu99, gnu89, gnucxx98, etc.
767 Options.GNUMode = LangStd >= lang_gnu_START;
768
Argiris Kirtzidisb1519552008-09-11 04:21:06 +0000769 if (Options.CPlusPlus) {
770 Options.C99 = 0;
Chris Lattner31e7f6f2009-03-20 15:44:26 +0000771 Options.HexFloats = Options.GNUMode;
Argiris Kirtzidisb1519552008-09-11 04:21:06 +0000772 }
Chris Lattner4b009652007-07-25 00:24:17 +0000773
Chris Lattner6ab935b2008-04-05 06:32:51 +0000774 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
775 Options.ImplicitInt = 1;
776 else
777 Options.ImplicitInt = 0;
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000778
Daniel Dunbarfacf3512009-04-07 22:13:21 +0000779 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
780 // is specified, or -std is set to a conforming mode.
Chris Lattner31e7f6f2009-03-20 15:44:26 +0000781 Options.Trigraphs = !Options.GNUMode;
Chris Lattner58d5ba52008-12-05 00:10:44 +0000782 if (Trigraphs.getPosition())
Chris Lattner31e7f6f2009-03-20 15:44:26 +0000783 Options.Trigraphs = Trigraphs; // Command line option wins if specified.
Ted Kremenek88bec0f2008-09-03 21:22:16 +0000784
Chris Lattner58d5ba52008-12-05 00:10:44 +0000785 // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off
786 // even if they are normally on for the target. In GNU modes (e.g.
787 // -std=gnu99) the default for blocks depends on the target settings.
Anders Carlsson8ffcf732009-01-21 18:47:36 +0000788 // However, blocks are not turned off when compiling Obj-C or Obj-C++ code.
Chris Lattner31e7f6f2009-03-20 15:44:26 +0000789 if (!Options.ObjC1 && !Options.GNUMode)
Chris Lattner58d5ba52008-12-05 00:10:44 +0000790 Options.Blocks = 0;
791
Chris Lattner284784c2009-04-19 07:06:52 +0000792 // Default to not accepting '$' in identifiers when preprocessing assembler,
793 // but do accept when preprocessing C. FIXME: these defaults are right for
794 // darwin, are they right everywhere?
795 Options.DollarIdents = LK != langkind_asm_cpp;
796 if (DollarsInIdents.getPosition()) // Explicit setting overrides default.
Chris Lattner77c04832009-04-19 07:00:02 +0000797 Options.DollarIdents = DollarsInIdents;
798
Chris Lattnerd7bc88b2008-12-04 23:20:07 +0000799 if (PascalStrings.getPosition())
800 Options.PascalStrings = PascalStrings;
Steve Naroff73a07032008-02-07 03:50:06 +0000801 Options.Microsoft = MSExtensions;
Chris Lattnerdb6be562007-11-28 05:34:05 +0000802 Options.WritableStrings = WritableStrings;
Anders Carlsson355ed052009-01-30 23:17:46 +0000803 if (NoLaxVectorConversions.getPosition())
804 Options.LaxVectorConversions = 0;
Daniel Dunbar91692d92008-08-11 17:36:14 +0000805 Options.Exceptions = Exceptions;
Mike Stump9093c742009-02-02 22:57:57 +0000806 if (EnableBlocks.getPosition())
Chris Lattnerd7bc88b2008-12-04 23:20:07 +0000807 Options.Blocks = EnableBlocks;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000808
Daniel Dunbar73e8e032009-03-20 23:49:28 +0000809 if (!AllowBuiltins)
Chris Lattner911b8672009-03-13 22:38:49 +0000810 Options.NoBuiltin = 1;
Douglas Gregor23d23262009-02-14 20:49:29 +0000811 if (Freestanding)
Chris Lattner911b8672009-03-13 22:38:49 +0000812 Options.Freestanding = Options.NoBuiltin = 1;
813
Chris Lattner1e3eedb2009-03-13 17:38:01 +0000814 if (EnableHeinousExtensions)
815 Options.HeinousExtensions = 1;
Douglas Gregor23d23262009-02-14 20:49:29 +0000816
Daniel Dunbarfd46ea22009-02-16 22:43:43 +0000817 Options.MathErrno = MathErrno;
818
Douglas Gregor375733c2009-03-10 00:06:19 +0000819 Options.InstantiationDepth = TemplateDepth;
820
Chris Lattnerddae7102008-12-04 22:54:33 +0000821 // Override the default runtime if the user requested it.
822 if (NeXTRuntime)
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000823 Options.NeXTRuntime = 1;
Chris Lattnerddae7102008-12-04 22:54:33 +0000824 else if (GNURuntime)
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000825 Options.NeXTRuntime = 0;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000826
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000827 if (ObjCNonFragileABI)
828 Options.ObjCNonFragileABI = 1;
Daniel Dunbar9bae8652009-02-04 21:19:06 +0000829
830 if (EmitAllDecls)
831 Options.EmitAllDecls = 1;
Anders Carlssondfd77642009-04-06 17:37:10 +0000832
Daniel Dunbarcdd3c762009-04-08 18:03:55 +0000833 // The __OPTIMIZE_SIZE__ define is tied to -Oz, which we don't
834 // support.
835 Options.OptimizeSize = 0;
Anders Carlssondfd77642009-04-06 17:37:10 +0000836
837 // -Os implies -O2
Daniel Dunbarcdd3c762009-04-08 18:03:55 +0000838 if (OptSize || OptLevel)
Anders Carlssondfd77642009-04-06 17:37:10 +0000839 Options.Optimize = 1;
Daniel Dunbare079c712009-04-08 03:03:23 +0000840
841 assert(PICLevel <= 2 && "Invalid value for -pic-level");
842 Options.PICLevel = PICLevel;
Daniel Dunbardedc94c2009-04-08 05:11:16 +0000843
Daniel Dunbarcdd3c762009-04-08 18:03:55 +0000844 Options.GNUInline = !Options.C99;
845 // FIXME: This is affected by other options (-fno-inline).
846 Options.NoInline = !OptSize && !OptLevel;
847
848 Options.Static = StaticDefine;
849
Daniel Dunbardedc94c2009-04-08 05:11:16 +0000850 if (MainFileName.getPosition())
851 Options.setMainFileName(MainFileName.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +0000852}
853
854//===----------------------------------------------------------------------===//
Ted Kremenek40499482007-12-03 22:06:55 +0000855// Target Triple Processing.
856//===----------------------------------------------------------------------===//
857
858static llvm::cl::opt<std::string>
859TargetTriple("triple",
Sanjiv Gupta69683532008-05-08 08:28:14 +0000860 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
Ted Kremenek40499482007-12-03 22:06:55 +0000861
Chris Lattnerfc457002008-03-05 01:18:20 +0000862static llvm::cl::opt<std::string>
Sanjiv Gupta69683532008-05-08 08:28:14 +0000863Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)"));
Ted Kremenek40499482007-12-03 22:06:55 +0000864
Chris Lattner2b168e02008-09-30 01:13:12 +0000865static llvm::cl::opt<std::string>
866MacOSVersionMin("mmacosx-version-min",
Daniel Dunbar93f64532009-04-10 19:52:24 +0000867 llvm::cl::desc("Specify target Mac OS X version (e.g. 10.5)"));
Chris Lattner2b168e02008-09-30 01:13:12 +0000868
Chris Lattner01de9c82008-09-30 20:16:56 +0000869// If -mmacosx-version-min=10.3.9 is specified, change the triple from being
870// something like powerpc-apple-darwin9 to powerpc-apple-darwin7
Daniel Dunbar4d36d982009-03-31 20:10:05 +0000871
872// FIXME: We should have the driver do this instead.
Chris Lattner01de9c82008-09-30 20:16:56 +0000873static void HandleMacOSVersionMin(std::string &Triple) {
874 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
875 if (DarwinDashIdx == std::string::npos) {
876 fprintf(stderr,
Daniel Dunbar93f64532009-04-10 19:52:24 +0000877 "-mmacosx-version-min only valid for darwin (Mac OS X) targets\n");
Chris Lattner01de9c82008-09-30 20:16:56 +0000878 exit(1);
879 }
880 unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin");
881
Chris Lattner01de9c82008-09-30 20:16:56 +0000882 // Remove the number.
883 Triple.resize(DarwinNumIdx);
884
885 // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9]
886 bool MacOSVersionMinIsInvalid = false;
887 int VersionNum = 0;
888 if (MacOSVersionMin.size() < 4 ||
889 MacOSVersionMin.substr(0, 3) != "10." ||
890 !isdigit(MacOSVersionMin[3])) {
891 MacOSVersionMinIsInvalid = true;
892 } else {
893 const char *Start = MacOSVersionMin.c_str()+3;
894 char *End = 0;
895 VersionNum = (int)strtol(Start, &End, 10);
896
Chris Lattnerd376f6d2008-09-30 20:30:12 +0000897 // The version number must be in the range 0-9.
898 MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9;
899
Chris Lattner01de9c82008-09-30 20:16:56 +0000900 // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7.
901 Triple += llvm::itostr(VersionNum+4);
902
Chris Lattnerd376f6d2008-09-30 20:30:12 +0000903 if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok.
904 // Add the period piece (.7) to the end of the triple. This gives us
905 // something like ...-darwin8.7
Chris Lattner01de9c82008-09-30 20:16:56 +0000906 Triple += End;
Chris Lattner01de9c82008-09-30 20:16:56 +0000907 } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not.
908 MacOSVersionMinIsInvalid = true;
909 }
910 }
911
912 if (MacOSVersionMinIsInvalid) {
913 fprintf(stderr,
Daniel Dunbar7fe2af62009-03-31 17:35:15 +0000914 "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n",
Chris Lattner01de9c82008-09-30 20:16:56 +0000915 MacOSVersionMin.c_str());
916 exit(1);
917 }
Fariborz Jahanian27c08a52009-04-10 20:33:45 +0000918 else if (VersionNum <= 4 &&
919 !strncmp(Triple.c_str(), "x86_64", strlen("x86_64"))) {
920 fprintf(stderr,
921 "-mmacosx-version-min=%s is invalid with -arch x86_64.\n",
922 MacOSVersionMin.c_str());
923 exit(1);
924 }
925
Chris Lattner01de9c82008-09-30 20:16:56 +0000926}
927
Daniel Dunbar93f64532009-04-10 19:52:24 +0000928static llvm::cl::opt<std::string>
929IPhoneOSVersionMin("miphoneos-version-min",
930 llvm::cl::desc("Specify target iPhone OS version (e.g. 2.0)"));
931
932// If -miphoneos-version-min=2.2 is specified, change the triple from being
933// something like armv6-apple-darwin10 to armv6-apple-darwin9.2.2. We use
934// 9 as the default major Darwin number, and encode the iPhone OS version
935// number in the minor version and revision.
936
937// FIXME: We should have the driver do this instead.
938static void HandleIPhoneOSVersionMin(std::string &Triple) {
939 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
940 if (DarwinDashIdx == std::string::npos) {
941 fprintf(stderr,
942 "-miphoneos-version-min only valid for darwin (Mac OS X) targets\n");
943 exit(1);
944 }
945 unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin");
946
947 // Remove the number.
948 Triple.resize(DarwinNumIdx);
949
950 // Validate that IPhoneOSVersionMin is a 'version number', starting with [2-9].[0-9]
951 bool IPhoneOSVersionMinIsInvalid = false;
952 int VersionNum = 0;
953 if (IPhoneOSVersionMin.size() < 3 ||
954 !isdigit(IPhoneOSVersionMin[0])) {
955 IPhoneOSVersionMinIsInvalid = true;
956 } else {
957 const char *Start = IPhoneOSVersionMin.c_str();
958 char *End = 0;
959 VersionNum = (int)strtol(Start, &End, 10);
960
961 // The version number must be in the range 0-9.
962 IPhoneOSVersionMinIsInvalid = (unsigned)VersionNum > 9;
963
964 // Turn IPhoneOSVersionMin into a darwin number: e.g. 2.0 is 2 -> 9.2.
965 Triple += "9." + llvm::itostr(VersionNum);
966
967 if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 2.2 is ok.
968 // Add the period piece (.2) to the end of the triple. This gives us
969 // something like ...-darwin9.2.2
970 Triple += End;
971 } else if (End[0] != '\0') { // "2.2" is ok. 2x is not.
972 IPhoneOSVersionMinIsInvalid = true;
973 }
974 }
975
976 if (IPhoneOSVersionMinIsInvalid) {
977 fprintf(stderr,
978 "-miphoneos-version-min=%s is invalid, expected something like '2.0'.\n",
979 IPhoneOSVersionMin.c_str());
980 exit(1);
981 }
982}
983
Chris Lattner01de9c82008-09-30 20:16:56 +0000984/// CreateTargetTriple - Process the various options that affect the target
985/// triple and build a final aggregate triple that we are compiling for.
Chris Lattnerf3d79c32008-03-09 01:35:13 +0000986static std::string CreateTargetTriple() {
Ted Kremenek40499482007-12-03 22:06:55 +0000987 // Initialize base triple. If a -triple option has been specified, use
988 // that triple. Otherwise, default to the host triple.
Chris Lattner210c0cc2007-12-12 05:01:48 +0000989 std::string Triple = TargetTriple;
Daniel Dunbar7fe2af62009-03-31 17:35:15 +0000990 if (Triple.empty())
991 Triple = llvm::sys::getHostTriple();
Ted Kremenek40499482007-12-03 22:06:55 +0000992
Chris Lattnerf3d79c32008-03-09 01:35:13 +0000993 // If -arch foo was specified, remove the architecture from the triple we have
994 // so far and replace it with the specified one.
Daniel Dunbar4d36d982009-03-31 20:10:05 +0000995
996 // FIXME: -arch should be removed, the driver should handle this.
Chris Lattner2b168e02008-09-30 01:13:12 +0000997 if (!Arch.empty()) {
998 // Decompose the base triple into "arch" and suffix.
999 std::string::size_type FirstDashIdx = Triple.find('-');
Chris Lattnerf3d79c32008-03-09 01:35:13 +00001000
Chris Lattner2b168e02008-09-30 01:13:12 +00001001 if (FirstDashIdx == std::string::npos) {
1002 fprintf(stderr,
1003 "Malformed target triple: \"%s\" ('-' could not be found).\n",
1004 Triple.c_str());
1005 exit(1);
1006 }
Chris Lattnerf6cde9f2009-03-24 16:18:41 +00001007
1008 // Canonicalize -arch ppc to add "powerpc" to the triple, not ppc.
1009 if (Arch == "ppc")
1010 Arch = "powerpc";
1011 else if (Arch == "ppc64")
1012 Arch = "powerpc64";
Ted Kremenek40499482007-12-03 22:06:55 +00001013
Chris Lattner2b168e02008-09-30 01:13:12 +00001014 Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end());
1015 }
1016
1017 // If -mmacosx-version-min=10.3.9 is specified, change the triple from being
1018 // something like powerpc-apple-darwin9 to powerpc-apple-darwin7
Chris Lattner01de9c82008-09-30 20:16:56 +00001019 if (!MacOSVersionMin.empty())
1020 HandleMacOSVersionMin(Triple);
Daniel Dunbar93f64532009-04-10 19:52:24 +00001021 else if (!IPhoneOSVersionMin.empty())
1022 HandleIPhoneOSVersionMin(Triple);;
Ted Kremenek40499482007-12-03 22:06:55 +00001023
Chris Lattner2b168e02008-09-30 01:13:12 +00001024 return Triple;
Ted Kremenek40499482007-12-03 22:06:55 +00001025}
1026
1027//===----------------------------------------------------------------------===//
Chris Lattner94269d72009-04-21 05:40:52 +00001028// SourceManager initialization.
Chris Lattner4b009652007-07-25 00:24:17 +00001029//===----------------------------------------------------------------------===//
1030
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001031static bool InitializeSourceManager(Preprocessor &PP,
1032 const std::string &InFile) {
1033 // Figure out where to get and map in the main file.
1034 SourceManager &SourceMgr = PP.getSourceManager();
1035 FileManager &FileMgr = PP.getFileManager();
1036
1037 if (InFile != "-") {
1038 const FileEntry *File = FileMgr.getFile(InFile);
1039 if (File) SourceMgr.createMainFileID(File, SourceLocation());
1040 if (SourceMgr.getMainFileID().isInvalid()) {
1041 PP.getDiagnostics().Report(FullSourceLoc(), diag::err_fe_error_reading)
1042 << InFile.c_str();
1043 return true;
1044 }
1045 } else {
1046 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
1047
1048 // If stdin was empty, SB is null. Cons up an empty memory
1049 // buffer now.
1050 if (!SB) {
1051 const char *EmptyStr = "";
1052 SB = llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<stdin>");
1053 }
1054
1055 SourceMgr.createMainFileIDForMemBuffer(SB);
1056 if (SourceMgr.getMainFileID().isInvalid()) {
1057 PP.getDiagnostics().Report(FullSourceLoc(),
1058 diag::err_fe_error_reading_stdin);
1059 return true;
1060 }
1061 }
1062
1063 return false;
1064}
1065
Chris Lattner47b6a162008-04-19 23:09:31 +00001066
Chris Lattner94269d72009-04-21 05:40:52 +00001067//===----------------------------------------------------------------------===//
1068// Preprocessor Initialization
1069//===----------------------------------------------------------------------===//
Sam Bishop61a20782008-04-14 14:41:57 +00001070
Chris Lattner94269d72009-04-21 05:40:52 +00001071// FIXME: Preprocessor builtins to support.
1072// -A... - Play with #assertions
1073// -undef - Undefine all predefined macros
Chris Lattnerb62396d2009-04-08 20:15:42 +00001074
Chris Lattner94269d72009-04-21 05:40:52 +00001075static llvm::cl::list<std::string>
1076D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
1077 llvm::cl::desc("Predefine the specified macro"));
1078static llvm::cl::list<std::string>
1079U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
1080 llvm::cl::desc("Undefine the specified macro"));
Chris Lattner0e004a12009-04-08 18:24:34 +00001081
Chris Lattner94269d72009-04-21 05:40:52 +00001082static llvm::cl::list<std::string>
1083ImplicitIncludes("include", llvm::cl::value_desc("file"),
1084 llvm::cl::desc("Include file before parsing"));
1085static llvm::cl::list<std::string>
1086ImplicitMacroIncludes("imacros", llvm::cl::value_desc("file"),
1087 llvm::cl::desc("Include macros from file before parsing"));
Chris Lattner0e004a12009-04-08 18:24:34 +00001088
Chris Lattner94269d72009-04-21 05:40:52 +00001089static llvm::cl::opt<std::string>
1090ImplicitIncludePCH("include-pch", llvm::cl::value_desc("file"),
1091 llvm::cl::desc("Include precompiled header file"));
1092
1093static llvm::cl::opt<std::string>
1094ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"),
1095 llvm::cl::desc("Include file before parsing"));
1096
Chris Lattner4b009652007-07-25 00:24:17 +00001097
1098//===----------------------------------------------------------------------===//
1099// Preprocessor include path information.
1100//===----------------------------------------------------------------------===//
1101
1102// This tool exports a large number of command line options to control how the
1103// preprocessor searches for header files. At root, however, the Preprocessor
1104// object takes a very simple interface: a list of directories to search for
1105//
1106// FIXME: -nostdinc,-nostdinc++
Chris Lattnerae3dcc02007-08-26 17:47:35 +00001107// FIXME: -imultilib
Chris Lattner4b009652007-07-25 00:24:17 +00001108//
Chris Lattner4b009652007-07-25 00:24:17 +00001109
1110static llvm::cl::opt<bool>
1111nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
1112
1113// Various command line options. These four add directories to each chain.
1114static llvm::cl::list<std::string>
1115F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
1116 llvm::cl::desc("Add directory to framework include search path"));
1117static llvm::cl::list<std::string>
1118I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
1119 llvm::cl::desc("Add directory to include search path"));
1120static llvm::cl::list<std::string>
1121idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
1122 llvm::cl::desc("Add directory to AFTER include search path"));
1123static llvm::cl::list<std::string>
1124iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
1125 llvm::cl::desc("Add directory to QUOTE include search path"));
1126static llvm::cl::list<std::string>
1127isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
1128 llvm::cl::desc("Add directory to SYSTEM include search path"));
1129
1130// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
1131static llvm::cl::list<std::string>
1132iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
1133 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
1134static llvm::cl::list<std::string>
1135iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
1136 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
1137static llvm::cl::list<std::string>
1138iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
1139 llvm::cl::Prefix,
1140 llvm::cl::desc("Set directory to include search path with prefix"));
1141
Chris Lattnerae3dcc02007-08-26 17:47:35 +00001142static llvm::cl::opt<std::string>
1143isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
1144 llvm::cl::desc("Set the system root directory (usually /)"));
1145
Chris Lattner4b009652007-07-25 00:24:17 +00001146// Finally, implement the code that groks the options above.
Chris Lattner4f022a72008-03-01 08:07:28 +00001147
Chris Lattner4b009652007-07-25 00:24:17 +00001148/// InitializeIncludePaths - Process the -I options and set them in the
1149/// HeaderSearch object.
Nico Weber770e3882008-08-22 09:25:22 +00001150void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers,
1151 FileManager &FM, const LangOptions &Lang) {
1152 InitHeaderSearch Init(Headers, Verbose, isysroot);
1153
Ted Kremenekb4d41e12008-05-31 00:27:00 +00001154 // Handle -I... and -F... options, walking the lists in parallel.
1155 unsigned Iidx = 0, Fidx = 0;
1156 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
1157 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
Nico Weber770e3882008-08-22 09:25:22 +00001158 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekb4d41e12008-05-31 00:27:00 +00001159 ++Iidx;
1160 } else {
Nico Weber770e3882008-08-22 09:25:22 +00001161 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Ted Kremenekb4d41e12008-05-31 00:27:00 +00001162 ++Fidx;
1163 }
1164 }
Chris Lattner4b009652007-07-25 00:24:17 +00001165
Ted Kremenekb4d41e12008-05-31 00:27:00 +00001166 // Consume what's left from whatever list was longer.
1167 for (; Iidx != I_dirs.size(); ++Iidx)
Nico Weber770e3882008-08-22 09:25:22 +00001168 Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false);
Ted Kremenekb4d41e12008-05-31 00:27:00 +00001169 for (; Fidx != F_dirs.size(); ++Fidx)
Nico Weber770e3882008-08-22 09:25:22 +00001170 Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001171
1172 // Handle -idirafter... options.
1173 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
Nico Weber770e3882008-08-22 09:25:22 +00001174 Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After,
1175 false, true, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001176
1177 // Handle -iquote... options.
1178 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
Nico Weber770e3882008-08-22 09:25:22 +00001179 Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001180
1181 // Handle -isystem... options.
1182 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
Nico Weber770e3882008-08-22 09:25:22 +00001183 Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001184
1185 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
1186 // parallel, processing the values in order of occurance to get the right
1187 // prefixes.
1188 {
1189 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
1190 unsigned iprefix_idx = 0;
1191 unsigned iwithprefix_idx = 0;
1192 unsigned iwithprefixbefore_idx = 0;
1193 bool iprefix_done = iprefix_vals.empty();
1194 bool iwithprefix_done = iwithprefix_vals.empty();
1195 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
1196 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
1197 if (!iprefix_done &&
1198 (iwithprefix_done ||
1199 iprefix_vals.getPosition(iprefix_idx) <
1200 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
1201 (iwithprefixbefore_done ||
1202 iprefix_vals.getPosition(iprefix_idx) <
1203 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
1204 Prefix = iprefix_vals[iprefix_idx];
1205 ++iprefix_idx;
1206 iprefix_done = iprefix_idx == iprefix_vals.size();
1207 } else if (!iwithprefix_done &&
1208 (iwithprefixbefore_done ||
1209 iwithprefix_vals.getPosition(iwithprefix_idx) <
1210 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
Nico Weber770e3882008-08-22 09:25:22 +00001211 Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
1212 InitHeaderSearch::System, false, false, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001213 ++iwithprefix_idx;
1214 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
1215 } else {
Nico Weber770e3882008-08-22 09:25:22 +00001216 Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
1217 InitHeaderSearch::Angled, false, false, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001218 ++iwithprefixbefore_idx;
1219 iwithprefixbefore_done =
1220 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
1221 }
1222 }
1223 }
Chris Lattner4f022a72008-03-01 08:07:28 +00001224
Nico Weber770e3882008-08-22 09:25:22 +00001225 Init.AddDefaultEnvVarPaths(Lang);
Chris Lattner4f022a72008-03-01 08:07:28 +00001226
Daniel Dunbar4e604292009-02-21 20:52:41 +00001227 // Add the clang headers, which are relative to the clang binary.
Chris Lattner3ee4a2f2008-03-03 03:16:03 +00001228 llvm::sys::Path MainExecutablePath =
Chris Lattner716a0542008-03-03 05:57:43 +00001229 llvm::sys::Path::GetMainExecutable(Argv0,
1230 (void*)(intptr_t)InitializeIncludePaths);
Chris Lattner3ee4a2f2008-03-03 03:16:03 +00001231 if (!MainExecutablePath.isEmpty()) {
1232 MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang
1233 MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin
Daniel Dunbar4e604292009-02-21 20:52:41 +00001234
1235 // Get foo/lib/clang/1.0/include
1236 //
1237 // FIXME: Don't embed version here.
1238 MainExecutablePath.appendComponent("lib");
1239 MainExecutablePath.appendComponent("clang");
1240 MainExecutablePath.appendComponent("1.0");
1241 MainExecutablePath.appendComponent("include");
Chris Lattner99a72652009-02-19 06:48:28 +00001242
1243 // We pass true to ignore sysroot so that we *always* look for clang headers
1244 // relative to our executable, never relative to -isysroot.
1245 Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System,
1246 false, false, false, true /*ignore sysroot*/);
Chris Lattner3ee4a2f2008-03-03 03:16:03 +00001247 }
1248
Nico Weber770e3882008-08-22 09:25:22 +00001249 if (!nostdinc)
1250 Init.AddDefaultSystemIncludePaths(Lang);
Chris Lattner4b009652007-07-25 00:24:17 +00001251
1252 // Now that we have collected all of the include paths, merge them all
1253 // together and tell the preprocessor about them.
1254
Nico Weber770e3882008-08-22 09:25:22 +00001255 Init.Realize();
Chris Lattner4b009652007-07-25 00:24:17 +00001256}
1257
Chris Lattner94269d72009-04-21 05:40:52 +00001258void InitializePreprocessorInitOptions(PreprocessorInitOptions &InitOpts)
1259{
1260 // Add macros from the command line.
1261 unsigned d = 0, D = D_macros.size();
1262 unsigned u = 0, U = U_macros.size();
1263 while (d < D || u < U) {
1264 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
1265 InitOpts.addMacroDef(D_macros[d++]);
1266 else
1267 InitOpts.addMacroUndef(U_macros[u++]);
1268 }
1269
1270 // If -imacros are specified, include them now. These are processed before
1271 // any -include directives.
1272 for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i)
1273 InitOpts.addMacroInclude(ImplicitMacroIncludes[i]);
1274
1275 if (!ImplicitIncludePTH.empty() || !ImplicitIncludes.empty()) {
1276 // We want to add these paths to the predefines buffer in order, make a
1277 // temporary vector to sort by their occurrence.
1278 llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths;
1279
1280 if (!ImplicitIncludePTH.empty())
1281 OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(),
1282 &ImplicitIncludePTH));
1283 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
1284 OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i),
1285 &ImplicitIncludes[i]));
1286 llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end());
1287
1288
1289 // Now that they are ordered by position, add to the predefines buffer.
1290 for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i) {
1291 std::string *Ptr = OrderedPaths[i].second;
1292 if (!ImplicitIncludes.empty() &&
1293 Ptr >= &ImplicitIncludes[0] &&
1294 Ptr <= &ImplicitIncludes[ImplicitIncludes.size()-1]) {
1295 InitOpts.addInclude(*Ptr, false);
1296 } else {
1297 assert(Ptr == &ImplicitIncludePTH);
1298 InitOpts.addInclude(*Ptr, true);
1299 }
1300 }
1301 }
1302}
1303
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001304//===----------------------------------------------------------------------===//
1305// Driver PreprocessorFactory - For lazily generating preprocessors ...
1306//===----------------------------------------------------------------------===//
1307
1308namespace {
1309class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory {
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001310 const std::string &InFile;
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001311 Diagnostic &Diags;
1312 const LangOptions &LangInfo;
1313 TargetInfo &Target;
1314 SourceManager &SourceMgr;
1315 HeaderSearch &HeaderInfo;
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001316
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001317public:
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001318 DriverPreprocessorFactory(const std::string &infile,
1319 Diagnostic &diags, const LangOptions &opts,
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001320 TargetInfo &target, SourceManager &SM,
1321 HeaderSearch &Headers)
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001322 : InFile(infile), Diags(diags), LangInfo(opts), Target(target),
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001323 SourceMgr(SM), HeaderInfo(Headers) {}
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001324
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001325
1326 virtual ~DriverPreprocessorFactory() {}
1327
1328 virtual Preprocessor* CreatePreprocessor() {
Ted Kremenekd976c3d2009-01-15 18:47:46 +00001329 llvm::OwningPtr<PTHManager> PTHMgr;
1330
Ted Kremenek2ee90d52009-03-20 00:26:38 +00001331 if (!TokenCache.empty() && !ImplicitIncludePTH.empty()) {
1332 fprintf(stderr, "error: cannot use both -token-cache and -include-pth "
1333 "options\n");
Ted Kremenek6348cc62009-03-22 06:42:39 +00001334 exit(1);
Ted Kremenek2ee90d52009-03-20 00:26:38 +00001335 }
1336
Ted Kremenekd976c3d2009-01-15 18:47:46 +00001337 // Use PTH?
Ted Kremenek2ee90d52009-03-20 00:26:38 +00001338 if (!TokenCache.empty() || !ImplicitIncludePTH.empty()) {
1339 const std::string& x = TokenCache.empty() ? ImplicitIncludePTH:TokenCache;
Ted Kremenek6348cc62009-03-22 06:42:39 +00001340 PTHMgr.reset(PTHManager::Create(x, &Diags,
1341 TokenCache.empty() ? Diagnostic::Error
1342 : Diagnostic::Warning));
Ted Kremenek2ee90d52009-03-20 00:26:38 +00001343 }
Ted Kremenekd976c3d2009-01-15 18:47:46 +00001344
Ted Kremenek6348cc62009-03-22 06:42:39 +00001345 if (Diags.hasErrorOccurred())
1346 exit(1);
1347
Ted Kremenekd976c3d2009-01-15 18:47:46 +00001348 // Create the Preprocessor.
1349 llvm::OwningPtr<Preprocessor> PP(new Preprocessor(Diags, LangInfo, Target,
1350 SourceMgr, HeaderInfo,
1351 PTHMgr.get()));
1352
1353 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
1354 // That argument is used as the IdentifierInfoLookup argument to
1355 // IdentifierTable's ctor.
1356 if (PTHMgr) {
1357 PTHMgr->setPreprocessor(PP.get());
1358 PP->setPTHManager(PTHMgr.take());
1359 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001360
Chris Lattner94269d72009-04-21 05:40:52 +00001361 PreprocessorInitOptions InitOpts;
1362 InitializePreprocessorInitOptions(InitOpts);
1363 if (InitializePreprocessor(*PP, InFile, InitOpts))
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001364 return 0;
Chris Lattner94269d72009-04-21 05:40:52 +00001365
Daniel Dunbar35fe5de2008-10-24 22:12:41 +00001366 /// FIXME: PP can only handle one callback
Daniel Dunbar0bf13eb2009-03-30 00:34:04 +00001367 if (ProgAction != PrintPreprocessedInput) {
1368 std::string ErrStr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001369 bool DFG = CreateDependencyFileGen(PP.get(), ErrStr);
Daniel Dunbar0bf13eb2009-03-30 00:34:04 +00001370 if (!DFG && !ErrStr.empty()) {
1371 fprintf(stderr, "%s", ErrStr.c_str());
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001372 return 0;
Daniel Dunbar35fe5de2008-10-24 22:12:41 +00001373 }
1374 }
1375
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001376 return PP.take();
Ted Kremenek01d3bf72008-04-17 21:38:34 +00001377 }
1378};
1379}
Chris Lattner4b009652007-07-25 00:24:17 +00001380
Chris Lattner4b009652007-07-25 00:24:17 +00001381//===----------------------------------------------------------------------===//
1382// Basic Parser driver
1383//===----------------------------------------------------------------------===//
1384
Chris Lattner9d818a22008-04-19 23:25:44 +00001385static void ParseFile(Preprocessor &PP, MinimalAction *PA) {
Chris Lattner4b009652007-07-25 00:24:17 +00001386 Parser P(PP, *PA);
Ted Kremenek17861c52007-12-19 22:51:13 +00001387 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +00001388
1389 // Parsing the specified input file.
1390 P.ParseTranslationUnit();
1391 delete PA;
1392}
1393
1394//===----------------------------------------------------------------------===//
Daniel Dunbaraa7a0662008-10-23 05:50:47 +00001395// Code generation options
1396//===----------------------------------------------------------------------===//
1397
1398static llvm::cl::opt<bool>
Chris Lattner9a09eda2009-03-09 22:05:03 +00001399GenerateDebugInfo("g",
1400 llvm::cl::desc("Generate source level debug information"));
1401
Daniel Dunbar9101a632009-02-17 19:47:34 +00001402static llvm::cl::opt<std::string>
1403TargetCPU("mcpu",
1404 llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)"));
1405
Chris Lattner8a9615c2009-03-16 18:41:18 +00001406static void InitializeCompileOptions(CompileOptions &Opts,
1407 const LangOptions &LangOpts) {
Daniel Dunbaraa7a0662008-10-23 05:50:47 +00001408 Opts.OptimizeSize = OptSize;
Chris Lattner88cbb9b2009-03-09 22:00:34 +00001409 Opts.DebugInfo = GenerateDebugInfo;
Daniel Dunbard725b162008-10-29 07:56:11 +00001410 if (OptSize) {
1411 // -Os implies -O2
1412 // FIXME: Diagnose conflicting options.
1413 Opts.OptimizationLevel = 2;
1414 } else {
1415 Opts.OptimizationLevel = OptLevel;
1416 }
Daniel Dunbar721cbf12008-10-29 03:42:18 +00001417
1418 // FIXME: There are llvm-gcc options to control these selectively.
1419 Opts.InlineFunctions = (Opts.OptimizationLevel > 1);
1420 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
Chris Lattner8a9615c2009-03-16 18:41:18 +00001421 Opts.SimplifyLibCalls = !LangOpts.NoBuiltin;
Daniel Dunbare8d0ba72008-10-31 09:34:21 +00001422
1423#ifdef NDEBUG
1424 Opts.VerifyModule = 0;
1425#endif
Daniel Dunbar9101a632009-02-17 19:47:34 +00001426
1427 Opts.CPU = TargetCPU;
1428 Opts.Features.insert(Opts.Features.end(),
1429 TargetFeatures.begin(), TargetFeatures.end());
Chris Lattnere8f70712009-02-18 01:23:44 +00001430
Chris Lattnerf04a7562009-03-26 05:00:52 +00001431 Opts.NoCommon = NoCommon | LangOpts.CPlusPlus;
1432
Chris Lattnere8f70712009-02-18 01:23:44 +00001433 // Handle -ftime-report.
1434 Opts.TimePasses = TimeReport;
Daniel Dunbaraa7a0662008-10-23 05:50:47 +00001435}
1436
1437//===----------------------------------------------------------------------===//
Douglas Gregor24b48b02009-04-02 19:05:20 +00001438// Fix-It Options
1439//===----------------------------------------------------------------------===//
1440static llvm::cl::list<ParsedSourceLocation>
1441FixItAtLocations("fixit-at", llvm::cl::value_desc("source-location"),
1442 llvm::cl::desc("Perform Fix-It modifications at the given source location"));
1443
1444//===----------------------------------------------------------------------===//
Chris Lattner94859302009-04-17 21:05:01 +00001445// -dump-build-information Stuff
1446//===----------------------------------------------------------------------===//
1447
1448static llvm::cl::opt<std::string>
1449DumpBuildInformation("dump-build-information",
1450 llvm::cl::value_desc("filename"),
1451 llvm::cl::desc("output a dump of some build information to a file"));
1452
1453static llvm::raw_ostream *BuildLogFile = 0;
1454
1455/// LoggingDiagnosticClient - This is a simple diagnostic client that forwards
1456/// all diagnostics to both BuildLogFile and a chained DiagnosticClient.
1457namespace {
1458class LoggingDiagnosticClient : public DiagnosticClient {
1459 llvm::OwningPtr<DiagnosticClient> Chain1;
1460 llvm::OwningPtr<DiagnosticClient> Chain2;
1461public:
1462
1463 LoggingDiagnosticClient(DiagnosticClient *Normal) {
1464 // Output diags both where requested...
1465 Chain1.reset(Normal);
1466 // .. and to our log file.
1467 Chain2.reset(new TextDiagnosticPrinter(*BuildLogFile,
1468 !NoShowColumn,
1469 !NoCaretDiagnostics,
1470 !NoShowLocation,
1471 PrintSourceRangeInfo,
Chris Lattner041bd732009-04-19 07:44:08 +00001472 PrintDiagnosticOption,
1473 !NoDiagnosticsFixIt));
Chris Lattner94859302009-04-17 21:05:01 +00001474 }
1475
1476 virtual void setLangOptions(const LangOptions *LO) {
1477 Chain1->setLangOptions(LO);
1478 Chain2->setLangOptions(LO);
1479 }
1480
1481 virtual bool IncludeInDiagnosticCounts() const {
1482 return Chain1->IncludeInDiagnosticCounts();
1483 }
1484
1485 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
1486 const DiagnosticInfo &Info) {
1487 Chain1->HandleDiagnostic(DiagLevel, Info);
1488 Chain2->HandleDiagnostic(DiagLevel, Info);
1489 }
1490};
1491} // end anonymous namespace.
1492
1493static void SetUpBuildDumpLog(unsigned argc, char **argv,
1494 llvm::OwningPtr<DiagnosticClient> &DiagClient) {
1495
1496 std::string ErrorInfo;
1497 BuildLogFile = new llvm::raw_fd_ostream(DumpBuildInformation.c_str(), false,
1498 ErrorInfo);
1499
1500 if (!ErrorInfo.empty()) {
1501 llvm::errs() << "error opening -dump-build-information file '"
1502 << DumpBuildInformation << "', option ignored!\n";
1503 delete BuildLogFile;
1504 BuildLogFile = 0;
1505 DumpBuildInformation = "";
1506 return;
1507 }
1508
1509 (*BuildLogFile) << "clang-cc command line arguments: ";
1510 for (unsigned i = 0; i != argc; ++i)
1511 (*BuildLogFile) << argv[i] << ' ';
1512 (*BuildLogFile) << '\n';
1513
1514 // LoggingDiagnosticClient - Insert a new logging diagnostic client in between
1515 // the diagnostic producers and the normal receiver.
1516 DiagClient.reset(new LoggingDiagnosticClient(DiagClient.take()));
1517}
1518
1519
1520
1521//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00001522// Main driver
1523//===----------------------------------------------------------------------===//
1524
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001525/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
Chris Lattner9a09eda2009-03-09 22:05:03 +00001526/// action. These consumers can operate on both ASTs that are freshly
1527/// parsed from source files as well as those deserialized from Bitcode.
1528/// Note that PP and PPF may be null here.
Chris Lattner7f902922009-02-18 01:20:05 +00001529static ASTConsumer *CreateASTConsumer(const std::string& InFile,
Ted Kremenek397de012007-12-13 00:37:31 +00001530 Diagnostic& Diag, FileManager& FileMgr,
Chris Lattner8d72ee02008-02-06 01:42:25 +00001531 const LangOptions& LangOpts,
Chris Lattner21f72d62008-04-16 06:11:58 +00001532 Preprocessor *PP,
Ted Kremenek7c65b6c2008-08-05 18:50:11 +00001533 PreprocessorFactory *PPF) {
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001534 switch (ProgAction) {
Chris Lattner7f902922009-02-18 01:20:05 +00001535 default:
1536 return NULL;
1537
1538 case ASTPrint:
1539 return CreateASTPrinter();
1540
1541 case ASTDump:
1542 return CreateASTDumper();
1543
1544 case ASTView:
1545 return CreateASTViewer();
Zhongxing Xu6036bbe2009-01-13 01:29:24 +00001546
Chris Lattner7f902922009-02-18 01:20:05 +00001547 case PrintDeclContext:
1548 return CreateDeclContextPrinter();
1549
1550 case EmitHTML:
1551 return CreateHTMLPrinter(OutputFile, Diag, PP, PPF);
Ted Kremeneke972d852008-07-02 18:23:21 +00001552
Chris Lattner7f902922009-02-18 01:20:05 +00001553 case InheritanceView:
1554 return CreateInheritanceViewer(InheritanceViewCls);
1555
Chris Lattner7f902922009-02-18 01:20:05 +00001556 case EmitAssembly:
1557 case EmitLLVM:
Daniel Dunbard999a8e2009-02-26 22:39:37 +00001558 case EmitBC:
1559 case EmitLLVMOnly: {
Chris Lattner7f902922009-02-18 01:20:05 +00001560 BackendAction Act;
1561 if (ProgAction == EmitAssembly)
1562 Act = Backend_EmitAssembly;
1563 else if (ProgAction == EmitLLVM)
1564 Act = Backend_EmitLL;
Daniel Dunbard999a8e2009-02-26 22:39:37 +00001565 else if (ProgAction == EmitLLVMOnly)
1566 Act = Backend_EmitNothing;
Chris Lattner7f902922009-02-18 01:20:05 +00001567 else
1568 Act = Backend_EmitBC;
1569
1570 CompileOptions Opts;
Chris Lattner8a9615c2009-03-16 18:41:18 +00001571 InitializeCompileOptions(Opts, LangOpts);
Chris Lattner7f902922009-02-18 01:20:05 +00001572 return CreateBackendConsumer(Act, Diag, LangOpts, Opts,
Chris Lattner88cbb9b2009-03-09 22:00:34 +00001573 InFile, OutputFile);
Chris Lattner7f902922009-02-18 01:20:05 +00001574 }
Seo Sanghyeon550a1eb2007-12-24 01:52:34 +00001575
Douglas Gregorc34897d2009-04-09 22:27:44 +00001576 case GeneratePCH:
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001577 return CreatePCHGenerator(*PP, OutputFile);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001578
Chris Lattner7f902922009-02-18 01:20:05 +00001579 case RewriteObjC:
1580 return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts);
Steve Naroff93c18352008-09-18 14:10:13 +00001581
Chris Lattner7f902922009-02-18 01:20:05 +00001582 case RewriteBlocks:
1583 return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts);
1584
1585 case RunAnalysis:
1586 return CreateAnalysisConsumer(Diag, PP, PPF, LangOpts, OutputFile);
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001587 }
1588}
1589
Chris Lattner4b009652007-07-25 00:24:17 +00001590/// ProcessInputFile - Process a single input file with the specified state.
1591///
Ted Kremenek4e9899f2008-04-17 22:31:54 +00001592static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001593 const std::string &InFile, ProgActions PA) {
Ted Kremenek50aab982008-08-08 02:46:37 +00001594 llvm::OwningPtr<ASTConsumer> Consumer;
Chris Lattner4b009652007-07-25 00:24:17 +00001595 bool ClearSourceMgr = false;
Douglas Gregor133d2552009-04-02 01:08:08 +00001596 FixItRewriter *FixItRewrite = 0;
Douglas Gregore0d5c562009-04-14 16:27:31 +00001597 bool CompleteTranslationUnit = true;
Douglas Gregor133d2552009-04-02 01:08:08 +00001598
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001599 switch (PA) {
Chris Lattner4b009652007-07-25 00:24:17 +00001600 default:
Ted Kremenek50aab982008-08-08 02:46:37 +00001601 Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(),
1602 PP.getFileManager(), PP.getLangOptions(),
1603 &PP, &PPF));
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001604
1605 if (!Consumer) {
1606 fprintf(stderr, "Unexpected program action!\n");
Daniel Dunbar70a66b12008-10-04 23:42:49 +00001607 HadErrors = true;
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001608 return;
1609 }
Ted Kremenekd890f6a2007-12-19 22:24:34 +00001610
Douglas Gregore0d5c562009-04-14 16:27:31 +00001611 if (ProgAction == GeneratePCH)
1612 CompleteTranslationUnit = false;
Ted Kremeneka36aaef2007-12-05 18:27:04 +00001613 break;
1614
Chris Lattneraf669fb2008-10-12 05:03:36 +00001615 case DumpRawTokens: {
Chris Lattnerefe33382009-02-18 01:51:21 +00001616 llvm::TimeRegion Timer(ClangFrontendTimer);
Chris Lattneraf669fb2008-10-12 05:03:36 +00001617 SourceManager &SM = PP.getSourceManager();
Chris Lattneraf669fb2008-10-12 05:03:36 +00001618 // Start lexing the specified input file.
Chris Lattnerc7b23592009-01-17 07:35:14 +00001619 Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions());
Chris Lattneraf669fb2008-10-12 05:03:36 +00001620 RawLex.SetKeepWhitespaceMode(true);
1621
1622 Token RawTok;
Chris Lattneraf669fb2008-10-12 05:03:36 +00001623 RawLex.LexFromRawLexer(RawTok);
1624 while (RawTok.isNot(tok::eof)) {
1625 PP.DumpToken(RawTok, true);
1626 fprintf(stderr, "\n");
1627 RawLex.LexFromRawLexer(RawTok);
1628 }
1629 ClearSourceMgr = true;
1630 break;
1631 }
Chris Lattner4b009652007-07-25 00:24:17 +00001632 case DumpTokens: { // Token dump mode.
Chris Lattnerefe33382009-02-18 01:51:21 +00001633 llvm::TimeRegion Timer(ClangFrontendTimer);
Chris Lattner4b009652007-07-25 00:24:17 +00001634 Token Tok;
Chris Lattneraf669fb2008-10-12 05:03:36 +00001635 // Start preprocessing the specified input file.
Ted Kremenek17861c52007-12-19 22:51:13 +00001636 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +00001637 do {
1638 PP.Lex(Tok);
1639 PP.DumpToken(Tok, true);
1640 fprintf(stderr, "\n");
Chris Lattner3b494152007-10-09 18:03:42 +00001641 } while (Tok.isNot(tok::eof));
Chris Lattner4b009652007-07-25 00:24:17 +00001642 ClearSourceMgr = true;
1643 break;
1644 }
1645 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
Chris Lattnerefe33382009-02-18 01:51:21 +00001646 llvm::TimeRegion Timer(ClangFrontendTimer);
Chris Lattner4b009652007-07-25 00:24:17 +00001647 Token Tok;
1648 // Start parsing the specified input file.
Ted Kremenek17861c52007-12-19 22:51:13 +00001649 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +00001650 do {
1651 PP.Lex(Tok);
Chris Lattner3b494152007-10-09 18:03:42 +00001652 } while (Tok.isNot(tok::eof));
Chris Lattner4b009652007-07-25 00:24:17 +00001653 ClearSourceMgr = true;
1654 break;
1655 }
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001656
Douglas Gregor2a0e8742009-04-02 23:43:50 +00001657 case GeneratePTH: {
Chris Lattnerefe33382009-02-18 01:51:21 +00001658 llvm::TimeRegion Timer(ClangFrontendTimer);
Ted Kremenek71c6cc62008-10-21 00:54:44 +00001659 CacheTokens(PP, OutputFile);
1660 ClearSourceMgr = true;
1661 break;
1662 }
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001663
Chris Lattnerefe33382009-02-18 01:51:21 +00001664 case PrintPreprocessedInput: { // -E mode.
1665 llvm::TimeRegion Timer(ClangFrontendTimer);
Chris Lattnerefd02a32008-01-27 23:55:11 +00001666 DoPrintPreprocessedInput(PP, OutputFile);
Chris Lattner4b009652007-07-25 00:24:17 +00001667 ClearSourceMgr = true;
1668 break;
Chris Lattnerefe33382009-02-18 01:51:21 +00001669 }
Chris Lattner1665a9f2008-05-08 06:52:13 +00001670
Chris Lattnerefe33382009-02-18 01:51:21 +00001671 case ParseNoop: { // -parse-noop
1672 llvm::TimeRegion Timer(ClangFrontendTimer);
Daniel Dunbar747a95e2008-10-31 08:56:51 +00001673 ParseFile(PP, new MinimalAction(PP));
Chris Lattner4b009652007-07-25 00:24:17 +00001674 ClearSourceMgr = true;
1675 break;
Chris Lattnerefe33382009-02-18 01:51:21 +00001676 }
Chris Lattner4b009652007-07-25 00:24:17 +00001677
Chris Lattnerefe33382009-02-18 01:51:21 +00001678 case ParsePrintCallbacks: {
1679 llvm::TimeRegion Timer(ClangFrontendTimer);
Daniel Dunbar747a95e2008-10-31 08:56:51 +00001680 ParseFile(PP, CreatePrintParserActionsAction(PP));
Chris Lattner4b009652007-07-25 00:24:17 +00001681 ClearSourceMgr = true;
1682 break;
Chris Lattnerefe33382009-02-18 01:51:21 +00001683 }
1684
1685 case ParseSyntaxOnly: { // -fsyntax-only
1686 llvm::TimeRegion Timer(ClangFrontendTimer);
Ted Kremenek50aab982008-08-08 02:46:37 +00001687 Consumer.reset(new ASTConsumer());
Ted Kremenek0a03ce62007-09-17 20:49:30 +00001688 break;
Chris Lattnerefe33382009-02-18 01:51:21 +00001689 }
Chris Lattner1665a9f2008-05-08 06:52:13 +00001690
1691 case RewriteMacros:
Chris Lattner302b0622008-05-09 22:43:24 +00001692 RewriteMacrosInInput(PP, InFile, OutputFile);
Chris Lattner1665a9f2008-05-08 06:52:13 +00001693 ClearSourceMgr = true;
1694 break;
Chris Lattnerc3fbf392008-10-12 05:29:20 +00001695
Chris Lattnerefe33382009-02-18 01:51:21 +00001696 case RewriteTest: {
Chris Lattnerc3fbf392008-10-12 05:29:20 +00001697 DoRewriteTest(PP, InFile, OutputFile);
1698 ClearSourceMgr = true;
1699 break;
Chris Lattner129758d2007-09-16 19:46:59 +00001700 }
Douglas Gregor133d2552009-04-02 01:08:08 +00001701
1702 case FixIt:
1703 llvm::TimeRegion Timer(ClangFrontendTimer);
1704 Consumer.reset(new ASTConsumer());
Douglas Gregor563a2512009-04-02 17:13:00 +00001705 FixItRewrite = new FixItRewriter(PP.getDiagnostics(),
Chris Lattnere1be6022009-04-14 23:22:57 +00001706 PP.getSourceManager(),
1707 PP.getLangOptions());
Douglas Gregor133d2552009-04-02 01:08:08 +00001708 break;
Chris Lattnerefe33382009-02-18 01:51:21 +00001709 }
Ted Kremenek88eebed2009-01-28 04:29:29 +00001710
1711 if (Consumer) {
Chris Lattner143fd6d2009-03-28 01:37:17 +00001712 llvm::OwningPtr<ASTContext> ContextOwner;
Chris Lattner143fd6d2009-03-28 01:37:17 +00001713
Douglas Gregor24b48b02009-04-02 19:05:20 +00001714 if (FixItAtLocations.size() > 0) {
1715 // Even without the "-fixit" flag, with may have some specific
1716 // locations where the user has requested fixes. Process those
1717 // locations now.
1718 if (!FixItRewrite)
1719 FixItRewrite = new FixItRewriter(PP.getDiagnostics(),
Chris Lattnere1be6022009-04-14 23:22:57 +00001720 PP.getSourceManager(),
1721 PP.getLangOptions());
Douglas Gregor24b48b02009-04-02 19:05:20 +00001722
1723 bool AddedFixitLocation = false;
1724 for (unsigned Idx = 0, Last = FixItAtLocations.size();
1725 Idx != Last; ++Idx) {
1726 RequestedSourceLocation Requested;
1727 if (FixItAtLocations[Idx].ResolveLocation(PP.getFileManager(),
1728 Requested)) {
1729 fprintf(stderr, "FIX-IT could not find file \"%s\"\n",
1730 FixItAtLocations[Idx].FileName.c_str());
1731 } else {
1732 FixItRewrite->addFixItLocation(Requested);
1733 AddedFixitLocation = true;
1734 }
1735 }
1736
1737 if (!AddedFixitLocation) {
1738 // All of the fix-it locations were bad. Don't fix anything.
1739 delete FixItRewrite;
1740 FixItRewrite = 0;
1741 }
1742 }
1743
Chris Lattner143fd6d2009-03-28 01:37:17 +00001744 ContextOwner.reset(new ASTContext(PP.getLangOptions(),
1745 PP.getSourceManager(),
1746 PP.getTargetInfo(),
1747 PP.getIdentifierTable(),
1748 PP.getSelectorTable(),
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001749 /* FreeMemory = */ !DisableFree,
1750 /* size_reserve = */0,
1751 /* InitializeBuiltins = */ImplicitIncludePCH.empty()));
Chris Lattner143fd6d2009-03-28 01:37:17 +00001752
Douglas Gregorc34897d2009-04-09 22:27:44 +00001753 if (!ImplicitIncludePCH.empty()) {
1754 // The user has asked us to include a precompiled header. Load
1755 // the precompiled header into the AST context.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001756 llvm::OwningPtr<PCHReader> Reader(new PCHReader(PP, *ContextOwner.get()));
1757 switch (Reader->ReadPCH(ImplicitIncludePCH)) {
1758 case PCHReader::Success: {
1759 // Attach the PCH reader to the AST context as an external AST
1760 // source, so that declarations will be deserialized from the
1761 // PCH file as needed.
1762 llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
1763 ContextOwner->setExternalSource(Source);
1764
1765 // Clear out the predefines buffer, because all of the
1766 // predefines are already in the PCH file.
1767 PP.setPredefines("");
1768 break;
1769 }
1770
1771 case PCHReader::Failure:
1772 // Unrecoverable failure: don't even try to process the input
1773 // file.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001774 return;
1775
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001776 case PCHReader::IgnorePCH:
1777 // No suitable PCH file could be found. Just ignore the
1778 // -include-pch option entirely.
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001779
1780 // We delayed the initialization of builtins in the hope of
1781 // loading the PCH file. Since the PCH file could not be
1782 // loaded, initialize builtins now.
1783 ContextOwner->InitializeBuiltins(PP.getIdentifierTable());
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001784 break;
1785 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001786
1787 // Finish preprocessor initialization. We do this now (rather
1788 // than earlier) because this initialization creates new source
1789 // location entries in the source manager, which must come after
1790 // the source location entries for the PCH file.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001791 if (InitializeSourceManager(PP, InFile))
Douglas Gregorab1cef72009-04-10 03:52:48 +00001792 return;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001793 }
1794
Douglas Gregore0d5c562009-04-14 16:27:31 +00001795 ParseAST(PP, Consumer.get(), *ContextOwner.get(), Stats,
1796 CompleteTranslationUnit);
Chris Lattner143fd6d2009-03-28 01:37:17 +00001797
Douglas Gregor133d2552009-04-02 01:08:08 +00001798 if (FixItRewrite)
1799 FixItRewrite->WriteFixedFile(InFile, OutputFile);
1800
Chris Lattner143fd6d2009-03-28 01:37:17 +00001801 // If in -disable-free mode, don't deallocate these when they go out of
1802 // scope.
Chris Lattner07b08c02009-03-28 04:13:34 +00001803 if (DisableFree)
Chris Lattner143fd6d2009-03-28 01:37:17 +00001804 ContextOwner.take();
Ted Kremenek88eebed2009-01-28 04:29:29 +00001805 }
Daniel Dunbar849bfc62008-10-27 22:03:52 +00001806
1807 if (VerifyDiagnostics)
Daniel Dunbar8d4dff52008-10-27 22:10:13 +00001808 if (CheckDiagnostics(PP))
1809 exit(1);
Chris Lattner8d72ee02008-02-06 01:42:25 +00001810
Chris Lattner4b009652007-07-25 00:24:17 +00001811 if (Stats) {
Ted Kremenekd890f6a2007-12-19 22:24:34 +00001812 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +00001813 PP.PrintStats();
1814 PP.getIdentifierTable().PrintStats();
Chris Lattner968982d2007-12-15 20:48:40 +00001815 PP.getHeaderSearchInfo().PrintStats();
Ted Kremenek40997b62009-01-09 18:20:21 +00001816 PP.getSourceManager().PrintStats();
Chris Lattner4b009652007-07-25 00:24:17 +00001817 fprintf(stderr, "\n");
1818 }
1819
1820 // For a multi-file compilation, some things are ok with nuking the source
1821 // manager tables, other require stable fileid/macroid's across multiple
1822 // files.
Chris Lattner968982d2007-12-15 20:48:40 +00001823 if (ClearSourceMgr)
1824 PP.getSourceManager().clearIDTables();
Daniel Dunbar622d6d02008-11-11 06:35:39 +00001825
1826 if (DisableFree)
1827 Consumer.take();
Chris Lattner4b009652007-07-25 00:24:17 +00001828}
1829
1830static llvm::cl::list<std::string>
1831InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1832
Chris Lattner4b009652007-07-25 00:24:17 +00001833int main(int argc, char **argv) {
Chris Lattner4b009652007-07-25 00:24:17 +00001834 llvm::sys::PrintStackTraceOnErrorSignal();
Chris Lattner2b2d0c42009-03-04 21:41:39 +00001835 llvm::PrettyStackTraceProgram X(argc, argv);
Chris Lattnercb7dddb2009-03-06 05:38:04 +00001836 llvm::cl::ParseCommandLineOptions(argc, argv,
Chris Lattner559a7472009-03-06 05:38:25 +00001837 "LLVM 'Clang' Compiler: http://clang.llvm.org\n");
Chris Lattner4b009652007-07-25 00:24:17 +00001838
Chris Lattnerefe33382009-02-18 01:51:21 +00001839 if (TimeReport)
1840 ClangFrontendTimer = new llvm::Timer("Clang front-end time");
1841
Chris Lattner4b009652007-07-25 00:24:17 +00001842 // If no input was specified, read from stdin.
1843 if (InputFilenames.empty())
1844 InputFilenames.push_back("-");
Chris Lattner93d4d982009-02-18 01:12:43 +00001845
Ted Kremenekb240e822007-12-11 23:28:38 +00001846 // Create the diagnostic client for reporting errors or for
1847 // implementing -verify.
Chris Lattner5f7937d2009-04-17 20:40:01 +00001848 llvm::OwningPtr<DiagnosticClient> DiagClient;
1849 if (VerifyDiagnostics) {
1850 // When checking diagnostics, just buffer them up.
1851 DiagClient.reset(new TextDiagnosticBuffer());
1852 if (InputFilenames.size() != 1) {
1853 fprintf(stderr, "-verify only works on single input files for now.\n");
1854 return 1;
1855 }
1856 if (!HTMLDiag.empty()) {
1857 fprintf(stderr, "-verify and -html-diags don't work together\n");
1858 return 1;
1859 }
1860 } else if (HTMLDiag.empty()) {
Ted Kremenek5c341732008-08-07 17:49:57 +00001861 // Print diagnostics to stderr by default.
Chris Lattner5f7937d2009-04-17 20:40:01 +00001862 DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(),
Chris Lattner92a33532008-11-19 06:56:25 +00001863 !NoShowColumn,
Chris Lattnerb96a04f2009-01-30 19:01:41 +00001864 !NoCaretDiagnostics,
Chris Lattner695a4f52009-03-13 01:08:23 +00001865 !NoShowLocation,
Chris Lattnera96ec3b2009-04-16 05:44:38 +00001866 PrintSourceRangeInfo,
Chris Lattner041bd732009-04-19 07:44:08 +00001867 PrintDiagnosticOption,
1868 !NoDiagnosticsFixIt));
Ted Kremenek5c341732008-08-07 17:49:57 +00001869 } else {
Chris Lattner5f7937d2009-04-17 20:40:01 +00001870 DiagClient.reset(CreateHTMLDiagnosticClient(HTMLDiag));
Chris Lattner4b009652007-07-25 00:24:17 +00001871 }
Chris Lattner94859302009-04-17 21:05:01 +00001872
1873 if (!DumpBuildInformation.empty()) {
1874 if (!HTMLDiag.empty()) {
1875 fprintf(stderr,
1876 "-dump-build-information and -html-diags don't work together\n");
1877 return 1;
1878 }
1879
1880 SetUpBuildDumpLog(argc, argv, DiagClient);
1881 }
1882
Ted Kremenek5c341732008-08-07 17:49:57 +00001883
Chris Lattner4b009652007-07-25 00:24:17 +00001884 // Configure our handling of diagnostics.
Ted Kremenek5c341732008-08-07 17:49:57 +00001885 Diagnostic Diags(DiagClient.get());
Sebastian Redlf10cbca2009-03-07 12:09:25 +00001886 if (ProcessWarningOptions(Diags))
Sebastian Redl44ff86c2009-03-06 17:41:35 +00001887 return 1;
Ted Kremenekb240e822007-12-11 23:28:38 +00001888
Chris Lattner45a56e02007-12-05 23:24:17 +00001889 // -I- is a deprecated GCC feature, scan for it and reject it.
1890 for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
1891 if (I_dirs[i] == "-") {
Chris Lattnera1433472008-11-18 05:05:28 +00001892 Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported);
Chris Lattner45a56e02007-12-05 23:24:17 +00001893 I_dirs.erase(I_dirs.begin()+i);
1894 --i;
1895 }
1896 }
Chris Lattner2c77d852008-03-14 06:12:05 +00001897
1898 // Get information about the target being compiled for.
1899 std::string Triple = CreateTargetTriple();
Ted Kremenekec6c5252008-08-07 18:13:12 +00001900 llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple));
1901
Chris Lattner2c77d852008-03-14 06:12:05 +00001902 if (Target == 0) {
Daniel Dunbarf45afe62009-03-12 10:14:16 +00001903 Diags.Report(FullSourceLoc(), diag::err_fe_unknown_triple)
1904 << Triple.c_str();
Sebastian Redlf10cbca2009-03-07 12:09:25 +00001905 return 1;
Chris Lattner2c77d852008-03-14 06:12:05 +00001906 }
Chris Lattner45a56e02007-12-05 23:24:17 +00001907
Daniel Dunbar9c321102009-01-20 23:17:32 +00001908 if (!InheritanceViewCls.empty()) // C++ visualization?
Ted Kremenekd9ceb3d2008-10-23 23:36:29 +00001909 ProgAction = InheritanceView;
Ted Kremenek5c341732008-08-07 17:49:57 +00001910
Ted Kremenek2a4224a2008-06-06 22:42:39 +00001911 llvm::OwningPtr<SourceManager> SourceMgr;
1912
Chris Lattnere1be6022009-04-14 23:22:57 +00001913 // Create a file manager object to provide access to and cache the filesystem.
1914 FileManager FileMgr;
1915
Chris Lattner4b009652007-07-25 00:24:17 +00001916 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenekb240e822007-12-11 23:28:38 +00001917 const std::string &InFile = InputFilenames[i];
Ted Kremenekb240e822007-12-11 23:28:38 +00001918
Chris Lattner2b989562009-03-04 21:40:56 +00001919 /// Create a SourceManager object. This tracks and owns all the file
1920 /// buffers allocated to a translation unit.
1921 if (!SourceMgr)
1922 SourceMgr.reset(new SourceManager());
1923 else
1924 SourceMgr->clearIDTables();
1925
1926 // Initialize language options, inferring file types from input filenames.
1927 LangOptions LangInfo;
Chris Lattner5f7937d2009-04-17 20:40:01 +00001928 DiagClient->setLangOptions(&LangInfo);
Chris Lattnere1be6022009-04-14 23:22:57 +00001929
Chris Lattner2b989562009-03-04 21:40:56 +00001930 InitializeBaseLanguage();
1931 LangKind LK = GetLanguage(InFile);
Daniel Dunbardb6126e2009-04-01 05:09:09 +00001932 InitializeLangOptions(LangInfo, LK);
Chris Lattner2b989562009-03-04 21:40:56 +00001933 InitializeLanguageStandard(LangInfo, LK, Target.get());
1934
1935 // Process the -I options and set them in the HeaderInfo.
1936 HeaderSearch HeaderInfo(FileMgr);
1937
Chris Lattnere1be6022009-04-14 23:22:57 +00001938
Chris Lattner2b989562009-03-04 21:40:56 +00001939 InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo);
1940
1941 // Set up the preprocessor with these options.
1942 DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target,
1943 *SourceMgr.get(), HeaderInfo);
1944
1945 llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor());
1946
1947 if (!PP)
1948 continue;
Ted Kremenek5c341732008-08-07 17:49:57 +00001949
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001950 if (ImplicitIncludePCH.empty() &&
1951 InitializeSourceManager(*PP.get(), InFile))
Douglas Gregorab1cef72009-04-10 03:52:48 +00001952 continue;
1953
Chris Lattner5f7937d2009-04-17 20:40:01 +00001954 if (!HTMLDiag.empty())
1955 ((PathDiagnosticClient*)DiagClient.get())->SetPreprocessor(PP.get());
Chris Lattner2b989562009-03-04 21:40:56 +00001956
1957 // Process the source file.
Daniel Dunbardb6126e2009-04-01 05:09:09 +00001958 ProcessInputFile(*PP, PPFactory, InFile, ProgAction);
Chris Lattner2b989562009-03-04 21:40:56 +00001959
Chris Lattner2961dc52009-04-17 20:16:08 +00001960 HeaderInfo.ClearFileInfo();
Chris Lattner5f7937d2009-04-17 20:40:01 +00001961 DiagClient->setLangOptions(0);
Chris Lattner4b009652007-07-25 00:24:17 +00001962 }
Chris Lattner2c77d852008-03-14 06:12:05 +00001963
Mike Stump91d01352009-01-28 02:43:35 +00001964 if (Verbose)
1965 fprintf(stderr, "clang version 1.0 based upon " PACKAGE_STRING
1966 " hosted on " LLVM_HOSTTRIPLE "\n");
1967
Ted Kremenekec6c5252008-08-07 18:13:12 +00001968 if (unsigned NumDiagnostics = Diags.getNumDiagnostics())
Chris Lattner4b009652007-07-25 00:24:17 +00001969 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1970 (NumDiagnostics == 1 ? "" : "s"));
1971
1972 if (Stats) {
Chris Lattner4b009652007-07-25 00:24:17 +00001973 FileMgr.PrintStats();
1974 fprintf(stderr, "\n");
1975 }
Chris Lattner94859302009-04-17 21:05:01 +00001976
1977 delete ClangFrontendTimer;
1978 delete BuildLogFile;
Chris Lattner4b009652007-07-25 00:24:17 +00001979
Daniel Dunbar8d4dff52008-10-27 22:10:13 +00001980 // If verifying diagnostics and we reached here, all is well.
1981 if (VerifyDiagnostics)
1982 return 0;
Chris Lattnerefe33382009-02-18 01:51:21 +00001983
Daniel Dunbarbb298c02008-10-28 00:38:08 +00001984 // Managed static deconstruction. Useful for making things like
1985 // -time-passes usable.
1986 llvm::llvm_shutdown();
1987
Daniel Dunbar70a66b12008-10-04 23:42:49 +00001988 return HadErrors || (Diags.getNumErrors() != 0);
Chris Lattner4b009652007-07-25 00:24:17 +00001989}