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. |
| 572 | Options.Trigraphs = LangStd < lang_gnu_START || Trigraphs ? 1 : 0; |
| 573 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 574 | Options.DollarIdents = 1; // FIXME: Really a target property. |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame^] | 575 | if (PascalStrings.getPosition()) |
| 576 | Options.PascalStrings = PascalStrings; |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 577 | Options.Microsoft = MSExtensions; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 578 | Options.WritableStrings = WritableStrings; |
Anders Carlsson | 695dbb6 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 579 | Options.LaxVectorConversions = LaxVectorConversions; |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 580 | Options.Exceptions = Exceptions; |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame^] | 581 | if (EnableBlocks.getPosition()) |
| 582 | Options.Blocks = EnableBlocks; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 584 | // Override the default runtime if the user requested it. |
| 585 | if (NeXTRuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 586 | Options.NeXTRuntime = 1; |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 587 | else if (GNURuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 588 | Options.NeXTRuntime = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 591 | static llvm::cl::opt<bool> |
| 592 | ObjCExclusiveGC("fobjc-gc-only", |
| 593 | llvm::cl::desc("Use GC exclusively for Objective-C related " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 594 | "memory management")); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 595 | |
| 596 | static llvm::cl::opt<bool> |
| 597 | ObjCEnableGC("fobjc-gc", |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 598 | llvm::cl::desc("Enable Objective-C garbage collection")); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 599 | |
| 600 | void InitializeGCMode(LangOptions &Options) { |
| 601 | if (ObjCExclusiveGC) |
| 602 | Options.setGCMode(LangOptions::GCOnly); |
| 603 | else if (ObjCEnableGC) |
| 604 | Options.setGCMode(LangOptions::HybridGC); |
| 605 | } |
| 606 | |
| 607 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 608 | //===----------------------------------------------------------------------===// |
| 609 | // Our DiagnosticClient implementation |
| 610 | //===----------------------------------------------------------------------===// |
| 611 | |
| 612 | // FIXME: Werror should take a list of things, -Werror=foo,bar |
| 613 | static llvm::cl::opt<bool> |
| 614 | WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors")); |
| 615 | |
| 616 | static llvm::cl::opt<bool> |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 617 | SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings")); |
| 618 | |
| 619 | static llvm::cl::opt<bool> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 620 | WarnOnExtensions("pedantic", llvm::cl::init(false), |
| 621 | llvm::cl::desc("Issue a warning on uses of GCC extensions")); |
| 622 | |
| 623 | static llvm::cl::opt<bool> |
| 624 | ErrorOnExtensions("pedantic-errors", |
| 625 | llvm::cl::desc("Issue an error on uses of GCC extensions")); |
| 626 | |
| 627 | static llvm::cl::opt<bool> |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 628 | SuppressSystemWarnings("suppress-system-warnings", |
Daniel Dunbar | 320a054 | 2008-09-30 20:49:53 +0000 | [diff] [blame] | 629 | llvm::cl::desc("Suppress warnings issued in system headers"), |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 630 | llvm::cl::init(true)); |
| 631 | |
| 632 | static llvm::cl::opt<bool> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 633 | WarnUnusedMacros("Wunused_macros", |
| 634 | llvm::cl::desc("Warn for unused macros in the main translation unit")); |
| 635 | |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 636 | static llvm::cl::opt<bool> |
| 637 | WarnFloatEqual("Wfloat-equal", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 638 | llvm::cl::desc("Warn about equality comparisons of floating point values")); |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 640 | static llvm::cl::opt<bool> |
| 641 | WarnNoFormatNonLiteral("Wno-format-nonliteral", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 642 | llvm::cl::desc("Do not warn about non-literal format strings")); |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 644 | static llvm::cl::opt<bool> |
| 645 | WarnUndefMacros("Wundef", |
| 646 | llvm::cl::desc("Warn on use of undefined macros in #if's")); |
| 647 | |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 648 | static llvm::cl::opt<bool> |
Ted Kremenek | 358256d | 2008-05-30 16:42:02 +0000 | [diff] [blame] | 649 | WarnImplicitFunctionDeclaration("Wimplicit-function-declaration", |
| 650 | llvm::cl::desc("Warn about uses of implicitly defined functions")); |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 651 | |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 652 | static llvm::cl::opt<bool> |
| 653 | WarnNoStrictSelectorMatch("Wno-strict-selector-match", |
| 654 | llvm::cl::desc("Do not warn about duplicate methods that have the same size and alignment"), |
| 655 | llvm::cl::init(true)); |
| 656 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | /// InitializeDiagnostics - Initialize the diagnostic object, based on the |
| 658 | /// current command line option settings. |
| 659 | static void InitializeDiagnostics(Diagnostic &Diags) { |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 660 | Diags.setIgnoreAllWarnings(SilenceWarnings); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 661 | Diags.setWarningsAsErrors(WarningsAsErrors); |
| 662 | Diags.setWarnOnExtensions(WarnOnExtensions); |
| 663 | Diags.setErrorOnExtensions(ErrorOnExtensions); |
| 664 | |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 665 | // Suppress warnings in system headers unless requested not to. |
| 666 | Diags.setSuppressSystemWarnings(SuppressSystemWarnings); |
| 667 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | // Silence the "macro is not used" warning unless requested. |
| 669 | if (!WarnUnusedMacros) |
| 670 | Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE); |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 671 | |
| 672 | // Silence "floating point comparison" warnings unless requested. |
| 673 | if (!WarnFloatEqual) |
| 674 | Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE); |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 675 | |
| 676 | // Silence "format string is not a string literal" warnings if requested |
| 677 | if (WarnNoFormatNonLiteral) |
Ted Kremenek | 7c1d3df | 2007-12-17 17:50:39 +0000 | [diff] [blame] | 678 | Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant, |
| 679 | diag::MAP_IGNORE); |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 680 | if (!WarnUndefMacros) |
| 681 | Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE); |
Steve Naroff | e7a3730 | 2008-02-11 22:40:08 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 683 | if (!WarnImplicitFunctionDeclaration) |
| 684 | Diags.setDiagnosticMapping(diag::warn_implicit_function_decl, |
| 685 | diag::MAP_IGNORE); |
| 686 | |
Steve Naroff | e7a3730 | 2008-02-11 22:40:08 +0000 | [diff] [blame] | 687 | if (MSExtensions) // MS allows unnamed struct/union fields. |
| 688 | Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE); |
Chris Lattner | 8aedf19 | 2008-05-04 23:52:02 +0000 | [diff] [blame] | 689 | |
| 690 | // If -pedantic-errors is set, turn extensions that warn by default into |
| 691 | // errors. |
| 692 | if (ErrorOnExtensions) { |
| 693 | Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large, |
| 694 | diag::MAP_ERROR); |
| 695 | Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large, |
| 696 | diag::MAP_ERROR); |
| 697 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 701 | // Analysis-specific options. |
| 702 | //===----------------------------------------------------------------------===// |
| 703 | |
| 704 | static llvm::cl::opt<std::string> |
| 705 | AnalyzeSpecificFunction("analyze-function", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 706 | llvm::cl::desc("Run analysis on specific function")); |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 707 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 708 | static llvm::cl::opt<bool> |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 709 | TrimGraph("trim-egraph", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 710 | llvm::cl::desc("Only show error-related paths in the analysis graph")); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 711 | |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 712 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 713 | // Target Triple Processing. |
| 714 | //===----------------------------------------------------------------------===// |
| 715 | |
| 716 | static llvm::cl::opt<std::string> |
| 717 | TargetTriple("triple", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 718 | llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)")); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 719 | |
Chris Lattner | 42e6737 | 2008-03-05 01:18:20 +0000 | [diff] [blame] | 720 | static llvm::cl::opt<std::string> |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 721 | Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)")); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 722 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 723 | static llvm::cl::opt<std::string> |
| 724 | MacOSVersionMin("mmacosx-version-min", |
| 725 | llvm::cl::desc("Specify target Mac OS/X version (e.g. 10.5)")); |
| 726 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 727 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 728 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
| 729 | static void HandleMacOSVersionMin(std::string &Triple) { |
| 730 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 731 | if (DarwinDashIdx == std::string::npos) { |
| 732 | fprintf(stderr, |
| 733 | "-mmacosx-version-min only valid for darwin (Mac OS/X) targets\n"); |
| 734 | exit(1); |
| 735 | } |
| 736 | unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin"); |
| 737 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 738 | // Remove the number. |
| 739 | Triple.resize(DarwinNumIdx); |
| 740 | |
| 741 | // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9] |
| 742 | bool MacOSVersionMinIsInvalid = false; |
| 743 | int VersionNum = 0; |
| 744 | if (MacOSVersionMin.size() < 4 || |
| 745 | MacOSVersionMin.substr(0, 3) != "10." || |
| 746 | !isdigit(MacOSVersionMin[3])) { |
| 747 | MacOSVersionMinIsInvalid = true; |
| 748 | } else { |
| 749 | const char *Start = MacOSVersionMin.c_str()+3; |
| 750 | char *End = 0; |
| 751 | VersionNum = (int)strtol(Start, &End, 10); |
| 752 | |
Chris Lattner | 079f2c46 | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 753 | // The version number must be in the range 0-9. |
| 754 | MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9; |
| 755 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 756 | // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7. |
| 757 | Triple += llvm::itostr(VersionNum+4); |
| 758 | |
Chris Lattner | 079f2c46 | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 759 | if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok. |
| 760 | // Add the period piece (.7) to the end of the triple. This gives us |
| 761 | // something like ...-darwin8.7 |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 762 | Triple += End; |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 763 | } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not. |
| 764 | MacOSVersionMinIsInvalid = true; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | if (MacOSVersionMinIsInvalid) { |
| 769 | fprintf(stderr, |
| 770 | "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n", |
| 771 | MacOSVersionMin.c_str()); |
| 772 | exit(1); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | /// CreateTargetTriple - Process the various options that affect the target |
| 777 | /// triple and build a final aggregate triple that we are compiling for. |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 778 | static std::string CreateTargetTriple() { |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 779 | // Initialize base triple. If a -triple option has been specified, use |
| 780 | // that triple. Otherwise, default to the host triple. |
Chris Lattner | 6590d21 | 2007-12-12 05:01:48 +0000 | [diff] [blame] | 781 | std::string Triple = TargetTriple; |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 782 | if (Triple.empty()) { |
| 783 | Triple = LLVM_HOSTTRIPLE; |
| 784 | |
| 785 | // On darwin, we want to update the version to match that of the |
| 786 | // host. |
| 787 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 788 | if (DarwinDashIdx != std::string::npos) { |
| 789 | Triple.resize(DarwinDashIdx + strlen("-darwin")); |
| 790 | |
| 791 | Triple += llvm::sys::osVersion(); |
| 792 | } |
| 793 | } |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 794 | |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 795 | // If -arch foo was specified, remove the architecture from the triple we have |
| 796 | // so far and replace it with the specified one. |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 797 | if (!Arch.empty()) { |
| 798 | // Decompose the base triple into "arch" and suffix. |
| 799 | std::string::size_type FirstDashIdx = Triple.find('-'); |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 800 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 801 | if (FirstDashIdx == std::string::npos) { |
| 802 | fprintf(stderr, |
| 803 | "Malformed target triple: \"%s\" ('-' could not be found).\n", |
| 804 | Triple.c_str()); |
| 805 | exit(1); |
| 806 | } |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 807 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 808 | Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end()); |
| 809 | } |
| 810 | |
| 811 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 812 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 813 | if (!MacOSVersionMin.empty()) |
| 814 | HandleMacOSVersionMin(Triple); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 816 | return Triple; |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 820 | // Preprocessor Initialization |
| 821 | //===----------------------------------------------------------------------===// |
| 822 | |
| 823 | // FIXME: Preprocessor builtins to support. |
| 824 | // -A... - Play with #assertions |
| 825 | // -undef - Undefine all predefined macros |
| 826 | |
| 827 | static llvm::cl::list<std::string> |
| 828 | D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 829 | llvm::cl::desc("Predefine the specified macro")); |
| 830 | static llvm::cl::list<std::string> |
| 831 | U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 832 | llvm::cl::desc("Undefine the specified macro")); |
| 833 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 834 | static llvm::cl::list<std::string> |
| 835 | ImplicitIncludes("include", llvm::cl::value_desc("file"), |
| 836 | llvm::cl::desc("Include file before parsing")); |
| 837 | |
| 838 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 839 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 840 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 841 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 842 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 843 | const char *Command = "#define ") { |
| 844 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 845 | if (const char *Equal = strchr(Macro, '=')) { |
| 846 | // Turn the = into ' '. |
| 847 | Buf.insert(Buf.end(), Macro, Equal); |
| 848 | Buf.push_back(' '); |
| 849 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 850 | } else { |
| 851 | // Push "macroname 1". |
| 852 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 853 | Buf.push_back(' '); |
| 854 | Buf.push_back('1'); |
| 855 | } |
| 856 | Buf.push_back('\n'); |
| 857 | } |
| 858 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 859 | /// AddImplicitInclude - Add an implicit #include of the specified file to the |
| 860 | /// predefines buffer. |
| 861 | static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){ |
| 862 | const char *Inc = "#include \""; |
| 863 | Buf.insert(Buf.end(), Inc, Inc+strlen(Inc)); |
| 864 | Buf.insert(Buf.end(), File.begin(), File.end()); |
| 865 | Buf.push_back('"'); |
| 866 | Buf.push_back('\n'); |
| 867 | } |
| 868 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 870 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 871 | /// environment ready to process a single file. This returns true on error. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 872 | /// |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 873 | static bool InitializePreprocessor(Preprocessor &PP, |
| 874 | bool InitializeSourceMgr, |
| 875 | const std::string &InFile) { |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 876 | FileManager &FileMgr = PP.getFileManager(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 877 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 878 | // Figure out where to get and map in the main file. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 879 | SourceManager &SourceMgr = PP.getSourceManager(); |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 880 | |
| 881 | if (InitializeSourceMgr) { |
| 882 | if (InFile != "-") { |
| 883 | const FileEntry *File = FileMgr.getFile(InFile); |
| 884 | if (File) SourceMgr.createMainFileID(File, SourceLocation()); |
| 885 | if (SourceMgr.getMainFileID() == 0) { |
| 886 | fprintf(stderr, "Error reading '%s'!\n",InFile.c_str()); |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 887 | return true; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 888 | } |
| 889 | } else { |
| 890 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
| 891 | if (SB) SourceMgr.createMainFileIDForMemBuffer(SB); |
| 892 | if (SourceMgr.getMainFileID() == 0) { |
| 893 | fprintf(stderr, "Error reading standard input! Empty?\n"); |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 894 | return true; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 895 | } |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 896 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 897 | } |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 898 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 899 | std::vector<char> PredefineBuffer; |
| 900 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 901 | // Add macros from the command line. |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 902 | unsigned d = 0, D = D_macros.size(); |
| 903 | unsigned u = 0, U = U_macros.size(); |
| 904 | while (d < D || u < U) { |
| 905 | if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u))) |
| 906 | DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str()); |
| 907 | else |
| 908 | DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef "); |
| 909 | } |
| 910 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 911 | // FIXME: Read any files specified by -imacros. |
| 912 | |
| 913 | // Add implicit #includes from -include. |
| 914 | for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i) |
| 915 | AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 916 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 917 | // Null terminate PredefinedBuffer and add it. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 918 | PredefineBuffer.push_back(0); |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 919 | PP.setPredefines(&PredefineBuffer[0]); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 920 | |
Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 921 | // Use PTH. |
| 922 | if (!TokenCache.empty()) { |
| 923 | PTHManager* PM = PTHManager::Create(TokenCache, PP); |
| 924 | if (PM) PP.setPTHManager(PM); |
| 925 | } |
| 926 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 927 | // Once we've read this, we're done. |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 928 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | //===----------------------------------------------------------------------===// |
| 932 | // Preprocessor include path information. |
| 933 | //===----------------------------------------------------------------------===// |
| 934 | |
| 935 | // This tool exports a large number of command line options to control how the |
| 936 | // preprocessor searches for header files. At root, however, the Preprocessor |
| 937 | // object takes a very simple interface: a list of directories to search for |
| 938 | // |
| 939 | // FIXME: -nostdinc,-nostdinc++ |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 940 | // FIXME: -imultilib |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 941 | // |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 942 | // FIXME: -imacros |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 943 | |
| 944 | static llvm::cl::opt<bool> |
| 945 | nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories")); |
| 946 | |
| 947 | // Various command line options. These four add directories to each chain. |
| 948 | static llvm::cl::list<std::string> |
| 949 | F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 950 | llvm::cl::desc("Add directory to framework include search path")); |
| 951 | static llvm::cl::list<std::string> |
| 952 | I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 953 | llvm::cl::desc("Add directory to include search path")); |
| 954 | static llvm::cl::list<std::string> |
| 955 | idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 956 | llvm::cl::desc("Add directory to AFTER include search path")); |
| 957 | static llvm::cl::list<std::string> |
| 958 | iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 959 | llvm::cl::desc("Add directory to QUOTE include search path")); |
| 960 | static llvm::cl::list<std::string> |
| 961 | isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 962 | llvm::cl::desc("Add directory to SYSTEM include search path")); |
| 963 | |
| 964 | // These handle -iprefix/-iwithprefix/-iwithprefixbefore. |
| 965 | static llvm::cl::list<std::string> |
| 966 | iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix, |
| 967 | llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix")); |
| 968 | static llvm::cl::list<std::string> |
| 969 | iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix, |
| 970 | llvm::cl::desc("Set directory to SYSTEM include search path with prefix")); |
| 971 | static llvm::cl::list<std::string> |
| 972 | iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"), |
| 973 | llvm::cl::Prefix, |
| 974 | llvm::cl::desc("Set directory to include search path with prefix")); |
| 975 | |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 976 | static llvm::cl::opt<std::string> |
| 977 | isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"), |
| 978 | llvm::cl::desc("Set the system root directory (usually /)")); |
| 979 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 980 | // Finally, implement the code that groks the options above. |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 981 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 982 | /// InitializeIncludePaths - Process the -I options and set them in the |
| 983 | /// HeaderSearch object. |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 984 | void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers, |
| 985 | FileManager &FM, const LangOptions &Lang) { |
| 986 | InitHeaderSearch Init(Headers, Verbose, isysroot); |
| 987 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 988 | // Handle -I... and -F... options, walking the lists in parallel. |
| 989 | unsigned Iidx = 0, Fidx = 0; |
| 990 | while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) { |
| 991 | if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 992 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 993 | ++Iidx; |
| 994 | } else { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 995 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 996 | ++Fidx; |
| 997 | } |
| 998 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 999 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1000 | // Consume what's left from whatever list was longer. |
| 1001 | for (; Iidx != I_dirs.size(); ++Iidx) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1002 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1003 | for (; Fidx != F_dirs.size(); ++Fidx) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1004 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Handle -idirafter... options. |
| 1007 | for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1008 | Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After, |
| 1009 | false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Handle -iquote... options. |
| 1012 | for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1013 | Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1014 | |
| 1015 | // Handle -isystem... options. |
| 1016 | for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1017 | Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1018 | |
| 1019 | // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in |
| 1020 | // parallel, processing the values in order of occurance to get the right |
| 1021 | // prefixes. |
| 1022 | { |
| 1023 | std::string Prefix = ""; // FIXME: this isn't the correct default prefix. |
| 1024 | unsigned iprefix_idx = 0; |
| 1025 | unsigned iwithprefix_idx = 0; |
| 1026 | unsigned iwithprefixbefore_idx = 0; |
| 1027 | bool iprefix_done = iprefix_vals.empty(); |
| 1028 | bool iwithprefix_done = iwithprefix_vals.empty(); |
| 1029 | bool iwithprefixbefore_done = iwithprefixbefore_vals.empty(); |
| 1030 | while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) { |
| 1031 | if (!iprefix_done && |
| 1032 | (iwithprefix_done || |
| 1033 | iprefix_vals.getPosition(iprefix_idx) < |
| 1034 | iwithprefix_vals.getPosition(iwithprefix_idx)) && |
| 1035 | (iwithprefixbefore_done || |
| 1036 | iprefix_vals.getPosition(iprefix_idx) < |
| 1037 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
| 1038 | Prefix = iprefix_vals[iprefix_idx]; |
| 1039 | ++iprefix_idx; |
| 1040 | iprefix_done = iprefix_idx == iprefix_vals.size(); |
| 1041 | } else if (!iwithprefix_done && |
| 1042 | (iwithprefixbefore_done || |
| 1043 | iwithprefix_vals.getPosition(iwithprefix_idx) < |
| 1044 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1045 | Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx], |
| 1046 | InitHeaderSearch::System, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1047 | ++iwithprefix_idx; |
| 1048 | iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size(); |
| 1049 | } else { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1050 | Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx], |
| 1051 | InitHeaderSearch::Angled, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1052 | ++iwithprefixbefore_idx; |
| 1053 | iwithprefixbefore_done = |
| 1054 | iwithprefixbefore_idx == iwithprefixbefore_vals.size(); |
| 1055 | } |
| 1056 | } |
| 1057 | } |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1058 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1059 | Init.AddDefaultEnvVarPaths(Lang); |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1061 | // Add the clang headers, which are relative to the clang driver. |
| 1062 | llvm::sys::Path MainExecutablePath = |
Chris Lattner | 985e182 | 2008-03-03 05:57:43 +0000 | [diff] [blame] | 1063 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 1064 | (void*)(intptr_t)InitializeIncludePaths); |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1065 | if (!MainExecutablePath.isEmpty()) { |
| 1066 | MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang |
| 1067 | MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin |
| 1068 | MainExecutablePath.appendComponent("Headers"); // Get foo/Headers |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1069 | Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System, |
| 1070 | false, false, false); |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1073 | if (!nostdinc) |
| 1074 | Init.AddDefaultSystemIncludePaths(Lang); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1075 | |
| 1076 | // Now that we have collected all of the include paths, merge them all |
| 1077 | // together and tell the preprocessor about them. |
| 1078 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1079 | Init.Realize(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1082 | //===----------------------------------------------------------------------===// |
| 1083 | // Driver PreprocessorFactory - For lazily generating preprocessors ... |
| 1084 | //===----------------------------------------------------------------------===// |
| 1085 | |
| 1086 | namespace { |
| 1087 | class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1088 | const std::string &InFile; |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1089 | Diagnostic &Diags; |
| 1090 | const LangOptions &LangInfo; |
| 1091 | TargetInfo &Target; |
| 1092 | SourceManager &SourceMgr; |
| 1093 | HeaderSearch &HeaderInfo; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1094 | bool InitializeSourceMgr; |
| 1095 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1096 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1097 | DriverPreprocessorFactory(const std::string &infile, |
| 1098 | Diagnostic &diags, const LangOptions &opts, |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1099 | TargetInfo &target, SourceManager &SM, |
| 1100 | HeaderSearch &Headers) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1101 | : InFile(infile), Diags(diags), LangInfo(opts), Target(target), |
| 1102 | SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {} |
| 1103 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1104 | |
| 1105 | virtual ~DriverPreprocessorFactory() {} |
| 1106 | |
| 1107 | virtual Preprocessor* CreatePreprocessor() { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1108 | Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target, |
| 1109 | SourceMgr, HeaderInfo); |
| 1110 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1111 | if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1112 | delete PP; |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1116 | /// FIXME: PP can only handle one callback |
| 1117 | if (ProgAction != PrintPreprocessedInput) { |
| 1118 | const char* ErrStr; |
| 1119 | bool DFG = CreateDependencyFileGen(PP, OutputFile, InFile, ErrStr); |
| 1120 | if (!DFG && ErrStr) { |
Ted Kremenek | 9a30c24 | 2008-10-25 20:19:34 +0000 | [diff] [blame] | 1121 | fprintf(stderr, "%s", ErrStr); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1122 | return NULL; |
| 1123 | } |
| 1124 | } |
| 1125 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1126 | InitializeSourceMgr = false; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1127 | return PP; |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1128 | } |
| 1129 | }; |
| 1130 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1131 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1132 | //===----------------------------------------------------------------------===// |
| 1133 | // Basic Parser driver |
| 1134 | //===----------------------------------------------------------------------===// |
| 1135 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1136 | static void ParseFile(Preprocessor &PP, MinimalAction *PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1137 | Parser P(PP, *PA); |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1138 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Parsing the specified input file. |
| 1141 | P.ParseTranslationUnit(); |
| 1142 | delete PA; |
| 1143 | } |
| 1144 | |
| 1145 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1146 | // Code generation options |
| 1147 | //===----------------------------------------------------------------------===// |
| 1148 | |
| 1149 | static llvm::cl::opt<bool> |
| 1150 | OptSize("Os", |
| 1151 | llvm::cl::desc("Optimize for size")); |
| 1152 | |
| 1153 | // It might be nice to add bounds to the CommandLine library directly. |
| 1154 | struct OptLevelParser : public llvm::cl::parser<unsigned> { |
| 1155 | bool parse(llvm::cl::Option &O, const char *ArgName, |
| 1156 | const std::string &Arg, unsigned &Val) { |
| 1157 | if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val)) |
| 1158 | return true; |
| 1159 | // FIXME: Support -O4. |
| 1160 | if (Val > 3) |
| 1161 | return O.error(": '" + Arg + "' invalid optimization level!"); |
| 1162 | return false; |
| 1163 | } |
| 1164 | }; |
| 1165 | static llvm::cl::opt<unsigned, false, OptLevelParser> |
| 1166 | OptLevel("O", llvm::cl::Prefix, |
| 1167 | llvm::cl::desc("Optimization level"), |
| 1168 | llvm::cl::init(0)); |
| 1169 | |
| 1170 | static void InitializeCompileOptions(CompileOptions &Opts) { |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1171 | Opts.OptimizeSize = OptSize; |
Daniel Dunbar | ac7ffe0 | 2008-10-29 07:56:11 +0000 | [diff] [blame] | 1172 | if (OptSize) { |
| 1173 | // -Os implies -O2 |
| 1174 | // FIXME: Diagnose conflicting options. |
| 1175 | Opts.OptimizationLevel = 2; |
| 1176 | } else { |
| 1177 | Opts.OptimizationLevel = OptLevel; |
| 1178 | } |
Daniel Dunbar | 8e8f3b7 | 2008-10-29 03:42:18 +0000 | [diff] [blame] | 1179 | |
| 1180 | // FIXME: There are llvm-gcc options to control these selectively. |
| 1181 | Opts.InlineFunctions = (Opts.OptimizationLevel > 1); |
| 1182 | Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize); |
| 1183 | Opts.SimplifyLibCalls = 1; |
Daniel Dunbar | dd913e5 | 2008-10-31 09:34:21 +0000 | [diff] [blame] | 1184 | |
| 1185 | #ifdef NDEBUG |
| 1186 | Opts.VerifyModule = 0; |
| 1187 | #endif |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1191 | // Main driver |
| 1192 | //===----------------------------------------------------------------------===// |
| 1193 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1194 | /// CreateASTConsumer - Create the ASTConsumer for the corresponding program |
| 1195 | /// action. These consumers can operate on both ASTs that are freshly |
| 1196 | /// parsed from source files as well as those deserialized from Bitcode. |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1197 | static ASTConsumer* CreateASTConsumer(const std::string& InFile, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1198 | Diagnostic& Diag, FileManager& FileMgr, |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1199 | const LangOptions& LangOpts, |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 1200 | Preprocessor *PP, |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1201 | PreprocessorFactory *PPF) { |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1202 | switch (ProgAction) { |
| 1203 | default: |
| 1204 | return NULL; |
| 1205 | |
| 1206 | case ASTPrint: |
| 1207 | return CreateASTPrinter(); |
| 1208 | |
| 1209 | case ASTDump: |
| 1210 | return CreateASTDumper(); |
| 1211 | |
| 1212 | case ASTView: |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 1213 | return CreateASTViewer(); |
| 1214 | |
| 1215 | case EmitHTML: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1216 | return CreateHTMLPrinter(OutputFile, Diag, PP, PPF); |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 1217 | |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1218 | case InheritanceView: |
| 1219 | return CreateInheritanceViewer(InheritanceViewCls); |
| 1220 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1221 | case TestSerialization: |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1222 | return CreateSerializationTest(Diag, FileMgr); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1223 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 1224 | case EmitAssembly: |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1225 | case EmitLLVM: |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1226 | case EmitBC: { |
| 1227 | BackendAction Act; |
| 1228 | if (ProgAction == EmitAssembly) { |
| 1229 | Act = Backend_EmitAssembly; |
| 1230 | } else if (ProgAction == EmitLLVM) { |
| 1231 | Act = Backend_EmitLL; |
| 1232 | } else { |
| 1233 | Act = Backend_EmitBC; |
| 1234 | } |
| 1235 | CompileOptions Opts; |
| 1236 | InitializeCompileOptions(Opts); |
| 1237 | return CreateBackendConsumer(Act, Diag, LangOpts, Opts, |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 1238 | InFile, OutputFile, GenerateDebugInfo); |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1239 | } |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 1240 | |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 1241 | case SerializeAST: |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1242 | // FIXME: Allow user to tailor where the file is written. |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1243 | return CreateASTSerializer(InFile, OutputFile, Diag); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1244 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1245 | case RewriteObjC: |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 1246 | return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts); |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 1247 | |
| 1248 | case RewriteBlocks: |
| 1249 | return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1250 | |
| 1251 | case RunAnalysis: |
| 1252 | assert (!AnalysisList.empty()); |
| 1253 | return CreateAnalysisConsumer(&AnalysisList[0], |
| 1254 | &AnalysisList[0]+AnalysisList.size(), |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 1255 | AnalysisStoreOpt, AnalysisDiagOpt, |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1256 | Diag, PP, PPF, LangOpts, |
| 1257 | AnalyzeSpecificFunction, |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 1258 | OutputFile, VisualizeEGDot, VisualizeEGUbi, |
| 1259 | TrimGraph, AnalyzeAll); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1263 | /// ProcessInputFile - Process a single input file with the specified state. |
| 1264 | /// |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1265 | static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1266 | const std::string &InFile, ProgActions PA) { |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1267 | llvm::OwningPtr<ASTConsumer> Consumer; |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1268 | bool ClearSourceMgr = false; |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1269 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1270 | switch (PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1271 | default: |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1272 | Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(), |
| 1273 | PP.getFileManager(), PP.getLangOptions(), |
| 1274 | &PP, &PPF)); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1275 | |
| 1276 | if (!Consumer) { |
| 1277 | fprintf(stderr, "Unexpected program action!\n"); |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1278 | HadErrors = true; |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1279 | return; |
| 1280 | } |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1281 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1282 | break; |
| 1283 | |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1284 | case DumpRawTokens: { |
| 1285 | SourceManager &SM = PP.getSourceManager(); |
| 1286 | std::pair<const char*,const char*> File = |
| 1287 | SM.getBufferData(SM.getMainFileID()); |
| 1288 | // Start lexing the specified input file. |
| 1289 | Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0), |
| 1290 | PP.getLangOptions(), File.first, File.second); |
| 1291 | RawLex.SetKeepWhitespaceMode(true); |
| 1292 | |
| 1293 | Token RawTok; |
| 1294 | |
| 1295 | RawLex.LexFromRawLexer(RawTok); |
| 1296 | while (RawTok.isNot(tok::eof)) { |
| 1297 | PP.DumpToken(RawTok, true); |
| 1298 | fprintf(stderr, "\n"); |
| 1299 | RawLex.LexFromRawLexer(RawTok); |
| 1300 | } |
| 1301 | ClearSourceMgr = true; |
| 1302 | break; |
| 1303 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1304 | case DumpTokens: { // Token dump mode. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1305 | Token Tok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1306 | // Start preprocessing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1307 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1308 | do { |
| 1309 | PP.Lex(Tok); |
| 1310 | PP.DumpToken(Tok, true); |
| 1311 | fprintf(stderr, "\n"); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1312 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1313 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1314 | break; |
| 1315 | } |
| 1316 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1317 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1318 | // Start parsing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1319 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1320 | do { |
| 1321 | PP.Lex(Tok); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1322 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1323 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1324 | break; |
| 1325 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1326 | |
| 1327 | case GeneratePCH: { |
| 1328 | CacheTokens(PP, OutputFile); |
| 1329 | ClearSourceMgr = true; |
| 1330 | break; |
| 1331 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1332 | |
| 1333 | case PrintPreprocessedInput: // -E mode. |
Chris Lattner | e988bc2 | 2008-01-27 23:55:11 +0000 | [diff] [blame] | 1334 | DoPrintPreprocessedInput(PP, OutputFile); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1335 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1336 | break; |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1337 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1338 | case ParseNoop: // -parse-noop |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1339 | ParseFile(PP, new MinimalAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1340 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1341 | break; |
| 1342 | |
| 1343 | case ParsePrintCallbacks: |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1344 | ParseFile(PP, CreatePrintParserActionsAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1345 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1346 | break; |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 1347 | |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1348 | case ParseSyntaxOnly: // -fsyntax-only |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1349 | Consumer.reset(new ASTConsumer()); |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 1350 | break; |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1351 | |
| 1352 | case RewriteMacros: |
Chris Lattner | 0951052 | 2008-05-09 22:43:24 +0000 | [diff] [blame] | 1353 | RewriteMacrosInInput(PP, InFile, OutputFile); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1354 | ClearSourceMgr = true; |
| 1355 | break; |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 1356 | |
| 1357 | case RewriteTest: |
| 1358 | DoRewriteTest(PP, InFile, OutputFile); |
| 1359 | ClearSourceMgr = true; |
| 1360 | break; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 1361 | } |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1362 | |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 1363 | if (Consumer) |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 1364 | ParseAST(PP, Consumer.get(), Stats, !DisableFree); |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 1365 | |
| 1366 | if (VerifyDiagnostics) |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1367 | if (CheckDiagnostics(PP)) |
| 1368 | exit(1); |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1369 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | if (Stats) { |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1371 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1372 | PP.PrintStats(); |
| 1373 | PP.getIdentifierTable().PrintStats(); |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1374 | PP.getHeaderSearchInfo().PrintStats(); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1375 | if (ClearSourceMgr) |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1376 | PP.getSourceManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1377 | fprintf(stderr, "\n"); |
| 1378 | } |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1379 | |
| 1380 | // For a multi-file compilation, some things are ok with nuking the source |
| 1381 | // manager tables, other require stable fileid/macroid's across multiple |
| 1382 | // files. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1383 | if (ClearSourceMgr) |
| 1384 | PP.getSourceManager().clearIDTables(); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 1385 | |
| 1386 | if (DisableFree) |
| 1387 | Consumer.take(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1390 | static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag, |
| 1391 | FileManager& FileMgr) { |
| 1392 | |
| 1393 | if (VerifyDiagnostics) { |
| 1394 | fprintf(stderr, "-verify does not yet work with serialized ASTs.\n"); |
| 1395 | exit (1); |
| 1396 | } |
| 1397 | |
| 1398 | llvm::sys::Path Filename(InFile); |
| 1399 | |
| 1400 | if (!Filename.isValid()) { |
| 1401 | fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str()); |
| 1402 | exit (1); |
| 1403 | } |
| 1404 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1405 | llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr)); |
Ted Kremenek | fe4e015 | 2007-12-13 18:11:11 +0000 | [diff] [blame] | 1406 | |
| 1407 | if (!TU) { |
| 1408 | fprintf(stderr, "error: file '%s' could not be deserialized\n", |
| 1409 | InFile.c_str()); |
| 1410 | exit (1); |
| 1411 | } |
| 1412 | |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 1413 | // Observe that we use the source file name stored in the deserialized |
| 1414 | // translation unit, rather than InFile. |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 1415 | llvm::OwningPtr<ASTConsumer> |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1416 | Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(), |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1417 | 0, 0)); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1418 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1419 | if (!Consumer) { |
| 1420 | fprintf(stderr, "Unsupported program action with serialized ASTs!\n"); |
| 1421 | exit (1); |
| 1422 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1423 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1424 | Consumer->Initialize(TU->getContext()); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1425 | |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1426 | // FIXME: We need to inform Consumer about completed TagDecls as well. |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1427 | for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I) |
| 1428 | Consumer->HandleTopLevelDecl(*I); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1432 | static llvm::cl::list<std::string> |
| 1433 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 1434 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1435 | static bool isSerializedFile(const std::string& InFile) { |
| 1436 | if (InFile.size() < 4) |
| 1437 | return false; |
| 1438 | |
| 1439 | const char* s = InFile.c_str()+InFile.size()-4; |
| 1440 | |
| 1441 | return s[0] == '.' && |
| 1442 | s[1] == 'a' && |
| 1443 | s[2] == 's' && |
| 1444 | s[3] == 't'; |
| 1445 | } |
| 1446 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1447 | |
| 1448 | int main(int argc, char **argv) { |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1449 | llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1450 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 1451 | |
| 1452 | // If no input was specified, read from stdin. |
| 1453 | if (InputFilenames.empty()) |
| 1454 | InputFilenames.push_back("-"); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1455 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1456 | // Create a file manager object to provide access to and cache the filesystem. |
| 1457 | FileManager FileMgr; |
| 1458 | |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1459 | // Create the diagnostic client for reporting errors or for |
| 1460 | // implementing -verify. |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1461 | DiagnosticClient* TextDiagClient = 0; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 1462 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1463 | if (!VerifyDiagnostics) { |
| 1464 | // Print diagnostics to stderr by default. |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 1465 | TextDiagClient = new TextDiagnosticPrinter(llvm::errs(), |
| 1466 | !NoShowColumn, |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1467 | !NoCaretDiagnostics); |
| 1468 | } else { |
| 1469 | // When checking diagnostics, just buffer them up. |
| 1470 | TextDiagClient = new TextDiagnosticBuffer(); |
| 1471 | |
| 1472 | if (InputFilenames.size() != 1) { |
| 1473 | fprintf(stderr, |
| 1474 | "-verify only works on single input files for now.\n"); |
| 1475 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1476 | } |
| 1477 | } |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1478 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1479 | // Configure our handling of diagnostics. |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1480 | llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient); |
| 1481 | Diagnostic Diags(DiagClient.get()); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1482 | InitializeDiagnostics(Diags); |
| 1483 | |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1484 | // -I- is a deprecated GCC feature, scan for it and reject it. |
| 1485 | for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) { |
| 1486 | if (I_dirs[i] == "-") { |
Chris Lattner | 5917fe1 | 2008-11-18 05:05:28 +0000 | [diff] [blame] | 1487 | Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported); |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1488 | I_dirs.erase(I_dirs.begin()+i); |
| 1489 | --i; |
| 1490 | } |
| 1491 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1492 | |
| 1493 | // Get information about the target being compiled for. |
| 1494 | std::string Triple = CreateTargetTriple(); |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1495 | llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple)); |
| 1496 | |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1497 | if (Target == 0) { |
| 1498 | fprintf(stderr, "Sorry, I don't know what target this is: %s\n", |
| 1499 | Triple.c_str()); |
| 1500 | fprintf(stderr, "Please use -triple or -arch.\n"); |
| 1501 | exit(1); |
| 1502 | } |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1503 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1504 | // Are we invoking one or more source analyses? |
| 1505 | if (!AnalysisList.empty() && ProgAction == ParseSyntaxOnly) |
| 1506 | ProgAction = RunAnalysis; |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1507 | else if (!InheritanceViewCls.empty()) // C++ visualization? |
| 1508 | ProgAction = InheritanceView; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1509 | |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1510 | llvm::OwningPtr<SourceManager> SourceMgr; |
| 1511 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1512 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1513 | const std::string &InFile = InputFilenames[i]; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1514 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1515 | if (isSerializedFile(InFile)) { |
| 1516 | Diags.setClient(TextDiagClient); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1517 | ProcessSerializedFile(InFile,Diags,FileMgr); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1518 | } |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1519 | else { |
| 1520 | /// Create a SourceManager object. This tracks and owns all the file |
| 1521 | /// buffers allocated to a translation unit. |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1522 | if (!SourceMgr) |
| 1523 | SourceMgr.reset(new SourceManager()); |
| 1524 | else |
| 1525 | SourceMgr->clearIDTables(); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1526 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1527 | // Initialize language options, inferring file types from input filenames. |
| 1528 | LangOptions LangInfo; |
| 1529 | InitializeBaseLanguage(); |
| 1530 | LangKind LK = GetLanguage(InFile); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1531 | bool PCH = InitializeLangOptions(LangInfo, LK); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 1532 | InitializeGCMode(LangInfo); |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 1533 | InitializeLanguageStandard(LangInfo, LK, Target.get()); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1534 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1535 | // Process the -I options and set them in the HeaderInfo. |
| 1536 | HeaderSearch HeaderInfo(FileMgr); |
Ted Kremenek | c68ecb5 | 2008-06-06 01:47:30 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1538 | InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1539 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1540 | // Set up the preprocessor with these options. |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1541 | DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target, |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1542 | *SourceMgr.get(), HeaderInfo); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1543 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1544 | llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor()); |
| 1545 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1546 | if (!PP) |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1547 | continue; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1548 | |
| 1549 | // Create the HTMLDiagnosticsClient if we are using one. Otherwise, |
| 1550 | // always reset to using TextDiagClient. |
| 1551 | llvm::OwningPtr<DiagnosticClient> TmpClient; |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1552 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1553 | if (!HTMLDiag.empty()) { |
| 1554 | TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(), |
| 1555 | &PPFactory)); |
| 1556 | Diags.setClient(TmpClient.get()); |
| 1557 | } |
| 1558 | else |
| 1559 | Diags.setClient(TextDiagClient); |
| 1560 | |
| 1561 | // Process the source file. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1562 | ProcessInputFile(*PP, PPFactory, InFile, PCH ? GeneratePCH : ProgAction); |
| 1563 | |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1564 | HeaderInfo.ClearFileInfo(); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1565 | |
| 1566 | if (Stats) |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1567 | SourceMgr->PrintStats(); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1568 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1569 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1570 | |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1571 | if (unsigned NumDiagnostics = Diags.getNumDiagnostics()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1572 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 1573 | (NumDiagnostics == 1 ? "" : "s")); |
| 1574 | |
| 1575 | if (Stats) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1576 | FileMgr.PrintStats(); |
| 1577 | fprintf(stderr, "\n"); |
| 1578 | } |
| 1579 | |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1580 | // If verifying diagnostics and we reached here, all is well. |
| 1581 | if (VerifyDiagnostics) |
| 1582 | return 0; |
| 1583 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 1584 | // Managed static deconstruction. Useful for making things like |
| 1585 | // -time-passes usable. |
| 1586 | llvm::llvm_shutdown(); |
| 1587 | |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1588 | return HadErrors || (Diags.getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1589 | } |