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" |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclGroup.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 18 | #include "clang/CodeGen/BackendUtil.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 19 | #include "clang/CodeGen/CodeGenAction.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 20 | #include "clang/CodeGen/ModuleBuilder.h" |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | acadc55 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/FrontendDiagnostic.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "llvm/Bitcode/ReaderWriter.h" |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DebugInfo.h" |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DiagnosticInfo.h" |
| 27 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 28 | #include "llvm/IR/LLVMContext.h" |
| 29 | #include "llvm/IR/Module.h" |
Chandler Carruth | b45836a | 2013-03-26 02:25:54 +0000 | [diff] [blame] | 30 | #include "llvm/IRReader/IRReader.h" |
Chandler Carruth | 00fa3f7 | 2014-03-06 03:46:44 +0000 | [diff] [blame] | 31 | #include "llvm/Linker/Linker.h" |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 32 | #include "llvm/Pass.h" |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
| 34 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Timer.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 36 | #include <memory> |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | using namespace llvm; |
| 39 | |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 40 | namespace clang { |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 41 | class BackendConsumer : public ASTConsumer { |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 42 | virtual void anchor(); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 43 | DiagnosticsEngine &Diags; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 44 | BackendAction Action; |
Daniel Dunbar | de18224 | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 45 | const CodeGenOptions &CodeGenOpts; |
Daniel Dunbar | de18224 | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 46 | const TargetOptions &TargetOpts; |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 47 | const LangOptions &LangOpts; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 48 | std::unique_ptr<raw_pwrite_stream> AsmOutStream; |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 49 | ASTContext *Context; |
Daniel Dunbar | b3a36cf | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 51 | Timer LLVMIRGeneration; |
Davide Italiano | b99fabd | 2016-07-21 06:28:48 +0000 | [diff] [blame^] | 52 | unsigned LLVMIRGenerationRefCount; |
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 | |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 56 | SmallVector<std::pair<unsigned, std::unique_ptr<llvm::Module>>, 4> |
| 57 | LinkModules; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 58 | |
Rafael Espindola | 8ce88a5 | 2015-12-14 23:17:07 +0000 | [diff] [blame] | 59 | // This is here so that the diagnostic printer knows the module a diagnostic |
| 60 | // refers to. |
| 61 | llvm::Module *CurLinkModule = nullptr; |
| 62 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | public: |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 64 | BackendConsumer( |
| 65 | BackendAction Action, DiagnosticsEngine &Diags, |
| 66 | const HeaderSearchOptions &HeaderSearchOpts, |
| 67 | const PreprocessorOptions &PPOpts, const CodeGenOptions &CodeGenOpts, |
| 68 | const TargetOptions &TargetOpts, const LangOptions &LangOpts, |
| 69 | bool TimePasses, const std::string &InFile, |
| 70 | const SmallVectorImpl<std::pair<unsigned, llvm::Module *>> &LinkModules, |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 71 | std::unique_ptr<raw_pwrite_stream> OS, LLVMContext &C, |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 72 | CoverageSourceInfo *CoverageInfo = nullptr) |
Justin Bogner | cb08299 | 2015-05-22 22:16:55 +0000 | [diff] [blame] | 73 | : Diags(Diags), Action(Action), CodeGenOpts(CodeGenOpts), |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 74 | TargetOpts(TargetOpts), LangOpts(LangOpts), |
| 75 | AsmOutStream(std::move(OS)), Context(nullptr), |
| 76 | LLVMIRGeneration("LLVM IR Generation Time"), |
Davide Italiano | b99fabd | 2016-07-21 06:28:48 +0000 | [diff] [blame^] | 77 | LLVMIRGenerationRefCount(0), |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 78 | Gen(CreateLLVMCodeGen(Diags, InFile, HeaderSearchOpts, PPOpts, |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 79 | CodeGenOpts, C, CoverageInfo)) { |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 80 | llvm::TimePassesIsEnabled = TimePasses; |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 81 | for (auto &I : LinkModules) |
| 82 | this->LinkModules.push_back( |
| 83 | std::make_pair(I.first, std::unique_ptr<llvm::Module>(I.second))); |
Chris Lattner | deffa13 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 84 | } |
Serge Pavlov | 41c1f79 | 2016-02-18 16:42:09 +0000 | [diff] [blame] | 85 | llvm::Module *getModule() const { return Gen->GetModule(); } |
| 86 | std::unique_ptr<llvm::Module> takeModule() { |
| 87 | return std::unique_ptr<llvm::Module>(Gen->ReleaseModule()); |
| 88 | } |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 89 | void releaseLinkModules() { |
| 90 | for (auto &I : LinkModules) |
| 91 | I.second.release(); |
| 92 | } |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 93 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 94 | void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override { |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 95 | Gen->HandleCXXStaticMemberVarInstantiation(VD); |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 98 | void Initialize(ASTContext &Ctx) override { |
Richard Smith | 293534b | 2015-08-18 20:39:29 +0000 | [diff] [blame] | 99 | assert(!Context && "initialized multiple times"); |
| 100 | |
Chris Lattner | 5cf49fe | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 101 | Context = &Ctx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 103 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 104 | LLVMIRGeneration.startTimer(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 5cf49fe | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 106 | Gen->Initialize(Ctx); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 107 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 108 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 109 | LLVMIRGeneration.stopTimer(); |
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 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 112 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 113 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 114 | Context->getSourceManager(), |
| 115 | "LLVM IR generation of declaration"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Davide Italiano | b99fabd | 2016-07-21 06:28:48 +0000 | [diff] [blame^] | 117 | // Recurse. |
| 118 | if (llvm::TimePassesIsEnabled) { |
| 119 | LLVMIRGenerationRefCount += 1; |
| 120 | if (LLVMIRGenerationRefCount == 1) |
| 121 | LLVMIRGeneration.startTimer(); |
| 122 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 123 | |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 124 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 125 | |
Davide Italiano | b99fabd | 2016-07-21 06:28:48 +0000 | [diff] [blame^] | 126 | if (llvm::TimePassesIsEnabled) { |
| 127 | LLVMIRGenerationRefCount -= 1; |
| 128 | if (LLVMIRGenerationRefCount == 0) |
| 129 | LLVMIRGeneration.stopTimer(); |
| 130 | } |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 131 | |
| 132 | return true; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 133 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Stephan Bergmann | 17d7d14 | 2016-03-30 06:27:31 +0000 | [diff] [blame] | 135 | void HandleInlineFunctionDefinition(FunctionDecl *D) override { |
Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 136 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 137 | Context->getSourceManager(), |
Stephan Bergmann | 17d7d14 | 2016-03-30 06:27:31 +0000 | [diff] [blame] | 138 | "LLVM IR generation of inline function"); |
Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 139 | if (llvm::TimePassesIsEnabled) |
| 140 | LLVMIRGeneration.startTimer(); |
| 141 | |
Stephan Bergmann | 17d7d14 | 2016-03-30 06:27:31 +0000 | [diff] [blame] | 142 | Gen->HandleInlineFunctionDefinition(D); |
Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 143 | |
| 144 | if (llvm::TimePassesIsEnabled) |
| 145 | LLVMIRGeneration.stopTimer(); |
| 146 | } |
| 147 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 148 | void HandleTranslationUnit(ASTContext &C) override { |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 149 | { |
Chris Lattner | e46de75 | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 150 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Davide Italiano | b99fabd | 2016-07-21 06:28:48 +0000 | [diff] [blame^] | 151 | if (llvm::TimePassesIsEnabled) { |
| 152 | LLVMIRGenerationRefCount += 1; |
| 153 | if (LLVMIRGenerationRefCount == 1) |
| 154 | LLVMIRGeneration.startTimer(); |
| 155 | } |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 156 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 157 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | a94d873 | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 158 | |
Davide Italiano | b99fabd | 2016-07-21 06:28:48 +0000 | [diff] [blame^] | 159 | if (llvm::TimePassesIsEnabled) { |
| 160 | LLVMIRGenerationRefCount -= 1; |
| 161 | if (LLVMIRGenerationRefCount == 0) |
| 162 | LLVMIRGeneration.stopTimer(); |
| 163 | } |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 165 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 166 | // Silently ignore if we weren't initialized for some reason. |
Serge Pavlov | 41c1f79 | 2016-02-18 16:42:09 +0000 | [diff] [blame] | 167 | if (!getModule()) |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 168 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 170 | // Install an inline asm handler so that diagnostics get printed through |
| 171 | // our diagnostics hooks. |
Serge Pavlov | 41c1f79 | 2016-02-18 16:42:09 +0000 | [diff] [blame] | 172 | LLVMContext &Ctx = getModule()->getContext(); |
Chris Lattner | 068f2ab | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 173 | LLVMContext::InlineAsmDiagHandlerTy OldHandler = |
| 174 | Ctx.getInlineAsmDiagnosticHandler(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 175 | void *OldContext = Ctx.getInlineAsmDiagnosticContext(); |
Chris Lattner | 068f2ab | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 176 | Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 177 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 178 | LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler = |
| 179 | Ctx.getDiagnosticHandler(); |
| 180 | void *OldDiagnosticContext = Ctx.getDiagnosticContext(); |
| 181 | Ctx.setDiagnosticHandler(DiagnosticHandler, this); |
| 182 | |
Rafael Espindola | 8ce88a5 | 2015-12-14 23:17:07 +0000 | [diff] [blame] | 183 | // Link LinkModule into this module if present, preserving its validity. |
| 184 | for (auto &I : LinkModules) { |
| 185 | unsigned LinkFlags = I.first; |
| 186 | CurLinkModule = I.second.get(); |
Serge Pavlov | 41c1f79 | 2016-02-18 16:42:09 +0000 | [diff] [blame] | 187 | if (Linker::linkModules(*getModule(), std::move(I.second), LinkFlags)) |
Rafael Espindola | 8ce88a5 | 2015-12-14 23:17:07 +0000 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | |
Steven Wu | 27fb522 | 2016-05-11 16:26:03 +0000 | [diff] [blame] | 191 | EmbedBitcode(getModule(), CodeGenOpts, llvm::MemoryBufferRef()); |
| 192 | |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 193 | EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 194 | C.getTargetInfo().getDataLayout(), |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 195 | getModule(), Action, std::move(AsmOutStream)); |
Rafael Espindola | 666a2ab | 2013-12-18 16:38:48 +0000 | [diff] [blame] | 196 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 197 | Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 198 | |
| 199 | Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 200 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 202 | void HandleTagDeclDefinition(TagDecl *D) override { |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 203 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 204 | Context->getSourceManager(), |
| 205 | "LLVM IR generation of declaration"); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 206 | Gen->HandleTagDeclDefinition(D); |
| 207 | } |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 208 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 209 | void HandleTagDeclRequiredDefinition(const TagDecl *D) override { |
David Blaikie | 48ad6dc | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 210 | Gen->HandleTagDeclRequiredDefinition(D); |
| 211 | } |
| 212 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 213 | void CompleteTentativeDefinition(VarDecl *D) override { |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 214 | Gen->CompleteTentativeDefinition(D); |
| 215 | } |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 216 | |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 217 | void AssignInheritanceModel(CXXRecordDecl *RD) override { |
| 218 | Gen->AssignInheritanceModel(RD); |
| 219 | } |
| 220 | |
Nico Weber | b6a5d05 | 2015-01-15 04:07:35 +0000 | [diff] [blame] | 221 | void HandleVTable(CXXRecordDecl *RD) override { |
| 222 | Gen->HandleVTable(RD); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 225 | static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context, |
| 226 | unsigned LocCookie) { |
| 227 | SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie); |
| 228 | ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc); |
| 229 | } |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 230 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 231 | static void DiagnosticHandler(const llvm::DiagnosticInfo &DI, |
| 232 | void *Context) { |
| 233 | ((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI); |
| 234 | } |
| 235 | |
Oliver Stannard | 9181785 | 2016-02-02 13:52:52 +0000 | [diff] [blame] | 236 | /// Get the best possible source location to represent a diagnostic that |
| 237 | /// may have associated debug info. |
| 238 | const FullSourceLoc |
| 239 | getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithDebugLocBase &D, |
| 240 | bool &BadDebugInfo, StringRef &Filename, |
| 241 | unsigned &Line, unsigned &Column) const; |
| 242 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 243 | void InlineAsmDiagHandler2(const llvm::SMDiagnostic &, |
| 244 | SourceLocation LocCookie); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 245 | |
| 246 | void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI); |
| 247 | /// \brief Specialized handler for InlineAsm diagnostic. |
| 248 | /// \return True if the diagnostic has been successfully reported, false |
| 249 | /// otherwise. |
| 250 | bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D); |
| 251 | /// \brief Specialized handler for StackSize diagnostic. |
| 252 | /// \return True if the diagnostic has been successfully reported, false |
| 253 | /// otherwise. |
| 254 | bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D); |
Oliver Stannard | 9181785 | 2016-02-02 13:52:52 +0000 | [diff] [blame] | 255 | /// \brief Specialized handler for unsupported backend feature diagnostic. |
| 256 | void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D); |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 257 | /// \brief Specialized handlers for optimization remarks. |
| 258 | /// Note that these handlers only accept remarks and they always handle |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 259 | /// them. |
Tyler Nowicki | f8a767d | 2014-07-18 19:40:19 +0000 | [diff] [blame] | 260 | void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D, |
| 261 | unsigned DiagID); |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 262 | void |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 263 | OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark &D); |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 264 | void OptimizationRemarkHandler( |
| 265 | const llvm::DiagnosticInfoOptimizationRemarkMissed &D); |
| 266 | void OptimizationRemarkHandler( |
| 267 | const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D); |
Tyler Nowicki | 8a0925c | 2015-08-10 19:56:40 +0000 | [diff] [blame] | 268 | void OptimizationRemarkHandler( |
| 269 | const llvm::DiagnosticInfoOptimizationRemarkAnalysisFPCommute &D); |
Tyler Nowicki | 034baf6 | 2015-08-10 23:05:16 +0000 | [diff] [blame] | 270 | void OptimizationRemarkHandler( |
| 271 | const llvm::DiagnosticInfoOptimizationRemarkAnalysisAliasing &D); |
Tyler Nowicki | f8a767d | 2014-07-18 19:40:19 +0000 | [diff] [blame] | 272 | void OptimizationFailureHandler( |
| 273 | const llvm::DiagnosticInfoOptimizationFailure &D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | }; |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 275 | |
| 276 | void BackendConsumer::anchor() {} |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 277 | } |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 279 | /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr |
| 280 | /// buffer to be a valid FullSourceLoc. |
| 281 | static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, |
| 282 | SourceManager &CSM) { |
| 283 | // Get both the clang and llvm source managers. The location is relative to |
| 284 | // 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] | 285 | // a copy to the Clang source manager. |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 286 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 288 | // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr |
| 289 | // already owns its one and clang::SourceManager wants to own its one. |
| 290 | const MemoryBuffer *LBuf = |
| 291 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 293 | // Create the copy and transfer ownership to clang::SourceManager. |
Alp Toker | aa0dd5a | 2014-06-27 06:02:00 +0000 | [diff] [blame] | 294 | // TODO: Avoid copying files into memory. |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 295 | std::unique_ptr<llvm::MemoryBuffer> CBuf = |
| 296 | llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(), |
| 297 | LBuf->getBufferIdentifier()); |
Alp Toker | aa0dd5a | 2014-06-27 06:02:00 +0000 | [diff] [blame] | 298 | // FIXME: Keep a file ID map instead of creating new IDs for each location. |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 299 | FileID FID = CSM.createFileID(std::move(CBuf)); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 301 | // Translate the offset into the file. |
Alp Toker | aa0dd5a | 2014-06-27 06:02:00 +0000 | [diff] [blame] | 302 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 303 | SourceLocation NewLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 304 | CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset); |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 305 | return FullSourceLoc(NewLoc, CSM); |
| 306 | } |
| 307 | |
Chris Lattner | 6d67213 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 309 | /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an |
| 310 | /// error parsing inline asm. The SMDiagnostic indicates the error relative to |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 311 | /// the temporary memory buffer that the inline asm parser has set up. |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 312 | void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D, |
| 313 | SourceLocation LocCookie) { |
| 314 | // There are a couple of different kinds of errors we could get here. First, |
| 315 | // we re-format the SMDiagnostic in terms of a clang diagnostic. |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 316 | |
| 317 | // Strip "error: " off the start of the message string. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 318 | StringRef Message = D.getMessage(); |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 319 | if (Message.startswith("error: ")) |
| 320 | Message = Message.substr(7); |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 321 | |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 322 | // If the SMDiagnostic has an inline asm source location, translate it. |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 323 | FullSourceLoc Loc; |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 324 | if (D.getLoc() != SMLoc()) |
| 325 | Loc = ConvertBackendLocation(D, Context->getSourceManager()); |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 326 | |
Joey Gouly | 5798b26 | 2014-06-05 21:23:42 +0000 | [diff] [blame] | 327 | unsigned DiagID; |
| 328 | switch (D.getKind()) { |
| 329 | case llvm::SourceMgr::DK_Error: |
| 330 | DiagID = diag::err_fe_inline_asm; |
| 331 | break; |
| 332 | case llvm::SourceMgr::DK_Warning: |
| 333 | DiagID = diag::warn_fe_inline_asm; |
| 334 | break; |
| 335 | case llvm::SourceMgr::DK_Note: |
| 336 | DiagID = diag::note_fe_inline_asm; |
| 337 | break; |
| 338 | } |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 339 | // If this problem has clang-level source location information, report the |
Joey Gouly | 5798b26 | 2014-06-05 21:23:42 +0000 | [diff] [blame] | 340 | // issue in the source with a note showing the instantiated |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 341 | // code. |
| 342 | if (LocCookie.isValid()) { |
Joey Gouly | 5798b26 | 2014-06-05 21:23:42 +0000 | [diff] [blame] | 343 | Diags.Report(LocCookie, DiagID).AddString(Message); |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 344 | |
Benjamin Kramer | e06b2b7 | 2011-10-16 10:48:28 +0000 | [diff] [blame] | 345 | if (D.getLoc().isValid()) { |
| 346 | DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here); |
| 347 | // Convert the SMDiagnostic ranges into SourceRange and attach them |
| 348 | // to the diagnostic. |
Yaron Keren | ede6030 | 2015-08-01 19:11:36 +0000 | [diff] [blame] | 349 | for (const std::pair<unsigned, unsigned> &Range : D.getRanges()) { |
Benjamin Kramer | e06b2b7 | 2011-10-16 10:48:28 +0000 | [diff] [blame] | 350 | unsigned Column = D.getColumnNo(); |
| 351 | B << SourceRange(Loc.getLocWithOffset(Range.first - Column), |
| 352 | Loc.getLocWithOffset(Range.second - Column)); |
| 353 | } |
| 354 | } |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 355 | return; |
| 356 | } |
| 357 | |
Joey Gouly | 5798b26 | 2014-06-05 21:23:42 +0000 | [diff] [blame] | 358 | // Otherwise, report the backend issue as occurring in the generated .s file. |
| 359 | // 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] | 360 | // location info. |
Joey Gouly | 5798b26 | 2014-06-05 21:23:42 +0000 | [diff] [blame] | 361 | Diags.Report(Loc, DiagID).AddString(Message); |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 364 | #define ComputeDiagID(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; \ |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 373 | case llvm::DS_Remark: \ |
| 374 | llvm_unreachable("'remark' severity not expected"); \ |
| 375 | break; \ |
| 376 | case llvm::DS_Note: \ |
| 377 | DiagID = diag::note_fe_##GroupName; \ |
| 378 | break; \ |
| 379 | } \ |
| 380 | } while (false) |
| 381 | |
| 382 | #define ComputeDiagRemarkID(Severity, GroupName, DiagID) \ |
| 383 | do { \ |
| 384 | switch (Severity) { \ |
| 385 | case llvm::DS_Error: \ |
| 386 | DiagID = diag::err_fe_##GroupName; \ |
| 387 | break; \ |
| 388 | case llvm::DS_Warning: \ |
| 389 | DiagID = diag::warn_fe_##GroupName; \ |
| 390 | break; \ |
| 391 | case llvm::DS_Remark: \ |
| 392 | DiagID = diag::remark_fe_##GroupName; \ |
| 393 | break; \ |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 394 | case llvm::DS_Note: \ |
| 395 | DiagID = diag::note_fe_##GroupName; \ |
| 396 | break; \ |
| 397 | } \ |
| 398 | } while (false) |
| 399 | |
| 400 | bool |
| 401 | BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) { |
| 402 | unsigned DiagID; |
| 403 | ComputeDiagID(D.getSeverity(), inline_asm, DiagID); |
| 404 | std::string Message = D.getMsgStr().str(); |
| 405 | |
| 406 | // If this problem has clang-level source location information, report the |
Tobias Grosser | bd25beb | 2014-02-26 10:21:56 +0000 | [diff] [blame] | 407 | // 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] | 408 | // code. |
| 409 | SourceLocation LocCookie = |
| 410 | SourceLocation::getFromRawEncoding(D.getLocCookie()); |
| 411 | if (LocCookie.isValid()) |
| 412 | Diags.Report(LocCookie, DiagID).AddString(Message); |
| 413 | else { |
| 414 | // Otherwise, report the backend diagnostic as occurring in the generated |
| 415 | // .s file. |
| 416 | // If Loc is invalid, we still need to report the diagnostic, it just gets |
| 417 | // no location info. |
| 418 | FullSourceLoc Loc; |
| 419 | Diags.Report(Loc, DiagID).AddString(Message); |
| 420 | } |
| 421 | // We handled all the possible severities. |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | bool |
| 426 | BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) { |
| 427 | if (D.getSeverity() != llvm::DS_Warning) |
| 428 | // For now, the only support we have for StackSize diagnostic is warning. |
| 429 | // We do not know how to format other severities. |
| 430 | return false; |
| 431 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 432 | if (const Decl *ND = Gen->GetDeclForMangledName(D.getFunction().getName())) { |
Matt Arsenault | 4deb4ed2 | 2016-06-20 18:13:09 +0000 | [diff] [blame] | 433 | // FIXME: Shouldn't need to truncate to uint32_t |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 434 | Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()), |
| 435 | diag::warn_fe_frame_larger_than) |
Matt Arsenault | 4deb4ed2 | 2016-06-20 18:13:09 +0000 | [diff] [blame] | 436 | << static_cast<uint32_t>(D.getStackSize()) << Decl::castToDeclContext(ND); |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | |
| 440 | return false; |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Oliver Stannard | 9181785 | 2016-02-02 13:52:52 +0000 | [diff] [blame] | 443 | const FullSourceLoc BackendConsumer::getBestLocationFromDebugLoc( |
| 444 | const llvm::DiagnosticInfoWithDebugLocBase &D, bool &BadDebugInfo, StringRef &Filename, |
| 445 | unsigned &Line, unsigned &Column) const { |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 446 | SourceManager &SourceMgr = Context->getSourceManager(); |
| 447 | FileManager &FileMgr = SourceMgr.getFileManager(); |
Alp Toker | 2750627 | 2014-06-05 22:11:12 +0000 | [diff] [blame] | 448 | SourceLocation DILoc; |
Diego Novillo | 86f884e | 2015-05-08 20:59:56 +0000 | [diff] [blame] | 449 | |
| 450 | if (D.isLocationAvailable()) { |
| 451 | D.getLocation(&Filename, &Line, &Column); |
| 452 | const FileEntry *FE = FileMgr.getFile(Filename); |
| 453 | if (FE && Line > 0) { |
| 454 | // If -gcolumn-info was not used, Column will be 0. This upsets the |
| 455 | // source manager, so pass 1 if Column is not set. |
| 456 | DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1); |
| 457 | } |
Oliver Stannard | 9181785 | 2016-02-02 13:52:52 +0000 | [diff] [blame] | 458 | BadDebugInfo = DILoc.isInvalid(); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 459 | } |
Alp Toker | 2750627 | 2014-06-05 22:11:12 +0000 | [diff] [blame] | 460 | |
| 461 | // If a location isn't available, try to approximate it using the associated |
| 462 | // function definition. We use the definition's right brace to differentiate |
| 463 | // from diagnostics that genuinely relate to the function itself. |
| 464 | FullSourceLoc Loc(DILoc, SourceMgr); |
| 465 | if (Loc.isInvalid()) |
| 466 | if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName())) |
Oliver Stannard | 9181785 | 2016-02-02 13:52:52 +0000 | [diff] [blame] | 467 | Loc = FD->getASTContext().getFullLoc(FD->getLocation()); |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 468 | |
Diego Novillo | 86f884e | 2015-05-08 20:59:56 +0000 | [diff] [blame] | 469 | if (DILoc.isInvalid() && D.isLocationAvailable()) |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 470 | // If we were not able to translate the file:line:col information |
| 471 | // back to a SourceLocation, at least emit a note stating that |
| 472 | // we could not translate this location. This can happen in the |
| 473 | // case of #line directives. |
Oliver Stannard | 9181785 | 2016-02-02 13:52:52 +0000 | [diff] [blame] | 474 | Diags.Report(Loc, diag::note_fe_backend_invalid_loc) |
Ben Craig | 6e4695a | 2016-05-06 13:29:46 +0000 | [diff] [blame] | 475 | << Filename << Line << Column; |
Oliver Stannard | 9181785 | 2016-02-02 13:52:52 +0000 | [diff] [blame] | 476 | |
| 477 | return Loc; |
| 478 | } |
| 479 | |
| 480 | void BackendConsumer::UnsupportedDiagHandler( |
| 481 | const llvm::DiagnosticInfoUnsupported &D) { |
| 482 | // We only support errors. |
| 483 | assert(D.getSeverity() == llvm::DS_Error); |
| 484 | |
| 485 | StringRef Filename; |
| 486 | unsigned Line, Column; |
| 487 | bool BadDebugInfo; |
| 488 | FullSourceLoc Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, |
| 489 | Line, Column); |
| 490 | |
| 491 | Diags.Report(Loc, diag::err_fe_backend_unsupported) << D.getMessage().str(); |
| 492 | |
| 493 | if (BadDebugInfo) |
| 494 | // If we were not able to translate the file:line:col information |
| 495 | // back to a SourceLocation, at least emit a note stating that |
| 496 | // we could not translate this location. This can happen in the |
| 497 | // case of #line directives. |
| 498 | Diags.Report(Loc, diag::note_fe_backend_invalid_loc) |
| 499 | << Filename << Line << Column; |
| 500 | } |
| 501 | |
| 502 | void BackendConsumer::EmitOptimizationMessage( |
| 503 | const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) { |
| 504 | // We only support warnings and remarks. |
| 505 | assert(D.getSeverity() == llvm::DS_Remark || |
| 506 | D.getSeverity() == llvm::DS_Warning); |
| 507 | |
| 508 | StringRef Filename; |
| 509 | unsigned Line, Column; |
| 510 | bool BadDebugInfo = false; |
| 511 | FullSourceLoc Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, |
| 512 | Line, Column); |
| 513 | |
| 514 | Diags.Report(Loc, DiagID) |
| 515 | << AddFlagValue(D.getPassName() ? D.getPassName() : "") |
| 516 | << D.getMsg().str(); |
| 517 | |
| 518 | if (BadDebugInfo) |
| 519 | // If we were not able to translate the file:line:col information |
| 520 | // back to a SourceLocation, at least emit a note stating that |
| 521 | // we could not translate this location. This can happen in the |
| 522 | // case of #line directives. |
| 523 | Diags.Report(Loc, diag::note_fe_backend_invalid_loc) |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 524 | << Filename << Line << Column; |
| 525 | } |
| 526 | |
| 527 | void BackendConsumer::OptimizationRemarkHandler( |
| 528 | const llvm::DiagnosticInfoOptimizationRemark &D) { |
| 529 | // Optimization remarks are active only if the -Rpass flag has a regular |
| 530 | // expression that matches the name of the pass name in \p D. |
| 531 | if (CodeGenOpts.OptimizationRemarkPattern && |
| 532 | CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName())) |
Tyler Nowicki | f8a767d | 2014-07-18 19:40:19 +0000 | [diff] [blame] | 533 | EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark); |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void BackendConsumer::OptimizationRemarkHandler( |
| 537 | const llvm::DiagnosticInfoOptimizationRemarkMissed &D) { |
| 538 | // Missed optimization remarks are active only if the -Rpass-missed |
| 539 | // flag has a regular expression that matches the name of the pass |
| 540 | // name in \p D. |
| 541 | if (CodeGenOpts.OptimizationRemarkMissedPattern && |
| 542 | CodeGenOpts.OptimizationRemarkMissedPattern->match(D.getPassName())) |
Tyler Nowicki | f8a767d | 2014-07-18 19:40:19 +0000 | [diff] [blame] | 543 | EmitOptimizationMessage(D, |
| 544 | diag::remark_fe_backend_optimization_remark_missed); |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void BackendConsumer::OptimizationRemarkHandler( |
| 548 | const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D) { |
Tyler Nowicki | 65061a2 | 2015-08-11 01:10:08 +0000 | [diff] [blame] | 549 | // Optimization analysis remarks are active if the pass name is set to |
| 550 | // llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a |
| 551 | // regular expression that matches the name of the pass name in \p D. |
| 552 | |
Adam Nemet | b5beb97 | 2016-06-29 04:55:31 +0000 | [diff] [blame] | 553 | if (D.shouldAlwaysPrint() || |
Tyler Nowicki | 65061a2 | 2015-08-11 01:10:08 +0000 | [diff] [blame] | 554 | (CodeGenOpts.OptimizationRemarkAnalysisPattern && |
| 555 | CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName()))) |
Tyler Nowicki | f8a767d | 2014-07-18 19:40:19 +0000 | [diff] [blame] | 556 | EmitOptimizationMessage( |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 557 | D, diag::remark_fe_backend_optimization_remark_analysis); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Tyler Nowicki | 8a0925c | 2015-08-10 19:56:40 +0000 | [diff] [blame] | 560 | void BackendConsumer::OptimizationRemarkHandler( |
| 561 | const llvm::DiagnosticInfoOptimizationRemarkAnalysisFPCommute &D) { |
Tyler Nowicki | 65061a2 | 2015-08-11 01:10:08 +0000 | [diff] [blame] | 562 | // Optimization analysis remarks are active if the pass name is set to |
| 563 | // llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a |
| 564 | // regular expression that matches the name of the pass name in \p D. |
| 565 | |
Adam Nemet | b5beb97 | 2016-06-29 04:55:31 +0000 | [diff] [blame] | 566 | if (D.shouldAlwaysPrint() || |
Tyler Nowicki | 65061a2 | 2015-08-11 01:10:08 +0000 | [diff] [blame] | 567 | (CodeGenOpts.OptimizationRemarkAnalysisPattern && |
| 568 | CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName()))) |
Tyler Nowicki | 8a0925c | 2015-08-10 19:56:40 +0000 | [diff] [blame] | 569 | EmitOptimizationMessage( |
| 570 | D, diag::remark_fe_backend_optimization_remark_analysis_fpcommute); |
| 571 | } |
| 572 | |
Tyler Nowicki | 034baf6 | 2015-08-10 23:05:16 +0000 | [diff] [blame] | 573 | void BackendConsumer::OptimizationRemarkHandler( |
| 574 | const llvm::DiagnosticInfoOptimizationRemarkAnalysisAliasing &D) { |
Tyler Nowicki | 65061a2 | 2015-08-11 01:10:08 +0000 | [diff] [blame] | 575 | // Optimization analysis remarks are active if the pass name is set to |
| 576 | // llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a |
| 577 | // regular expression that matches the name of the pass name in \p D. |
| 578 | |
Adam Nemet | b5beb97 | 2016-06-29 04:55:31 +0000 | [diff] [blame] | 579 | if (D.shouldAlwaysPrint() || |
Tyler Nowicki | 65061a2 | 2015-08-11 01:10:08 +0000 | [diff] [blame] | 580 | (CodeGenOpts.OptimizationRemarkAnalysisPattern && |
| 581 | CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName()))) |
Tyler Nowicki | 034baf6 | 2015-08-10 23:05:16 +0000 | [diff] [blame] | 582 | EmitOptimizationMessage( |
| 583 | D, diag::remark_fe_backend_optimization_remark_analysis_aliasing); |
| 584 | } |
| 585 | |
Tyler Nowicki | f8a767d | 2014-07-18 19:40:19 +0000 | [diff] [blame] | 586 | void BackendConsumer::OptimizationFailureHandler( |
| 587 | const llvm::DiagnosticInfoOptimizationFailure &D) { |
| 588 | EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure); |
| 589 | } |
| 590 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 591 | /// \brief This function is invoked when the backend needs |
| 592 | /// to report something to the user. |
| 593 | void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) { |
| 594 | unsigned DiagID = diag::err_fe_inline_asm; |
| 595 | llvm::DiagnosticSeverity Severity = DI.getSeverity(); |
| 596 | // Get the diagnostic ID based. |
| 597 | switch (DI.getKind()) { |
| 598 | case llvm::DK_InlineAsm: |
| 599 | if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI))) |
| 600 | return; |
| 601 | ComputeDiagID(Severity, inline_asm, DiagID); |
| 602 | break; |
| 603 | case llvm::DK_StackSize: |
| 604 | if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI))) |
| 605 | return; |
| 606 | ComputeDiagID(Severity, backend_frame_larger_than, DiagID); |
| 607 | break; |
Rafael Espindola | 8ce88a5 | 2015-12-14 23:17:07 +0000 | [diff] [blame] | 608 | case DK_Linker: |
| 609 | assert(CurLinkModule); |
| 610 | // FIXME: stop eating the warnings and notes. |
| 611 | if (Severity != DS_Error) |
| 612 | return; |
| 613 | DiagID = diag::err_fe_cannot_link_module; |
| 614 | break; |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 615 | case llvm::DK_OptimizationRemark: |
| 616 | // Optimization remarks are always handled completely by this |
| 617 | // handler. There is no generic way of emitting them. |
| 618 | OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemark>(DI)); |
| 619 | return; |
Diego Novillo | 9c89ff1 | 2014-05-29 16:19:27 +0000 | [diff] [blame] | 620 | case llvm::DK_OptimizationRemarkMissed: |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 621 | // Optimization remarks are always handled completely by this |
| 622 | // handler. There is no generic way of emitting them. |
| 623 | OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemarkMissed>(DI)); |
| 624 | return; |
Diego Novillo | 9c89ff1 | 2014-05-29 16:19:27 +0000 | [diff] [blame] | 625 | case llvm::DK_OptimizationRemarkAnalysis: |
Diego Novillo | d23ec94 | 2014-05-29 19:55:06 +0000 | [diff] [blame] | 626 | // Optimization remarks are always handled completely by this |
| 627 | // handler. There is no generic way of emitting them. |
| 628 | OptimizationRemarkHandler( |
| 629 | cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI)); |
Diego Novillo | 9c89ff1 | 2014-05-29 16:19:27 +0000 | [diff] [blame] | 630 | return; |
Tyler Nowicki | 8a0925c | 2015-08-10 19:56:40 +0000 | [diff] [blame] | 631 | case llvm::DK_OptimizationRemarkAnalysisFPCommute: |
| 632 | // Optimization remarks are always handled completely by this |
| 633 | // handler. There is no generic way of emitting them. |
| 634 | OptimizationRemarkHandler( |
| 635 | cast<DiagnosticInfoOptimizationRemarkAnalysisFPCommute>(DI)); |
| 636 | return; |
Tyler Nowicki | 034baf6 | 2015-08-10 23:05:16 +0000 | [diff] [blame] | 637 | case llvm::DK_OptimizationRemarkAnalysisAliasing: |
| 638 | // Optimization remarks are always handled completely by this |
| 639 | // handler. There is no generic way of emitting them. |
| 640 | OptimizationRemarkHandler( |
| 641 | cast<DiagnosticInfoOptimizationRemarkAnalysisAliasing>(DI)); |
| 642 | return; |
Tyler Nowicki | f8a767d | 2014-07-18 19:40:19 +0000 | [diff] [blame] | 643 | case llvm::DK_OptimizationFailure: |
| 644 | // Optimization failures are always handled completely by this |
| 645 | // handler. |
| 646 | OptimizationFailureHandler(cast<DiagnosticInfoOptimizationFailure>(DI)); |
| 647 | return; |
Oliver Stannard | 9181785 | 2016-02-02 13:52:52 +0000 | [diff] [blame] | 648 | case llvm::DK_Unsupported: |
| 649 | UnsupportedDiagHandler(cast<DiagnosticInfoUnsupported>(DI)); |
| 650 | return; |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 651 | default: |
| 652 | // 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] | 653 | ComputeDiagRemarkID(Severity, backend_plugin, DiagID); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 654 | break; |
| 655 | } |
| 656 | std::string MsgStorage; |
| 657 | { |
| 658 | raw_string_ostream Stream(MsgStorage); |
| 659 | DiagnosticPrinterRawOStream DP(Stream); |
| 660 | DI.print(DP); |
| 661 | } |
| 662 | |
Rafael Espindola | 8ce88a5 | 2015-12-14 23:17:07 +0000 | [diff] [blame] | 663 | if (DiagID == diag::err_fe_cannot_link_module) { |
| 664 | Diags.Report(diag::err_fe_cannot_link_module) |
| 665 | << CurLinkModule->getModuleIdentifier() << MsgStorage; |
| 666 | return; |
| 667 | } |
| 668 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 669 | // Report the backend message using the usual diagnostic mechanism. |
| 670 | FullSourceLoc Loc; |
| 671 | Diags.Report(Loc, DiagID).AddString(MsgStorage); |
| 672 | } |
| 673 | #undef ComputeDiagID |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 674 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 675 | CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext) |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 676 | : Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext), |
Eric Christopher | 02e3dd4 | 2016-03-12 01:47:11 +0000 | [diff] [blame] | 677 | OwnsVMContext(!_VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 678 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 679 | CodeGenAction::~CodeGenAction() { |
| 680 | TheModule.reset(); |
| 681 | if (OwnsVMContext) |
| 682 | delete VMContext; |
| 683 | } |
Daniel Dunbar | e8ecf9a | 2010-02-25 20:37:44 +0000 | [diff] [blame] | 684 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 685 | bool CodeGenAction::hasIRSupport() const { return true; } |
| 686 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 687 | void CodeGenAction::EndSourceFileAction() { |
| 688 | // If the consumer creation failed, do nothing. |
| 689 | if (!getCompilerInstance().hasASTConsumer()) |
| 690 | return; |
| 691 | |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 692 | // Take back ownership of link modules we passed to consumer. |
| 693 | if (!LinkModules.empty()) |
| 694 | BEConsumer->releaseLinkModules(); |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 695 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 696 | // Steal the module from the consumer. |
David Blaikie | 780dd3b | 2014-08-29 05:08:19 +0000 | [diff] [blame] | 697 | TheModule = BEConsumer->takeModule(); |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Rafael Espindola | e833730 | 2014-08-19 14:36:35 +0000 | [diff] [blame] | 700 | std::unique_ptr<llvm::Module> CodeGenAction::takeModule() { |
| 701 | return std::move(TheModule); |
| 702 | } |
| 703 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 704 | llvm::LLVMContext *CodeGenAction::takeLLVMContext() { |
| 705 | OwnsVMContext = false; |
| 706 | return VMContext; |
| 707 | } |
| 708 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 709 | static std::unique_ptr<raw_pwrite_stream> |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 710 | GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) { |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 711 | switch (Action) { |
| 712 | case Backend_EmitAssembly: |
| 713 | return CI.createDefaultOutputFile(false, InFile, "s"); |
| 714 | case Backend_EmitLL: |
| 715 | return CI.createDefaultOutputFile(false, InFile, "ll"); |
| 716 | case Backend_EmitBC: |
| 717 | return CI.createDefaultOutputFile(true, InFile, "bc"); |
| 718 | case Backend_EmitNothing: |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 719 | return nullptr; |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 720 | case Backend_EmitMCNull: |
Alp Toker | ea04672 | 2014-06-03 17:23:34 +0000 | [diff] [blame] | 721 | return CI.createNullOutputFile(); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 722 | case Backend_EmitObj: |
| 723 | return CI.createDefaultOutputFile(true, InFile, "o"); |
| 724 | } |
| 725 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 726 | llvm_unreachable("Invalid action!"); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 727 | } |
| 728 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 729 | std::unique_ptr<ASTConsumer> |
| 730 | CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 731 | BackendAction BA = static_cast<BackendAction>(Act); |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 732 | std::unique_ptr<raw_pwrite_stream> OS = GetOutputStream(CI, InFile, BA); |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 733 | if (BA != Backend_EmitNothing && !OS) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 734 | return nullptr; |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 735 | |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 736 | // Load bitcode modules to link with, if we need to. |
| 737 | if (LinkModules.empty()) |
| 738 | for (auto &I : CI.getCodeGenOpts().LinkBitcodeFiles) { |
| 739 | const std::string &LinkBCFile = I.second; |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 740 | |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 741 | auto BCBuf = CI.getFileManager().getBufferForFile(LinkBCFile); |
| 742 | if (!BCBuf) { |
| 743 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
| 744 | << LinkBCFile << BCBuf.getError().message(); |
| 745 | LinkModules.clear(); |
| 746 | return nullptr; |
| 747 | } |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 748 | |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 749 | ErrorOr<std::unique_ptr<llvm::Module>> ModuleOrErr = |
| 750 | getLazyBitcodeModule(std::move(*BCBuf), *VMContext); |
| 751 | if (std::error_code EC = ModuleOrErr.getError()) { |
| 752 | CI.getDiagnostics().Report(diag::err_cannot_open_file) << LinkBCFile |
| 753 | << EC.message(); |
| 754 | LinkModules.clear(); |
| 755 | return nullptr; |
| 756 | } |
| 757 | addLinkModule(ModuleOrErr.get().release(), I.first); |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 758 | } |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 759 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 760 | CoverageSourceInfo *CoverageInfo = nullptr; |
| 761 | // Add the preprocessor callback only when the coverage mapping is generated. |
| 762 | if (CI.getCodeGenOpts().CoverageMapping) { |
| 763 | CoverageInfo = new CoverageSourceInfo; |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 764 | CI.getPreprocessor().addPPCallbacks( |
| 765 | std::unique_ptr<PPCallbacks>(CoverageInfo)); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 766 | } |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 767 | |
David Blaikie | 037e75b | 2014-08-10 21:06:17 +0000 | [diff] [blame] | 768 | std::unique_ptr<BackendConsumer> Result(new BackendConsumer( |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 769 | BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(), |
| 770 | CI.getPreprocessorOpts(), CI.getCodeGenOpts(), CI.getTargetOpts(), |
Artem Belevich | 5d40ae3 | 2015-10-27 17:56:59 +0000 | [diff] [blame] | 771 | CI.getLangOpts(), CI.getFrontendOpts().ShowTimers, InFile, LinkModules, |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 772 | std::move(OS), *VMContext, CoverageInfo)); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 773 | BEConsumer = Result.get(); |
| 774 | return std::move(Result); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 775 | } |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 776 | |
Tim Northover | 1390b44 | 2016-04-06 19:58:07 +0000 | [diff] [blame] | 777 | static void BitcodeInlineAsmDiagHandler(const llvm::SMDiagnostic &SM, |
| 778 | void *Context, |
| 779 | unsigned LocCookie) { |
| 780 | SM.print(nullptr, llvm::errs()); |
| 781 | |
| 782 | auto Diags = static_cast<DiagnosticsEngine *>(Context); |
| 783 | unsigned DiagID; |
| 784 | switch (SM.getKind()) { |
| 785 | case llvm::SourceMgr::DK_Error: |
| 786 | DiagID = diag::err_fe_inline_asm; |
| 787 | break; |
| 788 | case llvm::SourceMgr::DK_Warning: |
| 789 | DiagID = diag::warn_fe_inline_asm; |
| 790 | break; |
| 791 | case llvm::SourceMgr::DK_Note: |
| 792 | DiagID = diag::note_fe_inline_asm; |
| 793 | break; |
| 794 | } |
| 795 | |
| 796 | Diags->Report(DiagID).AddString("cannot compile inline asm"); |
| 797 | } |
| 798 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 799 | void CodeGenAction::ExecuteAction() { |
| 800 | // If this is an IR file, we have to treat it specially. |
| 801 | if (getCurrentFileKind() == IK_LLVM_IR) { |
| 802 | BackendAction BA = static_cast<BackendAction>(Act); |
| 803 | CompilerInstance &CI = getCompilerInstance(); |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 804 | std::unique_ptr<raw_pwrite_stream> OS = |
| 805 | GetOutputStream(CI, getCurrentFile(), BA); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 806 | if (BA != Backend_EmitNothing && !OS) |
| 807 | return; |
| 808 | |
| 809 | bool Invalid; |
| 810 | SourceManager &SM = CI.getSourceManager(); |
Alp Toker | 7b463d5 | 2014-06-30 01:33:59 +0000 | [diff] [blame] | 811 | FileID FID = SM.getMainFileID(); |
| 812 | llvm::MemoryBuffer *MainFile = SM.getBuffer(FID, &Invalid); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 813 | if (Invalid) |
| 814 | return; |
| 815 | |
Teresa Johnson | b104749 | 2016-04-20 02:23:52 +0000 | [diff] [blame] | 816 | // For ThinLTO backend invocations, ensure that the context |
| 817 | // merges types based on ODR identifiers. |
| 818 | if (!CI.getCodeGenOpts().ThinLTOIndexFile.empty()) |
| 819 | VMContext->enableDebugTypeODRUniquing(); |
| 820 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 821 | llvm::SMDiagnostic Err; |
Rafael Espindola | 3232773 | 2014-08-26 21:49:29 +0000 | [diff] [blame] | 822 | TheModule = parseIR(MainFile->getMemBufferRef(), Err, *VMContext); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 823 | if (!TheModule) { |
Alp Toker | 7b463d5 | 2014-06-30 01:33:59 +0000 | [diff] [blame] | 824 | // Translate from the diagnostic info to the SourceManager location if |
| 825 | // available. |
| 826 | // TODO: Unify this with ConvertBackendLocation() |
| 827 | SourceLocation Loc; |
| 828 | if (Err.getLineNo() > 0) { |
| 829 | assert(Err.getColumnNo() >= 0); |
| 830 | Loc = SM.translateFileLineCol(SM.getFileEntryForID(FID), |
| 831 | Err.getLineNo(), Err.getColumnNo() + 1); |
| 832 | } |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 833 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 834 | // Strip off a leading diagnostic code if there is one. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 835 | StringRef Msg = Err.getMessage(); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 836 | if (Msg.startswith("error: ")) |
| 837 | Msg = Msg.substr(7); |
Benjamin Kramer | 778b8b8 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 838 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 839 | unsigned DiagID = |
| 840 | CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
Benjamin Kramer | 778b8b8 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 841 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 842 | CI.getDiagnostics().Report(Loc, DiagID) << Msg; |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 843 | return; |
| 844 | } |
Rafael Espindola | d5e81e5 | 2013-12-20 22:01:25 +0000 | [diff] [blame] | 845 | const TargetOptions &TargetOpts = CI.getTargetOpts(); |
| 846 | if (TheModule->getTargetTriple() != TargetOpts.Triple) { |
Nico Weber | 6cf2df2 | 2015-01-29 06:25:59 +0000 | [diff] [blame] | 847 | CI.getDiagnostics().Report(SourceLocation(), |
| 848 | diag::warn_fe_override_module) |
| 849 | << TargetOpts.Triple; |
Rafael Espindola | d5e81e5 | 2013-12-20 22:01:25 +0000 | [diff] [blame] | 850 | TheModule->setTargetTriple(TargetOpts.Triple); |
| 851 | } |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 852 | |
Steven Wu | 27fb522 | 2016-05-11 16:26:03 +0000 | [diff] [blame] | 853 | EmbedBitcode(TheModule.get(), CI.getCodeGenOpts(), |
| 854 | MainFile->getMemBufferRef()); |
| 855 | |
Tim Northover | 1390b44 | 2016-04-06 19:58:07 +0000 | [diff] [blame] | 856 | LLVMContext &Ctx = TheModule->getContext(); |
| 857 | Ctx.setInlineAsmDiagnosticHandler(BitcodeInlineAsmDiagHandler, |
| 858 | &CI.getDiagnostics()); |
Steven Wu | 27fb522 | 2016-05-11 16:26:03 +0000 | [diff] [blame] | 859 | |
Alp Toker | e83b906 | 2014-01-02 15:08:04 +0000 | [diff] [blame] | 860 | EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts, |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 861 | CI.getLangOpts(), CI.getTarget().getDataLayout(), |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 862 | TheModule.get(), BA, std::move(OS)); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 863 | return; |
| 864 | } |
| 865 | |
| 866 | // Otherwise follow the normal AST path. |
| 867 | this->ASTFrontendAction::ExecuteAction(); |
| 868 | } |
| 869 | |
| 870 | // |
| 871 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 872 | void EmitAssemblyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 873 | EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext) |
| 874 | : CodeGenAction(Backend_EmitAssembly, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 875 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 876 | void EmitBCAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 877 | EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext) |
| 878 | : CodeGenAction(Backend_EmitBC, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 879 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 880 | void EmitLLVMAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 881 | EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext) |
| 882 | : CodeGenAction(Backend_EmitLL, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 883 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 884 | void EmitLLVMOnlyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 885 | EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext) |
| 886 | : CodeGenAction(Backend_EmitNothing, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 887 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 888 | void EmitCodeGenOnlyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 889 | EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext) |
| 890 | : CodeGenAction(Backend_EmitMCNull, _VMContext) {} |
Daniel Dunbar | 4c77a64 | 2010-05-25 18:41:01 +0000 | [diff] [blame] | 891 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 892 | void EmitObjAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 893 | EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext) |
| 894 | : CodeGenAction(Backend_EmitObj, _VMContext) {} |