Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- clang.cpp - C-Language Front-end ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This utility may be invoked in the following manner: |
| 11 | // clang --help - Output help info. |
| 12 | // clang [options] - Read from stdin. |
| 13 | // clang [options] file - Read from "file". |
| 14 | // clang [options] file1 file2 - Read these files. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | // |
| 18 | // TODO: Options to support: |
| 19 | // |
| 20 | // -ffatal-errors |
| 21 | // -ftabstop=width |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "clang.h" |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 26 | #include "ASTConsumers.h" |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 27 | #include "clang/Driver/CompileOptions.h" |
Ted Kremenek | ad99dbf | 2008-11-03 22:31:48 +0000 | [diff] [blame] | 28 | #include "clang/Driver/PathDiagnosticClients.h" |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 29 | #include "clang/Driver/InitHeaderSearch.h" |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 30 | #include "clang/Driver/TextDiagnosticBuffer.h" |
| 31 | #include "clang/Driver/TextDiagnosticPrinter.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 32 | #include "clang/Analysis/PathDiagnostic.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 33 | #include "clang/AST/Decl.h" |
Ted Kremenek | 77cda50 | 2007-12-18 21:34:28 +0000 | [diff] [blame] | 34 | #include "clang/AST/TranslationUnit.h" |
Chris Lattner | 8ee3c03 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 35 | #include "clang/CodeGen/ModuleBuilder.h" |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 36 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 37 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | #include "clang/Parse/Parser.h" |
| 39 | #include "clang/Lex/HeaderSearch.h" |
| 40 | #include "clang/Basic/FileManager.h" |
| 41 | #include "clang/Basic/SourceManager.h" |
| 42 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 8f3dab8 | 2007-12-15 23:20:07 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/StringExtras.h" |
| 46 | #include "llvm/Config/config.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | #include "llvm/Support/MemoryBuffer.h" |
Zhongxing Xu | 2092236 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 50 | #include "llvm/Support/PluginLoader.h" |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 51 | #include "llvm/System/Host.h" |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 52 | #include "llvm/System/Path.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 53 | #include "llvm/System/Signals.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | using namespace clang; |
| 55 | |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | // Global options. |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 60 | static bool HadErrors = false; |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 61 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 62 | static llvm::cl::opt<bool> |
| 63 | Verbose("v", llvm::cl::desc("Enable verbose output")); |
| 64 | static llvm::cl::opt<bool> |
Nate Begeman | aabbb12 | 2007-12-30 01:38:50 +0000 | [diff] [blame] | 65 | Stats("print-stats", |
| 66 | llvm::cl::desc("Print performance metrics and statistics")); |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 67 | static llvm::cl::opt<bool> |
| 68 | DisableFree("disable-free", |
| 69 | llvm::cl::desc("Disable freeing of memory on exit"), |
| 70 | llvm::cl::init(false)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | |
| 72 | enum ProgActions { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 73 | RewriteObjC, // ObjC->C Rewriter. |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 74 | RewriteBlocks, // ObjC->C Rewriter for Blocks. |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 75 | RewriteMacros, // Expand macros but not #includes. |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 76 | RewriteTest, // Rewriter playground |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 77 | HTMLTest, // HTML displayer testing stuff. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 78 | EmitAssembly, // Emit a .s file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | EmitLLVM, // Emit a .ll file. |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 80 | EmitBC, // Emit a .bc file. |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 81 | SerializeAST, // Emit a .ast file. |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 82 | EmitHTML, // Translate input source into HTML. |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 83 | ASTPrint, // Parse ASTs and print them. |
| 84 | ASTDump, // Parse ASTs and dump them. |
| 85 | ASTView, // Parse ASTs and view them in Graphviz. |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 86 | PrintDeclContext, // Print DeclContext and their Decls. |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 87 | TestSerialization, // Run experimental serialization code. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | ParsePrintCallbacks, // Parse and print each callback. |
| 89 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
| 90 | ParseNoop, // Parse with noop callbacks. |
| 91 | RunPreprocessorOnly, // Just lex, no output. |
| 92 | PrintPreprocessedInput, // -E mode. |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 93 | DumpTokens, // Dump out preprocessed tokens. |
| 94 | DumpRawTokens, // Dump out raw tokens. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 95 | RunAnalysis, // Run one or more source code analyses. |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 96 | GeneratePCH, // Generate precompiled header. |
| 97 | InheritanceView // View C++ inheritance for a specified class. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static llvm::cl::opt<ProgActions> |
| 101 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 102 | llvm::cl::init(ParseSyntaxOnly), |
| 103 | llvm::cl::values( |
| 104 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 105 | "Just run preprocessor, no output (for timings)"), |
| 106 | clEnumValN(PrintPreprocessedInput, "E", |
| 107 | "Run preprocessor, emit preprocessed file"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 108 | clEnumValN(DumpRawTokens, "dump-raw-tokens", |
| 109 | "Lex file in raw mode and dump raw tokens"), |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 110 | clEnumValN(RunAnalysis, "analyze", |
| 111 | "Run static analysis engine"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 112 | clEnumValN(DumpTokens, "dump-tokens", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | "Run preprocessor, dump internal rep of tokens"), |
| 114 | clEnumValN(ParseNoop, "parse-noop", |
| 115 | "Run parser with noop callbacks (for timings)"), |
| 116 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 117 | "Run parser and perform semantic analysis"), |
| 118 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 119 | "Run parser and print each callback invoked"), |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 120 | clEnumValN(EmitHTML, "emit-html", |
| 121 | "Output input source as HTML"), |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 122 | clEnumValN(ASTPrint, "ast-print", |
| 123 | "Build ASTs and then pretty-print them"), |
| 124 | clEnumValN(ASTDump, "ast-dump", |
| 125 | "Build ASTs and then debug dump them"), |
Chris Lattner | ea254db | 2007-10-11 00:37:43 +0000 | [diff] [blame] | 126 | clEnumValN(ASTView, "ast-view", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 127 | "Build ASTs and view them with GraphViz"), |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 128 | clEnumValN(PrintDeclContext, "print-decl-contexts", |
| 129 | "Print DeclContexts and their Decls."), |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 130 | clEnumValN(TestSerialization, "test-pickling", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 131 | "Run prototype serialization code"), |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 132 | clEnumValN(EmitAssembly, "S", |
| 133 | "Emit native assembly code"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | clEnumValN(EmitLLVM, "emit-llvm", |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 135 | "Build ASTs then convert to LLVM, emit .ll file"), |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 136 | clEnumValN(EmitBC, "emit-llvm-bc", |
| 137 | "Build ASTs then convert to LLVM, emit .bc file"), |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 138 | clEnumValN(SerializeAST, "serialize", |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 139 | "Build ASTs and emit .ast file"), |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 140 | clEnumValN(RewriteTest, "rewrite-test", |
| 141 | "Rewriter playground"), |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 142 | clEnumValN(RewriteObjC, "rewrite-objc", |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 143 | "Rewrite ObjC into C (code rewriter example)"), |
| 144 | clEnumValN(RewriteMacros, "rewrite-macros", |
| 145 | "Expand macros without full preprocessing"), |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 146 | clEnumValN(RewriteBlocks, "rewrite-blocks", |
| 147 | "Rewrite Blocks to C"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | clEnumValEnd)); |
| 149 | |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 150 | |
| 151 | static llvm::cl::opt<std::string> |
| 152 | OutputFile("o", |
Ted Kremenek | 50b5641 | 2007-12-19 19:50:41 +0000 | [diff] [blame] | 153 | llvm::cl::value_desc("path"), |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 154 | llvm::cl::desc("Specify output file (for --serialize, this is a directory)")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 155 | |
| 156 | //===----------------------------------------------------------------------===// |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 157 | // Code Generator Options |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | static llvm::cl::opt<bool> |
| 160 | GenerateDebugInfo("g", |
| 161 | llvm::cl::desc("Generate source level debug information")); |
| 162 | |
Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 163 | |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | // PTH. |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | |
| 168 | static llvm::cl::opt<std::string> |
| 169 | TokenCache("token-cache", llvm::cl::value_desc("path"), |
| 170 | llvm::cl::desc("Use specified token cache file")); |
| 171 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 172 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 173 | // Diagnostic Options |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 176 | static llvm::cl::opt<bool> |
| 177 | VerifyDiagnostics("verify", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 178 | llvm::cl::desc("Verify emitted diagnostics and warnings")); |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 180 | static llvm::cl::opt<std::string> |
| 181 | HTMLDiag("html-diags", |
| 182 | llvm::cl::desc("Generate HTML to report diagnostics"), |
| 183 | llvm::cl::value_desc("HTML directory")); |
| 184 | |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 185 | static llvm::cl::opt<bool> |
| 186 | NoShowColumn("fno-show-column", |
| 187 | llvm::cl::desc("Do not include column number on diagnostics")); |
| 188 | |
| 189 | static llvm::cl::opt<bool> |
| 190 | NoCaretDiagnostics("fno-caret-diagnostics", |
| 191 | llvm::cl::desc("Do not include source line and caret with" |
| 192 | " diagnostics")); |
| 193 | |
| 194 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 196 | // C++ Visualization. |
| 197 | //===----------------------------------------------------------------------===// |
| 198 | |
| 199 | static llvm::cl::opt<std::string> |
| 200 | InheritanceViewCls("cxx-inheritance-view", |
| 201 | llvm::cl::value_desc("class name"), |
Daniel Dunbar | d77b251 | 2009-01-14 18:56:36 +0000 | [diff] [blame] | 202 | llvm::cl::desc("View C++ inheritance for a specified class")); |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 203 | |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | // Analyzer Options. |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
| 207 | |
| 208 | static llvm::cl::opt<bool> |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 209 | VisualizeEGDot("analyzer-viz-egraph-graphviz", |
| 210 | llvm::cl::desc("Display exploded graph using GraphViz")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 211 | |
| 212 | static llvm::cl::opt<bool> |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 213 | VisualizeEGUbi("analyzer-viz-egraph-ubigraph", |
| 214 | llvm::cl::desc("Display exploded graph using Ubigraph")); |
| 215 | |
| 216 | static llvm::cl::opt<bool> |
| 217 | AnalyzeAll("analyzer-opt-analyze-headers", |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 218 | llvm::cl::desc("Force the static analyzer to analyze " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 219 | "functions defined in header files")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 221 | static llvm::cl::list<Analyses> |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 222 | AnalysisList(llvm::cl::desc("SCA Checks/Analyses:"), |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 223 | llvm::cl::values( |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 224 | #define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\ |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 225 | clEnumValN(NAME, CMDFLAG, DESC), |
| 226 | #include "Analyses.def" |
| 227 | clEnumValEnd)); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 229 | static llvm::cl::opt<AnalysisStores> |
| 230 | AnalysisStoreOpt(llvm::cl::desc("SCA Low-Level Options (Store):"), |
| 231 | llvm::cl::init(BasicStoreModel), |
| 232 | llvm::cl::values( |
| 233 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC)\ |
| 234 | clEnumValN(NAME##Model, "analyzer-store-" CMDFLAG, DESC), |
| 235 | #include "Analyses.def" |
| 236 | clEnumValEnd)); |
| 237 | |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 238 | static llvm::cl::opt<AnalysisDiagClients> |
| 239 | AnalysisDiagOpt(llvm::cl::desc("SCA Output Options:"), |
| 240 | llvm::cl::init(PD_HTML), |
| 241 | llvm::cl::values( |
| 242 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN)\ |
| 243 | clEnumValN(PD_##NAME, "analyzer-output-" CMDFLAG, DESC), |
| 244 | #include "Analyses.def" |
| 245 | clEnumValEnd)); |
| 246 | |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 247 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | // Language Options |
| 249 | //===----------------------------------------------------------------------===// |
| 250 | |
| 251 | enum LangKind { |
| 252 | langkind_unspecified, |
| 253 | langkind_c, |
| 254 | langkind_c_cpp, |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 255 | langkind_asm_cpp, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 256 | langkind_c_pch, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | langkind_cxx, |
| 258 | langkind_cxx_cpp, |
| 259 | langkind_objc, |
| 260 | langkind_objc_cpp, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 261 | langkind_objc_pch, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | langkind_objcxx, |
Ted Kremenek | f890191 | 2009-01-09 00:38:08 +0000 | [diff] [blame] | 263 | langkind_objcxx_cpp, |
| 264 | langkind_objcxx_pch |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | /* TODO: GCC also accepts: |
| 268 | c-header c++-header objective-c-header objective-c++-header |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 269 | assembler |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | ada, f77*, ratfor (!), f95, java, treelang |
| 271 | */ |
| 272 | static llvm::cl::opt<LangKind> |
| 273 | BaseLang("x", llvm::cl::desc("Base language to compile"), |
| 274 | llvm::cl::init(langkind_unspecified), |
| 275 | llvm::cl::values(clEnumValN(langkind_c, "c", "C"), |
| 276 | clEnumValN(langkind_cxx, "c++", "C++"), |
| 277 | clEnumValN(langkind_objc, "objective-c", "Objective C"), |
| 278 | clEnumValN(langkind_objcxx,"objective-c++","Objective C++"), |
| 279 | clEnumValN(langkind_c_cpp, "c-cpp-output", |
| 280 | "Preprocessed C"), |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 281 | clEnumValN(langkind_asm_cpp, "assembler-with-cpp", |
| 282 | "Preprocessed asm"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | clEnumValN(langkind_cxx_cpp, "c++-cpp-output", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 284 | "Preprocessed C++"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | clEnumValN(langkind_objc_cpp, "objective-c-cpp-output", |
| 286 | "Preprocessed Objective C"), |
| 287 | clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output", |
| 288 | "Preprocessed Objective C++"), |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 289 | clEnumValN(langkind_c_pch,"c-header", |
| 290 | "Precompiled C header"), |
| 291 | clEnumValN(langkind_objc_pch, "objective-c-header", |
Ted Kremenek | f890191 | 2009-01-09 00:38:08 +0000 | [diff] [blame] | 292 | "Precompiled Objective-C header"), |
| 293 | clEnumValN(langkind_objcxx_pch, "objective-c++-header", |
| 294 | "Precompiled Objective-C++ header"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | clEnumValEnd)); |
| 296 | |
| 297 | static llvm::cl::opt<bool> |
| 298 | LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"), |
| 299 | llvm::cl::Hidden); |
| 300 | static llvm::cl::opt<bool> |
| 301 | LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"), |
| 302 | llvm::cl::Hidden); |
| 303 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 304 | /// InitializeBaseLanguage - Handle the -x foo options. |
| 305 | static void InitializeBaseLanguage() { |
| 306 | if (LangObjC) |
| 307 | BaseLang = langkind_objc; |
| 308 | else if (LangObjCXX) |
| 309 | BaseLang = langkind_objcxx; |
| 310 | } |
| 311 | |
| 312 | static LangKind GetLanguage(const std::string &Filename) { |
| 313 | if (BaseLang != langkind_unspecified) |
| 314 | return BaseLang; |
| 315 | |
| 316 | std::string::size_type DotPos = Filename.rfind('.'); |
| 317 | |
| 318 | if (DotPos == std::string::npos) { |
| 319 | BaseLang = langkind_c; // Default to C if no extension. |
Chris Lattner | 9b2f6c4 | 2008-01-04 19:12:28 +0000 | [diff] [blame] | 320 | return langkind_c; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 323 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 324 | // C header: .h |
| 325 | // C++ header: .hh or .H; |
| 326 | // assembler no preprocessing: .s |
| 327 | // assembler: .S |
| 328 | if (Ext == "c") |
| 329 | return langkind_c; |
Chris Lattner | 1b450b0 | 2008-10-28 20:33:42 +0000 | [diff] [blame] | 330 | else if (Ext == "S") |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 331 | return langkind_asm_cpp; |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 332 | else if (Ext == "i") |
| 333 | return langkind_c_cpp; |
| 334 | else if (Ext == "ii") |
| 335 | return langkind_cxx_cpp; |
| 336 | else if (Ext == "m") |
| 337 | return langkind_objc; |
| 338 | else if (Ext == "mi") |
| 339 | return langkind_objc_cpp; |
| 340 | else if (Ext == "mm" || Ext == "M") |
| 341 | return langkind_objcxx; |
| 342 | else if (Ext == "mii") |
| 343 | return langkind_objcxx_cpp; |
| 344 | else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" || |
| 345 | Ext == "c++" || Ext == "cp" || Ext == "cxx") |
| 346 | return langkind_cxx; |
| 347 | else |
| 348 | return langkind_c; |
| 349 | } |
| 350 | |
| 351 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 352 | static void InitializeCOptions(LangOptions &Options) { |
| 353 | // Do nothing. |
| 354 | } |
| 355 | |
| 356 | static void InitializeObjCOptions(LangOptions &Options) { |
| 357 | Options.ObjC1 = Options.ObjC2 = 1; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | static bool InitializeLangOptions(LangOptions &Options, LangKind LK){ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | // FIXME: implement -fpreprocessed mode. |
| 363 | bool NoPreprocess = false; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 364 | bool PCH = false; |
Ted Kremenek | f890191 | 2009-01-09 00:38:08 +0000 | [diff] [blame] | 365 | |
| 366 | // Test for 'PCH'. |
| 367 | switch (LK) { |
| 368 | default: |
| 369 | break; |
| 370 | case langkind_c_pch: |
| 371 | LK = langkind_c; |
| 372 | PCH = true; |
| 373 | break; |
| 374 | case langkind_objc_pch: |
| 375 | LK = langkind_objc; |
| 376 | PCH = true; |
| 377 | break; |
| 378 | case langkind_objcxx_pch: |
| 379 | LK = langkind_objcxx; |
| 380 | PCH = true; |
| 381 | break; |
| 382 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 384 | switch (LK) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | default: assert(0 && "Unknown language kind!"); |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 386 | case langkind_asm_cpp: |
Daniel Dunbar | c157145 | 2008-12-01 18:55:22 +0000 | [diff] [blame] | 387 | Options.AsmPreprocessor = 1; |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 388 | // FALLTHROUGH |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | case langkind_c_cpp: |
| 390 | NoPreprocess = true; |
| 391 | // FALLTHROUGH |
| 392 | case langkind_c: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 393 | InitializeCOptions(Options); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | break; |
| 395 | case langkind_cxx_cpp: |
| 396 | NoPreprocess = true; |
| 397 | // FALLTHROUGH |
| 398 | case langkind_cxx: |
| 399 | Options.CPlusPlus = 1; |
| 400 | break; |
| 401 | case langkind_objc_cpp: |
| 402 | NoPreprocess = true; |
| 403 | // FALLTHROUGH |
| 404 | case langkind_objc: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 405 | InitializeObjCOptions(Options); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | break; |
| 407 | case langkind_objcxx_cpp: |
| 408 | NoPreprocess = true; |
| 409 | // FALLTHROUGH |
| 410 | case langkind_objcxx: |
| 411 | Options.ObjC1 = Options.ObjC2 = 1; |
| 412 | Options.CPlusPlus = 1; |
| 413 | break; |
| 414 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 415 | |
| 416 | return PCH; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /// LangStds - Language standards we support. |
| 420 | enum LangStds { |
| 421 | lang_unspecified, |
| 422 | lang_c89, lang_c94, lang_c99, |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 423 | lang_gnu_START, |
| 424 | lang_gnu89 = lang_gnu_START, lang_gnu99, |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 425 | lang_cxx98, lang_gnucxx98, |
| 426 | lang_cxx0x, lang_gnucxx0x |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | static llvm::cl::opt<LangStds> |
| 430 | LangStd("std", llvm::cl::desc("Language standard to compile for"), |
| 431 | llvm::cl::init(lang_unspecified), |
| 432 | llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"), |
| 433 | clEnumValN(lang_c89, "c90", "ISO C 1990"), |
| 434 | clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"), |
| 435 | clEnumValN(lang_c94, "iso9899:199409", |
| 436 | "ISO C 1990 with amendment 1"), |
| 437 | clEnumValN(lang_c99, "c99", "ISO C 1999"), |
| 438 | // clEnumValN(lang_c99, "c9x", "ISO C 1999"), |
| 439 | clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"), |
| 440 | // clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"), |
| 441 | clEnumValN(lang_gnu89, "gnu89", |
| 442 | "ISO C 1990 with GNU extensions (default for C)"), |
| 443 | clEnumValN(lang_gnu99, "gnu99", |
| 444 | "ISO C 1999 with GNU extensions"), |
| 445 | clEnumValN(lang_gnu99, "gnu9x", |
| 446 | "ISO C 1999 with GNU extensions"), |
| 447 | clEnumValN(lang_cxx98, "c++98", |
| 448 | "ISO C++ 1998 with amendments"), |
| 449 | clEnumValN(lang_gnucxx98, "gnu++98", |
| 450 | "ISO C++ 1998 with amendments and GNU " |
| 451 | "extensions (default for C++)"), |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 452 | clEnumValN(lang_cxx0x, "c++0x", |
| 453 | "Upcoming ISO C++ 200x with amendments"), |
| 454 | clEnumValN(lang_gnucxx0x, "gnu++0x", |
| 455 | "Upcoming ISO C++ 200x with amendments and GNU " |
| 456 | "extensions (default for C++)"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 457 | clEnumValEnd)); |
| 458 | |
| 459 | static llvm::cl::opt<bool> |
| 460 | NoOperatorNames("fno-operator-names", |
| 461 | llvm::cl::desc("Do not treat C++ operator name keywords as " |
| 462 | "synonyms for operators")); |
| 463 | |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 464 | static llvm::cl::opt<bool> |
| 465 | PascalStrings("fpascal-strings", |
| 466 | llvm::cl::desc("Recognize and construct Pascal-style " |
| 467 | "string literals")); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 468 | |
| 469 | static llvm::cl::opt<bool> |
| 470 | MSExtensions("fms-extensions", |
| 471 | llvm::cl::desc("Accept some non-standard constructs used in " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 472 | "Microsoft header files ")); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 473 | |
| 474 | static llvm::cl::opt<bool> |
| 475 | WritableStrings("fwritable-strings", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 476 | llvm::cl::desc("Store string literals as writable data")); |
Anders Carlsson | 695dbb6 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 477 | |
| 478 | static llvm::cl::opt<bool> |
| 479 | LaxVectorConversions("flax-vector-conversions", |
| 480 | llvm::cl::desc("Allow implicit conversions between vectors" |
| 481 | " with a different number of elements or " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 482 | "different element types")); |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 483 | static llvm::cl::opt<bool> |
| 484 | EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature")); |
| 485 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame^] | 486 | static llvm::cl::opt<bool> |
| 487 | ObjCModernABI("fobjc-modern-abi", llvm::cl::desc("enable objective-c's modern abi")); |
| 488 | |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 489 | |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 490 | // FIXME: This (and all GCC -f options) really come in -f... and |
| 491 | // -fno-... forms, and additionally support automagic behavior when |
| 492 | // they are not defined. For example, -fexceptions defaults to on or |
| 493 | // off depending on the language. We should support this behavior in |
| 494 | // some form (perhaps just add a facility for distinguishing when an |
| 495 | // has its default value from when it has been set to its default |
| 496 | // value). |
| 497 | static llvm::cl::opt<bool> |
| 498 | Exceptions("fexceptions", |
| 499 | llvm::cl::desc("Enable support for exception handling.")); |
| 500 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 501 | static llvm::cl::opt<bool> |
| 502 | GNURuntime("fgnu-runtime", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 503 | llvm::cl::desc("Generate output compatible with the standard GNU " |
| 504 | "Objective-C runtime.")); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 505 | |
| 506 | static llvm::cl::opt<bool> |
| 507 | NeXTRuntime("fnext-runtime", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 508 | llvm::cl::desc("Generate output compatible with the NeXT " |
| 509 | "runtime.")); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 510 | |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 511 | |
| 512 | |
| 513 | static llvm::cl::opt<bool> |
| 514 | Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences.")); |
| 515 | |
| 516 | static llvm::cl::opt<bool> |
| 517 | Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89.")); |
| 518 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | // FIXME: add: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 520 | // -fdollars-in-identifiers |
Daniel Dunbar | dcb4a1a | 2008-08-23 08:43:39 +0000 | [diff] [blame] | 521 | static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, |
| 522 | TargetInfo *Target) { |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 523 | // Allow the target to set the default the langauge options as it sees fit. |
| 524 | Target->getDefaultLangOptions(Options); |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 525 | |
| 526 | if (Ansi) // "The -ansi option is equivalent to -std=c89." |
| 527 | LangStd = lang_c89; |
| 528 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 529 | if (LangStd == lang_unspecified) { |
| 530 | // Based on the base language, pick one. |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 531 | switch (LK) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | default: assert(0 && "Unknown base language"); |
| 533 | case langkind_c: |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 534 | case langkind_asm_cpp: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 535 | case langkind_c_cpp: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 536 | case langkind_c_pch: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 537 | case langkind_objc: |
| 538 | case langkind_objc_cpp: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 539 | case langkind_objc_pch: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 540 | LangStd = lang_gnu99; |
| 541 | break; |
| 542 | case langkind_cxx: |
| 543 | case langkind_cxx_cpp: |
| 544 | case langkind_objcxx: |
| 545 | case langkind_objcxx_cpp: |
Ted Kremenek | f890191 | 2009-01-09 00:38:08 +0000 | [diff] [blame] | 546 | case langkind_objcxx_pch: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 547 | LangStd = lang_gnucxx98; |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | switch (LangStd) { |
| 553 | default: assert(0 && "Unknown language standard!"); |
| 554 | |
| 555 | // Fall through from newer standards to older ones. This isn't really right. |
| 556 | // FIXME: Enable specifically the right features based on the language stds. |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 557 | case lang_gnucxx0x: |
| 558 | case lang_cxx0x: |
| 559 | Options.CPlusPlus0x = 1; |
| 560 | // FALL THROUGH |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 561 | case lang_gnucxx98: |
| 562 | case lang_cxx98: |
| 563 | Options.CPlusPlus = 1; |
| 564 | Options.CXXOperatorNames = !NoOperatorNames; |
Nate Begeman | 8aebcb7 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 565 | Options.Boolean = 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 566 | // FALL THROUGH. |
| 567 | case lang_gnu99: |
| 568 | case lang_c99: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 569 | Options.C99 = 1; |
| 570 | Options.HexFloats = 1; |
| 571 | // FALL THROUGH. |
| 572 | case lang_gnu89: |
| 573 | Options.BCPLComment = 1; // Only for C99/C++. |
| 574 | // FALL THROUGH. |
| 575 | case lang_c94: |
Chris Lattner | 3426b9b | 2008-02-25 04:01:39 +0000 | [diff] [blame] | 576 | Options.Digraphs = 1; // C94, C99, C++. |
| 577 | // FALL THROUGH. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 578 | case lang_c89: |
| 579 | break; |
| 580 | } |
Argyrios Kyrtzidis | d146552 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 581 | |
| 582 | if (Options.CPlusPlus) { |
| 583 | Options.C99 = 0; |
| 584 | Options.HexFloats = (LangStd == lang_gnucxx98 || LangStd==lang_gnucxx0x); |
| 585 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 586 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 587 | if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89) |
| 588 | Options.ImplicitInt = 1; |
| 589 | else |
| 590 | Options.ImplicitInt = 0; |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 591 | |
| 592 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi |
| 593 | // is specified, or -std is set to a conforming mode. |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 594 | Options.Trigraphs = LangStd < lang_gnu_START; |
| 595 | if (Trigraphs.getPosition()) |
| 596 | Options.Trigraphs = Trigraphs; // Command line option wins. |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 598 | // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off |
| 599 | // even if they are normally on for the target. In GNU modes (e.g. |
| 600 | // -std=gnu99) the default for blocks depends on the target settings. |
Anders Carlsson | e56f6ff | 2009-01-21 18:47:36 +0000 | [diff] [blame] | 601 | // However, blocks are not turned off when compiling Obj-C or Obj-C++ code. |
| 602 | if (!Options.ObjC1 && LangStd < lang_gnu_START) |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 603 | Options.Blocks = 0; |
| 604 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 605 | Options.DollarIdents = 1; // FIXME: Really a target property. |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 606 | if (PascalStrings.getPosition()) |
| 607 | Options.PascalStrings = PascalStrings; |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 608 | Options.Microsoft = MSExtensions; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 609 | Options.WritableStrings = WritableStrings; |
Anders Carlsson | 695dbb6 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 610 | Options.LaxVectorConversions = LaxVectorConversions; |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 611 | Options.Exceptions = Exceptions; |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 612 | if (EnableBlocks.getPosition()) |
| 613 | Options.Blocks = EnableBlocks; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 614 | |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 615 | // Override the default runtime if the user requested it. |
| 616 | if (NeXTRuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 617 | Options.NeXTRuntime = 1; |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 618 | else if (GNURuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 619 | Options.NeXTRuntime = 0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame^] | 620 | |
| 621 | if (ObjCModernABI) |
| 622 | Options.ObjCModernABI = 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 625 | static llvm::cl::opt<bool> |
| 626 | ObjCExclusiveGC("fobjc-gc-only", |
| 627 | llvm::cl::desc("Use GC exclusively for Objective-C related " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 628 | "memory management")); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 629 | |
| 630 | static llvm::cl::opt<bool> |
| 631 | ObjCEnableGC("fobjc-gc", |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 632 | llvm::cl::desc("Enable Objective-C garbage collection")); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 633 | |
| 634 | void InitializeGCMode(LangOptions &Options) { |
| 635 | if (ObjCExclusiveGC) |
| 636 | Options.setGCMode(LangOptions::GCOnly); |
| 637 | else if (ObjCEnableGC) |
| 638 | Options.setGCMode(LangOptions::HybridGC); |
| 639 | } |
| 640 | |
| 641 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 642 | //===----------------------------------------------------------------------===// |
| 643 | // Our DiagnosticClient implementation |
| 644 | //===----------------------------------------------------------------------===// |
| 645 | |
| 646 | // FIXME: Werror should take a list of things, -Werror=foo,bar |
| 647 | static llvm::cl::opt<bool> |
| 648 | WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors")); |
| 649 | |
| 650 | static llvm::cl::opt<bool> |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 651 | SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings")); |
| 652 | |
| 653 | static llvm::cl::opt<bool> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 654 | WarnOnExtensions("pedantic", llvm::cl::init(false), |
| 655 | llvm::cl::desc("Issue a warning on uses of GCC extensions")); |
| 656 | |
| 657 | static llvm::cl::opt<bool> |
| 658 | ErrorOnExtensions("pedantic-errors", |
| 659 | llvm::cl::desc("Issue an error on uses of GCC extensions")); |
| 660 | |
| 661 | static llvm::cl::opt<bool> |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 662 | SuppressSystemWarnings("suppress-system-warnings", |
Daniel Dunbar | 320a054 | 2008-09-30 20:49:53 +0000 | [diff] [blame] | 663 | llvm::cl::desc("Suppress warnings issued in system headers"), |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 664 | llvm::cl::init(true)); |
| 665 | |
| 666 | static llvm::cl::opt<bool> |
Daniel Dunbar | d77b251 | 2009-01-14 18:56:36 +0000 | [diff] [blame] | 667 | WarnUnusedMacros("Wunused-macros", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | llvm::cl::desc("Warn for unused macros in the main translation unit")); |
| 669 | |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 670 | static llvm::cl::opt<bool> |
| 671 | WarnFloatEqual("Wfloat-equal", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 672 | llvm::cl::desc("Warn about equality comparisons of floating point values")); |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 673 | |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 674 | static llvm::cl::opt<bool> |
Fariborz Jahanian | 17d0d0d | 2009-01-08 23:23:10 +0000 | [diff] [blame] | 675 | WarnPropertyReadonlyAttrs("Wreadonly-setter-attrs", |
| 676 | llvm::cl::desc("Warn about readonly properties with writable attributes")); |
| 677 | |
| 678 | static llvm::cl::opt<bool> |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 679 | WarnNoFormatNonLiteral("Wno-format-nonliteral", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 680 | llvm::cl::desc("Do not warn about non-literal format strings")); |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 682 | static llvm::cl::opt<bool> |
| 683 | WarnUndefMacros("Wundef", |
| 684 | llvm::cl::desc("Warn on use of undefined macros in #if's")); |
| 685 | |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 686 | static llvm::cl::opt<bool> |
Ted Kremenek | 358256d | 2008-05-30 16:42:02 +0000 | [diff] [blame] | 687 | WarnImplicitFunctionDeclaration("Wimplicit-function-declaration", |
| 688 | llvm::cl::desc("Warn about uses of implicitly defined functions")); |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 689 | |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 690 | static llvm::cl::opt<bool> |
| 691 | WarnNoStrictSelectorMatch("Wno-strict-selector-match", |
| 692 | llvm::cl::desc("Do not warn about duplicate methods that have the same size and alignment"), |
| 693 | llvm::cl::init(true)); |
| 694 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 695 | /// InitializeDiagnostics - Initialize the diagnostic object, based on the |
| 696 | /// current command line option settings. |
| 697 | static void InitializeDiagnostics(Diagnostic &Diags) { |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 698 | Diags.setIgnoreAllWarnings(SilenceWarnings); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 699 | Diags.setWarningsAsErrors(WarningsAsErrors); |
| 700 | Diags.setWarnOnExtensions(WarnOnExtensions); |
| 701 | Diags.setErrorOnExtensions(ErrorOnExtensions); |
| 702 | |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 703 | // Suppress warnings in system headers unless requested not to. |
| 704 | Diags.setSuppressSystemWarnings(SuppressSystemWarnings); |
| 705 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 706 | // Silence the "macro is not used" warning unless requested. |
| 707 | if (!WarnUnusedMacros) |
| 708 | Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE); |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 709 | |
| 710 | // Silence "floating point comparison" warnings unless requested. |
| 711 | if (!WarnFloatEqual) |
| 712 | Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE); |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 713 | |
Fariborz Jahanian | 17d0d0d | 2009-01-08 23:23:10 +0000 | [diff] [blame] | 714 | if (!WarnPropertyReadonlyAttrs) |
| 715 | Diags.setDiagnosticMapping(diag::warn_objc_property_attr_mutually_exclusive, |
| 716 | diag::MAP_IGNORE); |
| 717 | |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 718 | // Silence "format string is not a string literal" warnings if requested |
| 719 | if (WarnNoFormatNonLiteral) |
Ted Kremenek | 7c1d3df | 2007-12-17 17:50:39 +0000 | [diff] [blame] | 720 | Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant, |
| 721 | diag::MAP_IGNORE); |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 722 | if (!WarnUndefMacros) |
| 723 | Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE); |
Steve Naroff | e7a3730 | 2008-02-11 22:40:08 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 725 | if (!WarnImplicitFunctionDeclaration) |
| 726 | Diags.setDiagnosticMapping(diag::warn_implicit_function_decl, |
| 727 | diag::MAP_IGNORE); |
| 728 | |
Chris Lattner | 8aedf19 | 2008-05-04 23:52:02 +0000 | [diff] [blame] | 729 | // If -pedantic-errors is set, turn extensions that warn by default into |
| 730 | // errors. |
| 731 | if (ErrorOnExtensions) { |
| 732 | Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large, |
| 733 | diag::MAP_ERROR); |
| 734 | Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large, |
| 735 | diag::MAP_ERROR); |
| 736 | } |
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. |
| 836 | std::string Version = llvm::sys::osVersion(); |
| 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()) |
| 1152 | PTHMgr.reset(PTHManager::Create(TokenCache)); |
| 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, |
| 1316 | TrimGraph, AnalyzeAll); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1320 | /// ProcessInputFile - Process a single input file with the specified state. |
| 1321 | /// |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1322 | static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1323 | const std::string &InFile, ProgActions PA) { |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1324 | llvm::OwningPtr<ASTConsumer> Consumer; |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1325 | bool ClearSourceMgr = false; |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1326 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1327 | switch (PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1328 | default: |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1329 | Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(), |
| 1330 | PP.getFileManager(), PP.getLangOptions(), |
| 1331 | &PP, &PPF)); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1332 | |
| 1333 | if (!Consumer) { |
| 1334 | fprintf(stderr, "Unexpected program action!\n"); |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1335 | HadErrors = true; |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1336 | return; |
| 1337 | } |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1338 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1339 | break; |
| 1340 | |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1341 | case DumpRawTokens: { |
| 1342 | SourceManager &SM = PP.getSourceManager(); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1343 | // Start lexing the specified input file. |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 1344 | Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions()); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1345 | RawLex.SetKeepWhitespaceMode(true); |
| 1346 | |
| 1347 | Token RawTok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1348 | RawLex.LexFromRawLexer(RawTok); |
| 1349 | while (RawTok.isNot(tok::eof)) { |
| 1350 | PP.DumpToken(RawTok, true); |
| 1351 | fprintf(stderr, "\n"); |
| 1352 | RawLex.LexFromRawLexer(RawTok); |
| 1353 | } |
| 1354 | ClearSourceMgr = true; |
| 1355 | break; |
| 1356 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | case DumpTokens: { // Token dump mode. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1358 | Token Tok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1359 | // Start preprocessing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1360 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1361 | do { |
| 1362 | PP.Lex(Tok); |
| 1363 | PP.DumpToken(Tok, true); |
| 1364 | fprintf(stderr, "\n"); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1365 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1366 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1367 | break; |
| 1368 | } |
| 1369 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1370 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1371 | // Start parsing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1372 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1373 | do { |
| 1374 | PP.Lex(Tok); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1375 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1376 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1377 | break; |
| 1378 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1379 | |
| 1380 | case GeneratePCH: { |
| 1381 | CacheTokens(PP, OutputFile); |
| 1382 | ClearSourceMgr = true; |
| 1383 | break; |
| 1384 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | |
| 1386 | case PrintPreprocessedInput: // -E mode. |
Chris Lattner | e988bc2 | 2008-01-27 23:55:11 +0000 | [diff] [blame] | 1387 | DoPrintPreprocessedInput(PP, OutputFile); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1388 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1389 | break; |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1390 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1391 | case ParseNoop: // -parse-noop |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1392 | ParseFile(PP, new MinimalAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1393 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1394 | break; |
| 1395 | |
| 1396 | case ParsePrintCallbacks: |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1397 | ParseFile(PP, CreatePrintParserActionsAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1398 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1399 | break; |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 1400 | |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1401 | case ParseSyntaxOnly: // -fsyntax-only |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1402 | Consumer.reset(new ASTConsumer()); |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 1403 | break; |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1404 | |
| 1405 | case RewriteMacros: |
Chris Lattner | 0951052 | 2008-05-09 22:43:24 +0000 | [diff] [blame] | 1406 | RewriteMacrosInInput(PP, InFile, OutputFile); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1407 | ClearSourceMgr = true; |
| 1408 | break; |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 1409 | |
| 1410 | case RewriteTest: |
| 1411 | DoRewriteTest(PP, InFile, OutputFile); |
| 1412 | ClearSourceMgr = true; |
| 1413 | break; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 1414 | } |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1415 | |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 1416 | if (Consumer) |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 1417 | ParseAST(PP, Consumer.get(), Stats, !DisableFree); |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 1418 | |
| 1419 | if (VerifyDiagnostics) |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1420 | if (CheckDiagnostics(PP)) |
| 1421 | exit(1); |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1423 | if (Stats) { |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1424 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1425 | PP.PrintStats(); |
| 1426 | PP.getIdentifierTable().PrintStats(); |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1427 | PP.getHeaderSearchInfo().PrintStats(); |
Ted Kremenek | 1b95a65 | 2009-01-09 18:20:21 +0000 | [diff] [blame] | 1428 | PP.getSourceManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1429 | fprintf(stderr, "\n"); |
| 1430 | } |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1431 | |
| 1432 | // For a multi-file compilation, some things are ok with nuking the source |
| 1433 | // manager tables, other require stable fileid/macroid's across multiple |
| 1434 | // files. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1435 | if (ClearSourceMgr) |
| 1436 | PP.getSourceManager().clearIDTables(); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 1437 | |
| 1438 | if (DisableFree) |
| 1439 | Consumer.take(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1442 | static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag, |
| 1443 | FileManager& FileMgr) { |
| 1444 | |
| 1445 | if (VerifyDiagnostics) { |
| 1446 | fprintf(stderr, "-verify does not yet work with serialized ASTs.\n"); |
| 1447 | exit (1); |
| 1448 | } |
| 1449 | |
| 1450 | llvm::sys::Path Filename(InFile); |
| 1451 | |
| 1452 | if (!Filename.isValid()) { |
| 1453 | fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str()); |
| 1454 | exit (1); |
| 1455 | } |
| 1456 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1457 | llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr)); |
Ted Kremenek | fe4e015 | 2007-12-13 18:11:11 +0000 | [diff] [blame] | 1458 | |
| 1459 | if (!TU) { |
| 1460 | fprintf(stderr, "error: file '%s' could not be deserialized\n", |
| 1461 | InFile.c_str()); |
| 1462 | exit (1); |
| 1463 | } |
| 1464 | |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 1465 | // Observe that we use the source file name stored in the deserialized |
| 1466 | // translation unit, rather than InFile. |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 1467 | llvm::OwningPtr<ASTConsumer> |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1468 | Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(), |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1469 | 0, 0)); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1470 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1471 | if (!Consumer) { |
| 1472 | fprintf(stderr, "Unsupported program action with serialized ASTs!\n"); |
| 1473 | exit (1); |
| 1474 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1475 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1476 | Consumer->Initialize(TU->getContext()); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1477 | |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1478 | // FIXME: We need to inform Consumer about completed TagDecls as well. |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1479 | for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I) |
| 1480 | Consumer->HandleTopLevelDecl(*I); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1484 | static llvm::cl::list<std::string> |
| 1485 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 1486 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1487 | static bool isSerializedFile(const std::string& InFile) { |
| 1488 | if (InFile.size() < 4) |
| 1489 | return false; |
| 1490 | |
| 1491 | const char* s = InFile.c_str()+InFile.size()-4; |
| 1492 | |
| 1493 | return s[0] == '.' && |
| 1494 | s[1] == 'a' && |
| 1495 | s[2] == 's' && |
| 1496 | s[3] == 't'; |
| 1497 | } |
| 1498 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1499 | |
| 1500 | int main(int argc, char **argv) { |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1501 | llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1502 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 1503 | |
| 1504 | // If no input was specified, read from stdin. |
| 1505 | if (InputFilenames.empty()) |
| 1506 | InputFilenames.push_back("-"); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1507 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1508 | // Create a file manager object to provide access to and cache the filesystem. |
| 1509 | FileManager FileMgr; |
| 1510 | |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1511 | // Create the diagnostic client for reporting errors or for |
| 1512 | // implementing -verify. |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1513 | DiagnosticClient* TextDiagClient = 0; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 1514 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1515 | if (!VerifyDiagnostics) { |
| 1516 | // Print diagnostics to stderr by default. |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 1517 | TextDiagClient = new TextDiagnosticPrinter(llvm::errs(), |
| 1518 | !NoShowColumn, |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1519 | !NoCaretDiagnostics); |
| 1520 | } else { |
| 1521 | // When checking diagnostics, just buffer them up. |
| 1522 | TextDiagClient = new TextDiagnosticBuffer(); |
| 1523 | |
| 1524 | if (InputFilenames.size() != 1) { |
| 1525 | fprintf(stderr, |
| 1526 | "-verify only works on single input files for now.\n"); |
| 1527 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1528 | } |
| 1529 | } |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1530 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1531 | // Configure our handling of diagnostics. |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1532 | llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient); |
| 1533 | Diagnostic Diags(DiagClient.get()); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1534 | InitializeDiagnostics(Diags); |
| 1535 | |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1536 | // -I- is a deprecated GCC feature, scan for it and reject it. |
| 1537 | for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) { |
| 1538 | if (I_dirs[i] == "-") { |
Chris Lattner | 5917fe1 | 2008-11-18 05:05:28 +0000 | [diff] [blame] | 1539 | Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported); |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1540 | I_dirs.erase(I_dirs.begin()+i); |
| 1541 | --i; |
| 1542 | } |
| 1543 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Get information about the target being compiled for. |
| 1546 | std::string Triple = CreateTargetTriple(); |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1547 | llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple)); |
| 1548 | |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1549 | if (Target == 0) { |
| 1550 | fprintf(stderr, "Sorry, I don't know what target this is: %s\n", |
| 1551 | Triple.c_str()); |
| 1552 | fprintf(stderr, "Please use -triple or -arch.\n"); |
| 1553 | exit(1); |
| 1554 | } |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1555 | |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 1556 | if (!InheritanceViewCls.empty()) // C++ visualization? |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1557 | ProgAction = InheritanceView; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1558 | |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1559 | llvm::OwningPtr<SourceManager> SourceMgr; |
| 1560 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1561 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1562 | const std::string &InFile = InputFilenames[i]; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1563 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1564 | if (isSerializedFile(InFile)) { |
| 1565 | Diags.setClient(TextDiagClient); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1566 | ProcessSerializedFile(InFile,Diags,FileMgr); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1567 | } |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1568 | else { |
| 1569 | /// Create a SourceManager object. This tracks and owns all the file |
| 1570 | /// buffers allocated to a translation unit. |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1571 | if (!SourceMgr) |
| 1572 | SourceMgr.reset(new SourceManager()); |
| 1573 | else |
| 1574 | SourceMgr->clearIDTables(); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1575 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1576 | // Initialize language options, inferring file types from input filenames. |
| 1577 | LangOptions LangInfo; |
| 1578 | InitializeBaseLanguage(); |
| 1579 | LangKind LK = GetLanguage(InFile); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1580 | bool PCH = InitializeLangOptions(LangInfo, LK); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 1581 | InitializeGCMode(LangInfo); |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 1582 | InitializeLanguageStandard(LangInfo, LK, Target.get()); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1583 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1584 | // Process the -I options and set them in the HeaderInfo. |
| 1585 | HeaderSearch HeaderInfo(FileMgr); |
Ted Kremenek | c68ecb5 | 2008-06-06 01:47:30 +0000 | [diff] [blame] | 1586 | |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1587 | InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1588 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1589 | // Set up the preprocessor with these options. |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1590 | DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target, |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1591 | *SourceMgr.get(), HeaderInfo); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1592 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1593 | llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor()); |
| 1594 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1595 | if (!PP) |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1596 | continue; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1597 | |
| 1598 | // Create the HTMLDiagnosticsClient if we are using one. Otherwise, |
| 1599 | // always reset to using TextDiagClient. |
| 1600 | llvm::OwningPtr<DiagnosticClient> TmpClient; |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1601 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1602 | if (!HTMLDiag.empty()) { |
| 1603 | TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(), |
| 1604 | &PPFactory)); |
| 1605 | Diags.setClient(TmpClient.get()); |
| 1606 | } |
| 1607 | else |
| 1608 | Diags.setClient(TextDiagClient); |
| 1609 | |
| 1610 | // Process the source file. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1611 | ProcessInputFile(*PP, PPFactory, InFile, PCH ? GeneratePCH : ProgAction); |
| 1612 | |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1613 | HeaderInfo.ClearFileInfo(); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1614 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1615 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1616 | |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1617 | if (unsigned NumDiagnostics = Diags.getNumDiagnostics()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1618 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 1619 | (NumDiagnostics == 1 ? "" : "s")); |
| 1620 | |
| 1621 | if (Stats) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1622 | FileMgr.PrintStats(); |
| 1623 | fprintf(stderr, "\n"); |
| 1624 | } |
| 1625 | |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1626 | // If verifying diagnostics and we reached here, all is well. |
| 1627 | if (VerifyDiagnostics) |
| 1628 | return 0; |
| 1629 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 1630 | // Managed static deconstruction. Useful for making things like |
| 1631 | // -time-passes usable. |
| 1632 | llvm::llvm_shutdown(); |
| 1633 | |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1634 | return HadErrors || (Diags.getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1635 | } |