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