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