| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 1 | //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===// |
| Daniel Dunbar | d69bacc | 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 | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 10 | #include "clang/CodeGen/CodeGenAction.h" |
| Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
| Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
| Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclGroup.h" |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
| Chandler Carruth | 55fc873 | 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 | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 18 | #include "clang/CodeGen/BackendUtil.h" |
| Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 19 | #include "clang/CodeGen/ModuleBuilder.h" |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CompilerInstance.h" |
| Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/FrontendDiagnostic.h" |
| Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
| 23 | #include "llvm/Bitcode/ReaderWriter.h" |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 24 | #include "llvm/IR/DebugInfo.h" |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 25 | #include "llvm/IR/DiagnosticInfo.h" |
| 26 | #include "llvm/IR/DiagnosticPrinter.h" |
| Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 27 | #include "llvm/IR/LLVMContext.h" |
| 28 | #include "llvm/IR/Module.h" |
| Chandler Carruth | 9cc935b | 2013-03-26 02:25:54 +0000 | [diff] [blame] | 29 | #include "llvm/IRReader/IRReader.h" |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 30 | #include "llvm/Linker/Linker.h" |
| Daniel Dunbar | 05a7f3d | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
| Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
| 33 | #include "llvm/Support/SourceMgr.h" |
| Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Timer.h" |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 35 | #include <memory> |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | using namespace llvm; |
| 38 | |
| Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 39 | namespace clang { |
| Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 40 | class BackendConsumer : public ASTConsumer { |
| David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 41 | virtual void anchor(); |
| David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 42 | DiagnosticsEngine &Diags; |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 43 | BackendAction Action; |
| Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 44 | const CodeGenOptions &CodeGenOpts; |
| Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 45 | const TargetOptions &TargetOpts; |
| Dan Gohman | b18b8ad | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 46 | const LangOptions &LangOpts; |
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 47 | raw_ostream *AsmOutStream; |
| Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 48 | ASTContext *Context; |
| Daniel Dunbar | 90f4130 | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 49 | |
| Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 50 | Timer LLVMIRGeneration; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 52 | std::unique_ptr<CodeGenerator> Gen; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 54 | std::unique_ptr<llvm::Module> TheModule, LinkModule; |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 55 | |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | public: |
| David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 57 | BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags, |
| Daniel Dunbar | 6b0cf67 | 2010-06-07 23:19:17 +0000 | [diff] [blame] | 58 | const CodeGenOptions &compopts, |
| Dan Gohman | b18b8ad | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 59 | const TargetOptions &targetopts, |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [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 | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 68 | llvm::TimePassesIsEnabled = TimePasses; |
| Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 69 | } |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 70 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 71 | llvm::Module *takeModule() { return TheModule.release(); } |
| 72 | llvm::Module *takeLinkModule() { return LinkModule.release(); } |
| Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 73 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 74 | void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override { |
| Rafael Espindola | 0250393 | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 75 | Gen->HandleCXXStaticMemberVarInstantiation(VD); |
| Rafael Espindola | 234fe65 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 78 | void Initialize(ASTContext &Ctx) override { |
| Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 79 | Context = &Ctx; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
| Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 81 | if (llvm::TimePassesIsEnabled) |
| Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 82 | LLVMIRGeneration.startTimer(); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
| Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 84 | Gen->Initialize(Ctx); |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 85 | |
| Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 86 | TheModule.reset(Gen->GetModule()); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
| Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 88 | if (llvm::TimePassesIsEnabled) |
| Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 89 | LLVMIRGeneration.stopTimer(); |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 90 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 92 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
| Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 93 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
| Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 94 | Context->getSourceManager(), |
| 95 | "LLVM IR generation of declaration"); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
| Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 97 | if (llvm::TimePassesIsEnabled) |
| Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 98 | LLVMIRGeneration.startTimer(); |
| Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 99 | |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 100 | Gen->HandleTopLevelDecl(D); |
| Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 101 | |
| Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 102 | if (llvm::TimePassesIsEnabled) |
| Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 103 | LLVMIRGeneration.stopTimer(); |
| Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 104 | |
| 105 | return true; |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 106 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [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 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 121 | void HandleTranslationUnit(ASTContext &C) override { |
| Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 122 | { |
| Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 123 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
| Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 124 | if (llvm::TimePassesIsEnabled) |
| Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 125 | LLVMIRGeneration.startTimer(); |
| Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 126 | |
| Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 127 | Gen->HandleTranslationUnit(C); |
| Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 128 | |
| Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 129 | if (llvm::TimePassesIsEnabled) |
| Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 130 | LLVMIRGeneration.stopTimer(); |
| 131 | } |
| Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 132 | |
| Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 133 | // Silently ignore if we weren't initialized for some reason. |
| Daniel Dunbar | 05a7f3d | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 134 | if (!TheModule) |
| Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 135 | return; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
| Daniel Dunbar | 897c676 | 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 | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 139 | llvm::Module *M = Gen->ReleaseModule(); |
| Daniel Dunbar | 897c676 | 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. |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 143 | TheModule.release(); |
| Daniel Dunbar | 897c676 | 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 | 22a7dfe | 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 | 897c676 | 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 | 063e476 | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 164 | LLVMContext::InlineAsmDiagHandlerTy OldHandler = |
| 165 | Ctx.getInlineAsmDiagnosticHandler(); |
| Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 166 | void *OldContext = Ctx.getInlineAsmDiagnosticContext(); |
| Chris Lattner | 063e476 | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 167 | Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this); |
| Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 168 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 169 | LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler = |
| 170 | Ctx.getDiagnosticHandler(); |
| 171 | void *OldDiagnosticContext = Ctx.getDiagnosticContext(); |
| 172 | Ctx.setDiagnosticHandler(DiagnosticHandler, this); |
| 173 | |
| Dan Gohman | b18b8ad | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 174 | EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 175 | C.getTargetInfo().getTargetDescription(), |
| Daniel Dunbar | 05a7f3d | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 176 | TheModule.get(), Action, AsmOutStream); |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 177 | |
| Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 178 | Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 179 | |
| 180 | Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext); |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 181 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 183 | void HandleTagDeclDefinition(TagDecl *D) override { |
| Chris Lattner | 49f28ca | 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 | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 187 | Gen->HandleTagDeclDefinition(D); |
| 188 | } |
| Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 189 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 190 | void HandleTagDeclRequiredDefinition(const TagDecl *D) override { |
| David Blaikie | 658cd2c | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 191 | Gen->HandleTagDeclRequiredDefinition(D); |
| 192 | } |
| 193 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 194 | void CompleteTentativeDefinition(VarDecl *D) override { |
| Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 195 | Gen->CompleteTentativeDefinition(D); |
| 196 | } |
| Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 197 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 198 | void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override { |
| Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 199 | Gen->HandleVTable(RD, DefinitionRequired); |
| 200 | } |
| 201 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 202 | void HandleLinkerOptionPragma(llvm::StringRef Opts) override { |
| Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 203 | Gen->HandleLinkerOptionPragma(Opts); |
| 204 | } |
| 205 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 206 | void HandleDetectMismatch(llvm::StringRef Name, |
| 207 | llvm::StringRef Value) override { |
| Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 208 | Gen->HandleDetectMismatch(Name, Value); |
| 209 | } |
| 210 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 211 | void HandleDependentLibrary(llvm::StringRef Opts) override { |
| Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 212 | Gen->HandleDependentLibrary(Opts); |
| 213 | } |
| 214 | |
| Chris Lattner | 6da9eb6 | 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 | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 220 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 221 | static void DiagnosticHandler(const llvm::DiagnosticInfo &DI, |
| 222 | void *Context) { |
| 223 | ((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI); |
| 224 | } |
| 225 | |
| Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 226 | void InlineAsmDiagHandler2(const llvm::SMDiagnostic &, |
| 227 | SourceLocation LocCookie); |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [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); |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 238 | /// \brief Specialized handlers for optimization remarks. |
| 239 | /// Note that these handlers only accept remarks and they always handle |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 240 | /// them. |
| 241 | void |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 242 | EmitOptimizationRemark(const llvm::DiagnosticInfoOptimizationRemarkBase &D, |
| 243 | unsigned DiagID); |
| 244 | void |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 245 | OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark &D); |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 246 | void OptimizationRemarkHandler( |
| 247 | const llvm::DiagnosticInfoOptimizationRemarkMissed &D); |
| 248 | void OptimizationRemarkHandler( |
| 249 | const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | }; |
| David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 251 | |
| 252 | void BackendConsumer::anchor() {} |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 255 | /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr |
| 256 | /// buffer to be a valid FullSourceLoc. |
| 257 | static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, |
| 258 | SourceManager &CSM) { |
| 259 | // Get both the clang and llvm source managers. The location is relative to |
| 260 | // a memory buffer that the LLVM Source Manager is handling, we need to add |
| Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 261 | // a copy to the Clang source manager. |
| Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 262 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
| Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 263 | |
| Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 264 | // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr |
| 265 | // already owns its one and clang::SourceManager wants to own its one. |
| 266 | const MemoryBuffer *LBuf = |
| 267 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); |
| Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 268 | |
| Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 269 | // Create the copy and transfer ownership to clang::SourceManager. |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 270 | // TODO: Avoid copying files into memory. |
| Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 271 | llvm::MemoryBuffer *CBuf = |
| 272 | llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(), |
| 273 | LBuf->getBufferIdentifier()); |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 274 | // FIXME: Keep a file ID map instead of creating new IDs for each location. |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 275 | FileID FID = CSM.createFileID(CBuf); |
| Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 276 | |
| Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 277 | // Translate the offset into the file. |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 278 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
| Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 279 | SourceLocation NewLoc = |
| Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 280 | CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset); |
| Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 281 | return FullSourceLoc(NewLoc, CSM); |
| 282 | } |
| 283 | |
| Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 284 | |
| Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 285 | /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an |
| 286 | /// error parsing inline asm. The SMDiagnostic indicates the error relative to |
| Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 287 | /// the temporary memory buffer that the inline asm parser has set up. |
| Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 288 | void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D, |
| 289 | SourceLocation LocCookie) { |
| 290 | // There are a couple of different kinds of errors we could get here. First, |
| 291 | // we re-format the SMDiagnostic in terms of a clang diagnostic. |
| Argyrios Kyrtzidis | 5d3a4bb | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 292 | |
| 293 | // Strip "error: " off the start of the message string. |
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 294 | StringRef Message = D.getMessage(); |
| Argyrios Kyrtzidis | 5d3a4bb | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 295 | if (Message.startswith("error: ")) |
| 296 | Message = Message.substr(7); |
| Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 297 | |
| Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 298 | // If the SMDiagnostic has an inline asm source location, translate it. |
| Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 299 | FullSourceLoc Loc; |
| Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 300 | if (D.getLoc() != SMLoc()) |
| 301 | Loc = ConvertBackendLocation(D, Context->getSourceManager()); |
| Argyrios Kyrtzidis | 5d3a4bb | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 302 | |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 303 | unsigned DiagID; |
| 304 | switch (D.getKind()) { |
| 305 | case llvm::SourceMgr::DK_Error: |
| 306 | DiagID = diag::err_fe_inline_asm; |
| 307 | break; |
| 308 | case llvm::SourceMgr::DK_Warning: |
| 309 | DiagID = diag::warn_fe_inline_asm; |
| 310 | break; |
| 311 | case llvm::SourceMgr::DK_Note: |
| 312 | DiagID = diag::note_fe_inline_asm; |
| 313 | break; |
| 314 | } |
| Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 315 | // If this problem has clang-level source location information, report the |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 316 | // issue in the source with a note showing the instantiated |
| Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 317 | // code. |
| 318 | if (LocCookie.isValid()) { |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 319 | Diags.Report(LocCookie, DiagID).AddString(Message); |
| Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 320 | |
| Benjamin Kramer | 96fda0c | 2011-10-16 10:48:28 +0000 | [diff] [blame] | 321 | if (D.getLoc().isValid()) { |
| 322 | DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here); |
| 323 | // Convert the SMDiagnostic ranges into SourceRange and attach them |
| 324 | // to the diagnostic. |
| 325 | for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) { |
| 326 | std::pair<unsigned, unsigned> Range = D.getRanges()[i]; |
| 327 | unsigned Column = D.getColumnNo(); |
| 328 | B << SourceRange(Loc.getLocWithOffset(Range.first - Column), |
| 329 | Loc.getLocWithOffset(Range.second - Column)); |
| 330 | } |
| 331 | } |
| Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 332 | return; |
| 333 | } |
| 334 | |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 335 | // Otherwise, report the backend issue as occurring in the generated .s file. |
| 336 | // If Loc is invalid, we still need to report the issue, it just gets no |
| Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 337 | // location info. |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 338 | Diags.Report(Loc, DiagID).AddString(Message); |
| Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 341 | #define ComputeDiagID(Severity, GroupName, DiagID) \ |
| 342 | do { \ |
| 343 | switch (Severity) { \ |
| 344 | case llvm::DS_Error: \ |
| 345 | DiagID = diag::err_fe_##GroupName; \ |
| 346 | break; \ |
| 347 | case llvm::DS_Warning: \ |
| 348 | DiagID = diag::warn_fe_##GroupName; \ |
| 349 | break; \ |
| 350 | case llvm::DS_Remark: \ |
| 351 | llvm_unreachable("'remark' severity not expected"); \ |
| 352 | break; \ |
| 353 | case llvm::DS_Note: \ |
| 354 | DiagID = diag::note_fe_##GroupName; \ |
| 355 | break; \ |
| 356 | } \ |
| 357 | } while (false) |
| 358 | |
| 359 | #define ComputeDiagRemarkID(Severity, GroupName, DiagID) \ |
| 360 | do { \ |
| 361 | switch (Severity) { \ |
| 362 | case llvm::DS_Error: \ |
| 363 | DiagID = diag::err_fe_##GroupName; \ |
| 364 | break; \ |
| 365 | case llvm::DS_Warning: \ |
| 366 | DiagID = diag::warn_fe_##GroupName; \ |
| 367 | break; \ |
| 368 | case llvm::DS_Remark: \ |
| 369 | DiagID = diag::remark_fe_##GroupName; \ |
| 370 | break; \ |
| 371 | case llvm::DS_Note: \ |
| 372 | DiagID = diag::note_fe_##GroupName; \ |
| 373 | break; \ |
| 374 | } \ |
| 375 | } while (false) |
| 376 | |
| 377 | bool |
| 378 | BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) { |
| 379 | unsigned DiagID; |
| 380 | ComputeDiagID(D.getSeverity(), inline_asm, DiagID); |
| 381 | std::string Message = D.getMsgStr().str(); |
| 382 | |
| 383 | // If this problem has clang-level source location information, report the |
| 384 | // issue as being a problem in the source with a note showing the instantiated |
| 385 | // code. |
| 386 | SourceLocation LocCookie = |
| 387 | SourceLocation::getFromRawEncoding(D.getLocCookie()); |
| 388 | if (LocCookie.isValid()) |
| 389 | Diags.Report(LocCookie, DiagID).AddString(Message); |
| 390 | else { |
| 391 | // Otherwise, report the backend diagnostic as occurring in the generated |
| 392 | // .s file. |
| 393 | // If Loc is invalid, we still need to report the diagnostic, it just gets |
| 394 | // no location info. |
| 395 | FullSourceLoc Loc; |
| 396 | Diags.Report(Loc, DiagID).AddString(Message); |
| 397 | } |
| 398 | // We handled all the possible severities. |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | bool |
| 403 | BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) { |
| 404 | if (D.getSeverity() != llvm::DS_Warning) |
| 405 | // For now, the only support we have for StackSize diagnostic is warning. |
| 406 | // We do not know how to format other severities. |
| 407 | return false; |
| 408 | |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 409 | if (const Decl *ND = Gen->GetDeclForMangledName(D.getFunction().getName())) { |
| 410 | Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()), |
| 411 | diag::warn_fe_frame_larger_than) |
| 412 | << D.getStackSize() << Decl::castToDeclContext(ND); |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | void BackendConsumer::EmitOptimizationRemark( |
| 420 | const llvm::DiagnosticInfoOptimizationRemarkBase &D, unsigned DiagID) { |
| 421 | // We only support remarks. |
| 422 | assert(D.getSeverity() == llvm::DS_Remark); |
| 423 | |
| 424 | SourceManager &SourceMgr = Context->getSourceManager(); |
| 425 | FileManager &FileMgr = SourceMgr.getFileManager(); |
| 426 | StringRef Filename; |
| 427 | unsigned Line, Column; |
| 428 | D.getLocation(&Filename, &Line, &Column); |
| 429 | SourceLocation DILoc; |
| 430 | const FileEntry *FE = FileMgr.getFile(Filename); |
| 431 | if (FE && Line > 0) { |
| 432 | // If -gcolumn-info was not used, Column will be 0. This upsets the |
| 433 | // source manager, so pass 1 if Column is not set. |
| 434 | DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1); |
| 435 | } |
| 436 | |
| 437 | // If a location isn't available, try to approximate it using the associated |
| 438 | // function definition. We use the definition's right brace to differentiate |
| 439 | // from diagnostics that genuinely relate to the function itself. |
| 440 | FullSourceLoc Loc(DILoc, SourceMgr); |
| 441 | if (Loc.isInvalid()) |
| 442 | if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName())) |
| 443 | Loc = FD->getASTContext().getFullLoc(FD->getBodyRBrace()); |
| 444 | |
| 445 | Diags.Report(Loc, DiagID) << AddFlagValue(D.getPassName()) |
| 446 | << D.getMsg().str(); |
| 447 | |
| 448 | if (DILoc.isInvalid()) |
| 449 | // If we were not able to translate the file:line:col information |
| 450 | // back to a SourceLocation, at least emit a note stating that |
| 451 | // we could not translate this location. This can happen in the |
| 452 | // case of #line directives. |
| 453 | Diags.Report(Loc, diag::note_fe_backend_optimization_remark_invalid_loc) |
| 454 | << Filename << Line << Column; |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 457 | void BackendConsumer::OptimizationRemarkHandler( |
| 458 | const llvm::DiagnosticInfoOptimizationRemark &D) { |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 459 | // Optimization remarks are active only if the -Rpass flag has a regular |
| 460 | // expression that matches the name of the pass name in \p D. |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 461 | if (CodeGenOpts.OptimizationRemarkPattern && |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 462 | CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName())) |
| 463 | EmitOptimizationRemark(D, diag::remark_fe_backend_optimization_remark); |
| 464 | } |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 465 | |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 466 | void BackendConsumer::OptimizationRemarkHandler( |
| 467 | const llvm::DiagnosticInfoOptimizationRemarkMissed &D) { |
| 468 | // Missed optimization remarks are active only if the -Rpass-missed |
| 469 | // flag has a regular expression that matches the name of the pass |
| 470 | // name in \p D. |
| 471 | if (CodeGenOpts.OptimizationRemarkMissedPattern && |
| 472 | CodeGenOpts.OptimizationRemarkMissedPattern->match(D.getPassName())) |
| 473 | EmitOptimizationRemark(D, |
| 474 | diag::remark_fe_backend_optimization_remark_missed); |
| 475 | } |
| 476 | |
| 477 | void BackendConsumer::OptimizationRemarkHandler( |
| 478 | const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D) { |
| 479 | // Optimization analysis remarks are active only if the -Rpass-analysis |
| 480 | // flag has a regular expression that matches the name of the pass |
| 481 | // name in \p D. |
| 482 | if (CodeGenOpts.OptimizationRemarkAnalysisPattern && |
| 483 | CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())) |
| 484 | EmitOptimizationRemark( |
| 485 | D, diag::remark_fe_backend_optimization_remark_analysis); |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 488 | /// \brief This function is invoked when the backend needs |
| 489 | /// to report something to the user. |
| 490 | void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) { |
| 491 | unsigned DiagID = diag::err_fe_inline_asm; |
| 492 | llvm::DiagnosticSeverity Severity = DI.getSeverity(); |
| 493 | // Get the diagnostic ID based. |
| 494 | switch (DI.getKind()) { |
| 495 | case llvm::DK_InlineAsm: |
| 496 | if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI))) |
| 497 | return; |
| 498 | ComputeDiagID(Severity, inline_asm, DiagID); |
| 499 | break; |
| 500 | case llvm::DK_StackSize: |
| 501 | if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI))) |
| 502 | return; |
| 503 | ComputeDiagID(Severity, backend_frame_larger_than, DiagID); |
| 504 | break; |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 505 | case llvm::DK_OptimizationRemark: |
| 506 | // Optimization remarks are always handled completely by this |
| 507 | // handler. There is no generic way of emitting them. |
| 508 | OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemark>(DI)); |
| 509 | return; |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 510 | case llvm::DK_OptimizationRemarkMissed: |
| 511 | // Optimization remarks are always handled completely by this |
| 512 | // handler. There is no generic way of emitting them. |
| 513 | OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemarkMissed>(DI)); |
| 514 | return; |
| 515 | case llvm::DK_OptimizationRemarkAnalysis: |
| 516 | // Optimization remarks are always handled completely by this |
| 517 | // handler. There is no generic way of emitting them. |
| 518 | OptimizationRemarkHandler( |
| 519 | cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI)); |
| 520 | return; |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 521 | default: |
| 522 | // Plugin IDs are not bound to any value as they are set dynamically. |
| 523 | ComputeDiagRemarkID(Severity, backend_plugin, DiagID); |
| 524 | break; |
| 525 | } |
| 526 | std::string MsgStorage; |
| 527 | { |
| 528 | raw_string_ostream Stream(MsgStorage); |
| 529 | DiagnosticPrinterRawOStream DP(Stream); |
| 530 | DI.print(DP); |
| 531 | } |
| 532 | |
| 533 | // Report the backend message using the usual diagnostic mechanism. |
| 534 | FullSourceLoc Loc; |
| 535 | Diags.Report(Loc, DiagID).AddString(MsgStorage); |
| 536 | } |
| 537 | #undef ComputeDiagID |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 538 | |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 539 | CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext) |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 540 | : Act(_Act), LinkModule(nullptr), |
| Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 541 | VMContext(_VMContext ? _VMContext : new LLVMContext), |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 542 | OwnsVMContext(!_VMContext) {} |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 543 | |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 544 | CodeGenAction::~CodeGenAction() { |
| 545 | TheModule.reset(); |
| 546 | if (OwnsVMContext) |
| 547 | delete VMContext; |
| 548 | } |
| Daniel Dunbar | 9ad1c02 | 2010-02-25 20:37:44 +0000 | [diff] [blame] | 549 | |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 550 | bool CodeGenAction::hasIRSupport() const { return true; } |
| 551 | |
| Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 552 | void CodeGenAction::EndSourceFileAction() { |
| 553 | // If the consumer creation failed, do nothing. |
| 554 | if (!getCompilerInstance().hasASTConsumer()) |
| 555 | return; |
| 556 | |
| Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 557 | // If we were given a link module, release consumer's ownership of it. |
| 558 | if (LinkModule) |
| 559 | BEConsumer->takeLinkModule(); |
| 560 | |
| Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 561 | // Steal the module from the consumer. |
| Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 562 | TheModule.reset(BEConsumer->takeModule()); |
| Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 565 | llvm::Module *CodeGenAction::takeModule() { return TheModule.release(); } |
| Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 566 | |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 567 | llvm::LLVMContext *CodeGenAction::takeLLVMContext() { |
| 568 | OwnsVMContext = false; |
| 569 | return VMContext; |
| 570 | } |
| 571 | |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 572 | static raw_ostream *GetOutputStream(CompilerInstance &CI, |
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 573 | StringRef InFile, |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 574 | BackendAction Action) { |
| 575 | switch (Action) { |
| 576 | case Backend_EmitAssembly: |
| 577 | return CI.createDefaultOutputFile(false, InFile, "s"); |
| 578 | case Backend_EmitLL: |
| 579 | return CI.createDefaultOutputFile(false, InFile, "ll"); |
| 580 | case Backend_EmitBC: |
| 581 | return CI.createDefaultOutputFile(true, InFile, "bc"); |
| 582 | case Backend_EmitNothing: |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 583 | return nullptr; |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 584 | case Backend_EmitMCNull: |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 585 | return CI.createNullOutputFile(); |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 586 | case Backend_EmitObj: |
| 587 | return CI.createDefaultOutputFile(true, InFile, "o"); |
| 588 | } |
| 589 | |
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 590 | llvm_unreachable("Invalid action!"); |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 593 | ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, |
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 594 | StringRef InFile) { |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 595 | BackendAction BA = static_cast<BackendAction>(Act); |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 596 | std::unique_ptr<raw_ostream> OS(GetOutputStream(CI, InFile, BA)); |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 597 | if (BA != Backend_EmitNothing && !OS) |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 598 | return nullptr; |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 599 | |
| Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 600 | llvm::Module *LinkModuleToUse = LinkModule; |
| 601 | |
| 602 | // If we were not given a link module, and the user requested that one be |
| 603 | // loaded from bitcode, do so now. |
| 604 | const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile; |
| 605 | if (!LinkModuleToUse && !LinkBCFile.empty()) { |
| 606 | std::string ErrorStr; |
| 607 | |
| 608 | llvm::MemoryBuffer *BCBuf = |
| 609 | CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr); |
| 610 | if (!BCBuf) { |
| 611 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
| 612 | << LinkBCFile << ErrorStr; |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 613 | return nullptr; |
| Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 616 | ErrorOr<llvm::Module *> ModuleOrErr = |
| 617 | getLazyBitcodeModule(BCBuf, *VMContext); |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 618 | if (std::error_code EC = ModuleOrErr.getError()) { |
| Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 619 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 620 | << LinkBCFile << EC.message(); |
| Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 621 | return nullptr; |
| Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 622 | } |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 623 | LinkModuleToUse = ModuleOrErr.get(); |
| Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 626 | BEConsumer = new BackendConsumer(BA, CI.getDiagnostics(), CI.getCodeGenOpts(), |
| 627 | CI.getTargetOpts(), CI.getLangOpts(), |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 628 | CI.getFrontendOpts().ShowTimers, InFile, |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 629 | LinkModuleToUse, OS.release(), *VMContext); |
| Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 630 | return BEConsumer; |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 631 | } |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 632 | |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 633 | void CodeGenAction::ExecuteAction() { |
| 634 | // If this is an IR file, we have to treat it specially. |
| 635 | if (getCurrentFileKind() == IK_LLVM_IR) { |
| 636 | BackendAction BA = static_cast<BackendAction>(Act); |
| 637 | CompilerInstance &CI = getCompilerInstance(); |
| 638 | raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA); |
| 639 | if (BA != Backend_EmitNothing && !OS) |
| 640 | return; |
| 641 | |
| 642 | bool Invalid; |
| 643 | SourceManager &SM = CI.getSourceManager(); |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 644 | FileID FID = SM.getMainFileID(); |
| 645 | llvm::MemoryBuffer *MainFile = SM.getBuffer(FID, &Invalid); |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 646 | if (Invalid) |
| 647 | return; |
| 648 | |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 649 | llvm::SMDiagnostic Err; |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 650 | TheModule.reset(ParseIR(MainFile, Err, *VMContext)); |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 651 | if (!TheModule) { |
| Stephen Hines | ef82254 | 2014-07-21 00:47:37 -0700 | [diff] [blame^] | 652 | // Translate from the diagnostic info to the SourceManager location if |
| 653 | // available. |
| 654 | // TODO: Unify this with ConvertBackendLocation() |
| 655 | SourceLocation Loc; |
| 656 | if (Err.getLineNo() > 0) { |
| 657 | assert(Err.getColumnNo() >= 0); |
| 658 | Loc = SM.translateFileLineCol(SM.getFileEntryForID(FID), |
| 659 | Err.getLineNo(), Err.getColumnNo() + 1); |
| 660 | } |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 661 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 662 | // Strip off a leading diagnostic code if there is one. |
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 663 | StringRef Msg = Err.getMessage(); |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 664 | if (Msg.startswith("error: ")) |
| 665 | Msg = Msg.substr(7); |
| Benjamin Kramer | c9b47f9 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 666 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 667 | unsigned DiagID = |
| 668 | CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
| Benjamin Kramer | c9b47f9 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 669 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 670 | CI.getDiagnostics().Report(Loc, DiagID) << Msg; |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 671 | return; |
| 672 | } |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 673 | const TargetOptions &TargetOpts = CI.getTargetOpts(); |
| 674 | if (TheModule->getTargetTriple() != TargetOpts.Triple) { |
| 675 | unsigned DiagID = CI.getDiagnostics().getCustomDiagID( |
| 676 | DiagnosticsEngine::Warning, |
| 677 | "overriding the module target triple with %0"); |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 678 | |
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 679 | CI.getDiagnostics().Report(SourceLocation(), DiagID) << TargetOpts.Triple; |
| 680 | TheModule->setTargetTriple(TargetOpts.Triple); |
| 681 | } |
| 682 | |
| 683 | EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts, |
| 684 | CI.getLangOpts(), CI.getTarget().getTargetDescription(), |
| 685 | TheModule.get(), BA, OS); |
| Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 686 | return; |
| 687 | } |
| 688 | |
| 689 | // Otherwise follow the normal AST path. |
| 690 | this->ASTFrontendAction::ExecuteAction(); |
| 691 | } |
| 692 | |
| 693 | // |
| 694 | |
| David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 695 | void EmitAssemblyAction::anchor() { } |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 696 | EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext) |
| 697 | : CodeGenAction(Backend_EmitAssembly, _VMContext) {} |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 698 | |
| David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 699 | void EmitBCAction::anchor() { } |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 700 | EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext) |
| 701 | : CodeGenAction(Backend_EmitBC, _VMContext) {} |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 702 | |
| David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 703 | void EmitLLVMAction::anchor() { } |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 704 | EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext) |
| 705 | : CodeGenAction(Backend_EmitLL, _VMContext) {} |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 706 | |
| David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 707 | void EmitLLVMOnlyAction::anchor() { } |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 708 | EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext) |
| 709 | : CodeGenAction(Backend_EmitNothing, _VMContext) {} |
| Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 710 | |
| David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 711 | void EmitCodeGenOnlyAction::anchor() { } |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 712 | EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext) |
| 713 | : CodeGenAction(Backend_EmitMCNull, _VMContext) {} |
| Daniel Dunbar | 32148ce | 2010-05-25 18:41:01 +0000 | [diff] [blame] | 714 | |
| David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 715 | void EmitObjAction::anchor() { } |
| Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 716 | EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext) |
| 717 | : CodeGenAction(Backend_EmitObj, _VMContext) {} |