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