summaryrefslogtreecommitdiffstats
path: root/llvm/tools/gold/gold-plugin.cpp
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2015-01-10 00:07:30 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2015-01-10 00:07:30 +0000
commitd0b23bef6fe50041d83a9fa64871b8433a2102bd (patch)
tree7f3f32a76b349b8d5618ae78e1955c692736e330 /llvm/tools/gold/gold-plugin.cpp
parent9cdf582f72ba6f59d2112a277326b229b43f536f (diff)
downloadbcm5719-llvm-d0b23bef6fe50041d83a9fa64871b8433a2102bd.tar.gz
bcm5719-llvm-d0b23bef6fe50041d83a9fa64871b8433a2102bd.zip
Use the DiagnosticHandler to print diagnostics when reading bitcode.
The bitcode reading interface used std::error_code to report an error to the callers and it is the callers job to print diagnostics. This is not ideal for error handling or diagnostic reporting: * For error handling, all that the callers care about is 3 possibilities: * It worked * The bitcode file is corrupted/invalid. * The file is not bitcode at all. * For diagnostic, it is user friendly to include far more information about the invalid case so the user can find out what is wrong with the bitcode file. This comes up, for example, when a developer introduces a bug while extending the format. The compromise we had was to have a lot of error codes. With this patch we use the DiagnosticHandler to communicate with the human and std::error_code to communicate with the caller. This allows us to have far fewer error codes and adds the infrastructure to print better diagnostics. This is so because the diagnostics are printed when he issue is found. The code that detected the problem in alive in the stack and can pass down as much context as needed. As an example the patch updates test/Bitcode/invalid.ll. Using a DiagnosticHandler also moves the fatal/non-fatal error decision to the caller. A simple one like llvm-dis can just use fatal errors. The gold plugin needs a bit more complex treatment because of being passed non-bitcode files. An hypothetical interactive tool would make all bitcode errors non-fatal. llvm-svn: 225562
Diffstat (limited to 'llvm/tools/gold/gold-plugin.cpp')
-rw-r--r--llvm/tools/gold/gold-plugin.cpp23
1 files changed, 21 insertions, 2 deletions
diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
index 6b5864457a4..1ba08cc53dc 100644
--- a/llvm/tools/gold/gold-plugin.cpp
+++ b/llvm/tools/gold/gold-plugin.cpp
@@ -19,6 +19,8 @@
#include "llvm/CodeGen/Analysis.h"
#include "llvm/CodeGen/CommandFlags.h"
#include "llvm/IR/Constants.h"
+#include "llvm/IR/DiagnosticInfo.h"
+#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
@@ -269,6 +271,23 @@ static bool shouldSkip(uint32_t Symflags) {
return false;
}
+static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
+ assert(DI.getSeverity() == DS_Error && "Only expecting errors");
+ const auto &BDI = cast<BitcodeDiagnosticInfo>(DI);
+ std::error_code EC = BDI.getError();
+ if (EC == BitcodeError::InvalidBitcodeSignature)
+ return;
+
+ std::string ErrStorage;
+ {
+ raw_string_ostream OS(ErrStorage);
+ DiagnosticPrinterRawOStream DP(OS);
+ DI.print(DP);
+ }
+ message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
+ ErrStorage.c_str());
+}
+
/// Called by gold to see whether this file is one that our plugin can handle.
/// We'll try to open it and register all the symbols with add_symbol if
/// possible.
@@ -302,11 +321,11 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
BufferRef = Buffer->getMemBufferRef();
}
+ Context.setDiagnosticHandler(diagnosticHandler);
ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
object::IRObjectFile::create(BufferRef, Context);
std::error_code EC = ObjOrErr.getError();
- if (EC == BitcodeError::InvalidBitcodeSignature ||
- EC == object::object_error::invalid_file_type ||
+ if (EC == object::object_error::invalid_file_type ||
EC == object::object_error::bitcode_section_not_found)
return LDPS_OK;
OpenPOWER on IntegriCloud