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