blob: 0e7b6d8a71d426a03cfb97109aa7f8fa1c67172b [file] [log] [blame]
Daniel Dunbarcea0c702010-02-25 04:37:45 +00001//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
Daniel Dunbarc13935e2008-10-21 23:49:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Alex Lorenzee024992014-08-04 18:41:51 +000010#include "CoverageMappingGen.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000012#include "clang/AST/ASTContext.h"
Hans Wennborga926d842014-05-23 20:37:38 +000013#include "clang/AST/DeclCXX.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000014#include "clang/AST/DeclGroup.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Basic/TargetInfo.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000018#include "clang/CodeGen/BackendUtil.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000019#include "clang/CodeGen/CodeGenAction.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000020#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbarcea0c702010-02-25 04:37:45 +000021#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbaracadc552009-12-03 09:12:54 +000022#include "clang/Frontend/FrontendDiagnostic.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000023#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "llvm/ADT/SmallString.h"
25#include "llvm/Bitcode/ReaderWriter.h"
Diego Novillo829b1702014-04-16 16:54:24 +000026#include "llvm/IR/DebugInfo.h"
Quentin Colombet728c5542014-02-06 18:30:43 +000027#include "llvm/IR/DiagnosticInfo.h"
28#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000029#include "llvm/IR/LLVMContext.h"
30#include "llvm/IR/Module.h"
Chandler Carruthb45836a2013-03-26 02:25:54 +000031#include "llvm/IRReader/IRReader.h"
Chandler Carruth00fa3f72014-03-06 03:46:44 +000032#include "llvm/Linker/Linker.h"
Daniel Dunbar3e111522010-06-07 23:21:04 +000033#include "llvm/Pass.h"
Chris Lattner5ec32e72010-04-06 18:38:50 +000034#include "llvm/Support/MemoryBuffer.h"
35#include "llvm/Support/SourceMgr.h"
Chris Lattner263d64c2009-02-18 01:37:30 +000036#include "llvm/Support/Timer.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000037#include <memory>
Daniel Dunbarc13935e2008-10-21 23:49:24 +000038using namespace clang;
39using namespace llvm;
40
Nico Weber2992efa2011-01-25 20:34:14 +000041namespace clang {
Benjamin Kramer16634c22009-11-28 10:07:24 +000042 class BackendConsumer : public ASTConsumer {
David Blaikie68e081d2011-12-20 02:48:34 +000043 virtual void anchor();
David Blaikie9c902b52011-09-25 23:23:43 +000044 DiagnosticsEngine &Diags;
Daniel Dunbarc13935e2008-10-21 23:49:24 +000045 BackendAction Action;
Daniel Dunbarde182242009-11-30 08:39:32 +000046 const CodeGenOptions &CodeGenOpts;
Daniel Dunbarde182242009-11-30 08:39:32 +000047 const TargetOptions &TargetOpts;
Dan Gohmanfec0ff82011-07-05 22:02:36 +000048 const LangOptions &LangOpts;
Rafael Espindola2f16bc12015-04-14 15:15:49 +000049 raw_pwrite_stream *AsmOutStream;
Chris Lattnereae6cb62009-03-05 08:00:35 +000050 ASTContext *Context;
Daniel Dunbarb3a36cf2008-10-29 08:50:02 +000051
Chris Lattner263d64c2009-02-18 01:37:30 +000052 Timer LLVMIRGeneration;
Mike Stump11289f42009-09-09 15:08:12 +000053
Ahmed Charlesb8984322014-03-07 20:03:18 +000054 std::unique_ptr<CodeGenerator> Gen;
Mike Stump11289f42009-09-09 15:08:12 +000055
Ahmed Charlesb8984322014-03-07 20:03:18 +000056 std::unique_ptr<llvm::Module> TheModule, LinkModule;
Daniel Dunbarc13935e2008-10-21 23:49:24 +000057
Mike Stump11289f42009-09-09 15:08:12 +000058 public:
Justin Bognercb082992015-05-22 22:16:55 +000059 BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
Adrian Prantle74f5252015-06-30 02:26:03 +000060 const HeaderSearchOptions &HeaderSearchOpts,
61 const PreprocessorOptions &PPOpts,
Justin Bognercb082992015-05-22 22:16:55 +000062 const CodeGenOptions &CodeGenOpts,
63 const TargetOptions &TargetOpts,
64 const LangOptions &LangOpts, bool TimePasses,
65 const std::string &InFile, llvm::Module *LinkModule,
Rafael Espindola2f16bc12015-04-14 15:15:49 +000066 raw_pwrite_stream *OS, LLVMContext &C,
Alex Lorenzee024992014-08-04 18:41:51 +000067 CoverageSourceInfo *CoverageInfo = nullptr)
Justin Bognercb082992015-05-22 22:16:55 +000068 : Diags(Diags), Action(Action), CodeGenOpts(CodeGenOpts),
69 TargetOpts(TargetOpts), LangOpts(LangOpts), AsmOutStream(OS),
Yaron Keren2f5ec2f2014-12-25 12:21:56 +000070 Context(nullptr), LLVMIRGeneration("LLVM IR Generation Time"),
Adrian Prantle74f5252015-06-30 02:26:03 +000071 Gen(CreateLLVMCodeGen(Diags, InFile, HeaderSearchOpts, PPOpts,
72 CodeGenOpts, C, CoverageInfo)),
Rafael Espindola666a2ab2013-12-18 16:38:48 +000073 LinkModule(LinkModule) {
Daniel Dunbar8e705052009-11-30 08:39:52 +000074 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattnerdeffa132009-02-18 01:23:44 +000075 }
Daniel Dunbarc13935e2008-10-21 23:49:24 +000076
David Blaikie780dd3b2014-08-29 05:08:19 +000077 std::unique_ptr<llvm::Module> takeModule() { return std::move(TheModule); }
Ahmed Charles9a16beb2014-03-07 19:33:25 +000078 llvm::Module *takeLinkModule() { return LinkModule.release(); }
Daniel Dunbar400a6932010-02-25 04:37:50 +000079
Craig Topper4f12f102014-03-12 06:41:41 +000080 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
Rafael Espindoladf88f6f2012-03-08 15:51:03 +000081 Gen->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +000082 }
83
Craig Topper4f12f102014-03-12 06:41:41 +000084 void Initialize(ASTContext &Ctx) override {
Sean Callanan61ea0572015-04-28 18:24:12 +000085 if (Context) {
86 assert(Context == &Ctx);
87 return;
88 }
89
Chris Lattner5cf49fe2009-03-28 02:18:25 +000090 Context = &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000091
Daniel Dunbar8e705052009-11-30 08:39:52 +000092 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000093 LLVMIRGeneration.startTimer();
Mike Stump11289f42009-09-09 15:08:12 +000094
Chris Lattner5cf49fe2009-03-28 02:18:25 +000095 Gen->Initialize(Ctx);
Daniel Dunbarc13935e2008-10-21 23:49:24 +000096
Daniel Dunbar400a6932010-02-25 04:37:50 +000097 TheModule.reset(Gen->GetModule());
Mike Stump11289f42009-09-09 15:08:12 +000098
Daniel Dunbar8e705052009-11-30 08:39:52 +000099 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +0000100 LLVMIRGeneration.stopTimer();
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000101 }
Mike Stump11289f42009-09-09 15:08:12 +0000102
Craig Topper4f12f102014-03-12 06:41:41 +0000103 bool HandleTopLevelDecl(DeclGroupRef D) override {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000104 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattnereae6cb62009-03-05 08:00:35 +0000105 Context->getSourceManager(),
106 "LLVM IR generation of declaration");
Mike Stump11289f42009-09-09 15:08:12 +0000107
Daniel Dunbar8e705052009-11-30 08:39:52 +0000108 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +0000109 LLVMIRGeneration.startTimer();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000110
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000111 Gen->HandleTopLevelDecl(D);
Chris Lattner263d64c2009-02-18 01:37:30 +0000112
Daniel Dunbar8e705052009-11-30 08:39:52 +0000113 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +0000114 LLVMIRGeneration.stopTimer();
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000115
116 return true;
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000117 }
Mike Stump11289f42009-09-09 15:08:12 +0000118
Hans Wennborga926d842014-05-23 20:37:38 +0000119 void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
120 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
121 Context->getSourceManager(),
122 "LLVM IR generation of inline method");
123 if (llvm::TimePassesIsEnabled)
124 LLVMIRGeneration.startTimer();
125
126 Gen->HandleInlineMethodDefinition(D);
127
128 if (llvm::TimePassesIsEnabled)
129 LLVMIRGeneration.stopTimer();
130 }
131
Craig Topper4f12f102014-03-12 06:41:41 +0000132 void HandleTranslationUnit(ASTContext &C) override {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000133 {
Chris Lattnere46de752009-03-06 06:46:31 +0000134 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbar8e705052009-11-30 08:39:52 +0000135 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000136 LLVMIRGeneration.startTimer();
Chris Lattner263d64c2009-02-18 01:37:30 +0000137
Chris Lattnercf169832009-03-28 04:11:33 +0000138 Gen->HandleTranslationUnit(C);
Daniel Dunbara94d8732008-11-11 06:35:39 +0000139
Daniel Dunbar8e705052009-11-30 08:39:52 +0000140 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000141 LLVMIRGeneration.stopTimer();
142 }
Chris Lattner263d64c2009-02-18 01:37:30 +0000143
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000144 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar3e111522010-06-07 23:21:04 +0000145 if (!TheModule)
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000146 return;
Mike Stump11289f42009-09-09 15:08:12 +0000147
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000148 // Make sure IR generation is happy with the module. This is released by
149 // the module provider.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000150 llvm::Module *M = Gen->ReleaseModule();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000151 if (!M) {
152 // The module has been released by IR gen on failures, do not double
153 // free.
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000154 TheModule.release();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000155 return;
156 }
157
158 assert(TheModule.get() == M &&
159 "Unexpected module change during IR generation");
160
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000161 // Link LinkModule into this module if present, preserving its validity.
162 if (LinkModule) {
Rafael Espindolac008c642014-10-27 23:02:34 +0000163 if (Linker::LinkModules(
Rafael Espindola37ad1342014-10-28 00:29:51 +0000164 M, LinkModule.get(),
Rafael Espindolac008c642014-10-27 23:02:34 +0000165 [=](const DiagnosticInfo &DI) { linkerDiagnosticHandler(DI); }))
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000166 return;
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000167 }
168
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000169 // Install an inline asm handler so that diagnostics get printed through
170 // our diagnostics hooks.
171 LLVMContext &Ctx = TheModule->getContext();
Chris Lattner068f2ab2010-11-17 08:13:04 +0000172 LLVMContext::InlineAsmDiagHandlerTy OldHandler =
173 Ctx.getInlineAsmDiagnosticHandler();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000174 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
Chris Lattner068f2ab2010-11-17 08:13:04 +0000175 Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000176
Quentin Colombet728c5542014-02-06 18:30:43 +0000177 LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
178 Ctx.getDiagnosticHandler();
179 void *OldDiagnosticContext = Ctx.getDiagnosticContext();
180 Ctx.setDiagnosticHandler(DiagnosticHandler, this);
181
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000182 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Alp Tokere83b9062014-01-02 15:08:04 +0000183 C.getTargetInfo().getTargetDescription(),
Daniel Dunbar3e111522010-06-07 23:21:04 +0000184 TheModule.get(), Action, AsmOutStream);
Rafael Espindola666a2ab2013-12-18 16:38:48 +0000185
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000186 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Quentin Colombet728c5542014-02-06 18:30:43 +0000187
188 Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext);
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000189 }
Mike Stump11289f42009-09-09 15:08:12 +0000190
Craig Topper4f12f102014-03-12 06:41:41 +0000191 void HandleTagDeclDefinition(TagDecl *D) override {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000192 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
193 Context->getSourceManager(),
194 "LLVM IR generation of declaration");
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000195 Gen->HandleTagDeclDefinition(D);
196 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000197
Craig Topper4f12f102014-03-12 06:41:41 +0000198 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
David Blaikie48ad6dc2013-07-13 21:08:14 +0000199 Gen->HandleTagDeclRequiredDefinition(D);
200 }
201
Craig Topper4f12f102014-03-12 06:41:41 +0000202 void CompleteTentativeDefinition(VarDecl *D) override {
Douglas Gregorbeecd582009-04-21 17:11:58 +0000203 Gen->CompleteTentativeDefinition(D);
204 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000205
Nico Weberb6a5d052015-01-15 04:07:35 +0000206 void HandleVTable(CXXRecordDecl *RD) override {
207 Gen->HandleVTable(RD);
Douglas Gregor88d292c2010-05-13 16:44:06 +0000208 }
209
Craig Topper4f12f102014-03-12 06:41:41 +0000210 void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000211 Gen->HandleLinkerOptionPragma(Opts);
212 }
213
Craig Topper4f12f102014-03-12 06:41:41 +0000214 void HandleDetectMismatch(llvm::StringRef Name,
215 llvm::StringRef Value) override {
Aaron Ballman5d041be2013-06-04 02:07:14 +0000216 Gen->HandleDetectMismatch(Name, Value);
217 }
218
Craig Topper4f12f102014-03-12 06:41:41 +0000219 void HandleDependentLibrary(llvm::StringRef Opts) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000220 Gen->HandleDependentLibrary(Opts);
221 }
222
Chris Lattner5ec32e72010-04-06 18:38:50 +0000223 static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
224 unsigned LocCookie) {
225 SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
226 ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
227 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000228
Rafael Espindolac008c642014-10-27 23:02:34 +0000229 void linkerDiagnosticHandler(const llvm::DiagnosticInfo &DI);
Rafael Espindola07065482014-10-25 04:06:14 +0000230
Quentin Colombet728c5542014-02-06 18:30:43 +0000231 static void DiagnosticHandler(const llvm::DiagnosticInfo &DI,
232 void *Context) {
233 ((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI);
234 }
235
Chris Lattner5ec32e72010-04-06 18:38:50 +0000236 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
237 SourceLocation LocCookie);
Quentin Colombet728c5542014-02-06 18:30:43 +0000238
239 void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
240 /// \brief Specialized handler for InlineAsm diagnostic.
241 /// \return True if the diagnostic has been successfully reported, false
242 /// otherwise.
243 bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
244 /// \brief Specialized handler for StackSize diagnostic.
245 /// \return True if the diagnostic has been successfully reported, false
246 /// otherwise.
247 bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
Diego Novillod23ec942014-05-29 19:55:06 +0000248 /// \brief Specialized handlers for optimization remarks.
249 /// Note that these handlers only accept remarks and they always handle
Diego Novillo829b1702014-04-16 16:54:24 +0000250 /// them.
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000251 void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
252 unsigned DiagID);
Diego Novillod23ec942014-05-29 19:55:06 +0000253 void
Diego Novillo829b1702014-04-16 16:54:24 +0000254 OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark &D);
Diego Novillod23ec942014-05-29 19:55:06 +0000255 void OptimizationRemarkHandler(
256 const llvm::DiagnosticInfoOptimizationRemarkMissed &D);
257 void OptimizationRemarkHandler(
258 const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D);
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000259 void OptimizationFailureHandler(
260 const llvm::DiagnosticInfoOptimizationFailure &D);
Mike Stump11289f42009-09-09 15:08:12 +0000261 };
David Blaikie68e081d2011-12-20 02:48:34 +0000262
263 void BackendConsumer::anchor() {}
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000264}
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000265
Chris Lattner79f67a72010-04-08 00:23:06 +0000266/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
267/// buffer to be a valid FullSourceLoc.
268static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
269 SourceManager &CSM) {
270 // Get both the clang and llvm source managers. The location is relative to
271 // a memory buffer that the LLVM Source Manager is handling, we need to add
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000272 // a copy to the Clang source manager.
Chris Lattner79f67a72010-04-08 00:23:06 +0000273 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000274
Chris Lattner79f67a72010-04-08 00:23:06 +0000275 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
276 // already owns its one and clang::SourceManager wants to own its one.
277 const MemoryBuffer *LBuf =
278 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000279
Chris Lattner79f67a72010-04-08 00:23:06 +0000280 // Create the copy and transfer ownership to clang::SourceManager.
Alp Tokeraa0dd5a2014-06-27 06:02:00 +0000281 // TODO: Avoid copying files into memory.
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000282 std::unique_ptr<llvm::MemoryBuffer> CBuf =
283 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
284 LBuf->getBufferIdentifier());
Alp Tokeraa0dd5a2014-06-27 06:02:00 +0000285 // FIXME: Keep a file ID map instead of creating new IDs for each location.
David Blaikie50a5f972014-08-29 07:59:55 +0000286 FileID FID = CSM.createFileID(std::move(CBuf));
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000287
Chris Lattner79f67a72010-04-08 00:23:06 +0000288 // Translate the offset into the file.
Alp Tokeraa0dd5a2014-06-27 06:02:00 +0000289 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000290 SourceLocation NewLoc =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000291 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
Chris Lattner79f67a72010-04-08 00:23:06 +0000292 return FullSourceLoc(NewLoc, CSM);
293}
294
Chris Lattner6d672132010-04-06 17:52:14 +0000295
Chris Lattner5ec32e72010-04-06 18:38:50 +0000296/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
297/// error parsing inline asm. The SMDiagnostic indicates the error relative to
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000298/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000299void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
300 SourceLocation LocCookie) {
301 // There are a couple of different kinds of errors we could get here. First,
302 // we re-format the SMDiagnostic in terms of a clang diagnostic.
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000303
304 // Strip "error: " off the start of the message string.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000305 StringRef Message = D.getMessage();
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000306 if (Message.startswith("error: "))
307 Message = Message.substr(7);
Chris Lattner5ec32e72010-04-06 18:38:50 +0000308
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000309 // If the SMDiagnostic has an inline asm source location, translate it.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000310 FullSourceLoc Loc;
Chris Lattner79f67a72010-04-08 00:23:06 +0000311 if (D.getLoc() != SMLoc())
312 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000313
Joey Gouly5798b262014-06-05 21:23:42 +0000314 unsigned DiagID;
315 switch (D.getKind()) {
316 case llvm::SourceMgr::DK_Error:
317 DiagID = diag::err_fe_inline_asm;
318 break;
319 case llvm::SourceMgr::DK_Warning:
320 DiagID = diag::warn_fe_inline_asm;
321 break;
322 case llvm::SourceMgr::DK_Note:
323 DiagID = diag::note_fe_inline_asm;
324 break;
325 }
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000326 // If this problem has clang-level source location information, report the
Joey Gouly5798b262014-06-05 21:23:42 +0000327 // issue in the source with a note showing the instantiated
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000328 // code.
329 if (LocCookie.isValid()) {
Joey Gouly5798b262014-06-05 21:23:42 +0000330 Diags.Report(LocCookie, DiagID).AddString(Message);
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000331
Benjamin Kramere06b2b72011-10-16 10:48:28 +0000332 if (D.getLoc().isValid()) {
333 DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
334 // Convert the SMDiagnostic ranges into SourceRange and attach them
335 // to the diagnostic.
336 for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
337 std::pair<unsigned, unsigned> Range = D.getRanges()[i];
338 unsigned Column = D.getColumnNo();
339 B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
340 Loc.getLocWithOffset(Range.second - Column));
341 }
342 }
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000343 return;
344 }
345
Joey Gouly5798b262014-06-05 21:23:42 +0000346 // Otherwise, report the backend issue as occurring in the generated .s file.
347 // If Loc is invalid, we still need to report the issue, it just gets no
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000348 // location info.
Joey Gouly5798b262014-06-05 21:23:42 +0000349 Diags.Report(Loc, DiagID).AddString(Message);
Chris Lattner5ec32e72010-04-06 18:38:50 +0000350}
351
Quentin Colombet728c5542014-02-06 18:30:43 +0000352#define ComputeDiagID(Severity, GroupName, DiagID) \
353 do { \
354 switch (Severity) { \
355 case llvm::DS_Error: \
356 DiagID = diag::err_fe_##GroupName; \
357 break; \
358 case llvm::DS_Warning: \
359 DiagID = diag::warn_fe_##GroupName; \
360 break; \
Tobias Grosser74160242014-02-28 09:11:08 +0000361 case llvm::DS_Remark: \
362 llvm_unreachable("'remark' severity not expected"); \
363 break; \
364 case llvm::DS_Note: \
365 DiagID = diag::note_fe_##GroupName; \
366 break; \
367 } \
368 } while (false)
369
370#define ComputeDiagRemarkID(Severity, GroupName, DiagID) \
371 do { \
372 switch (Severity) { \
373 case llvm::DS_Error: \
374 DiagID = diag::err_fe_##GroupName; \
375 break; \
376 case llvm::DS_Warning: \
377 DiagID = diag::warn_fe_##GroupName; \
378 break; \
379 case llvm::DS_Remark: \
380 DiagID = diag::remark_fe_##GroupName; \
381 break; \
Quentin Colombet728c5542014-02-06 18:30:43 +0000382 case llvm::DS_Note: \
383 DiagID = diag::note_fe_##GroupName; \
384 break; \
385 } \
386 } while (false)
387
388bool
389BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) {
390 unsigned DiagID;
391 ComputeDiagID(D.getSeverity(), inline_asm, DiagID);
392 std::string Message = D.getMsgStr().str();
393
394 // If this problem has clang-level source location information, report the
Tobias Grosserbd25beb2014-02-26 10:21:56 +0000395 // issue as being a problem in the source with a note showing the instantiated
Quentin Colombet728c5542014-02-06 18:30:43 +0000396 // code.
397 SourceLocation LocCookie =
398 SourceLocation::getFromRawEncoding(D.getLocCookie());
399 if (LocCookie.isValid())
400 Diags.Report(LocCookie, DiagID).AddString(Message);
401 else {
402 // Otherwise, report the backend diagnostic as occurring in the generated
403 // .s file.
404 // If Loc is invalid, we still need to report the diagnostic, it just gets
405 // no location info.
406 FullSourceLoc Loc;
407 Diags.Report(Loc, DiagID).AddString(Message);
408 }
409 // We handled all the possible severities.
410 return true;
411}
412
413bool
414BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) {
415 if (D.getSeverity() != llvm::DS_Warning)
416 // For now, the only support we have for StackSize diagnostic is warning.
417 // We do not know how to format other severities.
418 return false;
419
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000420 if (const Decl *ND = Gen->GetDeclForMangledName(D.getFunction().getName())) {
421 Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()),
422 diag::warn_fe_frame_larger_than)
423 << D.getStackSize() << Decl::castToDeclContext(ND);
424 return true;
425 }
426
427 return false;
Quentin Colombet728c5542014-02-06 18:30:43 +0000428}
429
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000430void BackendConsumer::EmitOptimizationMessage(
Tyler Nowickie4707712014-07-16 00:40:42 +0000431 const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) {
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000432 // We only support warnings and remarks.
433 assert(D.getSeverity() == llvm::DS_Remark ||
434 D.getSeverity() == llvm::DS_Warning);
Diego Novillo829b1702014-04-16 16:54:24 +0000435
Diego Novillod23ec942014-05-29 19:55:06 +0000436 SourceManager &SourceMgr = Context->getSourceManager();
437 FileManager &FileMgr = SourceMgr.getFileManager();
438 StringRef Filename;
439 unsigned Line, Column;
Alp Toker27506272014-06-05 22:11:12 +0000440 SourceLocation DILoc;
Diego Novillo86f884e2015-05-08 20:59:56 +0000441
442 if (D.isLocationAvailable()) {
443 D.getLocation(&Filename, &Line, &Column);
444 const FileEntry *FE = FileMgr.getFile(Filename);
445 if (FE && Line > 0) {
446 // If -gcolumn-info was not used, Column will be 0. This upsets the
447 // source manager, so pass 1 if Column is not set.
448 DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);
449 }
Diego Novillo829b1702014-04-16 16:54:24 +0000450 }
Alp Toker27506272014-06-05 22:11:12 +0000451
452 // If a location isn't available, try to approximate it using the associated
453 // function definition. We use the definition's right brace to differentiate
454 // from diagnostics that genuinely relate to the function itself.
455 FullSourceLoc Loc(DILoc, SourceMgr);
456 if (Loc.isInvalid())
457 if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName()))
458 Loc = FD->getASTContext().getFullLoc(FD->getBodyRBrace());
459
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000460 Diags.Report(Loc, DiagID)
461 << AddFlagValue(D.getPassName() ? D.getPassName() : "")
462 << D.getMsg().str();
Diego Novillod23ec942014-05-29 19:55:06 +0000463
Diego Novillo86f884e2015-05-08 20:59:56 +0000464 if (DILoc.isInvalid() && D.isLocationAvailable())
Diego Novillod23ec942014-05-29 19:55:06 +0000465 // If we were not able to translate the file:line:col information
466 // back to a SourceLocation, at least emit a note stating that
467 // we could not translate this location. This can happen in the
468 // case of #line directives.
Alp Toker27506272014-06-05 22:11:12 +0000469 Diags.Report(Loc, diag::note_fe_backend_optimization_remark_invalid_loc)
Diego Novillod23ec942014-05-29 19:55:06 +0000470 << Filename << Line << Column;
471}
472
473void BackendConsumer::OptimizationRemarkHandler(
474 const llvm::DiagnosticInfoOptimizationRemark &D) {
475 // Optimization remarks are active only if the -Rpass flag has a regular
476 // expression that matches the name of the pass name in \p D.
477 if (CodeGenOpts.OptimizationRemarkPattern &&
478 CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName()))
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000479 EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark);
Diego Novillod23ec942014-05-29 19:55:06 +0000480}
481
482void BackendConsumer::OptimizationRemarkHandler(
483 const llvm::DiagnosticInfoOptimizationRemarkMissed &D) {
484 // Missed optimization remarks are active only if the -Rpass-missed
485 // flag has a regular expression that matches the name of the pass
486 // name in \p D.
487 if (CodeGenOpts.OptimizationRemarkMissedPattern &&
488 CodeGenOpts.OptimizationRemarkMissedPattern->match(D.getPassName()))
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000489 EmitOptimizationMessage(D,
490 diag::remark_fe_backend_optimization_remark_missed);
Diego Novillod23ec942014-05-29 19:55:06 +0000491}
492
493void BackendConsumer::OptimizationRemarkHandler(
494 const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D) {
495 // Optimization analysis remarks are active only if the -Rpass-analysis
496 // flag has a regular expression that matches the name of the pass
497 // name in \p D.
498 if (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
499 CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName()))
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000500 EmitOptimizationMessage(
Diego Novillod23ec942014-05-29 19:55:06 +0000501 D, diag::remark_fe_backend_optimization_remark_analysis);
Diego Novillo829b1702014-04-16 16:54:24 +0000502}
503
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000504void BackendConsumer::OptimizationFailureHandler(
505 const llvm::DiagnosticInfoOptimizationFailure &D) {
506 EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure);
507}
508
Rafael Espindolac008c642014-10-27 23:02:34 +0000509void BackendConsumer::linkerDiagnosticHandler(const DiagnosticInfo &DI) {
Rafael Espindola07065482014-10-25 04:06:14 +0000510 if (DI.getSeverity() != DS_Error)
511 return;
512
513 std::string MsgStorage;
514 {
515 raw_string_ostream Stream(MsgStorage);
516 DiagnosticPrinterRawOStream DP(Stream);
517 DI.print(DP);
518 }
519
520 Diags.Report(diag::err_fe_cannot_link_module)
521 << LinkModule->getModuleIdentifier() << MsgStorage;
522}
523
Quentin Colombet728c5542014-02-06 18:30:43 +0000524/// \brief This function is invoked when the backend needs
525/// to report something to the user.
526void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
527 unsigned DiagID = diag::err_fe_inline_asm;
528 llvm::DiagnosticSeverity Severity = DI.getSeverity();
529 // Get the diagnostic ID based.
530 switch (DI.getKind()) {
531 case llvm::DK_InlineAsm:
532 if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI)))
533 return;
534 ComputeDiagID(Severity, inline_asm, DiagID);
535 break;
536 case llvm::DK_StackSize:
537 if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI)))
538 return;
539 ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
540 break;
Diego Novillo829b1702014-04-16 16:54:24 +0000541 case llvm::DK_OptimizationRemark:
542 // Optimization remarks are always handled completely by this
543 // handler. There is no generic way of emitting them.
544 OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemark>(DI));
545 return;
Diego Novillo9c89ff12014-05-29 16:19:27 +0000546 case llvm::DK_OptimizationRemarkMissed:
Diego Novillod23ec942014-05-29 19:55:06 +0000547 // Optimization remarks are always handled completely by this
548 // handler. There is no generic way of emitting them.
549 OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemarkMissed>(DI));
550 return;
Diego Novillo9c89ff12014-05-29 16:19:27 +0000551 case llvm::DK_OptimizationRemarkAnalysis:
Diego Novillod23ec942014-05-29 19:55:06 +0000552 // Optimization remarks are always handled completely by this
553 // handler. There is no generic way of emitting them.
554 OptimizationRemarkHandler(
555 cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI));
Diego Novillo9c89ff12014-05-29 16:19:27 +0000556 return;
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000557 case llvm::DK_OptimizationFailure:
558 // Optimization failures are always handled completely by this
559 // handler.
560 OptimizationFailureHandler(cast<DiagnosticInfoOptimizationFailure>(DI));
561 return;
Quentin Colombet728c5542014-02-06 18:30:43 +0000562 default:
563 // Plugin IDs are not bound to any value as they are set dynamically.
Tobias Grosser74160242014-02-28 09:11:08 +0000564 ComputeDiagRemarkID(Severity, backend_plugin, DiagID);
Quentin Colombet728c5542014-02-06 18:30:43 +0000565 break;
566 }
567 std::string MsgStorage;
568 {
569 raw_string_ostream Stream(MsgStorage);
570 DiagnosticPrinterRawOStream DP(Stream);
571 DI.print(DP);
572 }
573
574 // Report the backend message using the usual diagnostic mechanism.
575 FullSourceLoc Loc;
576 Diags.Report(Loc, DiagID).AddString(MsgStorage);
577}
578#undef ComputeDiagID
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000579
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000580CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
Craig Topper8a13c412014-05-21 05:09:00 +0000581 : Act(_Act), LinkModule(nullptr),
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000582 VMContext(_VMContext ? _VMContext : new LLVMContext),
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000583 OwnsVMContext(!_VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000584
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000585CodeGenAction::~CodeGenAction() {
586 TheModule.reset();
587 if (OwnsVMContext)
588 delete VMContext;
589}
Daniel Dunbare8ecf9a2010-02-25 20:37:44 +0000590
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000591bool CodeGenAction::hasIRSupport() const { return true; }
592
Daniel Dunbar400a6932010-02-25 04:37:50 +0000593void CodeGenAction::EndSourceFileAction() {
594 // If the consumer creation failed, do nothing.
595 if (!getCompilerInstance().hasASTConsumer())
596 return;
597
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000598 // If we were given a link module, release consumer's ownership of it.
599 if (LinkModule)
600 BEConsumer->takeLinkModule();
601
Daniel Dunbar400a6932010-02-25 04:37:50 +0000602 // Steal the module from the consumer.
David Blaikie780dd3b2014-08-29 05:08:19 +0000603 TheModule = BEConsumer->takeModule();
Daniel Dunbar400a6932010-02-25 04:37:50 +0000604}
605
Rafael Espindolae8337302014-08-19 14:36:35 +0000606std::unique_ptr<llvm::Module> CodeGenAction::takeModule() {
607 return std::move(TheModule);
608}
609
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000610llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
611 OwnsVMContext = false;
612 return VMContext;
613}
614
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000615static raw_pwrite_stream *
616GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000617 switch (Action) {
618 case Backend_EmitAssembly:
619 return CI.createDefaultOutputFile(false, InFile, "s");
620 case Backend_EmitLL:
621 return CI.createDefaultOutputFile(false, InFile, "ll");
622 case Backend_EmitBC:
623 return CI.createDefaultOutputFile(true, InFile, "bc");
624 case Backend_EmitNothing:
Craig Topper8a13c412014-05-21 05:09:00 +0000625 return nullptr;
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000626 case Backend_EmitMCNull:
Alp Tokerea046722014-06-03 17:23:34 +0000627 return CI.createNullOutputFile();
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000628 case Backend_EmitObj:
629 return CI.createDefaultOutputFile(true, InFile, "o");
630 }
631
David Blaikie83d382b2011-09-23 05:06:16 +0000632 llvm_unreachable("Invalid action!");
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000633}
634
David Blaikie6beb6aa2014-08-10 19:56:51 +0000635std::unique_ptr<ASTConsumer>
636CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000637 BackendAction BA = static_cast<BackendAction>(Act);
Artem Beleviched0577c2015-05-12 17:44:15 +0000638 raw_pwrite_stream *OS = GetOutputStream(CI, InFile, BA);
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000639 if (BA != Backend_EmitNothing && !OS)
Craig Topper8a13c412014-05-21 05:09:00 +0000640 return nullptr;
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000641
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000642 llvm::Module *LinkModuleToUse = LinkModule;
643
644 // If we were not given a link module, and the user requested that one be
645 // loaded from bitcode, do so now.
646 const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
647 if (!LinkModuleToUse && !LinkBCFile.empty()) {
Benjamin Kramera8857962014-10-26 22:44:13 +0000648 auto BCBuf = CI.getFileManager().getBufferForFile(LinkBCFile);
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000649 if (!BCBuf) {
650 CI.getDiagnostics().Report(diag::err_cannot_open_file)
Benjamin Kramera8857962014-10-26 22:44:13 +0000651 << LinkBCFile << BCBuf.getError().message();
Craig Topper8a13c412014-05-21 05:09:00 +0000652 return nullptr;
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000653 }
654
Rafael Espindola4914d3a2015-06-16 22:32:44 +0000655 ErrorOr<std::unique_ptr<llvm::Module>> ModuleOrErr =
Benjamin Kramera8857962014-10-26 22:44:13 +0000656 getLazyBitcodeModule(std::move(*BCBuf), *VMContext);
Rafael Espindolab3046992014-06-12 21:36:35 +0000657 if (std::error_code EC = ModuleOrErr.getError()) {
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000658 CI.getDiagnostics().Report(diag::err_cannot_open_file)
Rafael Espindola6b6004a2014-01-13 18:31:09 +0000659 << LinkBCFile << EC.message();
Craig Topper8a13c412014-05-21 05:09:00 +0000660 return nullptr;
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000661 }
Rafael Espindola4914d3a2015-06-16 22:32:44 +0000662 LinkModuleToUse = ModuleOrErr.get().release();
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000663 }
664
Alex Lorenzee024992014-08-04 18:41:51 +0000665 CoverageSourceInfo *CoverageInfo = nullptr;
666 // Add the preprocessor callback only when the coverage mapping is generated.
667 if (CI.getCodeGenOpts().CoverageMapping) {
668 CoverageInfo = new CoverageSourceInfo;
Craig Topperb8a70532014-09-10 04:53:53 +0000669 CI.getPreprocessor().addPPCallbacks(
670 std::unique_ptr<PPCallbacks>(CoverageInfo));
Alex Lorenzee024992014-08-04 18:41:51 +0000671 }
David Blaikie037e75b2014-08-10 21:06:17 +0000672 std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
Adrian Prantle74f5252015-06-30 02:26:03 +0000673 BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
674 CI.getPreprocessorOpts(), CI.getCodeGenOpts(), CI.getTargetOpts(),
David Blaikie037e75b2014-08-10 21:06:17 +0000675 CI.getLangOpts(), CI.getFrontendOpts().ShowTimers, InFile,
Artem Beleviched0577c2015-05-12 17:44:15 +0000676 LinkModuleToUse, OS, *VMContext, CoverageInfo));
David Blaikie6beb6aa2014-08-10 19:56:51 +0000677 BEConsumer = Result.get();
678 return std::move(Result);
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000679}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000680
Steven Wu15b385f2015-02-12 02:06:55 +0000681static void BitcodeInlineAsmDiagHandler(const llvm::SMDiagnostic &SM,
682 void *Context,
683 unsigned LocCookie) {
684 SM.print(nullptr, llvm::errs());
685}
686
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000687void CodeGenAction::ExecuteAction() {
688 // If this is an IR file, we have to treat it specially.
689 if (getCurrentFileKind() == IK_LLVM_IR) {
690 BackendAction BA = static_cast<BackendAction>(Act);
691 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000692 raw_pwrite_stream *OS = GetOutputStream(CI, getCurrentFile(), BA);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000693 if (BA != Backend_EmitNothing && !OS)
694 return;
695
696 bool Invalid;
697 SourceManager &SM = CI.getSourceManager();
Alp Toker7b463d52014-06-30 01:33:59 +0000698 FileID FID = SM.getMainFileID();
699 llvm::MemoryBuffer *MainFile = SM.getBuffer(FID, &Invalid);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000700 if (Invalid)
701 return;
702
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000703 llvm::SMDiagnostic Err;
Rafael Espindola32327732014-08-26 21:49:29 +0000704 TheModule = parseIR(MainFile->getMemBufferRef(), Err, *VMContext);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000705 if (!TheModule) {
Alp Toker7b463d52014-06-30 01:33:59 +0000706 // Translate from the diagnostic info to the SourceManager location if
707 // available.
708 // TODO: Unify this with ConvertBackendLocation()
709 SourceLocation Loc;
710 if (Err.getLineNo() > 0) {
711 assert(Err.getColumnNo() >= 0);
712 Loc = SM.translateFileLineCol(SM.getFileEntryForID(FID),
713 Err.getLineNo(), Err.getColumnNo() + 1);
714 }
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000715
Alp Tokerbc043f22013-12-21 05:20:03 +0000716 // Strip off a leading diagnostic code if there is one.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000717 StringRef Msg = Err.getMessage();
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000718 if (Msg.startswith("error: "))
719 Msg = Msg.substr(7);
Benjamin Kramer778b8b82012-03-16 22:31:42 +0000720
Alp Tokerbc043f22013-12-21 05:20:03 +0000721 unsigned DiagID =
722 CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
Benjamin Kramer778b8b82012-03-16 22:31:42 +0000723
Alp Tokerbc043f22013-12-21 05:20:03 +0000724 CI.getDiagnostics().Report(Loc, DiagID) << Msg;
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000725 return;
726 }
Rafael Espindolad5e81e52013-12-20 22:01:25 +0000727 const TargetOptions &TargetOpts = CI.getTargetOpts();
728 if (TheModule->getTargetTriple() != TargetOpts.Triple) {
Nico Weber6cf2df22015-01-29 06:25:59 +0000729 CI.getDiagnostics().Report(SourceLocation(),
730 diag::warn_fe_override_module)
731 << TargetOpts.Triple;
Rafael Espindolad5e81e52013-12-20 22:01:25 +0000732 TheModule->setTargetTriple(TargetOpts.Triple);
733 }
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000734
Steven Wu15b385f2015-02-12 02:06:55 +0000735 LLVMContext &Ctx = TheModule->getContext();
736 Ctx.setInlineAsmDiagnosticHandler(BitcodeInlineAsmDiagHandler);
Alp Tokere83b9062014-01-02 15:08:04 +0000737 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts,
738 CI.getLangOpts(), CI.getTarget().getTargetDescription(),
739 TheModule.get(), BA, OS);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000740 return;
741 }
742
743 // Otherwise follow the normal AST path.
744 this->ASTFrontendAction::ExecuteAction();
745}
746
747//
748
David Blaikie68e081d2011-12-20 02:48:34 +0000749void EmitAssemblyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000750EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
751 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000752
David Blaikie68e081d2011-12-20 02:48:34 +0000753void EmitBCAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000754EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
755 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000756
David Blaikie68e081d2011-12-20 02:48:34 +0000757void EmitLLVMAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000758EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
759 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000760
David Blaikie68e081d2011-12-20 02:48:34 +0000761void EmitLLVMOnlyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000762EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
763 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000764
David Blaikie68e081d2011-12-20 02:48:34 +0000765void EmitCodeGenOnlyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000766EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
767 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar4c77a642010-05-25 18:41:01 +0000768
David Blaikie68e081d2011-12-20 02:48:34 +0000769void EmitObjAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000770EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
771 : CodeGenAction(Backend_EmitObj, _VMContext) {}