Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- clang.cpp - C-Language Front-end ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | |
Daniel Dunbar | 0498cfc | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 18 | #include "Options.h" |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/AnalysisConsumer.h" |
Eli Friedman | 8ceb0d9 | 2009-05-18 23:02:01 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/ASTConsumers.h" |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/ASTUnit.h" |
Daniel Dunbar | dbf75fe | 2009-11-11 08:13:24 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/ChainedDiagnosticClient.h" |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | eace874 | 2009-11-04 06:24:30 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/DiagnosticOptions.h" |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/FixItRewriter.h" |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/PCHReader.h" |
Daniel Dunbar | 8863b98 | 2009-11-07 04:20:15 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/PathDiagnosticClients.h" |
| 29 | #include "clang/Frontend/PreprocessorOptions.h" |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 31 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Argyrios Kyrtzidis | 34d25d8 | 2009-06-23 22:01:39 +0000 | [diff] [blame] | 32 | #include "clang/Frontend/CommandLineSourceLoc.h" |
Eli Friedman | b09f6e1 | 2009-05-19 04:14:29 +0000 | [diff] [blame] | 33 | #include "clang/Frontend/Utils.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 34 | #include "clang/Analysis/PathDiagnostic.h" |
Chris Lattner | 8ee3c03 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 35 | #include "clang/CodeGen/ModuleBuilder.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 36 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 38 | #include "clang/Sema/SemaDiagnostic.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 39 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 1266eca | 2009-03-28 04:31:31 +0000 | [diff] [blame] | 40 | #include "clang/AST/ASTContext.h" |
| 41 | #include "clang/AST/Decl.h" |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 42 | #include "clang/AST/DeclGroup.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | #include "clang/Parse/Parser.h" |
| 44 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | db76684 | 2009-02-06 04:16:41 +0000 | [diff] [blame] | 45 | #include "clang/Lex/LexDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | #include "clang/Basic/FileManager.h" |
| 47 | #include "clang/Basic/SourceManager.h" |
| 48 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | aa338bc | 2009-05-06 04:07:06 +0000 | [diff] [blame] | 49 | #include "clang/Basic/Version.h" |
Owen Anderson | 42253cc | 2009-07-01 17:00:06 +0000 | [diff] [blame] | 50 | #include "llvm/LLVMContext.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 8f3dab8 | 2007-12-15 23:20:07 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/StringMap.h" |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/StringSwitch.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 56 | #include "llvm/Config/config.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 58 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 59 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | #include "llvm/Support/MemoryBuffer.h" |
Zhongxing Xu | 2092236 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 61 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 62 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 63 | #include "llvm/Support/Timer.h" |
Chris Lattner | 0fa0daa | 2009-08-24 04:11:30 +0000 | [diff] [blame] | 64 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 65 | #include "llvm/System/Host.h" |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 66 | #include "llvm/System/Path.h" |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 67 | #include "llvm/System/Program.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 68 | #include "llvm/System/Signals.h" |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 69 | #include "llvm/Target/TargetSelect.h" |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 70 | #include <cstdlib> |
Douglas Gregor | 44cf08e | 2009-05-03 03:52:38 +0000 | [diff] [blame] | 71 | #if HAVE_SYS_TYPES_H |
Douglas Gregor | 68a0d78 | 2009-05-02 00:03:46 +0000 | [diff] [blame] | 72 | # include <sys/types.h> |
| 73 | #endif |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 74 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | using namespace clang; |
| 76 | |
| 77 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 78 | // Source Location Parser |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
Argyrios Kyrtzidis | 34d25d8 | 2009-06-23 22:01:39 +0000 | [diff] [blame] | 81 | static bool ResolveParsedLocation(ParsedSourceLocation &ParsedLoc, |
| 82 | FileManager &FileMgr, |
| 83 | RequestedSourceLocation &Result) { |
| 84 | const FileEntry *File = FileMgr.getFile(ParsedLoc.FileName); |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 85 | if (!File) |
| 86 | return true; |
| 87 | |
| 88 | Result.File = File; |
Argyrios Kyrtzidis | 34d25d8 | 2009-06-23 22:01:39 +0000 | [diff] [blame] | 89 | Result.Line = ParsedLoc.Line; |
| 90 | Result.Column = ParsedLoc.Column; |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 94 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | // Global options. |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 98 | /// ClangFrontendTimer - The front-end activities should charge time to it with |
| 99 | /// TimeRegion. The -ftime-report option controls whether this will do |
| 100 | /// anything. |
| 101 | llvm::Timer *ClangFrontendTimer = 0; |
| 102 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | static llvm::cl::opt<bool> |
| 104 | Verbose("v", llvm::cl::desc("Enable verbose output")); |
| 105 | static llvm::cl::opt<bool> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | Stats("print-stats", |
Nate Begeman | aabbb12 | 2007-12-30 01:38:50 +0000 | [diff] [blame] | 107 | llvm::cl::desc("Print performance metrics and statistics")); |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 108 | static llvm::cl::opt<bool> |
| 109 | DisableFree("disable-free", |
| 110 | llvm::cl::desc("Disable freeing of memory on exit"), |
| 111 | llvm::cl::init(false)); |
Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 112 | static llvm::cl::opt<bool> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | EmptyInputOnly("empty-input-only", |
Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 114 | llvm::cl::desc("Force running on an empty input file")); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | |
| 116 | enum ProgActions { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 117 | RewriteObjC, // ObjC->C Rewriter. |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 118 | RewriteBlocks, // ObjC->C Rewriter for Blocks. |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 119 | RewriteMacros, // Expand macros but not #includes. |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 120 | RewriteTest, // Rewriter playground |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 121 | FixIt, // Fix-It Rewriter |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 122 | HTMLTest, // HTML displayer testing stuff. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 123 | EmitAssembly, // Emit a .s file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | EmitLLVM, // Emit a .ll file. |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 125 | EmitBC, // Emit a .bc file. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | EmitLLVMOnly, // Generate LLVM IR, but do not |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 127 | EmitHTML, // Translate input source into HTML. |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 128 | ASTPrint, // Parse ASTs and print them. |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 129 | ASTPrintXML, // Parse ASTs and print them in XML. |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 130 | ASTDump, // Parse ASTs and dump them. |
| 131 | ASTView, // Parse ASTs and view them in Graphviz. |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 132 | PrintDeclContext, // Print DeclContext and their Decls. |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 133 | DumpRecordLayouts, // Dump record layout information. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | ParsePrintCallbacks, // Parse and print each callback. |
| 135 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
| 136 | ParseNoop, // Parse with noop callbacks. |
| 137 | RunPreprocessorOnly, // Just lex, no output. |
| 138 | PrintPreprocessedInput, // -E mode. |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 139 | DumpTokens, // Dump out preprocessed tokens. |
| 140 | DumpRawTokens, // Dump out raw tokens. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | RunAnalysis, // Run one or more source code analyses. |
Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 142 | GeneratePTH, // Generate pre-tokenized header. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 143 | GeneratePCH, // Generate pre-compiled header. |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 144 | InheritanceView // View C++ inheritance for a specified class. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | static llvm::cl::opt<ProgActions> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 149 | llvm::cl::init(ParseSyntaxOnly), |
| 150 | llvm::cl::values( |
| 151 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 152 | "Just run preprocessor, no output (for timings)"), |
| 153 | clEnumValN(PrintPreprocessedInput, "E", |
| 154 | "Run preprocessor, emit preprocessed file"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 155 | clEnumValN(DumpRawTokens, "dump-raw-tokens", |
| 156 | "Lex file in raw mode and dump raw tokens"), |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 157 | clEnumValN(RunAnalysis, "analyze", |
| 158 | "Run static analysis engine"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 159 | clEnumValN(DumpTokens, "dump-tokens", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | "Run preprocessor, dump internal rep of tokens"), |
| 161 | clEnumValN(ParseNoop, "parse-noop", |
| 162 | "Run parser with noop callbacks (for timings)"), |
| 163 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 164 | "Run parser and perform semantic analysis"), |
| 165 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 166 | "Run parser and print each callback invoked"), |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 167 | clEnumValN(EmitHTML, "emit-html", |
| 168 | "Output input source as HTML"), |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 169 | clEnumValN(ASTPrint, "ast-print", |
| 170 | "Build ASTs and then pretty-print them"), |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 171 | clEnumValN(ASTPrintXML, "ast-print-xml", |
| 172 | "Build ASTs and then print them in XML format"), |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 173 | clEnumValN(ASTDump, "ast-dump", |
| 174 | "Build ASTs and then debug dump them"), |
Chris Lattner | ea254db | 2007-10-11 00:37:43 +0000 | [diff] [blame] | 175 | clEnumValN(ASTView, "ast-view", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 176 | "Build ASTs and view them with GraphViz"), |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 177 | clEnumValN(PrintDeclContext, "print-decl-contexts", |
Ted Kremenek | 08478eb | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 178 | "Print DeclContexts and their Decls"), |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 179 | clEnumValN(DumpRecordLayouts, "dump-record-layouts", |
| 180 | "Dump record layout information"), |
Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 181 | clEnumValN(GeneratePTH, "emit-pth", |
Ted Kremenek | 08478eb | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 182 | "Generate pre-tokenized header file"), |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 183 | clEnumValN(GeneratePCH, "emit-pch", |
| 184 | "Generate pre-compiled header file"), |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 185 | clEnumValN(EmitAssembly, "S", |
| 186 | "Emit native assembly code"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | clEnumValN(EmitLLVM, "emit-llvm", |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 188 | "Build ASTs then convert to LLVM, emit .ll file"), |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 189 | clEnumValN(EmitBC, "emit-llvm-bc", |
| 190 | "Build ASTs then convert to LLVM, emit .bc file"), |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 191 | clEnumValN(EmitLLVMOnly, "emit-llvm-only", |
| 192 | "Build ASTs and convert to LLVM, discarding output"), |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 193 | clEnumValN(RewriteTest, "rewrite-test", |
| 194 | "Rewriter playground"), |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 195 | clEnumValN(RewriteObjC, "rewrite-objc", |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 196 | "Rewrite ObjC into C (code rewriter example)"), |
| 197 | clEnumValN(RewriteMacros, "rewrite-macros", |
| 198 | "Expand macros without full preprocessing"), |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 199 | clEnumValN(RewriteBlocks, "rewrite-blocks", |
| 200 | "Rewrite Blocks to C"), |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 201 | clEnumValN(FixIt, "fixit", |
| 202 | "Apply fix-it advice to the input source"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | clEnumValEnd)); |
| 204 | |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 205 | |
| 206 | static llvm::cl::opt<std::string> |
| 207 | OutputFile("o", |
Ted Kremenek | 50b5641 | 2007-12-19 19:50:41 +0000 | [diff] [blame] | 208 | llvm::cl::value_desc("path"), |
Douglas Gregor | 370187c | 2009-04-22 21:45:53 +0000 | [diff] [blame] | 209 | llvm::cl::desc("Specify output file")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 211 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 212 | enum CodeCompletionPrinter { |
| 213 | CCP_Debug, |
| 214 | CCP_CIndex |
| 215 | }; |
| 216 | |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 217 | static llvm::cl::opt<ParsedSourceLocation> |
| 218 | CodeCompletionAt("code-completion-at", |
| 219 | llvm::cl::value_desc("file:line:column"), |
| 220 | llvm::cl::desc("Dump code-completion information at a location")); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 222 | static llvm::cl::opt<CodeCompletionPrinter> |
| 223 | CodeCompletionPrinter("code-completion-printer", |
| 224 | llvm::cl::desc("Choose output type:"), |
| 225 | llvm::cl::init(CCP_Debug), |
| 226 | llvm::cl::values( |
| 227 | clEnumValN(CCP_Debug, "debug", |
| 228 | "Debug code-completion results"), |
| 229 | clEnumValN(CCP_CIndex, "cindex", |
| 230 | "Code-completion results for the CIndex library"), |
| 231 | clEnumValEnd)); |
| 232 | |
| 233 | static llvm::cl::opt<bool> |
| 234 | CodeCompletionWantsMacros("code-completion-macros", |
| 235 | llvm::cl::desc("Include macros in code-completion results")); |
| 236 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 237 | /// \brief Buld a new code-completion consumer that prints the results of |
| 238 | /// code completion to standard output. |
| 239 | static CodeCompleteConsumer *BuildPrintingCodeCompleter(Sema &S, void *) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 240 | switch (CodeCompletionPrinter.getValue()) { |
| 241 | case CCP_Debug: |
| 242 | return new PrintingCodeCompleteConsumer(S, CodeCompletionWantsMacros, |
| 243 | llvm::outs()); |
| 244 | |
| 245 | case CCP_CIndex: |
| 246 | return new CIndexCodeCompleteConsumer(S, CodeCompletionWantsMacros, |
| 247 | llvm::outs()); |
| 248 | }; |
| 249 | |
| 250 | return 0; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 253 | //===----------------------------------------------------------------------===// |
| 254 | // PTH. |
| 255 | //===----------------------------------------------------------------------===// |
| 256 | |
| 257 | static llvm::cl::opt<std::string> |
| 258 | TokenCache("token-cache", llvm::cl::value_desc("path"), |
| 259 | llvm::cl::desc("Use specified token cache file")); |
| 260 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 261 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 262 | // Diagnostic Options |
| 263 | //===----------------------------------------------------------------------===// |
| 264 | |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 265 | static llvm::cl::opt<bool> |
| 266 | VerifyDiagnostics("verify", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 267 | llvm::cl::desc("Verify emitted diagnostics and warnings")); |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 268 | |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 269 | static llvm::cl::opt<bool> |
| 270 | NoShowColumn("fno-show-column", |
| 271 | llvm::cl::desc("Do not include column number on diagnostics")); |
| 272 | |
| 273 | static llvm::cl::opt<bool> |
Chris Lattner | 65f5e64 | 2009-01-30 19:01:41 +0000 | [diff] [blame] | 274 | NoShowLocation("fno-show-source-location", |
| 275 | llvm::cl::desc("Do not include source location information with" |
| 276 | " diagnostics")); |
| 277 | |
| 278 | static llvm::cl::opt<bool> |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 279 | NoCaretDiagnostics("fno-caret-diagnostics", |
| 280 | llvm::cl::desc("Do not include source line and caret with" |
| 281 | " diagnostics")); |
| 282 | |
Chris Lattner | 1fbee5d | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 283 | static llvm::cl::opt<bool> |
Chris Lattner | aa5bf2e | 2009-04-19 07:44:08 +0000 | [diff] [blame] | 284 | NoDiagnosticsFixIt("fno-diagnostics-fixit-info", |
| 285 | llvm::cl::desc("Do not include fixit information in" |
| 286 | " diagnostics")); |
| 287 | |
| 288 | static llvm::cl::opt<bool> |
Chris Lattner | 182e092 | 2009-04-21 05:34:31 +0000 | [diff] [blame] | 289 | PrintSourceRangeInfo("fdiagnostics-print-source-range-info", |
| 290 | llvm::cl::desc("Print source range spans in numeric form")); |
| 291 | |
Chris Lattner | d51d74a | 2009-04-16 05:44:38 +0000 | [diff] [blame] | 292 | static llvm::cl::opt<bool> |
| 293 | PrintDiagnosticOption("fdiagnostics-show-option", |
| 294 | llvm::cl::desc("Print diagnostic name with mappable diagnostics")); |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 296 | static llvm::cl::opt<unsigned> |
| 297 | MessageLength("fmessage-length", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | llvm::cl::desc("Format message diagnostics so that they fit " |
| 299 | "within N columns or fewer, when possible."), |
| 300 | llvm::cl::value_desc("N")); |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 301 | |
Torok Edwin | 603fca7 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 302 | static llvm::cl::opt<bool> |
Daniel Dunbar | 838be48 | 2009-11-04 06:24:57 +0000 | [diff] [blame] | 303 | PrintColorDiagnostic("fcolor-diagnostics", |
| 304 | llvm::cl::desc("Use colors in diagnostics")); |
| 305 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 307 | // C++ Visualization. |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | |
| 310 | static llvm::cl::opt<std::string> |
| 311 | InheritanceViewCls("cxx-inheritance-view", |
| 312 | llvm::cl::value_desc("class name"), |
Daniel Dunbar | d77b251 | 2009-01-14 18:56:36 +0000 | [diff] [blame] | 313 | llvm::cl::desc("View C++ inheritance for a specified class")); |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 314 | |
| 315 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 316 | // Builtin Options |
| 317 | //===----------------------------------------------------------------------===// |
Chris Lattner | b2509e1 | 2009-02-18 01:12:43 +0000 | [diff] [blame] | 318 | |
| 319 | static llvm::cl::opt<bool> |
| 320 | TimeReport("ftime-report", |
| 321 | llvm::cl::desc("Print the amount of time each " |
| 322 | "phase of compilation takes")); |
| 323 | |
Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 324 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 325 | // Language Options |
| 326 | //===----------------------------------------------------------------------===// |
| 327 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | static llvm::cl::opt<LangKind> |
| 329 | BaseLang("x", llvm::cl::desc("Base language to compile"), |
| 330 | llvm::cl::init(langkind_unspecified), |
| 331 | llvm::cl::values(clEnumValN(langkind_c, "c", "C"), |
Nate Begeman | 4e3629e | 2009-06-25 22:43:10 +0000 | [diff] [blame] | 332 | clEnumValN(langkind_ocl, "cl", "OpenCL C"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 333 | clEnumValN(langkind_cxx, "c++", "C++"), |
| 334 | clEnumValN(langkind_objc, "objective-c", "Objective C"), |
| 335 | clEnumValN(langkind_objcxx,"objective-c++","Objective C++"), |
Daniel Dunbar | d2ea386 | 2009-01-29 23:50:47 +0000 | [diff] [blame] | 336 | clEnumValN(langkind_c_cpp, "cpp-output", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | "Preprocessed C"), |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 338 | clEnumValN(langkind_asm_cpp, "assembler-with-cpp", |
| 339 | "Preprocessed asm"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | clEnumValN(langkind_cxx_cpp, "c++-cpp-output", |
Chris Lattner | c76d807 | 2009-02-06 06:19:20 +0000 | [diff] [blame] | 341 | "Preprocessed C++"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | clEnumValN(langkind_objc_cpp, "objective-c-cpp-output", |
| 343 | "Preprocessed Objective C"), |
Chris Lattner | c76d807 | 2009-02-06 06:19:20 +0000 | [diff] [blame] | 344 | clEnumValN(langkind_objcxx_cpp, "objective-c++-cpp-output", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 345 | "Preprocessed Objective C++"), |
Daniel Dunbar | 0b5b0da | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 346 | clEnumValN(langkind_c, "c-header", |
| 347 | "C header"), |
| 348 | clEnumValN(langkind_objc, "objective-c-header", |
| 349 | "Objective-C header"), |
| 350 | clEnumValN(langkind_cxx, "c++-header", |
| 351 | "C++ header"), |
| 352 | clEnumValN(langkind_objcxx, "objective-c++-header", |
| 353 | "Objective-C++ header"), |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 354 | clEnumValN(langkind_ast, "ast", |
| 355 | "Clang AST"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 356 | clEnumValEnd)); |
| 357 | |
Daniel Dunbar | 7c15e71 | 2009-10-30 18:12:31 +0000 | [diff] [blame] | 358 | static llvm::cl::opt<std::string> |
| 359 | TargetTriple("triple", |
| 360 | llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)")); |
| 361 | |
Daniel Dunbar | 5674908 | 2009-11-11 07:26:12 +0000 | [diff] [blame] | 362 | static llvm::cl::opt<std::string> |
| 363 | TargetABI("target-abi", |
| 364 | llvm::cl::desc("Target a particular ABI type")); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | |
| 366 | //===----------------------------------------------------------------------===// |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 367 | // SourceManager initialization. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 368 | //===----------------------------------------------------------------------===// |
| 369 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 370 | static bool InitializeSourceManager(Preprocessor &PP, |
| 371 | const std::string &InFile) { |
| 372 | // Figure out where to get and map in the main file. |
| 373 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 374 | FileManager &FileMgr = PP.getFileManager(); |
Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 375 | |
| 376 | if (EmptyInputOnly) { |
| 377 | const char *EmptyStr = ""; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | llvm::MemoryBuffer *SB = |
Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 379 | llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<empty input>"); |
| 380 | SourceMgr.createMainFileIDForMemBuffer(SB); |
| 381 | } else if (InFile != "-") { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 382 | const FileEntry *File = FileMgr.getFile(InFile); |
| 383 | if (File) SourceMgr.createMainFileID(File, SourceLocation()); |
| 384 | if (SourceMgr.getMainFileID().isInvalid()) { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 385 | PP.getDiagnostics().Report(diag::err_fe_error_reading) << InFile.c_str(); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 386 | return true; |
| 387 | } |
| 388 | } else { |
| 389 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 390 | SourceMgr.createMainFileIDForMemBuffer(SB); |
| 391 | if (SourceMgr.getMainFileID().isInvalid()) { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 392 | PP.getDiagnostics().Report(diag::err_fe_error_reading_stdin); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 393 | return true; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return false; |
| 398 | } |
| 399 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 400 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 401 | //===----------------------------------------------------------------------===// |
| 402 | // Preprocessor Initialization |
| 403 | //===----------------------------------------------------------------------===// |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 404 | |
Chris Lattner | e6113de | 2009-11-03 19:50:27 +0000 | [diff] [blame] | 405 | static llvm::cl::opt<bool> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | RelocatablePCH("relocatable-pch", |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 407 | llvm::cl::desc("Whether to build a relocatable precompiled " |
| 408 | "header")); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 410 | // Finally, implement the code that groks the options above. |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 411 | |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 412 | // Add the clang headers, which are relative to the clang binary. |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 413 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 414 | llvm::sys::Path P = |
| 415 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 416 | (void*)(intptr_t) GetBuiltinIncludePath); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 417 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 418 | if (!P.isEmpty()) { |
| 419 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 420 | P.eraseComponent(); // Remove /bin from foo/bin |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 421 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 422 | // Get foo/lib/clang/<version>/include |
| 423 | P.appendComponent("lib"); |
| 424 | P.appendComponent("clang"); |
| 425 | P.appendComponent(CLANG_VERSION_STRING); |
| 426 | P.appendComponent("include"); |
| 427 | } |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 428 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 429 | return P.str(); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 432 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 433 | // Preprocessor construction |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 434 | //===----------------------------------------------------------------------===// |
| 435 | |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 436 | static Preprocessor * |
Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 437 | CreatePreprocessor(Diagnostic &Diags, const LangOptions &LangInfo, |
| 438 | const PreprocessorOptions &PPOpts, TargetInfo &Target, |
| 439 | SourceManager &SourceMgr, HeaderSearch &HeaderInfo) { |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 440 | PTHManager *PTHMgr = 0; |
Daniel Dunbar | e0a9581 | 2009-11-10 22:09:38 +0000 | [diff] [blame] | 441 | if (!TokenCache.empty() && !PPOpts.getImplicitPTHInclude().empty()) { |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 442 | fprintf(stderr, "error: cannot use both -token-cache and -include-pth " |
| 443 | "options\n"); |
| 444 | exit(1); |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 445 | } |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 446 | |
| 447 | // Use PTH? |
Daniel Dunbar | e0a9581 | 2009-11-10 22:09:38 +0000 | [diff] [blame] | 448 | if (!TokenCache.empty() || !PPOpts.getImplicitPTHInclude().empty()) { |
| 449 | const std::string& x = TokenCache.empty() ? |
| 450 | PPOpts.getImplicitPTHInclude() : TokenCache; |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 451 | PTHMgr = PTHManager::Create(x, &Diags, |
| 452 | TokenCache.empty() ? Diagnostic::Error |
| 453 | : Diagnostic::Warning); |
| 454 | } |
| 455 | |
| 456 | if (Diags.hasErrorOccurred()) |
| 457 | exit(1); |
| 458 | |
| 459 | // Create the Preprocessor. |
| 460 | Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target, |
| 461 | SourceMgr, HeaderInfo, PTHMgr); |
| 462 | |
| 463 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 464 | // That argument is used as the IdentifierInfoLookup argument to |
| 465 | // IdentifierTable's ctor. |
| 466 | if (PTHMgr) { |
| 467 | PTHMgr->setPreprocessor(PP); |
| 468 | PP->setPTHManager(PTHMgr); |
| 469 | } |
| 470 | |
Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 471 | InitializePreprocessor(*PP, PPOpts); |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 472 | |
| 473 | return PP; |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 474 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 475 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 476 | //===----------------------------------------------------------------------===// |
| 477 | // Basic Parser driver |
| 478 | //===----------------------------------------------------------------------===// |
| 479 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 480 | static void ParseFile(Preprocessor &PP, MinimalAction *PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 481 | Parser P(PP, *PA); |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 482 | PP.EnterMainSourceFile(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 484 | // Parsing the specified input file. |
| 485 | P.ParseTranslationUnit(); |
| 486 | delete PA; |
| 487 | } |
| 488 | |
| 489 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 490 | // Fix-It Options |
| 491 | //===----------------------------------------------------------------------===// |
| 492 | static llvm::cl::list<ParsedSourceLocation> |
| 493 | FixItAtLocations("fixit-at", llvm::cl::value_desc("source-location"), |
| 494 | llvm::cl::desc("Perform Fix-It modifications at the given source location")); |
| 495 | |
| 496 | //===----------------------------------------------------------------------===// |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 497 | // ObjC Rewriter Options |
| 498 | //===----------------------------------------------------------------------===// |
| 499 | static llvm::cl::opt<bool> |
| 500 | SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false), |
| 501 | llvm::cl::desc("Silence ObjC rewriting warnings")); |
| 502 | |
| 503 | //===----------------------------------------------------------------------===// |
Eli Friedman | 0eeb86e | 2009-05-19 01:17:04 +0000 | [diff] [blame] | 504 | // Warning Options |
| 505 | //===----------------------------------------------------------------------===// |
| 506 | |
| 507 | // This gets all -W options, including -Werror, -W[no-]system-headers, etc. The |
| 508 | // driver has stripped off -Wa,foo etc. The driver has also translated -W to |
| 509 | // -Wextra, so we don't need to worry about it. |
| 510 | static llvm::cl::list<std::string> |
| 511 | OptWarnings("W", llvm::cl::Prefix, llvm::cl::ValueOptional); |
| 512 | |
| 513 | static llvm::cl::opt<bool> OptPedantic("pedantic"); |
| 514 | static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors"); |
| 515 | static llvm::cl::opt<bool> OptNoWarnings("w"); |
| 516 | |
| 517 | //===----------------------------------------------------------------------===// |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 518 | // Preprocessing (-E mode) Options |
| 519 | //===----------------------------------------------------------------------===// |
| 520 | static llvm::cl::opt<bool> |
| 521 | DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode")); |
| 522 | static llvm::cl::opt<bool> |
| 523 | EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode")); |
| 524 | static llvm::cl::opt<bool> |
| 525 | EnableMacroCommentOutput("CC", |
| 526 | llvm::cl::desc("Enable comment output in -E mode, " |
| 527 | "even from macro expansions")); |
| 528 | static llvm::cl::opt<bool> |
| 529 | DumpMacros("dM", llvm::cl::desc("Print macro definitions in -E mode instead of" |
| 530 | " normal output")); |
| 531 | static llvm::cl::opt<bool> |
| 532 | DumpDefines("dD", llvm::cl::desc("Print macro definitions in -E mode in " |
| 533 | "addition to normal output")); |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 534 | |
| 535 | //===----------------------------------------------------------------------===// |
| 536 | // Dependency file options |
| 537 | //===----------------------------------------------------------------------===// |
| 538 | static llvm::cl::opt<std::string> |
| 539 | DependencyFile("dependency-file", |
| 540 | llvm::cl::desc("Filename (or -) to write dependency output to")); |
| 541 | |
| 542 | static llvm::cl::opt<bool> |
| 543 | DependenciesIncludeSystemHeaders("sys-header-deps", |
| 544 | llvm::cl::desc("Include system headers in dependency output")); |
| 545 | |
| 546 | static llvm::cl::list<std::string> |
| 547 | DependencyTargets("MT", |
| 548 | llvm::cl::desc("Specify target for dependency")); |
| 549 | |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 550 | static llvm::cl::opt<bool> |
| 551 | PhonyDependencyTarget("MP", |
| 552 | llvm::cl::desc("Create phony target for each dependency " |
| 553 | "(other than main file)")); |
| 554 | |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 555 | //===----------------------------------------------------------------------===// |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 556 | // Analysis options |
| 557 | //===----------------------------------------------------------------------===// |
| 558 | |
| 559 | static llvm::cl::list<Analyses> |
| 560 | AnalysisList(llvm::cl::desc("Source Code Analysis - Checks and Analyses"), |
| 561 | llvm::cl::values( |
| 562 | #define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\ |
| 563 | clEnumValN(NAME, CMDFLAG, DESC), |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 564 | #include "clang/Frontend/Analyses.def" |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 565 | clEnumValEnd)); |
| 566 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | static llvm::cl::opt<AnalysisStores> |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 568 | AnalysisStoreOpt("analyzer-store", |
| 569 | llvm::cl::desc("Source Code Analysis - Abstract Memory Store Models"), |
| 570 | llvm::cl::init(BasicStoreModel), |
| 571 | llvm::cl::values( |
| 572 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN)\ |
| 573 | clEnumValN(NAME##Model, CMDFLAG, DESC), |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 574 | #include "clang/Frontend/Analyses.def" |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 575 | clEnumValEnd)); |
| 576 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | static llvm::cl::opt<AnalysisConstraints> |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 578 | AnalysisConstraintsOpt("analyzer-constraints", |
| 579 | llvm::cl::desc("Source Code Analysis - Symbolic Constraint Engines"), |
| 580 | llvm::cl::init(RangeConstraintsModel), |
| 581 | llvm::cl::values( |
| 582 | #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN)\ |
| 583 | clEnumValN(NAME##Model, CMDFLAG, DESC), |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 584 | #include "clang/Frontend/Analyses.def" |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 585 | clEnumValEnd)); |
| 586 | |
| 587 | static llvm::cl::opt<AnalysisDiagClients> |
| 588 | AnalysisDiagOpt("analyzer-output", |
| 589 | llvm::cl::desc("Source Code Analysis - Output Options"), |
| 590 | llvm::cl::init(PD_HTML), |
| 591 | llvm::cl::values( |
| 592 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE)\ |
| 593 | clEnumValN(PD_##NAME, CMDFLAG, DESC), |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 594 | #include "clang/Frontend/Analyses.def" |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 595 | clEnumValEnd)); |
| 596 | |
| 597 | static llvm::cl::opt<bool> |
| 598 | VisualizeEGDot("analyzer-viz-egraph-graphviz", |
| 599 | llvm::cl::desc("Display exploded graph using GraphViz")); |
| 600 | |
| 601 | static llvm::cl::opt<bool> |
| 602 | VisualizeEGUbi("analyzer-viz-egraph-ubigraph", |
| 603 | llvm::cl::desc("Display exploded graph using Ubigraph")); |
| 604 | |
| 605 | static llvm::cl::opt<bool> |
| 606 | AnalyzeAll("analyzer-opt-analyze-headers", |
| 607 | llvm::cl::desc("Force the static analyzer to analyze " |
| 608 | "functions defined in header files")); |
| 609 | |
| 610 | static llvm::cl::opt<bool> |
| 611 | AnalyzerDisplayProgress("analyzer-display-progress", |
| 612 | llvm::cl::desc("Emit verbose output about the analyzer's progress.")); |
| 613 | |
| 614 | static llvm::cl::opt<bool> |
| 615 | PurgeDead("analyzer-purge-dead", |
| 616 | llvm::cl::init(true), |
| 617 | llvm::cl::desc("Remove dead symbols, bindings, and constraints before" |
| 618 | " processing a statement.")); |
| 619 | |
| 620 | static llvm::cl::opt<bool> |
| 621 | EagerlyAssume("analyzer-eagerly-assume", |
| 622 | llvm::cl::init(false), |
| 623 | llvm::cl::desc("Eagerly assume the truth/falseness of some " |
| 624 | "symbolic constraints.")); |
| 625 | |
| 626 | static llvm::cl::opt<std::string> |
| 627 | AnalyzeSpecificFunction("analyze-function", |
| 628 | llvm::cl::desc("Run analysis on specific function")); |
| 629 | |
| 630 | static llvm::cl::opt<bool> |
| 631 | TrimGraph("trim-egraph", |
| 632 | llvm::cl::desc("Only show error-related paths in the analysis graph")); |
| 633 | |
| 634 | static AnalyzerOptions ReadAnalyzerOptions() { |
| 635 | AnalyzerOptions Opts; |
| 636 | Opts.AnalysisList = AnalysisList; |
| 637 | Opts.AnalysisStoreOpt = AnalysisStoreOpt; |
| 638 | Opts.AnalysisConstraintsOpt = AnalysisConstraintsOpt; |
| 639 | Opts.AnalysisDiagOpt = AnalysisDiagOpt; |
| 640 | Opts.VisualizeEGDot = VisualizeEGDot; |
| 641 | Opts.VisualizeEGUbi = VisualizeEGUbi; |
| 642 | Opts.AnalyzeAll = AnalyzeAll; |
| 643 | Opts.AnalyzerDisplayProgress = AnalyzerDisplayProgress; |
| 644 | Opts.PurgeDead = PurgeDead; |
| 645 | Opts.EagerlyAssume = EagerlyAssume; |
| 646 | Opts.AnalyzeSpecificFunction = AnalyzeSpecificFunction; |
| 647 | Opts.TrimGraph = TrimGraph; |
| 648 | return Opts; |
| 649 | } |
| 650 | |
| 651 | //===----------------------------------------------------------------------===// |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 652 | // -dump-build-information Stuff |
| 653 | //===----------------------------------------------------------------------===// |
| 654 | |
| 655 | static llvm::cl::opt<std::string> |
| 656 | DumpBuildInformation("dump-build-information", |
| 657 | llvm::cl::value_desc("filename"), |
| 658 | llvm::cl::desc("output a dump of some build information to a file")); |
| 659 | |
| 660 | static llvm::raw_ostream *BuildLogFile = 0; |
| 661 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 662 | static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 663 | unsigned argc, char **argv, |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 664 | llvm::OwningPtr<DiagnosticClient> &DiagClient) { |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 665 | std::string ErrorInfo; |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 666 | BuildLogFile = new llvm::raw_fd_ostream(DumpBuildInformation.c_str(), |
Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 667 | ErrorInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 669 | if (!ErrorInfo.empty()) { |
| 670 | llvm::errs() << "error opening -dump-build-information file '" |
| 671 | << DumpBuildInformation << "', option ignored!\n"; |
| 672 | delete BuildLogFile; |
| 673 | BuildLogFile = 0; |
| 674 | DumpBuildInformation = ""; |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | (*BuildLogFile) << "clang-cc command line arguments: "; |
| 679 | for (unsigned i = 0; i != argc; ++i) |
| 680 | (*BuildLogFile) << argv[i] << ' '; |
| 681 | (*BuildLogFile) << '\n'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | |
Daniel Dunbar | dbf75fe | 2009-11-11 08:13:24 +0000 | [diff] [blame] | 683 | // Chain in a diagnostic client which will log the diagnostics. |
| 684 | DiagnosticClient *Logger = new TextDiagnosticPrinter(*BuildLogFile, DiagOpts); |
| 685 | DiagClient.reset(new ChainedDiagnosticClient(DiagClient.take(), Logger)); |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | |
| 689 | |
| 690 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 691 | // Main driver |
| 692 | //===----------------------------------------------------------------------===// |
| 693 | |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 694 | static llvm::raw_ostream *ComputeOutFile(const CompilerInvocation &CompOpts, |
| 695 | const std::string &InFile, |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 696 | const char *Extension, |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 697 | bool Binary, |
| 698 | llvm::sys::Path& OutPath) { |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 699 | llvm::raw_ostream *Ret; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 700 | std::string OutFile; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 701 | if (!CompOpts.getOutputFile().empty()) |
| 702 | OutFile = CompOpts.getOutputFile(); |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 703 | else if (InFile == "-") { |
| 704 | OutFile = "-"; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 705 | } else if (Extension) { |
| 706 | llvm::sys::Path Path(InFile); |
| 707 | Path.eraseSuffix(); |
| 708 | Path.appendSuffix(Extension); |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 709 | OutFile = Path.str(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 710 | } else { |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 711 | OutFile = "-"; |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 712 | } |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 714 | std::string Error; |
| 715 | Ret = new llvm::raw_fd_ostream(OutFile.c_str(), Error, |
Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 716 | (Binary ? llvm::raw_fd_ostream::F_Binary : 0)); |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 717 | if (!Error.empty()) { |
| 718 | // FIXME: Don't fail this way. |
| 719 | llvm::errs() << "ERROR: " << Error << "\n"; |
| 720 | ::exit(1); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 721 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 723 | if (OutFile != "-") |
| 724 | OutPath = OutFile; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 725 | |
| 726 | return Ret; |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 729 | static ASTConsumer *CreateConsumerAction(const CompilerInvocation &CompOpts, |
| 730 | Preprocessor &PP, |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 731 | const std::string &InFile, |
| 732 | ProgActions PA, |
| 733 | llvm::OwningPtr<llvm::raw_ostream> &OS, |
| 734 | llvm::sys::Path &OutPath, |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 735 | llvm::LLVMContext& Context) { |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 736 | switch (PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 737 | default: |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 738 | return 0; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 739 | |
| 740 | case ASTPrint: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 741 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, false, OutPath)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 742 | return CreateASTPrinter(OS.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 744 | case ASTPrintXML: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 745 | OS.reset(ComputeOutFile(CompOpts, InFile, "xml", false, OutPath)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 746 | return CreateASTPrinterXML(OS.get()); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 747 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 748 | case ASTDump: |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 749 | return CreateASTDumper(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 750 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 751 | case ASTView: |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 752 | return CreateASTViewer(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 753 | |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 754 | case DumpRecordLayouts: |
| 755 | return CreateRecordLayoutDumper(); |
| 756 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 757 | case InheritanceView: |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 758 | return CreateInheritanceViewer(InheritanceViewCls); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 759 | |
| 760 | case EmitAssembly: |
| 761 | case EmitLLVM: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | case EmitBC: |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 763 | case EmitLLVMOnly: { |
| 764 | BackendAction Act; |
| 765 | if (ProgAction == EmitAssembly) { |
| 766 | Act = Backend_EmitAssembly; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 767 | OS.reset(ComputeOutFile(CompOpts, InFile, "s", true, OutPath)); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 768 | } else if (ProgAction == EmitLLVM) { |
| 769 | Act = Backend_EmitLL; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 770 | OS.reset(ComputeOutFile(CompOpts, InFile, "ll", true, OutPath)); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 771 | } else if (ProgAction == EmitLLVMOnly) { |
| 772 | Act = Backend_EmitNothing; |
| 773 | } else { |
| 774 | Act = Backend_EmitBC; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 775 | OS.reset(ComputeOutFile(CompOpts, InFile, "bc", true, OutPath)); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 776 | } |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 777 | |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 778 | return CreateBackendConsumer(Act, PP.getDiagnostics(), PP.getLangOptions(), |
Daniel Dunbar | 36f4ec3 | 2009-11-10 16:19:45 +0000 | [diff] [blame] | 779 | CompOpts.getCompileOpts(), InFile, OS.get(), |
| 780 | Context); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 783 | case RewriteObjC: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 784 | OS.reset(ComputeOutFile(CompOpts, InFile, "cpp", true, OutPath)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 785 | return CreateObjCRewriter(InFile, OS.get(), PP.getDiagnostics(), |
| 786 | PP.getLangOptions(), SilenceRewriteMacroWarning); |
| 787 | |
| 788 | case RewriteBlocks: |
| 789 | return CreateBlockRewriter(InFile, PP.getDiagnostics(), |
| 790 | PP.getLangOptions()); |
Daniel Dunbar | 5ee0aa7 | 2009-11-11 00:54:56 +0000 | [diff] [blame] | 791 | |
| 792 | case ParseSyntaxOnly: |
| 793 | return new ASTConsumer(); |
| 794 | |
| 795 | case PrintDeclContext: |
| 796 | return CreateDeclContextPrinter(); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
| 800 | /// ProcessInputFile - Process a single input file with the specified state. |
| 801 | /// |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 802 | static void ProcessInputFile(const CompilerInvocation &CompOpts, |
| 803 | Preprocessor &PP, const std::string &InFile, |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 804 | ProgActions PA, |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 805 | llvm::LLVMContext& Context) { |
| 806 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 807 | llvm::OwningPtr<ASTConsumer> Consumer; |
| 808 | bool ClearSourceMgr = false; |
| 809 | FixItRewriter *FixItRewrite = 0; |
| 810 | bool CompleteTranslationUnit = true; |
| 811 | llvm::sys::Path OutPath; |
| 812 | |
| 813 | switch (PA) { |
| 814 | default: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 815 | Consumer.reset(CreateConsumerAction(CompOpts, PP, InFile, PA, OS, OutPath, |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 816 | Context)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 817 | |
| 818 | if (!Consumer.get()) { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 819 | PP.getDiagnostics().Report(diag::err_fe_invalid_ast_action); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 820 | return; |
| 821 | } |
| 822 | |
| 823 | break;; |
| 824 | |
| 825 | case EmitHTML: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 826 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 827 | Consumer.reset(CreateHTMLPrinter(OS.get(), PP)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 828 | break; |
| 829 | |
| 830 | case RunAnalysis: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 831 | Consumer.reset(CreateAnalysisConsumer(PP, CompOpts.getOutputFile(), |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 832 | ReadAnalyzerOptions())); |
| 833 | break; |
| 834 | |
Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame^] | 835 | case GeneratePCH: { |
| 836 | const std::string &Sysroot = CompOpts.getHeaderSearchOpts().Sysroot; |
| 837 | if (RelocatablePCH.getValue() && Sysroot.empty()) { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 838 | PP.Diag(SourceLocation(), diag::err_relocatable_without_without_isysroot); |
| 839 | RelocatablePCH.setValue(false); |
| 840 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 842 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 843 | if (RelocatablePCH.getValue()) |
Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame^] | 844 | Consumer.reset(CreatePCHGenerator(PP, OS.get(), Sysroot.c_str())); |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 845 | else |
| 846 | Consumer.reset(CreatePCHGenerator(PP, OS.get())); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 847 | CompleteTranslationUnit = false; |
| 848 | break; |
Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame^] | 849 | } |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 850 | case DumpRawTokens: { |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 851 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 852 | SourceManager &SM = PP.getSourceManager(); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 853 | // Start lexing the specified input file. |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 854 | Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions()); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 855 | RawLex.SetKeepWhitespaceMode(true); |
| 856 | |
| 857 | Token RawTok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 858 | RawLex.LexFromRawLexer(RawTok); |
| 859 | while (RawTok.isNot(tok::eof)) { |
| 860 | PP.DumpToken(RawTok, true); |
| 861 | fprintf(stderr, "\n"); |
| 862 | RawLex.LexFromRawLexer(RawTok); |
| 863 | } |
| 864 | ClearSourceMgr = true; |
| 865 | break; |
| 866 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 867 | case DumpTokens: { // Token dump mode. |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 868 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 869 | Token Tok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 870 | // Start preprocessing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 871 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 872 | do { |
| 873 | PP.Lex(Tok); |
| 874 | PP.DumpToken(Tok, true); |
| 875 | fprintf(stderr, "\n"); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 876 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 877 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 878 | break; |
| 879 | } |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 880 | case RunPreprocessorOnly: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 881 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | |
Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 883 | case GeneratePTH: { |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 884 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 885 | if (CompOpts.getOutputFile().empty() || CompOpts.getOutputFile() == "-") { |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 886 | // FIXME: Don't fail this way. |
| 887 | // FIXME: Verify that we can actually seek in the given file. |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 888 | llvm::errs() << "ERROR: PTH requires an seekable file for output!\n"; |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 889 | ::exit(1); |
| 890 | } |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 891 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 892 | CacheTokens(PP, static_cast<llvm::raw_fd_ostream*>(OS.get())); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 893 | ClearSourceMgr = true; |
| 894 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 896 | |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 897 | case PrintPreprocessedInput: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 898 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 899 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 901 | case ParseNoop: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 902 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 904 | case ParsePrintCallbacks: { |
| 905 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 906 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 907 | ParseFile(PP, CreatePrintParserActionsAction(PP, OS.get())); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 908 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 909 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 912 | case RewriteMacros: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 913 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 914 | RewriteMacrosInInput(PP, OS.get()); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 915 | ClearSourceMgr = true; |
| 916 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 918 | case RewriteTest: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 919 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 920 | DoRewriteTest(PP, OS.get()); |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 921 | ClearSourceMgr = true; |
| 922 | break; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 923 | |
| 924 | case FixIt: |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 925 | Consumer.reset(new ASTConsumer()); |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 926 | FixItRewrite = new FixItRewriter(PP.getDiagnostics(), |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 927 | PP.getSourceManager(), |
| 928 | PP.getLangOptions()); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 929 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 930 | } |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 932 | if (FixItAtLocations.size() > 0) { |
| 933 | // Even without the "-fixit" flag, with may have some specific |
| 934 | // locations where the user has requested fixes. Process those |
| 935 | // locations now. |
| 936 | if (!FixItRewrite) |
| 937 | FixItRewrite = new FixItRewriter(PP.getDiagnostics(), |
| 938 | PP.getSourceManager(), |
| 939 | PP.getLangOptions()); |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 940 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 941 | bool AddedFixitLocation = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | for (unsigned Idx = 0, Last = FixItAtLocations.size(); |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 943 | Idx != Last; ++Idx) { |
| 944 | RequestedSourceLocation Requested; |
Argyrios Kyrtzidis | 34d25d8 | 2009-06-23 22:01:39 +0000 | [diff] [blame] | 945 | if (ResolveParsedLocation(FixItAtLocations[Idx], |
| 946 | PP.getFileManager(), Requested)) { |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 947 | fprintf(stderr, "FIX-IT could not find file \"%s\"\n", |
| 948 | FixItAtLocations[Idx].FileName.c_str()); |
| 949 | } else { |
| 950 | FixItRewrite->addFixItLocation(Requested); |
| 951 | AddedFixitLocation = true; |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 955 | if (!AddedFixitLocation) { |
| 956 | // All of the fix-it locations were bad. Don't fix anything. |
| 957 | delete FixItRewrite; |
| 958 | FixItRewrite = 0; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | llvm::OwningPtr<ASTContext> ContextOwner; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 963 | if (Consumer) |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 964 | ContextOwner.reset(new ASTContext(PP.getLangOptions(), |
| 965 | PP.getSourceManager(), |
| 966 | PP.getTargetInfo(), |
| 967 | PP.getIdentifierTable(), |
| 968 | PP.getSelectorTable(), |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 969 | PP.getBuiltinInfo(), |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 970 | /* FreeMemory = */ !DisableFree, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 971 | /* size_reserve = */0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 973 | llvm::OwningPtr<PCHReader> Reader; |
| 974 | llvm::OwningPtr<ExternalASTSource> Source; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
Daniel Dunbar | e0a9581 | 2009-11-10 22:09:38 +0000 | [diff] [blame] | 976 | const std::string &ImplicitPCHInclude = |
| 977 | CompOpts.getPreprocessorOpts().getImplicitPCHInclude(); |
| 978 | if (!ImplicitPCHInclude.empty()) { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 979 | // If the user specified -isysroot, it will be used for relocatable PCH |
| 980 | // files. |
Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame^] | 981 | const char *isysrootPCH = CompOpts.getHeaderSearchOpts().Sysroot.c_str(); |
| 982 | if (isysrootPCH[0] == '\0') |
| 983 | isysrootPCH = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 985 | Reader.reset(new PCHReader(PP, ContextOwner.get(), isysrootPCH)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 987 | // The user has asked us to include a precompiled header. Load |
| 988 | // the precompiled header into the AST context. |
Daniel Dunbar | e0a9581 | 2009-11-10 22:09:38 +0000 | [diff] [blame] | 989 | switch (Reader->ReadPCH(ImplicitPCHInclude)) { |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 990 | case PCHReader::Success: { |
Douglas Gregor | e721f95 | 2009-04-28 18:58:38 +0000 | [diff] [blame] | 991 | // Set the predefines buffer as suggested by the PCH |
| 992 | // reader. Typically, the predefines buffer will be empty. |
| 993 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 994 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 995 | // Attach the PCH reader to the AST context as an external AST |
| 996 | // source, so that declarations will be deserialized from the |
| 997 | // PCH file as needed. |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 998 | if (ContextOwner) { |
| 999 | Source.reset(Reader.take()); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1000 | ContextOwner->setExternalSource(Source); |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 1001 | } |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1002 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1005 | case PCHReader::Failure: |
| 1006 | // Unrecoverable failure: don't even try to process the input |
| 1007 | // file. |
| 1008 | return; |
| 1009 | |
| 1010 | case PCHReader::IgnorePCH: |
Douglas Gregor | 1ab86ac | 2009-04-28 22:01:16 +0000 | [diff] [blame] | 1011 | // No suitable PCH file could be found. Return an error. |
| 1012 | return; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | // Finish preprocessor initialization. We do this now (rather |
| 1016 | // than earlier) because this initialization creates new source |
| 1017 | // location entries in the source manager, which must come after |
| 1018 | // the source location entries for the PCH file. |
| 1019 | if (InitializeSourceManager(PP, InFile)) |
| 1020 | return; |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 1021 | } |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1023 | // If we have an ASTConsumer, run the parser with it. |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1024 | if (Consumer) { |
| 1025 | CodeCompleteConsumer *(*CreateCodeCompleter)(Sema &, void *) = 0; |
| 1026 | void *CreateCodeCompleterData = 0; |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 1028 | if (!CodeCompletionAt.FileName.empty()) { |
| 1029 | // Tell the source manager to chop off the given file at a specific |
| 1030 | // line and column. |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1031 | if (const FileEntry *Entry |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 1032 | = PP.getFileManager().getFile(CodeCompletionAt.FileName)) { |
| 1033 | // Truncate the named file at the given line/column. |
| 1034 | PP.getSourceManager().truncateFileAt(Entry, CodeCompletionAt.Line, |
| 1035 | CodeCompletionAt.Column); |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1036 | |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 1037 | // Set up the creation routine for code-completion. |
| 1038 | CreateCodeCompleter = BuildPrintingCodeCompleter; |
| 1039 | } else { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 1040 | PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 1041 | << CodeCompletionAt.FileName; |
| 1042 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | ParseAST(PP, Consumer.get(), *ContextOwner.get(), Stats, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1046 | CompleteTranslationUnit, |
| 1047 | CreateCodeCompleter, CreateCodeCompleterData); |
| 1048 | } |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1049 | |
Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 1050 | // Perform post processing actions and actions which don't use a consumer. |
| 1051 | switch (PA) { |
| 1052 | default: break; |
| 1053 | |
| 1054 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1055 | llvm::TimeRegion Timer(ClangFrontendTimer); |
| 1056 | Token Tok; |
| 1057 | // Start parsing the specified input file. |
| 1058 | PP.EnterMainSourceFile(); |
| 1059 | do { |
| 1060 | PP.Lex(Tok); |
| 1061 | } while (Tok.isNot(tok::eof)); |
| 1062 | ClearSourceMgr = true; |
Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | } |
| 1065 | |
| 1066 | case ParseNoop: { |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1067 | llvm::TimeRegion Timer(ClangFrontendTimer); |
| 1068 | ParseFile(PP, new MinimalAction(PP)); |
| 1069 | ClearSourceMgr = true; |
Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 1070 | break; |
| 1071 | } |
| 1072 | |
| 1073 | case PrintPreprocessedInput: { |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 1074 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 1075 | if (DumpMacros) |
| 1076 | DoPrintMacros(PP, OS.get()); |
| 1077 | else |
| 1078 | DoPrintPreprocessedInput(PP, OS.get(), EnableCommentOutput, |
| 1079 | EnableMacroCommentOutput, |
| 1080 | DisableLineMarkers, DumpDefines); |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 1081 | ClearSourceMgr = true; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1082 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | |
Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 1086 | if (FixItRewrite) |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1087 | FixItRewrite->WriteFixedFile(InFile, CompOpts.getOutputFile()); |
Daniel Dunbar | 70186ab | 2009-07-29 02:40:09 +0000 | [diff] [blame] | 1088 | |
| 1089 | // Disable the consumer prior to the context, the consumer may perform actions |
| 1090 | // in its destructor which require the context. |
| 1091 | if (DisableFree) |
| 1092 | Consumer.take(); |
| 1093 | else |
| 1094 | Consumer.reset(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 1096 | // If in -disable-free mode, don't deallocate ASTContext. |
| 1097 | if (DisableFree) |
| 1098 | ContextOwner.take(); |
| 1099 | else |
| 1100 | ContextOwner.reset(); // Delete ASTContext |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1101 | |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 1102 | if (VerifyDiagnostics) |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1103 | if (CheckDiagnostics(PP)) |
| 1104 | exit(1); |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1105 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1106 | if (Stats) { |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1107 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1108 | PP.PrintStats(); |
| 1109 | PP.getIdentifierTable().PrintStats(); |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1110 | PP.getHeaderSearchInfo().PrintStats(); |
Ted Kremenek | 1b95a65 | 2009-01-09 18:20:21 +0000 | [diff] [blame] | 1111 | PP.getSourceManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1112 | fprintf(stderr, "\n"); |
| 1113 | } |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1114 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | // For a multi-file compilation, some things are ok with nuking the source |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1116 | // manager tables, other require stable fileid/macroid's across multiple |
| 1117 | // files. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1118 | if (ClearSourceMgr) |
| 1119 | PP.getSourceManager().clearIDTables(); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 1120 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1121 | // Always delete the output stream because we don't want to leak file |
| 1122 | // handles. Also, we don't want to try to erase an open file. |
| 1123 | OS.reset(); |
| 1124 | |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 1125 | // If we had errors, try to erase the output file. |
| 1126 | if (PP.getDiagnostics().getNumErrors() && !OutPath.isEmpty()) |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1127 | OutPath.eraseFromDisk(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1130 | /// ProcessInputFile - Process a single AST input file with the specified state. |
| 1131 | /// |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1132 | static void ProcessASTInputFile(const CompilerInvocation &CompOpts, |
| 1133 | const std::string &InFile, ProgActions PA, |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1134 | Diagnostic &Diags, FileManager &FileMgr, |
| 1135 | llvm::LLVMContext& Context) { |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1136 | std::string Error; |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 1137 | llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, &Error)); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1138 | if (!AST) { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 1139 | Diags.Report(diag::err_fe_invalid_ast_file) << Error; |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | Preprocessor &PP = AST->getPreprocessor(); |
| 1144 | |
| 1145 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 1146 | llvm::sys::Path OutPath; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1147 | llvm::OwningPtr<ASTConsumer> Consumer(CreateConsumerAction(CompOpts, PP, |
| 1148 | InFile, PA, OS, |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1149 | OutPath, Context)); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1150 | |
| 1151 | if (!Consumer.get()) { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 1152 | Diags.Report(diag::err_fe_invalid_ast_action); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1153 | return; |
| 1154 | } |
| 1155 | |
Daniel Dunbar | a674bf4 | 2009-09-21 03:03:56 +0000 | [diff] [blame] | 1156 | // Set the main file ID to an empty file. |
| 1157 | // |
| 1158 | // FIXME: We probably shouldn't need this, but for now this is the simplest |
| 1159 | // way to reuse the logic in ParseAST. |
| 1160 | const char *EmptyStr = ""; |
| 1161 | llvm::MemoryBuffer *SB = |
| 1162 | llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<dummy input>"); |
| 1163 | AST->getSourceManager().createMainFileIDForMemBuffer(SB); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1164 | |
Daniel Dunbar | a674bf4 | 2009-09-21 03:03:56 +0000 | [diff] [blame] | 1165 | // Stream the input AST to the consumer. |
Daniel Dunbar | efcbe94 | 2009-11-05 02:42:12 +0000 | [diff] [blame] | 1166 | Diags.getClient()->BeginSourceFile(PP.getLangOptions()); |
Daniel Dunbar | a674bf4 | 2009-09-21 03:03:56 +0000 | [diff] [blame] | 1167 | ParseAST(PP, Consumer.get(), AST->getASTContext(), Stats); |
Daniel Dunbar | efcbe94 | 2009-11-05 02:42:12 +0000 | [diff] [blame] | 1168 | Diags.getClient()->EndSourceFile(); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1169 | |
| 1170 | // Release the consumer and the AST, in that order since the consumer may |
| 1171 | // perform actions in its destructor which require the context. |
| 1172 | if (DisableFree) { |
| 1173 | Consumer.take(); |
| 1174 | AST.take(); |
| 1175 | } else { |
| 1176 | Consumer.reset(); |
| 1177 | AST.reset(); |
| 1178 | } |
| 1179 | |
| 1180 | // Always delete the output stream because we don't want to leak file |
| 1181 | // handles. Also, we don't want to try to erase an open file. |
| 1182 | OS.reset(); |
| 1183 | |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 1184 | // If we had errors, try to erase the output file. |
| 1185 | if (PP.getDiagnostics().getNumErrors() && !OutPath.isEmpty()) |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1186 | OutPath.eraseFromDisk(); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1189 | static llvm::cl::list<std::string> |
| 1190 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 1191 | |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 1192 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 1193 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 1194 | |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 1195 | Diags.Report(diag::err_fe_error_backend) << Message; |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 1196 | |
| 1197 | // We cannot recover from llvm errors. |
| 1198 | exit(1); |
| 1199 | } |
| 1200 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1201 | static LangKind GetLanguage() { |
| 1202 | // If -x was given, that's the language. |
| 1203 | if (BaseLang != langkind_unspecified) |
| 1204 | return BaseLang; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1205 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1206 | // Otherwise guess it from the input filenames; |
| 1207 | LangKind LK = langkind_unspecified; |
| 1208 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
| 1209 | llvm::StringRef Name(InputFilenames[i]); |
| 1210 | LangKind ThisKind = llvm::StringSwitch<LangKind>(Name.rsplit('.').second) |
| 1211 | .Case("ast", langkind_ast) |
| 1212 | .Case("c", langkind_c) |
| 1213 | .Cases("S", "s", langkind_asm_cpp) |
| 1214 | .Case("i", langkind_c_cpp) |
| 1215 | .Case("ii", langkind_cxx_cpp) |
| 1216 | .Case("m", langkind_objc) |
| 1217 | .Case("mi", langkind_objc_cpp) |
| 1218 | .Cases("mm", "M", langkind_objcxx) |
| 1219 | .Case("mii", langkind_objcxx_cpp) |
| 1220 | .Case("C", langkind_cxx) |
| 1221 | .Cases("C", "cc", "cp", langkind_cxx) |
| 1222 | .Cases("cpp", "CPP", "c++", "cxx", langkind_cxx) |
| 1223 | .Case("cl", langkind_ocl) |
| 1224 | .Default(langkind_c); |
| 1225 | |
| 1226 | if (LK != langkind_unspecified && ThisKind != LK) { |
| 1227 | llvm::errs() << "error: cannot have multiple input files of distinct " |
| 1228 | << "language kinds without -x\n"; |
| 1229 | exit(1); |
| 1230 | } |
| 1231 | |
| 1232 | LK = ThisKind; |
| 1233 | } |
| 1234 | |
| 1235 | return LK; |
| 1236 | } |
| 1237 | |
| 1238 | static void ConstructDiagnosticOptions(DiagnosticOptions &Opts) { |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1239 | // Initialize the diagnostic options. |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1240 | Opts.ShowColumn = !NoShowColumn; |
| 1241 | Opts.ShowLocation = !NoShowLocation; |
| 1242 | Opts.ShowCarets = !NoCaretDiagnostics; |
| 1243 | Opts.ShowFixits = !NoDiagnosticsFixIt; |
| 1244 | Opts.ShowSourceRanges = PrintSourceRangeInfo; |
| 1245 | Opts.ShowOptionNames = PrintDiagnosticOption; |
| 1246 | Opts.ShowColors = PrintColorDiagnostic; |
| 1247 | Opts.MessageLength = MessageLength; |
| 1248 | } |
| 1249 | |
Daniel Dunbar | 0498cfc | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 1250 | static void FinalizeCompileOptions(CompileOptions &Opts, |
| 1251 | const LangOptions &Lang) { |
| 1252 | if (Lang.NoBuiltin) |
| 1253 | Opts.SimplifyLibCalls = 0; |
| 1254 | if (Lang.CPlusPlus) |
| 1255 | Opts.NoCommon = 1; |
| 1256 | |
| 1257 | // Handle -ftime-report. |
| 1258 | Opts.TimePasses = TimeReport; |
| 1259 | } |
| 1260 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1261 | static void ConstructCompilerInvocation(CompilerInvocation &Opts, |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1262 | const char *Argv0, |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1263 | const DiagnosticOptions &DiagOpts, |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1264 | TargetInfo &Target, |
| 1265 | LangKind LK) { |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1266 | Opts.getDiagnosticOpts() = DiagOpts; |
| 1267 | |
| 1268 | Opts.getOutputFile() = OutputFile; |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1269 | |
| 1270 | // Compute the feature set, which may effect the language. |
| 1271 | ComputeFeatureMap(Target, Opts.getTargetFeatures()); |
Daniel Dunbar | 36f4ec3 | 2009-11-10 16:19:45 +0000 | [diff] [blame] | 1272 | |
Daniel Dunbar | fcb0c3b | 2009-11-10 18:47:35 +0000 | [diff] [blame] | 1273 | // Initialize backend options, which may also be used to key some language |
| 1274 | // options. |
| 1275 | InitializeCompileOptions(Opts.getCompileOpts(), Opts.getTargetFeatures()); |
| 1276 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1277 | // Initialize language options. |
| 1278 | LangOptions LangInfo; |
Daniel Dunbar | 36f4ec3 | 2009-11-10 16:19:45 +0000 | [diff] [blame] | 1279 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1280 | // FIXME: These aren't used during operations on ASTs. Split onto a separate |
| 1281 | // code path to make this obvious. |
Daniel Dunbar | 5674908 | 2009-11-11 07:26:12 +0000 | [diff] [blame] | 1282 | if (LK != langkind_ast) |
| 1283 | InitializeLangOptions(Opts.getLangOpts(), LK, Target, |
| 1284 | Opts.getCompileOpts(), Opts.getTargetFeatures()); |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Initialize the header search options. |
Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame^] | 1287 | InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(), |
| 1288 | GetBuiltinIncludePath(Argv0), |
| 1289 | Verbose, |
| 1290 | Opts.getLangOpts()); |
Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Initialize the other preprocessor options. |
| 1293 | InitializePreprocessorOptions(Opts.getPreprocessorOpts()); |
Daniel Dunbar | 36f4ec3 | 2009-11-10 16:19:45 +0000 | [diff] [blame] | 1294 | |
Daniel Dunbar | 0498cfc | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 1295 | // Finalize some code generation options. |
Daniel Dunbar | fcb0c3b | 2009-11-10 18:47:35 +0000 | [diff] [blame] | 1296 | FinalizeCompileOptions(Opts.getCompileOpts(), Opts.getLangOpts()); |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1299 | int main(int argc, char **argv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 1301 | llvm::PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | d720046 | 2009-07-16 00:14:12 +0000 | [diff] [blame] | 1302 | llvm::LLVMContext &Context = llvm::getGlobalContext(); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 1303 | |
Daniel Dunbar | 4d86151 | 2009-09-03 04:54:12 +0000 | [diff] [blame] | 1304 | // Initialize targets first, so that --version shows registered targets. |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 1305 | llvm::InitializeAllTargets(); |
| 1306 | llvm::InitializeAllAsmPrinters(); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 1307 | |
| 1308 | llvm::cl::ParseCommandLineOptions(argc, argv, |
| 1309 | "LLVM 'Clang' Compiler: http://clang.llvm.org\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1311 | if (TimeReport) |
| 1312 | ClangFrontendTimer = new llvm::Timer("Clang front-end time"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | |
Daniel Dunbar | 08e6dc6 | 2009-05-28 16:37:33 +0000 | [diff] [blame] | 1314 | if (Verbose) |
Mike Stump | 3cbf5a0 | 2009-09-15 21:49:22 +0000 | [diff] [blame] | 1315 | llvm::errs() << "clang-cc version " CLANG_VERSION_STRING |
| 1316 | << " based upon " << PACKAGE_STRING |
Daniel Dunbar | 2e30e59 | 2009-09-04 17:43:10 +0000 | [diff] [blame] | 1317 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1319 | // If no input was specified, read from stdin. |
| 1320 | if (InputFilenames.empty()) |
| 1321 | InputFilenames.push_back("-"); |
Douglas Gregor | 68a0d78 | 2009-05-02 00:03:46 +0000 | [diff] [blame] | 1322 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1323 | // Construct the diagnostic options first, which cannot fail, so that we can |
| 1324 | // build a diagnostic client to use for any errors during option handling. |
| 1325 | DiagnosticOptions DiagOpts; |
| 1326 | ConstructDiagnosticOptions(DiagOpts); |
Daniel Dunbar | eace874 | 2009-11-04 06:24:30 +0000 | [diff] [blame] | 1327 | |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1328 | // Create the diagnostic client for reporting errors or for |
| 1329 | // implementing -verify. |
Chris Lattner | 409d4e7 | 2009-04-17 20:40:01 +0000 | [diff] [blame] | 1330 | llvm::OwningPtr<DiagnosticClient> DiagClient; |
| 1331 | if (VerifyDiagnostics) { |
| 1332 | // When checking diagnostics, just buffer them up. |
| 1333 | DiagClient.reset(new TextDiagnosticBuffer()); |
| 1334 | if (InputFilenames.size() != 1) { |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1335 | fprintf(stderr, "-verify only works on single input files.\n"); |
Chris Lattner | 409d4e7 | 2009-04-17 20:40:01 +0000 | [diff] [blame] | 1336 | return 1; |
| 1337 | } |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1338 | } else { |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1339 | DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), DiagOpts)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1340 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | |
Daniel Dunbar | ad451cc | 2009-11-05 02:11:37 +0000 | [diff] [blame] | 1342 | if (!DumpBuildInformation.empty()) |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1343 | SetUpBuildDumpLog(DiagOpts, argc, argv, DiagClient); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1344 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1345 | // Configure our handling of diagnostics. |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1346 | Diagnostic Diags(DiagClient.get()); |
Eli Friedman | 0eeb86e | 2009-05-19 01:17:04 +0000 | [diff] [blame] | 1347 | if (ProcessWarningOptions(Diags, OptWarnings, OptPedantic, OptPedanticErrors, |
| 1348 | OptNoWarnings)) |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 1349 | return 1; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1350 | |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 1351 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 1352 | // error handler. |
| 1353 | llvm::llvm_install_error_handler(LLVMErrorHandler, |
| 1354 | static_cast<void*>(&Diags)); |
| 1355 | |
Daniel Dunbar | 7c15e71 | 2009-10-30 18:12:31 +0000 | [diff] [blame] | 1356 | // Initialize base triple. If a -triple option has been specified, use |
| 1357 | // that triple. Otherwise, default to the host triple. |
| 1358 | llvm::Triple Triple(TargetTriple); |
| 1359 | if (Triple.getTriple().empty()) |
| 1360 | Triple = llvm::Triple(llvm::sys::getHostTriple()); |
| 1361 | |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1362 | // Get information about the target being compiled for. |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1363 | llvm::OwningPtr<TargetInfo> |
Chris Lattner | 2f60af7 | 2009-09-12 22:45:58 +0000 | [diff] [blame] | 1364 | Target(TargetInfo::CreateTargetInfo(Triple.getTriple())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1366 | if (Target == 0) { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 1367 | Diags.Report(diag::err_fe_unknown_triple) << Triple.getTriple().c_str(); |
Sebastian Redl | c5613db | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 1368 | return 1; |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | |
Daniel Dunbar | 73b7959 | 2009-09-14 00:02:12 +0000 | [diff] [blame] | 1371 | // Set the target ABI if specified. |
| 1372 | if (!TargetABI.empty()) { |
| 1373 | if (!Target->setABI(TargetABI)) { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 1374 | Diags.Report(diag::err_fe_unknown_target_abi) << TargetABI; |
Daniel Dunbar | 73b7959 | 2009-09-14 00:02:12 +0000 | [diff] [blame] | 1375 | return 1; |
| 1376 | } |
| 1377 | } |
| 1378 | |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 1379 | if (!InheritanceViewCls.empty()) // C++ visualization? |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1380 | ProgAction = InheritanceView; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1382 | // Infer the input language. |
| 1383 | // |
| 1384 | // FIXME: We should move .ast inputs to taking a separate path, they are |
| 1385 | // really quite different. |
| 1386 | LangKind LK = GetLanguage(); |
| 1387 | |
| 1388 | // Now that we have initialized the diagnostics engine and the target, finish |
| 1389 | // setting up the compiler invocation. |
| 1390 | CompilerInvocation CompOpts; |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1391 | ConstructCompilerInvocation(CompOpts, argv[0], DiagOpts, *Target, LK); |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1392 | |
Daniel Dunbar | 4cc1a25 | 2009-11-05 01:53:23 +0000 | [diff] [blame] | 1393 | // Create the source manager. |
| 1394 | SourceManager SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 1396 | // Create a file manager object to provide access to and cache the filesystem. |
| 1397 | FileManager FileMgr; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1398 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1399 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1400 | const std::string &InFile = InputFilenames[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1402 | // AST inputs are handled specially. |
| 1403 | if (LK == langkind_ast) { |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1404 | ProcessASTInputFile(CompOpts, InFile, ProgAction, Diags, FileMgr, |
| 1405 | Context); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1406 | continue; |
| 1407 | } |
| 1408 | |
Daniel Dunbar | 4cc1a25 | 2009-11-05 01:53:23 +0000 | [diff] [blame] | 1409 | // Reset the ID tables if we are reusing the SourceManager. |
| 1410 | if (i) |
| 1411 | SourceMgr.clearIDTables(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1413 | // Process the -I options and set them in the HeaderInfo. |
| 1414 | HeaderSearch HeaderInfo(FileMgr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1416 | // Apply all the options to the header search object. |
| 1417 | ApplyHeaderSearchOptions(CompOpts.getHeaderSearchOpts(), HeaderInfo, |
| 1418 | CompOpts.getLangOpts(), Triple); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1420 | // Set up the preprocessor with these options. |
Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 1421 | llvm::OwningPtr<Preprocessor> |
| 1422 | PP(CreatePreprocessor(Diags, CompOpts.getLangOpts(), |
| 1423 | CompOpts.getPreprocessorOpts(), *Target, SourceMgr, |
| 1424 | HeaderInfo)); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1425 | |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 1426 | // Handle generating dependencies, if requested. |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 1427 | if (!DependencyFile.empty()) { |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 1428 | if (DependencyTargets.empty()) { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 1429 | Diags.Report(diag::err_fe_dependency_file_requires_MT); |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 1430 | continue; |
| 1431 | } |
| 1432 | std::string ErrStr; |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 1433 | llvm::raw_ostream *DependencyOS = |
Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 1434 | new llvm::raw_fd_ostream(DependencyFile.c_str(), ErrStr); |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 1435 | if (!ErrStr.empty()) { |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 1436 | Diags.Report(diag::err_fe_error_opening) << DependencyFile << ErrStr; |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 1437 | continue; |
| 1438 | } |
| 1439 | |
| 1440 | AttachDependencyFileGen(PP.get(), DependencyOS, DependencyTargets, |
| 1441 | DependenciesIncludeSystemHeaders, |
| 1442 | PhonyDependencyTarget); |
| 1443 | } |
| 1444 | |
Daniel Dunbar | e0a9581 | 2009-11-10 22:09:38 +0000 | [diff] [blame] | 1445 | if (CompOpts.getPreprocessorOpts().getImplicitPCHInclude().empty()) { |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 1446 | if (InitializeSourceManager(*PP.get(), InFile)) |
| 1447 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 1449 | // Initialize builtin info. |
| 1450 | PP->getBuiltinInfo().InitializeBuiltins(PP->getIdentifierTable(), |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 1451 | PP->getLangOptions().NoBuiltin); |
| 1452 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1453 | |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1454 | // Process the source file. |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1455 | Diags.getClient()->BeginSourceFile(CompOpts.getLangOpts()); |
| 1456 | ProcessInputFile(CompOpts, *PP, InFile, ProgAction, Context); |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1457 | Diags.getClient()->EndSourceFile(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | 4046965 | 2009-04-17 20:16:08 +0000 | [diff] [blame] | 1459 | HeaderInfo.ClearFileInfo(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1460 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1461 | |
Daniel Dunbar | 9253e49 | 2009-11-10 00:46:12 +0000 | [diff] [blame] | 1462 | if (CompOpts.getDiagnosticOpts().ShowCarets) |
Mike Stump | fc0fed3 | 2009-04-28 01:19:10 +0000 | [diff] [blame] | 1463 | if (unsigned NumDiagnostics = Diags.getNumDiagnostics()) |
| 1464 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 1465 | (NumDiagnostics == 1 ? "" : "s")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1467 | if (Stats) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1468 | FileMgr.PrintStats(); |
| 1469 | fprintf(stderr, "\n"); |
| 1470 | } |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1471 | |
| 1472 | delete ClangFrontendTimer; |
| 1473 | delete BuildLogFile; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1475 | // If verifying diagnostics and we reached here, all is well. |
| 1476 | if (VerifyDiagnostics) |
| 1477 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 1479 | // Managed static deconstruction. Useful for making things like |
| 1480 | // -time-passes usable. |
| 1481 | llvm::llvm_shutdown(); |
| 1482 | |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 1483 | return (Diags.getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1484 | } |