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