1f4a2713aSLionel Sambuc //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10*0a6a1f1dSLionel Sambuc #include "CoverageMappingGen.h"
11f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
12f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
13*0a6a1f1dSLionel Sambuc #include "clang/AST/DeclCXX.h"
14f4a2713aSLionel Sambuc #include "clang/AST/DeclGroup.h"
15f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
17f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
18f4a2713aSLionel Sambuc #include "clang/CodeGen/BackendUtil.h"
19*0a6a1f1dSLionel Sambuc #include "clang/CodeGen/CodeGenAction.h"
20f4a2713aSLionel Sambuc #include "clang/CodeGen/ModuleBuilder.h"
21f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
22f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendDiagnostic.h"
23*0a6a1f1dSLionel Sambuc #include "clang/Lex/Preprocessor.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
25f4a2713aSLionel Sambuc #include "llvm/Bitcode/ReaderWriter.h"
26*0a6a1f1dSLionel Sambuc #include "llvm/IR/DebugInfo.h"
27*0a6a1f1dSLionel Sambuc #include "llvm/IR/DiagnosticInfo.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/IR/DiagnosticPrinter.h"
29f4a2713aSLionel Sambuc #include "llvm/IR/LLVMContext.h"
30f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
31f4a2713aSLionel Sambuc #include "llvm/IRReader/IRReader.h"
32*0a6a1f1dSLionel Sambuc #include "llvm/Linker/Linker.h"
33f4a2713aSLionel Sambuc #include "llvm/Pass.h"
34f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
35f4a2713aSLionel Sambuc #include "llvm/Support/SourceMgr.h"
36f4a2713aSLionel Sambuc #include "llvm/Support/Timer.h"
37*0a6a1f1dSLionel Sambuc #include <memory>
38f4a2713aSLionel Sambuc using namespace clang;
39f4a2713aSLionel Sambuc using namespace llvm;
40f4a2713aSLionel Sambuc
41f4a2713aSLionel Sambuc namespace clang {
42f4a2713aSLionel Sambuc class BackendConsumer : public ASTConsumer {
43f4a2713aSLionel Sambuc virtual void anchor();
44f4a2713aSLionel Sambuc DiagnosticsEngine &Diags;
45f4a2713aSLionel Sambuc BackendAction Action;
46f4a2713aSLionel Sambuc const CodeGenOptions &CodeGenOpts;
47f4a2713aSLionel Sambuc const TargetOptions &TargetOpts;
48f4a2713aSLionel Sambuc const LangOptions &LangOpts;
49f4a2713aSLionel Sambuc raw_ostream *AsmOutStream;
50f4a2713aSLionel Sambuc ASTContext *Context;
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc Timer LLVMIRGeneration;
53f4a2713aSLionel Sambuc
54*0a6a1f1dSLionel Sambuc std::unique_ptr<CodeGenerator> Gen;
55f4a2713aSLionel Sambuc
56*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::Module> TheModule, LinkModule;
57f4a2713aSLionel Sambuc
58f4a2713aSLionel Sambuc public:
BackendConsumer(BackendAction action,DiagnosticsEngine & _Diags,const CodeGenOptions & compopts,const TargetOptions & targetopts,const LangOptions & langopts,bool TimePasses,const std::string & infile,llvm::Module * LinkModule,raw_ostream * OS,LLVMContext & C,CoverageSourceInfo * CoverageInfo=nullptr)59f4a2713aSLionel Sambuc BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
60f4a2713aSLionel Sambuc const CodeGenOptions &compopts,
61f4a2713aSLionel Sambuc const TargetOptions &targetopts,
62*0a6a1f1dSLionel Sambuc const LangOptions &langopts, bool TimePasses,
63*0a6a1f1dSLionel Sambuc const std::string &infile, llvm::Module *LinkModule,
64*0a6a1f1dSLionel Sambuc raw_ostream *OS, LLVMContext &C,
65*0a6a1f1dSLionel Sambuc CoverageSourceInfo *CoverageInfo = nullptr)
66*0a6a1f1dSLionel Sambuc : Diags(_Diags), Action(action), CodeGenOpts(compopts),
67*0a6a1f1dSLionel Sambuc TargetOpts(targetopts), LangOpts(langopts), AsmOutStream(OS),
68*0a6a1f1dSLionel Sambuc Context(nullptr), LLVMIRGeneration("LLVM IR Generation Time"),
69*0a6a1f1dSLionel Sambuc Gen(CreateLLVMCodeGen(Diags, infile, compopts,
70*0a6a1f1dSLionel Sambuc targetopts, C, CoverageInfo)),
71*0a6a1f1dSLionel Sambuc LinkModule(LinkModule) {
72f4a2713aSLionel Sambuc llvm::TimePassesIsEnabled = TimePasses;
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc
takeModule()75*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::Module> takeModule() { return std::move(TheModule); }
takeLinkModule()76*0a6a1f1dSLionel Sambuc llvm::Module *takeLinkModule() { return LinkModule.release(); }
77f4a2713aSLionel Sambuc
HandleCXXStaticMemberVarInstantiation(VarDecl * VD)78*0a6a1f1dSLionel Sambuc void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
79f4a2713aSLionel Sambuc Gen->HandleCXXStaticMemberVarInstantiation(VD);
80f4a2713aSLionel Sambuc }
81f4a2713aSLionel Sambuc
Initialize(ASTContext & Ctx)82*0a6a1f1dSLionel Sambuc void Initialize(ASTContext &Ctx) override {
83f4a2713aSLionel Sambuc Context = &Ctx;
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc if (llvm::TimePassesIsEnabled)
86f4a2713aSLionel Sambuc LLVMIRGeneration.startTimer();
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc Gen->Initialize(Ctx);
89f4a2713aSLionel Sambuc
90f4a2713aSLionel Sambuc TheModule.reset(Gen->GetModule());
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc if (llvm::TimePassesIsEnabled)
93f4a2713aSLionel Sambuc LLVMIRGeneration.stopTimer();
94f4a2713aSLionel Sambuc }
95f4a2713aSLionel Sambuc
HandleTopLevelDecl(DeclGroupRef D)96*0a6a1f1dSLionel Sambuc bool HandleTopLevelDecl(DeclGroupRef D) override {
97f4a2713aSLionel Sambuc PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
98f4a2713aSLionel Sambuc Context->getSourceManager(),
99f4a2713aSLionel Sambuc "LLVM IR generation of declaration");
100f4a2713aSLionel Sambuc
101f4a2713aSLionel Sambuc if (llvm::TimePassesIsEnabled)
102f4a2713aSLionel Sambuc LLVMIRGeneration.startTimer();
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc Gen->HandleTopLevelDecl(D);
105f4a2713aSLionel Sambuc
106f4a2713aSLionel Sambuc if (llvm::TimePassesIsEnabled)
107f4a2713aSLionel Sambuc LLVMIRGeneration.stopTimer();
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc return true;
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc
HandleInlineMethodDefinition(CXXMethodDecl * D)112*0a6a1f1dSLionel Sambuc void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
113*0a6a1f1dSLionel Sambuc PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
114*0a6a1f1dSLionel Sambuc Context->getSourceManager(),
115*0a6a1f1dSLionel Sambuc "LLVM IR generation of inline method");
116*0a6a1f1dSLionel Sambuc if (llvm::TimePassesIsEnabled)
117*0a6a1f1dSLionel Sambuc LLVMIRGeneration.startTimer();
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc Gen->HandleInlineMethodDefinition(D);
120*0a6a1f1dSLionel Sambuc
121*0a6a1f1dSLionel Sambuc if (llvm::TimePassesIsEnabled)
122*0a6a1f1dSLionel Sambuc LLVMIRGeneration.stopTimer();
123*0a6a1f1dSLionel Sambuc }
124*0a6a1f1dSLionel Sambuc
HandleTranslationUnit(ASTContext & C)125*0a6a1f1dSLionel Sambuc void HandleTranslationUnit(ASTContext &C) override {
126f4a2713aSLionel Sambuc {
127f4a2713aSLionel Sambuc PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
128f4a2713aSLionel Sambuc if (llvm::TimePassesIsEnabled)
129f4a2713aSLionel Sambuc LLVMIRGeneration.startTimer();
130f4a2713aSLionel Sambuc
131f4a2713aSLionel Sambuc Gen->HandleTranslationUnit(C);
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc if (llvm::TimePassesIsEnabled)
134f4a2713aSLionel Sambuc LLVMIRGeneration.stopTimer();
135f4a2713aSLionel Sambuc }
136f4a2713aSLionel Sambuc
137f4a2713aSLionel Sambuc // Silently ignore if we weren't initialized for some reason.
138f4a2713aSLionel Sambuc if (!TheModule)
139f4a2713aSLionel Sambuc return;
140f4a2713aSLionel Sambuc
141f4a2713aSLionel Sambuc // Make sure IR generation is happy with the module. This is released by
142f4a2713aSLionel Sambuc // the module provider.
143f4a2713aSLionel Sambuc llvm::Module *M = Gen->ReleaseModule();
144f4a2713aSLionel Sambuc if (!M) {
145f4a2713aSLionel Sambuc // The module has been released by IR gen on failures, do not double
146f4a2713aSLionel Sambuc // free.
147*0a6a1f1dSLionel Sambuc TheModule.release();
148f4a2713aSLionel Sambuc return;
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc
151f4a2713aSLionel Sambuc assert(TheModule.get() == M &&
152f4a2713aSLionel Sambuc "Unexpected module change during IR generation");
153f4a2713aSLionel Sambuc
154f4a2713aSLionel Sambuc // Link LinkModule into this module if present, preserving its validity.
155f4a2713aSLionel Sambuc if (LinkModule) {
156*0a6a1f1dSLionel Sambuc if (Linker::LinkModules(
157*0a6a1f1dSLionel Sambuc M, LinkModule.get(),
158*0a6a1f1dSLionel Sambuc [=](const DiagnosticInfo &DI) { linkerDiagnosticHandler(DI); }))
159f4a2713aSLionel Sambuc return;
160f4a2713aSLionel Sambuc }
161f4a2713aSLionel Sambuc
162f4a2713aSLionel Sambuc // Install an inline asm handler so that diagnostics get printed through
163f4a2713aSLionel Sambuc // our diagnostics hooks.
164f4a2713aSLionel Sambuc LLVMContext &Ctx = TheModule->getContext();
165f4a2713aSLionel Sambuc LLVMContext::InlineAsmDiagHandlerTy OldHandler =
166f4a2713aSLionel Sambuc Ctx.getInlineAsmDiagnosticHandler();
167f4a2713aSLionel Sambuc void *OldContext = Ctx.getInlineAsmDiagnosticContext();
168f4a2713aSLionel Sambuc Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
169f4a2713aSLionel Sambuc
170*0a6a1f1dSLionel Sambuc LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
171*0a6a1f1dSLionel Sambuc Ctx.getDiagnosticHandler();
172*0a6a1f1dSLionel Sambuc void *OldDiagnosticContext = Ctx.getDiagnosticContext();
173*0a6a1f1dSLionel Sambuc Ctx.setDiagnosticHandler(DiagnosticHandler, this);
174*0a6a1f1dSLionel Sambuc
175f4a2713aSLionel Sambuc EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
176*0a6a1f1dSLionel Sambuc C.getTargetInfo().getTargetDescription(),
177f4a2713aSLionel Sambuc TheModule.get(), Action, AsmOutStream);
178f4a2713aSLionel Sambuc
179f4a2713aSLionel Sambuc Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
180*0a6a1f1dSLionel Sambuc
181*0a6a1f1dSLionel Sambuc Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext);
182f4a2713aSLionel Sambuc }
183f4a2713aSLionel Sambuc
HandleTagDeclDefinition(TagDecl * D)184*0a6a1f1dSLionel Sambuc void HandleTagDeclDefinition(TagDecl *D) override {
185f4a2713aSLionel Sambuc PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
186f4a2713aSLionel Sambuc Context->getSourceManager(),
187f4a2713aSLionel Sambuc "LLVM IR generation of declaration");
188f4a2713aSLionel Sambuc Gen->HandleTagDeclDefinition(D);
189f4a2713aSLionel Sambuc }
190f4a2713aSLionel Sambuc
HandleTagDeclRequiredDefinition(const TagDecl * D)191*0a6a1f1dSLionel Sambuc void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
192f4a2713aSLionel Sambuc Gen->HandleTagDeclRequiredDefinition(D);
193f4a2713aSLionel Sambuc }
194f4a2713aSLionel Sambuc
CompleteTentativeDefinition(VarDecl * D)195*0a6a1f1dSLionel Sambuc void CompleteTentativeDefinition(VarDecl *D) override {
196f4a2713aSLionel Sambuc Gen->CompleteTentativeDefinition(D);
197f4a2713aSLionel Sambuc }
198f4a2713aSLionel Sambuc
HandleVTable(CXXRecordDecl * RD,bool DefinitionRequired)199*0a6a1f1dSLionel Sambuc void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
200f4a2713aSLionel Sambuc Gen->HandleVTable(RD, DefinitionRequired);
201f4a2713aSLionel Sambuc }
202f4a2713aSLionel Sambuc
HandleLinkerOptionPragma(llvm::StringRef Opts)203*0a6a1f1dSLionel Sambuc void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
204f4a2713aSLionel Sambuc Gen->HandleLinkerOptionPragma(Opts);
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc
HandleDetectMismatch(llvm::StringRef Name,llvm::StringRef Value)207*0a6a1f1dSLionel Sambuc void HandleDetectMismatch(llvm::StringRef Name,
208*0a6a1f1dSLionel Sambuc llvm::StringRef Value) override {
209f4a2713aSLionel Sambuc Gen->HandleDetectMismatch(Name, Value);
210f4a2713aSLionel Sambuc }
211f4a2713aSLionel Sambuc
HandleDependentLibrary(llvm::StringRef Opts)212*0a6a1f1dSLionel Sambuc void HandleDependentLibrary(llvm::StringRef Opts) override {
213f4a2713aSLionel Sambuc Gen->HandleDependentLibrary(Opts);
214f4a2713aSLionel Sambuc }
215f4a2713aSLionel Sambuc
InlineAsmDiagHandler(const llvm::SMDiagnostic & SM,void * Context,unsigned LocCookie)216f4a2713aSLionel Sambuc static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
217f4a2713aSLionel Sambuc unsigned LocCookie) {
218f4a2713aSLionel Sambuc SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
219f4a2713aSLionel Sambuc ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
220f4a2713aSLionel Sambuc }
221f4a2713aSLionel Sambuc
222*0a6a1f1dSLionel Sambuc void linkerDiagnosticHandler(const llvm::DiagnosticInfo &DI);
223*0a6a1f1dSLionel Sambuc
DiagnosticHandler(const llvm::DiagnosticInfo & DI,void * Context)224*0a6a1f1dSLionel Sambuc static void DiagnosticHandler(const llvm::DiagnosticInfo &DI,
225*0a6a1f1dSLionel Sambuc void *Context) {
226*0a6a1f1dSLionel Sambuc ((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI);
227*0a6a1f1dSLionel Sambuc }
228*0a6a1f1dSLionel Sambuc
229f4a2713aSLionel Sambuc void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
230f4a2713aSLionel Sambuc SourceLocation LocCookie);
231*0a6a1f1dSLionel Sambuc
232*0a6a1f1dSLionel Sambuc void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
233*0a6a1f1dSLionel Sambuc /// \brief Specialized handler for InlineAsm diagnostic.
234*0a6a1f1dSLionel Sambuc /// \return True if the diagnostic has been successfully reported, false
235*0a6a1f1dSLionel Sambuc /// otherwise.
236*0a6a1f1dSLionel Sambuc bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
237*0a6a1f1dSLionel Sambuc /// \brief Specialized handler for StackSize diagnostic.
238*0a6a1f1dSLionel Sambuc /// \return True if the diagnostic has been successfully reported, false
239*0a6a1f1dSLionel Sambuc /// otherwise.
240*0a6a1f1dSLionel Sambuc bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
241*0a6a1f1dSLionel Sambuc /// \brief Specialized handlers for optimization remarks.
242*0a6a1f1dSLionel Sambuc /// Note that these handlers only accept remarks and they always handle
243*0a6a1f1dSLionel Sambuc /// them.
244*0a6a1f1dSLionel Sambuc void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
245*0a6a1f1dSLionel Sambuc unsigned DiagID);
246*0a6a1f1dSLionel Sambuc void
247*0a6a1f1dSLionel Sambuc OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark &D);
248*0a6a1f1dSLionel Sambuc void OptimizationRemarkHandler(
249*0a6a1f1dSLionel Sambuc const llvm::DiagnosticInfoOptimizationRemarkMissed &D);
250*0a6a1f1dSLionel Sambuc void OptimizationRemarkHandler(
251*0a6a1f1dSLionel Sambuc const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D);
252*0a6a1f1dSLionel Sambuc void OptimizationFailureHandler(
253*0a6a1f1dSLionel Sambuc const llvm::DiagnosticInfoOptimizationFailure &D);
254f4a2713aSLionel Sambuc };
255f4a2713aSLionel Sambuc
anchor()256f4a2713aSLionel Sambuc void BackendConsumer::anchor() {}
257f4a2713aSLionel Sambuc }
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
260f4a2713aSLionel Sambuc /// buffer to be a valid FullSourceLoc.
ConvertBackendLocation(const llvm::SMDiagnostic & D,SourceManager & CSM)261f4a2713aSLionel Sambuc static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
262f4a2713aSLionel Sambuc SourceManager &CSM) {
263f4a2713aSLionel Sambuc // Get both the clang and llvm source managers. The location is relative to
264f4a2713aSLionel Sambuc // a memory buffer that the LLVM Source Manager is handling, we need to add
265f4a2713aSLionel Sambuc // a copy to the Clang source manager.
266f4a2713aSLionel Sambuc const llvm::SourceMgr &LSM = *D.getSourceMgr();
267f4a2713aSLionel Sambuc
268f4a2713aSLionel Sambuc // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
269f4a2713aSLionel Sambuc // already owns its one and clang::SourceManager wants to own its one.
270f4a2713aSLionel Sambuc const MemoryBuffer *LBuf =
271f4a2713aSLionel Sambuc LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
272f4a2713aSLionel Sambuc
273f4a2713aSLionel Sambuc // Create the copy and transfer ownership to clang::SourceManager.
274*0a6a1f1dSLionel Sambuc // TODO: Avoid copying files into memory.
275*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MemoryBuffer> CBuf =
276f4a2713aSLionel Sambuc llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
277f4a2713aSLionel Sambuc LBuf->getBufferIdentifier());
278*0a6a1f1dSLionel Sambuc // FIXME: Keep a file ID map instead of creating new IDs for each location.
279*0a6a1f1dSLionel Sambuc FileID FID = CSM.createFileID(std::move(CBuf));
280f4a2713aSLionel Sambuc
281f4a2713aSLionel Sambuc // Translate the offset into the file.
282f4a2713aSLionel Sambuc unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
283f4a2713aSLionel Sambuc SourceLocation NewLoc =
284f4a2713aSLionel Sambuc CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
285f4a2713aSLionel Sambuc return FullSourceLoc(NewLoc, CSM);
286f4a2713aSLionel Sambuc }
287f4a2713aSLionel Sambuc
288f4a2713aSLionel Sambuc
289f4a2713aSLionel Sambuc /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
290f4a2713aSLionel Sambuc /// error parsing inline asm. The SMDiagnostic indicates the error relative to
291f4a2713aSLionel Sambuc /// the temporary memory buffer that the inline asm parser has set up.
InlineAsmDiagHandler2(const llvm::SMDiagnostic & D,SourceLocation LocCookie)292f4a2713aSLionel Sambuc void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
293f4a2713aSLionel Sambuc SourceLocation LocCookie) {
294f4a2713aSLionel Sambuc // There are a couple of different kinds of errors we could get here. First,
295f4a2713aSLionel Sambuc // we re-format the SMDiagnostic in terms of a clang diagnostic.
296f4a2713aSLionel Sambuc
297f4a2713aSLionel Sambuc // Strip "error: " off the start of the message string.
298f4a2713aSLionel Sambuc StringRef Message = D.getMessage();
299f4a2713aSLionel Sambuc if (Message.startswith("error: "))
300f4a2713aSLionel Sambuc Message = Message.substr(7);
301f4a2713aSLionel Sambuc
302f4a2713aSLionel Sambuc // If the SMDiagnostic has an inline asm source location, translate it.
303f4a2713aSLionel Sambuc FullSourceLoc Loc;
304f4a2713aSLionel Sambuc if (D.getLoc() != SMLoc())
305f4a2713aSLionel Sambuc Loc = ConvertBackendLocation(D, Context->getSourceManager());
306f4a2713aSLionel Sambuc
307*0a6a1f1dSLionel Sambuc unsigned DiagID;
308*0a6a1f1dSLionel Sambuc switch (D.getKind()) {
309*0a6a1f1dSLionel Sambuc case llvm::SourceMgr::DK_Error:
310*0a6a1f1dSLionel Sambuc DiagID = diag::err_fe_inline_asm;
311*0a6a1f1dSLionel Sambuc break;
312*0a6a1f1dSLionel Sambuc case llvm::SourceMgr::DK_Warning:
313*0a6a1f1dSLionel Sambuc DiagID = diag::warn_fe_inline_asm;
314*0a6a1f1dSLionel Sambuc break;
315*0a6a1f1dSLionel Sambuc case llvm::SourceMgr::DK_Note:
316*0a6a1f1dSLionel Sambuc DiagID = diag::note_fe_inline_asm;
317*0a6a1f1dSLionel Sambuc break;
318*0a6a1f1dSLionel Sambuc }
319f4a2713aSLionel Sambuc // If this problem has clang-level source location information, report the
320*0a6a1f1dSLionel Sambuc // issue in the source with a note showing the instantiated
321f4a2713aSLionel Sambuc // code.
322f4a2713aSLionel Sambuc if (LocCookie.isValid()) {
323*0a6a1f1dSLionel Sambuc Diags.Report(LocCookie, DiagID).AddString(Message);
324f4a2713aSLionel Sambuc
325f4a2713aSLionel Sambuc if (D.getLoc().isValid()) {
326f4a2713aSLionel Sambuc DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
327f4a2713aSLionel Sambuc // Convert the SMDiagnostic ranges into SourceRange and attach them
328f4a2713aSLionel Sambuc // to the diagnostic.
329f4a2713aSLionel Sambuc for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
330f4a2713aSLionel Sambuc std::pair<unsigned, unsigned> Range = D.getRanges()[i];
331f4a2713aSLionel Sambuc unsigned Column = D.getColumnNo();
332f4a2713aSLionel Sambuc B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
333f4a2713aSLionel Sambuc Loc.getLocWithOffset(Range.second - Column));
334f4a2713aSLionel Sambuc }
335f4a2713aSLionel Sambuc }
336f4a2713aSLionel Sambuc return;
337f4a2713aSLionel Sambuc }
338f4a2713aSLionel Sambuc
339*0a6a1f1dSLionel Sambuc // Otherwise, report the backend issue as occurring in the generated .s file.
340*0a6a1f1dSLionel Sambuc // If Loc is invalid, we still need to report the issue, it just gets no
341f4a2713aSLionel Sambuc // location info.
342*0a6a1f1dSLionel Sambuc Diags.Report(Loc, DiagID).AddString(Message);
343f4a2713aSLionel Sambuc }
344f4a2713aSLionel Sambuc
345*0a6a1f1dSLionel Sambuc #define ComputeDiagID(Severity, GroupName, DiagID) \
346*0a6a1f1dSLionel Sambuc do { \
347*0a6a1f1dSLionel Sambuc switch (Severity) { \
348*0a6a1f1dSLionel Sambuc case llvm::DS_Error: \
349*0a6a1f1dSLionel Sambuc DiagID = diag::err_fe_##GroupName; \
350*0a6a1f1dSLionel Sambuc break; \
351*0a6a1f1dSLionel Sambuc case llvm::DS_Warning: \
352*0a6a1f1dSLionel Sambuc DiagID = diag::warn_fe_##GroupName; \
353*0a6a1f1dSLionel Sambuc break; \
354*0a6a1f1dSLionel Sambuc case llvm::DS_Remark: \
355*0a6a1f1dSLionel Sambuc llvm_unreachable("'remark' severity not expected"); \
356*0a6a1f1dSLionel Sambuc break; \
357*0a6a1f1dSLionel Sambuc case llvm::DS_Note: \
358*0a6a1f1dSLionel Sambuc DiagID = diag::note_fe_##GroupName; \
359*0a6a1f1dSLionel Sambuc break; \
360*0a6a1f1dSLionel Sambuc } \
361*0a6a1f1dSLionel Sambuc } while (false)
362*0a6a1f1dSLionel Sambuc
363*0a6a1f1dSLionel Sambuc #define ComputeDiagRemarkID(Severity, GroupName, DiagID) \
364*0a6a1f1dSLionel Sambuc do { \
365*0a6a1f1dSLionel Sambuc switch (Severity) { \
366*0a6a1f1dSLionel Sambuc case llvm::DS_Error: \
367*0a6a1f1dSLionel Sambuc DiagID = diag::err_fe_##GroupName; \
368*0a6a1f1dSLionel Sambuc break; \
369*0a6a1f1dSLionel Sambuc case llvm::DS_Warning: \
370*0a6a1f1dSLionel Sambuc DiagID = diag::warn_fe_##GroupName; \
371*0a6a1f1dSLionel Sambuc break; \
372*0a6a1f1dSLionel Sambuc case llvm::DS_Remark: \
373*0a6a1f1dSLionel Sambuc DiagID = diag::remark_fe_##GroupName; \
374*0a6a1f1dSLionel Sambuc break; \
375*0a6a1f1dSLionel Sambuc case llvm::DS_Note: \
376*0a6a1f1dSLionel Sambuc DiagID = diag::note_fe_##GroupName; \
377*0a6a1f1dSLionel Sambuc break; \
378*0a6a1f1dSLionel Sambuc } \
379*0a6a1f1dSLionel Sambuc } while (false)
380*0a6a1f1dSLionel Sambuc
381*0a6a1f1dSLionel Sambuc bool
InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm & D)382*0a6a1f1dSLionel Sambuc BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) {
383*0a6a1f1dSLionel Sambuc unsigned DiagID;
384*0a6a1f1dSLionel Sambuc ComputeDiagID(D.getSeverity(), inline_asm, DiagID);
385*0a6a1f1dSLionel Sambuc std::string Message = D.getMsgStr().str();
386*0a6a1f1dSLionel Sambuc
387*0a6a1f1dSLionel Sambuc // If this problem has clang-level source location information, report the
388*0a6a1f1dSLionel Sambuc // issue as being a problem in the source with a note showing the instantiated
389*0a6a1f1dSLionel Sambuc // code.
390*0a6a1f1dSLionel Sambuc SourceLocation LocCookie =
391*0a6a1f1dSLionel Sambuc SourceLocation::getFromRawEncoding(D.getLocCookie());
392*0a6a1f1dSLionel Sambuc if (LocCookie.isValid())
393*0a6a1f1dSLionel Sambuc Diags.Report(LocCookie, DiagID).AddString(Message);
394*0a6a1f1dSLionel Sambuc else {
395*0a6a1f1dSLionel Sambuc // Otherwise, report the backend diagnostic as occurring in the generated
396*0a6a1f1dSLionel Sambuc // .s file.
397*0a6a1f1dSLionel Sambuc // If Loc is invalid, we still need to report the diagnostic, it just gets
398*0a6a1f1dSLionel Sambuc // no location info.
399*0a6a1f1dSLionel Sambuc FullSourceLoc Loc;
400*0a6a1f1dSLionel Sambuc Diags.Report(Loc, DiagID).AddString(Message);
401*0a6a1f1dSLionel Sambuc }
402*0a6a1f1dSLionel Sambuc // We handled all the possible severities.
403*0a6a1f1dSLionel Sambuc return true;
404*0a6a1f1dSLionel Sambuc }
405*0a6a1f1dSLionel Sambuc
406*0a6a1f1dSLionel Sambuc bool
StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize & D)407*0a6a1f1dSLionel Sambuc BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) {
408*0a6a1f1dSLionel Sambuc if (D.getSeverity() != llvm::DS_Warning)
409*0a6a1f1dSLionel Sambuc // For now, the only support we have for StackSize diagnostic is warning.
410*0a6a1f1dSLionel Sambuc // We do not know how to format other severities.
411*0a6a1f1dSLionel Sambuc return false;
412*0a6a1f1dSLionel Sambuc
413*0a6a1f1dSLionel Sambuc if (const Decl *ND = Gen->GetDeclForMangledName(D.getFunction().getName())) {
414*0a6a1f1dSLionel Sambuc Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()),
415*0a6a1f1dSLionel Sambuc diag::warn_fe_frame_larger_than)
416*0a6a1f1dSLionel Sambuc << D.getStackSize() << Decl::castToDeclContext(ND);
417*0a6a1f1dSLionel Sambuc return true;
418*0a6a1f1dSLionel Sambuc }
419*0a6a1f1dSLionel Sambuc
420*0a6a1f1dSLionel Sambuc return false;
421*0a6a1f1dSLionel Sambuc }
422*0a6a1f1dSLionel Sambuc
EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase & D,unsigned DiagID)423*0a6a1f1dSLionel Sambuc void BackendConsumer::EmitOptimizationMessage(
424*0a6a1f1dSLionel Sambuc const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) {
425*0a6a1f1dSLionel Sambuc // We only support warnings and remarks.
426*0a6a1f1dSLionel Sambuc assert(D.getSeverity() == llvm::DS_Remark ||
427*0a6a1f1dSLionel Sambuc D.getSeverity() == llvm::DS_Warning);
428*0a6a1f1dSLionel Sambuc
429*0a6a1f1dSLionel Sambuc SourceManager &SourceMgr = Context->getSourceManager();
430*0a6a1f1dSLionel Sambuc FileManager &FileMgr = SourceMgr.getFileManager();
431*0a6a1f1dSLionel Sambuc StringRef Filename;
432*0a6a1f1dSLionel Sambuc unsigned Line, Column;
433*0a6a1f1dSLionel Sambuc D.getLocation(&Filename, &Line, &Column);
434*0a6a1f1dSLionel Sambuc SourceLocation DILoc;
435*0a6a1f1dSLionel Sambuc const FileEntry *FE = FileMgr.getFile(Filename);
436*0a6a1f1dSLionel Sambuc if (FE && Line > 0) {
437*0a6a1f1dSLionel Sambuc // If -gcolumn-info was not used, Column will be 0. This upsets the
438*0a6a1f1dSLionel Sambuc // source manager, so pass 1 if Column is not set.
439*0a6a1f1dSLionel Sambuc DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);
440*0a6a1f1dSLionel Sambuc }
441*0a6a1f1dSLionel Sambuc
442*0a6a1f1dSLionel Sambuc // If a location isn't available, try to approximate it using the associated
443*0a6a1f1dSLionel Sambuc // function definition. We use the definition's right brace to differentiate
444*0a6a1f1dSLionel Sambuc // from diagnostics that genuinely relate to the function itself.
445*0a6a1f1dSLionel Sambuc FullSourceLoc Loc(DILoc, SourceMgr);
446*0a6a1f1dSLionel Sambuc if (Loc.isInvalid())
447*0a6a1f1dSLionel Sambuc if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName()))
448*0a6a1f1dSLionel Sambuc Loc = FD->getASTContext().getFullLoc(FD->getBodyRBrace());
449*0a6a1f1dSLionel Sambuc
450*0a6a1f1dSLionel Sambuc Diags.Report(Loc, DiagID)
451*0a6a1f1dSLionel Sambuc << AddFlagValue(D.getPassName() ? D.getPassName() : "")
452*0a6a1f1dSLionel Sambuc << D.getMsg().str();
453*0a6a1f1dSLionel Sambuc
454*0a6a1f1dSLionel Sambuc if (DILoc.isInvalid())
455*0a6a1f1dSLionel Sambuc // If we were not able to translate the file:line:col information
456*0a6a1f1dSLionel Sambuc // back to a SourceLocation, at least emit a note stating that
457*0a6a1f1dSLionel Sambuc // we could not translate this location. This can happen in the
458*0a6a1f1dSLionel Sambuc // case of #line directives.
459*0a6a1f1dSLionel Sambuc Diags.Report(Loc, diag::note_fe_backend_optimization_remark_invalid_loc)
460*0a6a1f1dSLionel Sambuc << Filename << Line << Column;
461*0a6a1f1dSLionel Sambuc }
462*0a6a1f1dSLionel Sambuc
OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark & D)463*0a6a1f1dSLionel Sambuc void BackendConsumer::OptimizationRemarkHandler(
464*0a6a1f1dSLionel Sambuc const llvm::DiagnosticInfoOptimizationRemark &D) {
465*0a6a1f1dSLionel Sambuc // Optimization remarks are active only if the -Rpass flag has a regular
466*0a6a1f1dSLionel Sambuc // expression that matches the name of the pass name in \p D.
467*0a6a1f1dSLionel Sambuc if (CodeGenOpts.OptimizationRemarkPattern &&
468*0a6a1f1dSLionel Sambuc CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName()))
469*0a6a1f1dSLionel Sambuc EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark);
470*0a6a1f1dSLionel Sambuc }
471*0a6a1f1dSLionel Sambuc
OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemarkMissed & D)472*0a6a1f1dSLionel Sambuc void BackendConsumer::OptimizationRemarkHandler(
473*0a6a1f1dSLionel Sambuc const llvm::DiagnosticInfoOptimizationRemarkMissed &D) {
474*0a6a1f1dSLionel Sambuc // Missed optimization remarks are active only if the -Rpass-missed
475*0a6a1f1dSLionel Sambuc // flag has a regular expression that matches the name of the pass
476*0a6a1f1dSLionel Sambuc // name in \p D.
477*0a6a1f1dSLionel Sambuc if (CodeGenOpts.OptimizationRemarkMissedPattern &&
478*0a6a1f1dSLionel Sambuc CodeGenOpts.OptimizationRemarkMissedPattern->match(D.getPassName()))
479*0a6a1f1dSLionel Sambuc EmitOptimizationMessage(D,
480*0a6a1f1dSLionel Sambuc diag::remark_fe_backend_optimization_remark_missed);
481*0a6a1f1dSLionel Sambuc }
482*0a6a1f1dSLionel Sambuc
OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemarkAnalysis & D)483*0a6a1f1dSLionel Sambuc void BackendConsumer::OptimizationRemarkHandler(
484*0a6a1f1dSLionel Sambuc const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D) {
485*0a6a1f1dSLionel Sambuc // Optimization analysis remarks are active only if the -Rpass-analysis
486*0a6a1f1dSLionel Sambuc // flag has a regular expression that matches the name of the pass
487*0a6a1f1dSLionel Sambuc // name in \p D.
488*0a6a1f1dSLionel Sambuc if (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
489*0a6a1f1dSLionel Sambuc CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName()))
490*0a6a1f1dSLionel Sambuc EmitOptimizationMessage(
491*0a6a1f1dSLionel Sambuc D, diag::remark_fe_backend_optimization_remark_analysis);
492*0a6a1f1dSLionel Sambuc }
493*0a6a1f1dSLionel Sambuc
OptimizationFailureHandler(const llvm::DiagnosticInfoOptimizationFailure & D)494*0a6a1f1dSLionel Sambuc void BackendConsumer::OptimizationFailureHandler(
495*0a6a1f1dSLionel Sambuc const llvm::DiagnosticInfoOptimizationFailure &D) {
496*0a6a1f1dSLionel Sambuc EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure);
497*0a6a1f1dSLionel Sambuc }
498*0a6a1f1dSLionel Sambuc
linkerDiagnosticHandler(const DiagnosticInfo & DI)499*0a6a1f1dSLionel Sambuc void BackendConsumer::linkerDiagnosticHandler(const DiagnosticInfo &DI) {
500*0a6a1f1dSLionel Sambuc if (DI.getSeverity() != DS_Error)
501*0a6a1f1dSLionel Sambuc return;
502*0a6a1f1dSLionel Sambuc
503*0a6a1f1dSLionel Sambuc std::string MsgStorage;
504*0a6a1f1dSLionel Sambuc {
505*0a6a1f1dSLionel Sambuc raw_string_ostream Stream(MsgStorage);
506*0a6a1f1dSLionel Sambuc DiagnosticPrinterRawOStream DP(Stream);
507*0a6a1f1dSLionel Sambuc DI.print(DP);
508*0a6a1f1dSLionel Sambuc }
509*0a6a1f1dSLionel Sambuc
510*0a6a1f1dSLionel Sambuc Diags.Report(diag::err_fe_cannot_link_module)
511*0a6a1f1dSLionel Sambuc << LinkModule->getModuleIdentifier() << MsgStorage;
512*0a6a1f1dSLionel Sambuc }
513*0a6a1f1dSLionel Sambuc
514*0a6a1f1dSLionel Sambuc /// \brief This function is invoked when the backend needs
515*0a6a1f1dSLionel Sambuc /// to report something to the user.
DiagnosticHandlerImpl(const DiagnosticInfo & DI)516*0a6a1f1dSLionel Sambuc void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
517*0a6a1f1dSLionel Sambuc unsigned DiagID = diag::err_fe_inline_asm;
518*0a6a1f1dSLionel Sambuc llvm::DiagnosticSeverity Severity = DI.getSeverity();
519*0a6a1f1dSLionel Sambuc // Get the diagnostic ID based.
520*0a6a1f1dSLionel Sambuc switch (DI.getKind()) {
521*0a6a1f1dSLionel Sambuc case llvm::DK_InlineAsm:
522*0a6a1f1dSLionel Sambuc if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI)))
523*0a6a1f1dSLionel Sambuc return;
524*0a6a1f1dSLionel Sambuc ComputeDiagID(Severity, inline_asm, DiagID);
525*0a6a1f1dSLionel Sambuc break;
526*0a6a1f1dSLionel Sambuc case llvm::DK_StackSize:
527*0a6a1f1dSLionel Sambuc if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI)))
528*0a6a1f1dSLionel Sambuc return;
529*0a6a1f1dSLionel Sambuc ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
530*0a6a1f1dSLionel Sambuc break;
531*0a6a1f1dSLionel Sambuc case llvm::DK_OptimizationRemark:
532*0a6a1f1dSLionel Sambuc // Optimization remarks are always handled completely by this
533*0a6a1f1dSLionel Sambuc // handler. There is no generic way of emitting them.
534*0a6a1f1dSLionel Sambuc OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemark>(DI));
535*0a6a1f1dSLionel Sambuc return;
536*0a6a1f1dSLionel Sambuc case llvm::DK_OptimizationRemarkMissed:
537*0a6a1f1dSLionel Sambuc // Optimization remarks are always handled completely by this
538*0a6a1f1dSLionel Sambuc // handler. There is no generic way of emitting them.
539*0a6a1f1dSLionel Sambuc OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemarkMissed>(DI));
540*0a6a1f1dSLionel Sambuc return;
541*0a6a1f1dSLionel Sambuc case llvm::DK_OptimizationRemarkAnalysis:
542*0a6a1f1dSLionel Sambuc // Optimization remarks are always handled completely by this
543*0a6a1f1dSLionel Sambuc // handler. There is no generic way of emitting them.
544*0a6a1f1dSLionel Sambuc OptimizationRemarkHandler(
545*0a6a1f1dSLionel Sambuc cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI));
546*0a6a1f1dSLionel Sambuc return;
547*0a6a1f1dSLionel Sambuc case llvm::DK_OptimizationFailure:
548*0a6a1f1dSLionel Sambuc // Optimization failures are always handled completely by this
549*0a6a1f1dSLionel Sambuc // handler.
550*0a6a1f1dSLionel Sambuc OptimizationFailureHandler(cast<DiagnosticInfoOptimizationFailure>(DI));
551*0a6a1f1dSLionel Sambuc return;
552*0a6a1f1dSLionel Sambuc default:
553*0a6a1f1dSLionel Sambuc // Plugin IDs are not bound to any value as they are set dynamically.
554*0a6a1f1dSLionel Sambuc ComputeDiagRemarkID(Severity, backend_plugin, DiagID);
555*0a6a1f1dSLionel Sambuc break;
556*0a6a1f1dSLionel Sambuc }
557*0a6a1f1dSLionel Sambuc std::string MsgStorage;
558*0a6a1f1dSLionel Sambuc {
559*0a6a1f1dSLionel Sambuc raw_string_ostream Stream(MsgStorage);
560*0a6a1f1dSLionel Sambuc DiagnosticPrinterRawOStream DP(Stream);
561*0a6a1f1dSLionel Sambuc DI.print(DP);
562*0a6a1f1dSLionel Sambuc }
563*0a6a1f1dSLionel Sambuc
564*0a6a1f1dSLionel Sambuc // Report the backend message using the usual diagnostic mechanism.
565*0a6a1f1dSLionel Sambuc FullSourceLoc Loc;
566*0a6a1f1dSLionel Sambuc Diags.Report(Loc, DiagID).AddString(MsgStorage);
567*0a6a1f1dSLionel Sambuc }
568*0a6a1f1dSLionel Sambuc #undef ComputeDiagID
569f4a2713aSLionel Sambuc
CodeGenAction(unsigned _Act,LLVMContext * _VMContext)570f4a2713aSLionel Sambuc CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
571*0a6a1f1dSLionel Sambuc : Act(_Act), LinkModule(nullptr),
572f4a2713aSLionel Sambuc VMContext(_VMContext ? _VMContext : new LLVMContext),
573f4a2713aSLionel Sambuc OwnsVMContext(!_VMContext) {}
574f4a2713aSLionel Sambuc
~CodeGenAction()575f4a2713aSLionel Sambuc CodeGenAction::~CodeGenAction() {
576f4a2713aSLionel Sambuc TheModule.reset();
577f4a2713aSLionel Sambuc if (OwnsVMContext)
578f4a2713aSLionel Sambuc delete VMContext;
579f4a2713aSLionel Sambuc }
580f4a2713aSLionel Sambuc
hasIRSupport() const581f4a2713aSLionel Sambuc bool CodeGenAction::hasIRSupport() const { return true; }
582f4a2713aSLionel Sambuc
EndSourceFileAction()583f4a2713aSLionel Sambuc void CodeGenAction::EndSourceFileAction() {
584f4a2713aSLionel Sambuc // If the consumer creation failed, do nothing.
585f4a2713aSLionel Sambuc if (!getCompilerInstance().hasASTConsumer())
586f4a2713aSLionel Sambuc return;
587f4a2713aSLionel Sambuc
588f4a2713aSLionel Sambuc // If we were given a link module, release consumer's ownership of it.
589f4a2713aSLionel Sambuc if (LinkModule)
590f4a2713aSLionel Sambuc BEConsumer->takeLinkModule();
591f4a2713aSLionel Sambuc
592f4a2713aSLionel Sambuc // Steal the module from the consumer.
593*0a6a1f1dSLionel Sambuc TheModule = BEConsumer->takeModule();
594f4a2713aSLionel Sambuc }
595f4a2713aSLionel Sambuc
takeModule()596*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::Module> CodeGenAction::takeModule() {
597*0a6a1f1dSLionel Sambuc return std::move(TheModule);
598f4a2713aSLionel Sambuc }
599f4a2713aSLionel Sambuc
takeLLVMContext()600f4a2713aSLionel Sambuc llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
601f4a2713aSLionel Sambuc OwnsVMContext = false;
602f4a2713aSLionel Sambuc return VMContext;
603f4a2713aSLionel Sambuc }
604f4a2713aSLionel Sambuc
GetOutputStream(CompilerInstance & CI,StringRef InFile,BackendAction Action)605f4a2713aSLionel Sambuc static raw_ostream *GetOutputStream(CompilerInstance &CI,
606f4a2713aSLionel Sambuc StringRef InFile,
607f4a2713aSLionel Sambuc BackendAction Action) {
608f4a2713aSLionel Sambuc switch (Action) {
609f4a2713aSLionel Sambuc case Backend_EmitAssembly:
610f4a2713aSLionel Sambuc return CI.createDefaultOutputFile(false, InFile, "s");
611f4a2713aSLionel Sambuc case Backend_EmitLL:
612f4a2713aSLionel Sambuc return CI.createDefaultOutputFile(false, InFile, "ll");
613f4a2713aSLionel Sambuc case Backend_EmitBC:
614f4a2713aSLionel Sambuc return CI.createDefaultOutputFile(true, InFile, "bc");
615f4a2713aSLionel Sambuc case Backend_EmitNothing:
616*0a6a1f1dSLionel Sambuc return nullptr;
617f4a2713aSLionel Sambuc case Backend_EmitMCNull:
618*0a6a1f1dSLionel Sambuc return CI.createNullOutputFile();
619f4a2713aSLionel Sambuc case Backend_EmitObj:
620f4a2713aSLionel Sambuc return CI.createDefaultOutputFile(true, InFile, "o");
621f4a2713aSLionel Sambuc }
622f4a2713aSLionel Sambuc
623f4a2713aSLionel Sambuc llvm_unreachable("Invalid action!");
624f4a2713aSLionel Sambuc }
625f4a2713aSLionel Sambuc
626*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)627*0a6a1f1dSLionel Sambuc CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
628f4a2713aSLionel Sambuc BackendAction BA = static_cast<BackendAction>(Act);
629*0a6a1f1dSLionel Sambuc std::unique_ptr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
630f4a2713aSLionel Sambuc if (BA != Backend_EmitNothing && !OS)
631*0a6a1f1dSLionel Sambuc return nullptr;
632f4a2713aSLionel Sambuc
633f4a2713aSLionel Sambuc llvm::Module *LinkModuleToUse = LinkModule;
634f4a2713aSLionel Sambuc
635f4a2713aSLionel Sambuc // If we were not given a link module, and the user requested that one be
636f4a2713aSLionel Sambuc // loaded from bitcode, do so now.
637f4a2713aSLionel Sambuc const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
638f4a2713aSLionel Sambuc if (!LinkModuleToUse && !LinkBCFile.empty()) {
639*0a6a1f1dSLionel Sambuc auto BCBuf = CI.getFileManager().getBufferForFile(LinkBCFile);
640f4a2713aSLionel Sambuc if (!BCBuf) {
641f4a2713aSLionel Sambuc CI.getDiagnostics().Report(diag::err_cannot_open_file)
642*0a6a1f1dSLionel Sambuc << LinkBCFile << BCBuf.getError().message();
643*0a6a1f1dSLionel Sambuc return nullptr;
644f4a2713aSLionel Sambuc }
645f4a2713aSLionel Sambuc
646*0a6a1f1dSLionel Sambuc ErrorOr<llvm::Module *> ModuleOrErr =
647*0a6a1f1dSLionel Sambuc getLazyBitcodeModule(std::move(*BCBuf), *VMContext);
648*0a6a1f1dSLionel Sambuc if (std::error_code EC = ModuleOrErr.getError()) {
649f4a2713aSLionel Sambuc CI.getDiagnostics().Report(diag::err_cannot_open_file)
650*0a6a1f1dSLionel Sambuc << LinkBCFile << EC.message();
651*0a6a1f1dSLionel Sambuc return nullptr;
652f4a2713aSLionel Sambuc }
653*0a6a1f1dSLionel Sambuc LinkModuleToUse = ModuleOrErr.get();
654f4a2713aSLionel Sambuc }
655f4a2713aSLionel Sambuc
656*0a6a1f1dSLionel Sambuc CoverageSourceInfo *CoverageInfo = nullptr;
657*0a6a1f1dSLionel Sambuc // Add the preprocessor callback only when the coverage mapping is generated.
658*0a6a1f1dSLionel Sambuc if (CI.getCodeGenOpts().CoverageMapping) {
659*0a6a1f1dSLionel Sambuc CoverageInfo = new CoverageSourceInfo;
660*0a6a1f1dSLionel Sambuc CI.getPreprocessor().addPPCallbacks(
661*0a6a1f1dSLionel Sambuc std::unique_ptr<PPCallbacks>(CoverageInfo));
662*0a6a1f1dSLionel Sambuc }
663*0a6a1f1dSLionel Sambuc std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
664*0a6a1f1dSLionel Sambuc BA, CI.getDiagnostics(), CI.getCodeGenOpts(), CI.getTargetOpts(),
665*0a6a1f1dSLionel Sambuc CI.getLangOpts(), CI.getFrontendOpts().ShowTimers, InFile,
666*0a6a1f1dSLionel Sambuc LinkModuleToUse, OS.release(), *VMContext, CoverageInfo));
667*0a6a1f1dSLionel Sambuc BEConsumer = Result.get();
668*0a6a1f1dSLionel Sambuc return std::move(Result);
669f4a2713aSLionel Sambuc }
670f4a2713aSLionel Sambuc
ExecuteAction()671f4a2713aSLionel Sambuc void CodeGenAction::ExecuteAction() {
672f4a2713aSLionel Sambuc // If this is an IR file, we have to treat it specially.
673f4a2713aSLionel Sambuc if (getCurrentFileKind() == IK_LLVM_IR) {
674f4a2713aSLionel Sambuc BackendAction BA = static_cast<BackendAction>(Act);
675f4a2713aSLionel Sambuc CompilerInstance &CI = getCompilerInstance();
676f4a2713aSLionel Sambuc raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
677f4a2713aSLionel Sambuc if (BA != Backend_EmitNothing && !OS)
678f4a2713aSLionel Sambuc return;
679f4a2713aSLionel Sambuc
680f4a2713aSLionel Sambuc bool Invalid;
681f4a2713aSLionel Sambuc SourceManager &SM = CI.getSourceManager();
682*0a6a1f1dSLionel Sambuc FileID FID = SM.getMainFileID();
683*0a6a1f1dSLionel Sambuc llvm::MemoryBuffer *MainFile = SM.getBuffer(FID, &Invalid);
684f4a2713aSLionel Sambuc if (Invalid)
685f4a2713aSLionel Sambuc return;
686f4a2713aSLionel Sambuc
687f4a2713aSLionel Sambuc llvm::SMDiagnostic Err;
688*0a6a1f1dSLionel Sambuc TheModule = parseIR(MainFile->getMemBufferRef(), Err, *VMContext);
689f4a2713aSLionel Sambuc if (!TheModule) {
690*0a6a1f1dSLionel Sambuc // Translate from the diagnostic info to the SourceManager location if
691*0a6a1f1dSLionel Sambuc // available.
692*0a6a1f1dSLionel Sambuc // TODO: Unify this with ConvertBackendLocation()
693*0a6a1f1dSLionel Sambuc SourceLocation Loc;
694*0a6a1f1dSLionel Sambuc if (Err.getLineNo() > 0) {
695*0a6a1f1dSLionel Sambuc assert(Err.getColumnNo() >= 0);
696*0a6a1f1dSLionel Sambuc Loc = SM.translateFileLineCol(SM.getFileEntryForID(FID),
697*0a6a1f1dSLionel Sambuc Err.getLineNo(), Err.getColumnNo() + 1);
698*0a6a1f1dSLionel Sambuc }
699f4a2713aSLionel Sambuc
700*0a6a1f1dSLionel Sambuc // Strip off a leading diagnostic code if there is one.
701f4a2713aSLionel Sambuc StringRef Msg = Err.getMessage();
702f4a2713aSLionel Sambuc if (Msg.startswith("error: "))
703f4a2713aSLionel Sambuc Msg = Msg.substr(7);
704f4a2713aSLionel Sambuc
705*0a6a1f1dSLionel Sambuc unsigned DiagID =
706*0a6a1f1dSLionel Sambuc CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
707f4a2713aSLionel Sambuc
708*0a6a1f1dSLionel Sambuc CI.getDiagnostics().Report(Loc, DiagID) << Msg;
709f4a2713aSLionel Sambuc return;
710f4a2713aSLionel Sambuc }
711*0a6a1f1dSLionel Sambuc const TargetOptions &TargetOpts = CI.getTargetOpts();
712*0a6a1f1dSLionel Sambuc if (TheModule->getTargetTriple() != TargetOpts.Triple) {
713*0a6a1f1dSLionel Sambuc unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
714*0a6a1f1dSLionel Sambuc DiagnosticsEngine::Warning,
715*0a6a1f1dSLionel Sambuc "overriding the module target triple with %0");
716f4a2713aSLionel Sambuc
717*0a6a1f1dSLionel Sambuc CI.getDiagnostics().Report(SourceLocation(), DiagID) << TargetOpts.Triple;
718*0a6a1f1dSLionel Sambuc TheModule->setTargetTriple(TargetOpts.Triple);
719*0a6a1f1dSLionel Sambuc }
720*0a6a1f1dSLionel Sambuc
721*0a6a1f1dSLionel Sambuc EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts,
722*0a6a1f1dSLionel Sambuc CI.getLangOpts(), CI.getTarget().getTargetDescription(),
723*0a6a1f1dSLionel Sambuc TheModule.get(), BA, OS);
724f4a2713aSLionel Sambuc return;
725f4a2713aSLionel Sambuc }
726f4a2713aSLionel Sambuc
727f4a2713aSLionel Sambuc // Otherwise follow the normal AST path.
728f4a2713aSLionel Sambuc this->ASTFrontendAction::ExecuteAction();
729f4a2713aSLionel Sambuc }
730f4a2713aSLionel Sambuc
731f4a2713aSLionel Sambuc //
732f4a2713aSLionel Sambuc
anchor()733f4a2713aSLionel Sambuc void EmitAssemblyAction::anchor() { }
EmitAssemblyAction(llvm::LLVMContext * _VMContext)734f4a2713aSLionel Sambuc EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
735f4a2713aSLionel Sambuc : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
736f4a2713aSLionel Sambuc
anchor()737f4a2713aSLionel Sambuc void EmitBCAction::anchor() { }
EmitBCAction(llvm::LLVMContext * _VMContext)738f4a2713aSLionel Sambuc EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
739f4a2713aSLionel Sambuc : CodeGenAction(Backend_EmitBC, _VMContext) {}
740f4a2713aSLionel Sambuc
anchor()741f4a2713aSLionel Sambuc void EmitLLVMAction::anchor() { }
EmitLLVMAction(llvm::LLVMContext * _VMContext)742f4a2713aSLionel Sambuc EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
743f4a2713aSLionel Sambuc : CodeGenAction(Backend_EmitLL, _VMContext) {}
744f4a2713aSLionel Sambuc
anchor()745f4a2713aSLionel Sambuc void EmitLLVMOnlyAction::anchor() { }
EmitLLVMOnlyAction(llvm::LLVMContext * _VMContext)746f4a2713aSLionel Sambuc EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
747f4a2713aSLionel Sambuc : CodeGenAction(Backend_EmitNothing, _VMContext) {}
748f4a2713aSLionel Sambuc
anchor()749f4a2713aSLionel Sambuc void EmitCodeGenOnlyAction::anchor() { }
EmitCodeGenOnlyAction(llvm::LLVMContext * _VMContext)750f4a2713aSLionel Sambuc EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
751f4a2713aSLionel Sambuc : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
752f4a2713aSLionel Sambuc
anchor()753f4a2713aSLionel Sambuc void EmitObjAction::anchor() { }
EmitObjAction(llvm::LLVMContext * _VMContext)754f4a2713aSLionel Sambuc EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
755f4a2713aSLionel Sambuc : CodeGenAction(Backend_EmitObj, _VMContext) {}
756