]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/RemarkStreamer.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / IR / RemarkStreamer.h
1 //===- llvm/IR/RemarkStreamer.h - Remark Streamer ---------------*- 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 //
9 // This file declares the main interface for outputting remarks.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_IR_REMARKSTREAMER_H
14 #define LLVM_IR_REMARKSTREAMER_H
15
16 #include "llvm/IR/DiagnosticInfo.h"
17 #include "llvm/Remarks/RemarkSerializer.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/Regex.h"
20 #include "llvm/Support/ToolOutputFile.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <string>
23 #include <vector>
24
25 namespace llvm {
26 /// Streamer for remarks.
27 class RemarkStreamer {
28   /// The filename that the remark diagnostics are emitted to.
29   const std::string Filename;
30   /// The regex used to filter remarks based on the passes that emit them.
31   Optional<Regex> PassFilter;
32   /// The object used to serialize the remarks to a specific format.
33   std::unique_ptr<remarks::Serializer> Serializer;
34
35   /// Convert diagnostics into remark objects.
36   /// The lifetime of the members of the result is bound to the lifetime of
37   /// the LLVM diagnostics.
38   remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag);
39
40 public:
41   RemarkStreamer(StringRef Filename,
42                  std::unique_ptr<remarks::Serializer> Serializer);
43   /// Return the filename that the remark diagnostics are emitted to.
44   StringRef getFilename() const { return Filename; }
45   /// Return stream that the remark diagnostics are emitted to.
46   raw_ostream &getStream() { return Serializer->OS; }
47   /// Return the serializer used for this stream.
48   remarks::Serializer &getSerializer() { return *Serializer; }
49   /// Set a pass filter based on a regex \p Filter.
50   /// Returns an error if the regex is invalid.
51   Error setFilter(StringRef Filter);
52   /// Emit a diagnostic through the streamer.
53   void emit(const DiagnosticInfoOptimizationBase &Diag);
54 };
55
56 template <typename ThisError>
57 struct RemarkSetupErrorInfo : public ErrorInfo<ThisError> {
58   std::string Msg;
59   std::error_code EC;
60
61   RemarkSetupErrorInfo(Error E) {
62     handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
63       Msg = EIB.message();
64       EC = EIB.convertToErrorCode();
65     });
66   }
67
68   void log(raw_ostream &OS) const override { OS << Msg; }
69   std::error_code convertToErrorCode() const override { return EC; }
70 };
71
72 struct RemarkSetupFileError : RemarkSetupErrorInfo<RemarkSetupFileError> {
73   static char ID;
74   using RemarkSetupErrorInfo<RemarkSetupFileError>::RemarkSetupErrorInfo;
75 };
76
77 struct RemarkSetupPatternError : RemarkSetupErrorInfo<RemarkSetupPatternError> {
78   static char ID;
79   using RemarkSetupErrorInfo<RemarkSetupPatternError>::RemarkSetupErrorInfo;
80 };
81
82 struct RemarkSetupFormatError : RemarkSetupErrorInfo<RemarkSetupFormatError> {
83   static char ID;
84   using RemarkSetupErrorInfo<RemarkSetupFormatError>::RemarkSetupErrorInfo;
85 };
86
87 /// Setup optimization remarks.
88 Expected<std::unique_ptr<ToolOutputFile>>
89 setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
90                          StringRef RemarksPasses, StringRef RemarksFormat,
91                          bool RemarksWithHotness,
92                          unsigned RemarksHotnessThreshold = 0);
93
94 } // end namespace llvm
95
96 #endif // LLVM_IR_REMARKSTREAMER_H