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