* @{
*/
-#define LTO_API_VERSION 6
+#define LTO_API_VERSION 7
typedef enum {
LTO_SYMBOL_ALIGNMENT_MASK = 0x0000001F, /* log2 of alignment */
extern lto_symbol_attributes
lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
+/**
+ * Diagnostic severity.
+ */
+typedef enum {
+ LTO_DS_ERROR,
+ LTO_DS_WARNING,
+ LTO_DS_NOTE
+} lto_codegen_diagnostic_severity_t;
+
+/**
+ * Diagnostic handler type.
+ * \p severity defines the severity.
+ * \p diag is the actual diagnostic.
+ * The diagnostic is not prefixed by any of severity keyword, e.g., 'error: '.
+ * \p ctxt is used to pass the context set with the diagnostic handler.
+ */
+typedef void (*lto_diagnostic_handler_t)(
+ lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt);
+
+/**
+ * Set a diagnostic handler and the related context (void *).
+ * This is more general than lto_get_error_message, as the diagnostic handler
+ * can be called at anytime within lto.
+ */
+extern void lto_codegen_set_diagnostic_handler(lto_code_gen_t,
+ lto_diagnostic_handler_t,
+ void *);
/**
* Instantiates a code generator.
namespace llvm {
class LLVMContext;
+ class DiagnosticInfo;
class GlobalValue;
class Mangler;
class MemoryBuffer;
bool disableGVNLoadPRE,
std::string &errMsg);
+ void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
+
bool shouldInternalize() const {
return InternalizeStrategy != LTO_INTERNALIZE_NONE;
}
llvm::Mangler &Mangler);
bool determineTarget(std::string &errMsg);
+ static void DiagnosticHandler(const llvm::DiagnosticInfo &DI, void *Context);
+
+ void DiagnosticHandler2(const llvm::DiagnosticInfo &DI);
+
typedef llvm::StringMap<uint8_t> StringSet;
llvm::LLVMContext &Context;
std::string MCpu;
std::string NativeObjectPath;
llvm::TargetOptions Options;
+ lto_diagnostic_handler_t DiagHandler;
+ void *DiagContext;
};
#endif // LTO_CODE_GENERATOR_H
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/DiagnosticInfo.h"
+#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
: Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)),
TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false),
CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
- InternalizeStrategy(LTO_INTERNALIZE_FULL), NativeObjectFile(NULL) {
+ InternalizeStrategy(LTO_INTERNALIZE_FULL), NativeObjectFile(NULL),
+ DiagHandler(NULL), DiagContext(NULL) {
initializeLTOPasses();
}
cl::ParseCommandLineOptions(CodegenOptions.size(),
const_cast<char **>(&CodegenOptions[0]));
}
+
+void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
+ void *Context) {
+ ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
+}
+
+void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
+ // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
+ lto_codegen_diagnostic_severity_t Severity;
+ switch (DI.getSeverity()) {
+ case DS_Error:
+ Severity = LTO_DS_ERROR;
+ break;
+ case DS_Warning:
+ Severity = LTO_DS_WARNING;
+ break;
+ case DS_Note:
+ Severity = LTO_DS_NOTE;
+ break;
+ }
+ // Create the string that will be reported to the external diagnostic handler.
+ std::string MsgStorage;
+ raw_string_ostream Stream(MsgStorage);
+ DiagnosticPrinterRawOStream DP(Stream);
+ DI.print(DP);
+ Stream.flush();
+
+ // If this method has been called it means someone has set up an external
+ // diagnostic handler. Assert on that.
+ assert(DiagHandler && "Invalid diagnostic handler");
+ (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
+}
+
+void
+LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
+ void *Ctxt) {
+ this->DiagHandler = DiagHandler;
+ this->DiagContext = Ctxt;
+ if (!DiagHandler)
+ return Context.setDiagnosticHandler(NULL, NULL);
+ // Register the LTOCodeGenerator stub in the LLVMContext to forward the
+ // diagnostic to the external DiagHandler.
+ Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this);
+}