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