• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- DiagnosticHandler.h - DiagnosticHandler class for LLVM -*- C++ ---*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // Base DiagnosticHandler class declaration. Derive from this class to provide
10 // custom diagnostic reporting.
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_DIAGNOSTICHANDLER_H
14 #define LLVM_IR_DIAGNOSTICHANDLER_H
15 
16 #include "llvm/ADT/StringRef.h"
17 
18 namespace llvm {
19 class DiagnosticInfo;
20 
21 /// This is the base class for diagnostic handling in LLVM.
22 /// The handleDiagnostics method must be overriden by the subclasses to handle
23 /// diagnostic. The *RemarkEnabled methods can be overriden to control
24 /// which remarks are enabled.
25 struct DiagnosticHandler {
26   void *DiagnosticContext = nullptr;
27   DiagnosticHandler(void *DiagContext = nullptr)
DiagnosticContextDiagnosticHandler28       : DiagnosticContext(DiagContext) {}
29   virtual ~DiagnosticHandler() = default;
30 
31   using DiagnosticHandlerTy = void (*)(const DiagnosticInfo &DI, void *Context);
32 
33   /// DiagHandlerCallback is settable from the C API and base implementation
34   /// of DiagnosticHandler will call it from handleDiagnostics(). Any derived
35   /// class of DiagnosticHandler should not use callback but
36   /// implement handleDiagnostics().
37   DiagnosticHandlerTy DiagHandlerCallback = nullptr;
38 
39   /// Override handleDiagnostics to provide custom implementation.
40   /// Return true if it handles diagnostics reporting properly otherwise
41   /// return false to make LLVMContext::diagnose() to print the message
42   /// with a prefix based on the severity.
handleDiagnosticsDiagnosticHandler43   virtual bool handleDiagnostics(const DiagnosticInfo &DI) {
44     if (DiagHandlerCallback) {
45       DiagHandlerCallback(DI, DiagnosticContext);
46       return true;
47     }
48     return false;
49   }
50 
51   /// Return true if analysis remarks are enabled, override
52   /// to provide different implementation.
53   virtual bool isAnalysisRemarkEnabled(StringRef PassName) const;
54 
55   /// Return true if missed optimization remarks are enabled, override
56   /// to provide different implementation.
57   virtual bool isMissedOptRemarkEnabled(StringRef PassName) const;
58 
59   /// Return true if passed optimization remarks are enabled, override
60   /// to provide different implementation.
61   virtual bool isPassedOptRemarkEnabled(StringRef PassName) const;
62 
63   /// Return true if any type of remarks are enabled for this pass.
isAnyRemarkEnabledDiagnosticHandler64   bool isAnyRemarkEnabled(StringRef PassName) const {
65     return (isMissedOptRemarkEnabled(PassName) ||
66             isPassedOptRemarkEnabled(PassName) ||
67             isAnalysisRemarkEnabled(PassName));
68   }
69 
70   /// Return true if any type of remarks are enabled for any pass.
71   virtual bool isAnyRemarkEnabled() const;
72 };
73 } // namespace llvm
74 
75 #endif // LLVM_IR_DIAGNOSTICHANDLER_H
76