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 | // |
| 20 | // -ffatal-errors |
| 21 | // -ftabstop=width |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "clang.h" |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 26 | #include "ASTConsumers.h" |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 27 | #include "clang/Driver/CompileOptions.h" |
Ted Kremenek | ad99dbf | 2008-11-03 22:31:48 +0000 | [diff] [blame] | 28 | #include "clang/Driver/PathDiagnosticClients.h" |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 29 | #include "clang/Driver/InitHeaderSearch.h" |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 30 | #include "clang/Driver/TextDiagnosticBuffer.h" |
| 31 | #include "clang/Driver/TextDiagnosticPrinter.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 32 | #include "clang/Analysis/PathDiagnostic.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 33 | #include "clang/AST/Decl.h" |
Ted Kremenek | 77cda50 | 2007-12-18 21:34:28 +0000 | [diff] [blame] | 34 | #include "clang/AST/TranslationUnit.h" |
Chris Lattner | 8ee3c03 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 35 | #include "clang/CodeGen/ModuleBuilder.h" |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 36 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 37 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | #include "clang/Parse/Parser.h" |
| 39 | #include "clang/Lex/HeaderSearch.h" |
| 40 | #include "clang/Basic/FileManager.h" |
| 41 | #include "clang/Basic/SourceManager.h" |
| 42 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 8f3dab8 | 2007-12-15 23:20:07 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/StringExtras.h" |
| 46 | #include "llvm/Config/config.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | #include "llvm/Support/MemoryBuffer.h" |
Zhongxing Xu | 2092236 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 50 | #include "llvm/Support/PluginLoader.h" |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 51 | #include "llvm/System/Host.h" |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 52 | #include "llvm/System/Path.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 53 | #include "llvm/System/Signals.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | using namespace clang; |
| 55 | |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | // Global options. |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 60 | static bool HadErrors = false; |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 61 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 62 | static llvm::cl::opt<bool> |
| 63 | Verbose("v", llvm::cl::desc("Enable verbose output")); |
| 64 | static llvm::cl::opt<bool> |
Nate Begeman | aabbb12 | 2007-12-30 01:38:50 +0000 | [diff] [blame] | 65 | Stats("print-stats", |
| 66 | llvm::cl::desc("Print performance metrics and statistics")); |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 67 | static llvm::cl::opt<bool> |
| 68 | DisableFree("disable-free", |
| 69 | llvm::cl::desc("Disable freeing of memory on exit"), |
| 70 | llvm::cl::init(false)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | |
| 72 | enum ProgActions { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 73 | RewriteObjC, // ObjC->C Rewriter. |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 74 | RewriteBlocks, // ObjC->C Rewriter for Blocks. |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 75 | RewriteMacros, // Expand macros but not #includes. |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 76 | RewriteTest, // Rewriter playground |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 77 | HTMLTest, // HTML displayer testing stuff. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 78 | EmitAssembly, // Emit a .s file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | EmitLLVM, // Emit a .ll file. |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 80 | EmitBC, // Emit a .bc file. |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 81 | SerializeAST, // Emit a .ast file. |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 82 | EmitHTML, // Translate input source into HTML. |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 83 | ASTPrint, // Parse ASTs and print them. |
| 84 | ASTDump, // Parse ASTs and dump them. |
| 85 | ASTView, // Parse ASTs and view them in Graphviz. |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 86 | TestSerialization, // Run experimental serialization code. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | ParsePrintCallbacks, // Parse and print each callback. |
| 88 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
| 89 | ParseNoop, // Parse with noop callbacks. |
| 90 | RunPreprocessorOnly, // Just lex, no output. |
| 91 | PrintPreprocessedInput, // -E mode. |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 92 | DumpTokens, // Dump out preprocessed tokens. |
| 93 | DumpRawTokens, // Dump out raw tokens. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 94 | RunAnalysis, // Run one or more source code analyses. |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 95 | GeneratePCH, // Generate precompiled header. |
| 96 | InheritanceView // View C++ inheritance for a specified class. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static llvm::cl::opt<ProgActions> |
| 100 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 101 | llvm::cl::init(ParseSyntaxOnly), |
| 102 | llvm::cl::values( |
| 103 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 104 | "Just run preprocessor, no output (for timings)"), |
| 105 | clEnumValN(PrintPreprocessedInput, "E", |
| 106 | "Run preprocessor, emit preprocessed file"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 107 | clEnumValN(DumpRawTokens, "dump-raw-tokens", |
| 108 | "Lex file in raw mode and dump raw tokens"), |
| 109 | clEnumValN(DumpTokens, "dump-tokens", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | "Run preprocessor, dump internal rep of tokens"), |
| 111 | clEnumValN(ParseNoop, "parse-noop", |
| 112 | "Run parser with noop callbacks (for timings)"), |
| 113 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 114 | "Run parser and perform semantic analysis"), |
| 115 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 116 | "Run parser and print each callback invoked"), |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 117 | clEnumValN(EmitHTML, "emit-html", |
| 118 | "Output input source as HTML"), |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 119 | clEnumValN(ASTPrint, "ast-print", |
| 120 | "Build ASTs and then pretty-print them"), |
| 121 | clEnumValN(ASTDump, "ast-dump", |
| 122 | "Build ASTs and then debug dump them"), |
Chris Lattner | ea254db | 2007-10-11 00:37:43 +0000 | [diff] [blame] | 123 | clEnumValN(ASTView, "ast-view", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 124 | "Build ASTs and view them with GraphViz"), |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 125 | clEnumValN(TestSerialization, "test-pickling", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 126 | "Run prototype serialization code"), |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 127 | clEnumValN(EmitAssembly, "S", |
| 128 | "Emit native assembly code"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | clEnumValN(EmitLLVM, "emit-llvm", |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 130 | "Build ASTs then convert to LLVM, emit .ll file"), |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 131 | clEnumValN(EmitBC, "emit-llvm-bc", |
| 132 | "Build ASTs then convert to LLVM, emit .bc file"), |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 133 | clEnumValN(SerializeAST, "serialize", |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 134 | "Build ASTs and emit .ast file"), |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 135 | clEnumValN(RewriteTest, "rewrite-test", |
| 136 | "Rewriter playground"), |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 137 | clEnumValN(RewriteObjC, "rewrite-objc", |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 138 | "Rewrite ObjC into C (code rewriter example)"), |
| 139 | clEnumValN(RewriteMacros, "rewrite-macros", |
| 140 | "Expand macros without full preprocessing"), |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 141 | clEnumValN(RewriteBlocks, "rewrite-blocks", |
| 142 | "Rewrite Blocks to C"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | clEnumValEnd)); |
| 144 | |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 145 | |
| 146 | static llvm::cl::opt<std::string> |
| 147 | OutputFile("o", |
Ted Kremenek | 50b5641 | 2007-12-19 19:50:41 +0000 | [diff] [blame] | 148 | llvm::cl::value_desc("path"), |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 149 | llvm::cl::desc("Specify output file (for --serialize, this is a directory)")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 150 | |
| 151 | //===----------------------------------------------------------------------===// |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 152 | // Code Generator Options |
| 153 | //===----------------------------------------------------------------------===// |
| 154 | static llvm::cl::opt<bool> |
| 155 | GenerateDebugInfo("g", |
| 156 | llvm::cl::desc("Generate source level debug information")); |
| 157 | |
Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 158 | |
| 159 | //===----------------------------------------------------------------------===// |
| 160 | // PTH. |
| 161 | //===----------------------------------------------------------------------===// |
| 162 | |
| 163 | static llvm::cl::opt<std::string> |
| 164 | TokenCache("token-cache", llvm::cl::value_desc("path"), |
| 165 | llvm::cl::desc("Use specified token cache file")); |
| 166 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 167 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 168 | // Diagnostic Options |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 171 | static llvm::cl::opt<bool> |
| 172 | VerifyDiagnostics("verify", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 173 | llvm::cl::desc("Verify emitted diagnostics and warnings")); |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 175 | static llvm::cl::opt<std::string> |
| 176 | HTMLDiag("html-diags", |
| 177 | llvm::cl::desc("Generate HTML to report diagnostics"), |
| 178 | llvm::cl::value_desc("HTML directory")); |
| 179 | |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 180 | static llvm::cl::opt<bool> |
| 181 | NoShowColumn("fno-show-column", |
| 182 | llvm::cl::desc("Do not include column number on diagnostics")); |
| 183 | |
| 184 | static llvm::cl::opt<bool> |
| 185 | NoCaretDiagnostics("fno-caret-diagnostics", |
| 186 | llvm::cl::desc("Do not include source line and caret with" |
| 187 | " diagnostics")); |
| 188 | |
| 189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 191 | // C++ Visualization. |
| 192 | //===----------------------------------------------------------------------===// |
| 193 | |
| 194 | static llvm::cl::opt<std::string> |
| 195 | InheritanceViewCls("cxx-inheritance-view", |
| 196 | llvm::cl::value_desc("class name"), |
| 197 | llvm::cl::desc("View C++ inhertance for a specified class")); |
| 198 | |
| 199 | //===----------------------------------------------------------------------===// |
| 200 | // Analyzer Options. |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 201 | //===----------------------------------------------------------------------===// |
| 202 | |
| 203 | static llvm::cl::opt<bool> |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 204 | VisualizeEGDot("analyzer-viz-egraph-graphviz", |
| 205 | llvm::cl::desc("Display exploded graph using GraphViz")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 206 | |
| 207 | static llvm::cl::opt<bool> |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 208 | VisualizeEGUbi("analyzer-viz-egraph-ubigraph", |
| 209 | llvm::cl::desc("Display exploded graph using Ubigraph")); |
| 210 | |
| 211 | static llvm::cl::opt<bool> |
| 212 | AnalyzeAll("analyzer-opt-analyze-headers", |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 213 | llvm::cl::desc("Force the static analyzer to analyze " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 214 | "functions defined in header files")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 215 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 216 | static llvm::cl::list<Analyses> |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 217 | AnalysisList(llvm::cl::desc("SCA Checks/Analyses:"), |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 218 | llvm::cl::values( |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 219 | #define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\ |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 220 | clEnumValN(NAME, CMDFLAG, DESC), |
| 221 | #include "Analyses.def" |
| 222 | clEnumValEnd)); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 224 | static llvm::cl::opt<AnalysisStores> |
| 225 | AnalysisStoreOpt(llvm::cl::desc("SCA Low-Level Options (Store):"), |
| 226 | llvm::cl::init(BasicStoreModel), |
| 227 | llvm::cl::values( |
| 228 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC)\ |
| 229 | clEnumValN(NAME##Model, "analyzer-store-" CMDFLAG, DESC), |
| 230 | #include "Analyses.def" |
| 231 | clEnumValEnd)); |
| 232 | |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 233 | static llvm::cl::opt<AnalysisDiagClients> |
| 234 | AnalysisDiagOpt(llvm::cl::desc("SCA Output Options:"), |
| 235 | llvm::cl::init(PD_HTML), |
| 236 | llvm::cl::values( |
| 237 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN)\ |
| 238 | clEnumValN(PD_##NAME, "analyzer-output-" CMDFLAG, DESC), |
| 239 | #include "Analyses.def" |
| 240 | clEnumValEnd)); |
| 241 | |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 242 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | // Language Options |
| 244 | //===----------------------------------------------------------------------===// |
| 245 | |
| 246 | enum LangKind { |
| 247 | langkind_unspecified, |
| 248 | langkind_c, |
| 249 | langkind_c_cpp, |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 250 | langkind_asm_cpp, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 251 | langkind_c_pch, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 252 | langkind_cxx, |
| 253 | langkind_cxx_cpp, |
| 254 | langkind_objc, |
| 255 | langkind_objc_cpp, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 256 | langkind_objc_pch, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | langkind_objcxx, |
| 258 | langkind_objcxx_cpp |
| 259 | }; |
| 260 | |
| 261 | /* TODO: GCC also accepts: |
| 262 | c-header c++-header objective-c-header objective-c++-header |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 263 | assembler |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | ada, f77*, ratfor (!), f95, java, treelang |
| 265 | */ |
| 266 | static llvm::cl::opt<LangKind> |
| 267 | BaseLang("x", llvm::cl::desc("Base language to compile"), |
| 268 | llvm::cl::init(langkind_unspecified), |
| 269 | llvm::cl::values(clEnumValN(langkind_c, "c", "C"), |
| 270 | clEnumValN(langkind_cxx, "c++", "C++"), |
| 271 | clEnumValN(langkind_objc, "objective-c", "Objective C"), |
| 272 | clEnumValN(langkind_objcxx,"objective-c++","Objective C++"), |
| 273 | clEnumValN(langkind_c_cpp, "c-cpp-output", |
| 274 | "Preprocessed C"), |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 275 | clEnumValN(langkind_asm_cpp, "assembler-with-cpp", |
| 276 | "Preprocessed asm"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | clEnumValN(langkind_cxx_cpp, "c++-cpp-output", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 278 | "Preprocessed C++"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | clEnumValN(langkind_objc_cpp, "objective-c-cpp-output", |
| 280 | "Preprocessed Objective C"), |
| 281 | clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output", |
| 282 | "Preprocessed Objective C++"), |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 283 | clEnumValN(langkind_c_pch,"c-header", |
| 284 | "Precompiled C header"), |
| 285 | clEnumValN(langkind_objc_pch, "objective-c-header", |
| 286 | "Precompiled Objective C header"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 287 | clEnumValEnd)); |
| 288 | |
| 289 | static llvm::cl::opt<bool> |
| 290 | LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"), |
| 291 | llvm::cl::Hidden); |
| 292 | static llvm::cl::opt<bool> |
| 293 | LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"), |
| 294 | llvm::cl::Hidden); |
| 295 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 296 | /// InitializeBaseLanguage - Handle the -x foo options. |
| 297 | static void InitializeBaseLanguage() { |
| 298 | if (LangObjC) |
| 299 | BaseLang = langkind_objc; |
| 300 | else if (LangObjCXX) |
| 301 | BaseLang = langkind_objcxx; |
| 302 | } |
| 303 | |
| 304 | static LangKind GetLanguage(const std::string &Filename) { |
| 305 | if (BaseLang != langkind_unspecified) |
| 306 | return BaseLang; |
| 307 | |
| 308 | std::string::size_type DotPos = Filename.rfind('.'); |
| 309 | |
| 310 | if (DotPos == std::string::npos) { |
| 311 | BaseLang = langkind_c; // Default to C if no extension. |
Chris Lattner | 9b2f6c4 | 2008-01-04 19:12:28 +0000 | [diff] [blame] | 312 | return langkind_c; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 315 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 316 | // C header: .h |
| 317 | // C++ header: .hh or .H; |
| 318 | // assembler no preprocessing: .s |
| 319 | // assembler: .S |
| 320 | if (Ext == "c") |
| 321 | return langkind_c; |
Chris Lattner | 1b450b0 | 2008-10-28 20:33:42 +0000 | [diff] [blame] | 322 | else if (Ext == "S") |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 323 | return langkind_asm_cpp; |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 324 | else if (Ext == "i") |
| 325 | return langkind_c_cpp; |
| 326 | else if (Ext == "ii") |
| 327 | return langkind_cxx_cpp; |
| 328 | else if (Ext == "m") |
| 329 | return langkind_objc; |
| 330 | else if (Ext == "mi") |
| 331 | return langkind_objc_cpp; |
| 332 | else if (Ext == "mm" || Ext == "M") |
| 333 | return langkind_objcxx; |
| 334 | else if (Ext == "mii") |
| 335 | return langkind_objcxx_cpp; |
| 336 | else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" || |
| 337 | Ext == "c++" || Ext == "cp" || Ext == "cxx") |
| 338 | return langkind_cxx; |
| 339 | else |
| 340 | return langkind_c; |
| 341 | } |
| 342 | |
| 343 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 344 | static void InitializeCOptions(LangOptions &Options) { |
| 345 | // Do nothing. |
| 346 | } |
| 347 | |
| 348 | static void InitializeObjCOptions(LangOptions &Options) { |
| 349 | Options.ObjC1 = Options.ObjC2 = 1; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | static bool InitializeLangOptions(LangOptions &Options, LangKind LK){ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 354 | // FIXME: implement -fpreprocessed mode. |
| 355 | bool NoPreprocess = false; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 356 | bool PCH = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 358 | switch (LK) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | default: assert(0 && "Unknown language kind!"); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 360 | case langkind_c_pch: |
| 361 | InitializeCOptions(Options); |
| 362 | PCH = true; |
| 363 | break; |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 364 | case langkind_asm_cpp: |
Daniel Dunbar | c157145 | 2008-12-01 18:55:22 +0000 | [diff] [blame] | 365 | Options.AsmPreprocessor = 1; |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 366 | // FALLTHROUGH |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | case langkind_c_cpp: |
| 368 | NoPreprocess = true; |
| 369 | // FALLTHROUGH |
| 370 | case langkind_c: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 371 | InitializeCOptions(Options); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | break; |
| 373 | case langkind_cxx_cpp: |
| 374 | NoPreprocess = true; |
| 375 | // FALLTHROUGH |
| 376 | case langkind_cxx: |
| 377 | Options.CPlusPlus = 1; |
| 378 | break; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 379 | case langkind_objc_pch: |
| 380 | InitializeObjCOptions(Options); |
| 381 | PCH = true; |
| 382 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | case langkind_objc_cpp: |
| 384 | NoPreprocess = true; |
| 385 | // FALLTHROUGH |
| 386 | case langkind_objc: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 387 | InitializeObjCOptions(Options); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | break; |
| 389 | case langkind_objcxx_cpp: |
| 390 | NoPreprocess = true; |
| 391 | // FALLTHROUGH |
| 392 | case langkind_objcxx: |
| 393 | Options.ObjC1 = Options.ObjC2 = 1; |
| 394 | Options.CPlusPlus = 1; |
| 395 | break; |
| 396 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 397 | |
| 398 | return PCH; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /// LangStds - Language standards we support. |
| 402 | enum LangStds { |
| 403 | lang_unspecified, |
| 404 | lang_c89, lang_c94, lang_c99, |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 405 | lang_gnu_START, |
| 406 | lang_gnu89 = lang_gnu_START, lang_gnu99, |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 407 | lang_cxx98, lang_gnucxx98, |
| 408 | lang_cxx0x, lang_gnucxx0x |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | static llvm::cl::opt<LangStds> |
| 412 | LangStd("std", llvm::cl::desc("Language standard to compile for"), |
| 413 | llvm::cl::init(lang_unspecified), |
| 414 | llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"), |
| 415 | clEnumValN(lang_c89, "c90", "ISO C 1990"), |
| 416 | clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"), |
| 417 | clEnumValN(lang_c94, "iso9899:199409", |
| 418 | "ISO C 1990 with amendment 1"), |
| 419 | clEnumValN(lang_c99, "c99", "ISO C 1999"), |
| 420 | // clEnumValN(lang_c99, "c9x", "ISO C 1999"), |
| 421 | clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"), |
| 422 | // clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"), |
| 423 | clEnumValN(lang_gnu89, "gnu89", |
| 424 | "ISO C 1990 with GNU extensions (default for C)"), |
| 425 | clEnumValN(lang_gnu99, "gnu99", |
| 426 | "ISO C 1999 with GNU extensions"), |
| 427 | clEnumValN(lang_gnu99, "gnu9x", |
| 428 | "ISO C 1999 with GNU extensions"), |
| 429 | clEnumValN(lang_cxx98, "c++98", |
| 430 | "ISO C++ 1998 with amendments"), |
| 431 | clEnumValN(lang_gnucxx98, "gnu++98", |
| 432 | "ISO C++ 1998 with amendments and GNU " |
| 433 | "extensions (default for C++)"), |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 434 | clEnumValN(lang_cxx0x, "c++0x", |
| 435 | "Upcoming ISO C++ 200x with amendments"), |
| 436 | clEnumValN(lang_gnucxx0x, "gnu++0x", |
| 437 | "Upcoming ISO C++ 200x with amendments and GNU " |
| 438 | "extensions (default for C++)"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 439 | clEnumValEnd)); |
| 440 | |
| 441 | static llvm::cl::opt<bool> |
| 442 | NoOperatorNames("fno-operator-names", |
| 443 | llvm::cl::desc("Do not treat C++ operator name keywords as " |
| 444 | "synonyms for operators")); |
| 445 | |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 446 | static llvm::cl::opt<bool> |
| 447 | PascalStrings("fpascal-strings", |
| 448 | llvm::cl::desc("Recognize and construct Pascal-style " |
| 449 | "string literals")); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 450 | |
| 451 | static llvm::cl::opt<bool> |
| 452 | MSExtensions("fms-extensions", |
| 453 | llvm::cl::desc("Accept some non-standard constructs used in " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 454 | "Microsoft header files ")); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 455 | |
| 456 | static llvm::cl::opt<bool> |
| 457 | WritableStrings("fwritable-strings", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 458 | llvm::cl::desc("Store string literals as writable data")); |
Anders Carlsson | 695dbb6 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 459 | |
| 460 | static llvm::cl::opt<bool> |
| 461 | LaxVectorConversions("flax-vector-conversions", |
| 462 | llvm::cl::desc("Allow implicit conversions between vectors" |
| 463 | " with a different number of elements or " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 464 | "different element types")); |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 465 | static llvm::cl::opt<bool> |
| 466 | EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature")); |
| 467 | |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 468 | |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 469 | // FIXME: This (and all GCC -f options) really come in -f... and |
| 470 | // -fno-... forms, and additionally support automagic behavior when |
| 471 | // they are not defined. For example, -fexceptions defaults to on or |
| 472 | // off depending on the language. We should support this behavior in |
| 473 | // some form (perhaps just add a facility for distinguishing when an |
| 474 | // has its default value from when it has been set to its default |
| 475 | // value). |
| 476 | static llvm::cl::opt<bool> |
| 477 | Exceptions("fexceptions", |
| 478 | llvm::cl::desc("Enable support for exception handling.")); |
| 479 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 480 | static llvm::cl::opt<bool> |
| 481 | GNURuntime("fgnu-runtime", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 482 | llvm::cl::desc("Generate output compatible with the standard GNU " |
| 483 | "Objective-C runtime.")); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 484 | |
| 485 | static llvm::cl::opt<bool> |
| 486 | NeXTRuntime("fnext-runtime", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 487 | llvm::cl::desc("Generate output compatible with the NeXT " |
| 488 | "runtime.")); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 489 | |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 490 | |
| 491 | |
| 492 | static llvm::cl::opt<bool> |
| 493 | Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences.")); |
| 494 | |
| 495 | static llvm::cl::opt<bool> |
| 496 | Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89.")); |
| 497 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 498 | // FIXME: add: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 499 | // -fdollars-in-identifiers |
Daniel Dunbar | dcb4a1a | 2008-08-23 08:43:39 +0000 | [diff] [blame] | 500 | static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, |
| 501 | TargetInfo *Target) { |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 502 | // Allow the target to set the default the langauge options as it sees fit. |
| 503 | Target->getDefaultLangOptions(Options); |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 504 | |
| 505 | if (Ansi) // "The -ansi option is equivalent to -std=c89." |
| 506 | LangStd = lang_c89; |
| 507 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 508 | if (LangStd == lang_unspecified) { |
| 509 | // Based on the base language, pick one. |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 510 | switch (LK) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | default: assert(0 && "Unknown base language"); |
| 512 | case langkind_c: |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 513 | case langkind_asm_cpp: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 514 | case langkind_c_cpp: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 515 | case langkind_c_pch: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 516 | case langkind_objc: |
| 517 | case langkind_objc_cpp: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 518 | case langkind_objc_pch: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | LangStd = lang_gnu99; |
| 520 | break; |
| 521 | case langkind_cxx: |
| 522 | case langkind_cxx_cpp: |
| 523 | case langkind_objcxx: |
| 524 | case langkind_objcxx_cpp: |
| 525 | LangStd = lang_gnucxx98; |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | switch (LangStd) { |
| 531 | default: assert(0 && "Unknown language standard!"); |
| 532 | |
| 533 | // Fall through from newer standards to older ones. This isn't really right. |
| 534 | // FIXME: Enable specifically the right features based on the language stds. |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 535 | case lang_gnucxx0x: |
| 536 | case lang_cxx0x: |
| 537 | Options.CPlusPlus0x = 1; |
| 538 | // FALL THROUGH |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 539 | case lang_gnucxx98: |
| 540 | case lang_cxx98: |
| 541 | Options.CPlusPlus = 1; |
| 542 | Options.CXXOperatorNames = !NoOperatorNames; |
Nate Begeman | 8aebcb7 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 543 | Options.Boolean = 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 544 | // FALL THROUGH. |
| 545 | case lang_gnu99: |
| 546 | case lang_c99: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 547 | Options.C99 = 1; |
| 548 | Options.HexFloats = 1; |
| 549 | // FALL THROUGH. |
| 550 | case lang_gnu89: |
| 551 | Options.BCPLComment = 1; // Only for C99/C++. |
| 552 | // FALL THROUGH. |
| 553 | case lang_c94: |
Chris Lattner | 3426b9b | 2008-02-25 04:01:39 +0000 | [diff] [blame] | 554 | Options.Digraphs = 1; // C94, C99, C++. |
| 555 | // FALL THROUGH. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 556 | case lang_c89: |
| 557 | break; |
| 558 | } |
Argyrios Kyrtzidis | d146552 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 559 | |
| 560 | if (Options.CPlusPlus) { |
| 561 | Options.C99 = 0; |
| 562 | Options.HexFloats = (LangStd == lang_gnucxx98 || LangStd==lang_gnucxx0x); |
| 563 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 564 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 565 | if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89) |
| 566 | Options.ImplicitInt = 1; |
| 567 | else |
| 568 | Options.ImplicitInt = 0; |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 569 | |
| 570 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi |
| 571 | // is specified, or -std is set to a conforming mode. |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 572 | Options.Trigraphs = LangStd < lang_gnu_START; |
| 573 | if (Trigraphs.getPosition()) |
| 574 | Options.Trigraphs = Trigraphs; // Command line option wins. |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 576 | // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off |
| 577 | // even if they are normally on for the target. In GNU modes (e.g. |
| 578 | // -std=gnu99) the default for blocks depends on the target settings. |
| 579 | if (LangStd < lang_gnu_START) |
| 580 | Options.Blocks = 0; |
| 581 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 582 | Options.DollarIdents = 1; // FIXME: Really a target property. |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 583 | if (PascalStrings.getPosition()) |
| 584 | Options.PascalStrings = PascalStrings; |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 585 | Options.Microsoft = MSExtensions; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 586 | Options.WritableStrings = WritableStrings; |
Anders Carlsson | 695dbb6 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 587 | Options.LaxVectorConversions = LaxVectorConversions; |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 588 | Options.Exceptions = Exceptions; |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 589 | if (EnableBlocks.getPosition()) |
| 590 | Options.Blocks = EnableBlocks; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 592 | // Override the default runtime if the user requested it. |
| 593 | if (NeXTRuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 594 | Options.NeXTRuntime = 1; |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 595 | else if (GNURuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 596 | Options.NeXTRuntime = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 599 | static llvm::cl::opt<bool> |
| 600 | ObjCExclusiveGC("fobjc-gc-only", |
| 601 | llvm::cl::desc("Use GC exclusively for Objective-C related " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 602 | "memory management")); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 603 | |
| 604 | static llvm::cl::opt<bool> |
| 605 | ObjCEnableGC("fobjc-gc", |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 606 | llvm::cl::desc("Enable Objective-C garbage collection")); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 607 | |
| 608 | void InitializeGCMode(LangOptions &Options) { |
| 609 | if (ObjCExclusiveGC) |
| 610 | Options.setGCMode(LangOptions::GCOnly); |
| 611 | else if (ObjCEnableGC) |
| 612 | Options.setGCMode(LangOptions::HybridGC); |
| 613 | } |
| 614 | |
| 615 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | //===----------------------------------------------------------------------===// |
| 617 | // Our DiagnosticClient implementation |
| 618 | //===----------------------------------------------------------------------===// |
| 619 | |
| 620 | // FIXME: Werror should take a list of things, -Werror=foo,bar |
| 621 | static llvm::cl::opt<bool> |
| 622 | WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors")); |
| 623 | |
| 624 | static llvm::cl::opt<bool> |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 625 | SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings")); |
| 626 | |
| 627 | static llvm::cl::opt<bool> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 628 | WarnOnExtensions("pedantic", llvm::cl::init(false), |
| 629 | llvm::cl::desc("Issue a warning on uses of GCC extensions")); |
| 630 | |
| 631 | static llvm::cl::opt<bool> |
| 632 | ErrorOnExtensions("pedantic-errors", |
| 633 | llvm::cl::desc("Issue an error on uses of GCC extensions")); |
| 634 | |
| 635 | static llvm::cl::opt<bool> |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 636 | SuppressSystemWarnings("suppress-system-warnings", |
Daniel Dunbar | 320a054 | 2008-09-30 20:49:53 +0000 | [diff] [blame] | 637 | llvm::cl::desc("Suppress warnings issued in system headers"), |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 638 | llvm::cl::init(true)); |
| 639 | |
| 640 | static llvm::cl::opt<bool> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | WarnUnusedMacros("Wunused_macros", |
| 642 | llvm::cl::desc("Warn for unused macros in the main translation unit")); |
| 643 | |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 644 | static llvm::cl::opt<bool> |
| 645 | WarnFloatEqual("Wfloat-equal", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 646 | llvm::cl::desc("Warn about equality comparisons of floating point values")); |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 647 | |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 648 | static llvm::cl::opt<bool> |
| 649 | WarnNoFormatNonLiteral("Wno-format-nonliteral", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 650 | llvm::cl::desc("Do not warn about non-literal format strings")); |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 652 | static llvm::cl::opt<bool> |
| 653 | WarnUndefMacros("Wundef", |
| 654 | llvm::cl::desc("Warn on use of undefined macros in #if's")); |
| 655 | |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 656 | static llvm::cl::opt<bool> |
Ted Kremenek | 358256d | 2008-05-30 16:42:02 +0000 | [diff] [blame] | 657 | WarnImplicitFunctionDeclaration("Wimplicit-function-declaration", |
| 658 | llvm::cl::desc("Warn about uses of implicitly defined functions")); |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 659 | |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 660 | static llvm::cl::opt<bool> |
| 661 | WarnNoStrictSelectorMatch("Wno-strict-selector-match", |
| 662 | llvm::cl::desc("Do not warn about duplicate methods that have the same size and alignment"), |
| 663 | llvm::cl::init(true)); |
| 664 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 665 | /// InitializeDiagnostics - Initialize the diagnostic object, based on the |
| 666 | /// current command line option settings. |
| 667 | static void InitializeDiagnostics(Diagnostic &Diags) { |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 668 | Diags.setIgnoreAllWarnings(SilenceWarnings); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 669 | Diags.setWarningsAsErrors(WarningsAsErrors); |
| 670 | Diags.setWarnOnExtensions(WarnOnExtensions); |
| 671 | Diags.setErrorOnExtensions(ErrorOnExtensions); |
| 672 | |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 673 | // Suppress warnings in system headers unless requested not to. |
| 674 | Diags.setSuppressSystemWarnings(SuppressSystemWarnings); |
| 675 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 676 | // Silence the "macro is not used" warning unless requested. |
| 677 | if (!WarnUnusedMacros) |
| 678 | Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE); |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 679 | |
| 680 | // Silence "floating point comparison" warnings unless requested. |
| 681 | if (!WarnFloatEqual) |
| 682 | Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE); |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 683 | |
| 684 | // Silence "format string is not a string literal" warnings if requested |
| 685 | if (WarnNoFormatNonLiteral) |
Ted Kremenek | 7c1d3df | 2007-12-17 17:50:39 +0000 | [diff] [blame] | 686 | Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant, |
| 687 | diag::MAP_IGNORE); |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 688 | if (!WarnUndefMacros) |
| 689 | Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE); |
Steve Naroff | e7a3730 | 2008-02-11 22:40:08 +0000 | [diff] [blame] | 690 | |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 691 | if (!WarnImplicitFunctionDeclaration) |
| 692 | Diags.setDiagnosticMapping(diag::warn_implicit_function_decl, |
| 693 | diag::MAP_IGNORE); |
| 694 | |
Steve Naroff | e7a3730 | 2008-02-11 22:40:08 +0000 | [diff] [blame] | 695 | if (MSExtensions) // MS allows unnamed struct/union fields. |
| 696 | Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE); |
Chris Lattner | 8aedf19 | 2008-05-04 23:52:02 +0000 | [diff] [blame] | 697 | |
| 698 | // If -pedantic-errors is set, turn extensions that warn by default into |
| 699 | // errors. |
| 700 | if (ErrorOnExtensions) { |
| 701 | Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large, |
| 702 | diag::MAP_ERROR); |
| 703 | Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large, |
| 704 | diag::MAP_ERROR); |
| 705 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 709 | // Analysis-specific options. |
| 710 | //===----------------------------------------------------------------------===// |
| 711 | |
| 712 | static llvm::cl::opt<std::string> |
| 713 | AnalyzeSpecificFunction("analyze-function", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 714 | llvm::cl::desc("Run analysis on specific function")); |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 715 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 716 | static llvm::cl::opt<bool> |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 717 | TrimGraph("trim-egraph", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 718 | llvm::cl::desc("Only show error-related paths in the analysis graph")); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 719 | |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 720 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 721 | // Target Triple Processing. |
| 722 | //===----------------------------------------------------------------------===// |
| 723 | |
| 724 | static llvm::cl::opt<std::string> |
| 725 | TargetTriple("triple", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 726 | llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)")); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 727 | |
Chris Lattner | 42e6737 | 2008-03-05 01:18:20 +0000 | [diff] [blame] | 728 | static llvm::cl::opt<std::string> |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 729 | Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)")); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 730 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 731 | static llvm::cl::opt<std::string> |
| 732 | MacOSVersionMin("mmacosx-version-min", |
| 733 | llvm::cl::desc("Specify target Mac OS/X version (e.g. 10.5)")); |
| 734 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 735 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 736 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
| 737 | static void HandleMacOSVersionMin(std::string &Triple) { |
| 738 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 739 | if (DarwinDashIdx == std::string::npos) { |
| 740 | fprintf(stderr, |
| 741 | "-mmacosx-version-min only valid for darwin (Mac OS/X) targets\n"); |
| 742 | exit(1); |
| 743 | } |
| 744 | unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin"); |
| 745 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 746 | // Remove the number. |
| 747 | Triple.resize(DarwinNumIdx); |
| 748 | |
| 749 | // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9] |
| 750 | bool MacOSVersionMinIsInvalid = false; |
| 751 | int VersionNum = 0; |
| 752 | if (MacOSVersionMin.size() < 4 || |
| 753 | MacOSVersionMin.substr(0, 3) != "10." || |
| 754 | !isdigit(MacOSVersionMin[3])) { |
| 755 | MacOSVersionMinIsInvalid = true; |
| 756 | } else { |
| 757 | const char *Start = MacOSVersionMin.c_str()+3; |
| 758 | char *End = 0; |
| 759 | VersionNum = (int)strtol(Start, &End, 10); |
| 760 | |
Chris Lattner | 079f2c46 | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 761 | // The version number must be in the range 0-9. |
| 762 | MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9; |
| 763 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 764 | // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7. |
| 765 | Triple += llvm::itostr(VersionNum+4); |
| 766 | |
Chris Lattner | 079f2c46 | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 767 | if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok. |
| 768 | // Add the period piece (.7) to the end of the triple. This gives us |
| 769 | // something like ...-darwin8.7 |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 770 | Triple += End; |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 771 | } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not. |
| 772 | MacOSVersionMinIsInvalid = true; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | if (MacOSVersionMinIsInvalid) { |
| 777 | fprintf(stderr, |
| 778 | "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n", |
| 779 | MacOSVersionMin.c_str()); |
| 780 | exit(1); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | /// CreateTargetTriple - Process the various options that affect the target |
| 785 | /// triple and build a final aggregate triple that we are compiling for. |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 786 | static std::string CreateTargetTriple() { |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 787 | // Initialize base triple. If a -triple option has been specified, use |
| 788 | // that triple. Otherwise, default to the host triple. |
Chris Lattner | 6590d21 | 2007-12-12 05:01:48 +0000 | [diff] [blame] | 789 | std::string Triple = TargetTriple; |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 790 | if (Triple.empty()) { |
| 791 | Triple = LLVM_HOSTTRIPLE; |
| 792 | |
Daniel Dunbar | a629190 | 2008-12-12 18:34:35 +0000 | [diff] [blame^] | 793 | // Force i<N>86 to i386 when using LLVM_HOSTTRIPLE. |
| 794 | if (Triple[0] == 'i' && isdigit(Triple[1]) && |
| 795 | Triple[2] == '8' && Triple[3] == '6') |
| 796 | Triple[1] = '3'; |
| 797 | |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 798 | // On darwin, we want to update the version to match that of the |
| 799 | // host. |
| 800 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 801 | if (DarwinDashIdx != std::string::npos) { |
| 802 | Triple.resize(DarwinDashIdx + strlen("-darwin")); |
| 803 | |
Daniel Dunbar | a629190 | 2008-12-12 18:34:35 +0000 | [diff] [blame^] | 804 | // Only add the major part of the os version. |
| 805 | std::string Version = llvm::sys::osVersion(); |
| 806 | Triple += Version.substr(0, Version.find('.')); |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 810 | // If -arch foo was specified, remove the architecture from the triple we have |
| 811 | // so far and replace it with the specified one. |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 812 | if (!Arch.empty()) { |
| 813 | // Decompose the base triple into "arch" and suffix. |
| 814 | std::string::size_type FirstDashIdx = Triple.find('-'); |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 816 | if (FirstDashIdx == std::string::npos) { |
| 817 | fprintf(stderr, |
| 818 | "Malformed target triple: \"%s\" ('-' could not be found).\n", |
| 819 | Triple.c_str()); |
| 820 | exit(1); |
| 821 | } |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 822 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 823 | Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end()); |
| 824 | } |
| 825 | |
| 826 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 827 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 828 | if (!MacOSVersionMin.empty()) |
| 829 | HandleMacOSVersionMin(Triple); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 830 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 831 | return Triple; |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 835 | // Preprocessor Initialization |
| 836 | //===----------------------------------------------------------------------===// |
| 837 | |
| 838 | // FIXME: Preprocessor builtins to support. |
| 839 | // -A... - Play with #assertions |
| 840 | // -undef - Undefine all predefined macros |
| 841 | |
| 842 | static llvm::cl::list<std::string> |
| 843 | D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 844 | llvm::cl::desc("Predefine the specified macro")); |
| 845 | static llvm::cl::list<std::string> |
| 846 | U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 847 | llvm::cl::desc("Undefine the specified macro")); |
| 848 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 849 | static llvm::cl::list<std::string> |
| 850 | ImplicitIncludes("include", llvm::cl::value_desc("file"), |
| 851 | llvm::cl::desc("Include file before parsing")); |
| 852 | |
| 853 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 854 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 855 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 856 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 857 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 858 | const char *Command = "#define ") { |
| 859 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 860 | if (const char *Equal = strchr(Macro, '=')) { |
| 861 | // Turn the = into ' '. |
| 862 | Buf.insert(Buf.end(), Macro, Equal); |
| 863 | Buf.push_back(' '); |
| 864 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 865 | } else { |
| 866 | // Push "macroname 1". |
| 867 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 868 | Buf.push_back(' '); |
| 869 | Buf.push_back('1'); |
| 870 | } |
| 871 | Buf.push_back('\n'); |
| 872 | } |
| 873 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 874 | /// AddImplicitInclude - Add an implicit #include of the specified file to the |
| 875 | /// predefines buffer. |
| 876 | static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){ |
| 877 | const char *Inc = "#include \""; |
| 878 | Buf.insert(Buf.end(), Inc, Inc+strlen(Inc)); |
| 879 | Buf.insert(Buf.end(), File.begin(), File.end()); |
| 880 | Buf.push_back('"'); |
| 881 | Buf.push_back('\n'); |
| 882 | } |
| 883 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 884 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 885 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 886 | /// environment ready to process a single file. This returns true on error. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 887 | /// |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 888 | static bool InitializePreprocessor(Preprocessor &PP, |
| 889 | bool InitializeSourceMgr, |
| 890 | const std::string &InFile) { |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 891 | FileManager &FileMgr = PP.getFileManager(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 892 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 893 | // Figure out where to get and map in the main file. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 894 | SourceManager &SourceMgr = PP.getSourceManager(); |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 895 | |
| 896 | if (InitializeSourceMgr) { |
| 897 | if (InFile != "-") { |
| 898 | const FileEntry *File = FileMgr.getFile(InFile); |
| 899 | if (File) SourceMgr.createMainFileID(File, SourceLocation()); |
| 900 | if (SourceMgr.getMainFileID() == 0) { |
| 901 | fprintf(stderr, "Error reading '%s'!\n",InFile.c_str()); |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 902 | return true; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 903 | } |
| 904 | } else { |
| 905 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
| 906 | if (SB) SourceMgr.createMainFileIDForMemBuffer(SB); |
| 907 | if (SourceMgr.getMainFileID() == 0) { |
| 908 | fprintf(stderr, "Error reading standard input! Empty?\n"); |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 909 | return true; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 910 | } |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 911 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 912 | } |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 913 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 914 | std::vector<char> PredefineBuffer; |
| 915 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 916 | // Add macros from the command line. |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 917 | unsigned d = 0, D = D_macros.size(); |
| 918 | unsigned u = 0, U = U_macros.size(); |
| 919 | while (d < D || u < U) { |
| 920 | if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u))) |
| 921 | DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str()); |
| 922 | else |
| 923 | DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef "); |
| 924 | } |
| 925 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 926 | // FIXME: Read any files specified by -imacros. |
| 927 | |
| 928 | // Add implicit #includes from -include. |
| 929 | for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i) |
| 930 | AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 931 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 932 | // Null terminate PredefinedBuffer and add it. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 933 | PredefineBuffer.push_back(0); |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 934 | PP.setPredefines(&PredefineBuffer[0]); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 935 | |
Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 936 | // Use PTH. |
| 937 | if (!TokenCache.empty()) { |
| 938 | PTHManager* PM = PTHManager::Create(TokenCache, PP); |
| 939 | if (PM) PP.setPTHManager(PM); |
| 940 | } |
| 941 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 942 | // Once we've read this, we're done. |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 943 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | //===----------------------------------------------------------------------===// |
| 947 | // Preprocessor include path information. |
| 948 | //===----------------------------------------------------------------------===// |
| 949 | |
| 950 | // This tool exports a large number of command line options to control how the |
| 951 | // preprocessor searches for header files. At root, however, the Preprocessor |
| 952 | // object takes a very simple interface: a list of directories to search for |
| 953 | // |
| 954 | // FIXME: -nostdinc,-nostdinc++ |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 955 | // FIXME: -imultilib |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 956 | // |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 957 | // FIXME: -imacros |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 958 | |
| 959 | static llvm::cl::opt<bool> |
| 960 | nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories")); |
| 961 | |
| 962 | // Various command line options. These four add directories to each chain. |
| 963 | static llvm::cl::list<std::string> |
| 964 | F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 965 | llvm::cl::desc("Add directory to framework include search path")); |
| 966 | static llvm::cl::list<std::string> |
| 967 | I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 968 | llvm::cl::desc("Add directory to include search path")); |
| 969 | static llvm::cl::list<std::string> |
| 970 | idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 971 | llvm::cl::desc("Add directory to AFTER include search path")); |
| 972 | static llvm::cl::list<std::string> |
| 973 | iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 974 | llvm::cl::desc("Add directory to QUOTE include search path")); |
| 975 | static llvm::cl::list<std::string> |
| 976 | isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 977 | llvm::cl::desc("Add directory to SYSTEM include search path")); |
| 978 | |
| 979 | // These handle -iprefix/-iwithprefix/-iwithprefixbefore. |
| 980 | static llvm::cl::list<std::string> |
| 981 | iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix, |
| 982 | llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix")); |
| 983 | static llvm::cl::list<std::string> |
| 984 | iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix, |
| 985 | llvm::cl::desc("Set directory to SYSTEM include search path with prefix")); |
| 986 | static llvm::cl::list<std::string> |
| 987 | iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"), |
| 988 | llvm::cl::Prefix, |
| 989 | llvm::cl::desc("Set directory to include search path with prefix")); |
| 990 | |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 991 | static llvm::cl::opt<std::string> |
| 992 | isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"), |
| 993 | llvm::cl::desc("Set the system root directory (usually /)")); |
| 994 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 995 | // Finally, implement the code that groks the options above. |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 996 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | /// InitializeIncludePaths - Process the -I options and set them in the |
| 998 | /// HeaderSearch object. |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 999 | void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers, |
| 1000 | FileManager &FM, const LangOptions &Lang) { |
| 1001 | InitHeaderSearch Init(Headers, Verbose, isysroot); |
| 1002 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1003 | // Handle -I... and -F... options, walking the lists in parallel. |
| 1004 | unsigned Iidx = 0, Fidx = 0; |
| 1005 | while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) { |
| 1006 | if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1007 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1008 | ++Iidx; |
| 1009 | } else { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1010 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1011 | ++Fidx; |
| 1012 | } |
| 1013 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1014 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1015 | // Consume what's left from whatever list was longer. |
| 1016 | for (; Iidx != I_dirs.size(); ++Iidx) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1017 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1018 | for (; Fidx != F_dirs.size(); ++Fidx) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1019 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1020 | |
| 1021 | // Handle -idirafter... options. |
| 1022 | for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1023 | Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After, |
| 1024 | false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1025 | |
| 1026 | // Handle -iquote... options. |
| 1027 | for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1028 | Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1029 | |
| 1030 | // Handle -isystem... options. |
| 1031 | for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1032 | Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1033 | |
| 1034 | // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in |
| 1035 | // parallel, processing the values in order of occurance to get the right |
| 1036 | // prefixes. |
| 1037 | { |
| 1038 | std::string Prefix = ""; // FIXME: this isn't the correct default prefix. |
| 1039 | unsigned iprefix_idx = 0; |
| 1040 | unsigned iwithprefix_idx = 0; |
| 1041 | unsigned iwithprefixbefore_idx = 0; |
| 1042 | bool iprefix_done = iprefix_vals.empty(); |
| 1043 | bool iwithprefix_done = iwithprefix_vals.empty(); |
| 1044 | bool iwithprefixbefore_done = iwithprefixbefore_vals.empty(); |
| 1045 | while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) { |
| 1046 | if (!iprefix_done && |
| 1047 | (iwithprefix_done || |
| 1048 | iprefix_vals.getPosition(iprefix_idx) < |
| 1049 | iwithprefix_vals.getPosition(iwithprefix_idx)) && |
| 1050 | (iwithprefixbefore_done || |
| 1051 | iprefix_vals.getPosition(iprefix_idx) < |
| 1052 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
| 1053 | Prefix = iprefix_vals[iprefix_idx]; |
| 1054 | ++iprefix_idx; |
| 1055 | iprefix_done = iprefix_idx == iprefix_vals.size(); |
| 1056 | } else if (!iwithprefix_done && |
| 1057 | (iwithprefixbefore_done || |
| 1058 | iwithprefix_vals.getPosition(iwithprefix_idx) < |
| 1059 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1060 | Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx], |
| 1061 | InitHeaderSearch::System, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1062 | ++iwithprefix_idx; |
| 1063 | iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size(); |
| 1064 | } else { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1065 | Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx], |
| 1066 | InitHeaderSearch::Angled, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1067 | ++iwithprefixbefore_idx; |
| 1068 | iwithprefixbefore_done = |
| 1069 | iwithprefixbefore_idx == iwithprefixbefore_vals.size(); |
| 1070 | } |
| 1071 | } |
| 1072 | } |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1073 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1074 | Init.AddDefaultEnvVarPaths(Lang); |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1075 | |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1076 | // Add the clang headers, which are relative to the clang driver. |
| 1077 | llvm::sys::Path MainExecutablePath = |
Chris Lattner | 985e182 | 2008-03-03 05:57:43 +0000 | [diff] [blame] | 1078 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 1079 | (void*)(intptr_t)InitializeIncludePaths); |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1080 | if (!MainExecutablePath.isEmpty()) { |
| 1081 | MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang |
| 1082 | MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin |
| 1083 | MainExecutablePath.appendComponent("Headers"); // Get foo/Headers |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1084 | Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System, |
| 1085 | false, false, false); |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1088 | if (!nostdinc) |
| 1089 | Init.AddDefaultSystemIncludePaths(Lang); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1090 | |
| 1091 | // Now that we have collected all of the include paths, merge them all |
| 1092 | // together and tell the preprocessor about them. |
| 1093 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1094 | Init.Realize(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1097 | //===----------------------------------------------------------------------===// |
| 1098 | // Driver PreprocessorFactory - For lazily generating preprocessors ... |
| 1099 | //===----------------------------------------------------------------------===// |
| 1100 | |
| 1101 | namespace { |
| 1102 | class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1103 | const std::string &InFile; |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1104 | Diagnostic &Diags; |
| 1105 | const LangOptions &LangInfo; |
| 1106 | TargetInfo &Target; |
| 1107 | SourceManager &SourceMgr; |
| 1108 | HeaderSearch &HeaderInfo; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1109 | bool InitializeSourceMgr; |
| 1110 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1111 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1112 | DriverPreprocessorFactory(const std::string &infile, |
| 1113 | Diagnostic &diags, const LangOptions &opts, |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1114 | TargetInfo &target, SourceManager &SM, |
| 1115 | HeaderSearch &Headers) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1116 | : InFile(infile), Diags(diags), LangInfo(opts), Target(target), |
| 1117 | SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {} |
| 1118 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1119 | |
| 1120 | virtual ~DriverPreprocessorFactory() {} |
| 1121 | |
| 1122 | virtual Preprocessor* CreatePreprocessor() { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1123 | Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target, |
| 1124 | SourceMgr, HeaderInfo); |
| 1125 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1126 | if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1127 | delete PP; |
| 1128 | return NULL; |
| 1129 | } |
| 1130 | |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1131 | /// FIXME: PP can only handle one callback |
| 1132 | if (ProgAction != PrintPreprocessedInput) { |
| 1133 | const char* ErrStr; |
| 1134 | bool DFG = CreateDependencyFileGen(PP, OutputFile, InFile, ErrStr); |
| 1135 | if (!DFG && ErrStr) { |
Ted Kremenek | 9a30c24 | 2008-10-25 20:19:34 +0000 | [diff] [blame] | 1136 | fprintf(stderr, "%s", ErrStr); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1137 | return NULL; |
| 1138 | } |
| 1139 | } |
| 1140 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1141 | InitializeSourceMgr = false; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1142 | return PP; |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1143 | } |
| 1144 | }; |
| 1145 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1146 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1147 | //===----------------------------------------------------------------------===// |
| 1148 | // Basic Parser driver |
| 1149 | //===----------------------------------------------------------------------===// |
| 1150 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1151 | static void ParseFile(Preprocessor &PP, MinimalAction *PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1152 | Parser P(PP, *PA); |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1153 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1154 | |
| 1155 | // Parsing the specified input file. |
| 1156 | P.ParseTranslationUnit(); |
| 1157 | delete PA; |
| 1158 | } |
| 1159 | |
| 1160 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1161 | // Code generation options |
| 1162 | //===----------------------------------------------------------------------===// |
| 1163 | |
| 1164 | static llvm::cl::opt<bool> |
| 1165 | OptSize("Os", |
| 1166 | llvm::cl::desc("Optimize for size")); |
| 1167 | |
| 1168 | // It might be nice to add bounds to the CommandLine library directly. |
| 1169 | struct OptLevelParser : public llvm::cl::parser<unsigned> { |
| 1170 | bool parse(llvm::cl::Option &O, const char *ArgName, |
| 1171 | const std::string &Arg, unsigned &Val) { |
| 1172 | if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val)) |
| 1173 | return true; |
| 1174 | // FIXME: Support -O4. |
| 1175 | if (Val > 3) |
| 1176 | return O.error(": '" + Arg + "' invalid optimization level!"); |
| 1177 | return false; |
| 1178 | } |
| 1179 | }; |
| 1180 | static llvm::cl::opt<unsigned, false, OptLevelParser> |
| 1181 | OptLevel("O", llvm::cl::Prefix, |
| 1182 | llvm::cl::desc("Optimization level"), |
| 1183 | llvm::cl::init(0)); |
| 1184 | |
| 1185 | static void InitializeCompileOptions(CompileOptions &Opts) { |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1186 | Opts.OptimizeSize = OptSize; |
Daniel Dunbar | ac7ffe0 | 2008-10-29 07:56:11 +0000 | [diff] [blame] | 1187 | if (OptSize) { |
| 1188 | // -Os implies -O2 |
| 1189 | // FIXME: Diagnose conflicting options. |
| 1190 | Opts.OptimizationLevel = 2; |
| 1191 | } else { |
| 1192 | Opts.OptimizationLevel = OptLevel; |
| 1193 | } |
Daniel Dunbar | 8e8f3b7 | 2008-10-29 03:42:18 +0000 | [diff] [blame] | 1194 | |
| 1195 | // FIXME: There are llvm-gcc options to control these selectively. |
| 1196 | Opts.InlineFunctions = (Opts.OptimizationLevel > 1); |
| 1197 | Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize); |
| 1198 | Opts.SimplifyLibCalls = 1; |
Daniel Dunbar | dd913e5 | 2008-10-31 09:34:21 +0000 | [diff] [blame] | 1199 | |
| 1200 | #ifdef NDEBUG |
| 1201 | Opts.VerifyModule = 0; |
| 1202 | #endif |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1206 | // Main driver |
| 1207 | //===----------------------------------------------------------------------===// |
| 1208 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1209 | /// CreateASTConsumer - Create the ASTConsumer for the corresponding program |
| 1210 | /// action. These consumers can operate on both ASTs that are freshly |
| 1211 | /// parsed from source files as well as those deserialized from Bitcode. |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1212 | static ASTConsumer* CreateASTConsumer(const std::string& InFile, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1213 | Diagnostic& Diag, FileManager& FileMgr, |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1214 | const LangOptions& LangOpts, |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 1215 | Preprocessor *PP, |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1216 | PreprocessorFactory *PPF) { |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1217 | switch (ProgAction) { |
| 1218 | default: |
| 1219 | return NULL; |
| 1220 | |
| 1221 | case ASTPrint: |
| 1222 | return CreateASTPrinter(); |
| 1223 | |
| 1224 | case ASTDump: |
| 1225 | return CreateASTDumper(); |
| 1226 | |
| 1227 | case ASTView: |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 1228 | return CreateASTViewer(); |
| 1229 | |
| 1230 | case EmitHTML: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1231 | return CreateHTMLPrinter(OutputFile, Diag, PP, PPF); |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 1232 | |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1233 | case InheritanceView: |
| 1234 | return CreateInheritanceViewer(InheritanceViewCls); |
| 1235 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1236 | case TestSerialization: |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1237 | return CreateSerializationTest(Diag, FileMgr); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1238 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 1239 | case EmitAssembly: |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1240 | case EmitLLVM: |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1241 | case EmitBC: { |
| 1242 | BackendAction Act; |
| 1243 | if (ProgAction == EmitAssembly) { |
| 1244 | Act = Backend_EmitAssembly; |
| 1245 | } else if (ProgAction == EmitLLVM) { |
| 1246 | Act = Backend_EmitLL; |
| 1247 | } else { |
| 1248 | Act = Backend_EmitBC; |
| 1249 | } |
| 1250 | CompileOptions Opts; |
| 1251 | InitializeCompileOptions(Opts); |
| 1252 | return CreateBackendConsumer(Act, Diag, LangOpts, Opts, |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 1253 | InFile, OutputFile, GenerateDebugInfo); |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1254 | } |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 1255 | |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 1256 | case SerializeAST: |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1257 | // FIXME: Allow user to tailor where the file is written. |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1258 | return CreateASTSerializer(InFile, OutputFile, Diag); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1259 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1260 | case RewriteObjC: |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 1261 | return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts); |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 1262 | |
| 1263 | case RewriteBlocks: |
| 1264 | return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1265 | |
| 1266 | case RunAnalysis: |
| 1267 | assert (!AnalysisList.empty()); |
| 1268 | return CreateAnalysisConsumer(&AnalysisList[0], |
| 1269 | &AnalysisList[0]+AnalysisList.size(), |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 1270 | AnalysisStoreOpt, AnalysisDiagOpt, |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1271 | Diag, PP, PPF, LangOpts, |
| 1272 | AnalyzeSpecificFunction, |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 1273 | OutputFile, VisualizeEGDot, VisualizeEGUbi, |
| 1274 | TrimGraph, AnalyzeAll); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1278 | /// ProcessInputFile - Process a single input file with the specified state. |
| 1279 | /// |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1280 | static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1281 | const std::string &InFile, ProgActions PA) { |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1282 | llvm::OwningPtr<ASTConsumer> Consumer; |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1283 | bool ClearSourceMgr = false; |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1284 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1285 | switch (PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1286 | default: |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1287 | Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(), |
| 1288 | PP.getFileManager(), PP.getLangOptions(), |
| 1289 | &PP, &PPF)); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1290 | |
| 1291 | if (!Consumer) { |
| 1292 | fprintf(stderr, "Unexpected program action!\n"); |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1293 | HadErrors = true; |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1294 | return; |
| 1295 | } |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1296 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1297 | break; |
| 1298 | |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1299 | case DumpRawTokens: { |
| 1300 | SourceManager &SM = PP.getSourceManager(); |
| 1301 | std::pair<const char*,const char*> File = |
| 1302 | SM.getBufferData(SM.getMainFileID()); |
| 1303 | // Start lexing the specified input file. |
| 1304 | Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0), |
| 1305 | PP.getLangOptions(), File.first, File.second); |
| 1306 | RawLex.SetKeepWhitespaceMode(true); |
| 1307 | |
| 1308 | Token RawTok; |
| 1309 | |
| 1310 | RawLex.LexFromRawLexer(RawTok); |
| 1311 | while (RawTok.isNot(tok::eof)) { |
| 1312 | PP.DumpToken(RawTok, true); |
| 1313 | fprintf(stderr, "\n"); |
| 1314 | RawLex.LexFromRawLexer(RawTok); |
| 1315 | } |
| 1316 | ClearSourceMgr = true; |
| 1317 | break; |
| 1318 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1319 | case DumpTokens: { // Token dump mode. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1320 | Token Tok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1321 | // Start preprocessing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1322 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1323 | do { |
| 1324 | PP.Lex(Tok); |
| 1325 | PP.DumpToken(Tok, true); |
| 1326 | fprintf(stderr, "\n"); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1327 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1328 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | } |
| 1331 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1332 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1333 | // Start parsing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1334 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1335 | do { |
| 1336 | PP.Lex(Tok); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1337 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1338 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1339 | break; |
| 1340 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1341 | |
| 1342 | case GeneratePCH: { |
| 1343 | CacheTokens(PP, OutputFile); |
| 1344 | ClearSourceMgr = true; |
| 1345 | break; |
| 1346 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1347 | |
| 1348 | case PrintPreprocessedInput: // -E mode. |
Chris Lattner | e988bc2 | 2008-01-27 23:55:11 +0000 | [diff] [blame] | 1349 | DoPrintPreprocessedInput(PP, OutputFile); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1350 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1351 | break; |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1352 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1353 | case ParseNoop: // -parse-noop |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1354 | ParseFile(PP, new MinimalAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1355 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1356 | break; |
| 1357 | |
| 1358 | case ParsePrintCallbacks: |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1359 | ParseFile(PP, CreatePrintParserActionsAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1360 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1361 | break; |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 1362 | |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1363 | case ParseSyntaxOnly: // -fsyntax-only |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1364 | Consumer.reset(new ASTConsumer()); |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 1365 | break; |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1366 | |
| 1367 | case RewriteMacros: |
Chris Lattner | 0951052 | 2008-05-09 22:43:24 +0000 | [diff] [blame] | 1368 | RewriteMacrosInInput(PP, InFile, OutputFile); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1369 | ClearSourceMgr = true; |
| 1370 | break; |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 1371 | |
| 1372 | case RewriteTest: |
| 1373 | DoRewriteTest(PP, InFile, OutputFile); |
| 1374 | ClearSourceMgr = true; |
| 1375 | break; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 1376 | } |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1377 | |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 1378 | if (Consumer) |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 1379 | ParseAST(PP, Consumer.get(), Stats, !DisableFree); |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (VerifyDiagnostics) |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1382 | if (CheckDiagnostics(PP)) |
| 1383 | exit(1); |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1384 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | if (Stats) { |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1386 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1387 | PP.PrintStats(); |
| 1388 | PP.getIdentifierTable().PrintStats(); |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1389 | PP.getHeaderSearchInfo().PrintStats(); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1390 | if (ClearSourceMgr) |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1391 | PP.getSourceManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1392 | fprintf(stderr, "\n"); |
| 1393 | } |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1394 | |
| 1395 | // For a multi-file compilation, some things are ok with nuking the source |
| 1396 | // manager tables, other require stable fileid/macroid's across multiple |
| 1397 | // files. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1398 | if (ClearSourceMgr) |
| 1399 | PP.getSourceManager().clearIDTables(); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 1400 | |
| 1401 | if (DisableFree) |
| 1402 | Consumer.take(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1405 | static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag, |
| 1406 | FileManager& FileMgr) { |
| 1407 | |
| 1408 | if (VerifyDiagnostics) { |
| 1409 | fprintf(stderr, "-verify does not yet work with serialized ASTs.\n"); |
| 1410 | exit (1); |
| 1411 | } |
| 1412 | |
| 1413 | llvm::sys::Path Filename(InFile); |
| 1414 | |
| 1415 | if (!Filename.isValid()) { |
| 1416 | fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str()); |
| 1417 | exit (1); |
| 1418 | } |
| 1419 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1420 | llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr)); |
Ted Kremenek | fe4e015 | 2007-12-13 18:11:11 +0000 | [diff] [blame] | 1421 | |
| 1422 | if (!TU) { |
| 1423 | fprintf(stderr, "error: file '%s' could not be deserialized\n", |
| 1424 | InFile.c_str()); |
| 1425 | exit (1); |
| 1426 | } |
| 1427 | |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 1428 | // Observe that we use the source file name stored in the deserialized |
| 1429 | // translation unit, rather than InFile. |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 1430 | llvm::OwningPtr<ASTConsumer> |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1431 | Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(), |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1432 | 0, 0)); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1433 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1434 | if (!Consumer) { |
| 1435 | fprintf(stderr, "Unsupported program action with serialized ASTs!\n"); |
| 1436 | exit (1); |
| 1437 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1438 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1439 | Consumer->Initialize(TU->getContext()); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1440 | |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1441 | // FIXME: We need to inform Consumer about completed TagDecls as well. |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1442 | for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I) |
| 1443 | Consumer->HandleTopLevelDecl(*I); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1447 | static llvm::cl::list<std::string> |
| 1448 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 1449 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1450 | static bool isSerializedFile(const std::string& InFile) { |
| 1451 | if (InFile.size() < 4) |
| 1452 | return false; |
| 1453 | |
| 1454 | const char* s = InFile.c_str()+InFile.size()-4; |
| 1455 | |
| 1456 | return s[0] == '.' && |
| 1457 | s[1] == 'a' && |
| 1458 | s[2] == 's' && |
| 1459 | s[3] == 't'; |
| 1460 | } |
| 1461 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1462 | |
| 1463 | int main(int argc, char **argv) { |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1464 | llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1465 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 1466 | |
| 1467 | // If no input was specified, read from stdin. |
| 1468 | if (InputFilenames.empty()) |
| 1469 | InputFilenames.push_back("-"); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1470 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1471 | // Create a file manager object to provide access to and cache the filesystem. |
| 1472 | FileManager FileMgr; |
| 1473 | |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1474 | // Create the diagnostic client for reporting errors or for |
| 1475 | // implementing -verify. |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1476 | DiagnosticClient* TextDiagClient = 0; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 1477 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1478 | if (!VerifyDiagnostics) { |
| 1479 | // Print diagnostics to stderr by default. |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 1480 | TextDiagClient = new TextDiagnosticPrinter(llvm::errs(), |
| 1481 | !NoShowColumn, |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1482 | !NoCaretDiagnostics); |
| 1483 | } else { |
| 1484 | // When checking diagnostics, just buffer them up. |
| 1485 | TextDiagClient = new TextDiagnosticBuffer(); |
| 1486 | |
| 1487 | if (InputFilenames.size() != 1) { |
| 1488 | fprintf(stderr, |
| 1489 | "-verify only works on single input files for now.\n"); |
| 1490 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1493 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1494 | // Configure our handling of diagnostics. |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1495 | llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient); |
| 1496 | Diagnostic Diags(DiagClient.get()); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1497 | InitializeDiagnostics(Diags); |
| 1498 | |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1499 | // -I- is a deprecated GCC feature, scan for it and reject it. |
| 1500 | for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) { |
| 1501 | if (I_dirs[i] == "-") { |
Chris Lattner | 5917fe1 | 2008-11-18 05:05:28 +0000 | [diff] [blame] | 1502 | Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported); |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1503 | I_dirs.erase(I_dirs.begin()+i); |
| 1504 | --i; |
| 1505 | } |
| 1506 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1507 | |
| 1508 | // Get information about the target being compiled for. |
| 1509 | std::string Triple = CreateTargetTriple(); |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1510 | llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple)); |
| 1511 | |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1512 | if (Target == 0) { |
| 1513 | fprintf(stderr, "Sorry, I don't know what target this is: %s\n", |
| 1514 | Triple.c_str()); |
| 1515 | fprintf(stderr, "Please use -triple or -arch.\n"); |
| 1516 | exit(1); |
| 1517 | } |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1518 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1519 | // Are we invoking one or more source analyses? |
| 1520 | if (!AnalysisList.empty() && ProgAction == ParseSyntaxOnly) |
| 1521 | ProgAction = RunAnalysis; |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1522 | else if (!InheritanceViewCls.empty()) // C++ visualization? |
| 1523 | ProgAction = InheritanceView; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1524 | |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1525 | llvm::OwningPtr<SourceManager> SourceMgr; |
| 1526 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1527 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1528 | const std::string &InFile = InputFilenames[i]; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1529 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1530 | if (isSerializedFile(InFile)) { |
| 1531 | Diags.setClient(TextDiagClient); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1532 | ProcessSerializedFile(InFile,Diags,FileMgr); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1533 | } |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1534 | else { |
| 1535 | /// Create a SourceManager object. This tracks and owns all the file |
| 1536 | /// buffers allocated to a translation unit. |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1537 | if (!SourceMgr) |
| 1538 | SourceMgr.reset(new SourceManager()); |
| 1539 | else |
| 1540 | SourceMgr->clearIDTables(); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1541 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1542 | // Initialize language options, inferring file types from input filenames. |
| 1543 | LangOptions LangInfo; |
| 1544 | InitializeBaseLanguage(); |
| 1545 | LangKind LK = GetLanguage(InFile); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1546 | bool PCH = InitializeLangOptions(LangInfo, LK); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 1547 | InitializeGCMode(LangInfo); |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 1548 | InitializeLanguageStandard(LangInfo, LK, Target.get()); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1549 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1550 | // Process the -I options and set them in the HeaderInfo. |
| 1551 | HeaderSearch HeaderInfo(FileMgr); |
Ted Kremenek | c68ecb5 | 2008-06-06 01:47:30 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1553 | InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1554 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1555 | // Set up the preprocessor with these options. |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1556 | DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target, |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1557 | *SourceMgr.get(), HeaderInfo); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1558 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1559 | llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor()); |
| 1560 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1561 | if (!PP) |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1562 | continue; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1563 | |
| 1564 | // Create the HTMLDiagnosticsClient if we are using one. Otherwise, |
| 1565 | // always reset to using TextDiagClient. |
| 1566 | llvm::OwningPtr<DiagnosticClient> TmpClient; |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1567 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1568 | if (!HTMLDiag.empty()) { |
| 1569 | TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(), |
| 1570 | &PPFactory)); |
| 1571 | Diags.setClient(TmpClient.get()); |
| 1572 | } |
| 1573 | else |
| 1574 | Diags.setClient(TextDiagClient); |
| 1575 | |
| 1576 | // Process the source file. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1577 | ProcessInputFile(*PP, PPFactory, InFile, PCH ? GeneratePCH : ProgAction); |
| 1578 | |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1579 | HeaderInfo.ClearFileInfo(); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1580 | |
| 1581 | if (Stats) |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1582 | SourceMgr->PrintStats(); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1583 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1584 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1585 | |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1586 | if (unsigned NumDiagnostics = Diags.getNumDiagnostics()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1587 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 1588 | (NumDiagnostics == 1 ? "" : "s")); |
| 1589 | |
| 1590 | if (Stats) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1591 | FileMgr.PrintStats(); |
| 1592 | fprintf(stderr, "\n"); |
| 1593 | } |
| 1594 | |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1595 | // If verifying diagnostics and we reached here, all is well. |
| 1596 | if (VerifyDiagnostics) |
| 1597 | return 0; |
| 1598 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 1599 | // Managed static deconstruction. Useful for making things like |
| 1600 | // -time-passes usable. |
| 1601 | llvm::llvm_shutdown(); |
| 1602 | |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1603 | return HadErrors || (Diags.getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1604 | } |