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"
|
2016-11-11 13:35:12 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.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"
|
2016-10-11 08:26:09 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.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;
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<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;
|
2016-07-21 14:28:48 +08:00
|
|
|
unsigned LLVMIRGenerationRefCount;
|
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
|
|
|
|
2015-10-28 01:56:59 +08:00
|
|
|
SmallVector<std::pair<unsigned, std::unique_ptr<llvm::Module>>, 4>
|
|
|
|
LinkModules;
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2015-12-15 07:17:07 +08:00
|
|
|
// This is here so that the diagnostic printer knows the module a diagnostic
|
|
|
|
// refers to.
|
|
|
|
llvm::Module *CurLinkModule = nullptr;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
public:
|
2015-10-28 01:56:59 +08:00
|
|
|
BackendConsumer(
|
|
|
|
BackendAction Action, DiagnosticsEngine &Diags,
|
|
|
|
const HeaderSearchOptions &HeaderSearchOpts,
|
|
|
|
const PreprocessorOptions &PPOpts, const CodeGenOptions &CodeGenOpts,
|
|
|
|
const TargetOptions &TargetOpts, const LangOptions &LangOpts,
|
|
|
|
bool TimePasses, const std::string &InFile,
|
|
|
|
const SmallVectorImpl<std::pair<unsigned, llvm::Module *>> &LinkModules,
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS, LLVMContext &C,
|
2015-10-28 01:56:59 +08:00
|
|
|
CoverageSourceInfo *CoverageInfo = nullptr)
|
2015-05-23 06:16:55 +08:00
|
|
|
: Diags(Diags), Action(Action), CodeGenOpts(CodeGenOpts),
|
2016-07-15 08:55:40 +08:00
|
|
|
TargetOpts(TargetOpts), LangOpts(LangOpts),
|
|
|
|
AsmOutStream(std::move(OS)), Context(nullptr),
|
2016-11-19 03:43:25 +08:00
|
|
|
LLVMIRGeneration("irgen", "LLVM IR Generation Time"),
|
2016-07-21 14:28:48 +08:00
|
|
|
LLVMIRGenerationRefCount(0),
|
2015-06-30 10:26:03 +08:00
|
|
|
Gen(CreateLLVMCodeGen(Diags, InFile, HeaderSearchOpts, PPOpts,
|
2015-10-28 01:56:59 +08:00
|
|
|
CodeGenOpts, C, CoverageInfo)) {
|
2009-11-30 16:39:52 +08:00
|
|
|
llvm::TimePassesIsEnabled = TimePasses;
|
2015-10-28 01:56:59 +08:00
|
|
|
for (auto &I : LinkModules)
|
|
|
|
this->LinkModules.push_back(
|
|
|
|
std::make_pair(I.first, std::unique_ptr<llvm::Module>(I.second)));
|
2009-02-18 09:23:44 +08:00
|
|
|
}
|
2016-02-19 00:42:09 +08:00
|
|
|
llvm::Module *getModule() const { return Gen->GetModule(); }
|
|
|
|
std::unique_ptr<llvm::Module> takeModule() {
|
|
|
|
return std::unique_ptr<llvm::Module>(Gen->ReleaseModule());
|
|
|
|
}
|
2015-10-28 01:56:59 +08:00
|
|
|
void releaseLinkModules() {
|
|
|
|
for (auto &I : LinkModules)
|
|
|
|
I.second.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-08-19 04:39:29 +08:00
|
|
|
assert(!Context && "initialized multiple times");
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2016-07-21 14:28:48 +08:00
|
|
|
// Recurse.
|
|
|
|
if (llvm::TimePassesIsEnabled) {
|
|
|
|
LLVMIRGenerationRefCount += 1;
|
|
|
|
if (LLVMIRGenerationRefCount == 1)
|
|
|
|
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
|
|
|
|
2016-07-21 14:28:48 +08:00
|
|
|
if (llvm::TimePassesIsEnabled) {
|
|
|
|
LLVMIRGenerationRefCount -= 1;
|
|
|
|
if (LLVMIRGenerationRefCount == 0)
|
|
|
|
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
|
|
|
|
2016-03-30 14:27:31 +08:00
|
|
|
void HandleInlineFunctionDefinition(FunctionDecl *D) override {
|
2014-05-24 04:37:38 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
|
|
|
|
Context->getSourceManager(),
|
2016-03-30 14:27:31 +08:00
|
|
|
"LLVM IR generation of inline function");
|
2014-05-24 04:37:38 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
|
|
|
LLVMIRGeneration.startTimer();
|
|
|
|
|
2016-03-30 14:27:31 +08:00
|
|
|
Gen->HandleInlineFunctionDefinition(D);
|
2014-05-24 04:37:38 +08:00
|
|
|
|
|
|
|
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");
|
2016-07-21 14:28:48 +08:00
|
|
|
if (llvm::TimePassesIsEnabled) {
|
|
|
|
LLVMIRGenerationRefCount += 1;
|
|
|
|
if (LLVMIRGenerationRefCount == 1)
|
|
|
|
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
|
|
|
|
2016-07-21 14:28:48 +08:00
|
|
|
if (llvm::TimePassesIsEnabled) {
|
|
|
|
LLVMIRGenerationRefCount -= 1;
|
|
|
|
if (LLVMIRGenerationRefCount == 0)
|
|
|
|
LLVMIRGeneration.stopTimer();
|
|
|
|
}
|
2009-03-05 16:00:35 +08:00
|
|
|
}
|
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.
|
2016-02-19 00:42:09 +08:00
|
|
|
if (!getModule())
|
2010-06-08 07:20:08 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Install an inline asm handler so that diagnostics get printed through
|
|
|
|
// our diagnostics hooks.
|
2016-02-19 00:42:09 +08:00
|
|
|
LLVMContext &Ctx = getModule()->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);
|
2016-09-13 12:32:40 +08:00
|
|
|
Ctx.setDiagnosticHotnessRequested(CodeGenOpts.DiagnosticsWithHotness);
|
2014-02-07 02:30:43 +08:00
|
|
|
|
2016-10-11 08:26:09 +08:00
|
|
|
std::unique_ptr<llvm::tool_output_file> OptRecordFile;
|
|
|
|
if (!CodeGenOpts.OptRecordFile.empty()) {
|
|
|
|
std::error_code EC;
|
|
|
|
OptRecordFile =
|
|
|
|
llvm::make_unique<llvm::tool_output_file>(CodeGenOpts.OptRecordFile,
|
|
|
|
EC, sys::fs::F_None);
|
|
|
|
if (EC) {
|
|
|
|
Diags.Report(diag::err_cannot_open_file) <<
|
|
|
|
CodeGenOpts.OptRecordFile << EC.message();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ctx.setDiagnosticsOutputFile(new yaml::Output(OptRecordFile->os()));
|
|
|
|
|
|
|
|
if (CodeGenOpts.getProfileUse() != CodeGenOptions::ProfileNone)
|
|
|
|
Ctx.setDiagnosticHotnessRequested(true);
|
|
|
|
}
|
|
|
|
|
2015-12-15 07:17:07 +08:00
|
|
|
// Link LinkModule into this module if present, preserving its validity.
|
|
|
|
for (auto &I : LinkModules) {
|
|
|
|
unsigned LinkFlags = I.first;
|
|
|
|
CurLinkModule = I.second.get();
|
2016-02-19 00:42:09 +08:00
|
|
|
if (Linker::linkModules(*getModule(), std::move(I.second), LinkFlags))
|
2015-12-15 07:17:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-12 00:26:03 +08:00
|
|
|
EmbedBitcode(getModule(), CodeGenOpts, llvm::MemoryBufferRef());
|
|
|
|
|
2011-07-06 06:02:36 +08:00
|
|
|
EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
|
2016-03-05 03:00:41 +08:00
|
|
|
C.getTargetInfo().getDataLayout(),
|
2016-07-15 08:55:40 +08:00
|
|
|
getModule(), Action, std::move(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);
|
2016-10-11 08:26:09 +08:00
|
|
|
|
|
|
|
if (OptRecordFile)
|
|
|
|
OptRecordFile->keep();
|
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
|
|
|
|
2016-01-27 03:30:26 +08:00
|
|
|
void AssignInheritanceModel(CXXRecordDecl *RD) override {
|
|
|
|
Gen->AssignInheritanceModel(RD);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-02-07 02:30:43 +08:00
|
|
|
static void DiagnosticHandler(const llvm::DiagnosticInfo &DI,
|
|
|
|
void *Context) {
|
|
|
|
((BackendConsumer *)Context)->DiagnosticHandlerImpl(DI);
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:52:52 +08:00
|
|
|
/// Get the best possible source location to represent a diagnostic that
|
|
|
|
/// may have associated debug info.
|
|
|
|
const FullSourceLoc
|
|
|
|
getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithDebugLocBase &D,
|
|
|
|
bool &BadDebugInfo, StringRef &Filename,
|
|
|
|
unsigned &Line, unsigned &Column) const;
|
|
|
|
|
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);
|
2016-02-02 21:52:52 +08:00
|
|
|
/// \brief Specialized handler for unsupported backend feature diagnostic.
|
|
|
|
void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &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);
|
2016-09-28 06:19:29 +08:00
|
|
|
void OptimizationRemarkHandler(const llvm::OptimizationRemark &D);
|
|
|
|
void OptimizationRemarkHandler(const llvm::OptimizationRemarkMissed &D);
|
|
|
|
void OptimizationRemarkHandler(const llvm::OptimizationRemarkAnalysis &D);
|
2014-05-30 03:55:06 +08:00
|
|
|
void OptimizationRemarkHandler(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkAnalysisFPCommute &D);
|
2014-05-30 03:55:06 +08:00
|
|
|
void OptimizationRemarkHandler(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkAnalysisAliasing &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())) {
|
2016-06-21 02:13:09 +08:00
|
|
|
// FIXME: Shouldn't need to truncate to uint32_t
|
2014-06-06 06:10:59 +08:00
|
|
|
Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()),
|
|
|
|
diag::warn_fe_frame_larger_than)
|
2016-06-21 02:13:09 +08:00
|
|
|
<< static_cast<uint32_t>(D.getStackSize()) << Decl::castToDeclContext(ND);
|
2014-06-06 06:10:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-02-07 02:30:43 +08:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:52:52 +08:00
|
|
|
const FullSourceLoc BackendConsumer::getBestLocationFromDebugLoc(
|
|
|
|
const llvm::DiagnosticInfoWithDebugLocBase &D, bool &BadDebugInfo, StringRef &Filename,
|
|
|
|
unsigned &Line, unsigned &Column) const {
|
2014-05-30 03:55:06 +08:00
|
|
|
SourceManager &SourceMgr = Context->getSourceManager();
|
|
|
|
FileManager &FileMgr = SourceMgr.getFileManager();
|
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);
|
|
|
|
}
|
2016-02-02 21:52:52 +08:00
|
|
|
BadDebugInfo = DILoc.isInvalid();
|
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()))
|
2016-02-02 21:52:52 +08:00
|
|
|
Loc = FD->getASTContext().getFullLoc(FD->getLocation());
|
|
|
|
|
|
|
|
if (DILoc.isInvalid() && D.isLocationAvailable())
|
|
|
|
// 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.
|
|
|
|
Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
|
2016-05-06 21:29:46 +08:00
|
|
|
<< Filename << Line << Column;
|
2016-02-02 21:52:52 +08:00
|
|
|
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::UnsupportedDiagHandler(
|
|
|
|
const llvm::DiagnosticInfoUnsupported &D) {
|
|
|
|
// We only support errors.
|
|
|
|
assert(D.getSeverity() == llvm::DS_Error);
|
|
|
|
|
|
|
|
StringRef Filename;
|
|
|
|
unsigned Line, Column;
|
|
|
|
bool BadDebugInfo;
|
|
|
|
FullSourceLoc Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename,
|
|
|
|
Line, Column);
|
|
|
|
|
|
|
|
Diags.Report(Loc, diag::err_fe_backend_unsupported) << D.getMessage().str();
|
|
|
|
|
|
|
|
if (BadDebugInfo)
|
|
|
|
// 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.
|
|
|
|
Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
|
|
|
|
<< Filename << Line << Column;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::EmitOptimizationMessage(
|
|
|
|
const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) {
|
|
|
|
// We only support warnings and remarks.
|
|
|
|
assert(D.getSeverity() == llvm::DS_Remark ||
|
|
|
|
D.getSeverity() == llvm::DS_Warning);
|
|
|
|
|
|
|
|
StringRef Filename;
|
|
|
|
unsigned Line, Column;
|
|
|
|
bool BadDebugInfo = false;
|
|
|
|
FullSourceLoc Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename,
|
|
|
|
Line, Column);
|
2014-06-06 06:11:12 +08:00
|
|
|
|
2016-09-13 12:32:40 +08:00
|
|
|
std::string Msg;
|
|
|
|
raw_string_ostream MsgStream(Msg);
|
2016-09-28 04:55:12 +08:00
|
|
|
MsgStream << D.getMsg();
|
2016-09-13 12:32:40 +08:00
|
|
|
|
|
|
|
if (D.getHotness())
|
|
|
|
MsgStream << " (hotness: " << *D.getHotness() << ")";
|
|
|
|
|
2014-07-19 03:40:19 +08:00
|
|
|
Diags.Report(Loc, DiagID)
|
2016-10-01 10:56:57 +08:00
|
|
|
<< AddFlagValue(D.getPassName())
|
2016-09-13 12:32:40 +08:00
|
|
|
<< MsgStream.str();
|
2014-05-30 03:55:06 +08:00
|
|
|
|
2016-02-02 21:52:52 +08:00
|
|
|
if (BadDebugInfo)
|
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.
|
2016-02-02 21:52:52 +08:00
|
|
|
Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
|
2014-05-30 03:55:06 +08:00
|
|
|
<< Filename << Line << Column;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::OptimizationRemarkHandler(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemark &D) {
|
2014-05-30 03:55:06 +08:00
|
|
|
// 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(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkMissed &D) {
|
2014-05-30 03:55:06 +08:00
|
|
|
// 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(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkAnalysis &D) {
|
2015-08-11 09:10:08 +08:00
|
|
|
// Optimization analysis remarks are active if the pass name is set to
|
|
|
|
// llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
|
|
|
|
// regular expression that matches the name of the pass name in \p D.
|
|
|
|
|
2016-06-29 12:55:31 +08:00
|
|
|
if (D.shouldAlwaysPrint() ||
|
2015-08-11 09:10:08 +08:00
|
|
|
(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
|
|
|
}
|
|
|
|
|
2015-08-11 03:56:40 +08:00
|
|
|
void BackendConsumer::OptimizationRemarkHandler(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkAnalysisFPCommute &D) {
|
2015-08-11 09:10:08 +08:00
|
|
|
// Optimization analysis remarks are active if the pass name is set to
|
|
|
|
// llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
|
|
|
|
// regular expression that matches the name of the pass name in \p D.
|
|
|
|
|
2016-06-29 12:55:31 +08:00
|
|
|
if (D.shouldAlwaysPrint() ||
|
2015-08-11 09:10:08 +08:00
|
|
|
(CodeGenOpts.OptimizationRemarkAnalysisPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
|
2015-08-11 03:56:40 +08:00
|
|
|
EmitOptimizationMessage(
|
|
|
|
D, diag::remark_fe_backend_optimization_remark_analysis_fpcommute);
|
|
|
|
}
|
|
|
|
|
2015-08-11 07:05:16 +08:00
|
|
|
void BackendConsumer::OptimizationRemarkHandler(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkAnalysisAliasing &D) {
|
2015-08-11 09:10:08 +08:00
|
|
|
// Optimization analysis remarks are active if the pass name is set to
|
|
|
|
// llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
|
|
|
|
// regular expression that matches the name of the pass name in \p D.
|
|
|
|
|
2016-06-29 12:55:31 +08:00
|
|
|
if (D.shouldAlwaysPrint() ||
|
2015-08-11 09:10:08 +08:00
|
|
|
(CodeGenOpts.OptimizationRemarkAnalysisPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
|
2015-08-11 07:05:16 +08:00
|
|
|
EmitOptimizationMessage(
|
|
|
|
D, diag::remark_fe_backend_optimization_remark_analysis_aliasing);
|
|
|
|
}
|
|
|
|
|
2014-07-19 03:40:19 +08:00
|
|
|
void BackendConsumer::OptimizationFailureHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationFailure &D) {
|
|
|
|
EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure);
|
|
|
|
}
|
|
|
|
|
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;
|
2015-12-15 07:17:07 +08:00
|
|
|
case DK_Linker:
|
|
|
|
assert(CurLinkModule);
|
|
|
|
// FIXME: stop eating the warnings and notes.
|
|
|
|
if (Severity != DS_Error)
|
|
|
|
return;
|
|
|
|
DiagID = diag::err_fe_cannot_link_module;
|
|
|
|
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.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemark>(DI));
|
2014-04-17 00:54:24 +08:00
|
|
|
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.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemarkMissed>(DI));
|
2014-05-30 03:55:06 +08:00
|
|
|
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.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemarkAnalysis>(DI));
|
2014-05-30 00:19:27 +08:00
|
|
|
return;
|
2015-08-11 03:56:40 +08:00
|
|
|
case llvm::DK_OptimizationRemarkAnalysisFPCommute:
|
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisFPCommute>(DI));
|
2015-08-11 03:56:40 +08:00
|
|
|
return;
|
2015-08-11 07:05:16 +08:00
|
|
|
case llvm::DK_OptimizationRemarkAnalysisAliasing:
|
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisAliasing>(DI));
|
2015-08-11 07:05:16 +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;
|
2016-02-02 21:52:52 +08:00
|
|
|
case llvm::DK_Unsupported:
|
|
|
|
UnsupportedDiagHandler(cast<DiagnosticInfoUnsupported>(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);
|
|
|
|
}
|
|
|
|
|
2015-12-15 07:17:07 +08:00
|
|
|
if (DiagID == diag::err_fe_cannot_link_module) {
|
|
|
|
Diags.Report(diag::err_fe_cannot_link_module)
|
|
|
|
<< CurLinkModule->getModuleIdentifier() << MsgStorage;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:30:43 +08:00
|
|
|
// 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)
|
2015-10-28 01:56:59 +08:00
|
|
|
: Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext),
|
2016-03-12 09:47:11 +08:00
|
|
|
OwnsVMContext(!_VMContext) {}
|
2011-02-20 07:03:58 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-10-28 01:56:59 +08:00
|
|
|
// Take back ownership of link modules we passed to consumer.
|
|
|
|
if (!LinkModules.empty())
|
|
|
|
BEConsumer->releaseLinkModules();
|
2011-10-31 01:30:44 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
static std::unique_ptr<raw_pwrite_stream>
|
2015-04-14 23:15:49 +08:00
|
|
|
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);
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<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
|
|
|
|
2015-10-28 01:56:59 +08:00
|
|
|
// Load bitcode modules to link with, if we need to.
|
|
|
|
if (LinkModules.empty())
|
|
|
|
for (auto &I : CI.getCodeGenOpts().LinkBitcodeFiles) {
|
|
|
|
const std::string &LinkBCFile = I.second;
|
|
|
|
|
|
|
|
auto BCBuf = CI.getFileManager().getBufferForFile(LinkBCFile);
|
|
|
|
if (!BCBuf) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_cannot_open_file)
|
|
|
|
<< LinkBCFile << BCBuf.getError().message();
|
|
|
|
LinkModules.clear();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2011-10-31 01:30:44 +08:00
|
|
|
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<llvm::Module>> ModuleOrErr =
|
2016-11-08 14:03:43 +08:00
|
|
|
getOwningLazyBitcodeModule(std::move(*BCBuf), *VMContext);
|
2016-11-13 15:00:17 +08:00
|
|
|
if (!ModuleOrErr) {
|
|
|
|
handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_cannot_open_file)
|
|
|
|
<< LinkBCFile << EIB.message();
|
|
|
|
});
|
2015-10-28 01:56:59 +08:00
|
|
|
LinkModules.clear();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
addLinkModule(ModuleOrErr.get().release(), I.first);
|
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
|
|
|
}
|
2015-10-28 01:56:59 +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(),
|
2015-10-28 01:56:59 +08:00
|
|
|
CI.getLangOpts(), CI.getFrontendOpts().ShowTimers, InFile, LinkModules,
|
2016-07-15 08:55:40 +08:00
|
|
|
std::move(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
|
|
|
|
2016-04-07 03:58:07 +08:00
|
|
|
static void BitcodeInlineAsmDiagHandler(const llvm::SMDiagnostic &SM,
|
|
|
|
void *Context,
|
|
|
|
unsigned LocCookie) {
|
|
|
|
SM.print(nullptr, llvm::errs());
|
|
|
|
|
|
|
|
auto Diags = static_cast<DiagnosticsEngine *>(Context);
|
|
|
|
unsigned DiagID;
|
|
|
|
switch (SM.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Diags->Report(DiagID).AddString("cannot compile inline asm");
|
|
|
|
}
|
|
|
|
|
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();
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<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;
|
|
|
|
|
2016-04-20 10:23:52 +08:00
|
|
|
// For ThinLTO backend invocations, ensure that the context
|
|
|
|
// merges types based on ODR identifiers.
|
|
|
|
if (!CI.getCodeGenOpts().ThinLTOIndexFile.empty())
|
|
|
|
VMContext->enableDebugTypeODRUniquing();
|
|
|
|
|
2010-06-08 07:27:59 +08:00
|
|
|
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
|
|
|
|
2016-05-12 00:26:03 +08:00
|
|
|
EmbedBitcode(TheModule.get(), CI.getCodeGenOpts(),
|
|
|
|
MainFile->getMemBufferRef());
|
|
|
|
|
2016-04-07 03:58:07 +08:00
|
|
|
LLVMContext &Ctx = TheModule->getContext();
|
|
|
|
Ctx.setInlineAsmDiagnosticHandler(BitcodeInlineAsmDiagHandler,
|
|
|
|
&CI.getDiagnostics());
|
2016-05-12 00:26:03 +08:00
|
|
|
|
2014-01-02 23:08:04 +08:00
|
|
|
EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts,
|
2016-03-05 03:00:41 +08:00
|
|
|
CI.getLangOpts(), CI.getTarget().getDataLayout(),
|
2016-07-15 08:55:40 +08:00
|
|
|
TheModule.get(), BA, std::move(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) {}
|