2010-02-25 12:37:45 +08:00
|
|
|
//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
|
2008-10-22 07:49:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
#include "CoverageMappingGen.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-11-15 14:48:46 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-05-24 04:37:38 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "clang/AST/DeclGroup.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/CodeGen/BackendUtil.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "clang/CodeGen/CodeGenAction.h"
|
2009-11-15 14:48:46 +08:00
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
2010-02-25 12:37:45 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2009-12-03 17:12:54 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2014-04-17 00:54:24 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2014-02-07 02:30:43 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-26 10:25:54 +08:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2014-03-06 11:46:44 +08:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2010-06-08 07:21:04 +08:00
|
|
|
#include "llvm/Pass.h"
|
2010-04-07 02:38:50 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2009-02-18 09:37:30 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2008-10-22 07:49:24 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2011-01-26 04:34:14 +08:00
|
|
|
namespace clang {
|
2009-11-28 18:07:24 +08:00
|
|
|
class BackendConsumer : public ASTConsumer {
|
2011-12-20 10:48:34 +08:00
|
|
|
virtual void anchor();
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags;
|
2008-10-22 07:49:24 +08:00
|
|
|
BackendAction Action;
|
2009-11-30 16:39:32 +08:00
|
|
|
const CodeGenOptions &CodeGenOpts;
|
|
|
|
const TargetOptions &TargetOpts;
|
2011-07-06 06:02:36 +08:00
|
|
|
const LangOptions &LangOpts;
|
2015-04-14 23:15:49 +08:00
|
|
|
raw_pwrite_stream *AsmOutStream;
|
2009-03-05 16:00:35 +08:00
|
|
|
ASTContext *Context;
|
2008-10-29 16:50:02 +08:00
|
|
|
|
2009-02-18 09:37:30 +08:00
|
|
|
Timer LLVMIRGeneration;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CodeGenerator> Gen;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<llvm::Module> TheModule, LinkModule;
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
public:
|
2015-05-23 06:16:55 +08:00
|
|
|
BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
|
2015-06-30 10:26:03 +08:00
|
|
|
const HeaderSearchOptions &HeaderSearchOpts,
|
|
|
|
const PreprocessorOptions &PPOpts,
|
2015-05-23 06:16:55 +08:00
|
|
|
const CodeGenOptions &CodeGenOpts,
|
|
|
|
const TargetOptions &TargetOpts,
|
|
|
|
const LangOptions &LangOpts, bool TimePasses,
|
|
|
|
const std::string &InFile, llvm::Module *LinkModule,
|
2015-04-14 23:15:49 +08:00
|
|
|
raw_pwrite_stream *OS, LLVMContext &C,
|
2014-08-05 02:41:51 +08:00
|
|
|
CoverageSourceInfo *CoverageInfo = nullptr)
|
2015-05-23 06:16:55 +08:00
|
|
|
: Diags(Diags), Action(Action), CodeGenOpts(CodeGenOpts),
|
|
|
|
TargetOpts(TargetOpts), LangOpts(LangOpts), AsmOutStream(OS),
|
2014-12-25 20:21:56 +08:00
|
|
|
Context(nullptr), LLVMIRGeneration("LLVM IR Generation Time"),
|
2015-06-30 10:26:03 +08:00
|
|
|
Gen(CreateLLVMCodeGen(Diags, InFile, HeaderSearchOpts, PPOpts,
|
|
|
|
CodeGenOpts, C, CoverageInfo)),
|
2013-12-19 00:38:48 +08:00
|
|
|
LinkModule(LinkModule) {
|
2009-11-30 16:39:52 +08:00
|
|
|
llvm::TimePassesIsEnabled = TimePasses;
|
2009-02-18 09:23:44 +08:00
|
|
|
}
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2014-08-29 13:08:19 +08:00
|
|
|
std::unique_ptr<llvm::Module> takeModule() { return std::move(TheModule); }
|
2014-03-08 03:33:25 +08:00
|
|
|
llvm::Module *takeLinkModule() { return LinkModule.release(); }
|
2010-02-25 12:37:50 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
|
2012-03-08 23:51:03 +08:00
|
|
|
Gen->HandleCXXStaticMemberVarInstantiation(VD);
|
2012-03-05 18:54:55 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void Initialize(ASTContext &Ctx) override {
|
2015-04-29 02:24:12 +08:00
|
|
|
if (Context) {
|
|
|
|
assert(Context == &Ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-28 10:18:25 +08:00
|
|
|
Context = &Ctx;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 10:18:25 +08:00
|
|
|
Gen->Initialize(Ctx);
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2010-02-25 12:37:50 +08:00
|
|
|
TheModule.reset(Gen->GetModule());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
bool HandleTopLevelDecl(DeclGroupRef D) override {
|
2009-03-30 00:50:03 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
|
2009-03-05 16:00:35 +08:00
|
|
|
Context->getSourceManager(),
|
|
|
|
"LLVM IR generation of declaration");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2009-03-30 00:50:03 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
Gen->HandleTopLevelDecl(D);
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
2011-11-18 08:26:59 +08:00
|
|
|
|
|
|
|
return true;
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-05-24 04:37:38 +08:00
|
|
|
void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
|
|
|
|
PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
|
|
|
|
Context->getSourceManager(),
|
|
|
|
"LLVM IR generation of inline method");
|
|
|
|
if (llvm::TimePassesIsEnabled)
|
|
|
|
LLVMIRGeneration.startTimer();
|
|
|
|
|
|
|
|
Gen->HandleInlineMethodDefinition(D);
|
|
|
|
|
|
|
|
if (llvm::TimePassesIsEnabled)
|
|
|
|
LLVMIRGeneration.stopTimer();
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleTranslationUnit(ASTContext &C) override {
|
2009-03-05 16:00:35 +08:00
|
|
|
{
|
2009-03-06 14:46:31 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-03-05 16:00:35 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
Gen->HandleTranslationUnit(C);
|
2008-11-11 14:35:39 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-03-05 16:00:35 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
|
|
|
}
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
// Silently ignore if we weren't initialized for some reason.
|
2010-06-08 07:21:04 +08:00
|
|
|
if (!TheModule)
|
2010-06-08 07:20:08 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Make sure IR generation is happy with the module. This is released by
|
|
|
|
// the module provider.
|
2011-12-01 07:21:26 +08:00
|
|
|
llvm::Module *M = Gen->ReleaseModule();
|
2010-06-08 07:20:08 +08:00
|
|
|
if (!M) {
|
|
|
|
// The module has been released by IR gen on failures, do not double
|
|
|
|
// free.
|
2014-03-08 03:33:25 +08:00
|
|
|
TheModule.release();
|
2010-06-08 07:20:08 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
assert(TheModule.get() == M &&
|
|
|
|
"Unexpected module change during IR generation");
|
|
|
|
|
2011-10-31 01:30:44 +08:00
|
|
|
// Link LinkModule into this module if present, preserving its validity.
|
|
|
|
if (LinkModule) {
|
2014-10-28 07:02:34 +08:00
|
|
|
if (Linker::LinkModules(
|
2014-10-28 08:29:51 +08:00
|
|
|
M, LinkModule.get(),
|
2014-10-28 07:02:34 +08:00
|
|
|
[=](const DiagnosticInfo &DI) { linkerDiagnosticHandler(DI); }))
|
2011-10-31 01:30:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
// Install an inline asm handler so that diagnostics get printed through
|
|
|
|
// our diagnostics hooks.
|
|
|
|
LLVMContext &Ctx = TheModule->getContext();
|
2010-11-17 16:13:04 +08:00
|
|
|
LLVMContext::InlineAsmDiagHandlerTy OldHandler =
|
|
|
|
Ctx.getInlineAsmDiagnosticHandler();
|
2010-06-08 07:20:08 +08:00
|
|
|
void *OldContext = Ctx.getInlineAsmDiagnosticContext();
|
2010-11-17 16:13:04 +08:00
|
|
|
Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2014-02-07 02:30:43 +08:00
|
|
|
LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
|
|
|
|
Ctx.getDiagnosticHandler();
|
|
|
|
void *OldDiagnosticContext = Ctx.getDiagnosticContext();
|
|
|
|
Ctx.setDiagnosticHandler(DiagnosticHandler, this);
|
|
|
|
|
2011-07-06 06:02:36 +08:00
|
|
|
EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
|
2014-01-02 23:08:04 +08:00
|
|
|
C.getTargetInfo().getTargetDescription(),
|
2010-06-08 07:21:04 +08:00
|
|
|
TheModule.get(), Action, AsmOutStream);
|
2013-12-19 00:38:48 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
|
2014-02-07 02:30:43 +08:00
|
|
|
|
|
|
|
Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext);
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleTagDeclDefinition(TagDecl *D) override {
|
2009-03-05 16:00:35 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
|
|
|
|
Context->getSourceManager(),
|
|
|
|
"LLVM IR generation of declaration");
|
2008-10-22 07:49:24 +08:00
|
|
|
Gen->HandleTagDeclDefinition(D);
|
|
|
|
}
|
2009-04-22 01:11:58 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
|
2013-07-14 05:08:14 +08:00
|
|
|
Gen->HandleTagDeclRequiredDefinition(D);
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void CompleteTentativeDefinition(VarDecl *D) override {
|
2009-04-22 01:11:58 +08:00
|
|
|
Gen->CompleteTentativeDefinition(D);
|
|
|
|
}
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2015-01-15 12:07:35 +08:00
|
|
|
void HandleVTable(CXXRecordDecl *RD) override {
|
|
|
|
Gen->HandleVTable(RD);
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
llvm-svn: 103718
2010-05-14 00:44:06 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
|
2013-05-08 21:44:39 +08:00
|
|
|
Gen->HandleLinkerOptionPragma(Opts);
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleDetectMismatch(llvm::StringRef Name,
|
|
|
|
llvm::StringRef Value) override {
|
2013-06-04 10:07:14 +08:00
|
|
|
Gen->HandleDetectMismatch(Name, Value);
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleDependentLibrary(llvm::StringRef Opts) override {
|
2013-05-08 21:44:39 +08:00
|
|
|
Gen->HandleDependentLibrary(Opts);
|
|
|
|
}
|
|
|
|
|
2010-04-07 02:38:50 +08:00
|
|
|
static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
|
|
|
|
unsigned LocCookie) {
|
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
|
|
|
|
((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
|
|
|
|
}
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2014-10-28 07:02:34 +08:00
|
|
|
void linkerDiagnosticHandler(const llvm::DiagnosticInfo &DI);
|
2014-10-25 12:06:14 +08:00
|
|
|
|
2014-02-07 02:30:43 +08:00
|
|
|
static void DiagnosticHandler(const llvm::DiagnosticInfo &DI,
|
|
|
|
void *Context) {
|
|
|
|
((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI);
|
|
|
|
}
|
|
|
|
|
2010-04-07 02:38:50 +08:00
|
|
|
void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
|
|
|
|
SourceLocation LocCookie);
|
2014-02-07 02:30:43 +08:00
|
|
|
|
|
|
|
void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
|
|
|
|
/// \brief Specialized handler for InlineAsm diagnostic.
|
|
|
|
/// \return True if the diagnostic has been successfully reported, false
|
|
|
|
/// otherwise.
|
|
|
|
bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
|
|
|
|
/// \brief Specialized handler for StackSize diagnostic.
|
|
|
|
/// \return True if the diagnostic has been successfully reported, false
|
|
|
|
/// otherwise.
|
|
|
|
bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
|
2014-05-30 03:55:06 +08:00
|
|
|
/// \brief Specialized handlers for optimization remarks.
|
|
|
|
/// Note that these handlers only accept remarks and they always handle
|
2014-04-17 00:54:24 +08:00
|
|
|
/// them.
|
2014-07-19 03:40:19 +08:00
|
|
|
void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
|
|
|
|
unsigned DiagID);
|
2014-05-30 03:55:06 +08:00
|
|
|
void
|
2014-04-17 00:54:24 +08:00
|
|
|
OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark &D);
|
2014-05-30 03:55:06 +08:00
|
|
|
void OptimizationRemarkHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationRemarkMissed &D);
|
|
|
|
void OptimizationRemarkHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D);
|
2014-07-19 03:40:19 +08:00
|
|
|
void OptimizationFailureHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationFailure &D);
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2011-12-20 10:48:34 +08:00
|
|
|
|
|
|
|
void BackendConsumer::anchor() {}
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
|
|
|
|
/// buffer to be a valid FullSourceLoc.
|
|
|
|
static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
|
|
|
|
SourceManager &CSM) {
|
|
|
|
// Get both the clang and llvm source managers. The location is relative to
|
|
|
|
// a memory buffer that the LLVM Source Manager is handling, we need to add
|
2010-04-30 00:29:09 +08:00
|
|
|
// a copy to the Clang source manager.
|
2010-04-08 08:23:06 +08:00
|
|
|
const llvm::SourceMgr &LSM = *D.getSourceMgr();
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
// We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
|
|
|
|
// already owns its one and clang::SourceManager wants to own its one.
|
|
|
|
const MemoryBuffer *LBuf =
|
|
|
|
LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
// Create the copy and transfer ownership to clang::SourceManager.
|
2014-06-27 14:02:00 +08:00
|
|
|
// TODO: Avoid copying files into memory.
|
2014-08-28 04:03:29 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> CBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
|
|
|
|
LBuf->getBufferIdentifier());
|
2014-06-27 14:02:00 +08:00
|
|
|
// FIXME: Keep a file ID map instead of creating new IDs for each location.
|
2014-08-29 15:59:55 +08:00
|
|
|
FileID FID = CSM.createFileID(std::move(CBuf));
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
// Translate the offset into the file.
|
2014-06-27 14:02:00 +08:00
|
|
|
unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
|
2010-04-30 00:29:09 +08:00
|
|
|
SourceLocation NewLoc =
|
2011-09-20 04:40:19 +08:00
|
|
|
CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
|
2010-04-08 08:23:06 +08:00
|
|
|
return FullSourceLoc(NewLoc, CSM);
|
|
|
|
}
|
|
|
|
|
2010-04-07 01:52:14 +08:00
|
|
|
|
2010-04-07 02:38:50 +08:00
|
|
|
/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
|
|
|
|
/// error parsing inline asm. The SMDiagnostic indicates the error relative to
|
2010-04-30 00:29:09 +08:00
|
|
|
/// the temporary memory buffer that the inline asm parser has set up.
|
2010-04-07 02:38:50 +08:00
|
|
|
void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
|
|
|
|
SourceLocation LocCookie) {
|
|
|
|
// There are a couple of different kinds of errors we could get here. First,
|
|
|
|
// we re-format the SMDiagnostic in terms of a clang diagnostic.
|
2012-02-01 14:36:49 +08:00
|
|
|
|
|
|
|
// Strip "error: " off the start of the message string.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Message = D.getMessage();
|
2012-02-01 14:36:49 +08:00
|
|
|
if (Message.startswith("error: "))
|
|
|
|
Message = Message.substr(7);
|
2010-04-07 02:38:50 +08:00
|
|
|
|
2010-06-15 08:03:12 +08:00
|
|
|
// If the SMDiagnostic has an inline asm source location, translate it.
|
2010-04-07 02:38:50 +08:00
|
|
|
FullSourceLoc Loc;
|
2010-04-08 08:23:06 +08:00
|
|
|
if (D.getLoc() != SMLoc())
|
|
|
|
Loc = ConvertBackendLocation(D, Context->getSourceManager());
|
2012-02-01 14:36:49 +08:00
|
|
|
|
2014-06-06 05:23:42 +08:00
|
|
|
unsigned DiagID;
|
|
|
|
switch (D.getKind()) {
|
|
|
|
case llvm::SourceMgr::DK_Error:
|
|
|
|
DiagID = diag::err_fe_inline_asm;
|
|
|
|
break;
|
|
|
|
case llvm::SourceMgr::DK_Warning:
|
|
|
|
DiagID = diag::warn_fe_inline_asm;
|
|
|
|
break;
|
|
|
|
case llvm::SourceMgr::DK_Note:
|
|
|
|
DiagID = diag::note_fe_inline_asm;
|
|
|
|
break;
|
|
|
|
}
|
2010-06-15 08:03:12 +08:00
|
|
|
// If this problem has clang-level source location information, report the
|
2014-06-06 05:23:42 +08:00
|
|
|
// issue in the source with a note showing the instantiated
|
2010-06-15 08:03:12 +08:00
|
|
|
// code.
|
|
|
|
if (LocCookie.isValid()) {
|
2014-06-06 05:23:42 +08:00
|
|
|
Diags.Report(LocCookie, DiagID).AddString(Message);
|
2010-06-15 08:03:12 +08:00
|
|
|
|
2011-10-16 18:48:28 +08:00
|
|
|
if (D.getLoc().isValid()) {
|
|
|
|
DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
|
|
|
|
// Convert the SMDiagnostic ranges into SourceRange and attach them
|
|
|
|
// to the diagnostic.
|
2015-08-02 03:11:36 +08:00
|
|
|
for (const std::pair<unsigned, unsigned> &Range : D.getRanges()) {
|
2011-10-16 18:48:28 +08:00
|
|
|
unsigned Column = D.getColumnNo();
|
|
|
|
B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
|
|
|
|
Loc.getLocWithOffset(Range.second - Column));
|
|
|
|
}
|
|
|
|
}
|
2010-06-15 08:03:12 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-06 05:23:42 +08:00
|
|
|
// Otherwise, report the backend issue as occurring in the generated .s file.
|
|
|
|
// If Loc is invalid, we still need to report the issue, it just gets no
|
2010-06-15 08:03:12 +08:00
|
|
|
// location info.
|
2014-06-06 05:23:42 +08:00
|
|
|
Diags.Report(Loc, DiagID).AddString(Message);
|
2010-04-07 02:38:50 +08:00
|
|
|
}
|
|
|
|
|
2014-02-07 02:30:43 +08:00
|
|
|
#define ComputeDiagID(Severity, GroupName, DiagID) \
|
|
|
|
do { \
|
|
|
|
switch (Severity) { \
|
|
|
|
case llvm::DS_Error: \
|
|
|
|
DiagID = diag::err_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
case llvm::DS_Warning: \
|
|
|
|
DiagID = diag::warn_fe_##GroupName; \
|
|
|
|
break; \
|
2014-02-28 17:11:08 +08:00
|
|
|
case llvm::DS_Remark: \
|
|
|
|
llvm_unreachable("'remark' severity not expected"); \
|
|
|
|
break; \
|
|
|
|
case llvm::DS_Note: \
|
|
|
|
DiagID = diag::note_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define ComputeDiagRemarkID(Severity, GroupName, DiagID) \
|
|
|
|
do { \
|
|
|
|
switch (Severity) { \
|
|
|
|
case llvm::DS_Error: \
|
|
|
|
DiagID = diag::err_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
case llvm::DS_Warning: \
|
|
|
|
DiagID = diag::warn_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
case llvm::DS_Remark: \
|
|
|
|
DiagID = diag::remark_fe_##GroupName; \
|
|
|
|
break; \
|
2014-02-07 02:30:43 +08:00
|
|
|
case llvm::DS_Note: \
|
|
|
|
DiagID = diag::note_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
bool
|
|
|
|
BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) {
|
|
|
|
unsigned DiagID;
|
|
|
|
ComputeDiagID(D.getSeverity(), inline_asm, DiagID);
|
|
|
|
std::string Message = D.getMsgStr().str();
|
|
|
|
|
|
|
|
// If this problem has clang-level source location information, report the
|
2014-02-26 18:21:56 +08:00
|
|
|
// issue as being a problem in the source with a note showing the instantiated
|
2014-02-07 02:30:43 +08:00
|
|
|
// code.
|
|
|
|
SourceLocation LocCookie =
|
|
|
|
SourceLocation::getFromRawEncoding(D.getLocCookie());
|
|
|
|
if (LocCookie.isValid())
|
|
|
|
Diags.Report(LocCookie, DiagID).AddString(Message);
|
|
|
|
else {
|
|
|
|
// Otherwise, report the backend diagnostic as occurring in the generated
|
|
|
|
// .s file.
|
|
|
|
// If Loc is invalid, we still need to report the diagnostic, it just gets
|
|
|
|
// no location info.
|
|
|
|
FullSourceLoc Loc;
|
|
|
|
Diags.Report(Loc, DiagID).AddString(Message);
|
|
|
|
}
|
|
|
|
// We handled all the possible severities.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) {
|
|
|
|
if (D.getSeverity() != llvm::DS_Warning)
|
|
|
|
// For now, the only support we have for StackSize diagnostic is warning.
|
|
|
|
// We do not know how to format other severities.
|
|
|
|
return false;
|
|
|
|
|
2014-06-06 06:10:59 +08:00
|
|
|
if (const Decl *ND = Gen->GetDeclForMangledName(D.getFunction().getName())) {
|
|
|
|
Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()),
|
|
|
|
diag::warn_fe_frame_larger_than)
|
|
|
|
<< D.getStackSize() << Decl::castToDeclContext(ND);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-02-07 02:30:43 +08:00
|
|
|
}
|
|
|
|
|
2014-07-19 03:40:19 +08:00
|
|
|
void BackendConsumer::EmitOptimizationMessage(
|
2014-07-16 08:40:42 +08:00
|
|
|
const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) {
|
2014-07-19 03:40:19 +08:00
|
|
|
// We only support warnings and remarks.
|
|
|
|
assert(D.getSeverity() == llvm::DS_Remark ||
|
|
|
|
D.getSeverity() == llvm::DS_Warning);
|
2014-04-17 00:54:24 +08:00
|
|
|
|
2014-05-30 03:55:06 +08:00
|
|
|
SourceManager &SourceMgr = Context->getSourceManager();
|
|
|
|
FileManager &FileMgr = SourceMgr.getFileManager();
|
|
|
|
StringRef Filename;
|
|
|
|
unsigned Line, Column;
|
2014-06-06 06:11:12 +08:00
|
|
|
SourceLocation DILoc;
|
2015-05-09 04:59:56 +08:00
|
|
|
|
|
|
|
if (D.isLocationAvailable()) {
|
|
|
|
D.getLocation(&Filename, &Line, &Column);
|
|
|
|
const FileEntry *FE = FileMgr.getFile(Filename);
|
|
|
|
if (FE && Line > 0) {
|
|
|
|
// If -gcolumn-info was not used, Column will be 0. This upsets the
|
|
|
|
// source manager, so pass 1 if Column is not set.
|
|
|
|
DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);
|
|
|
|
}
|
2014-04-17 00:54:24 +08:00
|
|
|
}
|
2014-06-06 06:11:12 +08:00
|
|
|
|
|
|
|
// If a location isn't available, try to approximate it using the associated
|
|
|
|
// function definition. We use the definition's right brace to differentiate
|
|
|
|
// from diagnostics that genuinely relate to the function itself.
|
|
|
|
FullSourceLoc Loc(DILoc, SourceMgr);
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName()))
|
|
|
|
Loc = FD->getASTContext().getFullLoc(FD->getBodyRBrace());
|
|
|
|
|
2014-07-19 03:40:19 +08:00
|
|
|
Diags.Report(Loc, DiagID)
|
|
|
|
<< AddFlagValue(D.getPassName() ? D.getPassName() : "")
|
|
|
|
<< D.getMsg().str();
|
2014-05-30 03:55:06 +08:00
|
|
|
|
2015-05-09 04:59:56 +08:00
|
|
|
if (DILoc.isInvalid() && D.isLocationAvailable())
|
2014-05-30 03:55:06 +08:00
|
|
|
// If we were not able to translate the file:line:col information
|
|
|
|
// back to a SourceLocation, at least emit a note stating that
|
|
|
|
// we could not translate this location. This can happen in the
|
|
|
|
// case of #line directives.
|
2014-06-06 06:11:12 +08:00
|
|
|
Diags.Report(Loc, diag::note_fe_backend_optimization_remark_invalid_loc)
|
2014-05-30 03:55:06 +08:00
|
|
|
<< Filename << Line << Column;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::OptimizationRemarkHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationRemark &D) {
|
|
|
|
// Optimization remarks are active only if the -Rpass flag has a regular
|
|
|
|
// expression that matches the name of the pass name in \p D.
|
|
|
|
if (CodeGenOpts.OptimizationRemarkPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName()))
|
2014-07-19 03:40:19 +08:00
|
|
|
EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark);
|
2014-05-30 03:55:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::OptimizationRemarkHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationRemarkMissed &D) {
|
|
|
|
// Missed optimization remarks are active only if the -Rpass-missed
|
|
|
|
// flag has a regular expression that matches the name of the pass
|
|
|
|
// name in \p D.
|
|
|
|
if (CodeGenOpts.OptimizationRemarkMissedPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkMissedPattern->match(D.getPassName()))
|
2014-07-19 03:40:19 +08:00
|
|
|
EmitOptimizationMessage(D,
|
|
|
|
diag::remark_fe_backend_optimization_remark_missed);
|
2014-05-30 03:55:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::OptimizationRemarkHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationRemarkAnalysis &D) {
|
|
|
|
// Optimization analysis remarks are active only if the -Rpass-analysis
|
|
|
|
// flag has a regular expression that matches the name of the pass
|
|
|
|
// name in \p D.
|
|
|
|
if (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName()))
|
2014-07-19 03:40:19 +08:00
|
|
|
EmitOptimizationMessage(
|
2014-05-30 03:55:06 +08:00
|
|
|
D, diag::remark_fe_backend_optimization_remark_analysis);
|
2014-04-17 00:54:24 +08:00
|
|
|
}
|
|
|
|
|
2014-07-19 03:40:19 +08:00
|
|
|
void BackendConsumer::OptimizationFailureHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationFailure &D) {
|
|
|
|
EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure);
|
|
|
|
}
|
|
|
|
|
2014-10-28 07:02:34 +08:00
|
|
|
void BackendConsumer::linkerDiagnosticHandler(const DiagnosticInfo &DI) {
|
2014-10-25 12:06:14 +08:00
|
|
|
if (DI.getSeverity() != DS_Error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string MsgStorage;
|
|
|
|
{
|
|
|
|
raw_string_ostream Stream(MsgStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
|
|
|
DI.print(DP);
|
|
|
|
}
|
|
|
|
|
|
|
|
Diags.Report(diag::err_fe_cannot_link_module)
|
|
|
|
<< LinkModule->getModuleIdentifier() << MsgStorage;
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:30:43 +08:00
|
|
|
/// \brief This function is invoked when the backend needs
|
|
|
|
/// to report something to the user.
|
|
|
|
void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
|
|
|
|
unsigned DiagID = diag::err_fe_inline_asm;
|
|
|
|
llvm::DiagnosticSeverity Severity = DI.getSeverity();
|
|
|
|
// Get the diagnostic ID based.
|
|
|
|
switch (DI.getKind()) {
|
|
|
|
case llvm::DK_InlineAsm:
|
|
|
|
if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI)))
|
|
|
|
return;
|
|
|
|
ComputeDiagID(Severity, inline_asm, DiagID);
|
|
|
|
break;
|
|
|
|
case llvm::DK_StackSize:
|
|
|
|
if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI)))
|
|
|
|
return;
|
|
|
|
ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
|
|
|
|
break;
|
2014-04-17 00:54:24 +08:00
|
|
|
case llvm::DK_OptimizationRemark:
|
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
|
|
|
OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemark>(DI));
|
|
|
|
return;
|
2014-05-30 00:19:27 +08:00
|
|
|
case llvm::DK_OptimizationRemarkMissed:
|
2014-05-30 03:55:06 +08:00
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
|
|
|
OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemarkMissed>(DI));
|
|
|
|
return;
|
2014-05-30 00:19:27 +08:00
|
|
|
case llvm::DK_OptimizationRemarkAnalysis:
|
2014-05-30 03:55:06 +08:00
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
|
|
|
OptimizationRemarkHandler(
|
|
|
|
cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI));
|
2014-05-30 00:19:27 +08:00
|
|
|
return;
|
2014-07-19 03:40:19 +08:00
|
|
|
case llvm::DK_OptimizationFailure:
|
|
|
|
// Optimization failures are always handled completely by this
|
|
|
|
// handler.
|
|
|
|
OptimizationFailureHandler(cast<DiagnosticInfoOptimizationFailure>(DI));
|
|
|
|
return;
|
2014-02-07 02:30:43 +08:00
|
|
|
default:
|
|
|
|
// Plugin IDs are not bound to any value as they are set dynamically.
|
2014-02-28 17:11:08 +08:00
|
|
|
ComputeDiagRemarkID(Severity, backend_plugin, DiagID);
|
2014-02-07 02:30:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::string MsgStorage;
|
|
|
|
{
|
|
|
|
raw_string_ostream Stream(MsgStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
|
|
|
DI.print(DP);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report the backend message using the usual diagnostic mechanism.
|
|
|
|
FullSourceLoc Loc;
|
|
|
|
Diags.Report(Loc, DiagID).AddString(MsgStorage);
|
|
|
|
}
|
|
|
|
#undef ComputeDiagID
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-02-20 07:03:58 +08:00
|
|
|
CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
|
2014-05-21 13:09:00 +08:00
|
|
|
: Act(_Act), LinkModule(nullptr),
|
2011-10-31 01:30:44 +08:00
|
|
|
VMContext(_VMContext ? _VMContext : new LLVMContext),
|
2011-02-20 07:03:58 +08:00
|
|
|
OwnsVMContext(!_VMContext) {}
|
|
|
|
|
|
|
|
CodeGenAction::~CodeGenAction() {
|
|
|
|
TheModule.reset();
|
|
|
|
if (OwnsVMContext)
|
|
|
|
delete VMContext;
|
|
|
|
}
|
2010-02-26 04:37:44 +08:00
|
|
|
|
2010-06-08 07:27:59 +08:00
|
|
|
bool CodeGenAction::hasIRSupport() const { return true; }
|
|
|
|
|
2010-02-25 12:37:50 +08:00
|
|
|
void CodeGenAction::EndSourceFileAction() {
|
|
|
|
// If the consumer creation failed, do nothing.
|
|
|
|
if (!getCompilerInstance().hasASTConsumer())
|
|
|
|
return;
|
|
|
|
|
2011-10-31 01:30:44 +08:00
|
|
|
// If we were given a link module, release consumer's ownership of it.
|
|
|
|
if (LinkModule)
|
|
|
|
BEConsumer->takeLinkModule();
|
|
|
|
|
2010-02-25 12:37:50 +08:00
|
|
|
// Steal the module from the consumer.
|
2014-08-29 13:08:19 +08:00
|
|
|
TheModule = BEConsumer->takeModule();
|
2010-02-25 12:37:50 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 22:36:35 +08:00
|
|
|
std::unique_ptr<llvm::Module> CodeGenAction::takeModule() {
|
|
|
|
return std::move(TheModule);
|
|
|
|
}
|
|
|
|
|
2011-02-20 07:03:58 +08:00
|
|
|
llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
|
|
|
|
OwnsVMContext = false;
|
|
|
|
return VMContext;
|
|
|
|
}
|
|
|
|
|
2015-04-14 23:15:49 +08:00
|
|
|
static raw_pwrite_stream *
|
|
|
|
GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
|
2010-06-08 07:27:59 +08:00
|
|
|
switch (Action) {
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitAssembly:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(false, InFile, "s");
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitLL:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(false, InFile, "ll");
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitBC:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(true, InFile, "bc");
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitNothing:
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2010-05-26 02:41:01 +08:00
|
|
|
case Backend_EmitMCNull:
|
2014-06-04 01:23:34 +08:00
|
|
|
return CI.createNullOutputFile();
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitObj:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(true, InFile, "o");
|
2010-02-25 12:37:45 +08:00
|
|
|
}
|
2010-06-08 07:27:59 +08:00
|
|
|
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Invalid action!");
|
2010-06-08 07:27:59 +08:00
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2010-06-08 07:27:59 +08:00
|
|
|
BackendAction BA = static_cast<BackendAction>(Act);
|
2015-05-13 01:44:15 +08:00
|
|
|
raw_pwrite_stream *OS = GetOutputStream(CI, InFile, BA);
|
2010-02-25 12:37:45 +08:00
|
|
|
if (BA != Backend_EmitNothing && !OS)
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-10-31 01:30:44 +08:00
|
|
|
llvm::Module *LinkModuleToUse = LinkModule;
|
|
|
|
|
|
|
|
// If we were not given a link module, and the user requested that one be
|
|
|
|
// loaded from bitcode, do so now.
|
|
|
|
const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
|
|
|
|
if (!LinkModuleToUse && !LinkBCFile.empty()) {
|
2014-10-27 06:44:13 +08:00
|
|
|
auto BCBuf = CI.getFileManager().getBufferForFile(LinkBCFile);
|
2011-10-31 01:30:44 +08:00
|
|
|
if (!BCBuf) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_cannot_open_file)
|
2014-10-27 06:44:13 +08:00
|
|
|
<< LinkBCFile << BCBuf.getError().message();
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2011-10-31 01:30:44 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:32:44 +08:00
|
|
|
ErrorOr<std::unique_ptr<llvm::Module>> ModuleOrErr =
|
2014-10-27 06:44:13 +08:00
|
|
|
getLazyBitcodeModule(std::move(*BCBuf), *VMContext);
|
2014-06-13 05:36:35 +08:00
|
|
|
if (std::error_code EC = ModuleOrErr.getError()) {
|
2011-10-31 01:30:44 +08:00
|
|
|
CI.getDiagnostics().Report(diag::err_cannot_open_file)
|
2014-01-14 02:31:09 +08:00
|
|
|
<< LinkBCFile << EC.message();
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2011-10-31 01:30:44 +08:00
|
|
|
}
|
2015-06-17 06:32:44 +08:00
|
|
|
LinkModuleToUse = ModuleOrErr.get().release();
|
2011-10-31 01:30:44 +08:00
|
|
|
}
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
CoverageSourceInfo *CoverageInfo = nullptr;
|
|
|
|
// Add the preprocessor callback only when the coverage mapping is generated.
|
|
|
|
if (CI.getCodeGenOpts().CoverageMapping) {
|
|
|
|
CoverageInfo = new CoverageSourceInfo;
|
2014-09-10 12:53:53 +08:00
|
|
|
CI.getPreprocessor().addPPCallbacks(
|
|
|
|
std::unique_ptr<PPCallbacks>(CoverageInfo));
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
2014-08-11 05:06:17 +08:00
|
|
|
std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
|
2015-06-30 10:26:03 +08:00
|
|
|
BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
|
|
|
|
CI.getPreprocessorOpts(), CI.getCodeGenOpts(), CI.getTargetOpts(),
|
2014-08-11 05:06:17 +08:00
|
|
|
CI.getLangOpts(), CI.getFrontendOpts().ShowTimers, InFile,
|
2015-05-13 01:44:15 +08:00
|
|
|
LinkModuleToUse, OS, *VMContext, CoverageInfo));
|
2014-08-11 03:56:51 +08:00
|
|
|
BEConsumer = Result.get();
|
|
|
|
return std::move(Result);
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2015-02-12 10:06:55 +08:00
|
|
|
static void BitcodeInlineAsmDiagHandler(const llvm::SMDiagnostic &SM,
|
|
|
|
void *Context,
|
|
|
|
unsigned LocCookie) {
|
|
|
|
SM.print(nullptr, llvm::errs());
|
|
|
|
}
|
|
|
|
|
2010-06-08 07:27:59 +08:00
|
|
|
void CodeGenAction::ExecuteAction() {
|
|
|
|
// If this is an IR file, we have to treat it specially.
|
|
|
|
if (getCurrentFileKind() == IK_LLVM_IR) {
|
|
|
|
BackendAction BA = static_cast<BackendAction>(Act);
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2015-04-14 23:15:49 +08:00
|
|
|
raw_pwrite_stream *OS = GetOutputStream(CI, getCurrentFile(), BA);
|
2010-06-08 07:27:59 +08:00
|
|
|
if (BA != Backend_EmitNothing && !OS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool Invalid;
|
|
|
|
SourceManager &SM = CI.getSourceManager();
|
2014-06-30 09:33:59 +08:00
|
|
|
FileID FID = SM.getMainFileID();
|
|
|
|
llvm::MemoryBuffer *MainFile = SM.getBuffer(FID, &Invalid);
|
2010-06-08 07:27:59 +08:00
|
|
|
if (Invalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::SMDiagnostic Err;
|
2014-08-27 05:49:29 +08:00
|
|
|
TheModule = parseIR(MainFile->getMemBufferRef(), Err, *VMContext);
|
2010-06-08 07:27:59 +08:00
|
|
|
if (!TheModule) {
|
2014-06-30 09:33:59 +08:00
|
|
|
// Translate from the diagnostic info to the SourceManager location if
|
|
|
|
// available.
|
|
|
|
// TODO: Unify this with ConvertBackendLocation()
|
|
|
|
SourceLocation Loc;
|
|
|
|
if (Err.getLineNo() > 0) {
|
|
|
|
assert(Err.getColumnNo() >= 0);
|
|
|
|
Loc = SM.translateFileLineCol(SM.getFileEntryForID(FID),
|
|
|
|
Err.getLineNo(), Err.getColumnNo() + 1);
|
|
|
|
}
|
2010-06-08 07:27:59 +08:00
|
|
|
|
2013-12-21 13:20:03 +08:00
|
|
|
// Strip off a leading diagnostic code if there is one.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Msg = Err.getMessage();
|
2010-06-08 07:27:59 +08:00
|
|
|
if (Msg.startswith("error: "))
|
|
|
|
Msg = Msg.substr(7);
|
2012-03-17 06:31:42 +08:00
|
|
|
|
2013-12-21 13:20:03 +08:00
|
|
|
unsigned DiagID =
|
|
|
|
CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
|
2010-06-08 07:27:59 +08:00
|
|
|
|
2013-12-21 13:20:03 +08:00
|
|
|
CI.getDiagnostics().Report(Loc, DiagID) << Msg;
|
2010-06-08 07:27:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-12-21 06:01:25 +08:00
|
|
|
const TargetOptions &TargetOpts = CI.getTargetOpts();
|
|
|
|
if (TheModule->getTargetTriple() != TargetOpts.Triple) {
|
2015-01-29 14:25:59 +08:00
|
|
|
CI.getDiagnostics().Report(SourceLocation(),
|
|
|
|
diag::warn_fe_override_module)
|
|
|
|
<< TargetOpts.Triple;
|
2013-12-21 06:01:25 +08:00
|
|
|
TheModule->setTargetTriple(TargetOpts.Triple);
|
|
|
|
}
|
2010-06-08 07:27:59 +08:00
|
|
|
|
2015-02-12 10:06:55 +08:00
|
|
|
LLVMContext &Ctx = TheModule->getContext();
|
|
|
|
Ctx.setInlineAsmDiagnosticHandler(BitcodeInlineAsmDiagHandler);
|
2014-01-02 23:08:04 +08:00
|
|
|
EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts,
|
|
|
|
CI.getLangOpts(), CI.getTarget().getTargetDescription(),
|
|
|
|
TheModule.get(), BA, OS);
|
2010-06-08 07:27:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise follow the normal AST path.
|
|
|
|
this->ASTFrontendAction::ExecuteAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitAssemblyAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitAssembly, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitBCAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitBC, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitLLVMAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitLL, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitLLVMOnlyAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitNothing, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitCodeGenOnlyAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitMCNull, _VMContext) {}
|
2010-05-26 02:41:01 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitObjAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitObj, _VMContext) {}
|