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