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