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: |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 11 | // clang-cc --help - Output help info. |
| 12 | // clang-cc [options] - Read from stdin. |
| 13 | // clang-cc [options] file - Read from "file". |
| 14 | // clang-cc [options] file1 file2 - Read these files. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | |
Daniel Dunbar | 0498cfc | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 18 | #include "Options.h" |
Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Basic/TargetInfo.h" |
| 22 | #include "clang/Basic/Version.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 8863b98 | 2009-11-07 04:20:15 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/PathDiagnosticClients.h" |
| 28 | #include "clang/Frontend/PreprocessorOptions.h" |
Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/PreprocessorOutputOptions.h" |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 32 | #include "llvm/Config/config.h" |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 33 | #include "llvm/LLVMContext.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ManagedStatic.h" |
Zhongxing Xu | 2092236 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 37 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 38 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Timer.h" |
Chris Lattner | 0fa0daa | 2009-08-24 04:11:30 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 41 | #include "llvm/System/Host.h" |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 42 | #include "llvm/System/Path.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 43 | #include "llvm/System/Signals.h" |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetSelect.h" |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 45 | #include <cstdlib> |
Douglas Gregor | 44cf08e | 2009-05-03 03:52:38 +0000 | [diff] [blame] | 46 | #if HAVE_SYS_TYPES_H |
Douglas Gregor | 68a0d78 | 2009-05-02 00:03:46 +0000 | [diff] [blame] | 47 | # include <sys/types.h> |
| 48 | #endif |
Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 49 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | using namespace clang; |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | aa57614 | 2009-11-12 15:23:20 +0000 | [diff] [blame] | 53 | // Frontend Actions |
| 54 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | |
| 56 | enum ProgActions { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 57 | RewriteObjC, // ObjC->C Rewriter. |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 58 | RewriteBlocks, // ObjC->C Rewriter for Blocks. |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 59 | RewriteMacros, // Expand macros but not #includes. |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 60 | RewriteTest, // Rewriter playground |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 61 | HTMLTest, // HTML displayer testing stuff. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 62 | EmitAssembly, // Emit a .s file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | EmitLLVM, // Emit a .ll file. |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 64 | EmitBC, // Emit a .bc file. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | EmitLLVMOnly, // Generate LLVM IR, but do not |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 66 | EmitHTML, // Translate input source into HTML. |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 67 | ASTPrint, // Parse ASTs and print them. |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 68 | ASTPrintXML, // Parse ASTs and print them in XML. |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 69 | ASTDump, // Parse ASTs and dump them. |
| 70 | ASTView, // Parse ASTs and view them in Graphviz. |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 71 | PrintDeclContext, // Print DeclContext and their Decls. |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 72 | DumpRecordLayouts, // Dump record layout information. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | ParsePrintCallbacks, // Parse and print each callback. |
| 74 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
Daniel Dunbar | 79b55f9 | 2009-11-14 04:39:29 +0000 | [diff] [blame] | 75 | FixIt, // Parse and apply any fixits to the source. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | ParseNoop, // Parse with noop callbacks. |
| 77 | RunPreprocessorOnly, // Just lex, no output. |
| 78 | PrintPreprocessedInput, // -E mode. |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 79 | DumpTokens, // Dump out preprocessed tokens. |
| 80 | DumpRawTokens, // Dump out raw tokens. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | RunAnalysis, // Run one or more source code analyses. |
Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 82 | GeneratePTH, // Generate pre-tokenized header. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 83 | GeneratePCH, // Generate pre-compiled header. |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 84 | InheritanceView // View C++ inheritance for a specified class. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | static llvm::cl::opt<ProgActions> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 89 | llvm::cl::init(ParseSyntaxOnly), |
| 90 | llvm::cl::values( |
| 91 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 92 | "Just run preprocessor, no output (for timings)"), |
| 93 | clEnumValN(PrintPreprocessedInput, "E", |
| 94 | "Run preprocessor, emit preprocessed file"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 95 | clEnumValN(DumpRawTokens, "dump-raw-tokens", |
| 96 | "Lex file in raw mode and dump raw tokens"), |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 97 | clEnumValN(RunAnalysis, "analyze", |
| 98 | "Run static analysis engine"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 99 | clEnumValN(DumpTokens, "dump-tokens", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | "Run preprocessor, dump internal rep of tokens"), |
| 101 | clEnumValN(ParseNoop, "parse-noop", |
| 102 | "Run parser with noop callbacks (for timings)"), |
| 103 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 104 | "Run parser and perform semantic analysis"), |
Daniel Dunbar | 79b55f9 | 2009-11-14 04:39:29 +0000 | [diff] [blame] | 105 | clEnumValN(FixIt, "fixit", |
| 106 | "Apply fix-it advice to the input source"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 108 | "Run parser and print each callback invoked"), |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 109 | clEnumValN(EmitHTML, "emit-html", |
| 110 | "Output input source as HTML"), |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 111 | clEnumValN(ASTPrint, "ast-print", |
| 112 | "Build ASTs and then pretty-print them"), |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 113 | clEnumValN(ASTPrintXML, "ast-print-xml", |
| 114 | "Build ASTs and then print them in XML format"), |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 115 | clEnumValN(ASTDump, "ast-dump", |
| 116 | "Build ASTs and then debug dump them"), |
Chris Lattner | ea254db | 2007-10-11 00:37:43 +0000 | [diff] [blame] | 117 | clEnumValN(ASTView, "ast-view", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 118 | "Build ASTs and view them with GraphViz"), |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 119 | clEnumValN(PrintDeclContext, "print-decl-contexts", |
Ted Kremenek | 08478eb | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 120 | "Print DeclContexts and their Decls"), |
Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 121 | clEnumValN(DumpRecordLayouts, "dump-record-layouts", |
| 122 | "Dump record layout information"), |
Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 123 | clEnumValN(GeneratePTH, "emit-pth", |
Ted Kremenek | 08478eb | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 124 | "Generate pre-tokenized header file"), |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 125 | clEnumValN(GeneratePCH, "emit-pch", |
| 126 | "Generate pre-compiled header file"), |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 127 | clEnumValN(EmitAssembly, "S", |
| 128 | "Emit native assembly code"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | clEnumValN(EmitLLVM, "emit-llvm", |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 130 | "Build ASTs then convert to LLVM, emit .ll file"), |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 131 | clEnumValN(EmitBC, "emit-llvm-bc", |
| 132 | "Build ASTs then convert to LLVM, emit .bc file"), |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 133 | clEnumValN(EmitLLVMOnly, "emit-llvm-only", |
| 134 | "Build ASTs and convert to LLVM, discarding output"), |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 135 | clEnumValN(RewriteTest, "rewrite-test", |
| 136 | "Rewriter playground"), |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 137 | clEnumValN(RewriteObjC, "rewrite-objc", |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 138 | "Rewrite ObjC into C (code rewriter example)"), |
| 139 | clEnumValN(RewriteMacros, "rewrite-macros", |
| 140 | "Expand macros without full preprocessing"), |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 141 | clEnumValN(RewriteBlocks, "rewrite-blocks", |
| 142 | "Rewrite Blocks to C"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | clEnumValEnd)); |
| 144 | |
Daniel Dunbar | aa57614 | 2009-11-12 15:23:20 +0000 | [diff] [blame] | 145 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 914474c | 2009-11-13 01:02:10 +0000 | [diff] [blame] | 146 | // Utility Methods |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | //===----------------------------------------------------------------------===// |
| 148 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 149 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 150 | llvm::sys::Path P = |
| 151 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 152 | (void*)(intptr_t) GetBuiltinIncludePath); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 153 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 154 | if (!P.isEmpty()) { |
| 155 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 156 | P.eraseComponent(); // Remove /bin from foo/bin |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 157 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 158 | // Get foo/lib/clang/<version>/include |
| 159 | P.appendComponent("lib"); |
| 160 | P.appendComponent("clang"); |
| 161 | P.appendComponent(CLANG_VERSION_STRING); |
| 162 | P.appendComponent("include"); |
| 163 | } |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 164 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 165 | return P.str(); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | // Main driver |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | |
Daniel Dunbar | aa57614 | 2009-11-12 15:23:20 +0000 | [diff] [blame] | 172 | /// ClangFrontendTimer - The front-end activities should charge time to it with |
| 173 | /// TimeRegion. The -ftime-report option controls whether this will do |
| 174 | /// anything. |
| 175 | llvm::Timer *ClangFrontendTimer = 0; |
| 176 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 177 | static FrontendAction *CreateFrontendAction(ProgActions PA) { |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 178 | switch (PA) { |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 179 | default: return 0; |
| 180 | case ASTDump: return new ASTDumpAction(); |
| 181 | case ASTPrint: return new ASTPrintAction(); |
| 182 | case ASTPrintXML: return new ASTPrintXMLAction(); |
| 183 | case ASTView: return new ASTViewAction(); |
| 184 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 185 | case DumpRecordLayouts: return new DumpRecordAction(); |
| 186 | case DumpTokens: return new DumpTokensAction(); |
| 187 | case EmitAssembly: return new EmitAssemblyAction(); |
| 188 | case EmitBC: return new EmitBCAction(); |
| 189 | case EmitHTML: return new HTMLPrintAction(); |
| 190 | case EmitLLVM: return new EmitLLVMAction(); |
| 191 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 192 | case FixIt: return new FixItAction(); |
| 193 | case GeneratePCH: return new GeneratePCHAction(); |
| 194 | case GeneratePTH: return new GeneratePTHAction(); |
| 195 | case InheritanceView: return new InheritanceViewAction(); |
| 196 | case ParseNoop: return new ParseOnlyAction(); |
| 197 | case ParsePrintCallbacks: return new PrintParseAction(); |
| 198 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
| 199 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 200 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 201 | case RewriteBlocks: return new RewriteBlocksAction(); |
| 202 | case RewriteMacros: return new RewriteMacrosAction(); |
| 203 | case RewriteObjC: return new RewriteObjCAction(); |
| 204 | case RewriteTest: return new RewriteTestAction(); |
| 205 | case RunAnalysis: return new AnalysisAction(); |
| 206 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 207 | } |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 210 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 211 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 212 | |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 213 | Diags.Report(diag::err_fe_error_backend) << Message; |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 214 | |
| 215 | // We cannot recover from llvm errors. |
| 216 | exit(1); |
| 217 | } |
| 218 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 219 | static TargetInfo * |
| 220 | ConstructCompilerInvocation(CompilerInvocation &Opts, Diagnostic &Diags, |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 221 | const char *Argv0, bool &IsAST) { |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 222 | // Initialize frontend options. |
| 223 | InitializeFrontendOptions(Opts.getFrontendOpts()); |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 224 | |
Daniel Dunbar | f819b25 | 2009-11-13 05:52:19 +0000 | [diff] [blame] | 225 | // FIXME: The target information in frontend options should be split out into |
| 226 | // TargetOptions, and the target options in codegen options should move there |
| 227 | // as well. Then we could properly initialize in layering order. |
| 228 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 229 | // Initialize base triple. If a -triple option has been specified, use |
| 230 | // that triple. Otherwise, default to the host triple. |
| 231 | llvm::Triple Triple(Opts.getFrontendOpts().TargetTriple); |
| 232 | if (Triple.getTriple().empty()) |
| 233 | Triple = llvm::Triple(llvm::sys::getHostTriple()); |
| 234 | |
| 235 | // Get information about the target being compiled for. |
| 236 | TargetInfo *Target = TargetInfo::CreateTargetInfo(Triple.getTriple()); |
| 237 | if (!Target) { |
| 238 | Diags.Report(diag::err_fe_unknown_triple) << Triple.getTriple().c_str(); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | // Set the target ABI if specified. |
| 243 | if (!Opts.getFrontendOpts().TargetABI.empty() && |
| 244 | !Target->setABI(Opts.getFrontendOpts().TargetABI)) { |
| 245 | Diags.Report(diag::err_fe_unknown_target_abi) |
| 246 | << Opts.getFrontendOpts().TargetABI; |
| 247 | return 0; |
| 248 | } |
| 249 | |
Daniel Dunbar | fcb0c3b | 2009-11-10 18:47:35 +0000 | [diff] [blame] | 250 | // Initialize backend options, which may also be used to key some language |
| 251 | // options. |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 252 | InitializeCodeGenOptions(Opts.getCodeGenOpts(), *Target); |
Daniel Dunbar | fcb0c3b | 2009-11-10 18:47:35 +0000 | [diff] [blame] | 253 | |
Daniel Dunbar | fbe2faf | 2009-11-13 02:06:12 +0000 | [diff] [blame] | 254 | // Determine the input language, we currently require all files to match. |
| 255 | FrontendOptions::InputKind IK = Opts.getFrontendOpts().Inputs[0].first; |
| 256 | for (unsigned i = 1, e = Opts.getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 257 | if (Opts.getFrontendOpts().Inputs[i].first != IK) { |
| 258 | llvm::errs() << "error: cannot have multiple input files of distinct " |
| 259 | << "language kinds without -x\n"; |
| 260 | return 0; |
| 261 | } |
| 262 | } |
| 263 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 264 | // Initialize language options. |
Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 265 | // |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 266 | // FIXME: These aren't used during operations on ASTs. Split onto a separate |
| 267 | // code path to make this obvious. |
Daniel Dunbar | fbe2faf | 2009-11-13 02:06:12 +0000 | [diff] [blame] | 268 | IsAST = (IK == FrontendOptions::IK_AST); |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 269 | if (!IsAST) |
Daniel Dunbar | fbe2faf | 2009-11-13 02:06:12 +0000 | [diff] [blame] | 270 | InitializeLangOptions(Opts.getLangOpts(), IK, *Target, |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 271 | Opts.getCodeGenOpts()); |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 272 | |
Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 273 | // Initialize the static analyzer options. |
| 274 | InitializeAnalyzerOptions(Opts.getAnalyzerOpts()); |
| 275 | |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 276 | // Initialize the dependency output options (-M...). |
| 277 | InitializeDependencyOutputOptions(Opts.getDependencyOutputOpts()); |
| 278 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 279 | // Initialize the header search options. |
Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame] | 280 | InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(), |
| 281 | GetBuiltinIncludePath(Argv0), |
Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame] | 282 | Opts.getLangOpts()); |
Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 283 | |
| 284 | // Initialize the other preprocessor options. |
| 285 | InitializePreprocessorOptions(Opts.getPreprocessorOpts()); |
Daniel Dunbar | 36f4ec3 | 2009-11-10 16:19:45 +0000 | [diff] [blame] | 286 | |
Daniel Dunbar | 29cf746 | 2009-11-11 10:07:44 +0000 | [diff] [blame] | 287 | // Initialize the preprocessed output options. |
| 288 | InitializePreprocessorOutputOptions(Opts.getPreprocessorOutputOpts()); |
| 289 | |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 290 | // Finalize some code generation options which are derived from other places. |
| 291 | if (Opts.getLangOpts().NoBuiltin) |
| 292 | Opts.getCodeGenOpts().SimplifyLibCalls = 0; |
| 293 | if (Opts.getLangOpts().CPlusPlus) |
| 294 | Opts.getCodeGenOpts().NoCommon = 1; |
| 295 | Opts.getCodeGenOpts().TimePasses = Opts.getFrontendOpts().ShowTimers; |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 296 | |
| 297 | return Target; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | int main(int argc, char **argv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 301 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 302 | llvm::PrettyStackTraceProgram X(argc, argv); |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 303 | CompilerInstance Clang(&llvm::getGlobalContext(), false); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 304 | |
Daniel Dunbar | 4d86151 | 2009-09-03 04:54:12 +0000 | [diff] [blame] | 305 | // Initialize targets first, so that --version shows registered targets. |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 306 | llvm::InitializeAllTargets(); |
| 307 | llvm::InitializeAllAsmPrinters(); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 308 | |
| 309 | llvm::cl::ParseCommandLineOptions(argc, argv, |
| 310 | "LLVM 'Clang' Compiler: http://clang.llvm.org\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 312 | // Construct the diagnostic engine first, so that we can build a diagnostic |
| 313 | // client to use for any errors during option handling. |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 314 | InitializeDiagnosticOptions(Clang.getDiagnosticOpts()); |
| 315 | Clang.createDiagnostics(argc, argv); |
Daniel Dunbar | 704e48a | 2009-11-13 08:20:57 +0000 | [diff] [blame] | 316 | if (!Clang.hasDiagnostics()) |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 317 | return 1; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 318 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 319 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 320 | // error handler. |
| 321 | llvm::llvm_install_error_handler(LLVMErrorHandler, |
| 322 | static_cast<void*>(&Clang.getDiagnostics())); |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 323 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 324 | // Now that we have initialized the diagnostics engine, create the target and |
| 325 | // the compiler invocation object. |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 326 | // |
| 327 | // FIXME: We should move .ast inputs to taking a separate path, they are |
| 328 | // really quite different. |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 329 | bool IsAST; |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 330 | Clang.setTarget( |
| 331 | ConstructCompilerInvocation(Clang.getInvocation(), Clang.getDiagnostics(), |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 332 | argv[0], IsAST)); |
Daniel Dunbar | 704e48a | 2009-11-13 08:20:57 +0000 | [diff] [blame] | 333 | if (!Clang.hasTarget()) |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 334 | return 1; |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 335 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 336 | // Validate/process some options |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 337 | if (Clang.getHeaderSearchOpts().Verbose) |
Daniel Dunbar | 1417c74 | 2009-11-12 23:52:46 +0000 | [diff] [blame] | 338 | llvm::errs() << "clang-cc version " CLANG_VERSION_STRING |
| 339 | << " based upon " << PACKAGE_STRING |
| 340 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 341 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 342 | if (Clang.getFrontendOpts().ShowTimers) |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 343 | ClangFrontendTimer = new llvm::Timer("Clang front-end time"); |
| 344 | |
Daniel Dunbar | 79b55f9 | 2009-11-14 04:39:29 +0000 | [diff] [blame] | 345 | // Enforce certain implications. |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 346 | if (!Clang.getFrontendOpts().ViewClassInheritance.empty()) |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 347 | ProgAction = InheritanceView; |
Daniel Dunbar | 79b55f9 | 2009-11-14 04:39:29 +0000 | [diff] [blame] | 348 | if (!Clang.getFrontendOpts().FixItLocations.empty()) |
| 349 | ProgAction = FixIt; |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 350 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 351 | for (unsigned i = 0, e = Clang.getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 352 | const std::string &InFile = Clang.getFrontendOpts().Inputs[i].second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 354 | // If we aren't using an AST file, setup the file and source managers and |
| 355 | // the preprocessor. |
| 356 | if (!IsAST) { |
| 357 | if (!i) { |
| 358 | // Create a file manager object to provide access to and cache the |
| 359 | // filesystem. |
| 360 | Clang.createFileManager(); |
| 361 | |
| 362 | // Create the source manager. |
| 363 | Clang.createSourceManager(); |
| 364 | } else { |
| 365 | // Reset the ID tables if we are reusing the SourceManager. |
| 366 | Clang.getSourceManager().clearIDTables(); |
| 367 | } |
| 368 | |
| 369 | // Create the preprocessor. |
| 370 | Clang.createPreprocessor(); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 373 | llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(ProgAction)); |
| 374 | assert(Act && "Invalid program action!"); |
| 375 | Act->setCurrentTimer(ClangFrontendTimer); |
| 376 | if (Act->BeginSourceFile(Clang, InFile, IsAST)) { |
| 377 | Act->Execute(); |
| 378 | Act->EndSourceFile(); |
| 379 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 381 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 382 | if (Clang.getDiagnosticOpts().ShowCarets) |
| 383 | if (unsigned NumDiagnostics = Clang.getDiagnostics().getNumDiagnostics()) |
Mike Stump | fc0fed3 | 2009-04-28 01:19:10 +0000 | [diff] [blame] | 384 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 385 | (NumDiagnostics == 1 ? "" : "s")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 387 | if (Clang.getFrontendOpts().ShowStats) { |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 388 | Clang.getFileManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | fprintf(stderr, "\n"); |
| 390 | } |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 391 | |
| 392 | delete ClangFrontendTimer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 394 | // Return the appropriate status when verifying diagnostics. |
| 395 | // |
| 396 | // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need |
| 397 | // this. |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 398 | if (Clang.getDiagnosticOpts().VerifyDiagnostics) |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 399 | return static_cast<VerifyDiagnosticsClient&>( |
| 400 | Clang.getDiagnosticClient()).HadErrors(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 402 | // Managed static deconstruction. Useful for making things like |
| 403 | // -time-passes usable. |
| 404 | llvm::llvm_shutdown(); |
| 405 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 406 | return (Clang.getDiagnostics().getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | } |