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