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