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