blob: 2177fcdef91c6662c153531716fee7e6d1105b63 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- clang.cpp - C-Language Front-end ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This utility may be invoked in the following manner:
11// clang --help - Output help info.
12// clang [options] - Read from stdin.
13// clang [options] file - Read from "file".
14// clang [options] file1 file2 - Read these files.
15//
16//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000017
Daniel Dunbar0498cfc2009-11-10 19:51:53 +000018#include "Options.h"
Eli Friedman0ec78fa2009-05-19 21:10:40 +000019#include "clang/Frontend/AnalysisConsumer.h"
Eli Friedman8ceb0d92009-05-18 23:02:01 +000020#include "clang/Frontend/ASTConsumers.h"
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +000021#include "clang/Frontend/ASTUnit.h"
Daniel Dunbardbf75fe2009-11-11 08:13:24 +000022#include "clang/Frontend/ChainedDiagnosticClient.h"
Daniel Dunbare29709f2009-11-09 20:55:08 +000023#include "clang/Frontend/CompilerInvocation.h"
Douglas Gregor558cb562009-04-02 01:08:08 +000024#include "clang/Frontend/FixItRewriter.h"
Daniel Dunbar50f4f462009-03-12 10:14:16 +000025#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/Frontend/PCHReader.h"
Daniel Dunbar8863b982009-11-07 04:20:15 +000027#include "clang/Frontend/PathDiagnosticClients.h"
28#include "clang/Frontend/PreprocessorOptions.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000029#include "clang/Frontend/TextDiagnosticBuffer.h"
30#include "clang/Frontend/TextDiagnosticPrinter.h"
Argyrios Kyrtzidis34d25d82009-06-23 22:01:39 +000031#include "clang/Frontend/CommandLineSourceLoc.h"
Eli Friedmanb09f6e12009-05-19 04:14:29 +000032#include "clang/Frontend/Utils.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000033#include "clang/Analysis/PathDiagnostic.h"
Chris Lattner8ee3c032008-02-06 02:01:47 +000034#include "clang/CodeGen/ModuleBuilder.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000035#include "clang/Sema/CodeCompleteConsumer.h"
Chris Lattnere91c1342008-02-06 00:23:21 +000036#include "clang/Sema/ParseAST.h"
Chris Lattner88eccaf2009-01-29 06:55:46 +000037#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner556beb72007-09-15 22:56:56 +000038#include "clang/AST/ASTConsumer.h"
Chris Lattner1266eca2009-03-28 04:31:31 +000039#include "clang/AST/ASTContext.h"
40#include "clang/AST/Decl.h"
Chris Lattner682bf922009-03-29 16:50:03 +000041#include "clang/AST/DeclGroup.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000042#include "clang/Parse/Parser.h"
43#include "clang/Lex/HeaderSearch.h"
Chris Lattnerdb766842009-02-06 04:16:41 +000044#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000045#include "clang/Basic/FileManager.h"
46#include "clang/Basic/SourceManager.h"
47#include "clang/Basic/TargetInfo.h"
Daniel Dunbaraa338bc2009-05-06 04:07:06 +000048#include "clang/Basic/Version.h"
Owen Anderson42253cc2009-07-01 17:00:06 +000049#include "llvm/LLVMContext.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000050#include "llvm/ADT/OwningPtr.h"
Chris Lattner8f3dab82007-12-15 23:20:07 +000051#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000052#include "llvm/ADT/StringExtras.h"
Daniel Dunbar868bd0a2009-05-06 03:16:41 +000053#include "llvm/ADT/StringMap.h"
Daniel Dunbar227b2382009-11-09 22:45:57 +000054#include "llvm/ADT/StringSwitch.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000055#include "llvm/Config/config.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000056#include "llvm/Support/CommandLine.h"
Daniel Dunbar70121eb2009-08-10 03:40:28 +000057#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar524b86f2008-10-28 00:38:08 +000058#include "llvm/Support/ManagedStatic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000059#include "llvm/Support/MemoryBuffer.h"
Zhongxing Xu20922362008-11-26 05:23:17 +000060#include "llvm/Support/PluginLoader.h"
Chris Lattner09e94a32009-03-04 21:41:39 +000061#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner47099742009-02-18 01:51:21 +000062#include "llvm/Support/Timer.h"
Chris Lattner0fa0daa2009-08-24 04:11:30 +000063#include "llvm/Support/raw_ostream.h"
Daniel Dunbare553a722008-10-02 01:21:33 +000064#include "llvm/System/Host.h"
Chris Lattnerdcaa0962008-03-03 03:16:03 +000065#include "llvm/System/Path.h"
Eli Friedman66d6f042009-05-18 22:20:00 +000066#include "llvm/System/Program.h"
Chris Lattnerba0f25f2008-09-30 20:16:56 +000067#include "llvm/System/Signals.h"
Chris Lattner2fe11942009-06-17 17:25:50 +000068#include "llvm/Target/TargetSelect.h"
Douglas Gregor26df2f02009-04-02 19:05:20 +000069#include <cstdlib>
Douglas Gregor44cf08e2009-05-03 03:52:38 +000070#if HAVE_SYS_TYPES_H
Douglas Gregor68a0d782009-05-02 00:03:46 +000071# include <sys/types.h>
72#endif
Douglas Gregor26df2f02009-04-02 19:05:20 +000073
Reid Spencer5f016e22007-07-11 17:01:13 +000074using namespace clang;
75
76//===----------------------------------------------------------------------===//
Douglas Gregor26df2f02009-04-02 19:05:20 +000077// Source Location Parser
78//===----------------------------------------------------------------------===//
79
Argyrios Kyrtzidis34d25d82009-06-23 22:01:39 +000080static bool ResolveParsedLocation(ParsedSourceLocation &ParsedLoc,
81 FileManager &FileMgr,
82 RequestedSourceLocation &Result) {
83 const FileEntry *File = FileMgr.getFile(ParsedLoc.FileName);
Douglas Gregor26df2f02009-04-02 19:05:20 +000084 if (!File)
85 return true;
86
87 Result.File = File;
Argyrios Kyrtzidis34d25d82009-06-23 22:01:39 +000088 Result.Line = ParsedLoc.Line;
89 Result.Column = ParsedLoc.Column;
Douglas Gregor26df2f02009-04-02 19:05:20 +000090 return false;
91}
92
Douglas Gregor26df2f02009-04-02 19:05:20 +000093//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000094// Global options.
95//===----------------------------------------------------------------------===//
96
Chris Lattner47099742009-02-18 01:51:21 +000097/// ClangFrontendTimer - The front-end activities should charge time to it with
98/// TimeRegion. The -ftime-report option controls whether this will do
99/// anything.
100llvm::Timer *ClangFrontendTimer = 0;
101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102static llvm::cl::opt<bool>
103Verbose("v", llvm::cl::desc("Enable verbose output"));
104static llvm::cl::opt<bool>
Mike Stump1eb44332009-09-09 15:08:12 +0000105Stats("print-stats",
Nate Begemanaabbb122007-12-30 01:38:50 +0000106 llvm::cl::desc("Print performance metrics and statistics"));
Daniel Dunbard3db4012008-10-16 16:54:18 +0000107static llvm::cl::opt<bool>
108DisableFree("disable-free",
109 llvm::cl::desc("Disable freeing of memory on exit"),
110 llvm::cl::init(false));
Daniel Dunbar57cbfc02009-04-27 21:19:07 +0000111static llvm::cl::opt<bool>
Mike Stump1eb44332009-09-09 15:08:12 +0000112EmptyInputOnly("empty-input-only",
Daniel Dunbar57cbfc02009-04-27 21:19:07 +0000113 llvm::cl::desc("Force running on an empty input file"));
Reid Spencer5f016e22007-07-11 17:01:13 +0000114
115enum ProgActions {
Steve Naroffb29b4272008-04-14 22:03:09 +0000116 RewriteObjC, // ObjC->C Rewriter.
Steve Naroff13188952008-09-18 14:10:13 +0000117 RewriteBlocks, // ObjC->C Rewriter for Blocks.
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000118 RewriteMacros, // Expand macros but not #includes.
Chris Lattnerb13c5ee2008-10-12 05:29:20 +0000119 RewriteTest, // Rewriter playground
Douglas Gregor558cb562009-04-02 01:08:08 +0000120 FixIt, // Fix-It Rewriter
Ted Kremenek13e479b2008-03-19 07:53:42 +0000121 HTMLTest, // HTML displayer testing stuff.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000122 EmitAssembly, // Emit a .s file.
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 EmitLLVM, // Emit a .ll file.
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000124 EmitBC, // Emit a .bc file.
Mike Stump1eb44332009-09-09 15:08:12 +0000125 EmitLLVMOnly, // Generate LLVM IR, but do not
Ted Kremenek6a340832008-03-18 21:19:49 +0000126 EmitHTML, // Translate input source into HTML.
Chris Lattner3b427b32007-10-11 00:18:28 +0000127 ASTPrint, // Parse ASTs and print them.
Douglas Gregoree75c052009-05-21 20:55:50 +0000128 ASTPrintXML, // Parse ASTs and print them in XML.
Chris Lattner3b427b32007-10-11 00:18:28 +0000129 ASTDump, // Parse ASTs and dump them.
130 ASTView, // Parse ASTs and view them in Graphviz.
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000131 PrintDeclContext, // Print DeclContext and their Decls.
Anders Carlsson78762eb2009-09-24 18:54:49 +0000132 DumpRecordLayouts, // Dump record layout information.
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 ParsePrintCallbacks, // Parse and print each callback.
134 ParseSyntaxOnly, // Parse and perform semantic analysis.
135 ParseNoop, // Parse with noop callbacks.
136 RunPreprocessorOnly, // Just lex, no output.
137 PrintPreprocessedInput, // -E mode.
Chris Lattnerc106c102008-10-12 05:03:36 +0000138 DumpTokens, // Dump out preprocessed tokens.
139 DumpRawTokens, // Dump out raw tokens.
Mike Stump1eb44332009-09-09 15:08:12 +0000140 RunAnalysis, // Run one or more source code analyses.
Douglas Gregorbf1bd6e2009-04-02 23:43:50 +0000141 GeneratePTH, // Generate pre-tokenized header.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 GeneratePCH, // Generate pre-compiled header.
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000143 InheritanceView // View C++ inheritance for a specified class.
Reid Spencer5f016e22007-07-11 17:01:13 +0000144};
145
Mike Stump1eb44332009-09-09 15:08:12 +0000146static llvm::cl::opt<ProgActions>
Reid Spencer5f016e22007-07-11 17:01:13 +0000147ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
148 llvm::cl::init(ParseSyntaxOnly),
149 llvm::cl::values(
150 clEnumValN(RunPreprocessorOnly, "Eonly",
151 "Just run preprocessor, no output (for timings)"),
152 clEnumValN(PrintPreprocessedInput, "E",
153 "Run preprocessor, emit preprocessed file"),
Chris Lattnerc106c102008-10-12 05:03:36 +0000154 clEnumValN(DumpRawTokens, "dump-raw-tokens",
155 "Lex file in raw mode and dump raw tokens"),
Daniel Dunbard4270232009-01-20 23:17:32 +0000156 clEnumValN(RunAnalysis, "analyze",
157 "Run static analysis engine"),
Chris Lattnerc106c102008-10-12 05:03:36 +0000158 clEnumValN(DumpTokens, "dump-tokens",
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 "Run preprocessor, dump internal rep of tokens"),
160 clEnumValN(ParseNoop, "parse-noop",
161 "Run parser with noop callbacks (for timings)"),
162 clEnumValN(ParseSyntaxOnly, "fsyntax-only",
163 "Run parser and perform semantic analysis"),
164 clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
165 "Run parser and print each callback invoked"),
Ted Kremenek6a340832008-03-18 21:19:49 +0000166 clEnumValN(EmitHTML, "emit-html",
167 "Output input source as HTML"),
Chris Lattner3b427b32007-10-11 00:18:28 +0000168 clEnumValN(ASTPrint, "ast-print",
169 "Build ASTs and then pretty-print them"),
Douglas Gregoree75c052009-05-21 20:55:50 +0000170 clEnumValN(ASTPrintXML, "ast-print-xml",
171 "Build ASTs and then print them in XML format"),
Chris Lattner3b427b32007-10-11 00:18:28 +0000172 clEnumValN(ASTDump, "ast-dump",
173 "Build ASTs and then debug dump them"),
Chris Lattnerea254db2007-10-11 00:37:43 +0000174 clEnumValN(ASTView, "ast-view",
Sanjiv Gupta56cf96b2008-05-08 08:28:14 +0000175 "Build ASTs and view them with GraphViz"),
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000176 clEnumValN(PrintDeclContext, "print-decl-contexts",
Ted Kremenek08478eb2009-04-01 00:23:28 +0000177 "Print DeclContexts and their Decls"),
Anders Carlsson78762eb2009-09-24 18:54:49 +0000178 clEnumValN(DumpRecordLayouts, "dump-record-layouts",
179 "Dump record layout information"),
Douglas Gregorbf1bd6e2009-04-02 23:43:50 +0000180 clEnumValN(GeneratePTH, "emit-pth",
Ted Kremenek08478eb2009-04-01 00:23:28 +0000181 "Generate pre-tokenized header file"),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182 clEnumValN(GeneratePCH, "emit-pch",
183 "Generate pre-compiled header file"),
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000184 clEnumValN(EmitAssembly, "S",
185 "Emit native assembly code"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 clEnumValN(EmitLLVM, "emit-llvm",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000187 "Build ASTs then convert to LLVM, emit .ll file"),
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000188 clEnumValN(EmitBC, "emit-llvm-bc",
189 "Build ASTs then convert to LLVM, emit .bc file"),
Daniel Dunbare8e26002009-02-26 22:39:37 +0000190 clEnumValN(EmitLLVMOnly, "emit-llvm-only",
191 "Build ASTs and convert to LLVM, discarding output"),
Chris Lattnerb13c5ee2008-10-12 05:29:20 +0000192 clEnumValN(RewriteTest, "rewrite-test",
193 "Rewriter playground"),
Steve Naroffb29b4272008-04-14 22:03:09 +0000194 clEnumValN(RewriteObjC, "rewrite-objc",
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000195 "Rewrite ObjC into C (code rewriter example)"),
196 clEnumValN(RewriteMacros, "rewrite-macros",
197 "Expand macros without full preprocessing"),
Steve Naroff13188952008-09-18 14:10:13 +0000198 clEnumValN(RewriteBlocks, "rewrite-blocks",
199 "Rewrite Blocks to C"),
Douglas Gregor558cb562009-04-02 01:08:08 +0000200 clEnumValN(FixIt, "fixit",
201 "Apply fix-it advice to the input source"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 clEnumValEnd));
203
Ted Kremenekccc76472007-12-19 19:47:59 +0000204
205static llvm::cl::opt<std::string>
206OutputFile("o",
Ted Kremenek50b56412007-12-19 19:50:41 +0000207 llvm::cl::value_desc("path"),
Douglas Gregor370187c2009-04-22 21:45:53 +0000208 llvm::cl::desc("Specify output file"));
Ted Kremenek55af98c2008-04-14 18:40:58 +0000209
Ted Kremenekc2e72992008-12-02 19:57:31 +0000210
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000211enum CodeCompletionPrinter {
212 CCP_Debug,
213 CCP_CIndex
214};
215
Douglas Gregorb657f112009-09-22 21:11:38 +0000216static llvm::cl::opt<ParsedSourceLocation>
217CodeCompletionAt("code-completion-at",
218 llvm::cl::value_desc("file:line:column"),
219 llvm::cl::desc("Dump code-completion information at a location"));
Douglas Gregor81b747b2009-09-17 21:32:03 +0000220
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000221static llvm::cl::opt<CodeCompletionPrinter>
222CodeCompletionPrinter("code-completion-printer",
223 llvm::cl::desc("Choose output type:"),
224 llvm::cl::init(CCP_Debug),
225 llvm::cl::values(
226 clEnumValN(CCP_Debug, "debug",
227 "Debug code-completion results"),
228 clEnumValN(CCP_CIndex, "cindex",
229 "Code-completion results for the CIndex library"),
230 clEnumValEnd));
231
232static llvm::cl::opt<bool>
233CodeCompletionWantsMacros("code-completion-macros",
234 llvm::cl::desc("Include macros in code-completion results"));
235
Douglas Gregor81b747b2009-09-17 21:32:03 +0000236/// \brief Buld a new code-completion consumer that prints the results of
237/// code completion to standard output.
238static CodeCompleteConsumer *BuildPrintingCodeCompleter(Sema &S, void *) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000239 switch (CodeCompletionPrinter.getValue()) {
240 case CCP_Debug:
241 return new PrintingCodeCompleteConsumer(S, CodeCompletionWantsMacros,
242 llvm::outs());
243
244 case CCP_CIndex:
245 return new CIndexCodeCompleteConsumer(S, CodeCompletionWantsMacros,
246 llvm::outs());
247 };
248
249 return 0;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000250}
251
Ted Kremenekc2e72992008-12-02 19:57:31 +0000252//===----------------------------------------------------------------------===//
253// PTH.
254//===----------------------------------------------------------------------===//
255
256static llvm::cl::opt<std::string>
257TokenCache("token-cache", llvm::cl::value_desc("path"),
258 llvm::cl::desc("Use specified token cache file"));
259
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000260//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000261// C++ Visualization.
262//===----------------------------------------------------------------------===//
263
264static llvm::cl::opt<std::string>
265InheritanceViewCls("cxx-inheritance-view",
266 llvm::cl::value_desc("class name"),
Daniel Dunbard77b2512009-01-14 18:56:36 +0000267 llvm::cl::desc("View C++ inheritance for a specified class"));
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000268
269//===----------------------------------------------------------------------===//
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000270// Frontend Options
Douglas Gregor3573c0c2009-02-14 20:49:29 +0000271//===----------------------------------------------------------------------===//
Chris Lattnerb2509e12009-02-18 01:12:43 +0000272
273static llvm::cl::opt<bool>
274TimeReport("ftime-report",
275 llvm::cl::desc("Print the amount of time each "
276 "phase of compilation takes"));
277
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000278static llvm::cl::opt<bool>
279VerifyDiagnostics("verify",
280 llvm::cl::desc("Verify emitted diagnostics and warnings"));
281
Douglas Gregor3573c0c2009-02-14 20:49:29 +0000282//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000283// Language Options
284//===----------------------------------------------------------------------===//
285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286static llvm::cl::opt<LangKind>
287BaseLang("x", llvm::cl::desc("Base language to compile"),
288 llvm::cl::init(langkind_unspecified),
289 llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
Nate Begeman4e3629e2009-06-25 22:43:10 +0000290 clEnumValN(langkind_ocl, "cl", "OpenCL C"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 clEnumValN(langkind_cxx, "c++", "C++"),
292 clEnumValN(langkind_objc, "objective-c", "Objective C"),
293 clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
Daniel Dunbard2ea3862009-01-29 23:50:47 +0000294 clEnumValN(langkind_c_cpp, "cpp-output",
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 "Preprocessed C"),
Chris Lattnera778d7d2008-10-22 17:29:21 +0000296 clEnumValN(langkind_asm_cpp, "assembler-with-cpp",
297 "Preprocessed asm"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
Chris Lattnerc76d8072009-02-06 06:19:20 +0000299 "Preprocessed C++"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
301 "Preprocessed Objective C"),
Chris Lattnerc76d8072009-02-06 06:19:20 +0000302 clEnumValN(langkind_objcxx_cpp, "objective-c++-cpp-output",
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 "Preprocessed Objective C++"),
Daniel Dunbar0b5b0da2009-04-01 05:09:09 +0000304 clEnumValN(langkind_c, "c-header",
305 "C header"),
306 clEnumValN(langkind_objc, "objective-c-header",
307 "Objective-C header"),
308 clEnumValN(langkind_cxx, "c++-header",
309 "C++ header"),
310 clEnumValN(langkind_objcxx, "objective-c++-header",
311 "Objective-C++ header"),
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +0000312 clEnumValN(langkind_ast, "ast",
313 "Clang AST"),
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 clEnumValEnd));
315
Daniel Dunbar7c15e712009-10-30 18:12:31 +0000316static llvm::cl::opt<std::string>
317TargetTriple("triple",
318 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
319
Daniel Dunbar56749082009-11-11 07:26:12 +0000320static llvm::cl::opt<std::string>
321TargetABI("target-abi",
322 llvm::cl::desc("Target a particular ABI type"));
Reid Spencer5f016e22007-07-11 17:01:13 +0000323
324//===----------------------------------------------------------------------===//
Chris Lattnere116ccf2009-04-21 05:40:52 +0000325// SourceManager initialization.
Reid Spencer5f016e22007-07-11 17:01:13 +0000326//===----------------------------------------------------------------------===//
327
Douglas Gregore1d918e2009-04-10 23:10:45 +0000328static bool InitializeSourceManager(Preprocessor &PP,
329 const std::string &InFile) {
330 // Figure out where to get and map in the main file.
331 SourceManager &SourceMgr = PP.getSourceManager();
332 FileManager &FileMgr = PP.getFileManager();
Daniel Dunbar57cbfc02009-04-27 21:19:07 +0000333
334 if (EmptyInputOnly) {
335 const char *EmptyStr = "";
Mike Stump1eb44332009-09-09 15:08:12 +0000336 llvm::MemoryBuffer *SB =
Daniel Dunbar57cbfc02009-04-27 21:19:07 +0000337 llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<empty input>");
338 SourceMgr.createMainFileIDForMemBuffer(SB);
339 } else if (InFile != "-") {
Douglas Gregore1d918e2009-04-10 23:10:45 +0000340 const FileEntry *File = FileMgr.getFile(InFile);
341 if (File) SourceMgr.createMainFileID(File, SourceLocation());
342 if (SourceMgr.getMainFileID().isInvalid()) {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +0000343 PP.getDiagnostics().Report(diag::err_fe_error_reading) << InFile.c_str();
Douglas Gregore1d918e2009-04-10 23:10:45 +0000344 return true;
345 }
346 } else {
347 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
Douglas Gregore1d918e2009-04-10 23:10:45 +0000348 SourceMgr.createMainFileIDForMemBuffer(SB);
349 if (SourceMgr.getMainFileID().isInvalid()) {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +0000350 PP.getDiagnostics().Report(diag::err_fe_error_reading_stdin);
Douglas Gregore1d918e2009-04-10 23:10:45 +0000351 return true;
352 }
353 }
354
355 return false;
356}
357
Chris Lattneraa391972008-04-19 23:09:31 +0000358
Chris Lattnere116ccf2009-04-21 05:40:52 +0000359//===----------------------------------------------------------------------===//
360// Preprocessor Initialization
361//===----------------------------------------------------------------------===//
Sam Bishop1102d6b2008-04-14 14:41:57 +0000362
Chris Lattnere6113de2009-11-03 19:50:27 +0000363static llvm::cl::opt<bool>
Mike Stump1eb44332009-09-09 15:08:12 +0000364RelocatablePCH("relocatable-pch",
Douglas Gregore650c8c2009-07-07 00:12:59 +0000365 llvm::cl::desc("Whether to build a relocatable precompiled "
366 "header"));
Reid Spencer5f016e22007-07-11 17:01:13 +0000367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368// Finally, implement the code that groks the options above.
Chris Lattner5f9eae52008-03-01 08:07:28 +0000369
Rafael Espindola1bb15a92009-10-05 13:12:17 +0000370// Add the clang headers, which are relative to the clang binary.
Daniel Dunbar750156a2009-11-07 04:19:57 +0000371std::string GetBuiltinIncludePath(const char *Argv0) {
372 llvm::sys::Path P =
373 llvm::sys::Path::GetMainExecutable(Argv0,
374 (void*)(intptr_t) GetBuiltinIncludePath);
Rafael Espindola1bb15a92009-10-05 13:12:17 +0000375
Daniel Dunbar750156a2009-11-07 04:19:57 +0000376 if (!P.isEmpty()) {
377 P.eraseComponent(); // Remove /clang from foo/bin/clang
378 P.eraseComponent(); // Remove /bin from foo/bin
Rafael Espindola1bb15a92009-10-05 13:12:17 +0000379
Daniel Dunbar750156a2009-11-07 04:19:57 +0000380 // Get foo/lib/clang/<version>/include
381 P.appendComponent("lib");
382 P.appendComponent("clang");
383 P.appendComponent(CLANG_VERSION_STRING);
384 P.appendComponent("include");
385 }
Rafael Espindola1bb15a92009-10-05 13:12:17 +0000386
Daniel Dunbar750156a2009-11-07 04:19:57 +0000387 return P.str();
Rafael Espindola1bb15a92009-10-05 13:12:17 +0000388}
389
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000390//===----------------------------------------------------------------------===//
Daniel Dunbar90b18272009-11-04 23:56:25 +0000391// Preprocessor construction
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000392//===----------------------------------------------------------------------===//
393
Daniel Dunbar90b18272009-11-04 23:56:25 +0000394static Preprocessor *
Daniel Dunbar5fc7d342009-11-09 23:12:31 +0000395CreatePreprocessor(Diagnostic &Diags, const LangOptions &LangInfo,
396 const PreprocessorOptions &PPOpts, TargetInfo &Target,
397 SourceManager &SourceMgr, HeaderSearch &HeaderInfo) {
Daniel Dunbar90b18272009-11-04 23:56:25 +0000398 PTHManager *PTHMgr = 0;
Daniel Dunbare0a95812009-11-10 22:09:38 +0000399 if (!TokenCache.empty() && !PPOpts.getImplicitPTHInclude().empty()) {
Daniel Dunbar90b18272009-11-04 23:56:25 +0000400 fprintf(stderr, "error: cannot use both -token-cache and -include-pth "
401 "options\n");
402 exit(1);
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000403 }
Daniel Dunbar90b18272009-11-04 23:56:25 +0000404
405 // Use PTH?
Daniel Dunbare0a95812009-11-10 22:09:38 +0000406 if (!TokenCache.empty() || !PPOpts.getImplicitPTHInclude().empty()) {
407 const std::string& x = TokenCache.empty() ?
408 PPOpts.getImplicitPTHInclude() : TokenCache;
Daniel Dunbar90b18272009-11-04 23:56:25 +0000409 PTHMgr = PTHManager::Create(x, &Diags,
410 TokenCache.empty() ? Diagnostic::Error
411 : Diagnostic::Warning);
412 }
413
414 if (Diags.hasErrorOccurred())
415 exit(1);
416
417 // Create the Preprocessor.
418 Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
419 SourceMgr, HeaderInfo, PTHMgr);
420
421 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
422 // That argument is used as the IdentifierInfoLookup argument to
423 // IdentifierTable's ctor.
424 if (PTHMgr) {
425 PTHMgr->setPreprocessor(PP);
426 PP->setPTHManager(PTHMgr);
427 }
428
Daniel Dunbar5fc7d342009-11-09 23:12:31 +0000429 InitializePreprocessor(*PP, PPOpts);
Daniel Dunbar90b18272009-11-04 23:56:25 +0000430
431 return PP;
Ted Kremeneka42cf2e2008-04-17 21:38:34 +0000432}
Reid Spencer5f016e22007-07-11 17:01:13 +0000433
Reid Spencer5f016e22007-07-11 17:01:13 +0000434//===----------------------------------------------------------------------===//
435// Basic Parser driver
436//===----------------------------------------------------------------------===//
437
Chris Lattner51574ea2008-04-19 23:25:44 +0000438static void ParseFile(Preprocessor &PP, MinimalAction *PA) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 Parser P(PP, *PA);
Ted Kremenek95041a22007-12-19 22:51:13 +0000440 PP.EnterMainSourceFile();
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 // Parsing the specified input file.
443 P.ParseTranslationUnit();
444 delete PA;
445}
446
447//===----------------------------------------------------------------------===//
Douglas Gregor26df2f02009-04-02 19:05:20 +0000448// Fix-It Options
449//===----------------------------------------------------------------------===//
450static llvm::cl::list<ParsedSourceLocation>
451FixItAtLocations("fixit-at", llvm::cl::value_desc("source-location"),
452 llvm::cl::desc("Perform Fix-It modifications at the given source location"));
453
454//===----------------------------------------------------------------------===//
Eli Friedmanc6d656e2009-05-18 22:39:16 +0000455// ObjC Rewriter Options
456//===----------------------------------------------------------------------===//
457static llvm::cl::opt<bool>
458SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false),
459 llvm::cl::desc("Silence ObjC rewriting warnings"));
460
461//===----------------------------------------------------------------------===//
Eli Friedman0eeb86e2009-05-19 01:17:04 +0000462// Warning Options
463//===----------------------------------------------------------------------===//
464
465// This gets all -W options, including -Werror, -W[no-]system-headers, etc. The
466// driver has stripped off -Wa,foo etc. The driver has also translated -W to
467// -Wextra, so we don't need to worry about it.
468static llvm::cl::list<std::string>
469OptWarnings("W", llvm::cl::Prefix, llvm::cl::ValueOptional);
470
471static llvm::cl::opt<bool> OptPedantic("pedantic");
472static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors");
473static llvm::cl::opt<bool> OptNoWarnings("w");
474
475//===----------------------------------------------------------------------===//
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000476// Preprocessing (-E mode) Options
477//===----------------------------------------------------------------------===//
478static llvm::cl::opt<bool>
479DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
480static llvm::cl::opt<bool>
481EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
482static llvm::cl::opt<bool>
483EnableMacroCommentOutput("CC",
484 llvm::cl::desc("Enable comment output in -E mode, "
485 "even from macro expansions"));
486static llvm::cl::opt<bool>
487DumpMacros("dM", llvm::cl::desc("Print macro definitions in -E mode instead of"
488 " normal output"));
489static llvm::cl::opt<bool>
490DumpDefines("dD", llvm::cl::desc("Print macro definitions in -E mode in "
491 "addition to normal output"));
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000492
493//===----------------------------------------------------------------------===//
494// Dependency file options
495//===----------------------------------------------------------------------===//
496static llvm::cl::opt<std::string>
497DependencyFile("dependency-file",
498 llvm::cl::desc("Filename (or -) to write dependency output to"));
499
500static llvm::cl::opt<bool>
501DependenciesIncludeSystemHeaders("sys-header-deps",
502 llvm::cl::desc("Include system headers in dependency output"));
503
504static llvm::cl::list<std::string>
505DependencyTargets("MT",
506 llvm::cl::desc("Specify target for dependency"));
507
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000508static llvm::cl::opt<bool>
509PhonyDependencyTarget("MP",
510 llvm::cl::desc("Create phony target for each dependency "
511 "(other than main file)"));
512
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000513//===----------------------------------------------------------------------===//
Eli Friedmane71b85f2009-05-19 10:18:02 +0000514// Analysis options
515//===----------------------------------------------------------------------===//
516
517static llvm::cl::list<Analyses>
518AnalysisList(llvm::cl::desc("Source Code Analysis - Checks and Analyses"),
519llvm::cl::values(
520#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
521clEnumValN(NAME, CMDFLAG, DESC),
Eli Friedman0ec78fa2009-05-19 21:10:40 +0000522#include "clang/Frontend/Analyses.def"
Eli Friedmane71b85f2009-05-19 10:18:02 +0000523clEnumValEnd));
524
Mike Stump1eb44332009-09-09 15:08:12 +0000525static llvm::cl::opt<AnalysisStores>
Eli Friedmane71b85f2009-05-19 10:18:02 +0000526AnalysisStoreOpt("analyzer-store",
527 llvm::cl::desc("Source Code Analysis - Abstract Memory Store Models"),
528 llvm::cl::init(BasicStoreModel),
529 llvm::cl::values(
530#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN)\
531clEnumValN(NAME##Model, CMDFLAG, DESC),
Eli Friedman0ec78fa2009-05-19 21:10:40 +0000532#include "clang/Frontend/Analyses.def"
Eli Friedmane71b85f2009-05-19 10:18:02 +0000533clEnumValEnd));
534
Mike Stump1eb44332009-09-09 15:08:12 +0000535static llvm::cl::opt<AnalysisConstraints>
Eli Friedmane71b85f2009-05-19 10:18:02 +0000536AnalysisConstraintsOpt("analyzer-constraints",
537 llvm::cl::desc("Source Code Analysis - Symbolic Constraint Engines"),
538 llvm::cl::init(RangeConstraintsModel),
539 llvm::cl::values(
540#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN)\
541clEnumValN(NAME##Model, CMDFLAG, DESC),
Eli Friedman0ec78fa2009-05-19 21:10:40 +0000542#include "clang/Frontend/Analyses.def"
Eli Friedmane71b85f2009-05-19 10:18:02 +0000543clEnumValEnd));
544
545static llvm::cl::opt<AnalysisDiagClients>
546AnalysisDiagOpt("analyzer-output",
547 llvm::cl::desc("Source Code Analysis - Output Options"),
548 llvm::cl::init(PD_HTML),
549 llvm::cl::values(
550#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE)\
551clEnumValN(PD_##NAME, CMDFLAG, DESC),
Eli Friedman0ec78fa2009-05-19 21:10:40 +0000552#include "clang/Frontend/Analyses.def"
Eli Friedmane71b85f2009-05-19 10:18:02 +0000553clEnumValEnd));
554
555static llvm::cl::opt<bool>
556VisualizeEGDot("analyzer-viz-egraph-graphviz",
557 llvm::cl::desc("Display exploded graph using GraphViz"));
558
559static llvm::cl::opt<bool>
560VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
561 llvm::cl::desc("Display exploded graph using Ubigraph"));
562
563static llvm::cl::opt<bool>
564AnalyzeAll("analyzer-opt-analyze-headers",
565 llvm::cl::desc("Force the static analyzer to analyze "
566 "functions defined in header files"));
567
568static llvm::cl::opt<bool>
569AnalyzerDisplayProgress("analyzer-display-progress",
570 llvm::cl::desc("Emit verbose output about the analyzer's progress."));
571
572static llvm::cl::opt<bool>
573PurgeDead("analyzer-purge-dead",
574 llvm::cl::init(true),
575 llvm::cl::desc("Remove dead symbols, bindings, and constraints before"
576 " processing a statement."));
577
578static llvm::cl::opt<bool>
579EagerlyAssume("analyzer-eagerly-assume",
580 llvm::cl::init(false),
581 llvm::cl::desc("Eagerly assume the truth/falseness of some "
582 "symbolic constraints."));
583
584static llvm::cl::opt<std::string>
585AnalyzeSpecificFunction("analyze-function",
586 llvm::cl::desc("Run analysis on specific function"));
587
588static llvm::cl::opt<bool>
589TrimGraph("trim-egraph",
590 llvm::cl::desc("Only show error-related paths in the analysis graph"));
591
592static AnalyzerOptions ReadAnalyzerOptions() {
593 AnalyzerOptions Opts;
594 Opts.AnalysisList = AnalysisList;
595 Opts.AnalysisStoreOpt = AnalysisStoreOpt;
596 Opts.AnalysisConstraintsOpt = AnalysisConstraintsOpt;
597 Opts.AnalysisDiagOpt = AnalysisDiagOpt;
598 Opts.VisualizeEGDot = VisualizeEGDot;
599 Opts.VisualizeEGUbi = VisualizeEGUbi;
600 Opts.AnalyzeAll = AnalyzeAll;
601 Opts.AnalyzerDisplayProgress = AnalyzerDisplayProgress;
602 Opts.PurgeDead = PurgeDead;
603 Opts.EagerlyAssume = EagerlyAssume;
604 Opts.AnalyzeSpecificFunction = AnalyzeSpecificFunction;
605 Opts.TrimGraph = TrimGraph;
606 return Opts;
607}
608
609//===----------------------------------------------------------------------===//
Chris Lattner75a97cb2009-04-17 21:05:01 +0000610// -dump-build-information Stuff
611//===----------------------------------------------------------------------===//
612
613static llvm::cl::opt<std::string>
614DumpBuildInformation("dump-build-information",
615 llvm::cl::value_desc("filename"),
616 llvm::cl::desc("output a dump of some build information to a file"));
617
618static llvm::raw_ostream *BuildLogFile = 0;
619
Daniel Dunbar227b2382009-11-09 22:45:57 +0000620static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
Daniel Dunbare29709f2009-11-09 20:55:08 +0000621 unsigned argc, char **argv,
Chris Lattner75a97cb2009-04-17 21:05:01 +0000622 llvm::OwningPtr<DiagnosticClient> &DiagClient) {
Chris Lattner75a97cb2009-04-17 21:05:01 +0000623 std::string ErrorInfo;
Chris Lattner92bcc272009-08-23 02:59:41 +0000624 BuildLogFile = new llvm::raw_fd_ostream(DumpBuildInformation.c_str(),
Dan Gohmanb044c472009-08-25 15:36:09 +0000625 ErrorInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Chris Lattner75a97cb2009-04-17 21:05:01 +0000627 if (!ErrorInfo.empty()) {
628 llvm::errs() << "error opening -dump-build-information file '"
629 << DumpBuildInformation << "', option ignored!\n";
630 delete BuildLogFile;
631 BuildLogFile = 0;
632 DumpBuildInformation = "";
633 return;
634 }
635
636 (*BuildLogFile) << "clang-cc command line arguments: ";
637 for (unsigned i = 0; i != argc; ++i)
638 (*BuildLogFile) << argv[i] << ' ';
639 (*BuildLogFile) << '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Daniel Dunbardbf75fe2009-11-11 08:13:24 +0000641 // Chain in a diagnostic client which will log the diagnostics.
642 DiagnosticClient *Logger = new TextDiagnosticPrinter(*BuildLogFile, DiagOpts);
643 DiagClient.reset(new ChainedDiagnosticClient(DiagClient.take(), Logger));
Chris Lattner75a97cb2009-04-17 21:05:01 +0000644}
645
646
647
648//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000649// Main driver
650//===----------------------------------------------------------------------===//
651
Daniel Dunbare29709f2009-11-09 20:55:08 +0000652static llvm::raw_ostream *ComputeOutFile(const CompilerInvocation &CompOpts,
653 const std::string &InFile,
Chris Lattner92bcc272009-08-23 02:59:41 +0000654 const char *Extension,
Eli Friedman66d6f042009-05-18 22:20:00 +0000655 bool Binary,
656 llvm::sys::Path& OutPath) {
Chris Lattner92bcc272009-08-23 02:59:41 +0000657 llvm::raw_ostream *Ret;
Eli Friedman66d6f042009-05-18 22:20:00 +0000658 std::string OutFile;
Daniel Dunbare29709f2009-11-09 20:55:08 +0000659 if (!CompOpts.getOutputFile().empty())
660 OutFile = CompOpts.getOutputFile();
Chris Lattner92bcc272009-08-23 02:59:41 +0000661 else if (InFile == "-") {
662 OutFile = "-";
Eli Friedman66d6f042009-05-18 22:20:00 +0000663 } else if (Extension) {
664 llvm::sys::Path Path(InFile);
665 Path.eraseSuffix();
666 Path.appendSuffix(Extension);
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000667 OutFile = Path.str();
Eli Friedman66d6f042009-05-18 22:20:00 +0000668 } else {
Chris Lattner92bcc272009-08-23 02:59:41 +0000669 OutFile = "-";
Chris Lattner8a5c8092009-02-18 01:20:05 +0000670 }
Seo Sanghyeonfe947ad2007-12-24 01:52:34 +0000671
Chris Lattner92bcc272009-08-23 02:59:41 +0000672 std::string Error;
673 Ret = new llvm::raw_fd_ostream(OutFile.c_str(), Error,
Dan Gohmanb044c472009-08-25 15:36:09 +0000674 (Binary ? llvm::raw_fd_ostream::F_Binary : 0));
Chris Lattner92bcc272009-08-23 02:59:41 +0000675 if (!Error.empty()) {
676 // FIXME: Don't fail this way.
677 llvm::errs() << "ERROR: " << Error << "\n";
678 ::exit(1);
Ted Kremenekdb094a22007-12-05 18:27:04 +0000679 }
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Chris Lattner92bcc272009-08-23 02:59:41 +0000681 if (OutFile != "-")
682 OutPath = OutFile;
Eli Friedman66d6f042009-05-18 22:20:00 +0000683
684 return Ret;
Ted Kremenekdb094a22007-12-05 18:27:04 +0000685}
686
Daniel Dunbare29709f2009-11-09 20:55:08 +0000687static ASTConsumer *CreateConsumerAction(const CompilerInvocation &CompOpts,
688 Preprocessor &PP,
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000689 const std::string &InFile,
690 ProgActions PA,
691 llvm::OwningPtr<llvm::raw_ostream> &OS,
692 llvm::sys::Path &OutPath,
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000693 llvm::LLVMContext& Context) {
Ted Kremenek85888962008-10-21 00:54:44 +0000694 switch (PA) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 default:
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000696 return 0;
Eli Friedman66d6f042009-05-18 22:20:00 +0000697
698 case ASTPrint:
Daniel Dunbare29709f2009-11-09 20:55:08 +0000699 OS.reset(ComputeOutFile(CompOpts, InFile, 0, false, OutPath));
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000700 return CreateASTPrinter(OS.get());
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Douglas Gregoree75c052009-05-21 20:55:50 +0000702 case ASTPrintXML:
Daniel Dunbare29709f2009-11-09 20:55:08 +0000703 OS.reset(ComputeOutFile(CompOpts, InFile, "xml", false, OutPath));
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000704 return CreateASTPrinterXML(OS.get());
Douglas Gregoree75c052009-05-21 20:55:50 +0000705
Eli Friedman66d6f042009-05-18 22:20:00 +0000706 case ASTDump:
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000707 return CreateASTDumper();
Eli Friedman66d6f042009-05-18 22:20:00 +0000708
Eli Friedman66d6f042009-05-18 22:20:00 +0000709 case ASTView:
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000710 return CreateASTViewer();
Eli Friedman66d6f042009-05-18 22:20:00 +0000711
Anders Carlsson78762eb2009-09-24 18:54:49 +0000712 case DumpRecordLayouts:
713 return CreateRecordLayoutDumper();
714
Eli Friedman66d6f042009-05-18 22:20:00 +0000715 case InheritanceView:
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000716 return CreateInheritanceViewer(InheritanceViewCls);
Eli Friedman66d6f042009-05-18 22:20:00 +0000717
718 case EmitAssembly:
719 case EmitLLVM:
Mike Stump1eb44332009-09-09 15:08:12 +0000720 case EmitBC:
Eli Friedman66d6f042009-05-18 22:20:00 +0000721 case EmitLLVMOnly: {
722 BackendAction Act;
723 if (ProgAction == EmitAssembly) {
724 Act = Backend_EmitAssembly;
Daniel Dunbare29709f2009-11-09 20:55:08 +0000725 OS.reset(ComputeOutFile(CompOpts, InFile, "s", true, OutPath));
Eli Friedman66d6f042009-05-18 22:20:00 +0000726 } else if (ProgAction == EmitLLVM) {
727 Act = Backend_EmitLL;
Daniel Dunbare29709f2009-11-09 20:55:08 +0000728 OS.reset(ComputeOutFile(CompOpts, InFile, "ll", true, OutPath));
Eli Friedman66d6f042009-05-18 22:20:00 +0000729 } else if (ProgAction == EmitLLVMOnly) {
730 Act = Backend_EmitNothing;
731 } else {
732 Act = Backend_EmitBC;
Daniel Dunbare29709f2009-11-09 20:55:08 +0000733 OS.reset(ComputeOutFile(CompOpts, InFile, "bc", true, OutPath));
Ted Kremenekdb094a22007-12-05 18:27:04 +0000734 }
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000735
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000736 return CreateBackendConsumer(Act, PP.getDiagnostics(), PP.getLangOptions(),
Daniel Dunbar36f4ec32009-11-10 16:19:45 +0000737 CompOpts.getCompileOpts(), InFile, OS.get(),
738 Context);
Eli Friedman66d6f042009-05-18 22:20:00 +0000739 }
740
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000741 case RewriteObjC:
Daniel Dunbare29709f2009-11-09 20:55:08 +0000742 OS.reset(ComputeOutFile(CompOpts, InFile, "cpp", true, OutPath));
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000743 return CreateObjCRewriter(InFile, OS.get(), PP.getDiagnostics(),
744 PP.getLangOptions(), SilenceRewriteMacroWarning);
745
746 case RewriteBlocks:
747 return CreateBlockRewriter(InFile, PP.getDiagnostics(),
748 PP.getLangOptions());
Daniel Dunbar5ee0aa72009-11-11 00:54:56 +0000749
750 case ParseSyntaxOnly:
751 return new ASTConsumer();
752
753 case PrintDeclContext:
754 return CreateDeclContextPrinter();
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000755 }
756}
757
758/// ProcessInputFile - Process a single input file with the specified state.
759///
Daniel Dunbare29709f2009-11-09 20:55:08 +0000760static void ProcessInputFile(const CompilerInvocation &CompOpts,
761 Preprocessor &PP, const std::string &InFile,
Daniel Dunbar90b18272009-11-04 23:56:25 +0000762 ProgActions PA,
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000763 llvm::LLVMContext& Context) {
764 llvm::OwningPtr<llvm::raw_ostream> OS;
765 llvm::OwningPtr<ASTConsumer> Consumer;
766 bool ClearSourceMgr = false;
767 FixItRewriter *FixItRewrite = 0;
768 bool CompleteTranslationUnit = true;
769 llvm::sys::Path OutPath;
770
771 switch (PA) {
772 default:
Daniel Dunbare29709f2009-11-09 20:55:08 +0000773 Consumer.reset(CreateConsumerAction(CompOpts, PP, InFile, PA, OS, OutPath,
Daniel Dunbar26a0cac2009-11-09 22:46:17 +0000774 Context));
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000775
776 if (!Consumer.get()) {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +0000777 PP.getDiagnostics().Report(diag::err_fe_invalid_ast_action);
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000778 return;
779 }
780
781 break;;
782
783 case EmitHTML:
Daniel Dunbare29709f2009-11-09 20:55:08 +0000784 OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath));
Daniel Dunbar90b18272009-11-04 23:56:25 +0000785 Consumer.reset(CreateHTMLPrinter(OS.get(), PP));
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000786 break;
787
788 case RunAnalysis:
Daniel Dunbare29709f2009-11-09 20:55:08 +0000789 Consumer.reset(CreateAnalysisConsumer(PP, CompOpts.getOutputFile(),
Daniel Dunbar0794d8d2009-09-17 00:48:00 +0000790 ReadAnalyzerOptions()));
791 break;
792
Daniel Dunbarf7973292009-11-11 08:13:32 +0000793 case GeneratePCH: {
794 const std::string &Sysroot = CompOpts.getHeaderSearchOpts().Sysroot;
795 if (RelocatablePCH.getValue() && Sysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000796 PP.Diag(SourceLocation(), diag::err_relocatable_without_without_isysroot);
797 RelocatablePCH.setValue(false);
798 }
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Daniel Dunbare29709f2009-11-09 20:55:08 +0000800 OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000801 if (RelocatablePCH.getValue())
Daniel Dunbarf7973292009-11-11 08:13:32 +0000802 Consumer.reset(CreatePCHGenerator(PP, OS.get(), Sysroot.c_str()));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000803 else
804 Consumer.reset(CreatePCHGenerator(PP, OS.get()));
Eli Friedman66d6f042009-05-18 22:20:00 +0000805 CompleteTranslationUnit = false;
806 break;
Daniel Dunbarf7973292009-11-11 08:13:32 +0000807 }
Chris Lattnerc106c102008-10-12 05:03:36 +0000808 case DumpRawTokens: {
Chris Lattner47099742009-02-18 01:51:21 +0000809 llvm::TimeRegion Timer(ClangFrontendTimer);
Chris Lattnerc106c102008-10-12 05:03:36 +0000810 SourceManager &SM = PP.getSourceManager();
Chris Lattnerc106c102008-10-12 05:03:36 +0000811 // Start lexing the specified input file.
Chris Lattner025c3a62009-01-17 07:35:14 +0000812 Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions());
Chris Lattnerc106c102008-10-12 05:03:36 +0000813 RawLex.SetKeepWhitespaceMode(true);
814
815 Token RawTok;
Chris Lattnerc106c102008-10-12 05:03:36 +0000816 RawLex.LexFromRawLexer(RawTok);
817 while (RawTok.isNot(tok::eof)) {
818 PP.DumpToken(RawTok, true);
819 fprintf(stderr, "\n");
820 RawLex.LexFromRawLexer(RawTok);
821 }
822 ClearSourceMgr = true;
823 break;
824 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 case DumpTokens: { // Token dump mode.
Chris Lattner47099742009-02-18 01:51:21 +0000826 llvm::TimeRegion Timer(ClangFrontendTimer);
Chris Lattnerd2177732007-07-20 16:59:19 +0000827 Token Tok;
Chris Lattnerc106c102008-10-12 05:03:36 +0000828 // Start preprocessing the specified input file.
Ted Kremenek95041a22007-12-19 22:51:13 +0000829 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 do {
831 PP.Lex(Tok);
832 PP.DumpToken(Tok, true);
833 fprintf(stderr, "\n");
Chris Lattner057aaf62007-10-09 18:03:42 +0000834 } while (Tok.isNot(tok::eof));
Chris Lattnerbd247762007-07-22 06:05:44 +0000835 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 break;
837 }
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000838 case RunPreprocessorOnly:
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Douglas Gregorbf1bd6e2009-04-02 23:43:50 +0000841 case GeneratePTH: {
Chris Lattner47099742009-02-18 01:51:21 +0000842 llvm::TimeRegion Timer(ClangFrontendTimer);
Daniel Dunbare29709f2009-11-09 20:55:08 +0000843 if (CompOpts.getOutputFile().empty() || CompOpts.getOutputFile() == "-") {
Eli Friedmanf54fce82009-05-19 01:02:07 +0000844 // FIXME: Don't fail this way.
845 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner92bcc272009-08-23 02:59:41 +0000846 llvm::errs() << "ERROR: PTH requires an seekable file for output!\n";
Eli Friedmanf54fce82009-05-19 01:02:07 +0000847 ::exit(1);
848 }
Daniel Dunbare29709f2009-11-09 20:55:08 +0000849 OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath));
Eli Friedmanf54fce82009-05-19 01:02:07 +0000850 CacheTokens(PP, static_cast<llvm::raw_fd_ostream*>(OS.get()));
Ted Kremenek85888962008-10-21 00:54:44 +0000851 ClearSourceMgr = true;
852 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000853 }
Douglas Gregor6ab35242009-04-09 21:40:53 +0000854
Chris Lattnercc7dea82009-04-27 22:02:30 +0000855 case PrintPreprocessedInput:
Daniel Dunbare29709f2009-11-09 20:55:08 +0000856 OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath));
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000859 case ParseNoop:
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Chris Lattner47099742009-02-18 01:51:21 +0000862 case ParsePrintCallbacks: {
863 llvm::TimeRegion Timer(ClangFrontendTimer);
Daniel Dunbare29709f2009-11-09 20:55:08 +0000864 OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath));
Eli Friedmanf54fce82009-05-19 01:02:07 +0000865 ParseFile(PP, CreatePrintParserActionsAction(PP, OS.get()));
Chris Lattnerbd247762007-07-22 06:05:44 +0000866 ClearSourceMgr = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 break;
Chris Lattner47099742009-02-18 01:51:21 +0000868 }
869
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000870 case RewriteMacros:
Daniel Dunbare29709f2009-11-09 20:55:08 +0000871 OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath));
Eli Friedmanf54fce82009-05-19 01:02:07 +0000872 RewriteMacrosInInput(PP, OS.get());
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000873 ClearSourceMgr = true;
874 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Eli Friedmanf54fce82009-05-19 01:02:07 +0000876 case RewriteTest:
Daniel Dunbare29709f2009-11-09 20:55:08 +0000877 OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath));
Eli Friedmanf54fce82009-05-19 01:02:07 +0000878 DoRewriteTest(PP, OS.get());
Chris Lattnerb13c5ee2008-10-12 05:29:20 +0000879 ClearSourceMgr = true;
880 break;
Douglas Gregor558cb562009-04-02 01:08:08 +0000881
882 case FixIt:
Douglas Gregor558cb562009-04-02 01:08:08 +0000883 Consumer.reset(new ASTConsumer());
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000884 FixItRewrite = new FixItRewriter(PP.getDiagnostics(),
Chris Lattner2c78b872009-04-14 23:22:57 +0000885 PP.getSourceManager(),
886 PP.getLangOptions());
Douglas Gregor558cb562009-04-02 01:08:08 +0000887 break;
Chris Lattner47099742009-02-18 01:51:21 +0000888 }
Ted Kremenek46157b52009-01-28 04:29:29 +0000889
Chris Lattner1aee61a2009-04-27 21:25:27 +0000890 if (FixItAtLocations.size() > 0) {
891 // Even without the "-fixit" flag, with may have some specific
892 // locations where the user has requested fixes. Process those
893 // locations now.
894 if (!FixItRewrite)
895 FixItRewrite = new FixItRewriter(PP.getDiagnostics(),
896 PP.getSourceManager(),
897 PP.getLangOptions());
Chris Lattner9ecd26a2009-03-28 01:37:17 +0000898
Chris Lattner1aee61a2009-04-27 21:25:27 +0000899 bool AddedFixitLocation = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000900 for (unsigned Idx = 0, Last = FixItAtLocations.size();
Chris Lattner1aee61a2009-04-27 21:25:27 +0000901 Idx != Last; ++Idx) {
902 RequestedSourceLocation Requested;
Argyrios Kyrtzidis34d25d82009-06-23 22:01:39 +0000903 if (ResolveParsedLocation(FixItAtLocations[Idx],
904 PP.getFileManager(), Requested)) {
Chris Lattner1aee61a2009-04-27 21:25:27 +0000905 fprintf(stderr, "FIX-IT could not find file \"%s\"\n",
906 FixItAtLocations[Idx].FileName.c_str());
907 } else {
908 FixItRewrite->addFixItLocation(Requested);
909 AddedFixitLocation = true;
Douglas Gregor26df2f02009-04-02 19:05:20 +0000910 }
911 }
912
Chris Lattner1aee61a2009-04-27 21:25:27 +0000913 if (!AddedFixitLocation) {
914 // All of the fix-it locations were bad. Don't fix anything.
915 delete FixItRewrite;
916 FixItRewrite = 0;
917 }
918 }
919
920 llvm::OwningPtr<ASTContext> ContextOwner;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000921 if (Consumer)
Chris Lattner9ecd26a2009-03-28 01:37:17 +0000922 ContextOwner.reset(new ASTContext(PP.getLangOptions(),
923 PP.getSourceManager(),
924 PP.getTargetInfo(),
925 PP.getIdentifierTable(),
926 PP.getSelectorTable(),
Chris Lattner1b63e4f2009-06-14 01:54:56 +0000927 PP.getBuiltinInfo(),
Douglas Gregor2deaea32009-04-22 18:49:13 +0000928 /* FreeMemory = */ !DisableFree,
Chris Lattner1b63e4f2009-06-14 01:54:56 +0000929 /* size_reserve = */0));
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Chris Lattnercc7dea82009-04-27 22:02:30 +0000931 llvm::OwningPtr<PCHReader> Reader;
932 llvm::OwningPtr<ExternalASTSource> Source;
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Daniel Dunbare0a95812009-11-10 22:09:38 +0000934 const std::string &ImplicitPCHInclude =
935 CompOpts.getPreprocessorOpts().getImplicitPCHInclude();
936 if (!ImplicitPCHInclude.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000937 // If the user specified -isysroot, it will be used for relocatable PCH
938 // files.
Daniel Dunbarf7973292009-11-11 08:13:32 +0000939 const char *isysrootPCH = CompOpts.getHeaderSearchOpts().Sysroot.c_str();
940 if (isysrootPCH[0] == '\0')
941 isysrootPCH = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Douglas Gregore650c8c2009-07-07 00:12:59 +0000943 Reader.reset(new PCHReader(PP, ContextOwner.get(), isysrootPCH));
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000945 // The user has asked us to include a precompiled header. Load
946 // the precompiled header into the AST context.
Daniel Dunbare0a95812009-11-10 22:09:38 +0000947 switch (Reader->ReadPCH(ImplicitPCHInclude)) {
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000948 case PCHReader::Success: {
Douglas Gregore721f952009-04-28 18:58:38 +0000949 // Set the predefines buffer as suggested by the PCH
950 // reader. Typically, the predefines buffer will be empty.
951 PP.setPredefines(Reader->getSuggestedPredefines());
952
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000953 // Attach the PCH reader to the AST context as an external AST
954 // source, so that declarations will be deserialized from the
955 // PCH file as needed.
Chris Lattnercc7dea82009-04-27 22:02:30 +0000956 if (ContextOwner) {
957 Source.reset(Reader.take());
Douglas Gregore1d918e2009-04-10 23:10:45 +0000958 ContextOwner->setExternalSource(Source);
Chris Lattnercc7dea82009-04-27 22:02:30 +0000959 }
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000960 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000961 }
962
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000963 case PCHReader::Failure:
964 // Unrecoverable failure: don't even try to process the input
965 // file.
966 return;
967
968 case PCHReader::IgnorePCH:
Douglas Gregor1ab86ac2009-04-28 22:01:16 +0000969 // No suitable PCH file could be found. Return an error.
970 return;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000971 }
972
973 // Finish preprocessor initialization. We do this now (rather
974 // than earlier) because this initialization creates new source
975 // location entries in the source manager, which must come after
976 // the source location entries for the PCH file.
977 if (InitializeSourceManager(PP, InFile))
978 return;
Ted Kremenek46157b52009-01-28 04:29:29 +0000979 }
Daniel Dunbarbea5a842009-10-29 01:53:18 +0000980
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000981 // If we have an ASTConsumer, run the parser with it.
Douglas Gregor81b747b2009-09-17 21:32:03 +0000982 if (Consumer) {
983 CodeCompleteConsumer *(*CreateCodeCompleter)(Sema &, void *) = 0;
984 void *CreateCodeCompleterData = 0;
Daniel Dunbarbea5a842009-10-29 01:53:18 +0000985
Douglas Gregorb657f112009-09-22 21:11:38 +0000986 if (!CodeCompletionAt.FileName.empty()) {
987 // Tell the source manager to chop off the given file at a specific
988 // line and column.
Daniel Dunbarbea5a842009-10-29 01:53:18 +0000989 if (const FileEntry *Entry
Douglas Gregorb657f112009-09-22 21:11:38 +0000990 = PP.getFileManager().getFile(CodeCompletionAt.FileName)) {
991 // Truncate the named file at the given line/column.
992 PP.getSourceManager().truncateFileAt(Entry, CodeCompletionAt.Line,
993 CodeCompletionAt.Column);
Daniel Dunbarbea5a842009-10-29 01:53:18 +0000994
Douglas Gregorb657f112009-09-22 21:11:38 +0000995 // Set up the creation routine for code-completion.
996 CreateCodeCompleter = BuildPrintingCodeCompleter;
997 } else {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +0000998 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
Douglas Gregorb657f112009-09-22 21:11:38 +0000999 << CodeCompletionAt.FileName;
1000 }
Douglas Gregor81b747b2009-09-17 21:32:03 +00001001 }
1002
Mike Stump1eb44332009-09-09 15:08:12 +00001003 ParseAST(PP, Consumer.get(), *ContextOwner.get(), Stats,
Douglas Gregor81b747b2009-09-17 21:32:03 +00001004 CompleteTranslationUnit,
1005 CreateCodeCompleter, CreateCodeCompleterData);
1006 }
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001007
Daniel Dunbar593c41f2009-11-04 23:41:40 +00001008 // Perform post processing actions and actions which don't use a consumer.
1009 switch (PA) {
1010 default: break;
1011
1012 case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001013 llvm::TimeRegion Timer(ClangFrontendTimer);
1014 Token Tok;
1015 // Start parsing the specified input file.
1016 PP.EnterMainSourceFile();
1017 do {
1018 PP.Lex(Tok);
1019 } while (Tok.isNot(tok::eof));
1020 ClearSourceMgr = true;
Daniel Dunbar593c41f2009-11-04 23:41:40 +00001021 break;
1022 }
1023
1024 case ParseNoop: {
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001025 llvm::TimeRegion Timer(ClangFrontendTimer);
1026 ParseFile(PP, new MinimalAction(PP));
1027 ClearSourceMgr = true;
Daniel Dunbar593c41f2009-11-04 23:41:40 +00001028 break;
1029 }
1030
1031 case PrintPreprocessedInput: {
Chris Lattnercc7dea82009-04-27 22:02:30 +00001032 llvm::TimeRegion Timer(ClangFrontendTimer);
Eli Friedman12d3b1d2009-05-19 03:06:47 +00001033 if (DumpMacros)
1034 DoPrintMacros(PP, OS.get());
1035 else
1036 DoPrintPreprocessedInput(PP, OS.get(), EnableCommentOutput,
1037 EnableMacroCommentOutput,
1038 DisableLineMarkers, DumpDefines);
Chris Lattnercc7dea82009-04-27 22:02:30 +00001039 ClearSourceMgr = true;
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001040 }
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Daniel Dunbar593c41f2009-11-04 23:41:40 +00001042 }
1043
Chris Lattner1aee61a2009-04-27 21:25:27 +00001044 if (FixItRewrite)
Daniel Dunbare29709f2009-11-09 20:55:08 +00001045 FixItRewrite->WriteFixedFile(InFile, CompOpts.getOutputFile());
Daniel Dunbar70186ab2009-07-29 02:40:09 +00001046
1047 // Disable the consumer prior to the context, the consumer may perform actions
1048 // in its destructor which require the context.
1049 if (DisableFree)
1050 Consumer.take();
1051 else
1052 Consumer.reset();
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Chris Lattner1aee61a2009-04-27 21:25:27 +00001054 // If in -disable-free mode, don't deallocate ASTContext.
1055 if (DisableFree)
1056 ContextOwner.take();
1057 else
1058 ContextOwner.reset(); // Delete ASTContext
Eli Friedman66d6f042009-05-18 22:20:00 +00001059
Daniel Dunbar879c3ea2008-10-27 22:03:52 +00001060 if (VerifyDiagnostics)
Daniel Dunbar276373d2008-10-27 22:10:13 +00001061 if (CheckDiagnostics(PP))
1062 exit(1);
Chris Lattnere66b65c2008-02-06 01:42:25 +00001063
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 if (Stats) {
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001065 fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 PP.PrintStats();
1067 PP.getIdentifierTable().PrintStats();
Chris Lattnerdee73592007-12-15 20:48:40 +00001068 PP.getHeaderSearchInfo().PrintStats();
Ted Kremenek1b95a652009-01-09 18:20:21 +00001069 PP.getSourceManager().PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 fprintf(stderr, "\n");
1071 }
Chris Lattnerbd247762007-07-22 06:05:44 +00001072
Mike Stump1eb44332009-09-09 15:08:12 +00001073 // For a multi-file compilation, some things are ok with nuking the source
Chris Lattnerbd247762007-07-22 06:05:44 +00001074 // manager tables, other require stable fileid/macroid's across multiple
1075 // files.
Chris Lattnerdee73592007-12-15 20:48:40 +00001076 if (ClearSourceMgr)
1077 PP.getSourceManager().clearIDTables();
Daniel Dunbard68ba0e2008-11-11 06:35:39 +00001078
Eli Friedman66d6f042009-05-18 22:20:00 +00001079 // Always delete the output stream because we don't want to leak file
1080 // handles. Also, we don't want to try to erase an open file.
1081 OS.reset();
1082
Daniel Dunbar8cb65622009-10-29 21:05:18 +00001083 // If we had errors, try to erase the output file.
1084 if (PP.getDiagnostics().getNumErrors() && !OutPath.isEmpty())
Eli Friedman66d6f042009-05-18 22:20:00 +00001085 OutPath.eraseFromDisk();
Reid Spencer5f016e22007-07-11 17:01:13 +00001086}
1087
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001088/// ProcessInputFile - Process a single AST input file with the specified state.
1089///
Daniel Dunbare29709f2009-11-09 20:55:08 +00001090static void ProcessASTInputFile(const CompilerInvocation &CompOpts,
1091 const std::string &InFile, ProgActions PA,
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001092 Diagnostic &Diags, FileManager &FileMgr,
1093 llvm::LLVMContext& Context) {
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001094 std::string Error;
Steve Naroff36c44642009-10-19 14:34:22 +00001095 llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, &Error));
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001096 if (!AST) {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +00001097 Diags.Report(diag::err_fe_invalid_ast_file) << Error;
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001098 return;
1099 }
1100
1101 Preprocessor &PP = AST->getPreprocessor();
1102
1103 llvm::OwningPtr<llvm::raw_ostream> OS;
1104 llvm::sys::Path OutPath;
Daniel Dunbare29709f2009-11-09 20:55:08 +00001105 llvm::OwningPtr<ASTConsumer> Consumer(CreateConsumerAction(CompOpts, PP,
1106 InFile, PA, OS,
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001107 OutPath, Context));
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001108
1109 if (!Consumer.get()) {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +00001110 Diags.Report(diag::err_fe_invalid_ast_action);
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001111 return;
1112 }
1113
Daniel Dunbara674bf42009-09-21 03:03:56 +00001114 // Set the main file ID to an empty file.
1115 //
1116 // FIXME: We probably shouldn't need this, but for now this is the simplest
1117 // way to reuse the logic in ParseAST.
1118 const char *EmptyStr = "";
1119 llvm::MemoryBuffer *SB =
1120 llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<dummy input>");
1121 AST->getSourceManager().createMainFileIDForMemBuffer(SB);
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001122
Daniel Dunbara674bf42009-09-21 03:03:56 +00001123 // Stream the input AST to the consumer.
Daniel Dunbarefcbe942009-11-05 02:42:12 +00001124 Diags.getClient()->BeginSourceFile(PP.getLangOptions());
Daniel Dunbara674bf42009-09-21 03:03:56 +00001125 ParseAST(PP, Consumer.get(), AST->getASTContext(), Stats);
Daniel Dunbarefcbe942009-11-05 02:42:12 +00001126 Diags.getClient()->EndSourceFile();
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001127
1128 // Release the consumer and the AST, in that order since the consumer may
1129 // perform actions in its destructor which require the context.
1130 if (DisableFree) {
1131 Consumer.take();
1132 AST.take();
1133 } else {
1134 Consumer.reset();
1135 AST.reset();
1136 }
1137
1138 // Always delete the output stream because we don't want to leak file
1139 // handles. Also, we don't want to try to erase an open file.
1140 OS.reset();
1141
Daniel Dunbar8cb65622009-10-29 21:05:18 +00001142 // If we had errors, try to erase the output file.
1143 if (PP.getDiagnostics().getNumErrors() && !OutPath.isEmpty())
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001144 OutPath.eraseFromDisk();
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001145}
1146
Reid Spencer5f016e22007-07-11 17:01:13 +00001147static llvm::cl::list<std::string>
1148InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
1149
Daniel Dunbar70121eb2009-08-10 03:40:28 +00001150static void LLVMErrorHandler(void *UserData, const std::string &Message) {
1151 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
1152
Daniel Dunbar0f9fed72009-11-10 23:55:23 +00001153 Diags.Report(diag::err_fe_error_backend) << Message;
Daniel Dunbar70121eb2009-08-10 03:40:28 +00001154
1155 // We cannot recover from llvm errors.
1156 exit(1);
1157}
1158
Daniel Dunbar227b2382009-11-09 22:45:57 +00001159static LangKind GetLanguage() {
1160 // If -x was given, that's the language.
1161 if (BaseLang != langkind_unspecified)
1162 return BaseLang;
Daniel Dunbare29709f2009-11-09 20:55:08 +00001163
Daniel Dunbar227b2382009-11-09 22:45:57 +00001164 // Otherwise guess it from the input filenames;
1165 LangKind LK = langkind_unspecified;
1166 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
1167 llvm::StringRef Name(InputFilenames[i]);
1168 LangKind ThisKind = llvm::StringSwitch<LangKind>(Name.rsplit('.').second)
1169 .Case("ast", langkind_ast)
1170 .Case("c", langkind_c)
1171 .Cases("S", "s", langkind_asm_cpp)
1172 .Case("i", langkind_c_cpp)
1173 .Case("ii", langkind_cxx_cpp)
1174 .Case("m", langkind_objc)
1175 .Case("mi", langkind_objc_cpp)
1176 .Cases("mm", "M", langkind_objcxx)
1177 .Case("mii", langkind_objcxx_cpp)
1178 .Case("C", langkind_cxx)
1179 .Cases("C", "cc", "cp", langkind_cxx)
1180 .Cases("cpp", "CPP", "c++", "cxx", langkind_cxx)
1181 .Case("cl", langkind_ocl)
1182 .Default(langkind_c);
1183
1184 if (LK != langkind_unspecified && ThisKind != LK) {
1185 llvm::errs() << "error: cannot have multiple input files of distinct "
1186 << "language kinds without -x\n";
1187 exit(1);
1188 }
1189
1190 LK = ThisKind;
1191 }
1192
1193 return LK;
1194}
1195
Daniel Dunbar0498cfc2009-11-10 19:51:53 +00001196static void FinalizeCompileOptions(CompileOptions &Opts,
1197 const LangOptions &Lang) {
1198 if (Lang.NoBuiltin)
1199 Opts.SimplifyLibCalls = 0;
1200 if (Lang.CPlusPlus)
1201 Opts.NoCommon = 1;
1202
1203 // Handle -ftime-report.
1204 Opts.TimePasses = TimeReport;
1205}
1206
Daniel Dunbar227b2382009-11-09 22:45:57 +00001207static void ConstructCompilerInvocation(CompilerInvocation &Opts,
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001208 const char *Argv0,
Daniel Dunbar227b2382009-11-09 22:45:57 +00001209 const DiagnosticOptions &DiagOpts,
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001210 TargetInfo &Target,
1211 LangKind LK) {
Daniel Dunbar227b2382009-11-09 22:45:57 +00001212 Opts.getDiagnosticOpts() = DiagOpts;
1213
1214 Opts.getOutputFile() = OutputFile;
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001215
1216 // Compute the feature set, which may effect the language.
1217 ComputeFeatureMap(Target, Opts.getTargetFeatures());
Daniel Dunbar36f4ec32009-11-10 16:19:45 +00001218
Daniel Dunbarfcb0c3b2009-11-10 18:47:35 +00001219 // Initialize backend options, which may also be used to key some language
1220 // options.
1221 InitializeCompileOptions(Opts.getCompileOpts(), Opts.getTargetFeatures());
1222
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001223 // Initialize language options.
1224 LangOptions LangInfo;
Daniel Dunbar36f4ec32009-11-10 16:19:45 +00001225
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001226 // FIXME: These aren't used during operations on ASTs. Split onto a separate
1227 // code path to make this obvious.
Daniel Dunbar56749082009-11-11 07:26:12 +00001228 if (LK != langkind_ast)
1229 InitializeLangOptions(Opts.getLangOpts(), LK, Target,
1230 Opts.getCompileOpts(), Opts.getTargetFeatures());
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001231
1232 // Initialize the header search options.
Daniel Dunbarf7973292009-11-11 08:13:32 +00001233 InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(),
1234 GetBuiltinIncludePath(Argv0),
1235 Verbose,
1236 Opts.getLangOpts());
Daniel Dunbar5fc7d342009-11-09 23:12:31 +00001237
1238 // Initialize the other preprocessor options.
1239 InitializePreprocessorOptions(Opts.getPreprocessorOpts());
Daniel Dunbar36f4ec32009-11-10 16:19:45 +00001240
Daniel Dunbar0498cfc2009-11-10 19:51:53 +00001241 // Finalize some code generation options.
Daniel Dunbarfcb0c3b2009-11-10 18:47:35 +00001242 FinalizeCompileOptions(Opts.getCompileOpts(), Opts.getLangOpts());
Daniel Dunbare29709f2009-11-09 20:55:08 +00001243}
1244
Reid Spencer5f016e22007-07-11 17:01:13 +00001245int main(int argc, char **argv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 llvm::sys::PrintStackTraceOnErrorSignal();
Chris Lattner09e94a32009-03-04 21:41:39 +00001247 llvm::PrettyStackTraceProgram X(argc, argv);
Owen Andersond7200462009-07-16 00:14:12 +00001248 llvm::LLVMContext &Context = llvm::getGlobalContext();
Daniel Dunbard6970812009-09-02 23:20:15 +00001249
Daniel Dunbar4d861512009-09-03 04:54:12 +00001250 // Initialize targets first, so that --version shows registered targets.
Chris Lattner2fe11942009-06-17 17:25:50 +00001251 llvm::InitializeAllTargets();
1252 llvm::InitializeAllAsmPrinters();
Daniel Dunbard6970812009-09-02 23:20:15 +00001253
1254 llvm::cl::ParseCommandLineOptions(argc, argv,
1255 "LLVM 'Clang' Compiler: http://clang.llvm.org\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Chris Lattner47099742009-02-18 01:51:21 +00001257 if (TimeReport)
1258 ClangFrontendTimer = new llvm::Timer("Clang front-end time");
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Daniel Dunbar08e6dc62009-05-28 16:37:33 +00001260 if (Verbose)
Mike Stump3cbf5a02009-09-15 21:49:22 +00001261 llvm::errs() << "clang-cc version " CLANG_VERSION_STRING
1262 << " based upon " << PACKAGE_STRING
Daniel Dunbar2e30e592009-09-04 17:43:10 +00001263 << " hosted on " << llvm::sys::getHostTriple() << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 // If no input was specified, read from stdin.
1266 if (InputFilenames.empty())
1267 InputFilenames.push_back("-");
Douglas Gregor68a0d782009-05-02 00:03:46 +00001268
Daniel Dunbar227b2382009-11-09 22:45:57 +00001269 // Construct the diagnostic options first, which cannot fail, so that we can
1270 // build a diagnostic client to use for any errors during option handling.
1271 DiagnosticOptions DiagOpts;
Daniel Dunbar0db4b762009-11-11 08:13:40 +00001272 InitializeDiagnosticOptions(DiagOpts);
Daniel Dunbareace8742009-11-04 06:24:30 +00001273
Ted Kremenek31e703b2007-12-11 23:28:38 +00001274 // Create the diagnostic client for reporting errors or for
1275 // implementing -verify.
Chris Lattner409d4e72009-04-17 20:40:01 +00001276 llvm::OwningPtr<DiagnosticClient> DiagClient;
1277 if (VerifyDiagnostics) {
1278 // When checking diagnostics, just buffer them up.
1279 DiagClient.reset(new TextDiagnosticBuffer());
1280 if (InputFilenames.size() != 1) {
Daniel Dunbar227b2382009-11-09 22:45:57 +00001281 fprintf(stderr, "-verify only works on single input files.\n");
Chris Lattner409d4e72009-04-17 20:40:01 +00001282 return 1;
1283 }
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001284 } else {
Daniel Dunbar227b2382009-11-09 22:45:57 +00001285 DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), DiagOpts));
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 }
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Daniel Dunbarad451cc2009-11-05 02:11:37 +00001288 if (!DumpBuildInformation.empty())
Daniel Dunbar227b2382009-11-09 22:45:57 +00001289 SetUpBuildDumpLog(DiagOpts, argc, argv, DiagClient);
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001290
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 // Configure our handling of diagnostics.
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001292 Diagnostic Diags(DiagClient.get());
Eli Friedman0eeb86e2009-05-19 01:17:04 +00001293 if (ProcessWarningOptions(Diags, OptWarnings, OptPedantic, OptPedanticErrors,
1294 OptNoWarnings))
Sebastian Redl63a9e0f2009-03-06 17:41:35 +00001295 return 1;
Ted Kremenek31e703b2007-12-11 23:28:38 +00001296
Daniel Dunbar70121eb2009-08-10 03:40:28 +00001297 // Set an error handler, so that any LLVM backend diagnostics go through our
1298 // error handler.
1299 llvm::llvm_install_error_handler(LLVMErrorHandler,
1300 static_cast<void*>(&Diags));
1301
Daniel Dunbar7c15e712009-10-30 18:12:31 +00001302 // Initialize base triple. If a -triple option has been specified, use
1303 // that triple. Otherwise, default to the host triple.
1304 llvm::Triple Triple(TargetTriple);
1305 if (Triple.getTriple().empty())
1306 Triple = llvm::Triple(llvm::sys::getHostTriple());
1307
Chris Lattner11215192008-03-14 06:12:05 +00001308 // Get information about the target being compiled for.
Daniel Dunbarbea5a842009-10-29 01:53:18 +00001309 llvm::OwningPtr<TargetInfo>
Chris Lattner2f60af72009-09-12 22:45:58 +00001310 Target(TargetInfo::CreateTargetInfo(Triple.getTriple()));
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Chris Lattner11215192008-03-14 06:12:05 +00001312 if (Target == 0) {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +00001313 Diags.Report(diag::err_fe_unknown_triple) << Triple.getTriple().c_str();
Sebastian Redlc5613db2009-03-07 12:09:25 +00001314 return 1;
Chris Lattner11215192008-03-14 06:12:05 +00001315 }
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Daniel Dunbar73b79592009-09-14 00:02:12 +00001317 // Set the target ABI if specified.
1318 if (!TargetABI.empty()) {
1319 if (!Target->setABI(TargetABI)) {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +00001320 Diags.Report(diag::err_fe_unknown_target_abi) << TargetABI;
Daniel Dunbar73b79592009-09-14 00:02:12 +00001321 return 1;
1322 }
1323 }
1324
Daniel Dunbard4270232009-01-20 23:17:32 +00001325 if (!InheritanceViewCls.empty()) // C++ visualization?
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001326 ProgAction = InheritanceView;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Daniel Dunbar227b2382009-11-09 22:45:57 +00001328 // Infer the input language.
1329 //
1330 // FIXME: We should move .ast inputs to taking a separate path, they are
1331 // really quite different.
1332 LangKind LK = GetLanguage();
1333
1334 // Now that we have initialized the diagnostics engine and the target, finish
1335 // setting up the compiler invocation.
1336 CompilerInvocation CompOpts;
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001337 ConstructCompilerInvocation(CompOpts, argv[0], DiagOpts, *Target, LK);
Daniel Dunbar227b2382009-11-09 22:45:57 +00001338
Daniel Dunbar4cc1a252009-11-05 01:53:23 +00001339 // Create the source manager.
1340 SourceManager SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Chris Lattner2c78b872009-04-14 23:22:57 +00001342 // Create a file manager object to provide access to and cache the filesystem.
1343 FileManager FileMgr;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001344
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
Ted Kremenek31e703b2007-12-11 23:28:38 +00001346 const std::string &InFile = InputFilenames[i];
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001348 // AST inputs are handled specially.
1349 if (LK == langkind_ast) {
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001350 ProcessASTInputFile(CompOpts, InFile, ProgAction, Diags, FileMgr,
1351 Context);
Daniel Dunbaraca2ebd2009-09-17 00:48:13 +00001352 continue;
1353 }
1354
Daniel Dunbar4cc1a252009-11-05 01:53:23 +00001355 // Reset the ID tables if we are reusing the SourceManager.
1356 if (i)
1357 SourceMgr.clearIDTables();
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Chris Lattnerf63aea32009-03-04 21:40:56 +00001359 // Process the -I options and set them in the HeaderInfo.
1360 HeaderSearch HeaderInfo(FileMgr);
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001362 // Apply all the options to the header search object.
1363 ApplyHeaderSearchOptions(CompOpts.getHeaderSearchOpts(), HeaderInfo,
1364 CompOpts.getLangOpts(), Triple);
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Chris Lattnerf63aea32009-03-04 21:40:56 +00001366 // Set up the preprocessor with these options.
Daniel Dunbar5fc7d342009-11-09 23:12:31 +00001367 llvm::OwningPtr<Preprocessor>
1368 PP(CreatePreprocessor(Diags, CompOpts.getLangOpts(),
1369 CompOpts.getPreprocessorOpts(), *Target, SourceMgr,
1370 HeaderInfo));
Ted Kremenekb4398aa2008-08-07 17:49:57 +00001371
Daniel Dunbar8cb65622009-10-29 21:05:18 +00001372 // Handle generating dependencies, if requested.
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +00001373 if (!DependencyFile.empty()) {
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +00001374 if (DependencyTargets.empty()) {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +00001375 Diags.Report(diag::err_fe_dependency_file_requires_MT);
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +00001376 continue;
1377 }
1378 std::string ErrStr;
Daniel Dunbar8cb65622009-10-29 21:05:18 +00001379 llvm::raw_ostream *DependencyOS =
Dan Gohmanb044c472009-08-25 15:36:09 +00001380 new llvm::raw_fd_ostream(DependencyFile.c_str(), ErrStr);
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +00001381 if (!ErrStr.empty()) {
Daniel Dunbar0f9fed72009-11-10 23:55:23 +00001382 Diags.Report(diag::err_fe_error_opening) << DependencyFile << ErrStr;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +00001383 continue;
1384 }
1385
1386 AttachDependencyFileGen(PP.get(), DependencyOS, DependencyTargets,
1387 DependenciesIncludeSystemHeaders,
1388 PhonyDependencyTarget);
1389 }
1390
Daniel Dunbare0a95812009-11-10 22:09:38 +00001391 if (CompOpts.getPreprocessorOpts().getImplicitPCHInclude().empty()) {
Chris Lattner1b63e4f2009-06-14 01:54:56 +00001392 if (InitializeSourceManager(*PP.get(), InFile))
1393 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Chris Lattner1b63e4f2009-06-14 01:54:56 +00001395 // Initialize builtin info.
1396 PP->getBuiltinInfo().InitializeBuiltins(PP->getIdentifierTable(),
Chris Lattner1b63e4f2009-06-14 01:54:56 +00001397 PP->getLangOptions().NoBuiltin);
1398 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001399
Chris Lattnerf63aea32009-03-04 21:40:56 +00001400 // Process the source file.
Daniel Dunbar26a0cac2009-11-09 22:46:17 +00001401 Diags.getClient()->BeginSourceFile(CompOpts.getLangOpts());
1402 ProcessInputFile(CompOpts, *PP, InFile, ProgAction, Context);
Daniel Dunbar227b2382009-11-09 22:45:57 +00001403 Diags.getClient()->EndSourceFile();
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Chris Lattner40469652009-04-17 20:16:08 +00001405 HeaderInfo.ClearFileInfo();
Reid Spencer5f016e22007-07-11 17:01:13 +00001406 }
Chris Lattner11215192008-03-14 06:12:05 +00001407
Daniel Dunbar9253e492009-11-10 00:46:12 +00001408 if (CompOpts.getDiagnosticOpts().ShowCarets)
Mike Stumpfc0fed32009-04-28 01:19:10 +00001409 if (unsigned NumDiagnostics = Diags.getNumDiagnostics())
1410 fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
1411 (NumDiagnostics == 1 ? "" : "s"));
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 if (Stats) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 FileMgr.PrintStats();
1415 fprintf(stderr, "\n");
1416 }
Chris Lattner75a97cb2009-04-17 21:05:01 +00001417
1418 delete ClangFrontendTimer;
1419 delete BuildLogFile;
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Daniel Dunbar276373d2008-10-27 22:10:13 +00001421 // If verifying diagnostics and we reached here, all is well.
1422 if (VerifyDiagnostics)
1423 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Daniel Dunbar524b86f2008-10-28 00:38:08 +00001425 // Managed static deconstruction. Useful for making things like
1426 // -time-passes usable.
1427 llvm::llvm_shutdown();
1428
Daniel Dunbar8cb65622009-10-29 21:05:18 +00001429 return (Diags.getNumErrors() != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00001430}