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 | //===----------------------------------------------------------------------===// |
| 17 | // |
| 18 | // TODO: Options to support: |
| 19 | // |
Chris Lattner | dddaa9c | 2009-02-18 01:17:01 +0000 | [diff] [blame] | 20 | // -Wfatal-errors |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | // -ftabstop=width |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/AnalysisConsumer.h" |
Eli Friedman | 8ceb0d9 | 2009-05-18 23:02:01 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/ASTConsumers.h" |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/ASTUnit.h" |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/CompileOptions.h" |
Daniel Dunbar | eace874 | 2009-11-04 06:24:30 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/DiagnosticOptions.h" |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 31 | #include "clang/Frontend/FixItRewriter.h" |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 32 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 33 | #include "clang/Frontend/HeaderSearchOptions.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 34 | #include "clang/Frontend/PCHReader.h" |
Daniel Dunbar | 8863b98 | 2009-11-07 04:20:15 +0000 | [diff] [blame] | 35 | #include "clang/Frontend/PathDiagnosticClients.h" |
| 36 | #include "clang/Frontend/PreprocessorOptions.h" |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 37 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 38 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Argyrios Kyrtzidis | 34d25d8 | 2009-06-23 22:01:39 +0000 | [diff] [blame] | 39 | #include "clang/Frontend/CommandLineSourceLoc.h" |
Eli Friedman | b09f6e1 | 2009-05-19 04:14:29 +0000 | [diff] [blame] | 40 | #include "clang/Frontend/Utils.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 41 | #include "clang/Analysis/PathDiagnostic.h" |
Chris Lattner | 8ee3c03 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 42 | #include "clang/CodeGen/ModuleBuilder.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 43 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 44 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 45 | #include "clang/Sema/SemaDiagnostic.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 46 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 1266eca | 2009-03-28 04:31:31 +0000 | [diff] [blame] | 47 | #include "clang/AST/ASTContext.h" |
| 48 | #include "clang/AST/Decl.h" |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 49 | #include "clang/AST/DeclGroup.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | #include "clang/Parse/Parser.h" |
| 51 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | db76684 | 2009-02-06 04:16:41 +0000 | [diff] [blame] | 52 | #include "clang/Lex/LexDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | #include "clang/Basic/FileManager.h" |
| 54 | #include "clang/Basic/SourceManager.h" |
| 55 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | aa338bc | 2009-05-06 04:07:06 +0000 | [diff] [blame] | 56 | #include "clang/Basic/Version.h" |
Owen Anderson | 42253cc | 2009-07-01 17:00:06 +0000 | [diff] [blame] | 57 | #include "llvm/LLVMContext.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 8f3dab8 | 2007-12-15 23:20:07 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/StringMap.h" |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/StringSwitch.h" |
Chris Lattner | b8e240e | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 63 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 64 | #include "llvm/Config/config.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 66 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 67 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | #include "llvm/Support/MemoryBuffer.h" |
Zhongxing Xu | 2092236 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 69 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 70 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 71 | #include "llvm/Support/Timer.h" |
Chris Lattner | 0fa0daa | 2009-08-24 04:11:30 +0000 | [diff] [blame] | 72 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 73 | #include "llvm/System/Host.h" |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 74 | #include "llvm/System/Path.h" |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 75 | #include "llvm/System/Program.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 76 | #include "llvm/System/Signals.h" |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 77 | #include "llvm/Target/TargetSelect.h" |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 78 | #include <cstdlib> |
Douglas Gregor | 44cf08e | 2009-05-03 03:52:38 +0000 | [diff] [blame] | 79 | #if HAVE_SYS_TYPES_H |
Douglas Gregor | 68a0d78 | 2009-05-02 00:03:46 +0000 | [diff] [blame] | 80 | # include <sys/types.h> |
| 81 | #endif |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 82 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | using namespace clang; |
| 84 | |
| 85 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 86 | // Source Location Parser |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | |
Argyrios Kyrtzidis | 34d25d8 | 2009-06-23 22:01:39 +0000 | [diff] [blame] | 89 | static bool ResolveParsedLocation(ParsedSourceLocation &ParsedLoc, |
| 90 | FileManager &FileMgr, |
| 91 | RequestedSourceLocation &Result) { |
| 92 | const FileEntry *File = FileMgr.getFile(ParsedLoc.FileName); |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 93 | if (!File) |
| 94 | return true; |
| 95 | |
| 96 | Result.File = File; |
Argyrios Kyrtzidis | 34d25d8 | 2009-06-23 22:01:39 +0000 | [diff] [blame] | 97 | Result.Line = ParsedLoc.Line; |
| 98 | Result.Column = ParsedLoc.Column; |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 102 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | // Global options. |
| 104 | //===----------------------------------------------------------------------===// |
| 105 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 106 | /// ClangFrontendTimer - The front-end activities should charge time to it with |
| 107 | /// TimeRegion. The -ftime-report option controls whether this will do |
| 108 | /// anything. |
| 109 | llvm::Timer *ClangFrontendTimer = 0; |
| 110 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | static llvm::cl::opt<bool> |
| 112 | Verbose("v", llvm::cl::desc("Enable verbose output")); |
| 113 | static llvm::cl::opt<bool> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | Stats("print-stats", |
Nate Begeman | aabbb12 | 2007-12-30 01:38:50 +0000 | [diff] [blame] | 115 | llvm::cl::desc("Print performance metrics and statistics")); |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 116 | static llvm::cl::opt<bool> |
| 117 | DisableFree("disable-free", |
| 118 | llvm::cl::desc("Disable freeing of memory on exit"), |
| 119 | llvm::cl::init(false)); |
Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 120 | static llvm::cl::opt<bool> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | EmptyInputOnly("empty-input-only", |
Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 122 | llvm::cl::desc("Force running on an empty input file")); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | |
| 124 | enum ProgActions { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 125 | RewriteObjC, // ObjC->C Rewriter. |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 126 | RewriteBlocks, // ObjC->C Rewriter for Blocks. |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 127 | RewriteMacros, // Expand macros but not #includes. |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 128 | RewriteTest, // Rewriter playground |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 129 | FixIt, // Fix-It Rewriter |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 130 | HTMLTest, // HTML displayer testing stuff. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 131 | EmitAssembly, // Emit a .s file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | EmitLLVM, // Emit a .ll file. |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 133 | EmitBC, // Emit a .bc file. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | EmitLLVMOnly, // Generate LLVM IR, but do not |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 135 | EmitHTML, // Translate input source into HTML. |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 136 | ASTPrint, // Parse ASTs and print them. |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 137 | ASTPrintXML, // Parse ASTs and print them in XML. |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 138 | ASTDump, // Parse ASTs and dump them. |
| 139 | ASTView, // Parse ASTs and view them in Graphviz. |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 140 | PrintDeclContext, // Print DeclContext and their Decls. |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 141 | DumpRecordLayouts, // Dump record layout information. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | ParsePrintCallbacks, // Parse and print each callback. |
| 143 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
| 144 | ParseNoop, // Parse with noop callbacks. |
| 145 | RunPreprocessorOnly, // Just lex, no output. |
| 146 | PrintPreprocessedInput, // -E mode. |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 147 | DumpTokens, // Dump out preprocessed tokens. |
| 148 | DumpRawTokens, // Dump out raw tokens. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | RunAnalysis, // Run one or more source code analyses. |
Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 150 | GeneratePTH, // Generate pre-tokenized header. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 151 | GeneratePCH, // Generate pre-compiled header. |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 152 | InheritanceView // View C++ inheritance for a specified class. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | static llvm::cl::opt<ProgActions> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 157 | llvm::cl::init(ParseSyntaxOnly), |
| 158 | llvm::cl::values( |
| 159 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 160 | "Just run preprocessor, no output (for timings)"), |
| 161 | clEnumValN(PrintPreprocessedInput, "E", |
| 162 | "Run preprocessor, emit preprocessed file"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 163 | clEnumValN(DumpRawTokens, "dump-raw-tokens", |
| 164 | "Lex file in raw mode and dump raw tokens"), |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 165 | clEnumValN(RunAnalysis, "analyze", |
| 166 | "Run static analysis engine"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 167 | clEnumValN(DumpTokens, "dump-tokens", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | "Run preprocessor, dump internal rep of tokens"), |
| 169 | clEnumValN(ParseNoop, "parse-noop", |
| 170 | "Run parser with noop callbacks (for timings)"), |
| 171 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 172 | "Run parser and perform semantic analysis"), |
| 173 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 174 | "Run parser and print each callback invoked"), |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 175 | clEnumValN(EmitHTML, "emit-html", |
| 176 | "Output input source as HTML"), |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 177 | clEnumValN(ASTPrint, "ast-print", |
| 178 | "Build ASTs and then pretty-print them"), |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 179 | clEnumValN(ASTPrintXML, "ast-print-xml", |
| 180 | "Build ASTs and then print them in XML format"), |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 181 | clEnumValN(ASTDump, "ast-dump", |
| 182 | "Build ASTs and then debug dump them"), |
Chris Lattner | ea254db | 2007-10-11 00:37:43 +0000 | [diff] [blame] | 183 | clEnumValN(ASTView, "ast-view", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 184 | "Build ASTs and view them with GraphViz"), |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 185 | clEnumValN(PrintDeclContext, "print-decl-contexts", |
Ted Kremenek | 08478eb | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 186 | "Print DeclContexts and their Decls"), |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 187 | clEnumValN(DumpRecordLayouts, "dump-record-layouts", |
| 188 | "Dump record layout information"), |
Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 189 | clEnumValN(GeneratePTH, "emit-pth", |
Ted Kremenek | 08478eb | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 190 | "Generate pre-tokenized header file"), |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 191 | clEnumValN(GeneratePCH, "emit-pch", |
| 192 | "Generate pre-compiled header file"), |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 193 | clEnumValN(EmitAssembly, "S", |
| 194 | "Emit native assembly code"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | clEnumValN(EmitLLVM, "emit-llvm", |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 196 | "Build ASTs then convert to LLVM, emit .ll file"), |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 197 | clEnumValN(EmitBC, "emit-llvm-bc", |
| 198 | "Build ASTs then convert to LLVM, emit .bc file"), |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 199 | clEnumValN(EmitLLVMOnly, "emit-llvm-only", |
| 200 | "Build ASTs and convert to LLVM, discarding output"), |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 201 | clEnumValN(RewriteTest, "rewrite-test", |
| 202 | "Rewriter playground"), |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 203 | clEnumValN(RewriteObjC, "rewrite-objc", |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 204 | "Rewrite ObjC into C (code rewriter example)"), |
| 205 | clEnumValN(RewriteMacros, "rewrite-macros", |
| 206 | "Expand macros without full preprocessing"), |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 207 | clEnumValN(RewriteBlocks, "rewrite-blocks", |
| 208 | "Rewrite Blocks to C"), |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 209 | clEnumValN(FixIt, "fixit", |
| 210 | "Apply fix-it advice to the input source"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 211 | clEnumValEnd)); |
| 212 | |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 213 | |
| 214 | static llvm::cl::opt<std::string> |
| 215 | OutputFile("o", |
Ted Kremenek | 50b5641 | 2007-12-19 19:50:41 +0000 | [diff] [blame] | 216 | llvm::cl::value_desc("path"), |
Douglas Gregor | 370187c | 2009-04-22 21:45:53 +0000 | [diff] [blame] | 217 | llvm::cl::desc("Specify output file")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 219 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 220 | enum CodeCompletionPrinter { |
| 221 | CCP_Debug, |
| 222 | CCP_CIndex |
| 223 | }; |
| 224 | |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 225 | static llvm::cl::opt<ParsedSourceLocation> |
| 226 | CodeCompletionAt("code-completion-at", |
| 227 | llvm::cl::value_desc("file:line:column"), |
| 228 | llvm::cl::desc("Dump code-completion information at a location")); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 230 | static llvm::cl::opt<CodeCompletionPrinter> |
| 231 | CodeCompletionPrinter("code-completion-printer", |
| 232 | llvm::cl::desc("Choose output type:"), |
| 233 | llvm::cl::init(CCP_Debug), |
| 234 | llvm::cl::values( |
| 235 | clEnumValN(CCP_Debug, "debug", |
| 236 | "Debug code-completion results"), |
| 237 | clEnumValN(CCP_CIndex, "cindex", |
| 238 | "Code-completion results for the CIndex library"), |
| 239 | clEnumValEnd)); |
| 240 | |
| 241 | static llvm::cl::opt<bool> |
| 242 | CodeCompletionWantsMacros("code-completion-macros", |
| 243 | llvm::cl::desc("Include macros in code-completion results")); |
| 244 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 245 | /// \brief Buld a new code-completion consumer that prints the results of |
| 246 | /// code completion to standard output. |
| 247 | static CodeCompleteConsumer *BuildPrintingCodeCompleter(Sema &S, void *) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 248 | switch (CodeCompletionPrinter.getValue()) { |
| 249 | case CCP_Debug: |
| 250 | return new PrintingCodeCompleteConsumer(S, CodeCompletionWantsMacros, |
| 251 | llvm::outs()); |
| 252 | |
| 253 | case CCP_CIndex: |
| 254 | return new CIndexCodeCompleteConsumer(S, CodeCompletionWantsMacros, |
| 255 | llvm::outs()); |
| 256 | }; |
| 257 | |
| 258 | return 0; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 261 | //===----------------------------------------------------------------------===// |
| 262 | // PTH. |
| 263 | //===----------------------------------------------------------------------===// |
| 264 | |
| 265 | static llvm::cl::opt<std::string> |
| 266 | TokenCache("token-cache", llvm::cl::value_desc("path"), |
| 267 | llvm::cl::desc("Use specified token cache file")); |
| 268 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 269 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 270 | // Diagnostic Options |
| 271 | //===----------------------------------------------------------------------===// |
| 272 | |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 273 | static llvm::cl::opt<bool> |
| 274 | VerifyDiagnostics("verify", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 275 | llvm::cl::desc("Verify emitted diagnostics and warnings")); |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 276 | |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 277 | static llvm::cl::opt<bool> |
| 278 | NoShowColumn("fno-show-column", |
| 279 | llvm::cl::desc("Do not include column number on diagnostics")); |
| 280 | |
| 281 | static llvm::cl::opt<bool> |
Chris Lattner | 65f5e64 | 2009-01-30 19:01:41 +0000 | [diff] [blame] | 282 | NoShowLocation("fno-show-source-location", |
| 283 | llvm::cl::desc("Do not include source location information with" |
| 284 | " diagnostics")); |
| 285 | |
| 286 | static llvm::cl::opt<bool> |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 287 | NoCaretDiagnostics("fno-caret-diagnostics", |
| 288 | llvm::cl::desc("Do not include source line and caret with" |
| 289 | " diagnostics")); |
| 290 | |
Chris Lattner | 1fbee5d | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 291 | static llvm::cl::opt<bool> |
Chris Lattner | aa5bf2e | 2009-04-19 07:44:08 +0000 | [diff] [blame] | 292 | NoDiagnosticsFixIt("fno-diagnostics-fixit-info", |
| 293 | llvm::cl::desc("Do not include fixit information in" |
| 294 | " diagnostics")); |
| 295 | |
| 296 | static llvm::cl::opt<bool> |
Chris Lattner | 182e092 | 2009-04-21 05:34:31 +0000 | [diff] [blame] | 297 | PrintSourceRangeInfo("fdiagnostics-print-source-range-info", |
| 298 | llvm::cl::desc("Print source range spans in numeric form")); |
| 299 | |
Chris Lattner | d51d74a | 2009-04-16 05:44:38 +0000 | [diff] [blame] | 300 | static llvm::cl::opt<bool> |
| 301 | PrintDiagnosticOption("fdiagnostics-show-option", |
| 302 | llvm::cl::desc("Print diagnostic name with mappable diagnostics")); |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 303 | |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 304 | static llvm::cl::opt<unsigned> |
| 305 | MessageLength("fmessage-length", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | llvm::cl::desc("Format message diagnostics so that they fit " |
| 307 | "within N columns or fewer, when possible."), |
| 308 | llvm::cl::value_desc("N")); |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 309 | |
Torok Edwin | 603fca7 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 310 | static llvm::cl::opt<bool> |
Daniel Dunbar | 838be48 | 2009-11-04 06:24:57 +0000 | [diff] [blame] | 311 | PrintColorDiagnostic("fcolor-diagnostics", |
| 312 | llvm::cl::desc("Use colors in diagnostics")); |
| 313 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 315 | // C++ Visualization. |
| 316 | //===----------------------------------------------------------------------===// |
| 317 | |
| 318 | static llvm::cl::opt<std::string> |
| 319 | InheritanceViewCls("cxx-inheritance-view", |
| 320 | llvm::cl::value_desc("class name"), |
Daniel Dunbar | d77b251 | 2009-01-14 18:56:36 +0000 | [diff] [blame] | 321 | llvm::cl::desc("View C++ inheritance for a specified class")); |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 322 | |
| 323 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 324 | // Builtin Options |
| 325 | //===----------------------------------------------------------------------===// |
Chris Lattner | b2509e1 | 2009-02-18 01:12:43 +0000 | [diff] [blame] | 326 | |
| 327 | static llvm::cl::opt<bool> |
| 328 | TimeReport("ftime-report", |
| 329 | llvm::cl::desc("Print the amount of time each " |
| 330 | "phase of compilation takes")); |
| 331 | |
Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 332 | static llvm::cl::opt<bool> |
| 333 | Freestanding("ffreestanding", |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 334 | llvm::cl::desc("Assert that the compilation takes place in a " |
Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 335 | "freestanding environment")); |
| 336 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 337 | static llvm::cl::opt<bool> |
Daniel Dunbar | 48d1ef7 | 2009-04-07 21:16:11 +0000 | [diff] [blame] | 338 | AllowBuiltins("fbuiltin", llvm::cl::init(true), |
| 339 | llvm::cl::desc("Disable implicit builtin knowledge of functions")); |
Chris Lattner | 7644f07 | 2009-03-13 22:38:49 +0000 | [diff] [blame] | 340 | |
| 341 | |
| 342 | static llvm::cl::opt<bool> |
Daniel Dunbar | 48d1ef7 | 2009-04-07 21:16:11 +0000 | [diff] [blame] | 343 | MathErrno("fmath-errno", llvm::cl::init(true), |
| 344 | llvm::cl::desc("Require math functions to respect errno")); |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 345 | |
Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 346 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | // Language Options |
| 348 | //===----------------------------------------------------------------------===// |
| 349 | |
| 350 | enum LangKind { |
| 351 | langkind_unspecified, |
| 352 | langkind_c, |
| 353 | langkind_c_cpp, |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 354 | langkind_asm_cpp, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | langkind_cxx, |
| 356 | langkind_cxx_cpp, |
| 357 | langkind_objc, |
| 358 | langkind_objc_cpp, |
| 359 | langkind_objcxx, |
Nate Begeman | 4e3629e | 2009-06-25 22:43:10 +0000 | [diff] [blame] | 360 | langkind_objcxx_cpp, |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 361 | langkind_ocl, |
| 362 | langkind_ast |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | static llvm::cl::opt<LangKind> |
| 366 | BaseLang("x", llvm::cl::desc("Base language to compile"), |
| 367 | llvm::cl::init(langkind_unspecified), |
| 368 | llvm::cl::values(clEnumValN(langkind_c, "c", "C"), |
Nate Begeman | 4e3629e | 2009-06-25 22:43:10 +0000 | [diff] [blame] | 369 | clEnumValN(langkind_ocl, "cl", "OpenCL C"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 370 | clEnumValN(langkind_cxx, "c++", "C++"), |
| 371 | clEnumValN(langkind_objc, "objective-c", "Objective C"), |
| 372 | clEnumValN(langkind_objcxx,"objective-c++","Objective C++"), |
Daniel Dunbar | d2ea386 | 2009-01-29 23:50:47 +0000 | [diff] [blame] | 373 | clEnumValN(langkind_c_cpp, "cpp-output", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 374 | "Preprocessed C"), |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 375 | clEnumValN(langkind_asm_cpp, "assembler-with-cpp", |
| 376 | "Preprocessed asm"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | clEnumValN(langkind_cxx_cpp, "c++-cpp-output", |
Chris Lattner | c76d807 | 2009-02-06 06:19:20 +0000 | [diff] [blame] | 378 | "Preprocessed C++"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 379 | clEnumValN(langkind_objc_cpp, "objective-c-cpp-output", |
| 380 | "Preprocessed Objective C"), |
Chris Lattner | c76d807 | 2009-02-06 06:19:20 +0000 | [diff] [blame] | 381 | clEnumValN(langkind_objcxx_cpp, "objective-c++-cpp-output", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 382 | "Preprocessed Objective C++"), |
Daniel Dunbar | 0b5b0da | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 383 | clEnumValN(langkind_c, "c-header", |
| 384 | "C header"), |
| 385 | clEnumValN(langkind_objc, "objective-c-header", |
| 386 | "Objective-C header"), |
| 387 | clEnumValN(langkind_cxx, "c++-header", |
| 388 | "C++ header"), |
| 389 | clEnumValN(langkind_objcxx, "objective-c++-header", |
| 390 | "Objective-C++ header"), |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 391 | clEnumValN(langkind_ast, "ast", |
| 392 | "Clang AST"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | clEnumValEnd)); |
| 394 | |
| 395 | static llvm::cl::opt<bool> |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 396 | ObjCExclusiveGC("fobjc-gc-only", |
| 397 | llvm::cl::desc("Use GC exclusively for Objective-C related " |
| 398 | "memory management")); |
| 399 | |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 400 | static llvm::cl::opt<std::string> |
| 401 | ObjCConstantStringClass("fconstant-string-class", |
| 402 | llvm::cl::value_desc("class name"), |
| 403 | llvm::cl::desc("Specify the class to use for constant " |
| 404 | "Objective-C string objects.")); |
| 405 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 406 | static llvm::cl::opt<bool> |
| 407 | ObjCEnableGC("fobjc-gc", |
| 408 | llvm::cl::desc("Enable Objective-C garbage collection")); |
| 409 | |
Fariborz Jahanian | 448f5e6 | 2009-04-17 03:04:15 +0000 | [diff] [blame] | 410 | static llvm::cl::opt<bool> |
| 411 | ObjCEnableGCBitmapPrint("print-ivar-layout", |
| 412 | llvm::cl::desc("Enable Objective-C Ivar layout bitmap print trace")); |
| 413 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 414 | static llvm::cl::opt<LangOptions::VisibilityMode> |
| 415 | SymbolVisibility("fvisibility", |
| 416 | llvm::cl::desc("Set the default symbol visibility:"), |
| 417 | llvm::cl::init(LangOptions::Default), |
| 418 | llvm::cl::values(clEnumValN(LangOptions::Default, "default", |
| 419 | "Use default symbol visibility"), |
| 420 | clEnumValN(LangOptions::Hidden, "hidden", |
| 421 | "Use hidden symbol visibility"), |
| 422 | clEnumValN(LangOptions::Protected,"protected", |
| 423 | "Use protected symbol visibility"), |
| 424 | clEnumValEnd)); |
| 425 | |
| 426 | static llvm::cl::opt<bool> |
| 427 | OverflowChecking("ftrapv", |
| 428 | llvm::cl::desc("Trap on integer overflow"), |
| 429 | llvm::cl::init(false)); |
| 430 | |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 431 | static llvm::cl::opt<bool> |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 432 | AltiVec("faltivec", llvm::cl::desc("Enable AltiVec vector initializer syntax"), |
| 433 | llvm::cl::init(false)); |
| 434 | |
| 435 | static llvm::cl::opt<bool> |
Daniel Dunbar | 5345c39 | 2009-09-03 04:54:28 +0000 | [diff] [blame] | 436 | PThread("pthread", llvm::cl::desc("Support POSIX threads in generated code"), |
| 437 | llvm::cl::init(false)); |
| 438 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 439 | static void InitializeCOptions(LangOptions &Options) { |
| 440 | // Do nothing. |
| 441 | } |
| 442 | |
| 443 | static void InitializeObjCOptions(LangOptions &Options) { |
| 444 | Options.ObjC1 = Options.ObjC2 = 1; |
| 445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 447 | |
Daniel Dunbar | 0b5b0da | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 448 | static void InitializeLangOptions(LangOptions &Options, LangKind LK){ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 449 | // FIXME: implement -fpreprocessed mode. |
| 450 | bool NoPreprocess = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 452 | switch (LK) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | default: assert(0 && "Unknown language kind!"); |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 454 | case langkind_asm_cpp: |
Daniel Dunbar | c157145 | 2008-12-01 18:55:22 +0000 | [diff] [blame] | 455 | Options.AsmPreprocessor = 1; |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 456 | // FALLTHROUGH |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 457 | case langkind_c_cpp: |
| 458 | NoPreprocess = true; |
| 459 | // FALLTHROUGH |
| 460 | case langkind_c: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 461 | InitializeCOptions(Options); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 462 | break; |
| 463 | case langkind_cxx_cpp: |
| 464 | NoPreprocess = true; |
| 465 | // FALLTHROUGH |
| 466 | case langkind_cxx: |
| 467 | Options.CPlusPlus = 1; |
| 468 | break; |
| 469 | case langkind_objc_cpp: |
| 470 | NoPreprocess = true; |
| 471 | // FALLTHROUGH |
| 472 | case langkind_objc: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 473 | InitializeObjCOptions(Options); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 474 | break; |
| 475 | case langkind_objcxx_cpp: |
| 476 | NoPreprocess = true; |
| 477 | // FALLTHROUGH |
| 478 | case langkind_objcxx: |
| 479 | Options.ObjC1 = Options.ObjC2 = 1; |
| 480 | Options.CPlusPlus = 1; |
| 481 | break; |
Nate Begeman | 4e3629e | 2009-06-25 22:43:10 +0000 | [diff] [blame] | 482 | case langkind_ocl: |
| 483 | Options.OpenCL = 1; |
| 484 | Options.AltiVec = 1; |
| 485 | Options.CXXOperatorNames = 1; |
| 486 | Options.LaxVectorConversions = 1; |
| 487 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 490 | if (ObjCExclusiveGC) |
| 491 | Options.setGCMode(LangOptions::GCOnly); |
| 492 | else if (ObjCEnableGC) |
| 493 | Options.setGCMode(LangOptions::HybridGC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | |
Fariborz Jahanian | 448f5e6 | 2009-04-17 03:04:15 +0000 | [diff] [blame] | 495 | if (ObjCEnableGCBitmapPrint) |
| 496 | Options.ObjCGCBitmapPrint = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 498 | if (AltiVec) |
| 499 | Options.AltiVec = 1; |
Daniel Dunbar | 5345c39 | 2009-09-03 04:54:28 +0000 | [diff] [blame] | 500 | |
| 501 | if (PThread) |
| 502 | Options.POSIXThreads = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 504 | Options.setVisibilityMode(SymbolVisibility); |
| 505 | Options.OverflowChecking = OverflowChecking; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /// LangStds - Language standards we support. |
| 509 | enum LangStds { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | lang_unspecified, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | lang_c89, lang_c94, lang_c99, |
Douglas Gregor | 214904e | 2009-09-29 14:42:43 +0000 | [diff] [blame] | 512 | lang_gnu89, lang_gnu99, |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 513 | lang_cxx98, lang_gnucxx98, |
| 514 | lang_cxx0x, lang_gnucxx0x |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 515 | }; |
| 516 | |
| 517 | static llvm::cl::opt<LangStds> |
| 518 | LangStd("std", llvm::cl::desc("Language standard to compile for"), |
| 519 | llvm::cl::init(lang_unspecified), |
| 520 | llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"), |
| 521 | clEnumValN(lang_c89, "c90", "ISO C 1990"), |
| 522 | clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"), |
| 523 | clEnumValN(lang_c94, "iso9899:199409", |
| 524 | "ISO C 1990 with amendment 1"), |
| 525 | clEnumValN(lang_c99, "c99", "ISO C 1999"), |
Chris Lattner | 50748f4 | 2009-04-06 17:17:55 +0000 | [diff] [blame] | 526 | clEnumValN(lang_c99, "c9x", "ISO C 1999"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 527 | clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"), |
Chris Lattner | 50748f4 | 2009-04-06 17:17:55 +0000 | [diff] [blame] | 528 | clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 529 | clEnumValN(lang_gnu89, "gnu89", |
Gabor Greif | 10b2614 | 2009-02-28 09:22:15 +0000 | [diff] [blame] | 530 | "ISO C 1990 with GNU extensions"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 531 | clEnumValN(lang_gnu99, "gnu99", |
Gabor Greif | 10b2614 | 2009-02-28 09:22:15 +0000 | [diff] [blame] | 532 | "ISO C 1999 with GNU extensions (default for C)"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 533 | clEnumValN(lang_gnu99, "gnu9x", |
| 534 | "ISO C 1999 with GNU extensions"), |
| 535 | clEnumValN(lang_cxx98, "c++98", |
| 536 | "ISO C++ 1998 with amendments"), |
| 537 | clEnumValN(lang_gnucxx98, "gnu++98", |
| 538 | "ISO C++ 1998 with amendments and GNU " |
| 539 | "extensions (default for C++)"), |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 540 | clEnumValN(lang_cxx0x, "c++0x", |
| 541 | "Upcoming ISO C++ 200x with amendments"), |
| 542 | clEnumValN(lang_gnucxx0x, "gnu++0x", |
| 543 | "Upcoming ISO C++ 200x with amendments and GNU " |
Gabor Greif | 5f8d1db | 2009-03-11 23:07:18 +0000 | [diff] [blame] | 544 | "extensions"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 545 | clEnumValEnd)); |
| 546 | |
| 547 | static llvm::cl::opt<bool> |
| 548 | NoOperatorNames("fno-operator-names", |
| 549 | llvm::cl::desc("Do not treat C++ operator name keywords as " |
| 550 | "synonyms for operators")); |
| 551 | |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 552 | static llvm::cl::opt<bool> |
| 553 | PascalStrings("fpascal-strings", |
| 554 | llvm::cl::desc("Recognize and construct Pascal-style " |
| 555 | "string literals")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 557 | static llvm::cl::opt<bool> |
| 558 | MSExtensions("fms-extensions", |
| 559 | llvm::cl::desc("Accept some non-standard constructs used in " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 560 | "Microsoft header files ")); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 561 | |
| 562 | static llvm::cl::opt<bool> |
| 563 | WritableStrings("fwritable-strings", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 564 | llvm::cl::desc("Store string literals as writable data")); |
Anders Carlsson | 695dbb6 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 565 | |
| 566 | static llvm::cl::opt<bool> |
Anders Carlsson | ad53eff | 2009-01-30 23:26:40 +0000 | [diff] [blame] | 567 | NoLaxVectorConversions("fno-lax-vector-conversions", |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 568 | llvm::cl::desc("Disallow implicit conversions between " |
| 569 | "vectors with a different number of " |
| 570 | "elements or different element types")); |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 571 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 572 | static llvm::cl::opt<bool> |
Daniel Dunbar | 48d1ef7 | 2009-04-07 21:16:11 +0000 | [diff] [blame] | 573 | EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature")); |
Mike Stump | a0f02aa | 2009-02-02 22:57:57 +0000 | [diff] [blame] | 574 | |
| 575 | static llvm::cl::opt<bool> |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 576 | EnableHeinousExtensions("fheinous-gnu-extensions", |
| 577 | llvm::cl::desc("enable GNU extensions that you really really shouldn't use"), |
| 578 | llvm::cl::ValueDisallowed, llvm::cl::Hidden); |
| 579 | |
| 580 | static llvm::cl::opt<bool> |
Mike Stump | a0f02aa | 2009-02-02 22:57:57 +0000 | [diff] [blame] | 581 | ObjCNonFragileABI("fobjc-nonfragile-abi", |
| 582 | llvm::cl::desc("enable objective-c's nonfragile abi")); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 583 | |
Daniel Dunbar | d6884a0 | 2009-05-04 05:16:21 +0000 | [diff] [blame] | 584 | |
| 585 | static llvm::cl::opt<bool> |
Daniel Dunbar | d604c40 | 2009-02-04 21:19:06 +0000 | [diff] [blame] | 586 | EmitAllDecls("femit-all-decls", |
| 587 | llvm::cl::desc("Emit all declarations, even if unused")); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 588 | |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 589 | static llvm::cl::opt<bool> |
| 590 | Exceptions("fexceptions", |
Chris Lattner | f5db8f8 | 2009-04-19 07:00:02 +0000 | [diff] [blame] | 591 | llvm::cl::desc("Enable support for exception handling")); |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 592 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 593 | static llvm::cl::opt<bool> |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 594 | Rtti("frtti", llvm::cl::init(true), |
| 595 | llvm::cl::desc("Enable generation of rtti information")); |
| 596 | |
| 597 | static llvm::cl::opt<bool> |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 598 | GNURuntime("fgnu-runtime", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 599 | llvm::cl::desc("Generate output compatible with the standard GNU " |
Chris Lattner | f5db8f8 | 2009-04-19 07:00:02 +0000 | [diff] [blame] | 600 | "Objective-C runtime")); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 601 | |
| 602 | static llvm::cl::opt<bool> |
| 603 | NeXTRuntime("fnext-runtime", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 604 | llvm::cl::desc("Generate output compatible with the NeXT " |
Chris Lattner | f5db8f8 | 2009-04-19 07:00:02 +0000 | [diff] [blame] | 605 | "runtime")); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 606 | |
Eli Friedman | c4757bd | 2009-06-05 07:12:17 +0000 | [diff] [blame] | 607 | static llvm::cl::opt<bool> |
| 608 | CharIsSigned("fsigned-char", |
| 609 | llvm::cl::desc("Force char to be a signed/unsigned type")); |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 610 | |
John Thompson | a6fda12 | 2009-11-05 20:14:16 +0000 | [diff] [blame] | 611 | static llvm::cl::opt<bool> |
| 612 | ShortWChar("fshort-wchar", |
| 613 | llvm::cl::desc("Force wchar_t to be a short unsigned int")); |
| 614 | |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 615 | |
| 616 | static llvm::cl::opt<bool> |
Chris Lattner | f5db8f8 | 2009-04-19 07:00:02 +0000 | [diff] [blame] | 617 | Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences")); |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 619 | static llvm::cl::opt<unsigned> |
| 620 | TemplateDepth("ftemplate-depth", llvm::cl::init(99), |
| 621 | llvm::cl::desc("Maximum depth of recursive template " |
| 622 | "instantiation")); |
Chris Lattner | f5db8f8 | 2009-04-19 07:00:02 +0000 | [diff] [blame] | 623 | static llvm::cl::opt<bool> |
| 624 | DollarsInIdents("fdollars-in-identifiers", |
| 625 | llvm::cl::desc("Allow '$' in identifiers")); |
Chris Lattner | 16167a6 | 2009-03-02 22:11:07 +0000 | [diff] [blame] | 626 | |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 627 | |
| 628 | static llvm::cl::opt<bool> |
| 629 | OptSize("Os", llvm::cl::desc("Optimize for size")); |
| 630 | |
| 631 | static llvm::cl::opt<bool> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | DisableLLVMOptimizations("disable-llvm-optzns", |
Daniel Dunbar | 877db38 | 2009-06-02 22:07:45 +0000 | [diff] [blame] | 633 | llvm::cl::desc("Don't run LLVM optimization passes")); |
| 634 | |
| 635 | static llvm::cl::opt<bool> |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 636 | NoCommon("fno-common", |
| 637 | llvm::cl::desc("Compile common globals like normal definitions"), |
| 638 | llvm::cl::ValueDisallowed); |
| 639 | |
Daniel Dunbar | c9abc04 | 2009-04-08 05:11:16 +0000 | [diff] [blame] | 640 | static llvm::cl::opt<std::string> |
| 641 | MainFileName("main-file-name", |
| 642 | llvm::cl::desc("Main file name to use for debug info")); |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 643 | |
Anders Carlsson | a33d9b4 | 2009-05-13 19:49:53 +0000 | [diff] [blame] | 644 | // FIXME: Also add an "-fno-access-control" option. |
| 645 | static llvm::cl::opt<bool> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | AccessControl("faccess-control", |
Anders Carlsson | a33d9b4 | 2009-05-13 19:49:53 +0000 | [diff] [blame] | 647 | llvm::cl::desc("Enable C++ access control")); |
| 648 | |
Anders Carlsson | 92f5822 | 2009-08-22 22:30:33 +0000 | [diff] [blame] | 649 | static llvm::cl::opt<bool> |
| 650 | NoElideConstructors("fno-elide-constructors", |
| 651 | llvm::cl::desc("Disable C++ copy constructor elision")); |
| 652 | |
Tanya Lattner | 59876c2 | 2009-11-04 01:18:09 +0000 | [diff] [blame] | 653 | static llvm::cl::opt<bool> |
| 654 | NoMergeConstants("fno-merge-all-constants", |
| 655 | llvm::cl::desc("Disallow merging of constants.")); |
| 656 | |
Daniel Dunbar | 73b7959 | 2009-09-14 00:02:12 +0000 | [diff] [blame] | 657 | static llvm::cl::opt<std::string> |
| 658 | TargetABI("target-abi", |
| 659 | llvm::cl::desc("Target a particular ABI type")); |
| 660 | |
Daniel Dunbar | 7c15e71 | 2009-10-30 18:12:31 +0000 | [diff] [blame] | 661 | static llvm::cl::opt<std::string> |
| 662 | TargetTriple("triple", |
| 663 | llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)")); |
| 664 | |
Anders Carlsson | 92f5822 | 2009-08-22 22:30:33 +0000 | [diff] [blame] | 665 | |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 666 | // It might be nice to add bounds to the CommandLine library directly. |
| 667 | struct OptLevelParser : public llvm::cl::parser<unsigned> { |
Chris Lattner | 59b2172 | 2009-09-20 00:39:15 +0000 | [diff] [blame] | 668 | bool parse(llvm::cl::Option &O, llvm::StringRef ArgName, |
| 669 | llvm::StringRef Arg, unsigned &Val) { |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 670 | if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val)) |
| 671 | return true; |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 672 | if (Val > 3) |
Benjamin Kramer | 21fde99 | 2009-08-02 12:12:11 +0000 | [diff] [blame] | 673 | return O.error("'" + Arg + "' invalid optimization level!"); |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | }; |
| 677 | static llvm::cl::opt<unsigned, false, OptLevelParser> |
| 678 | OptLevel("O", llvm::cl::Prefix, |
| 679 | llvm::cl::desc("Optimization level"), |
| 680 | llvm::cl::init(0)); |
| 681 | |
Daniel Dunbar | 9fd0b1f | 2009-04-08 03:03:23 +0000 | [diff] [blame] | 682 | static llvm::cl::opt<unsigned> |
Daniel Dunbar | 3bbc753 | 2009-04-08 18:03:55 +0000 | [diff] [blame] | 683 | PICLevel("pic-level", llvm::cl::desc("Value for __PIC__")); |
| 684 | |
| 685 | static llvm::cl::opt<bool> |
| 686 | StaticDefine("static-define", llvm::cl::desc("Should __STATIC__ be defined")); |
Daniel Dunbar | 9fd0b1f | 2009-04-08 03:03:23 +0000 | [diff] [blame] | 687 | |
Bill Wendling | 45483f7 | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 688 | static llvm::cl::opt<int> |
| 689 | StackProtector("stack-protector", |
| 690 | llvm::cl::desc("Enable stack protectors"), |
| 691 | llvm::cl::init(-1)); |
| 692 | |
Daniel Dunbar | dcb4a1a | 2008-08-23 08:43:39 +0000 | [diff] [blame] | 693 | static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, |
Daniel Dunbar | 638c901 | 2009-11-09 22:46:09 +0000 | [diff] [blame] | 694 | TargetInfo &Target, |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 695 | const llvm::StringMap<bool> &Features) { |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 696 | // Allow the target to set the default the language options as it sees fit. |
Daniel Dunbar | 638c901 | 2009-11-09 22:46:09 +0000 | [diff] [blame] | 697 | Target.getDefaultLangOptions(Options); |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 698 | |
| 699 | // Pass the map of target features to the target for validation and |
| 700 | // processing. |
Daniel Dunbar | 638c901 | 2009-11-09 22:46:09 +0000 | [diff] [blame] | 701 | Target.HandleTargetFeatures(Features); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 703 | if (LangStd == lang_unspecified) { |
| 704 | // Based on the base language, pick one. |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 705 | switch (LK) { |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 706 | case langkind_ast: assert(0 && "Invalid call for AST inputs"); |
Ted Kremenek | f2a17b1 | 2009-03-19 19:02:20 +0000 | [diff] [blame] | 707 | case lang_unspecified: assert(0 && "Unknown base language"); |
Nate Begeman | 4e3629e | 2009-06-25 22:43:10 +0000 | [diff] [blame] | 708 | case langkind_ocl: |
| 709 | LangStd = lang_c99; |
| 710 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 711 | case langkind_c: |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 712 | case langkind_asm_cpp: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 713 | case langkind_c_cpp: |
| 714 | case langkind_objc: |
| 715 | case langkind_objc_cpp: |
| 716 | LangStd = lang_gnu99; |
| 717 | break; |
| 718 | case langkind_cxx: |
| 719 | case langkind_cxx_cpp: |
| 720 | case langkind_objcxx: |
| 721 | case langkind_objcxx_cpp: |
| 722 | LangStd = lang_gnucxx98; |
| 723 | break; |
| 724 | } |
| 725 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 727 | switch (LangStd) { |
| 728 | default: assert(0 && "Unknown language standard!"); |
| 729 | |
| 730 | // Fall through from newer standards to older ones. This isn't really right. |
| 731 | // FIXME: Enable specifically the right features based on the language stds. |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 732 | case lang_gnucxx0x: |
| 733 | case lang_cxx0x: |
| 734 | Options.CPlusPlus0x = 1; |
| 735 | // FALL THROUGH |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 736 | case lang_gnucxx98: |
| 737 | case lang_cxx98: |
| 738 | Options.CPlusPlus = 1; |
| 739 | Options.CXXOperatorNames = !NoOperatorNames; |
| 740 | // FALL THROUGH. |
| 741 | case lang_gnu99: |
| 742 | case lang_c99: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 743 | Options.C99 = 1; |
| 744 | Options.HexFloats = 1; |
| 745 | // FALL THROUGH. |
| 746 | case lang_gnu89: |
| 747 | Options.BCPLComment = 1; // Only for C99/C++. |
| 748 | // FALL THROUGH. |
| 749 | case lang_c94: |
Chris Lattner | 3426b9b | 2008-02-25 04:01:39 +0000 | [diff] [blame] | 750 | Options.Digraphs = 1; // C94, C99, C++. |
| 751 | // FALL THROUGH. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 752 | case lang_c89: |
| 753 | break; |
| 754 | } |
Argyrios Kyrtzidis | d146552 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 7e9c90b | 2009-03-20 15:44:26 +0000 | [diff] [blame] | 756 | // GNUMode - Set if we're in gnu99, gnu89, gnucxx98, etc. |
Douglas Gregor | 214904e | 2009-09-29 14:42:43 +0000 | [diff] [blame] | 757 | switch (LangStd) { |
| 758 | default: assert(0 && "Unknown language standard!"); |
| 759 | case lang_gnucxx0x: |
| 760 | case lang_gnucxx98: |
| 761 | case lang_gnu99: |
| 762 | case lang_gnu89: |
| 763 | Options.GNUMode = 1; |
| 764 | break; |
| 765 | case lang_cxx0x: |
| 766 | case lang_cxx98: |
| 767 | case lang_c99: |
| 768 | case lang_c94: |
| 769 | case lang_c89: |
| 770 | Options.GNUMode = 0; |
| 771 | break; |
| 772 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
Argyrios Kyrtzidis | d146552 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 774 | if (Options.CPlusPlus) { |
| 775 | Options.C99 = 0; |
Eli Friedman | ab24c8d | 2009-08-26 20:15:14 +0000 | [diff] [blame] | 776 | Options.HexFloats = 0; |
Argyrios Kyrtzidis | d146552 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 777 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 779 | if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89) |
| 780 | Options.ImplicitInt = 1; |
| 781 | else |
| 782 | Options.ImplicitInt = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Daniel Dunbar | d573d26 | 2009-04-07 22:13:21 +0000 | [diff] [blame] | 784 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs |
| 785 | // is specified, or -std is set to a conforming mode. |
Chris Lattner | 7e9c90b | 2009-03-20 15:44:26 +0000 | [diff] [blame] | 786 | Options.Trigraphs = !Options.GNUMode; |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 787 | if (Trigraphs.getPosition()) |
Chris Lattner | 7e9c90b | 2009-03-20 15:44:26 +0000 | [diff] [blame] | 788 | Options.Trigraphs = Trigraphs; // Command line option wins if specified. |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 789 | |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 790 | // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off |
| 791 | // even if they are normally on for the target. In GNU modes (e.g. |
| 792 | // -std=gnu99) the default for blocks depends on the target settings. |
Anders Carlsson | e56f6ff | 2009-01-21 18:47:36 +0000 | [diff] [blame] | 793 | // However, blocks are not turned off when compiling Obj-C or Obj-C++ code. |
Chris Lattner | 7e9c90b | 2009-03-20 15:44:26 +0000 | [diff] [blame] | 794 | if (!Options.ObjC1 && !Options.GNUMode) |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 795 | Options.Blocks = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 01638a6 | 2009-04-19 07:06:52 +0000 | [diff] [blame] | 797 | // Default to not accepting '$' in identifiers when preprocessing assembler, |
| 798 | // but do accept when preprocessing C. FIXME: these defaults are right for |
| 799 | // darwin, are they right everywhere? |
| 800 | Options.DollarIdents = LK != langkind_asm_cpp; |
| 801 | if (DollarsInIdents.getPosition()) // Explicit setting overrides default. |
Chris Lattner | f5db8f8 | 2009-04-19 07:00:02 +0000 | [diff] [blame] | 802 | Options.DollarIdents = DollarsInIdents; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 803 | |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 804 | if (PascalStrings.getPosition()) |
| 805 | Options.PascalStrings = PascalStrings; |
Eli Friedman | abc4e32 | 2009-06-08 06:11:14 +0000 | [diff] [blame] | 806 | if (MSExtensions.getPosition()) |
| 807 | Options.Microsoft = MSExtensions; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 808 | Options.WritableStrings = WritableStrings; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 809 | if (NoLaxVectorConversions.getPosition()) |
| 810 | Options.LaxVectorConversions = 0; |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 811 | Options.Exceptions = Exceptions; |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 812 | Options.Rtti = Rtti; |
Mike Stump | a0f02aa | 2009-02-02 22:57:57 +0000 | [diff] [blame] | 813 | if (EnableBlocks.getPosition()) |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 814 | Options.Blocks = EnableBlocks; |
Eli Friedman | c4757bd | 2009-06-05 07:12:17 +0000 | [diff] [blame] | 815 | if (CharIsSigned.getPosition()) |
| 816 | Options.CharIsSigned = CharIsSigned; |
John Thompson | 40d1bb6 | 2009-11-05 22:03:02 +0000 | [diff] [blame] | 817 | if (ShortWChar.getPosition()) |
| 818 | Options.ShortWChar = ShortWChar; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 819 | |
Daniel Dunbar | 9f9768c | 2009-03-20 23:49:28 +0000 | [diff] [blame] | 820 | if (!AllowBuiltins) |
Chris Lattner | 7644f07 | 2009-03-13 22:38:49 +0000 | [diff] [blame] | 821 | Options.NoBuiltin = 1; |
Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 822 | if (Freestanding) |
Chris Lattner | 7644f07 | 2009-03-13 22:38:49 +0000 | [diff] [blame] | 823 | Options.Freestanding = Options.NoBuiltin = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
Chris Lattner | 810f6d5 | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 825 | if (EnableHeinousExtensions) |
| 826 | Options.HeinousExtensions = 1; |
Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 827 | |
Anders Carlsson | a33d9b4 | 2009-05-13 19:49:53 +0000 | [diff] [blame] | 828 | if (AccessControl) |
| 829 | Options.AccessControl = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | |
Anders Carlsson | 92f5822 | 2009-08-22 22:30:33 +0000 | [diff] [blame] | 831 | Options.ElideConstructors = !NoElideConstructors; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 833 | // OpenCL and C++ both have bool, true, false keywords. |
| 834 | Options.Bool = Options.OpenCL | Options.CPlusPlus; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 836 | Options.MathErrno = MathErrno; |
| 837 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 838 | Options.InstantiationDepth = TemplateDepth; |
| 839 | |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 840 | // Override the default runtime if the user requested it. |
| 841 | if (NeXTRuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 842 | Options.NeXTRuntime = 1; |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 843 | else if (GNURuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 844 | Options.NeXTRuntime = 0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 845 | |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 846 | if (!ObjCConstantStringClass.empty()) |
| 847 | Options.ObjCConstantStringClass = ObjCConstantStringClass.c_str(); |
| 848 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 849 | if (ObjCNonFragileABI) |
| 850 | Options.ObjCNonFragileABI = 1; |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 851 | |
Daniel Dunbar | d604c40 | 2009-02-04 21:19:06 +0000 | [diff] [blame] | 852 | if (EmitAllDecls) |
| 853 | Options.EmitAllDecls = 1; |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 854 | |
Daniel Dunbar | 3bbc753 | 2009-04-08 18:03:55 +0000 | [diff] [blame] | 855 | // The __OPTIMIZE_SIZE__ define is tied to -Oz, which we don't |
| 856 | // support. |
| 857 | Options.OptimizeSize = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 859 | // -Os implies -O2 |
Daniel Dunbar | 3bbc753 | 2009-04-08 18:03:55 +0000 | [diff] [blame] | 860 | if (OptSize || OptLevel) |
Anders Carlsson | 4ca076f | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 861 | Options.Optimize = 1; |
Daniel Dunbar | 9fd0b1f | 2009-04-08 03:03:23 +0000 | [diff] [blame] | 862 | |
| 863 | assert(PICLevel <= 2 && "Invalid value for -pic-level"); |
| 864 | Options.PICLevel = PICLevel; |
Daniel Dunbar | c9abc04 | 2009-04-08 05:11:16 +0000 | [diff] [blame] | 865 | |
Daniel Dunbar | 3bbc753 | 2009-04-08 18:03:55 +0000 | [diff] [blame] | 866 | Options.GNUInline = !Options.C99; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | // FIXME: This is affected by other options (-fno-inline). |
Daniel Dunbar | 3bbc753 | 2009-04-08 18:03:55 +0000 | [diff] [blame] | 868 | Options.NoInline = !OptSize && !OptLevel; |
| 869 | |
| 870 | Options.Static = StaticDefine; |
| 871 | |
Bill Wendling | 4ebe3e4 | 2009-06-28 23:01:01 +0000 | [diff] [blame] | 872 | switch (StackProtector) { |
| 873 | default: |
| 874 | assert(StackProtector <= 2 && "Invalid value for -stack-protector"); |
| 875 | case -1: break; |
| 876 | case 0: Options.setStackProtectorMode(LangOptions::SSPOff); break; |
| 877 | case 1: Options.setStackProtectorMode(LangOptions::SSPOn); break; |
| 878 | case 2: Options.setStackProtectorMode(LangOptions::SSPReq); break; |
| 879 | } |
Bill Wendling | 45483f7 | 2009-06-28 07:36:13 +0000 | [diff] [blame] | 880 | |
Daniel Dunbar | c9abc04 | 2009-04-08 05:11:16 +0000 | [diff] [blame] | 881 | if (MainFileName.getPosition()) |
| 882 | Options.setMainFileName(MainFileName.c_str()); |
John Thompson | a6fda12 | 2009-11-05 20:14:16 +0000 | [diff] [blame] | 883 | |
Daniel Dunbar | 638c901 | 2009-11-09 22:46:09 +0000 | [diff] [blame] | 884 | Target.setForcedLangOptions(Options); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | //===----------------------------------------------------------------------===// |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 888 | // SourceManager initialization. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 889 | //===----------------------------------------------------------------------===// |
| 890 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 891 | static bool InitializeSourceManager(Preprocessor &PP, |
| 892 | const std::string &InFile) { |
| 893 | // Figure out where to get and map in the main file. |
| 894 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 895 | FileManager &FileMgr = PP.getFileManager(); |
Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 896 | |
| 897 | if (EmptyInputOnly) { |
| 898 | const char *EmptyStr = ""; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | llvm::MemoryBuffer *SB = |
Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 900 | llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<empty input>"); |
| 901 | SourceMgr.createMainFileIDForMemBuffer(SB); |
| 902 | } else if (InFile != "-") { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 903 | const FileEntry *File = FileMgr.getFile(InFile); |
| 904 | if (File) SourceMgr.createMainFileID(File, SourceLocation()); |
| 905 | if (SourceMgr.getMainFileID().isInvalid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | PP.getDiagnostics().Report(FullSourceLoc(), diag::err_fe_error_reading) |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 907 | << InFile.c_str(); |
| 908 | return true; |
| 909 | } |
| 910 | } else { |
| 911 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
| 912 | |
| 913 | // If stdin was empty, SB is null. Cons up an empty memory |
| 914 | // buffer now. |
| 915 | if (!SB) { |
| 916 | const char *EmptyStr = ""; |
| 917 | SB = llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<stdin>"); |
| 918 | } |
| 919 | |
| 920 | SourceMgr.createMainFileIDForMemBuffer(SB); |
| 921 | if (SourceMgr.getMainFileID().isInvalid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | PP.getDiagnostics().Report(FullSourceLoc(), |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 923 | diag::err_fe_error_reading_stdin); |
| 924 | return true; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | return false; |
| 929 | } |
| 930 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 931 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 932 | //===----------------------------------------------------------------------===// |
| 933 | // Preprocessor Initialization |
| 934 | //===----------------------------------------------------------------------===// |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 935 | |
Chris Lattner | e6113de | 2009-11-03 19:50:27 +0000 | [diff] [blame] | 936 | static llvm::cl::opt<bool> |
Daniel Dunbar | 468fe24 | 2009-11-04 21:13:02 +0000 | [diff] [blame] | 937 | UndefMacros("undef", llvm::cl::value_desc("macro"), |
| 938 | llvm::cl::desc("undef all system defines")); |
Chris Lattner | e6113de | 2009-11-03 19:50:27 +0000 | [diff] [blame] | 939 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 940 | static llvm::cl::list<std::string> |
| 941 | D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 942 | llvm::cl::desc("Predefine the specified macro")); |
| 943 | static llvm::cl::list<std::string> |
| 944 | U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 945 | llvm::cl::desc("Undefine the specified macro")); |
Chris Lattner | b8e240e | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 946 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 947 | static llvm::cl::list<std::string> |
| 948 | ImplicitIncludes("include", llvm::cl::value_desc("file"), |
| 949 | llvm::cl::desc("Include file before parsing")); |
| 950 | static llvm::cl::list<std::string> |
| 951 | ImplicitMacroIncludes("imacros", llvm::cl::value_desc("file"), |
| 952 | llvm::cl::desc("Include macros from file before parsing")); |
Chris Lattner | b8e240e | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 953 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 954 | static llvm::cl::opt<std::string> |
| 955 | ImplicitIncludePCH("include-pch", llvm::cl::value_desc("file"), |
| 956 | llvm::cl::desc("Include precompiled header file")); |
| 957 | |
| 958 | static llvm::cl::opt<std::string> |
| 959 | ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"), |
| 960 | llvm::cl::desc("Include file before parsing")); |
| 961 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 962 | static llvm::cl::opt<bool> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | RelocatablePCH("relocatable-pch", |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 964 | llvm::cl::desc("Whether to build a relocatable precompiled " |
| 965 | "header")); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 966 | |
| 967 | //===----------------------------------------------------------------------===// |
| 968 | // Preprocessor include path information. |
| 969 | //===----------------------------------------------------------------------===// |
| 970 | |
| 971 | // This tool exports a large number of command line options to control how the |
| 972 | // preprocessor searches for header files. At root, however, the Preprocessor |
| 973 | // object takes a very simple interface: a list of directories to search for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | // |
Daniel Dunbar | fb4ac7b | 2009-05-06 03:48:17 +0000 | [diff] [blame] | 975 | // FIXME: -nostdinc++ |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 976 | // FIXME: -imultilib |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 977 | // |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 978 | |
| 979 | static llvm::cl::opt<bool> |
| 980 | nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories")); |
| 981 | |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 982 | static llvm::cl::opt<bool> |
Rafael Espindola | 8d737cc | 2009-10-26 13:36:57 +0000 | [diff] [blame] | 983 | nobuiltininc("nobuiltininc", |
| 984 | llvm::cl::desc("Disable builtin #include directories")); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 985 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 986 | // Various command line options. These four add directories to each chain. |
| 987 | static llvm::cl::list<std::string> |
| 988 | F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 989 | llvm::cl::desc("Add directory to framework include search path")); |
| 990 | static llvm::cl::list<std::string> |
| 991 | I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 992 | llvm::cl::desc("Add directory to include search path")); |
| 993 | static llvm::cl::list<std::string> |
| 994 | idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 995 | llvm::cl::desc("Add directory to AFTER include search path")); |
| 996 | static llvm::cl::list<std::string> |
| 997 | iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 998 | llvm::cl::desc("Add directory to QUOTE include search path")); |
| 999 | static llvm::cl::list<std::string> |
| 1000 | isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1001 | llvm::cl::desc("Add directory to SYSTEM include search path")); |
| 1002 | |
| 1003 | // These handle -iprefix/-iwithprefix/-iwithprefixbefore. |
| 1004 | static llvm::cl::list<std::string> |
| 1005 | iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix, |
| 1006 | llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix")); |
| 1007 | static llvm::cl::list<std::string> |
| 1008 | iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix, |
| 1009 | llvm::cl::desc("Set directory to SYSTEM include search path with prefix")); |
| 1010 | static llvm::cl::list<std::string> |
| 1011 | iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"), |
| 1012 | llvm::cl::Prefix, |
| 1013 | llvm::cl::desc("Set directory to include search path with prefix")); |
| 1014 | |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 1015 | static llvm::cl::opt<std::string> |
| 1016 | isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"), |
| 1017 | llvm::cl::desc("Set the system root directory (usually /)")); |
| 1018 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1019 | // Finally, implement the code that groks the options above. |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1020 | |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 1021 | // Add the clang headers, which are relative to the clang binary. |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 1022 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 1023 | llvm::sys::Path P = |
| 1024 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 1025 | (void*)(intptr_t) GetBuiltinIncludePath); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 1026 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 1027 | if (!P.isEmpty()) { |
| 1028 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 1029 | P.eraseComponent(); // Remove /bin from foo/bin |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 1030 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 1031 | // Get foo/lib/clang/<version>/include |
| 1032 | P.appendComponent("lib"); |
| 1033 | P.appendComponent("clang"); |
| 1034 | P.appendComponent(CLANG_VERSION_STRING); |
| 1035 | P.appendComponent("include"); |
| 1036 | } |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 1037 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 1038 | return P.str(); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1041 | /// InitializeIncludePaths - Process the -I options and set them in the |
| 1042 | /// HeaderSearch object. |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1043 | static void InitializeIncludePaths(HeaderSearchOptions &Opts, |
| 1044 | const char *Argv0, |
| 1045 | const LangOptions &Lang) { |
| 1046 | Opts.Sysroot = isysroot; |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1047 | Opts.Verbose = Verbose; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1048 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1049 | // Handle -I... and -F... options, walking the lists in parallel. |
| 1050 | unsigned Iidx = 0, Fidx = 0; |
| 1051 | while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) { |
| 1052 | if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) { |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame^] | 1053 | Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1054 | ++Iidx; |
| 1055 | } else { |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame^] | 1056 | Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1057 | ++Fidx; |
| 1058 | } |
| 1059 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1061 | // Consume what's left from whatever list was longer. |
| 1062 | for (; Iidx != I_dirs.size(); ++Iidx) |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame^] | 1063 | Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1064 | for (; Fidx != F_dirs.size(); ++Fidx) |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame^] | 1065 | Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1067 | // Handle -idirafter... options. |
| 1068 | for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i) |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame^] | 1069 | Opts.AddPath(idirafter_dirs[i], frontend::After, |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1070 | false, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1072 | // Handle -iquote... options. |
| 1073 | for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i) |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame^] | 1074 | Opts.AddPath(iquote_dirs[i], frontend::Quoted, false, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1076 | // Handle -isystem... options. |
| 1077 | for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i) |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame^] | 1078 | Opts.AddPath(isystem_dirs[i], frontend::System, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1079 | |
| 1080 | // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in |
| 1081 | // parallel, processing the values in order of occurance to get the right |
| 1082 | // prefixes. |
| 1083 | { |
| 1084 | std::string Prefix = ""; // FIXME: this isn't the correct default prefix. |
| 1085 | unsigned iprefix_idx = 0; |
| 1086 | unsigned iwithprefix_idx = 0; |
| 1087 | unsigned iwithprefixbefore_idx = 0; |
| 1088 | bool iprefix_done = iprefix_vals.empty(); |
| 1089 | bool iwithprefix_done = iwithprefix_vals.empty(); |
| 1090 | bool iwithprefixbefore_done = iwithprefixbefore_vals.empty(); |
| 1091 | while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) { |
| 1092 | if (!iprefix_done && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | (iwithprefix_done || |
| 1094 | iprefix_vals.getPosition(iprefix_idx) < |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1095 | iwithprefix_vals.getPosition(iwithprefix_idx)) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | (iwithprefixbefore_done || |
| 1097 | iprefix_vals.getPosition(iprefix_idx) < |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1098 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
| 1099 | Prefix = iprefix_vals[iprefix_idx]; |
| 1100 | ++iprefix_idx; |
| 1101 | iprefix_done = iprefix_idx == iprefix_vals.size(); |
| 1102 | } else if (!iwithprefix_done && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | (iwithprefixbefore_done || |
| 1104 | iwithprefix_vals.getPosition(iwithprefix_idx) < |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1105 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1106 | Opts.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx], |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame^] | 1107 | frontend::System, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1108 | ++iwithprefix_idx; |
| 1109 | iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size(); |
| 1110 | } else { |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1111 | Opts.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx], |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame^] | 1112 | frontend::Angled, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1113 | ++iwithprefixbefore_idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | iwithprefixbefore_done = |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1115 | iwithprefixbefore_idx == iwithprefixbefore_vals.size(); |
| 1116 | } |
| 1117 | } |
| 1118 | } |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1119 | |
Daniel Dunbar | e166582 | 2009-11-07 04:20:39 +0000 | [diff] [blame] | 1120 | // Add CPATH environment paths. |
| 1121 | if (const char *Env = getenv("CPATH")) |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1122 | Opts.EnvIncPath = Env; |
Daniel Dunbar | e166582 | 2009-11-07 04:20:39 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Add language specific environment paths. |
| 1125 | if (Lang.CPlusPlus && Lang.ObjC1) { |
| 1126 | if (const char *Env = getenv("OBJCPLUS_INCLUDE_PATH")) |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1127 | Opts.LangEnvIncPath = Env; |
Daniel Dunbar | e166582 | 2009-11-07 04:20:39 +0000 | [diff] [blame] | 1128 | } else if (Lang.CPlusPlus) { |
| 1129 | if (const char *Env = getenv("CPLUS_INCLUDE_PATH")) |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1130 | Opts.LangEnvIncPath = Env; |
Daniel Dunbar | e166582 | 2009-11-07 04:20:39 +0000 | [diff] [blame] | 1131 | } else if (Lang.ObjC1) { |
| 1132 | if (const char *Env = getenv("OBJC_INCLUDE_PATH")) |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1133 | Opts.LangEnvIncPath = Env; |
Daniel Dunbar | e166582 | 2009-11-07 04:20:39 +0000 | [diff] [blame] | 1134 | } else { |
| 1135 | if (const char *Env = getenv("C_INCLUDE_PATH")) |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1136 | Opts.LangEnvIncPath = Env; |
Daniel Dunbar | e166582 | 2009-11-07 04:20:39 +0000 | [diff] [blame] | 1137 | } |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1138 | |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1139 | if (!nobuiltininc) |
| 1140 | Opts.BuiltinIncludePath = GetBuiltinIncludePath(Argv0); |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 1141 | |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 1142 | Opts.UseStandardIncludes = !nostdinc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Daniel Dunbar | 8863b98 | 2009-11-07 04:20:15 +0000 | [diff] [blame] | 1145 | void InitializePreprocessorOptions(PreprocessorOptions &InitOpts) { |
Daniel Dunbar | 468fe24 | 2009-11-04 21:13:02 +0000 | [diff] [blame] | 1146 | // Use predefines? |
| 1147 | InitOpts.setUsePredefines(!UndefMacros); |
| 1148 | |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 1149 | // Add macros from the command line. |
| 1150 | unsigned d = 0, D = D_macros.size(); |
| 1151 | unsigned u = 0, U = U_macros.size(); |
| 1152 | while (d < D || u < U) { |
| 1153 | if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u))) |
| 1154 | InitOpts.addMacroDef(D_macros[d++]); |
| 1155 | else |
| 1156 | InitOpts.addMacroUndef(U_macros[u++]); |
| 1157 | } |
| 1158 | |
| 1159 | // If -imacros are specified, include them now. These are processed before |
| 1160 | // any -include directives. |
| 1161 | for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i) |
| 1162 | InitOpts.addMacroInclude(ImplicitMacroIncludes[i]); |
| 1163 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1164 | if (!ImplicitIncludePTH.empty() || !ImplicitIncludes.empty() || |
| 1165 | (!ImplicitIncludePCH.empty() && ProgAction == PrintPreprocessedInput)) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 1166 | // We want to add these paths to the predefines buffer in order, make a |
| 1167 | // temporary vector to sort by their occurrence. |
| 1168 | llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths; |
| 1169 | |
| 1170 | if (!ImplicitIncludePTH.empty()) |
| 1171 | OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(), |
| 1172 | &ImplicitIncludePTH)); |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1173 | if (!ImplicitIncludePCH.empty() && ProgAction == PrintPreprocessedInput) |
| 1174 | OrderedPaths.push_back(std::make_pair(ImplicitIncludePCH.getPosition(), |
| 1175 | &ImplicitIncludePCH)); |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 1176 | for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i) |
| 1177 | OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i), |
| 1178 | &ImplicitIncludes[i])); |
| 1179 | llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end()); |
| 1180 | |
| 1181 | |
| 1182 | // Now that they are ordered by position, add to the predefines buffer. |
| 1183 | for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i) { |
| 1184 | std::string *Ptr = OrderedPaths[i].second; |
| 1185 | if (!ImplicitIncludes.empty() && |
| 1186 | Ptr >= &ImplicitIncludes[0] && |
| 1187 | Ptr <= &ImplicitIncludes[ImplicitIncludes.size()-1]) { |
| 1188 | InitOpts.addInclude(*Ptr, false); |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1189 | } else if (Ptr == &ImplicitIncludePTH) { |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 1190 | InitOpts.addInclude(*Ptr, true); |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1191 | } else { |
| 1192 | // We end up here when we're producing preprocessed output and |
| 1193 | // we loaded a PCH file. In this case, just include the header |
| 1194 | // file that was used to build the precompiled header. |
| 1195 | assert(Ptr == &ImplicitIncludePCH); |
| 1196 | std::string OriginalFile = PCHReader::getOriginalSourceFile(*Ptr); |
| 1197 | if (!OriginalFile.empty()) { |
| 1198 | InitOpts.addInclude(OriginalFile, false); |
| 1199 | ImplicitIncludePCH.clear(); |
| 1200 | } |
Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1206 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 1207 | // Preprocessor construction |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1208 | //===----------------------------------------------------------------------===// |
| 1209 | |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 1210 | static Preprocessor * |
| 1211 | CreatePreprocessor(Diagnostic &Diags,const LangOptions &LangInfo, |
| 1212 | TargetInfo &Target, SourceManager &SourceMgr, |
| 1213 | HeaderSearch &HeaderInfo) { |
| 1214 | PTHManager *PTHMgr = 0; |
| 1215 | if (!TokenCache.empty() && !ImplicitIncludePTH.empty()) { |
| 1216 | fprintf(stderr, "error: cannot use both -token-cache and -include-pth " |
| 1217 | "options\n"); |
| 1218 | exit(1); |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1219 | } |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Use PTH? |
| 1222 | if (!TokenCache.empty() || !ImplicitIncludePTH.empty()) { |
| 1223 | const std::string& x = TokenCache.empty() ? ImplicitIncludePTH:TokenCache; |
| 1224 | PTHMgr = PTHManager::Create(x, &Diags, |
| 1225 | TokenCache.empty() ? Diagnostic::Error |
| 1226 | : Diagnostic::Warning); |
| 1227 | } |
| 1228 | |
| 1229 | if (Diags.hasErrorOccurred()) |
| 1230 | exit(1); |
| 1231 | |
| 1232 | // Create the Preprocessor. |
| 1233 | Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target, |
| 1234 | SourceMgr, HeaderInfo, PTHMgr); |
| 1235 | |
| 1236 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 1237 | // That argument is used as the IdentifierInfoLookup argument to |
| 1238 | // IdentifierTable's ctor. |
| 1239 | if (PTHMgr) { |
| 1240 | PTHMgr->setPreprocessor(PP); |
| 1241 | PP->setPTHManager(PTHMgr); |
| 1242 | } |
| 1243 | |
Daniel Dunbar | 8863b98 | 2009-11-07 04:20:15 +0000 | [diff] [blame] | 1244 | PreprocessorOptions InitOpts; |
| 1245 | InitializePreprocessorOptions(InitOpts); |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 1246 | InitializePreprocessor(*PP, InitOpts); |
| 1247 | |
| 1248 | return PP; |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1249 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1251 | //===----------------------------------------------------------------------===// |
| 1252 | // Basic Parser driver |
| 1253 | //===----------------------------------------------------------------------===// |
| 1254 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1255 | static void ParseFile(Preprocessor &PP, MinimalAction *PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1256 | Parser P(PP, *PA); |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1257 | PP.EnterMainSourceFile(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1259 | // Parsing the specified input file. |
| 1260 | P.ParseTranslationUnit(); |
| 1261 | delete PA; |
| 1262 | } |
| 1263 | |
| 1264 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1265 | // Code generation options |
| 1266 | //===----------------------------------------------------------------------===// |
| 1267 | |
| 1268 | static llvm::cl::opt<bool> |
Chris Lattner | 1510488 | 2009-03-09 22:05:03 +0000 | [diff] [blame] | 1269 | GenerateDebugInfo("g", |
| 1270 | llvm::cl::desc("Generate source level debug information")); |
| 1271 | |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 1272 | static llvm::cl::opt<std::string> |
| 1273 | TargetCPU("mcpu", |
| 1274 | llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)")); |
| 1275 | |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1276 | static llvm::cl::list<std::string> |
| 1277 | TargetFeatures("target-feature", llvm::cl::desc("Target specific attributes")); |
| 1278 | |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 1279 | |
| 1280 | static llvm::cl::opt<bool> |
| 1281 | DisableRedZone("disable-red-zone", |
| 1282 | llvm::cl::desc("Do not emit code that uses the red zone."), |
| 1283 | llvm::cl::init(false)); |
| 1284 | |
Devang Patel | acebb39 | 2009-06-05 22:05:48 +0000 | [diff] [blame] | 1285 | static llvm::cl::opt<bool> |
| 1286 | NoImplicitFloat("no-implicit-float", |
| 1287 | llvm::cl::desc("Don't generate implicit floating point instructions (x86-only)"), |
| 1288 | llvm::cl::init(false)); |
| 1289 | |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1290 | /// ComputeTargetFeatures - Recompute the target feature list to only |
| 1291 | /// be the list of things that are enabled, based on the target cpu |
| 1292 | /// and feature list. |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1293 | static void ComputeFeatureMap(TargetInfo &Target, |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1294 | llvm::StringMap<bool> &Features) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | assert(Features.empty() && "invalid map"); |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1296 | |
| 1297 | // Initialize the feature map based on the target. |
Daniel Dunbar | 638c901 | 2009-11-09 22:46:09 +0000 | [diff] [blame] | 1298 | Target.getDefaultFeatures(TargetCPU, Features); |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1299 | |
| 1300 | // Apply the user specified deltas. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | for (llvm::cl::list<std::string>::iterator it = TargetFeatures.begin(), |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1302 | ie = TargetFeatures.end(); it != ie; ++it) { |
| 1303 | const char *Name = it->c_str(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1305 | // FIXME: Don't handle errors like this. |
| 1306 | if (Name[0] != '-' && Name[0] != '+') { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | fprintf(stderr, "error: clang-cc: invalid target feature string: %s\n", |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1308 | Name); |
| 1309 | exit(1); |
| 1310 | } |
Daniel Dunbar | 638c901 | 2009-11-09 22:46:09 +0000 | [diff] [blame] | 1311 | if (!Target.setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | fprintf(stderr, "error: clang-cc: invalid target feature name: %s\n", |
Daniel Dunbar | 17ca363 | 2009-05-06 21:07:50 +0000 | [diff] [blame] | 1313 | Name + 1); |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1314 | exit(1); |
| 1315 | } |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | |
Chris Lattner | 7afae71 | 2009-03-16 18:41:18 +0000 | [diff] [blame] | 1319 | static void InitializeCompileOptions(CompileOptions &Opts, |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1320 | const LangOptions &LangOpts, |
| 1321 | const llvm::StringMap<bool> &Features) { |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1322 | Opts.OptimizeSize = OptSize; |
Chris Lattner | 2012604 | 2009-03-09 22:00:34 +0000 | [diff] [blame] | 1323 | Opts.DebugInfo = GenerateDebugInfo; |
Daniel Dunbar | 877db38 | 2009-06-02 22:07:45 +0000 | [diff] [blame] | 1324 | |
| 1325 | if (DisableLLVMOptimizations) { |
| 1326 | Opts.OptimizationLevel = 0; |
| 1327 | Opts.Inlining = CompileOptions::NoInlining; |
Daniel Dunbar | ac7ffe0 | 2008-10-29 07:56:11 +0000 | [diff] [blame] | 1328 | } else { |
Daniel Dunbar | 877db38 | 2009-06-02 22:07:45 +0000 | [diff] [blame] | 1329 | if (OptSize) { |
| 1330 | // -Os implies -O2 |
| 1331 | Opts.OptimizationLevel = 2; |
| 1332 | } else { |
| 1333 | Opts.OptimizationLevel = OptLevel; |
| 1334 | } |
| 1335 | |
| 1336 | // We must always run at least the always inlining pass. |
| 1337 | if (Opts.OptimizationLevel > 1) |
| 1338 | Opts.Inlining = CompileOptions::NormalInlining; |
| 1339 | else |
| 1340 | Opts.Inlining = CompileOptions::OnlyAlwaysInlining; |
Daniel Dunbar | ac7ffe0 | 2008-10-29 07:56:11 +0000 | [diff] [blame] | 1341 | } |
Daniel Dunbar | 8e8f3b7 | 2008-10-29 03:42:18 +0000 | [diff] [blame] | 1342 | |
| 1343 | // FIXME: There are llvm-gcc options to control these selectively. |
Daniel Dunbar | 8e8f3b7 | 2008-10-29 03:42:18 +0000 | [diff] [blame] | 1344 | Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize); |
Chris Lattner | 7afae71 | 2009-03-16 18:41:18 +0000 | [diff] [blame] | 1345 | Opts.SimplifyLibCalls = !LangOpts.NoBuiltin; |
Daniel Dunbar | dd913e5 | 2008-10-31 09:34:21 +0000 | [diff] [blame] | 1346 | |
| 1347 | #ifdef NDEBUG |
| 1348 | Opts.VerifyModule = 0; |
| 1349 | #endif |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 1350 | |
| 1351 | Opts.CPU = TargetCPU; |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1352 | Opts.Features.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | for (llvm::StringMap<bool>::const_iterator it = Features.begin(), |
Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 1354 | ie = Features.end(); it != ie; ++it) { |
| 1355 | // FIXME: If we are completely confident that we have the right |
| 1356 | // set, we only need to pass the minuses. |
| 1357 | std::string Name(it->second ? "+" : "-"); |
| 1358 | Name += it->first(); |
| 1359 | Opts.Features.push_back(Name); |
| 1360 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 1362 | Opts.NoCommon = NoCommon | LangOpts.CPlusPlus; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 1364 | // Handle -ftime-report. |
| 1365 | Opts.TimePasses = TimeReport; |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 1366 | |
| 1367 | Opts.DisableRedZone = DisableRedZone; |
Devang Patel | acebb39 | 2009-06-05 22:05:48 +0000 | [diff] [blame] | 1368 | Opts.NoImplicitFloat = NoImplicitFloat; |
Tanya Lattner | 59876c2 | 2009-11-04 01:18:09 +0000 | [diff] [blame] | 1369 | |
| 1370 | Opts.MergeAllConstants = !NoMergeConstants; |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 1374 | // Fix-It Options |
| 1375 | //===----------------------------------------------------------------------===// |
| 1376 | static llvm::cl::list<ParsedSourceLocation> |
| 1377 | FixItAtLocations("fixit-at", llvm::cl::value_desc("source-location"), |
| 1378 | llvm::cl::desc("Perform Fix-It modifications at the given source location")); |
| 1379 | |
| 1380 | //===----------------------------------------------------------------------===// |
Eli Friedman | c6d656e | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 1381 | // ObjC Rewriter Options |
| 1382 | //===----------------------------------------------------------------------===// |
| 1383 | static llvm::cl::opt<bool> |
| 1384 | SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false), |
| 1385 | llvm::cl::desc("Silence ObjC rewriting warnings")); |
| 1386 | |
| 1387 | //===----------------------------------------------------------------------===// |
Eli Friedman | 0eeb86e | 2009-05-19 01:17:04 +0000 | [diff] [blame] | 1388 | // Warning Options |
| 1389 | //===----------------------------------------------------------------------===// |
| 1390 | |
| 1391 | // This gets all -W options, including -Werror, -W[no-]system-headers, etc. The |
| 1392 | // driver has stripped off -Wa,foo etc. The driver has also translated -W to |
| 1393 | // -Wextra, so we don't need to worry about it. |
| 1394 | static llvm::cl::list<std::string> |
| 1395 | OptWarnings("W", llvm::cl::Prefix, llvm::cl::ValueOptional); |
| 1396 | |
| 1397 | static llvm::cl::opt<bool> OptPedantic("pedantic"); |
| 1398 | static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors"); |
| 1399 | static llvm::cl::opt<bool> OptNoWarnings("w"); |
| 1400 | |
| 1401 | //===----------------------------------------------------------------------===// |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 1402 | // Preprocessing (-E mode) Options |
| 1403 | //===----------------------------------------------------------------------===// |
| 1404 | static llvm::cl::opt<bool> |
| 1405 | DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode")); |
| 1406 | static llvm::cl::opt<bool> |
| 1407 | EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode")); |
| 1408 | static llvm::cl::opt<bool> |
| 1409 | EnableMacroCommentOutput("CC", |
| 1410 | llvm::cl::desc("Enable comment output in -E mode, " |
| 1411 | "even from macro expansions")); |
| 1412 | static llvm::cl::opt<bool> |
| 1413 | DumpMacros("dM", llvm::cl::desc("Print macro definitions in -E mode instead of" |
| 1414 | " normal output")); |
| 1415 | static llvm::cl::opt<bool> |
| 1416 | DumpDefines("dD", llvm::cl::desc("Print macro definitions in -E mode in " |
| 1417 | "addition to normal output")); |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 1418 | |
| 1419 | //===----------------------------------------------------------------------===// |
| 1420 | // Dependency file options |
| 1421 | //===----------------------------------------------------------------------===// |
| 1422 | static llvm::cl::opt<std::string> |
| 1423 | DependencyFile("dependency-file", |
| 1424 | llvm::cl::desc("Filename (or -) to write dependency output to")); |
| 1425 | |
| 1426 | static llvm::cl::opt<bool> |
| 1427 | DependenciesIncludeSystemHeaders("sys-header-deps", |
| 1428 | llvm::cl::desc("Include system headers in dependency output")); |
| 1429 | |
| 1430 | static llvm::cl::list<std::string> |
| 1431 | DependencyTargets("MT", |
| 1432 | llvm::cl::desc("Specify target for dependency")); |
| 1433 | |
| 1434 | // FIXME: Implement feature |
| 1435 | static llvm::cl::opt<bool> |
| 1436 | PhonyDependencyTarget("MP", |
| 1437 | llvm::cl::desc("Create phony target for each dependency " |
| 1438 | "(other than main file)")); |
| 1439 | |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 1440 | //===----------------------------------------------------------------------===// |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 1441 | // Analysis options |
| 1442 | //===----------------------------------------------------------------------===// |
| 1443 | |
| 1444 | static llvm::cl::list<Analyses> |
| 1445 | AnalysisList(llvm::cl::desc("Source Code Analysis - Checks and Analyses"), |
| 1446 | llvm::cl::values( |
| 1447 | #define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\ |
| 1448 | clEnumValN(NAME, CMDFLAG, DESC), |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 1449 | #include "clang/Frontend/Analyses.def" |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 1450 | clEnumValEnd)); |
| 1451 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | static llvm::cl::opt<AnalysisStores> |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 1453 | AnalysisStoreOpt("analyzer-store", |
| 1454 | llvm::cl::desc("Source Code Analysis - Abstract Memory Store Models"), |
| 1455 | llvm::cl::init(BasicStoreModel), |
| 1456 | llvm::cl::values( |
| 1457 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN)\ |
| 1458 | clEnumValN(NAME##Model, CMDFLAG, DESC), |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 1459 | #include "clang/Frontend/Analyses.def" |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 1460 | clEnumValEnd)); |
| 1461 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | static llvm::cl::opt<AnalysisConstraints> |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 1463 | AnalysisConstraintsOpt("analyzer-constraints", |
| 1464 | llvm::cl::desc("Source Code Analysis - Symbolic Constraint Engines"), |
| 1465 | llvm::cl::init(RangeConstraintsModel), |
| 1466 | llvm::cl::values( |
| 1467 | #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN)\ |
| 1468 | clEnumValN(NAME##Model, CMDFLAG, DESC), |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 1469 | #include "clang/Frontend/Analyses.def" |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 1470 | clEnumValEnd)); |
| 1471 | |
| 1472 | static llvm::cl::opt<AnalysisDiagClients> |
| 1473 | AnalysisDiagOpt("analyzer-output", |
| 1474 | llvm::cl::desc("Source Code Analysis - Output Options"), |
| 1475 | llvm::cl::init(PD_HTML), |
| 1476 | llvm::cl::values( |
| 1477 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE)\ |
| 1478 | clEnumValN(PD_##NAME, CMDFLAG, DESC), |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 1479 | #include "clang/Frontend/Analyses.def" |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 1480 | clEnumValEnd)); |
| 1481 | |
| 1482 | static llvm::cl::opt<bool> |
| 1483 | VisualizeEGDot("analyzer-viz-egraph-graphviz", |
| 1484 | llvm::cl::desc("Display exploded graph using GraphViz")); |
| 1485 | |
| 1486 | static llvm::cl::opt<bool> |
| 1487 | VisualizeEGUbi("analyzer-viz-egraph-ubigraph", |
| 1488 | llvm::cl::desc("Display exploded graph using Ubigraph")); |
| 1489 | |
| 1490 | static llvm::cl::opt<bool> |
| 1491 | AnalyzeAll("analyzer-opt-analyze-headers", |
| 1492 | llvm::cl::desc("Force the static analyzer to analyze " |
| 1493 | "functions defined in header files")); |
| 1494 | |
| 1495 | static llvm::cl::opt<bool> |
| 1496 | AnalyzerDisplayProgress("analyzer-display-progress", |
| 1497 | llvm::cl::desc("Emit verbose output about the analyzer's progress.")); |
| 1498 | |
| 1499 | static llvm::cl::opt<bool> |
| 1500 | PurgeDead("analyzer-purge-dead", |
| 1501 | llvm::cl::init(true), |
| 1502 | llvm::cl::desc("Remove dead symbols, bindings, and constraints before" |
| 1503 | " processing a statement.")); |
| 1504 | |
| 1505 | static llvm::cl::opt<bool> |
| 1506 | EagerlyAssume("analyzer-eagerly-assume", |
| 1507 | llvm::cl::init(false), |
| 1508 | llvm::cl::desc("Eagerly assume the truth/falseness of some " |
| 1509 | "symbolic constraints.")); |
| 1510 | |
| 1511 | static llvm::cl::opt<std::string> |
| 1512 | AnalyzeSpecificFunction("analyze-function", |
| 1513 | llvm::cl::desc("Run analysis on specific function")); |
| 1514 | |
| 1515 | static llvm::cl::opt<bool> |
| 1516 | TrimGraph("trim-egraph", |
| 1517 | llvm::cl::desc("Only show error-related paths in the analysis graph")); |
| 1518 | |
| 1519 | static AnalyzerOptions ReadAnalyzerOptions() { |
| 1520 | AnalyzerOptions Opts; |
| 1521 | Opts.AnalysisList = AnalysisList; |
| 1522 | Opts.AnalysisStoreOpt = AnalysisStoreOpt; |
| 1523 | Opts.AnalysisConstraintsOpt = AnalysisConstraintsOpt; |
| 1524 | Opts.AnalysisDiagOpt = AnalysisDiagOpt; |
| 1525 | Opts.VisualizeEGDot = VisualizeEGDot; |
| 1526 | Opts.VisualizeEGUbi = VisualizeEGUbi; |
| 1527 | Opts.AnalyzeAll = AnalyzeAll; |
| 1528 | Opts.AnalyzerDisplayProgress = AnalyzerDisplayProgress; |
| 1529 | Opts.PurgeDead = PurgeDead; |
| 1530 | Opts.EagerlyAssume = EagerlyAssume; |
| 1531 | Opts.AnalyzeSpecificFunction = AnalyzeSpecificFunction; |
| 1532 | Opts.TrimGraph = TrimGraph; |
| 1533 | return Opts; |
| 1534 | } |
| 1535 | |
| 1536 | //===----------------------------------------------------------------------===// |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1537 | // -dump-build-information Stuff |
| 1538 | //===----------------------------------------------------------------------===// |
| 1539 | |
| 1540 | static llvm::cl::opt<std::string> |
| 1541 | DumpBuildInformation("dump-build-information", |
| 1542 | llvm::cl::value_desc("filename"), |
| 1543 | llvm::cl::desc("output a dump of some build information to a file")); |
| 1544 | |
| 1545 | static llvm::raw_ostream *BuildLogFile = 0; |
| 1546 | |
| 1547 | /// LoggingDiagnosticClient - This is a simple diagnostic client that forwards |
| 1548 | /// all diagnostics to both BuildLogFile and a chained DiagnosticClient. |
| 1549 | namespace { |
| 1550 | class LoggingDiagnosticClient : public DiagnosticClient { |
| 1551 | llvm::OwningPtr<DiagnosticClient> Chain1; |
| 1552 | llvm::OwningPtr<DiagnosticClient> Chain2; |
| 1553 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1555 | LoggingDiagnosticClient(const DiagnosticOptions &DiagOpts, |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1556 | DiagnosticClient *Normal) { |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1557 | // Output diags both where requested... |
| 1558 | Chain1.reset(Normal); |
| 1559 | // .. and to our log file. |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1560 | Chain2.reset(new TextDiagnosticPrinter(*BuildLogFile, DiagOpts)); |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | |
Daniel Dunbar | efcbe94 | 2009-11-05 02:42:12 +0000 | [diff] [blame] | 1563 | virtual void BeginSourceFile(const LangOptions &LO) { |
| 1564 | Chain1->BeginSourceFile(LO); |
| 1565 | Chain2->BeginSourceFile(LO); |
| 1566 | } |
| 1567 | |
| 1568 | virtual void EndSourceFile() { |
| 1569 | Chain1->EndSourceFile(); |
| 1570 | Chain2->EndSourceFile(); |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1573 | virtual bool IncludeInDiagnosticCounts() const { |
| 1574 | return Chain1->IncludeInDiagnosticCounts(); |
| 1575 | } |
| 1576 | |
| 1577 | virtual void HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 1578 | const DiagnosticInfo &Info) { |
| 1579 | Chain1->HandleDiagnostic(DiagLevel, Info); |
| 1580 | Chain2->HandleDiagnostic(DiagLevel, Info); |
| 1581 | } |
| 1582 | }; |
| 1583 | } // end anonymous namespace. |
| 1584 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1585 | static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1586 | unsigned argc, char **argv, |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1587 | llvm::OwningPtr<DiagnosticClient> &DiagClient) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1589 | std::string ErrorInfo; |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 1590 | BuildLogFile = new llvm::raw_fd_ostream(DumpBuildInformation.c_str(), |
Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 1591 | ErrorInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1593 | if (!ErrorInfo.empty()) { |
| 1594 | llvm::errs() << "error opening -dump-build-information file '" |
| 1595 | << DumpBuildInformation << "', option ignored!\n"; |
| 1596 | delete BuildLogFile; |
| 1597 | BuildLogFile = 0; |
| 1598 | DumpBuildInformation = ""; |
| 1599 | return; |
| 1600 | } |
| 1601 | |
| 1602 | (*BuildLogFile) << "clang-cc command line arguments: "; |
| 1603 | for (unsigned i = 0; i != argc; ++i) |
| 1604 | (*BuildLogFile) << argv[i] << ' '; |
| 1605 | (*BuildLogFile) << '\n'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1607 | // LoggingDiagnosticClient - Insert a new logging diagnostic client in between |
| 1608 | // the diagnostic producers and the normal receiver. |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1609 | DiagClient.reset(new LoggingDiagnosticClient(DiagOpts, DiagClient.take())); |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | |
| 1613 | |
| 1614 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1615 | // Main driver |
| 1616 | //===----------------------------------------------------------------------===// |
| 1617 | |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1618 | static llvm::raw_ostream *ComputeOutFile(const CompilerInvocation &CompOpts, |
| 1619 | const std::string &InFile, |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 1620 | const char *Extension, |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1621 | bool Binary, |
| 1622 | llvm::sys::Path& OutPath) { |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 1623 | llvm::raw_ostream *Ret; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1624 | std::string OutFile; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1625 | if (!CompOpts.getOutputFile().empty()) |
| 1626 | OutFile = CompOpts.getOutputFile(); |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 1627 | else if (InFile == "-") { |
| 1628 | OutFile = "-"; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1629 | } else if (Extension) { |
| 1630 | llvm::sys::Path Path(InFile); |
| 1631 | Path.eraseSuffix(); |
| 1632 | Path.appendSuffix(Extension); |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 1633 | OutFile = Path.str(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1634 | } else { |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 1635 | OutFile = "-"; |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1636 | } |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 1637 | |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 1638 | std::string Error; |
| 1639 | Ret = new llvm::raw_fd_ostream(OutFile.c_str(), Error, |
Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 1640 | (Binary ? llvm::raw_fd_ostream::F_Binary : 0)); |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 1641 | if (!Error.empty()) { |
| 1642 | // FIXME: Don't fail this way. |
| 1643 | llvm::errs() << "ERROR: " << Error << "\n"; |
| 1644 | ::exit(1); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1645 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 1647 | if (OutFile != "-") |
| 1648 | OutPath = OutFile; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1649 | |
| 1650 | return Ret; |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1653 | static ASTConsumer *CreateConsumerAction(const CompilerInvocation &CompOpts, |
| 1654 | Preprocessor &PP, |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1655 | const std::string &InFile, |
| 1656 | ProgActions PA, |
| 1657 | llvm::OwningPtr<llvm::raw_ostream> &OS, |
| 1658 | llvm::sys::Path &OutPath, |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1659 | llvm::LLVMContext& Context) { |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1660 | switch (PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1661 | default: |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1662 | return 0; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1663 | |
| 1664 | case ASTPrint: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1665 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, false, OutPath)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1666 | return CreateASTPrinter(OS.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 1668 | case ASTPrintXML: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1669 | OS.reset(ComputeOutFile(CompOpts, InFile, "xml", false, OutPath)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1670 | return CreateASTPrinterXML(OS.get()); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 1671 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1672 | case ASTDump: |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1673 | return CreateASTDumper(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1674 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1675 | case ASTView: |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1676 | return CreateASTViewer(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1677 | |
| 1678 | case PrintDeclContext: |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1679 | return CreateDeclContextPrinter(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1680 | |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 1681 | case DumpRecordLayouts: |
| 1682 | return CreateRecordLayoutDumper(); |
| 1683 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1684 | case InheritanceView: |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1685 | return CreateInheritanceViewer(InheritanceViewCls); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1686 | |
| 1687 | case EmitAssembly: |
| 1688 | case EmitLLVM: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | case EmitBC: |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1690 | case EmitLLVMOnly: { |
| 1691 | BackendAction Act; |
| 1692 | if (ProgAction == EmitAssembly) { |
| 1693 | Act = Backend_EmitAssembly; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1694 | OS.reset(ComputeOutFile(CompOpts, InFile, "s", true, OutPath)); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1695 | } else if (ProgAction == EmitLLVM) { |
| 1696 | Act = Backend_EmitLL; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1697 | OS.reset(ComputeOutFile(CompOpts, InFile, "ll", true, OutPath)); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1698 | } else if (ProgAction == EmitLLVMOnly) { |
| 1699 | Act = Backend_EmitNothing; |
| 1700 | } else { |
| 1701 | Act = Backend_EmitBC; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1702 | OS.reset(ComputeOutFile(CompOpts, InFile, "bc", true, OutPath)); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1703 | } |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1704 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1705 | CompileOptions Opts; |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1706 | InitializeCompileOptions(Opts, PP.getLangOptions(), |
| 1707 | CompOpts.getTargetFeatures()); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1708 | return CreateBackendConsumer(Act, PP.getDiagnostics(), PP.getLangOptions(), |
| 1709 | Opts, InFile, OS.get(), Context); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1712 | case RewriteObjC: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1713 | OS.reset(ComputeOutFile(CompOpts, InFile, "cpp", true, OutPath)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1714 | return CreateObjCRewriter(InFile, OS.get(), PP.getDiagnostics(), |
| 1715 | PP.getLangOptions(), SilenceRewriteMacroWarning); |
| 1716 | |
| 1717 | case RewriteBlocks: |
| 1718 | return CreateBlockRewriter(InFile, PP.getDiagnostics(), |
| 1719 | PP.getLangOptions()); |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | /// ProcessInputFile - Process a single input file with the specified state. |
| 1724 | /// |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1725 | static void ProcessInputFile(const CompilerInvocation &CompOpts, |
| 1726 | Preprocessor &PP, const std::string &InFile, |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 1727 | ProgActions PA, |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1728 | llvm::LLVMContext& Context) { |
| 1729 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 1730 | llvm::OwningPtr<ASTConsumer> Consumer; |
| 1731 | bool ClearSourceMgr = false; |
| 1732 | FixItRewriter *FixItRewrite = 0; |
| 1733 | bool CompleteTranslationUnit = true; |
| 1734 | llvm::sys::Path OutPath; |
| 1735 | |
| 1736 | switch (PA) { |
| 1737 | default: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1738 | Consumer.reset(CreateConsumerAction(CompOpts, PP, InFile, PA, OS, OutPath, |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1739 | Context)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1740 | |
| 1741 | if (!Consumer.get()) { |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 1742 | PP.getDiagnostics().Report(FullSourceLoc(), |
| 1743 | diag::err_fe_invalid_ast_action); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1744 | return; |
| 1745 | } |
| 1746 | |
| 1747 | break;; |
| 1748 | |
| 1749 | case EmitHTML: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1750 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 1751 | Consumer.reset(CreateHTMLPrinter(OS.get(), PP)); |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1752 | break; |
| 1753 | |
| 1754 | case RunAnalysis: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1755 | Consumer.reset(CreateAnalysisConsumer(PP, CompOpts.getOutputFile(), |
Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 1756 | ReadAnalyzerOptions())); |
| 1757 | break; |
| 1758 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1759 | case GeneratePCH: |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1760 | if (RelocatablePCH.getValue() && !isysroot.getNumOccurrences()) { |
| 1761 | PP.Diag(SourceLocation(), diag::err_relocatable_without_without_isysroot); |
| 1762 | RelocatablePCH.setValue(false); |
| 1763 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1765 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1766 | if (RelocatablePCH.getValue()) |
| 1767 | Consumer.reset(CreatePCHGenerator(PP, OS.get(), isysroot.c_str())); |
| 1768 | else |
| 1769 | Consumer.reset(CreatePCHGenerator(PP, OS.get())); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 1770 | CompleteTranslationUnit = false; |
| 1771 | break; |
| 1772 | |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1773 | case DumpRawTokens: { |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1774 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1775 | SourceManager &SM = PP.getSourceManager(); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1776 | // Start lexing the specified input file. |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 1777 | Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions()); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1778 | RawLex.SetKeepWhitespaceMode(true); |
| 1779 | |
| 1780 | Token RawTok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1781 | RawLex.LexFromRawLexer(RawTok); |
| 1782 | while (RawTok.isNot(tok::eof)) { |
| 1783 | PP.DumpToken(RawTok, true); |
| 1784 | fprintf(stderr, "\n"); |
| 1785 | RawLex.LexFromRawLexer(RawTok); |
| 1786 | } |
| 1787 | ClearSourceMgr = true; |
| 1788 | break; |
| 1789 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1790 | case DumpTokens: { // Token dump mode. |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1791 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1792 | Token Tok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1793 | // Start preprocessing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1794 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1795 | do { |
| 1796 | PP.Lex(Tok); |
| 1797 | PP.DumpToken(Tok, true); |
| 1798 | fprintf(stderr, "\n"); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1799 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1800 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1801 | break; |
| 1802 | } |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1803 | case RunPreprocessorOnly: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1804 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | |
Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 1806 | case GeneratePTH: { |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1807 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1808 | if (CompOpts.getOutputFile().empty() || CompOpts.getOutputFile() == "-") { |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 1809 | // FIXME: Don't fail this way. |
| 1810 | // FIXME: Verify that we can actually seek in the given file. |
Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 1811 | llvm::errs() << "ERROR: PTH requires an seekable file for output!\n"; |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 1812 | ::exit(1); |
| 1813 | } |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1814 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 1815 | CacheTokens(PP, static_cast<llvm::raw_fd_ostream*>(OS.get())); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1816 | ClearSourceMgr = true; |
| 1817 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 1820 | case PrintPreprocessedInput: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1821 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1822 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1824 | case ParseNoop: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1825 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1827 | case ParsePrintCallbacks: { |
| 1828 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1829 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 1830 | ParseFile(PP, CreatePrintParserActionsAction(PP, OS.get())); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1831 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1832 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | case ParseSyntaxOnly: { // -fsyntax-only |
| 1836 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1837 | Consumer.reset(new ASTConsumer()); |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 1838 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1839 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1841 | case RewriteMacros: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1842 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 1843 | RewriteMacrosInInput(PP, OS.get()); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1844 | ClearSourceMgr = true; |
| 1845 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 1847 | case RewriteTest: |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1848 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 1849 | DoRewriteTest(PP, OS.get()); |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 1850 | ClearSourceMgr = true; |
| 1851 | break; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 1852 | |
| 1853 | case FixIt: |
| 1854 | llvm::TimeRegion Timer(ClangFrontendTimer); |
| 1855 | Consumer.reset(new ASTConsumer()); |
Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 1856 | FixItRewrite = new FixItRewriter(PP.getDiagnostics(), |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 1857 | PP.getSourceManager(), |
| 1858 | PP.getLangOptions()); |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 1859 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1860 | } |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 1861 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 1862 | if (FixItAtLocations.size() > 0) { |
| 1863 | // Even without the "-fixit" flag, with may have some specific |
| 1864 | // locations where the user has requested fixes. Process those |
| 1865 | // locations now. |
| 1866 | if (!FixItRewrite) |
| 1867 | FixItRewrite = new FixItRewriter(PP.getDiagnostics(), |
| 1868 | PP.getSourceManager(), |
| 1869 | PP.getLangOptions()); |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 1871 | bool AddedFixitLocation = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | for (unsigned Idx = 0, Last = FixItAtLocations.size(); |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 1873 | Idx != Last; ++Idx) { |
| 1874 | RequestedSourceLocation Requested; |
Argyrios Kyrtzidis | 34d25d8 | 2009-06-23 22:01:39 +0000 | [diff] [blame] | 1875 | if (ResolveParsedLocation(FixItAtLocations[Idx], |
| 1876 | PP.getFileManager(), Requested)) { |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 1877 | fprintf(stderr, "FIX-IT could not find file \"%s\"\n", |
| 1878 | FixItAtLocations[Idx].FileName.c_str()); |
| 1879 | } else { |
| 1880 | FixItRewrite->addFixItLocation(Requested); |
| 1881 | AddedFixitLocation = true; |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 1885 | if (!AddedFixitLocation) { |
| 1886 | // All of the fix-it locations were bad. Don't fix anything. |
| 1887 | delete FixItRewrite; |
| 1888 | FixItRewrite = 0; |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | llvm::OwningPtr<ASTContext> ContextOwner; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1893 | if (Consumer) |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 1894 | ContextOwner.reset(new ASTContext(PP.getLangOptions(), |
| 1895 | PP.getSourceManager(), |
| 1896 | PP.getTargetInfo(), |
| 1897 | PP.getIdentifierTable(), |
| 1898 | PP.getSelectorTable(), |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 1899 | PP.getBuiltinInfo(), |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 1900 | /* FreeMemory = */ !DisableFree, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 1901 | /* size_reserve = */0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 1903 | llvm::OwningPtr<PCHReader> Reader; |
| 1904 | llvm::OwningPtr<ExternalASTSource> Source; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1906 | if (!ImplicitIncludePCH.empty()) { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1907 | // If the user specified -isysroot, it will be used for relocatable PCH |
| 1908 | // files. |
| 1909 | const char *isysrootPCH = 0; |
| 1910 | if (isysroot.getNumOccurrences() != 0) |
| 1911 | isysrootPCH = isysroot.c_str(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1913 | Reader.reset(new PCHReader(PP, ContextOwner.get(), isysrootPCH)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1914 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1915 | // The user has asked us to include a precompiled header. Load |
| 1916 | // the precompiled header into the AST context. |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1917 | switch (Reader->ReadPCH(ImplicitIncludePCH)) { |
| 1918 | case PCHReader::Success: { |
Douglas Gregor | e721f95 | 2009-04-28 18:58:38 +0000 | [diff] [blame] | 1919 | // Set the predefines buffer as suggested by the PCH |
| 1920 | // reader. Typically, the predefines buffer will be empty. |
| 1921 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 1922 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1923 | // Attach the PCH reader to the AST context as an external AST |
| 1924 | // source, so that declarations will be deserialized from the |
| 1925 | // PCH file as needed. |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 1926 | if (ContextOwner) { |
| 1927 | Source.reset(Reader.take()); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1928 | ContextOwner->setExternalSource(Source); |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 1929 | } |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1930 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1933 | case PCHReader::Failure: |
| 1934 | // Unrecoverable failure: don't even try to process the input |
| 1935 | // file. |
| 1936 | return; |
| 1937 | |
| 1938 | case PCHReader::IgnorePCH: |
Douglas Gregor | 1ab86ac | 2009-04-28 22:01:16 +0000 | [diff] [blame] | 1939 | // No suitable PCH file could be found. Return an error. |
| 1940 | return; |
| 1941 | |
| 1942 | #if 0 |
| 1943 | // FIXME: We can recover from failed attempts to load PCH |
| 1944 | // files. This code will do so, if we ever want to enable it. |
| 1945 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1946 | // We delayed the initialization of builtins in the hope of |
| 1947 | // loading the PCH file. Since the PCH file could not be |
| 1948 | // loaded, initialize builtins now. |
| 1949 | if (ContextOwner) |
| 1950 | ContextOwner->InitializeBuiltins(PP.getIdentifierTable()); |
Douglas Gregor | 1ab86ac | 2009-04-28 22:01:16 +0000 | [diff] [blame] | 1951 | #endif |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | // Finish preprocessor initialization. We do this now (rather |
| 1955 | // than earlier) because this initialization creates new source |
| 1956 | // location entries in the source manager, which must come after |
| 1957 | // the source location entries for the PCH file. |
| 1958 | if (InitializeSourceManager(PP, InFile)) |
| 1959 | return; |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 1960 | } |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1961 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1962 | // If we have an ASTConsumer, run the parser with it. |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1963 | if (Consumer) { |
| 1964 | CodeCompleteConsumer *(*CreateCodeCompleter)(Sema &, void *) = 0; |
| 1965 | void *CreateCodeCompleterData = 0; |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1966 | |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 1967 | if (!CodeCompletionAt.FileName.empty()) { |
| 1968 | // Tell the source manager to chop off the given file at a specific |
| 1969 | // line and column. |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1970 | if (const FileEntry *Entry |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 1971 | = PP.getFileManager().getFile(CodeCompletionAt.FileName)) { |
| 1972 | // Truncate the named file at the given line/column. |
| 1973 | PP.getSourceManager().truncateFileAt(Entry, CodeCompletionAt.Line, |
| 1974 | CodeCompletionAt.Column); |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 1976 | // Set up the creation routine for code-completion. |
| 1977 | CreateCodeCompleter = BuildPrintingCodeCompleter; |
| 1978 | } else { |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1979 | PP.getDiagnostics().Report(FullSourceLoc(), |
Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 1980 | diag::err_fe_invalid_code_complete_file) |
| 1981 | << CodeCompletionAt.FileName; |
| 1982 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | ParseAST(PP, Consumer.get(), *ContextOwner.get(), Stats, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1986 | CompleteTranslationUnit, |
| 1987 | CreateCodeCompleter, CreateCodeCompleterData); |
| 1988 | } |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1989 | |
Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 1990 | // Perform post processing actions and actions which don't use a consumer. |
| 1991 | switch (PA) { |
| 1992 | default: break; |
| 1993 | |
| 1994 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1995 | llvm::TimeRegion Timer(ClangFrontendTimer); |
| 1996 | Token Tok; |
| 1997 | // Start parsing the specified input file. |
| 1998 | PP.EnterMainSourceFile(); |
| 1999 | do { |
| 2000 | PP.Lex(Tok); |
| 2001 | } while (Tok.isNot(tok::eof)); |
| 2002 | ClearSourceMgr = true; |
Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 2003 | break; |
| 2004 | } |
| 2005 | |
| 2006 | case ParseNoop: { |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2007 | llvm::TimeRegion Timer(ClangFrontendTimer); |
| 2008 | ParseFile(PP, new MinimalAction(PP)); |
| 2009 | ClearSourceMgr = true; |
Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 2010 | break; |
| 2011 | } |
| 2012 | |
| 2013 | case PrintPreprocessedInput: { |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 2014 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 2015 | if (DumpMacros) |
| 2016 | DoPrintMacros(PP, OS.get()); |
| 2017 | else |
| 2018 | DoPrintPreprocessedInput(PP, OS.get(), EnableCommentOutput, |
| 2019 | EnableMacroCommentOutput, |
| 2020 | DisableLineMarkers, DumpDefines); |
Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 2021 | ClearSourceMgr = true; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2022 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | |
Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 2026 | if (FixItRewrite) |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 2027 | FixItRewrite->WriteFixedFile(InFile, CompOpts.getOutputFile()); |
Daniel Dunbar | 70186ab | 2009-07-29 02:40:09 +0000 | [diff] [blame] | 2028 | |
| 2029 | // Disable the consumer prior to the context, the consumer may perform actions |
| 2030 | // in its destructor which require the context. |
| 2031 | if (DisableFree) |
| 2032 | Consumer.take(); |
| 2033 | else |
| 2034 | Consumer.reset(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | |
Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 2036 | // If in -disable-free mode, don't deallocate ASTContext. |
| 2037 | if (DisableFree) |
| 2038 | ContextOwner.take(); |
| 2039 | else |
| 2040 | ContextOwner.reset(); // Delete ASTContext |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 2041 | |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 2042 | if (VerifyDiagnostics) |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 2043 | if (CheckDiagnostics(PP)) |
| 2044 | exit(1); |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 2045 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2046 | if (Stats) { |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 2047 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2048 | PP.PrintStats(); |
| 2049 | PP.getIdentifierTable().PrintStats(); |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 2050 | PP.getHeaderSearchInfo().PrintStats(); |
Ted Kremenek | 1b95a65 | 2009-01-09 18:20:21 +0000 | [diff] [blame] | 2051 | PP.getSourceManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2052 | fprintf(stderr, "\n"); |
| 2053 | } |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 2054 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | // 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] | 2056 | // manager tables, other require stable fileid/macroid's across multiple |
| 2057 | // files. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 2058 | if (ClearSourceMgr) |
| 2059 | PP.getSourceManager().clearIDTables(); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 2060 | |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 2061 | // Always delete the output stream because we don't want to leak file |
| 2062 | // handles. Also, we don't want to try to erase an open file. |
| 2063 | OS.reset(); |
| 2064 | |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 2065 | // If we had errors, try to erase the output file. |
| 2066 | if (PP.getDiagnostics().getNumErrors() && !OutPath.isEmpty()) |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 2067 | OutPath.eraseFromDisk(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2070 | /// ProcessInputFile - Process a single AST input file with the specified state. |
| 2071 | /// |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 2072 | static void ProcessASTInputFile(const CompilerInvocation &CompOpts, |
| 2073 | const std::string &InFile, ProgActions PA, |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2074 | Diagnostic &Diags, FileManager &FileMgr, |
| 2075 | llvm::LLVMContext& Context) { |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2076 | std::string Error; |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 2077 | llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, &Error)); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2078 | if (!AST) { |
| 2079 | Diags.Report(FullSourceLoc(), diag::err_fe_invalid_ast_file) << Error; |
| 2080 | return; |
| 2081 | } |
| 2082 | |
| 2083 | Preprocessor &PP = AST->getPreprocessor(); |
| 2084 | |
| 2085 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 2086 | llvm::sys::Path OutPath; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 2087 | llvm::OwningPtr<ASTConsumer> Consumer(CreateConsumerAction(CompOpts, PP, |
| 2088 | InFile, PA, OS, |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 2089 | OutPath, Context)); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2090 | |
| 2091 | if (!Consumer.get()) { |
| 2092 | Diags.Report(FullSourceLoc(), diag::err_fe_invalid_ast_action); |
| 2093 | return; |
| 2094 | } |
| 2095 | |
Daniel Dunbar | a674bf4 | 2009-09-21 03:03:56 +0000 | [diff] [blame] | 2096 | // Set the main file ID to an empty file. |
| 2097 | // |
| 2098 | // FIXME: We probably shouldn't need this, but for now this is the simplest |
| 2099 | // way to reuse the logic in ParseAST. |
| 2100 | const char *EmptyStr = ""; |
| 2101 | llvm::MemoryBuffer *SB = |
| 2102 | llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<dummy input>"); |
| 2103 | AST->getSourceManager().createMainFileIDForMemBuffer(SB); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2104 | |
Daniel Dunbar | a674bf4 | 2009-09-21 03:03:56 +0000 | [diff] [blame] | 2105 | // Stream the input AST to the consumer. |
Daniel Dunbar | efcbe94 | 2009-11-05 02:42:12 +0000 | [diff] [blame] | 2106 | Diags.getClient()->BeginSourceFile(PP.getLangOptions()); |
Daniel Dunbar | a674bf4 | 2009-09-21 03:03:56 +0000 | [diff] [blame] | 2107 | ParseAST(PP, Consumer.get(), AST->getASTContext(), Stats); |
Daniel Dunbar | efcbe94 | 2009-11-05 02:42:12 +0000 | [diff] [blame] | 2108 | Diags.getClient()->EndSourceFile(); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2109 | |
| 2110 | // Release the consumer and the AST, in that order since the consumer may |
| 2111 | // perform actions in its destructor which require the context. |
| 2112 | if (DisableFree) { |
| 2113 | Consumer.take(); |
| 2114 | AST.take(); |
| 2115 | } else { |
| 2116 | Consumer.reset(); |
| 2117 | AST.reset(); |
| 2118 | } |
| 2119 | |
| 2120 | // Always delete the output stream because we don't want to leak file |
| 2121 | // handles. Also, we don't want to try to erase an open file. |
| 2122 | OS.reset(); |
| 2123 | |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 2124 | // If we had errors, try to erase the output file. |
| 2125 | if (PP.getDiagnostics().getNumErrors() && !OutPath.isEmpty()) |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2126 | OutPath.eraseFromDisk(); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2129 | static llvm::cl::list<std::string> |
| 2130 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 2131 | |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 2132 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 2133 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 2134 | |
| 2135 | Diags.Report(FullSourceLoc(), diag::err_fe_error_backend) << Message; |
| 2136 | |
| 2137 | // We cannot recover from llvm errors. |
| 2138 | exit(1); |
| 2139 | } |
| 2140 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2141 | static LangKind GetLanguage() { |
| 2142 | // If -x was given, that's the language. |
| 2143 | if (BaseLang != langkind_unspecified) |
| 2144 | return BaseLang; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 2145 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2146 | // Otherwise guess it from the input filenames; |
| 2147 | LangKind LK = langkind_unspecified; |
| 2148 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
| 2149 | llvm::StringRef Name(InputFilenames[i]); |
| 2150 | LangKind ThisKind = llvm::StringSwitch<LangKind>(Name.rsplit('.').second) |
| 2151 | .Case("ast", langkind_ast) |
| 2152 | .Case("c", langkind_c) |
| 2153 | .Cases("S", "s", langkind_asm_cpp) |
| 2154 | .Case("i", langkind_c_cpp) |
| 2155 | .Case("ii", langkind_cxx_cpp) |
| 2156 | .Case("m", langkind_objc) |
| 2157 | .Case("mi", langkind_objc_cpp) |
| 2158 | .Cases("mm", "M", langkind_objcxx) |
| 2159 | .Case("mii", langkind_objcxx_cpp) |
| 2160 | .Case("C", langkind_cxx) |
| 2161 | .Cases("C", "cc", "cp", langkind_cxx) |
| 2162 | .Cases("cpp", "CPP", "c++", "cxx", langkind_cxx) |
| 2163 | .Case("cl", langkind_ocl) |
| 2164 | .Default(langkind_c); |
| 2165 | |
| 2166 | if (LK != langkind_unspecified && ThisKind != LK) { |
| 2167 | llvm::errs() << "error: cannot have multiple input files of distinct " |
| 2168 | << "language kinds without -x\n"; |
| 2169 | exit(1); |
| 2170 | } |
| 2171 | |
| 2172 | LK = ThisKind; |
| 2173 | } |
| 2174 | |
| 2175 | return LK; |
| 2176 | } |
| 2177 | |
| 2178 | static void ConstructDiagnosticOptions(DiagnosticOptions &Opts) { |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 2179 | // Initialize the diagnostic options. |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2180 | Opts.ShowColumn = !NoShowColumn; |
| 2181 | Opts.ShowLocation = !NoShowLocation; |
| 2182 | Opts.ShowCarets = !NoCaretDiagnostics; |
| 2183 | Opts.ShowFixits = !NoDiagnosticsFixIt; |
| 2184 | Opts.ShowSourceRanges = PrintSourceRangeInfo; |
| 2185 | Opts.ShowOptionNames = PrintDiagnosticOption; |
| 2186 | Opts.ShowColors = PrintColorDiagnostic; |
| 2187 | Opts.MessageLength = MessageLength; |
| 2188 | } |
| 2189 | |
| 2190 | static void ConstructCompilerInvocation(CompilerInvocation &Opts, |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 2191 | const char *Argv0, |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2192 | const DiagnosticOptions &DiagOpts, |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 2193 | TargetInfo &Target, |
| 2194 | LangKind LK) { |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2195 | Opts.getDiagnosticOpts() = DiagOpts; |
| 2196 | |
| 2197 | Opts.getOutputFile() = OutputFile; |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 2198 | |
| 2199 | // Compute the feature set, which may effect the language. |
| 2200 | ComputeFeatureMap(Target, Opts.getTargetFeatures()); |
| 2201 | |
| 2202 | // Initialize language options. |
| 2203 | LangOptions LangInfo; |
| 2204 | |
| 2205 | // FIXME: These aren't used during operations on ASTs. Split onto a separate |
| 2206 | // code path to make this obvious. |
| 2207 | if (LK != langkind_ast) { |
| 2208 | InitializeLangOptions(Opts.getLangOpts(), LK); |
| 2209 | InitializeLanguageStandard(Opts.getLangOpts(), LK, Target, |
| 2210 | Opts.getTargetFeatures()); |
| 2211 | } |
| 2212 | |
| 2213 | // Initialize the header search options. |
| 2214 | InitializeIncludePaths(Opts.getHeaderSearchOpts(), Argv0, Opts.getLangOpts()); |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2217 | int main(int argc, char **argv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2218 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 2219 | llvm::PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | d720046 | 2009-07-16 00:14:12 +0000 | [diff] [blame] | 2220 | llvm::LLVMContext &Context = llvm::getGlobalContext(); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 2221 | |
Daniel Dunbar | 4d86151 | 2009-09-03 04:54:12 +0000 | [diff] [blame] | 2222 | // Initialize targets first, so that --version shows registered targets. |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 2223 | llvm::InitializeAllTargets(); |
| 2224 | llvm::InitializeAllAsmPrinters(); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 2225 | |
| 2226 | llvm::cl::ParseCommandLineOptions(argc, argv, |
| 2227 | "LLVM 'Clang' Compiler: http://clang.llvm.org\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2229 | if (TimeReport) |
| 2230 | ClangFrontendTimer = new llvm::Timer("Clang front-end time"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | |
Daniel Dunbar | 08e6dc6 | 2009-05-28 16:37:33 +0000 | [diff] [blame] | 2232 | if (Verbose) |
Mike Stump | 3cbf5a0 | 2009-09-15 21:49:22 +0000 | [diff] [blame] | 2233 | llvm::errs() << "clang-cc version " CLANG_VERSION_STRING |
| 2234 | << " based upon " << PACKAGE_STRING |
Daniel Dunbar | 2e30e59 | 2009-09-04 17:43:10 +0000 | [diff] [blame] | 2235 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2237 | // If no input was specified, read from stdin. |
| 2238 | if (InputFilenames.empty()) |
| 2239 | InputFilenames.push_back("-"); |
Douglas Gregor | 68a0d78 | 2009-05-02 00:03:46 +0000 | [diff] [blame] | 2240 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2241 | // Construct the diagnostic options first, which cannot fail, so that we can |
| 2242 | // build a diagnostic client to use for any errors during option handling. |
| 2243 | DiagnosticOptions DiagOpts; |
| 2244 | ConstructDiagnosticOptions(DiagOpts); |
Daniel Dunbar | eace874 | 2009-11-04 06:24:30 +0000 | [diff] [blame] | 2245 | |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 2246 | // Create the diagnostic client for reporting errors or for |
| 2247 | // implementing -verify. |
Chris Lattner | 409d4e7 | 2009-04-17 20:40:01 +0000 | [diff] [blame] | 2248 | llvm::OwningPtr<DiagnosticClient> DiagClient; |
| 2249 | if (VerifyDiagnostics) { |
| 2250 | // When checking diagnostics, just buffer them up. |
| 2251 | DiagClient.reset(new TextDiagnosticBuffer()); |
| 2252 | if (InputFilenames.size() != 1) { |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2253 | fprintf(stderr, "-verify only works on single input files.\n"); |
Chris Lattner | 409d4e7 | 2009-04-17 20:40:01 +0000 | [diff] [blame] | 2254 | return 1; |
| 2255 | } |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2256 | } else { |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2257 | DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), DiagOpts)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2258 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | |
Daniel Dunbar | ad451cc | 2009-11-05 02:11:37 +0000 | [diff] [blame] | 2260 | if (!DumpBuildInformation.empty()) |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2261 | SetUpBuildDumpLog(DiagOpts, argc, argv, DiagClient); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2262 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2263 | // Configure our handling of diagnostics. |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2264 | Diagnostic Diags(DiagClient.get()); |
Eli Friedman | 0eeb86e | 2009-05-19 01:17:04 +0000 | [diff] [blame] | 2265 | if (ProcessWarningOptions(Diags, OptWarnings, OptPedantic, OptPedanticErrors, |
| 2266 | OptNoWarnings)) |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 2267 | return 1; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 2268 | |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 2269 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 2270 | // error handler. |
| 2271 | llvm::llvm_install_error_handler(LLVMErrorHandler, |
| 2272 | static_cast<void*>(&Diags)); |
| 2273 | |
Daniel Dunbar | 7c15e71 | 2009-10-30 18:12:31 +0000 | [diff] [blame] | 2274 | // Initialize base triple. If a -triple option has been specified, use |
| 2275 | // that triple. Otherwise, default to the host triple. |
| 2276 | llvm::Triple Triple(TargetTriple); |
| 2277 | if (Triple.getTriple().empty()) |
| 2278 | Triple = llvm::Triple(llvm::sys::getHostTriple()); |
| 2279 | |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 2280 | // Get information about the target being compiled for. |
Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 2281 | llvm::OwningPtr<TargetInfo> |
Chris Lattner | 2f60af7 | 2009-09-12 22:45:58 +0000 | [diff] [blame] | 2282 | Target(TargetInfo::CreateTargetInfo(Triple.getTriple())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 2284 | if (Target == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2285 | Diags.Report(FullSourceLoc(), diag::err_fe_unknown_triple) |
Chris Lattner | 2f60af7 | 2009-09-12 22:45:58 +0000 | [diff] [blame] | 2286 | << Triple.getTriple().c_str(); |
Sebastian Redl | c5613db | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 2287 | return 1; |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 2288 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | |
Daniel Dunbar | 73b7959 | 2009-09-14 00:02:12 +0000 | [diff] [blame] | 2290 | // Set the target ABI if specified. |
| 2291 | if (!TargetABI.empty()) { |
| 2292 | if (!Target->setABI(TargetABI)) { |
| 2293 | Diags.Report(FullSourceLoc(), diag::err_fe_unknown_target_abi) |
| 2294 | << TargetABI; |
| 2295 | return 1; |
| 2296 | } |
| 2297 | } |
| 2298 | |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 2299 | if (!InheritanceViewCls.empty()) // C++ visualization? |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 2300 | ProgAction = InheritanceView; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2302 | // Infer the input language. |
| 2303 | // |
| 2304 | // FIXME: We should move .ast inputs to taking a separate path, they are |
| 2305 | // really quite different. |
| 2306 | LangKind LK = GetLanguage(); |
| 2307 | |
| 2308 | // Now that we have initialized the diagnostics engine and the target, finish |
| 2309 | // setting up the compiler invocation. |
| 2310 | CompilerInvocation CompOpts; |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 2311 | ConstructCompilerInvocation(CompOpts, argv[0], DiagOpts, *Target, LK); |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2312 | |
Daniel Dunbar | 4cc1a25 | 2009-11-05 01:53:23 +0000 | [diff] [blame] | 2313 | // Create the source manager. |
| 2314 | SourceManager SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2315 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2316 | // Create a file manager object to provide access to and cache the filesystem. |
| 2317 | FileManager FileMgr; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2319 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 2320 | const std::string &InFile = InputFilenames[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2322 | // AST inputs are handled specially. |
| 2323 | if (LK == langkind_ast) { |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 2324 | ProcessASTInputFile(CompOpts, InFile, ProgAction, Diags, FileMgr, |
| 2325 | Context); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 2326 | continue; |
| 2327 | } |
| 2328 | |
Daniel Dunbar | 4cc1a25 | 2009-11-05 01:53:23 +0000 | [diff] [blame] | 2329 | // Reset the ID tables if we are reusing the SourceManager. |
| 2330 | if (i) |
| 2331 | SourceMgr.clearIDTables(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2332 | |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2333 | // Process the -I options and set them in the HeaderInfo. |
| 2334 | HeaderSearch HeaderInfo(FileMgr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 2336 | // Apply all the options to the header search object. |
| 2337 | ApplyHeaderSearchOptions(CompOpts.getHeaderSearchOpts(), HeaderInfo, |
| 2338 | CompOpts.getLangOpts(), Triple); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2339 | |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2340 | // Set up the preprocessor with these options. |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 2341 | llvm::OwningPtr<Preprocessor> PP(CreatePreprocessor(Diags, |
| 2342 | CompOpts.getLangOpts(), |
Daniel Dunbar | 4cc1a25 | 2009-11-05 01:53:23 +0000 | [diff] [blame] | 2343 | *Target, SourceMgr, |
Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 2344 | HeaderInfo)); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2345 | |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 2346 | // Handle generating dependencies, if requested. |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 2347 | if (!DependencyFile.empty()) { |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 2348 | if (DependencyTargets.empty()) { |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 2349 | Diags.Report(FullSourceLoc(), diag::err_fe_dependency_file_requires_MT); |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 2350 | continue; |
| 2351 | } |
| 2352 | std::string ErrStr; |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 2353 | llvm::raw_ostream *DependencyOS = |
Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 2354 | new llvm::raw_fd_ostream(DependencyFile.c_str(), ErrStr); |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 2355 | if (!ErrStr.empty()) { |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 2356 | Diags.Report(FullSourceLoc(), diag::err_fe_error_opening) |
| 2357 | << DependencyFile << ErrStr; |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 2358 | continue; |
| 2359 | } |
| 2360 | |
| 2361 | AttachDependencyFileGen(PP.get(), DependencyOS, DependencyTargets, |
| 2362 | DependenciesIncludeSystemHeaders, |
| 2363 | PhonyDependencyTarget); |
| 2364 | } |
| 2365 | |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 2366 | if (ImplicitIncludePCH.empty()) { |
| 2367 | if (InitializeSourceManager(*PP.get(), InFile)) |
| 2368 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 2370 | // Initialize builtin info. |
| 2371 | PP->getBuiltinInfo().InitializeBuiltins(PP->getIdentifierTable(), |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 2372 | PP->getLangOptions().NoBuiltin); |
| 2373 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 2374 | |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2375 | // Process the source file. |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 2376 | Diags.getClient()->BeginSourceFile(CompOpts.getLangOpts()); |
| 2377 | ProcessInputFile(CompOpts, *PP, InFile, ProgAction, Context); |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 2378 | Diags.getClient()->EndSourceFile(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | |
Chris Lattner | 4046965 | 2009-04-17 20:16:08 +0000 | [diff] [blame] | 2380 | HeaderInfo.ClearFileInfo(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2381 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 2382 | |
Mike Stump | fc0fed3 | 2009-04-28 01:19:10 +0000 | [diff] [blame] | 2383 | if (!NoCaretDiagnostics) |
| 2384 | if (unsigned NumDiagnostics = Diags.getNumDiagnostics()) |
| 2385 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 2386 | (NumDiagnostics == 1 ? "" : "s")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2388 | if (Stats) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2389 | FileMgr.PrintStats(); |
| 2390 | fprintf(stderr, "\n"); |
| 2391 | } |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 2392 | |
| 2393 | delete ClangFrontendTimer; |
| 2394 | delete BuildLogFile; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 2396 | // If verifying diagnostics and we reached here, all is well. |
| 2397 | if (VerifyDiagnostics) |
| 2398 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2399 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 2400 | // Managed static deconstruction. Useful for making things like |
| 2401 | // -time-passes usable. |
| 2402 | llvm::llvm_shutdown(); |
| 2403 | |
Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 2404 | return (Diags.getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2405 | } |