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