llvm.org GIT mirror llvm / 4c831d9
[LTO] Add a hook to map LLVM diagnostics into the clients of LTO. Add a hook in the C API of LTO so that clients of the code generator can set their own handler for the LLVM diagnostics. The handler is defined like this: typedef void (*lto_diagnostic_handler_t)(lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt) - severity says how bad this is. - diag is a string that contains the diagnostic message. - ctxt is the registered context for this handler. This hook is more general than the lto_get_error_message, since this function keeps only the latest message and can only be queried when something went wrong (no warning for instance). <rdar://problem/15517596> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199338 91177308-0d34-0410-b5e6-96231b3b80d8 Quentin Colombet 6 years ago
5 changed file(s) with 94 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
4545
4646 namespace llvm {
4747 class LLVMContext;
48 class DiagnosticInfo;
4849 class GlobalValue;
4950 class Mangler;
5051 class MemoryBuffer;
114115 bool disableGVNLoadPRE,
115116 std::string &errMsg);
116117
118 void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
119
117120 bool shouldInternalize() const {
118121 return InternalizeStrategy != LTO_INTERNALIZE_NONE;
119122 }
138141 llvm::Mangler &Mangler);
139142 bool determineTarget(std::string &errMsg);
140143
144 static void DiagnosticHandler(const llvm::DiagnosticInfo &DI, void *Context);
145
146 void DiagnosticHandler2(const llvm::DiagnosticInfo &DI);
147
141148 typedef llvm::StringMap StringSet;
142149
143150 llvm::LLVMContext &Context;
154161 std::string MCpu;
155162 std::string NativeObjectPath;
156163 llvm::TargetOptions Options;
164 lto_diagnostic_handler_t DiagHandler;
165 void *DiagContext;
157166 };
158167
159168 #endif // LTO_CODE_GENERATOR_H
3939 * @{
4040 */
4141
42 #define LTO_API_VERSION 6
42 #define LTO_API_VERSION 7
4343
4444 typedef enum {
4545 LTO_SYMBOL_ALIGNMENT_MASK = 0x0000001F, /* log2 of alignment */
203203 extern lto_symbol_attributes
204204 lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
205205
206 /**
207 * Diagnostic severity.
208 */
209 typedef enum {
210 LTO_DS_ERROR,
211 LTO_DS_WARNING,
212 LTO_DS_NOTE
213 } lto_codegen_diagnostic_severity_t;
214
215 /**
216 * Diagnostic handler type.
217 * \p severity defines the severity.
218 * \p diag is the actual diagnostic.
219 * The diagnostic is not prefixed by any of severity keyword, e.g., 'error: '.
220 * \p ctxt is used to pass the context set with the diagnostic handler.
221 */
222 typedef void (*lto_diagnostic_handler_t)(
223 lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt);
224
225 /**
226 * Set a diagnostic handler and the related context (void *).
227 * This is more general than lto_get_error_message, as the diagnostic handler
228 * can be called at anytime within lto.
229 */
230 extern void lto_codegen_set_diagnostic_handler(lto_code_gen_t,
231 lto_diagnostic_handler_t,
232 void *);
206233
207234 /**
208235 * Instantiates a code generator.
2020 #include "llvm/IR/Constants.h"
2121 #include "llvm/IR/DataLayout.h"
2222 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/DiagnosticPrinter.h"
2325 #include "llvm/IR/LLVMContext.h"
2426 #include "llvm/IR/Mangler.h"
2527 #include "llvm/IR/Module.h"
3638 #include "llvm/Support/FormattedStream.h"
3739 #include "llvm/Support/Host.h"
3840 #include "llvm/Support/MemoryBuffer.h"
41 #include "llvm/Support/raw_ostream.h"
3942 #include "llvm/Support/Signals.h"
4043 #include "llvm/Support/TargetRegistry.h"
4144 #include "llvm/Support/TargetSelect.h"
6265 : Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)),
6366 TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false),
6467 CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
65 InternalizeStrategy(LTO_INTERNALIZE_FULL), NativeObjectFile(NULL) {
68 InternalizeStrategy(LTO_INTERNALIZE_FULL), NativeObjectFile(NULL),
69 DiagHandler(NULL), DiagContext(NULL) {
6670 initializeLTOPasses();
6771 }
6872
535539 cl::ParseCommandLineOptions(CodegenOptions.size(),
536540 const_cast(&CodegenOptions[0]));
537541 }
542
543 void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
544 void *Context) {
545 ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
546 }
547
548 void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
549 // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
550 lto_codegen_diagnostic_severity_t Severity;
551 switch (DI.getSeverity()) {
552 case DS_Error:
553 Severity = LTO_DS_ERROR;
554 break;
555 case DS_Warning:
556 Severity = LTO_DS_WARNING;
557 break;
558 case DS_Note:
559 Severity = LTO_DS_NOTE;
560 break;
561 }
562 // Create the string that will be reported to the external diagnostic handler.
563 std::string MsgStorage;
564 raw_string_ostream Stream(MsgStorage);
565 DiagnosticPrinterRawOStream DP(Stream);
566 DI.print(DP);
567 Stream.flush();
568
569 // If this method has been called it means someone has set up an external
570 // diagnostic handler. Assert on that.
571 assert(DiagHandler && "Invalid diagnostic handler");
572 (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
573 }
574
575 void
576 LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
577 void *Ctxt) {
578 this->DiagHandler = DiagHandler;
579 this->DiagContext = Ctxt;
580 if (!DiagHandler)
581 return Context.setDiagnosticHandler(NULL, NULL);
582 // Register the LTOCodeGenerator stub in the LLVMContext to forward the
583 // diagnostic to the external DiagHandler.
584 Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this);
585 }
192192 return mod->getSymbolAttributes(index);
193193 }
194194
195 /// Set a diagnostic handler.
196 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
197 lto_diagnostic_handler_t diag_handler,
198 void *ctxt) {
199 cg->setDiagnosticHandler(diag_handler, ctxt);
200 }
201
195202 /// lto_codegen_create - Instantiates a code generator. Returns NULL if there
196203 /// is an error.
197204 lto_code_gen_t lto_codegen_create(void) {
1414 lto_module_is_object_file_in_memory
1515 lto_module_is_object_file_in_memory_for_target
1616 lto_module_dispose
17 lto_codegen_set_diagnostic_handler
1718 lto_codegen_add_module
1819 lto_codegen_add_must_preserve_symbol
1920 lto_codegen_compile