xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/DiagnosticHandler.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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   bool HasErrors = false;
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