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