Skip to content

Commit

Permalink
Use diagnostic handler in the LLVMContext
Browse files Browse the repository at this point in the history
This patch converts code that has access to a LLVMContext to not take a
diagnostic handler.

This has a few advantages

* It is easier to use a consistent diagnostic handler in a single program.
* Less clutter since we are not passing a handler around.

It does make it a bit awkward to implement some C APIs that return a
diagnostic string. I will propose new versions of these APIs and
deprecate the current ones.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255571 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Dec 14, 2015
1 parent 265bc7d commit 7a1fc2d
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 192 deletions.
31 changes: 14 additions & 17 deletions include/llvm/Bitcode/ReaderWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,30 @@ namespace llvm {
ErrorOr<std::unique_ptr<Module>>
getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler = nullptr,
bool ShouldLazyLoadMetadata = false);

/// Read the header of the specified stream and prepare for lazy
/// deserialization and streaming of function bodies.
ErrorOr<std::unique_ptr<Module>> getStreamedBitcodeModule(
StringRef Name, std::unique_ptr<DataStreamer> Streamer,
LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler = nullptr);
ErrorOr<std::unique_ptr<Module>>
getStreamedBitcodeModule(StringRef Name,
std::unique_ptr<DataStreamer> Streamer,
LLVMContext &Context);

/// Read the header of the specified bitcode buffer and extract just the
/// triple information. If successful, this returns a string. On error, this
/// returns "".
std::string
getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler = nullptr);
std::string getBitcodeTargetTriple(MemoryBufferRef Buffer,
LLVMContext &Context);

/// Read the header of the specified bitcode buffer and extract just the
/// producer string information. If successful, this returns a string. On
/// error, this returns "".
std::string getBitcodeProducerString(
MemoryBufferRef Buffer, LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler = nullptr);
std::string getBitcodeProducerString(MemoryBufferRef Buffer,
LLVMContext &Context);

/// Read the specified bitcode file, returning the module.
ErrorOr<std::unique_ptr<Module>>
parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler = nullptr);
ErrorOr<std::unique_ptr<Module>> parseBitcodeFile(MemoryBufferRef Buffer,
LLVMContext &Context);

/// Check if the given bitcode buffer contains a function summary block.
bool hasFunctionSummary(MemoryBufferRef Buffer,
Expand All @@ -75,9 +71,10 @@ namespace llvm {
/// the index. Otherwise skip the function summary section, and only create
/// an index object with a map from function name to function summary offset.
/// The index is used to perform lazy function summary reading later.
ErrorOr<std::unique_ptr<FunctionInfoIndex>> getFunctionInfoIndex(
MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
bool IsLazy = false);
ErrorOr<std::unique_ptr<FunctionInfoIndex>>
getFunctionInfoIndex(MemoryBufferRef Buffer,
DiagnosticHandlerFunction DiagnosticHandler,
bool IsLazy = false);

/// This method supports lazy reading of function summary data from the
/// combined index during function importing. When reading the combined index
Expand Down
8 changes: 1 addition & 7 deletions include/llvm/Linker/IRMover.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/IR/DiagnosticInfo.h"

namespace llvm {
class GlobalValue;
Expand Down Expand Up @@ -54,7 +53,7 @@ class IRMover {
bool hasType(StructType *Ty);
};

IRMover(Module &M, DiagnosticHandlerFunction DiagnosticHandler);
IRMover(Module &M);

typedef std::function<void(GlobalValue &)> ValueAdder;
/// Move in the provide values. The source is destroyed.
Expand All @@ -63,14 +62,9 @@ class IRMover {
std::function<void(GlobalValue &GV, ValueAdder Add)> AddLazyFor);
Module &getModule() { return Composite; }

DiagnosticHandlerFunction getDiagnosticHandler() const {
return DiagnosticHandler;
}

private:
Module &Composite;
IdentifiedStructTypeSet IdentifiedStructTypes;
DiagnosticHandlerFunction DiagnosticHandler;
};

} // End llvm namespace
Expand Down
13 changes: 3 additions & 10 deletions include/llvm/Linker/Linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#ifndef LLVM_LINKER_LINKER_H
#define LLVM_LINKER_LINKER_H

#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/FunctionInfo.h"
#include "llvm/Linker/IRMover.h"

Expand All @@ -34,7 +33,7 @@ class Linker {
InternalizeLinkedSymbols = (1 << 2)
};

Linker(Module &M, DiagnosticHandlerFunction DiagnosticHandler);
Linker(Module &M);

/// \brief Link \p Src into the composite. The source is destroyed.
///
Expand All @@ -50,20 +49,14 @@ class Linker {
DenseSet<const GlobalValue *> *FunctionsToImport = nullptr);

static bool linkModules(Module &Dest, Module &Src,
DiagnosticHandlerFunction DiagnosticHandler,
unsigned Flags = Flags::None);

DiagnosticHandlerFunction getDiagnosticHandler() const {
return Mover.getDiagnosticHandler();
}
};

/// Create a new module with exported local functions renamed and promoted
/// for ThinLTO.
std::unique_ptr<Module>
renameModuleForThinLTO(std::unique_ptr<Module> &M,
const FunctionInfoIndex *Index,
DiagnosticHandlerFunction DiagnosticHandler);
std::unique_ptr<Module> renameModuleForThinLTO(std::unique_ptr<Module> &M,
const FunctionInfoIndex *Index);

} // End llvm namespace

Expand Down
8 changes: 1 addition & 7 deletions include/llvm/Transforms/IPO/FunctionImport.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#ifndef LLVM_FUNCTIONIMPORT_H
#define LLVM_FUNCTIONIMPORT_H

#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/ADT/StringMap.h"

namespace llvm {
Expand All @@ -25,20 +24,15 @@ class FunctionImporter {
/// The summaries index used to trigger importing.
const FunctionInfoIndex &Index;

/// Diagnostic will be sent to this handler.
DiagnosticHandlerFunction DiagnosticHandler;

/// Factory function to load a Module for a given identifier
std::function<std::unique_ptr<Module>(StringRef Identifier)> ModuleLoader;

public:
/// Create a Function Importer.
FunctionImporter(
const FunctionInfoIndex &Index,
DiagnosticHandlerFunction DiagnosticHandler,
std::function<std::unique_ptr<Module>(StringRef Identifier)> ModuleLoader)
: Index(Index), DiagnosticHandler(DiagnosticHandler),
ModuleLoader(ModuleLoader) {}
: Index(Index), ModuleLoader(ModuleLoader) {}

/// Import functions in Module \p M based on the summary informations.
bool importFunctions(Module &M);
Expand Down
23 changes: 16 additions & 7 deletions lib/Bitcode/Reader/BitReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,33 @@ LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
OutMessage);
}

static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
auto *Message = reinterpret_cast<std::string *>(C);
raw_string_ostream Stream(*Message);
DiagnosticPrinterRawOStream DP(Stream);
DI.print(DP);
}

LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
LLVMMemoryBufferRef MemBuf,
LLVMModuleRef *OutModule,
char **OutMessage) {
MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
LLVMContext &Ctx = *unwrap(ContextRef);

LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
Ctx.getDiagnosticHandler();
void *OldDiagnosticContext = Ctx.getDiagnosticContext();
std::string Message;
raw_string_ostream Stream(Message);
DiagnosticPrinterRawOStream DP(Stream);
Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);

ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);

Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);

ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(
Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); });
if (ModuleOrErr.getError()) {
if (OutMessage) {
Stream.flush();
if (OutMessage)
*OutMessage = strdup(Message.c_str());
}
*OutModule = wrap((Module*)nullptr);
return 1;
}
Expand Down
Loading

0 comments on commit 7a1fc2d

Please sign in to comment.