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