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