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