blob: 044eec643bb0b6209e4d705a2d18b0c8a516792a [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"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000011#include "clang/CodeGen/CodeGenAction.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000012#include "clang/AST/ASTConsumer.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000013#include "clang/AST/ASTContext.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000014#include "clang/AST/DeclGroup.h"
Hans Wennborga926d842014-05-23 20:37:38 +000015#include "clang/AST/DeclCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/TargetInfo.h"
Alex Lorenzee024992014-08-04 18:41:51 +000019#include "clang/Lex/Preprocessor.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000020#include "clang/CodeGen/BackendUtil.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000021#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbarcea0c702010-02-25 04:37:45 +000022#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbaracadc552009-12-03 09:12:54 +000023#include "clang/Frontend/FrontendDiagnostic.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;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000049 raw_ostream *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:
David Blaikie9c902b52011-09-25 23:23:43 +000059 BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
Daniel Dunbar6d5824f2010-06-07 23:19:17 +000060 const CodeGenOptions &compopts,
Dan Gohmanfec0ff82011-07-05 22:02:36 +000061 const TargetOptions &targetopts,
Rafael Espindola666a2ab2013-12-18 16:38:48 +000062 const LangOptions &langopts, bool TimePasses,
63 const std::string &infile, llvm::Module *LinkModule,
Alex Lorenzee024992014-08-04 18:41:51 +000064 raw_ostream *OS, LLVMContext &C,
65 CoverageSourceInfo *CoverageInfo = nullptr)
Rafael Espindola666a2ab2013-12-18 16:38:48 +000066 : Diags(_Diags), Action(action), CodeGenOpts(compopts),
67 TargetOpts(targetopts), LangOpts(langopts), AsmOutStream(OS),
68 Context(), LLVMIRGeneration("LLVM IR Generation Time"),
Alex Lorenzee024992014-08-04 18:41:51 +000069 Gen(CreateLLVMCodeGen(Diags, infile, compopts,
70 targetopts, C, CoverageInfo)),
Rafael Espindola666a2ab2013-12-18 16:38:48 +000071 LinkModule(LinkModule) {
Daniel Dunbar8e705052009-11-30 08:39:52 +000072 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattnerdeffa132009-02-18 01:23:44 +000073 }
Daniel Dunbarc13935e2008-10-21 23:49:24 +000074
David Blaikie780dd3b2014-08-29 05:08:19 +000075 std::unique_ptr<llvm::Module> takeModule() { return std::move(TheModule); }
Ahmed Charles9a16beb2014-03-07 19:33:25 +000076 llvm::Module *takeLinkModule() { return LinkModule.release(); }
Daniel Dunbar400a6932010-02-25 04:37:50 +000077
Craig Topper4f12f102014-03-12 06:41:41 +000078 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
Rafael Espindoladf88f6f2012-03-08 15:51:03 +000079 Gen->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +000080 }
81
Craig Topper4f12f102014-03-12 06:41:41 +000082 void Initialize(ASTContext &Ctx) override {
Chris Lattner5cf49fe2009-03-28 02:18:25 +000083 Context = &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000084
Daniel Dunbar8e705052009-11-30 08:39:52 +000085 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000086 LLVMIRGeneration.startTimer();
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner5cf49fe2009-03-28 02:18:25 +000088 Gen->Initialize(Ctx);
Daniel Dunbarc13935e2008-10-21 23:49:24 +000089
Daniel Dunbar400a6932010-02-25 04:37:50 +000090 TheModule.reset(Gen->GetModule());
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.stopTimer();
Daniel Dunbarc13935e2008-10-21 23:49:24 +000094 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Craig Topper4f12f102014-03-12 06:41:41 +000096 bool HandleTopLevelDecl(DeclGroupRef D) override {
Chris Lattner5bbb3c82009-03-29 16:50:03 +000097 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattnereae6cb62009-03-05 08:00:35 +000098 Context->getSourceManager(),
99 "LLVM IR generation of declaration");
Mike Stump11289f42009-09-09 15:08:12 +0000100
Daniel Dunbar8e705052009-11-30 08:39:52 +0000101 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +0000102 LLVMIRGeneration.startTimer();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000103
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000104 Gen->HandleTopLevelDecl(D);
Chris Lattner263d64c2009-02-18 01:37:30 +0000105
Daniel Dunbar8e705052009-11-30 08:39:52 +0000106 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +0000107 LLVMIRGeneration.stopTimer();
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000108
109 return true;
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000110 }
Mike Stump11289f42009-09-09 15:08:12 +0000111
Hans Wennborga926d842014-05-23 20:37:38 +0000112 void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
113 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
114 Context->getSourceManager(),
115 "LLVM IR generation of inline method");
116 if (llvm::TimePassesIsEnabled)
117 LLVMIRGeneration.startTimer();
118
119 Gen->HandleInlineMethodDefinition(D);
120
121 if (llvm::TimePassesIsEnabled)
122 LLVMIRGeneration.stopTimer();
123 }
124
Craig Topper4f12f102014-03-12 06:41:41 +0000125 void HandleTranslationUnit(ASTContext &C) override {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000126 {
Chris Lattnere46de752009-03-06 06:46:31 +0000127 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbar8e705052009-11-30 08:39:52 +0000128 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000129 LLVMIRGeneration.startTimer();
Chris Lattner263d64c2009-02-18 01:37:30 +0000130
Chris Lattnercf169832009-03-28 04:11:33 +0000131 Gen->HandleTranslationUnit(C);
Daniel Dunbara94d8732008-11-11 06:35:39 +0000132
Daniel Dunbar8e705052009-11-30 08:39:52 +0000133 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000134 LLVMIRGeneration.stopTimer();
135 }
Chris Lattner263d64c2009-02-18 01:37:30 +0000136
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000137 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar3e111522010-06-07 23:21:04 +0000138 if (!TheModule)
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000139 return;
Mike Stump11289f42009-09-09 15:08:12 +0000140
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000141 // Make sure IR generation is happy with the module. This is released by
142 // the module provider.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000143 llvm::Module *M = Gen->ReleaseModule();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000144 if (!M) {
145 // The module has been released by IR gen on failures, do not double
146 // free.
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000147 TheModule.release();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000148 return;
149 }
150
151 assert(TheModule.get() == M &&
152 "Unexpected module change during IR generation");
153
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000154 // Link LinkModule into this module if present, preserving its validity.
155 if (LinkModule) {
Rafael Espindola07065482014-10-25 04:06:14 +0000156 LLVMContext &Ctx = LinkModule->getContext();
157 LLVMContext::DiagnosticHandlerTy OldHandler =
158 Ctx.getDiagnosticHandler();
159 void *OldDiagnosticContext = Ctx.getDiagnosticContext();
160 Ctx.setDiagnosticHandler(linkerDiagnosticHandler, this);
161 bool Failed =
162 Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource);
163 Ctx.setDiagnosticHandler(OldHandler, OldDiagnosticContext);
164 if (Failed)
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000165 return;
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000166 }
167
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000168 // Install an inline asm handler so that diagnostics get printed through
169 // our diagnostics hooks.
170 LLVMContext &Ctx = TheModule->getContext();
Chris Lattner068f2ab2010-11-17 08:13:04 +0000171 LLVMContext::InlineAsmDiagHandlerTy OldHandler =
172 Ctx.getInlineAsmDiagnosticHandler();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000173 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
Chris Lattner068f2ab2010-11-17 08:13:04 +0000174 Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000175
Quentin Colombet728c5542014-02-06 18:30:43 +0000176 LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
177 Ctx.getDiagnosticHandler();
178 void *OldDiagnosticContext = Ctx.getDiagnosticContext();
179 Ctx.setDiagnosticHandler(DiagnosticHandler, this);
180
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000181 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Alp Tokere83b9062014-01-02 15:08:04 +0000182 C.getTargetInfo().getTargetDescription(),
Daniel Dunbar3e111522010-06-07 23:21:04 +0000183 TheModule.get(), Action, AsmOutStream);
Rafael Espindola666a2ab2013-12-18 16:38:48 +0000184
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000185 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Quentin Colombet728c5542014-02-06 18:30:43 +0000186
187 Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext);
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000188 }
Mike Stump11289f42009-09-09 15:08:12 +0000189
Craig Topper4f12f102014-03-12 06:41:41 +0000190 void HandleTagDeclDefinition(TagDecl *D) override {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000191 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
192 Context->getSourceManager(),
193 "LLVM IR generation of declaration");
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000194 Gen->HandleTagDeclDefinition(D);
195 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000196
Craig Topper4f12f102014-03-12 06:41:41 +0000197 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
David Blaikie48ad6dc2013-07-13 21:08:14 +0000198 Gen->HandleTagDeclRequiredDefinition(D);
199 }
200
Craig Topper4f12f102014-03-12 06:41:41 +0000201 void CompleteTentativeDefinition(VarDecl *D) override {
Douglas Gregorbeecd582009-04-21 17:11:58 +0000202 Gen->CompleteTentativeDefinition(D);
203 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000204
Craig Topper4f12f102014-03-12 06:41:41 +0000205 void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
Douglas Gregor88d292c2010-05-13 16:44:06 +0000206 Gen->HandleVTable(RD, DefinitionRequired);
207 }
208
Craig Topper4f12f102014-03-12 06:41:41 +0000209 void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000210 Gen->HandleLinkerOptionPragma(Opts);
211 }
212
Craig Topper4f12f102014-03-12 06:41:41 +0000213 void HandleDetectMismatch(llvm::StringRef Name,
214 llvm::StringRef Value) override {
Aaron Ballman5d041be2013-06-04 02:07:14 +0000215 Gen->HandleDetectMismatch(Name, Value);
216 }
217
Craig Topper4f12f102014-03-12 06:41:41 +0000218 void HandleDependentLibrary(llvm::StringRef Opts) override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000219 Gen->HandleDependentLibrary(Opts);
220 }
221
Chris Lattner5ec32e72010-04-06 18:38:50 +0000222 static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
223 unsigned LocCookie) {
224 SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
225 ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
226 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000227
Rafael Espindola07065482014-10-25 04:06:14 +0000228 static void linkerDiagnosticHandler(const llvm::DiagnosticInfo &DI,
229 void *Context) {
230 ((BackendConsumer *)Context)->linkerDiagnosticHandlerImpl(DI);
231 }
232
233 void linkerDiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
234
Quentin Colombet728c5542014-02-06 18:30:43 +0000235 static void DiagnosticHandler(const llvm::DiagnosticInfo &DI,
236 void *Context) {
237 ((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI);
238 }
239
Chris Lattner5ec32e72010-04-06 18:38:50 +0000240 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
241 SourceLocation LocCookie);
Quentin Colombet728c5542014-02-06 18:30:43 +0000242
243 void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
244 /// \brief Specialized handler for InlineAsm diagnostic.
245 /// \return True if the diagnostic has been successfully reported, false
246 /// otherwise.
247 bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
248 /// \brief Specialized handler for StackSize diagnostic.
249 /// \return True if the diagnostic has been successfully reported, false
250 /// otherwise.
251 bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
Diego Novillod23ec942014-05-29 19:55:06 +0000252 /// \brief Specialized handlers for optimization remarks.
253 /// Note that these handlers only accept remarks and they always handle
Diego Novillo829b1702014-04-16 16:54:24 +0000254 /// them.
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000255 void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
256 unsigned DiagID);
Diego Novillod23ec942014-05-29 19:55:06 +0000257 void
Diego Novillo829b1702014-04-16 16:54:24 +0000258 OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark &D);
Diego Novillod23ec942014-05-29 19:55:06 +0000259 void OptimizationRemarkHandler(
260 const llvm::DiagnosticInfoOptimizationRemarkMissed &D);
261 void OptimizationRemarkHandler(
262 const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D);
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000263 void OptimizationFailureHandler(
264 const llvm::DiagnosticInfoOptimizationFailure &D);
Mike Stump11289f42009-09-09 15:08:12 +0000265 };
David Blaikie68e081d2011-12-20 02:48:34 +0000266
267 void BackendConsumer::anchor() {}
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000268}
269
Chris Lattner79f67a72010-04-08 00:23:06 +0000270/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
271/// buffer to be a valid FullSourceLoc.
272static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
273 SourceManager &CSM) {
274 // Get both the clang and llvm source managers. The location is relative to
275 // a memory buffer that the LLVM Source Manager is handling, we need to add
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000276 // a copy to the Clang source manager.
Chris Lattner79f67a72010-04-08 00:23:06 +0000277 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000278
Chris Lattner79f67a72010-04-08 00:23:06 +0000279 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
280 // already owns its one and clang::SourceManager wants to own its one.
281 const MemoryBuffer *LBuf =
282 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000283
Chris Lattner79f67a72010-04-08 00:23:06 +0000284 // Create the copy and transfer ownership to clang::SourceManager.
Alp Tokeraa0dd5a2014-06-27 06:02:00 +0000285 // TODO: Avoid copying files into memory.
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000286 std::unique_ptr<llvm::MemoryBuffer> CBuf =
287 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
288 LBuf->getBufferIdentifier());
Alp Tokeraa0dd5a2014-06-27 06:02:00 +0000289 // FIXME: Keep a file ID map instead of creating new IDs for each location.
David Blaikie50a5f972014-08-29 07:59:55 +0000290 FileID FID = CSM.createFileID(std::move(CBuf));
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000291
Chris Lattner79f67a72010-04-08 00:23:06 +0000292 // Translate the offset into the file.
Alp Tokeraa0dd5a2014-06-27 06:02:00 +0000293 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000294 SourceLocation NewLoc =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000295 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
Chris Lattner79f67a72010-04-08 00:23:06 +0000296 return FullSourceLoc(NewLoc, CSM);
297}
298
Chris Lattner6d672132010-04-06 17:52:14 +0000299
Chris Lattner5ec32e72010-04-06 18:38:50 +0000300/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
301/// error parsing inline asm. The SMDiagnostic indicates the error relative to
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000302/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000303void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
304 SourceLocation LocCookie) {
305 // There are a couple of different kinds of errors we could get here. First,
306 // we re-format the SMDiagnostic in terms of a clang diagnostic.
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000307
308 // Strip "error: " off the start of the message string.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000309 StringRef Message = D.getMessage();
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000310 if (Message.startswith("error: "))
311 Message = Message.substr(7);
Chris Lattner5ec32e72010-04-06 18:38:50 +0000312
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000313 // If the SMDiagnostic has an inline asm source location, translate it.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000314 FullSourceLoc Loc;
Chris Lattner79f67a72010-04-08 00:23:06 +0000315 if (D.getLoc() != SMLoc())
316 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000317
Joey Gouly5798b262014-06-05 21:23:42 +0000318 unsigned DiagID;
319 switch (D.getKind()) {
320 case llvm::SourceMgr::DK_Error:
321 DiagID = diag::err_fe_inline_asm;
322 break;
323 case llvm::SourceMgr::DK_Warning:
324 DiagID = diag::warn_fe_inline_asm;
325 break;
326 case llvm::SourceMgr::DK_Note:
327 DiagID = diag::note_fe_inline_asm;
328 break;
329 }
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000330 // If this problem has clang-level source location information, report the
Joey Gouly5798b262014-06-05 21:23:42 +0000331 // issue in the source with a note showing the instantiated
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000332 // code.
333 if (LocCookie.isValid()) {
Joey Gouly5798b262014-06-05 21:23:42 +0000334 Diags.Report(LocCookie, DiagID).AddString(Message);
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000335
Benjamin Kramere06b2b72011-10-16 10:48:28 +0000336 if (D.getLoc().isValid()) {
337 DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
338 // Convert the SMDiagnostic ranges into SourceRange and attach them
339 // to the diagnostic.
340 for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
341 std::pair<unsigned, unsigned> Range = D.getRanges()[i];
342 unsigned Column = D.getColumnNo();
343 B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
344 Loc.getLocWithOffset(Range.second - Column));
345 }
346 }
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000347 return;
348 }
349
Joey Gouly5798b262014-06-05 21:23:42 +0000350 // Otherwise, report the backend issue as occurring in the generated .s file.
351 // If Loc is invalid, we still need to report the issue, it just gets no
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000352 // location info.
Joey Gouly5798b262014-06-05 21:23:42 +0000353 Diags.Report(Loc, DiagID).AddString(Message);
Chris Lattner5ec32e72010-04-06 18:38:50 +0000354}
355
Quentin Colombet728c5542014-02-06 18:30:43 +0000356#define ComputeDiagID(Severity, GroupName, DiagID) \
357 do { \
358 switch (Severity) { \
359 case llvm::DS_Error: \
360 DiagID = diag::err_fe_##GroupName; \
361 break; \
362 case llvm::DS_Warning: \
363 DiagID = diag::warn_fe_##GroupName; \
364 break; \
Tobias Grosser74160242014-02-28 09:11:08 +0000365 case llvm::DS_Remark: \
366 llvm_unreachable("'remark' severity not expected"); \
367 break; \
368 case llvm::DS_Note: \
369 DiagID = diag::note_fe_##GroupName; \
370 break; \
371 } \
372 } while (false)
373
374#define ComputeDiagRemarkID(Severity, GroupName, DiagID) \
375 do { \
376 switch (Severity) { \
377 case llvm::DS_Error: \
378 DiagID = diag::err_fe_##GroupName; \
379 break; \
380 case llvm::DS_Warning: \
381 DiagID = diag::warn_fe_##GroupName; \
382 break; \
383 case llvm::DS_Remark: \
384 DiagID = diag::remark_fe_##GroupName; \
385 break; \
Quentin Colombet728c5542014-02-06 18:30:43 +0000386 case llvm::DS_Note: \
387 DiagID = diag::note_fe_##GroupName; \
388 break; \
389 } \
390 } while (false)
391
392bool
393BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) {
394 unsigned DiagID;
395 ComputeDiagID(D.getSeverity(), inline_asm, DiagID);
396 std::string Message = D.getMsgStr().str();
397
398 // If this problem has clang-level source location information, report the
Tobias Grosserbd25beb2014-02-26 10:21:56 +0000399 // issue as being a problem in the source with a note showing the instantiated
Quentin Colombet728c5542014-02-06 18:30:43 +0000400 // code.
401 SourceLocation LocCookie =
402 SourceLocation::getFromRawEncoding(D.getLocCookie());
403 if (LocCookie.isValid())
404 Diags.Report(LocCookie, DiagID).AddString(Message);
405 else {
406 // Otherwise, report the backend diagnostic as occurring in the generated
407 // .s file.
408 // If Loc is invalid, we still need to report the diagnostic, it just gets
409 // no location info.
410 FullSourceLoc Loc;
411 Diags.Report(Loc, DiagID).AddString(Message);
412 }
413 // We handled all the possible severities.
414 return true;
415}
416
417bool
418BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) {
419 if (D.getSeverity() != llvm::DS_Warning)
420 // For now, the only support we have for StackSize diagnostic is warning.
421 // We do not know how to format other severities.
422 return false;
423
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000424 if (const Decl *ND = Gen->GetDeclForMangledName(D.getFunction().getName())) {
425 Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()),
426 diag::warn_fe_frame_larger_than)
427 << D.getStackSize() << Decl::castToDeclContext(ND);
428 return true;
429 }
430
431 return false;
Quentin Colombet728c5542014-02-06 18:30:43 +0000432}
433
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000434void BackendConsumer::EmitOptimizationMessage(
Tyler Nowickie4707712014-07-16 00:40:42 +0000435 const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) {
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000436 // We only support warnings and remarks.
437 assert(D.getSeverity() == llvm::DS_Remark ||
438 D.getSeverity() == llvm::DS_Warning);
Diego Novillo829b1702014-04-16 16:54:24 +0000439
Diego Novillod23ec942014-05-29 19:55:06 +0000440 SourceManager &SourceMgr = Context->getSourceManager();
441 FileManager &FileMgr = SourceMgr.getFileManager();
442 StringRef Filename;
443 unsigned Line, Column;
444 D.getLocation(&Filename, &Line, &Column);
Alp Toker27506272014-06-05 22:11:12 +0000445 SourceLocation DILoc;
Diego Novillod23ec942014-05-29 19:55:06 +0000446 const FileEntry *FE = FileMgr.getFile(Filename);
447 if (FE && Line > 0) {
448 // If -gcolumn-info was not used, Column will be 0. This upsets the
Alp Toker27506272014-06-05 22:11:12 +0000449 // source manager, so pass 1 if Column is not set.
450 DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);
Diego Novillo829b1702014-04-16 16:54:24 +0000451 }
Alp Toker27506272014-06-05 22:11:12 +0000452
453 // If a location isn't available, try to approximate it using the associated
454 // function definition. We use the definition's right brace to differentiate
455 // from diagnostics that genuinely relate to the function itself.
456 FullSourceLoc Loc(DILoc, SourceMgr);
457 if (Loc.isInvalid())
458 if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName()))
459 Loc = FD->getASTContext().getFullLoc(FD->getBodyRBrace());
460
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000461 Diags.Report(Loc, DiagID)
462 << AddFlagValue(D.getPassName() ? D.getPassName() : "")
463 << D.getMsg().str();
Diego Novillod23ec942014-05-29 19:55:06 +0000464
Diego Novillo913690c2014-06-24 17:02:17 +0000465 if (DILoc.isInvalid())
Diego Novillod23ec942014-05-29 19:55:06 +0000466 // If we were not able to translate the file:line:col information
467 // back to a SourceLocation, at least emit a note stating that
468 // we could not translate this location. This can happen in the
469 // case of #line directives.
Alp Toker27506272014-06-05 22:11:12 +0000470 Diags.Report(Loc, diag::note_fe_backend_optimization_remark_invalid_loc)
Diego Novillod23ec942014-05-29 19:55:06 +0000471 << Filename << Line << Column;
472}
473
474void BackendConsumer::OptimizationRemarkHandler(
475 const llvm::DiagnosticInfoOptimizationRemark &D) {
476 // Optimization remarks are active only if the -Rpass flag has a regular
477 // expression that matches the name of the pass name in \p D.
478 if (CodeGenOpts.OptimizationRemarkPattern &&
479 CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName()))
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000480 EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark);
Diego Novillod23ec942014-05-29 19:55:06 +0000481}
482
483void BackendConsumer::OptimizationRemarkHandler(
484 const llvm::DiagnosticInfoOptimizationRemarkMissed &D) {
485 // Missed optimization remarks are active only if the -Rpass-missed
486 // flag has a regular expression that matches the name of the pass
487 // name in \p D.
488 if (CodeGenOpts.OptimizationRemarkMissedPattern &&
489 CodeGenOpts.OptimizationRemarkMissedPattern->match(D.getPassName()))
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000490 EmitOptimizationMessage(D,
491 diag::remark_fe_backend_optimization_remark_missed);
Diego Novillod23ec942014-05-29 19:55:06 +0000492}
493
494void BackendConsumer::OptimizationRemarkHandler(
495 const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D) {
496 // Optimization analysis remarks are active only if the -Rpass-analysis
497 // flag has a regular expression that matches the name of the pass
498 // name in \p D.
499 if (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
500 CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName()))
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000501 EmitOptimizationMessage(
Diego Novillod23ec942014-05-29 19:55:06 +0000502 D, diag::remark_fe_backend_optimization_remark_analysis);
Diego Novillo829b1702014-04-16 16:54:24 +0000503}
504
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000505void BackendConsumer::OptimizationFailureHandler(
506 const llvm::DiagnosticInfoOptimizationFailure &D) {
507 EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure);
508}
509
Rafael Espindola07065482014-10-25 04:06:14 +0000510void BackendConsumer::linkerDiagnosticHandlerImpl(const DiagnosticInfo &DI) {
511 if (DI.getSeverity() != DS_Error)
512 return;
513
514 std::string MsgStorage;
515 {
516 raw_string_ostream Stream(MsgStorage);
517 DiagnosticPrinterRawOStream DP(Stream);
518 DI.print(DP);
519 }
520
521 Diags.Report(diag::err_fe_cannot_link_module)
522 << LinkModule->getModuleIdentifier() << MsgStorage;
523}
524
Quentin Colombet728c5542014-02-06 18:30:43 +0000525/// \brief This function is invoked when the backend needs
526/// to report something to the user.
527void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
528 unsigned DiagID = diag::err_fe_inline_asm;
529 llvm::DiagnosticSeverity Severity = DI.getSeverity();
530 // Get the diagnostic ID based.
531 switch (DI.getKind()) {
532 case llvm::DK_InlineAsm:
533 if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI)))
534 return;
535 ComputeDiagID(Severity, inline_asm, DiagID);
536 break;
537 case llvm::DK_StackSize:
538 if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI)))
539 return;
540 ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
541 break;
Diego Novillo829b1702014-04-16 16:54:24 +0000542 case llvm::DK_OptimizationRemark:
543 // Optimization remarks are always handled completely by this
544 // handler. There is no generic way of emitting them.
545 OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemark>(DI));
546 return;
Diego Novillo9c89ff12014-05-29 16:19:27 +0000547 case llvm::DK_OptimizationRemarkMissed:
Diego Novillod23ec942014-05-29 19:55:06 +0000548 // Optimization remarks are always handled completely by this
549 // handler. There is no generic way of emitting them.
550 OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemarkMissed>(DI));
551 return;
Diego Novillo9c89ff12014-05-29 16:19:27 +0000552 case llvm::DK_OptimizationRemarkAnalysis:
Diego Novillod23ec942014-05-29 19:55:06 +0000553 // Optimization remarks are always handled completely by this
554 // handler. There is no generic way of emitting them.
555 OptimizationRemarkHandler(
556 cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI));
Diego Novillo9c89ff12014-05-29 16:19:27 +0000557 return;
Tyler Nowickif8a767d2014-07-18 19:40:19 +0000558 case llvm::DK_OptimizationFailure:
559 // Optimization failures are always handled completely by this
560 // handler.
561 OptimizationFailureHandler(cast<DiagnosticInfoOptimizationFailure>(DI));
562 return;
Quentin Colombet728c5542014-02-06 18:30:43 +0000563 default:
564 // Plugin IDs are not bound to any value as they are set dynamically.
Tobias Grosser74160242014-02-28 09:11:08 +0000565 ComputeDiagRemarkID(Severity, backend_plugin, DiagID);
Quentin Colombet728c5542014-02-06 18:30:43 +0000566 break;
567 }
568 std::string MsgStorage;
569 {
570 raw_string_ostream Stream(MsgStorage);
571 DiagnosticPrinterRawOStream DP(Stream);
572 DI.print(DP);
573 }
574
575 // Report the backend message using the usual diagnostic mechanism.
576 FullSourceLoc Loc;
577 Diags.Report(Loc, DiagID).AddString(MsgStorage);
578}
579#undef ComputeDiagID
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000580
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000581CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
Craig Topper8a13c412014-05-21 05:09:00 +0000582 : Act(_Act), LinkModule(nullptr),
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000583 VMContext(_VMContext ? _VMContext : new LLVMContext),
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000584 OwnsVMContext(!_VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000585
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000586CodeGenAction::~CodeGenAction() {
587 TheModule.reset();
588 if (OwnsVMContext)
589 delete VMContext;
590}
Daniel Dunbare8ecf9a2010-02-25 20:37:44 +0000591
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000592bool CodeGenAction::hasIRSupport() const { return true; }
593
Daniel Dunbar400a6932010-02-25 04:37:50 +0000594void CodeGenAction::EndSourceFileAction() {
595 // If the consumer creation failed, do nothing.
596 if (!getCompilerInstance().hasASTConsumer())
597 return;
598
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000599 // If we were given a link module, release consumer's ownership of it.
600 if (LinkModule)
601 BEConsumer->takeLinkModule();
602
Daniel Dunbar400a6932010-02-25 04:37:50 +0000603 // Steal the module from the consumer.
David Blaikie780dd3b2014-08-29 05:08:19 +0000604 TheModule = BEConsumer->takeModule();
Daniel Dunbar400a6932010-02-25 04:37:50 +0000605}
606
Rafael Espindolae8337302014-08-19 14:36:35 +0000607std::unique_ptr<llvm::Module> CodeGenAction::takeModule() {
608 return std::move(TheModule);
609}
610
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000611llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
612 OwnsVMContext = false;
613 return VMContext;
614}
615
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000616static raw_ostream *GetOutputStream(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000617 StringRef InFile,
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000618 BackendAction Action) {
619 switch (Action) {
620 case Backend_EmitAssembly:
621 return CI.createDefaultOutputFile(false, InFile, "s");
622 case Backend_EmitLL:
623 return CI.createDefaultOutputFile(false, InFile, "ll");
624 case Backend_EmitBC:
625 return CI.createDefaultOutputFile(true, InFile, "bc");
626 case Backend_EmitNothing:
Craig Topper8a13c412014-05-21 05:09:00 +0000627 return nullptr;
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000628 case Backend_EmitMCNull:
Alp Tokerea046722014-06-03 17:23:34 +0000629 return CI.createNullOutputFile();
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000630 case Backend_EmitObj:
631 return CI.createDefaultOutputFile(true, InFile, "o");
632 }
633
David Blaikie83d382b2011-09-23 05:06:16 +0000634 llvm_unreachable("Invalid action!");
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000635}
636
David Blaikie6beb6aa2014-08-10 19:56:51 +0000637std::unique_ptr<ASTConsumer>
638CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000639 BackendAction BA = static_cast<BackendAction>(Act);
NAKAMURA Takumi69f35282014-08-11 06:53:11 +0000640 std::unique_ptr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000641 if (BA != Backend_EmitNothing && !OS)
Craig Topper8a13c412014-05-21 05:09:00 +0000642 return nullptr;
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000643
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000644 llvm::Module *LinkModuleToUse = LinkModule;
645
646 // If we were not given a link module, and the user requested that one be
647 // loaded from bitcode, do so now.
648 const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
649 if (!LinkModuleToUse && !LinkBCFile.empty()) {
Benjamin Kramera8857962014-10-26 22:44:13 +0000650 auto BCBuf = CI.getFileManager().getBufferForFile(LinkBCFile);
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000651 if (!BCBuf) {
652 CI.getDiagnostics().Report(diag::err_cannot_open_file)
Benjamin Kramera8857962014-10-26 22:44:13 +0000653 << LinkBCFile << BCBuf.getError().message();
Craig Topper8a13c412014-05-21 05:09:00 +0000654 return nullptr;
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000655 }
656
Rafael Espindola6b6004a2014-01-13 18:31:09 +0000657 ErrorOr<llvm::Module *> ModuleOrErr =
Benjamin Kramera8857962014-10-26 22:44:13 +0000658 getLazyBitcodeModule(std::move(*BCBuf), *VMContext);
Rafael Espindolab3046992014-06-12 21:36:35 +0000659 if (std::error_code EC = ModuleOrErr.getError()) {
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000660 CI.getDiagnostics().Report(diag::err_cannot_open_file)
Rafael Espindola6b6004a2014-01-13 18:31:09 +0000661 << LinkBCFile << EC.message();
Craig Topper8a13c412014-05-21 05:09:00 +0000662 return nullptr;
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000663 }
Rafael Espindola6b6004a2014-01-13 18:31:09 +0000664 LinkModuleToUse = ModuleOrErr.get();
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000665 }
666
Alex Lorenzee024992014-08-04 18:41:51 +0000667 CoverageSourceInfo *CoverageInfo = nullptr;
668 // Add the preprocessor callback only when the coverage mapping is generated.
669 if (CI.getCodeGenOpts().CoverageMapping) {
670 CoverageInfo = new CoverageSourceInfo;
Craig Topperb8a70532014-09-10 04:53:53 +0000671 CI.getPreprocessor().addPPCallbacks(
672 std::unique_ptr<PPCallbacks>(CoverageInfo));
Alex Lorenzee024992014-08-04 18:41:51 +0000673 }
David Blaikie037e75b2014-08-10 21:06:17 +0000674 std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
David Blaikie6beb6aa2014-08-10 19:56:51 +0000675 BA, CI.getDiagnostics(), CI.getCodeGenOpts(), CI.getTargetOpts(),
David Blaikie037e75b2014-08-10 21:06:17 +0000676 CI.getLangOpts(), CI.getFrontendOpts().ShowTimers, InFile,
NAKAMURA Takumi69f35282014-08-11 06:53:11 +0000677 LinkModuleToUse, OS.release(), *VMContext, CoverageInfo));
David Blaikie6beb6aa2014-08-10 19:56:51 +0000678 BEConsumer = Result.get();
679 return std::move(Result);
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000680}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000681
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000682void CodeGenAction::ExecuteAction() {
683 // If this is an IR file, we have to treat it specially.
684 if (getCurrentFileKind() == IK_LLVM_IR) {
685 BackendAction BA = static_cast<BackendAction>(Act);
686 CompilerInstance &CI = getCompilerInstance();
687 raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
688 if (BA != Backend_EmitNothing && !OS)
689 return;
690
691 bool Invalid;
692 SourceManager &SM = CI.getSourceManager();
Alp Toker7b463d52014-06-30 01:33:59 +0000693 FileID FID = SM.getMainFileID();
694 llvm::MemoryBuffer *MainFile = SM.getBuffer(FID, &Invalid);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000695 if (Invalid)
696 return;
697
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000698 llvm::SMDiagnostic Err;
Rafael Espindola32327732014-08-26 21:49:29 +0000699 TheModule = parseIR(MainFile->getMemBufferRef(), Err, *VMContext);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000700 if (!TheModule) {
Alp Toker7b463d52014-06-30 01:33:59 +0000701 // Translate from the diagnostic info to the SourceManager location if
702 // available.
703 // TODO: Unify this with ConvertBackendLocation()
704 SourceLocation Loc;
705 if (Err.getLineNo() > 0) {
706 assert(Err.getColumnNo() >= 0);
707 Loc = SM.translateFileLineCol(SM.getFileEntryForID(FID),
708 Err.getLineNo(), Err.getColumnNo() + 1);
709 }
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000710
Alp Tokerbc043f22013-12-21 05:20:03 +0000711 // Strip off a leading diagnostic code if there is one.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000712 StringRef Msg = Err.getMessage();
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000713 if (Msg.startswith("error: "))
714 Msg = Msg.substr(7);
Benjamin Kramer778b8b82012-03-16 22:31:42 +0000715
Alp Tokerbc043f22013-12-21 05:20:03 +0000716 unsigned DiagID =
717 CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
Benjamin Kramer778b8b82012-03-16 22:31:42 +0000718
Alp Tokerbc043f22013-12-21 05:20:03 +0000719 CI.getDiagnostics().Report(Loc, DiagID) << Msg;
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000720 return;
721 }
Rafael Espindolad5e81e52013-12-20 22:01:25 +0000722 const TargetOptions &TargetOpts = CI.getTargetOpts();
723 if (TheModule->getTargetTriple() != TargetOpts.Triple) {
724 unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
725 DiagnosticsEngine::Warning,
726 "overriding the module target triple with %0");
727
728 CI.getDiagnostics().Report(SourceLocation(), DiagID) << TargetOpts.Triple;
729 TheModule->setTargetTriple(TargetOpts.Triple);
730 }
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000731
Alp Tokere83b9062014-01-02 15:08:04 +0000732 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts,
733 CI.getLangOpts(), CI.getTarget().getTargetDescription(),
734 TheModule.get(), BA, OS);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000735 return;
736 }
737
738 // Otherwise follow the normal AST path.
739 this->ASTFrontendAction::ExecuteAction();
740}
741
742//
743
David Blaikie68e081d2011-12-20 02:48:34 +0000744void EmitAssemblyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000745EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
746 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000747
David Blaikie68e081d2011-12-20 02:48:34 +0000748void EmitBCAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000749EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
750 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000751
David Blaikie68e081d2011-12-20 02:48:34 +0000752void EmitLLVMAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000753EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
754 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000755
David Blaikie68e081d2011-12-20 02:48:34 +0000756void EmitLLVMOnlyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000757EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
758 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000759
David Blaikie68e081d2011-12-20 02:48:34 +0000760void EmitCodeGenOnlyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000761EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
762 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar4c77a642010-05-25 18:41:01 +0000763
David Blaikie68e081d2011-12-20 02:48:34 +0000764void EmitObjAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000765EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
766 : CodeGenAction(Backend_EmitObj, _VMContext) {}