Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 1 | //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===// |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 10 | #include "clang/CodeGen/CodeGenAction.h" |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclGroup.h" |
Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 18 | #include "clang/CodeGen/BackendUtil.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 19 | #include "clang/CodeGen/ModuleBuilder.h" |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | acadc55 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/FrontendDiagnostic.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
| 23 | #include "llvm/Bitcode/ReaderWriter.h" |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DebugInfo.h" |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DiagnosticInfo.h" |
| 26 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 27 | #include "llvm/IR/LLVMContext.h" |
| 28 | #include "llvm/IR/Module.h" |
Chandler Carruth | b45836a | 2013-03-26 02:25:54 +0000 | [diff] [blame] | 29 | #include "llvm/IRReader/IRReader.h" |
Chandler Carruth | 00fa3f7 | 2014-03-06 03:46:44 +0000 | [diff] [blame] | 30 | #include "llvm/Linker/Linker.h" |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
| 33 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Timer.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 35 | #include <memory> |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | using namespace llvm; |
| 38 | |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 39 | namespace clang { |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 40 | class BackendConsumer : public ASTConsumer { |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 41 | virtual void anchor(); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 42 | DiagnosticsEngine &Diags; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 43 | BackendAction Action; |
Daniel Dunbar | de18224 | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 44 | const CodeGenOptions &CodeGenOpts; |
Daniel Dunbar | de18224 | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 45 | const TargetOptions &TargetOpts; |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 46 | const LangOptions &LangOpts; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 47 | raw_ostream *AsmOutStream; |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 48 | ASTContext *Context; |
Daniel Dunbar | b3a36cf | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 50 | Timer LLVMIRGeneration; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 52 | std::unique_ptr<CodeGenerator> Gen; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 54 | std::unique_ptr<llvm::Module> TheModule, LinkModule; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 55 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | public: |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 57 | BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags, |
Daniel Dunbar | 6d5824f | 2010-06-07 23:19:17 +0000 | [diff] [blame] | 58 | const CodeGenOptions &compopts, |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 59 | const TargetOptions &targetopts, |
Rafael Espindola | 666a2ab | 2013-12-18 16:38:48 +0000 | [diff] [blame] | 60 | const LangOptions &langopts, bool TimePasses, |
| 61 | const std::string &infile, llvm::Module *LinkModule, |
| 62 | raw_ostream *OS, LLVMContext &C) |
| 63 | : Diags(_Diags), Action(action), CodeGenOpts(compopts), |
| 64 | TargetOpts(targetopts), LangOpts(langopts), AsmOutStream(OS), |
| 65 | Context(), LLVMIRGeneration("LLVM IR Generation Time"), |
| 66 | Gen(CreateLLVMCodeGen(Diags, infile, compopts, targetopts, C)), |
| 67 | LinkModule(LinkModule) { |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 68 | llvm::TimePassesIsEnabled = TimePasses; |
Chris Lattner | deffa13 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 69 | } |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 70 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 71 | llvm::Module *takeModule() { return TheModule.release(); } |
| 72 | llvm::Module *takeLinkModule() { return LinkModule.release(); } |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 73 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 74 | void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override { |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 75 | Gen->HandleCXXStaticMemberVarInstantiation(VD); |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 78 | void Initialize(ASTContext &Ctx) override { |
Chris Lattner | 5cf49fe | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 79 | Context = &Ctx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 81 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 82 | LLVMIRGeneration.startTimer(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 5cf49fe | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 84 | Gen->Initialize(Ctx); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 85 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 86 | TheModule.reset(Gen->GetModule()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 88 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 89 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 90 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 92 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 93 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 94 | Context->getSourceManager(), |
| 95 | "LLVM IR generation of declaration"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 97 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 98 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 99 | |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 100 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 101 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 102 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 103 | LLVMIRGeneration.stopTimer(); |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 104 | |
| 105 | return true; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 106 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 108 | void HandleInlineMethodDefinition(CXXMethodDecl *D) override { |
| 109 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 110 | Context->getSourceManager(), |
| 111 | "LLVM IR generation of inline method"); |
| 112 | if (llvm::TimePassesIsEnabled) |
| 113 | LLVMIRGeneration.startTimer(); |
| 114 | |
| 115 | Gen->HandleInlineMethodDefinition(D); |
| 116 | |
| 117 | if (llvm::TimePassesIsEnabled) |
| 118 | LLVMIRGeneration.stopTimer(); |
| 119 | } |
| 120 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 121 | void HandleTranslationUnit(ASTContext &C) override { |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 122 | { |
Chris Lattner | e46de75 | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 123 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 124 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 125 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 126 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 127 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | a94d873 | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 128 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 129 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 130 | LLVMIRGeneration.stopTimer(); |
| 131 | } |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 132 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 133 | // Silently ignore if we weren't initialized for some reason. |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 134 | if (!TheModule) |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 135 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 137 | // Make sure IR generation is happy with the module. This is released by |
| 138 | // the module provider. |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 139 | llvm::Module *M = Gen->ReleaseModule(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 140 | if (!M) { |
| 141 | // The module has been released by IR gen on failures, do not double |
| 142 | // free. |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 143 | TheModule.release(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
| 147 | assert(TheModule.get() == M && |
| 148 | "Unexpected module change during IR generation"); |
| 149 | |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 150 | // Link LinkModule into this module if present, preserving its validity. |
| 151 | if (LinkModule) { |
| 152 | std::string ErrorMsg; |
| 153 | if (Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource, |
| 154 | &ErrorMsg)) { |
| 155 | Diags.Report(diag::err_fe_cannot_link_module) |
| 156 | << LinkModule->getModuleIdentifier() << ErrorMsg; |
| 157 | return; |
| 158 | } |
| 159 | } |
| 160 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 161 | // Install an inline asm handler so that diagnostics get printed through |
| 162 | // our diagnostics hooks. |
| 163 | LLVMContext &Ctx = TheModule->getContext(); |
Chris Lattner | 068f2ab | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 164 | LLVMContext::InlineAsmDiagHandlerTy OldHandler = |
| 165 | Ctx.getInlineAsmDiagnosticHandler(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 166 | void *OldContext = Ctx.getInlineAsmDiagnosticContext(); |
Chris Lattner | 068f2ab | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 167 | Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 168 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 169 | LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler = |
| 170 | Ctx.getDiagnosticHandler(); |
| 171 | void *OldDiagnosticContext = Ctx.getDiagnosticContext(); |
| 172 | Ctx.setDiagnosticHandler(DiagnosticHandler, this); |
| 173 | |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 174 | EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
Alp Toker | e83b906 | 2014-01-02 15:08:04 +0000 | [diff] [blame] | 175 | C.getTargetInfo().getTargetDescription(), |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 176 | TheModule.get(), Action, AsmOutStream); |
Rafael Espindola | 666a2ab | 2013-12-18 16:38:48 +0000 | [diff] [blame] | 177 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 178 | Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 179 | |
| 180 | Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 181 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 183 | void HandleTagDeclDefinition(TagDecl *D) override { |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 184 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 185 | Context->getSourceManager(), |
| 186 | "LLVM IR generation of declaration"); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 187 | Gen->HandleTagDeclDefinition(D); |
| 188 | } |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 189 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 190 | void HandleTagDeclRequiredDefinition(const TagDecl *D) override { |
David Blaikie | 48ad6dc | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 191 | Gen->HandleTagDeclRequiredDefinition(D); |
| 192 | } |
| 193 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 194 | void CompleteTentativeDefinition(VarDecl *D) override { |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 195 | Gen->CompleteTentativeDefinition(D); |
| 196 | } |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 197 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 198 | void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 199 | Gen->HandleVTable(RD, DefinitionRequired); |
| 200 | } |
| 201 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 202 | void HandleLinkerOptionPragma(llvm::StringRef Opts) override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 203 | Gen->HandleLinkerOptionPragma(Opts); |
| 204 | } |
| 205 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 206 | void HandleDetectMismatch(llvm::StringRef Name, |
| 207 | llvm::StringRef Value) override { |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 208 | Gen->HandleDetectMismatch(Name, Value); |
| 209 | } |
| 210 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 211 | void HandleDependentLibrary(llvm::StringRef Opts) override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 212 | Gen->HandleDependentLibrary(Opts); |
| 213 | } |
| 214 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 215 | static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context, |
| 216 | unsigned LocCookie) { |
| 217 | SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie); |
| 218 | ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc); |
| 219 | } |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 220 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 221 | static void DiagnosticHandler(const llvm::DiagnosticInfo &DI, |
| 222 | void *Context) { |
| 223 | ((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI); |
| 224 | } |
| 225 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 226 | void InlineAsmDiagHandler2(const llvm::SMDiagnostic &, |
| 227 | SourceLocation LocCookie); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 228 | |
| 229 | void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI); |
| 230 | /// \brief Specialized handler for InlineAsm diagnostic. |
| 231 | /// \return True if the diagnostic has been successfully reported, false |
| 232 | /// otherwise. |
| 233 | bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D); |
| 234 | /// \brief Specialized handler for StackSize diagnostic. |
| 235 | /// \return True if the diagnostic has been successfully reported, false |
| 236 | /// otherwise. |
| 237 | bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 238 | /// \brief Specialized handler for the optimization diagnostic. |
| 239 | /// Note that this handler only accepts remarks and it always handles |
| 240 | /// them. |
| 241 | void |
| 242 | OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark &D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | }; |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 244 | |
| 245 | void BackendConsumer::anchor() {} |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 248 | /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr |
| 249 | /// buffer to be a valid FullSourceLoc. |
| 250 | static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, |
| 251 | SourceManager &CSM) { |
| 252 | // Get both the clang and llvm source managers. The location is relative to |
| 253 | // a memory buffer that the LLVM Source Manager is handling, we need to add |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 254 | // a copy to the Clang source manager. |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 255 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 257 | // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr |
| 258 | // already owns its one and clang::SourceManager wants to own its one. |
| 259 | const MemoryBuffer *LBuf = |
| 260 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 262 | // Create the copy and transfer ownership to clang::SourceManager. |
| 263 | llvm::MemoryBuffer *CBuf = |
| 264 | llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(), |
| 265 | LBuf->getBufferIdentifier()); |
Alp Toker | 6ac2cd0 | 2014-05-16 17:23:01 +0000 | [diff] [blame] | 266 | FileID FID = CSM.createFileID(CBuf); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 268 | // Translate the offset into the file. |
| 269 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 270 | SourceLocation NewLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 271 | CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset); |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 272 | return FullSourceLoc(NewLoc, CSM); |
| 273 | } |
| 274 | |
Chris Lattner | 6d67213 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 276 | /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an |
| 277 | /// error parsing inline asm. The SMDiagnostic indicates the error relative to |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 278 | /// the temporary memory buffer that the inline asm parser has set up. |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 279 | void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D, |
| 280 | SourceLocation LocCookie) { |
| 281 | // There are a couple of different kinds of errors we could get here. First, |
| 282 | // we re-format the SMDiagnostic in terms of a clang diagnostic. |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 283 | |
| 284 | // Strip "error: " off the start of the message string. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 285 | StringRef Message = D.getMessage(); |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 286 | if (Message.startswith("error: ")) |
| 287 | Message = Message.substr(7); |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 288 | |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 289 | // If the SMDiagnostic has an inline asm source location, translate it. |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 290 | FullSourceLoc Loc; |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 291 | if (D.getLoc() != SMLoc()) |
| 292 | Loc = ConvertBackendLocation(D, Context->getSourceManager()); |
Chris Lattner | f4a4bec | 2012-01-31 06:13:55 +0000 | [diff] [blame] | 293 | |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 294 | |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 295 | // If this problem has clang-level source location information, report the |
| 296 | // issue as being an error in the source with a note showing the instantiated |
| 297 | // code. |
| 298 | if (LocCookie.isValid()) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 299 | Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message); |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 300 | |
Benjamin Kramer | e06b2b7 | 2011-10-16 10:48:28 +0000 | [diff] [blame] | 301 | if (D.getLoc().isValid()) { |
| 302 | DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here); |
| 303 | // Convert the SMDiagnostic ranges into SourceRange and attach them |
| 304 | // to the diagnostic. |
| 305 | for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) { |
| 306 | std::pair<unsigned, unsigned> Range = D.getRanges()[i]; |
| 307 | unsigned Column = D.getColumnNo(); |
| 308 | B << SourceRange(Loc.getLocWithOffset(Range.first - Column), |
| 309 | Loc.getLocWithOffset(Range.second - Column)); |
| 310 | } |
| 311 | } |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 312 | return; |
| 313 | } |
| 314 | |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 315 | // Otherwise, report the backend error as occurring in the generated .s file. |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 316 | // If Loc is invalid, we still need to report the error, it just gets no |
| 317 | // location info. |
| 318 | Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message); |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 321 | #define ComputeDiagID(Severity, GroupName, DiagID) \ |
| 322 | do { \ |
| 323 | switch (Severity) { \ |
| 324 | case llvm::DS_Error: \ |
| 325 | DiagID = diag::err_fe_##GroupName; \ |
| 326 | break; \ |
| 327 | case llvm::DS_Warning: \ |
| 328 | DiagID = diag::warn_fe_##GroupName; \ |
| 329 | break; \ |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 330 | case llvm::DS_Remark: \ |
| 331 | llvm_unreachable("'remark' severity not expected"); \ |
| 332 | break; \ |
| 333 | case llvm::DS_Note: \ |
| 334 | DiagID = diag::note_fe_##GroupName; \ |
| 335 | break; \ |
| 336 | } \ |
| 337 | } while (false) |
| 338 | |
| 339 | #define ComputeDiagRemarkID(Severity, GroupName, DiagID) \ |
| 340 | do { \ |
| 341 | switch (Severity) { \ |
| 342 | case llvm::DS_Error: \ |
| 343 | DiagID = diag::err_fe_##GroupName; \ |
| 344 | break; \ |
| 345 | case llvm::DS_Warning: \ |
| 346 | DiagID = diag::warn_fe_##GroupName; \ |
| 347 | break; \ |
| 348 | case llvm::DS_Remark: \ |
| 349 | DiagID = diag::remark_fe_##GroupName; \ |
| 350 | break; \ |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 351 | case llvm::DS_Note: \ |
| 352 | DiagID = diag::note_fe_##GroupName; \ |
| 353 | break; \ |
| 354 | } \ |
| 355 | } while (false) |
| 356 | |
| 357 | bool |
| 358 | BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) { |
| 359 | unsigned DiagID; |
| 360 | ComputeDiagID(D.getSeverity(), inline_asm, DiagID); |
| 361 | std::string Message = D.getMsgStr().str(); |
| 362 | |
| 363 | // If this problem has clang-level source location information, report the |
Tobias Grosser | bd25beb | 2014-02-26 10:21:56 +0000 | [diff] [blame] | 364 | // issue as being a problem in the source with a note showing the instantiated |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 365 | // code. |
| 366 | SourceLocation LocCookie = |
| 367 | SourceLocation::getFromRawEncoding(D.getLocCookie()); |
| 368 | if (LocCookie.isValid()) |
| 369 | Diags.Report(LocCookie, DiagID).AddString(Message); |
| 370 | else { |
| 371 | // Otherwise, report the backend diagnostic as occurring in the generated |
| 372 | // .s file. |
| 373 | // If Loc is invalid, we still need to report the diagnostic, it just gets |
| 374 | // no location info. |
| 375 | FullSourceLoc Loc; |
| 376 | Diags.Report(Loc, DiagID).AddString(Message); |
| 377 | } |
| 378 | // We handled all the possible severities. |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | bool |
| 383 | BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) { |
| 384 | if (D.getSeverity() != llvm::DS_Warning) |
| 385 | // For now, the only support we have for StackSize diagnostic is warning. |
| 386 | // We do not know how to format other severities. |
| 387 | return false; |
| 388 | |
| 389 | // FIXME: We should demangle the function name. |
| 390 | // FIXME: Is there a way to get a location for that function? |
| 391 | FullSourceLoc Loc; |
| 392 | Diags.Report(Loc, diag::warn_fe_backend_frame_larger_than) |
| 393 | << D.getStackSize() << D.getFunction().getName(); |
| 394 | return true; |
| 395 | } |
| 396 | |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 397 | void BackendConsumer::OptimizationRemarkHandler( |
| 398 | const llvm::DiagnosticInfoOptimizationRemark &D) { |
| 399 | // We only support remarks. |
Eric Christopher | 25c4e67 | 2014-05-02 17:52:19 +0000 | [diff] [blame] | 400 | assert(D.getSeverity() == llvm::DS_Remark); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 401 | |
| 402 | // Optimization remarks are active only if -Rpass=regexp is given and the |
| 403 | // regular expression pattern in 'regexp' matches the name of the pass |
| 404 | // name in \p D. |
| 405 | if (CodeGenOpts.OptimizationRemarkPattern && |
| 406 | CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName())) { |
| 407 | SourceManager &SourceMgr = Context->getSourceManager(); |
| 408 | FileManager &FileMgr = SourceMgr.getFileManager(); |
| 409 | StringRef Filename; |
| 410 | unsigned Line, Column; |
| 411 | D.getLocation(&Filename, &Line, &Column); |
| 412 | SourceLocation Loc; |
Diego Novillo | 6dc9c48 | 2014-05-08 13:49:54 +0000 | [diff] [blame] | 413 | const FileEntry *FE = FileMgr.getFile(Filename); |
| 414 | if (FE && Line > 0) { |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 415 | // If -gcolumn-info was not used, Column will be 0. This upsets the |
| 416 | // source manager, so if Column is not set, set it to 1. |
| 417 | if (Column == 0) |
| 418 | Column = 1; |
Diego Novillo | 6dc9c48 | 2014-05-08 13:49:54 +0000 | [diff] [blame] | 419 | Loc = SourceMgr.translateFileLineCol(FE, Line, Column); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 420 | } |
Diego Novillo | b344224 | 2014-04-22 19:56:49 +0000 | [diff] [blame] | 421 | Diags.Report(Loc, diag::remark_fe_backend_optimization_remark) |
| 422 | << AddFlagValue(D.getPassName()) << D.getMsg().str(); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 423 | |
| 424 | if (Line == 0) |
| 425 | // If we could not extract a source location for the diagnostic, |
| 426 | // inform the user how they can get source locations back. |
| 427 | // |
| 428 | // FIXME: We should really be generating !srcloc annotations when |
| 429 | // -Rpass is used. !srcloc annotations need to be emitted in |
| 430 | // approximately the same spots as !dbg nodes. |
| 431 | Diags.Report(diag::note_fe_backend_optimization_remark_missing_loc); |
Diego Novillo | 6dc9c48 | 2014-05-08 13:49:54 +0000 | [diff] [blame] | 432 | else if (Loc.isInvalid()) |
| 433 | // If we were not able to translate the file:line:col information |
| 434 | // back to a SourceLocation, at least emit a note stating that |
| 435 | // we could not translate this location. This can happen in the |
| 436 | // case of #line directives. |
| 437 | Diags.Report(diag::note_fe_backend_optimization_remark_invalid_loc) |
| 438 | << Filename << Line << Column; |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 442 | /// \brief This function is invoked when the backend needs |
| 443 | /// to report something to the user. |
| 444 | void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) { |
| 445 | unsigned DiagID = diag::err_fe_inline_asm; |
| 446 | llvm::DiagnosticSeverity Severity = DI.getSeverity(); |
| 447 | // Get the diagnostic ID based. |
| 448 | switch (DI.getKind()) { |
| 449 | case llvm::DK_InlineAsm: |
| 450 | if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI))) |
| 451 | return; |
| 452 | ComputeDiagID(Severity, inline_asm, DiagID); |
| 453 | break; |
| 454 | case llvm::DK_StackSize: |
| 455 | if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI))) |
| 456 | return; |
| 457 | ComputeDiagID(Severity, backend_frame_larger_than, DiagID); |
| 458 | break; |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 459 | case llvm::DK_OptimizationRemark: |
| 460 | // Optimization remarks are always handled completely by this |
| 461 | // handler. There is no generic way of emitting them. |
| 462 | OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemark>(DI)); |
| 463 | return; |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 464 | default: |
| 465 | // Plugin IDs are not bound to any value as they are set dynamically. |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 466 | ComputeDiagRemarkID(Severity, backend_plugin, DiagID); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 467 | break; |
| 468 | } |
| 469 | std::string MsgStorage; |
| 470 | { |
| 471 | raw_string_ostream Stream(MsgStorage); |
| 472 | DiagnosticPrinterRawOStream DP(Stream); |
| 473 | DI.print(DP); |
| 474 | } |
| 475 | |
| 476 | // Report the backend message using the usual diagnostic mechanism. |
| 477 | FullSourceLoc Loc; |
| 478 | Diags.Report(Loc, DiagID).AddString(MsgStorage); |
| 479 | } |
| 480 | #undef ComputeDiagID |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 481 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 482 | CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 483 | : Act(_Act), LinkModule(nullptr), |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 484 | VMContext(_VMContext ? _VMContext : new LLVMContext), |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 485 | OwnsVMContext(!_VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 486 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 487 | CodeGenAction::~CodeGenAction() { |
| 488 | TheModule.reset(); |
| 489 | if (OwnsVMContext) |
| 490 | delete VMContext; |
| 491 | } |
Daniel Dunbar | e8ecf9a | 2010-02-25 20:37:44 +0000 | [diff] [blame] | 492 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 493 | bool CodeGenAction::hasIRSupport() const { return true; } |
| 494 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 495 | void CodeGenAction::EndSourceFileAction() { |
| 496 | // If the consumer creation failed, do nothing. |
| 497 | if (!getCompilerInstance().hasASTConsumer()) |
| 498 | return; |
| 499 | |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 500 | // If we were given a link module, release consumer's ownership of it. |
| 501 | if (LinkModule) |
| 502 | BEConsumer->takeLinkModule(); |
| 503 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 504 | // Steal the module from the consumer. |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 505 | TheModule.reset(BEConsumer->takeModule()); |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 508 | llvm::Module *CodeGenAction::takeModule() { return TheModule.release(); } |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 509 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 510 | llvm::LLVMContext *CodeGenAction::takeLLVMContext() { |
| 511 | OwnsVMContext = false; |
| 512 | return VMContext; |
| 513 | } |
| 514 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 515 | static raw_ostream *GetOutputStream(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 516 | StringRef InFile, |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 517 | BackendAction Action) { |
| 518 | switch (Action) { |
| 519 | case Backend_EmitAssembly: |
| 520 | return CI.createDefaultOutputFile(false, InFile, "s"); |
| 521 | case Backend_EmitLL: |
| 522 | return CI.createDefaultOutputFile(false, InFile, "ll"); |
| 523 | case Backend_EmitBC: |
| 524 | return CI.createDefaultOutputFile(true, InFile, "bc"); |
| 525 | case Backend_EmitNothing: |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 526 | return nullptr; |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 527 | case Backend_EmitMCNull: |
| 528 | case Backend_EmitObj: |
| 529 | return CI.createDefaultOutputFile(true, InFile, "o"); |
| 530 | } |
| 531 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 532 | llvm_unreachable("Invalid action!"); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 535 | ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 536 | StringRef InFile) { |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 537 | BackendAction BA = static_cast<BackendAction>(Act); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 538 | std::unique_ptr<raw_ostream> OS(GetOutputStream(CI, InFile, BA)); |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 539 | if (BA != Backend_EmitNothing && !OS) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 540 | return nullptr; |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 541 | |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 542 | llvm::Module *LinkModuleToUse = LinkModule; |
| 543 | |
| 544 | // If we were not given a link module, and the user requested that one be |
| 545 | // loaded from bitcode, do so now. |
| 546 | const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile; |
| 547 | if (!LinkModuleToUse && !LinkBCFile.empty()) { |
| 548 | std::string ErrorStr; |
| 549 | |
| 550 | llvm::MemoryBuffer *BCBuf = |
| 551 | CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr); |
| 552 | if (!BCBuf) { |
| 553 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
| 554 | << LinkBCFile << ErrorStr; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 555 | return nullptr; |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Rafael Espindola | 6b6004a | 2014-01-13 18:31:09 +0000 | [diff] [blame] | 558 | ErrorOr<llvm::Module *> ModuleOrErr = |
| 559 | getLazyBitcodeModule(BCBuf, *VMContext); |
| 560 | if (error_code EC = ModuleOrErr.getError()) { |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 561 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
Rafael Espindola | 6b6004a | 2014-01-13 18:31:09 +0000 | [diff] [blame] | 562 | << LinkBCFile << EC.message(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 563 | return nullptr; |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 564 | } |
Rafael Espindola | 6b6004a | 2014-01-13 18:31:09 +0000 | [diff] [blame] | 565 | LinkModuleToUse = ModuleOrErr.get(); |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 568 | BEConsumer = new BackendConsumer(BA, CI.getDiagnostics(), CI.getCodeGenOpts(), |
| 569 | CI.getTargetOpts(), CI.getLangOpts(), |
Warren Hunt | 583db19 | 2014-05-28 19:17:45 +0000 | [diff] [blame] | 570 | CI.getFrontendOpts().ShowTimers, InFile, |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 571 | LinkModuleToUse, OS.release(), *VMContext); |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 572 | return BEConsumer; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 573 | } |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 574 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 575 | void CodeGenAction::ExecuteAction() { |
| 576 | // If this is an IR file, we have to treat it specially. |
| 577 | if (getCurrentFileKind() == IK_LLVM_IR) { |
| 578 | BackendAction BA = static_cast<BackendAction>(Act); |
| 579 | CompilerInstance &CI = getCompilerInstance(); |
| 580 | raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA); |
| 581 | if (BA != Backend_EmitNothing && !OS) |
| 582 | return; |
| 583 | |
| 584 | bool Invalid; |
| 585 | SourceManager &SM = CI.getSourceManager(); |
| 586 | const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(), |
| 587 | &Invalid); |
| 588 | if (Invalid) |
| 589 | return; |
| 590 | |
| 591 | // FIXME: This is stupid, IRReader shouldn't take ownership. |
| 592 | llvm::MemoryBuffer *MainFileCopy = |
| 593 | llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(), |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 594 | getCurrentFile()); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 595 | |
| 596 | llvm::SMDiagnostic Err; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 597 | TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext)); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 598 | if (!TheModule) { |
| 599 | // Translate from the diagnostic info to the SourceManager location. |
Argyrios Kyrtzidis | 27bf76d | 2011-09-19 20:40:38 +0000 | [diff] [blame] | 600 | SourceLocation Loc = SM.translateFileLineCol( |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 601 | SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(), |
| 602 | Err.getColumnNo() + 1); |
| 603 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 604 | // Strip off a leading diagnostic code if there is one. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 605 | StringRef Msg = Err.getMessage(); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 606 | if (Msg.startswith("error: ")) |
| 607 | Msg = Msg.substr(7); |
Benjamin Kramer | 778b8b8 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 608 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 609 | unsigned DiagID = |
| 610 | CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
Benjamin Kramer | 778b8b8 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 611 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 612 | CI.getDiagnostics().Report(Loc, DiagID) << Msg; |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 613 | return; |
| 614 | } |
Rafael Espindola | d5e81e5 | 2013-12-20 22:01:25 +0000 | [diff] [blame] | 615 | const TargetOptions &TargetOpts = CI.getTargetOpts(); |
| 616 | if (TheModule->getTargetTriple() != TargetOpts.Triple) { |
| 617 | unsigned DiagID = CI.getDiagnostics().getCustomDiagID( |
| 618 | DiagnosticsEngine::Warning, |
| 619 | "overriding the module target triple with %0"); |
| 620 | |
| 621 | CI.getDiagnostics().Report(SourceLocation(), DiagID) << TargetOpts.Triple; |
| 622 | TheModule->setTargetTriple(TargetOpts.Triple); |
| 623 | } |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 624 | |
Alp Toker | e83b906 | 2014-01-02 15:08:04 +0000 | [diff] [blame] | 625 | EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts, |
| 626 | CI.getLangOpts(), CI.getTarget().getTargetDescription(), |
| 627 | TheModule.get(), BA, OS); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | |
| 631 | // Otherwise follow the normal AST path. |
| 632 | this->ASTFrontendAction::ExecuteAction(); |
| 633 | } |
| 634 | |
| 635 | // |
| 636 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 637 | void EmitAssemblyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 638 | EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext) |
| 639 | : CodeGenAction(Backend_EmitAssembly, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 640 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 641 | void EmitBCAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 642 | EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext) |
| 643 | : CodeGenAction(Backend_EmitBC, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 644 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 645 | void EmitLLVMAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 646 | EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext) |
| 647 | : CodeGenAction(Backend_EmitLL, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 648 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 649 | void EmitLLVMOnlyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 650 | EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext) |
| 651 | : CodeGenAction(Backend_EmitNothing, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 652 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 653 | void EmitCodeGenOnlyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 654 | EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext) |
| 655 | : CodeGenAction(Backend_EmitMCNull, _VMContext) {} |
Daniel Dunbar | 4c77a64 | 2010-05-25 18:41:01 +0000 | [diff] [blame] | 656 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 657 | void EmitObjAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 658 | EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext) |
| 659 | : CodeGenAction(Backend_EmitObj, _VMContext) {} |