Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 1 | //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===// |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 10 | #include "clang/CodeGen/CodeGenAction.h" |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclGroup.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Basic/FileManager.h" |
| 15 | #include "clang/Basic/SourceManager.h" |
| 16 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | c1b1729 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 17 | #include "clang/CodeGen/BackendUtil.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 18 | #include "clang/CodeGen/ModuleBuilder.h" |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | acadc55 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
| 22 | #include "llvm/Bitcode/ReaderWriter.h" |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DebugInfo.h" |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DiagnosticInfo.h" |
| 25 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LLVMContext.h" |
| 27 | #include "llvm/IR/Module.h" |
Chandler Carruth | b45836a | 2013-03-26 02:25:54 +0000 | [diff] [blame] | 28 | #include "llvm/IRReader/IRReader.h" |
Chandler Carruth | 00fa3f7 | 2014-03-06 03:46:44 +0000 | [diff] [blame] | 29 | #include "llvm/Linker/Linker.h" |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MemoryBuffer.h" |
| 32 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Timer.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 34 | #include <memory> |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | using namespace llvm; |
| 37 | |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 38 | namespace clang { |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 39 | class BackendConsumer : public ASTConsumer { |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 40 | virtual void anchor(); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 41 | DiagnosticsEngine &Diags; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 42 | BackendAction Action; |
Daniel Dunbar | de18224 | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 43 | const CodeGenOptions &CodeGenOpts; |
Daniel Dunbar | de18224 | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 44 | const TargetOptions &TargetOpts; |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 45 | const LangOptions &LangOpts; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 46 | raw_ostream *AsmOutStream; |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 47 | ASTContext *Context; |
Daniel Dunbar | b3a36cf | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 49 | Timer LLVMIRGeneration; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 51 | std::unique_ptr<CodeGenerator> Gen; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 53 | std::unique_ptr<llvm::Module> TheModule, LinkModule; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 54 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | public: |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 56 | BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags, |
Daniel Dunbar | 6d5824f | 2010-06-07 23:19:17 +0000 | [diff] [blame] | 57 | const CodeGenOptions &compopts, |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 58 | const TargetOptions &targetopts, |
Rafael Espindola | 666a2ab | 2013-12-18 16:38:48 +0000 | [diff] [blame] | 59 | const LangOptions &langopts, bool TimePasses, |
| 60 | const std::string &infile, llvm::Module *LinkModule, |
| 61 | raw_ostream *OS, LLVMContext &C) |
| 62 | : Diags(_Diags), Action(action), CodeGenOpts(compopts), |
| 63 | TargetOpts(targetopts), LangOpts(langopts), AsmOutStream(OS), |
| 64 | Context(), LLVMIRGeneration("LLVM IR Generation Time"), |
| 65 | Gen(CreateLLVMCodeGen(Diags, infile, compopts, targetopts, C)), |
| 66 | LinkModule(LinkModule) { |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 67 | llvm::TimePassesIsEnabled = TimePasses; |
Chris Lattner | deffa13 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 68 | } |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 69 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 70 | llvm::Module *takeModule() { return TheModule.release(); } |
| 71 | llvm::Module *takeLinkModule() { return LinkModule.release(); } |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 72 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 73 | void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override { |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 74 | Gen->HandleCXXStaticMemberVarInstantiation(VD); |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 77 | void Initialize(ASTContext &Ctx) override { |
Chris Lattner | 5cf49fe | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 78 | Context = &Ctx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 80 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 81 | LLVMIRGeneration.startTimer(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 5cf49fe | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 83 | Gen->Initialize(Ctx); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 84 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 85 | TheModule.reset(Gen->GetModule()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 87 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 88 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 89 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 91 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 92 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 93 | Context->getSourceManager(), |
| 94 | "LLVM IR generation of declaration"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 96 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 97 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 98 | |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 99 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 100 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 101 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 102 | LLVMIRGeneration.stopTimer(); |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 103 | |
| 104 | return true; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 105 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 107 | void HandleTranslationUnit(ASTContext &C) override { |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 108 | { |
Chris Lattner | e46de75 | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 109 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 110 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 111 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 112 | |
Chris Lattner | cf16983 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 113 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | a94d873 | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 114 | |
Daniel Dunbar | 8e70505 | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 115 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 116 | LLVMIRGeneration.stopTimer(); |
| 117 | } |
Chris Lattner | 263d64c | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 118 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 119 | // Silently ignore if we weren't initialized for some reason. |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 120 | if (!TheModule) |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 121 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 123 | // Make sure IR generation is happy with the module. This is released by |
| 124 | // the module provider. |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 125 | llvm::Module *M = Gen->ReleaseModule(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 126 | if (!M) { |
| 127 | // The module has been released by IR gen on failures, do not double |
| 128 | // free. |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 129 | TheModule.release(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
| 133 | assert(TheModule.get() == M && |
| 134 | "Unexpected module change during IR generation"); |
| 135 | |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 136 | // Link LinkModule into this module if present, preserving its validity. |
| 137 | if (LinkModule) { |
| 138 | std::string ErrorMsg; |
| 139 | if (Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource, |
| 140 | &ErrorMsg)) { |
| 141 | Diags.Report(diag::err_fe_cannot_link_module) |
| 142 | << LinkModule->getModuleIdentifier() << ErrorMsg; |
| 143 | return; |
| 144 | } |
| 145 | } |
| 146 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 147 | // Install an inline asm handler so that diagnostics get printed through |
| 148 | // our diagnostics hooks. |
| 149 | LLVMContext &Ctx = TheModule->getContext(); |
Chris Lattner | 068f2ab | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 150 | LLVMContext::InlineAsmDiagHandlerTy OldHandler = |
| 151 | Ctx.getInlineAsmDiagnosticHandler(); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 152 | void *OldContext = Ctx.getInlineAsmDiagnosticContext(); |
Chris Lattner | 068f2ab | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 153 | Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this); |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 154 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 155 | LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler = |
| 156 | Ctx.getDiagnosticHandler(); |
| 157 | void *OldDiagnosticContext = Ctx.getDiagnosticContext(); |
| 158 | Ctx.setDiagnosticHandler(DiagnosticHandler, this); |
| 159 | |
Dan Gohman | fec0ff8 | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 160 | EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
Alp Toker | e83b906 | 2014-01-02 15:08:04 +0000 | [diff] [blame] | 161 | C.getTargetInfo().getTargetDescription(), |
Daniel Dunbar | 3e11152 | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 162 | TheModule.get(), Action, AsmOutStream); |
Rafael Espindola | 666a2ab | 2013-12-18 16:38:48 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | f976d1b | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 164 | Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 165 | |
| 166 | Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 167 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 169 | void HandleTagDeclDefinition(TagDecl *D) override { |
Chris Lattner | eae6cb6 | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 170 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 171 | Context->getSourceManager(), |
| 172 | "LLVM IR generation of declaration"); |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 173 | Gen->HandleTagDeclDefinition(D); |
| 174 | } |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 175 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 176 | void HandleTagDeclRequiredDefinition(const TagDecl *D) override { |
David Blaikie | 48ad6dc | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 177 | Gen->HandleTagDeclRequiredDefinition(D); |
| 178 | } |
| 179 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 180 | void CompleteTentativeDefinition(VarDecl *D) override { |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 181 | Gen->CompleteTentativeDefinition(D); |
| 182 | } |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 183 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 184 | void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 185 | Gen->HandleVTable(RD, DefinitionRequired); |
| 186 | } |
| 187 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 188 | void HandleLinkerOptionPragma(llvm::StringRef Opts) override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 189 | Gen->HandleLinkerOptionPragma(Opts); |
| 190 | } |
| 191 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 192 | void HandleDetectMismatch(llvm::StringRef Name, |
| 193 | llvm::StringRef Value) override { |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 194 | Gen->HandleDetectMismatch(Name, Value); |
| 195 | } |
| 196 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 197 | void HandleDependentLibrary(llvm::StringRef Opts) override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 198 | Gen->HandleDependentLibrary(Opts); |
| 199 | } |
| 200 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 201 | static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context, |
| 202 | unsigned LocCookie) { |
| 203 | SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie); |
| 204 | ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc); |
| 205 | } |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 206 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 207 | static void DiagnosticHandler(const llvm::DiagnosticInfo &DI, |
| 208 | void *Context) { |
| 209 | ((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI); |
| 210 | } |
| 211 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 212 | void InlineAsmDiagHandler2(const llvm::SMDiagnostic &, |
| 213 | SourceLocation LocCookie); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 214 | |
| 215 | void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI); |
| 216 | /// \brief Specialized handler for InlineAsm diagnostic. |
| 217 | /// \return True if the diagnostic has been successfully reported, false |
| 218 | /// otherwise. |
| 219 | bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D); |
| 220 | /// \brief Specialized handler for StackSize diagnostic. |
| 221 | /// \return True if the diagnostic has been successfully reported, false |
| 222 | /// otherwise. |
| 223 | bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 224 | /// \brief Specialized handler for the optimization diagnostic. |
| 225 | /// Note that this handler only accepts remarks and it always handles |
| 226 | /// them. |
| 227 | void |
| 228 | OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark &D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | }; |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 230 | |
| 231 | void BackendConsumer::anchor() {} |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 234 | /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr |
| 235 | /// buffer to be a valid FullSourceLoc. |
| 236 | static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, |
| 237 | SourceManager &CSM) { |
| 238 | // Get both the clang and llvm source managers. The location is relative to |
| 239 | // 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] | 240 | // a copy to the Clang source manager. |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 241 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 243 | // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr |
| 244 | // already owns its one and clang::SourceManager wants to own its one. |
| 245 | const MemoryBuffer *LBuf = |
| 246 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 248 | // Create the copy and transfer ownership to clang::SourceManager. |
| 249 | llvm::MemoryBuffer *CBuf = |
| 250 | llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(), |
| 251 | LBuf->getBufferIdentifier()); |
Alp Toker | 6ac2cd0 | 2014-05-16 17:23:01 +0000 | [diff] [blame^] | 252 | FileID FID = CSM.createFileID(CBuf); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 254 | // Translate the offset into the file. |
| 255 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 256 | SourceLocation NewLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 257 | CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset); |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 258 | return FullSourceLoc(NewLoc, CSM); |
| 259 | } |
| 260 | |
Chris Lattner | 6d67213 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 262 | /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an |
| 263 | /// error parsing inline asm. The SMDiagnostic indicates the error relative to |
Daniel Dunbar | 50aa0a5 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 264 | /// the temporary memory buffer that the inline asm parser has set up. |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 265 | void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D, |
| 266 | SourceLocation LocCookie) { |
| 267 | // There are a couple of different kinds of errors we could get here. First, |
| 268 | // we re-format the SMDiagnostic in terms of a clang diagnostic. |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 269 | |
| 270 | // Strip "error: " off the start of the message string. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 271 | StringRef Message = D.getMessage(); |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 272 | if (Message.startswith("error: ")) |
| 273 | Message = Message.substr(7); |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 274 | |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 275 | // If the SMDiagnostic has an inline asm source location, translate it. |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 276 | FullSourceLoc Loc; |
Chris Lattner | 79f67a7 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 277 | if (D.getLoc() != SMLoc()) |
| 278 | Loc = ConvertBackendLocation(D, Context->getSourceManager()); |
Chris Lattner | f4a4bec | 2012-01-31 06:13:55 +0000 | [diff] [blame] | 279 | |
Argyrios Kyrtzidis | a11b35a | 2012-02-01 06:36:49 +0000 | [diff] [blame] | 280 | |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 281 | // If this problem has clang-level source location information, report the |
| 282 | // issue as being an error in the source with a note showing the instantiated |
| 283 | // code. |
| 284 | if (LocCookie.isValid()) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 285 | Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message); |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 286 | |
Benjamin Kramer | e06b2b7 | 2011-10-16 10:48:28 +0000 | [diff] [blame] | 287 | if (D.getLoc().isValid()) { |
| 288 | DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here); |
| 289 | // Convert the SMDiagnostic ranges into SourceRange and attach them |
| 290 | // to the diagnostic. |
| 291 | for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) { |
| 292 | std::pair<unsigned, unsigned> Range = D.getRanges()[i]; |
| 293 | unsigned Column = D.getColumnNo(); |
| 294 | B << SourceRange(Loc.getLocWithOffset(Range.first - Column), |
| 295 | Loc.getLocWithOffset(Range.second - Column)); |
| 296 | } |
| 297 | } |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 301 | // Otherwise, report the backend error as occurring in the generated .s file. |
Chris Lattner | c7ed7ea | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 302 | // If Loc is invalid, we still need to report the error, it just gets no |
| 303 | // location info. |
| 304 | Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message); |
Chris Lattner | 5ec32e7 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 307 | #define ComputeDiagID(Severity, GroupName, DiagID) \ |
| 308 | do { \ |
| 309 | switch (Severity) { \ |
| 310 | case llvm::DS_Error: \ |
| 311 | DiagID = diag::err_fe_##GroupName; \ |
| 312 | break; \ |
| 313 | case llvm::DS_Warning: \ |
| 314 | DiagID = diag::warn_fe_##GroupName; \ |
| 315 | break; \ |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 316 | case llvm::DS_Remark: \ |
| 317 | llvm_unreachable("'remark' severity not expected"); \ |
| 318 | break; \ |
| 319 | case llvm::DS_Note: \ |
| 320 | DiagID = diag::note_fe_##GroupName; \ |
| 321 | break; \ |
| 322 | } \ |
| 323 | } while (false) |
| 324 | |
| 325 | #define ComputeDiagRemarkID(Severity, GroupName, DiagID) \ |
| 326 | do { \ |
| 327 | switch (Severity) { \ |
| 328 | case llvm::DS_Error: \ |
| 329 | DiagID = diag::err_fe_##GroupName; \ |
| 330 | break; \ |
| 331 | case llvm::DS_Warning: \ |
| 332 | DiagID = diag::warn_fe_##GroupName; \ |
| 333 | break; \ |
| 334 | case llvm::DS_Remark: \ |
| 335 | DiagID = diag::remark_fe_##GroupName; \ |
| 336 | break; \ |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 337 | case llvm::DS_Note: \ |
| 338 | DiagID = diag::note_fe_##GroupName; \ |
| 339 | break; \ |
| 340 | } \ |
| 341 | } while (false) |
| 342 | |
| 343 | bool |
| 344 | BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) { |
| 345 | unsigned DiagID; |
| 346 | ComputeDiagID(D.getSeverity(), inline_asm, DiagID); |
| 347 | std::string Message = D.getMsgStr().str(); |
| 348 | |
| 349 | // If this problem has clang-level source location information, report the |
Tobias Grosser | bd25beb | 2014-02-26 10:21:56 +0000 | [diff] [blame] | 350 | // 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] | 351 | // code. |
| 352 | SourceLocation LocCookie = |
| 353 | SourceLocation::getFromRawEncoding(D.getLocCookie()); |
| 354 | if (LocCookie.isValid()) |
| 355 | Diags.Report(LocCookie, DiagID).AddString(Message); |
| 356 | else { |
| 357 | // Otherwise, report the backend diagnostic as occurring in the generated |
| 358 | // .s file. |
| 359 | // If Loc is invalid, we still need to report the diagnostic, it just gets |
| 360 | // no location info. |
| 361 | FullSourceLoc Loc; |
| 362 | Diags.Report(Loc, DiagID).AddString(Message); |
| 363 | } |
| 364 | // We handled all the possible severities. |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | bool |
| 369 | BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) { |
| 370 | if (D.getSeverity() != llvm::DS_Warning) |
| 371 | // For now, the only support we have for StackSize diagnostic is warning. |
| 372 | // We do not know how to format other severities. |
| 373 | return false; |
| 374 | |
| 375 | // FIXME: We should demangle the function name. |
| 376 | // FIXME: Is there a way to get a location for that function? |
| 377 | FullSourceLoc Loc; |
| 378 | Diags.Report(Loc, diag::warn_fe_backend_frame_larger_than) |
| 379 | << D.getStackSize() << D.getFunction().getName(); |
| 380 | return true; |
| 381 | } |
| 382 | |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 383 | void BackendConsumer::OptimizationRemarkHandler( |
| 384 | const llvm::DiagnosticInfoOptimizationRemark &D) { |
| 385 | // We only support remarks. |
Eric Christopher | 25c4e67 | 2014-05-02 17:52:19 +0000 | [diff] [blame] | 386 | assert(D.getSeverity() == llvm::DS_Remark); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 387 | |
| 388 | // Optimization remarks are active only if -Rpass=regexp is given and the |
| 389 | // regular expression pattern in 'regexp' matches the name of the pass |
| 390 | // name in \p D. |
| 391 | if (CodeGenOpts.OptimizationRemarkPattern && |
| 392 | CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName())) { |
| 393 | SourceManager &SourceMgr = Context->getSourceManager(); |
| 394 | FileManager &FileMgr = SourceMgr.getFileManager(); |
| 395 | StringRef Filename; |
| 396 | unsigned Line, Column; |
| 397 | D.getLocation(&Filename, &Line, &Column); |
| 398 | SourceLocation Loc; |
Diego Novillo | 6dc9c48 | 2014-05-08 13:49:54 +0000 | [diff] [blame] | 399 | const FileEntry *FE = FileMgr.getFile(Filename); |
| 400 | if (FE && Line > 0) { |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 401 | // If -gcolumn-info was not used, Column will be 0. This upsets the |
| 402 | // source manager, so if Column is not set, set it to 1. |
| 403 | if (Column == 0) |
| 404 | Column = 1; |
Diego Novillo | 6dc9c48 | 2014-05-08 13:49:54 +0000 | [diff] [blame] | 405 | Loc = SourceMgr.translateFileLineCol(FE, Line, Column); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 406 | } |
Diego Novillo | b344224 | 2014-04-22 19:56:49 +0000 | [diff] [blame] | 407 | Diags.Report(Loc, diag::remark_fe_backend_optimization_remark) |
| 408 | << AddFlagValue(D.getPassName()) << D.getMsg().str(); |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 409 | |
| 410 | if (Line == 0) |
| 411 | // If we could not extract a source location for the diagnostic, |
| 412 | // inform the user how they can get source locations back. |
| 413 | // |
| 414 | // FIXME: We should really be generating !srcloc annotations when |
| 415 | // -Rpass is used. !srcloc annotations need to be emitted in |
| 416 | // approximately the same spots as !dbg nodes. |
| 417 | Diags.Report(diag::note_fe_backend_optimization_remark_missing_loc); |
Diego Novillo | 6dc9c48 | 2014-05-08 13:49:54 +0000 | [diff] [blame] | 418 | else if (Loc.isInvalid()) |
| 419 | // If we were not able to translate the file:line:col information |
| 420 | // back to a SourceLocation, at least emit a note stating that |
| 421 | // we could not translate this location. This can happen in the |
| 422 | // case of #line directives. |
| 423 | Diags.Report(diag::note_fe_backend_optimization_remark_invalid_loc) |
| 424 | << Filename << Line << Column; |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 428 | /// \brief This function is invoked when the backend needs |
| 429 | /// to report something to the user. |
| 430 | void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) { |
| 431 | unsigned DiagID = diag::err_fe_inline_asm; |
| 432 | llvm::DiagnosticSeverity Severity = DI.getSeverity(); |
| 433 | // Get the diagnostic ID based. |
| 434 | switch (DI.getKind()) { |
| 435 | case llvm::DK_InlineAsm: |
| 436 | if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI))) |
| 437 | return; |
| 438 | ComputeDiagID(Severity, inline_asm, DiagID); |
| 439 | break; |
| 440 | case llvm::DK_StackSize: |
| 441 | if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI))) |
| 442 | return; |
| 443 | ComputeDiagID(Severity, backend_frame_larger_than, DiagID); |
| 444 | break; |
Diego Novillo | 829b170 | 2014-04-16 16:54:24 +0000 | [diff] [blame] | 445 | case llvm::DK_OptimizationRemark: |
| 446 | // Optimization remarks are always handled completely by this |
| 447 | // handler. There is no generic way of emitting them. |
| 448 | OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemark>(DI)); |
| 449 | return; |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 450 | default: |
| 451 | // 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] | 452 | ComputeDiagRemarkID(Severity, backend_plugin, DiagID); |
Quentin Colombet | 728c554 | 2014-02-06 18:30:43 +0000 | [diff] [blame] | 453 | break; |
| 454 | } |
| 455 | std::string MsgStorage; |
| 456 | { |
| 457 | raw_string_ostream Stream(MsgStorage); |
| 458 | DiagnosticPrinterRawOStream DP(Stream); |
| 459 | DI.print(DP); |
| 460 | } |
| 461 | |
| 462 | // Report the backend message using the usual diagnostic mechanism. |
| 463 | FullSourceLoc Loc; |
| 464 | Diags.Report(Loc, DiagID).AddString(MsgStorage); |
| 465 | } |
| 466 | #undef ComputeDiagID |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 467 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 468 | CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext) |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 469 | : Act(_Act), LinkModule(0), |
| 470 | VMContext(_VMContext ? _VMContext : new LLVMContext), |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 471 | OwnsVMContext(!_VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 472 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 473 | CodeGenAction::~CodeGenAction() { |
| 474 | TheModule.reset(); |
| 475 | if (OwnsVMContext) |
| 476 | delete VMContext; |
| 477 | } |
Daniel Dunbar | e8ecf9a | 2010-02-25 20:37:44 +0000 | [diff] [blame] | 478 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 479 | bool CodeGenAction::hasIRSupport() const { return true; } |
| 480 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 481 | void CodeGenAction::EndSourceFileAction() { |
| 482 | // If the consumer creation failed, do nothing. |
| 483 | if (!getCompilerInstance().hasASTConsumer()) |
| 484 | return; |
| 485 | |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 486 | // If we were given a link module, release consumer's ownership of it. |
| 487 | if (LinkModule) |
| 488 | BEConsumer->takeLinkModule(); |
| 489 | |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 490 | // Steal the module from the consumer. |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 491 | TheModule.reset(BEConsumer->takeModule()); |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 494 | llvm::Module *CodeGenAction::takeModule() { return TheModule.release(); } |
Daniel Dunbar | 400a693 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 495 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 496 | llvm::LLVMContext *CodeGenAction::takeLLVMContext() { |
| 497 | OwnsVMContext = false; |
| 498 | return VMContext; |
| 499 | } |
| 500 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 501 | static raw_ostream *GetOutputStream(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 502 | StringRef InFile, |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 503 | BackendAction Action) { |
| 504 | switch (Action) { |
| 505 | case Backend_EmitAssembly: |
| 506 | return CI.createDefaultOutputFile(false, InFile, "s"); |
| 507 | case Backend_EmitLL: |
| 508 | return CI.createDefaultOutputFile(false, InFile, "ll"); |
| 509 | case Backend_EmitBC: |
| 510 | return CI.createDefaultOutputFile(true, InFile, "bc"); |
| 511 | case Backend_EmitNothing: |
| 512 | return 0; |
| 513 | case Backend_EmitMCNull: |
| 514 | case Backend_EmitObj: |
| 515 | return CI.createDefaultOutputFile(true, InFile, "o"); |
| 516 | } |
| 517 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 518 | llvm_unreachable("Invalid action!"); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 521 | ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 522 | StringRef InFile) { |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 523 | BackendAction BA = static_cast<BackendAction>(Act); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 524 | std::unique_ptr<raw_ostream> OS(GetOutputStream(CI, InFile, BA)); |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 525 | if (BA != Backend_EmitNothing && !OS) |
| 526 | return 0; |
| 527 | |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 528 | llvm::Module *LinkModuleToUse = LinkModule; |
| 529 | |
| 530 | // If we were not given a link module, and the user requested that one be |
| 531 | // loaded from bitcode, do so now. |
| 532 | const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile; |
| 533 | if (!LinkModuleToUse && !LinkBCFile.empty()) { |
| 534 | std::string ErrorStr; |
| 535 | |
| 536 | llvm::MemoryBuffer *BCBuf = |
| 537 | CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr); |
| 538 | if (!BCBuf) { |
| 539 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
| 540 | << LinkBCFile << ErrorStr; |
| 541 | return 0; |
| 542 | } |
| 543 | |
Rafael Espindola | 6b6004a | 2014-01-13 18:31:09 +0000 | [diff] [blame] | 544 | ErrorOr<llvm::Module *> ModuleOrErr = |
| 545 | getLazyBitcodeModule(BCBuf, *VMContext); |
| 546 | if (error_code EC = ModuleOrErr.getError()) { |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 547 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
Rafael Espindola | 6b6004a | 2014-01-13 18:31:09 +0000 | [diff] [blame] | 548 | << LinkBCFile << EC.message(); |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 549 | return 0; |
| 550 | } |
Rafael Espindola | 6b6004a | 2014-01-13 18:31:09 +0000 | [diff] [blame] | 551 | LinkModuleToUse = ModuleOrErr.get(); |
Peter Collingbourne | f1d76db | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 554 | BEConsumer = new BackendConsumer(BA, CI.getDiagnostics(), CI.getCodeGenOpts(), |
| 555 | CI.getTargetOpts(), CI.getLangOpts(), |
| 556 | CI.getFrontendOpts().ShowTimers, InFile, |
| 557 | LinkModuleToUse, OS.release(), *VMContext); |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 558 | return BEConsumer; |
Daniel Dunbar | c13935e | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 559 | } |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 560 | |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 561 | void CodeGenAction::ExecuteAction() { |
| 562 | // If this is an IR file, we have to treat it specially. |
| 563 | if (getCurrentFileKind() == IK_LLVM_IR) { |
| 564 | BackendAction BA = static_cast<BackendAction>(Act); |
| 565 | CompilerInstance &CI = getCompilerInstance(); |
| 566 | raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA); |
| 567 | if (BA != Backend_EmitNothing && !OS) |
| 568 | return; |
| 569 | |
| 570 | bool Invalid; |
| 571 | SourceManager &SM = CI.getSourceManager(); |
| 572 | const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(), |
| 573 | &Invalid); |
| 574 | if (Invalid) |
| 575 | return; |
| 576 | |
| 577 | // FIXME: This is stupid, IRReader shouldn't take ownership. |
| 578 | llvm::MemoryBuffer *MainFileCopy = |
| 579 | llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(), |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 580 | getCurrentFile()); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 581 | |
| 582 | llvm::SMDiagnostic Err; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 583 | TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext)); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 584 | if (!TheModule) { |
| 585 | // Translate from the diagnostic info to the SourceManager location. |
Argyrios Kyrtzidis | 27bf76d | 2011-09-19 20:40:38 +0000 | [diff] [blame] | 586 | SourceLocation Loc = SM.translateFileLineCol( |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 587 | SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(), |
| 588 | Err.getColumnNo() + 1); |
| 589 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 590 | // Strip off a leading diagnostic code if there is one. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 591 | StringRef Msg = Err.getMessage(); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 592 | if (Msg.startswith("error: ")) |
| 593 | Msg = Msg.substr(7); |
Benjamin Kramer | 778b8b8 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 594 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 595 | unsigned DiagID = |
| 596 | CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
Benjamin Kramer | 778b8b8 | 2012-03-16 22:31:42 +0000 | [diff] [blame] | 597 | |
Alp Toker | bc043f2 | 2013-12-21 05:20:03 +0000 | [diff] [blame] | 598 | CI.getDiagnostics().Report(Loc, DiagID) << Msg; |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 599 | return; |
| 600 | } |
Rafael Espindola | d5e81e5 | 2013-12-20 22:01:25 +0000 | [diff] [blame] | 601 | const TargetOptions &TargetOpts = CI.getTargetOpts(); |
| 602 | if (TheModule->getTargetTriple() != TargetOpts.Triple) { |
| 603 | unsigned DiagID = CI.getDiagnostics().getCustomDiagID( |
| 604 | DiagnosticsEngine::Warning, |
| 605 | "overriding the module target triple with %0"); |
| 606 | |
| 607 | CI.getDiagnostics().Report(SourceLocation(), DiagID) << TargetOpts.Triple; |
| 608 | TheModule->setTargetTriple(TargetOpts.Triple); |
| 609 | } |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 610 | |
Alp Toker | e83b906 | 2014-01-02 15:08:04 +0000 | [diff] [blame] | 611 | EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts, |
| 612 | CI.getLangOpts(), CI.getTarget().getTargetDescription(), |
| 613 | TheModule.get(), BA, OS); |
Daniel Dunbar | 6f8362c | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
| 617 | // Otherwise follow the normal AST path. |
| 618 | this->ASTFrontendAction::ExecuteAction(); |
| 619 | } |
| 620 | |
| 621 | // |
| 622 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 623 | void EmitAssemblyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 624 | EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext) |
| 625 | : CodeGenAction(Backend_EmitAssembly, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 626 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 627 | void EmitBCAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 628 | EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext) |
| 629 | : CodeGenAction(Backend_EmitBC, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 630 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 631 | void EmitLLVMAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 632 | EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext) |
| 633 | : CodeGenAction(Backend_EmitLL, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 634 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 635 | void EmitLLVMOnlyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 636 | EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext) |
| 637 | : CodeGenAction(Backend_EmitNothing, _VMContext) {} |
Daniel Dunbar | cea0c70 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 638 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 639 | void EmitCodeGenOnlyAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 640 | EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext) |
| 641 | : CodeGenAction(Backend_EmitMCNull, _VMContext) {} |
Daniel Dunbar | 4c77a64 | 2010-05-25 18:41:01 +0000 | [diff] [blame] | 642 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 643 | void EmitObjAction::anchor() { } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 644 | EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext) |
| 645 | : CodeGenAction(Backend_EmitObj, _VMContext) {} |