MLIR  20.0.0git
Diagnostics.h
Go to the documentation of this file.
1 //===--- Diagnostics.h - Helper class for error diagnostics -----*- 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 // Diagnostics class to manage error messages. Implementation shares similarity
10 // to clang-query Diagnostics.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TOOLS_MLIRQUERY_MATCHER_DIAGNOSTICS_H
15 #define MLIR_TOOLS_MLIRQUERY_MATCHER_DIAGNOSTICS_H
16 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <string>
23 #include <vector>
24 
26 
27 // Diagnostics class to manage error messages.
28 class Diagnostics {
29 public:
30  // Helper stream class for constructing error messages.
31  class ArgStream {
32  public:
33  ArgStream(std::vector<std::string> *out) : out(out) {}
34  template <class T>
35  ArgStream &operator<<(const T &arg) {
36  return operator<<(llvm::Twine(arg));
37  }
38  ArgStream &operator<<(const llvm::Twine &arg);
39 
40  private:
41  std::vector<std::string> *out;
42  };
43 
44  // Add an error message with the specified range and error type.
45  // Returns an ArgStream object to allow constructing the error message using
46  // the << operator.
47  ArgStream addError(SourceRange range, ErrorType error);
48 
49  // Print all error messages to the specified output stream.
50  void print(llvm::raw_ostream &os) const;
51 
52 private:
53  // Information stored for one frame of the context.
54  struct ContextFrame {
55  SourceRange range;
56  std::vector<std::string> args;
57  };
58 
59  // Information stored for each error found.
60  struct ErrorContent {
61  std::vector<ContextFrame> contextStack;
62  struct Message {
65  std::vector<std::string> args;
66  };
67  std::vector<Message> messages;
68  };
69 
70  void printMessage(const ErrorContent::Message &message,
71  const llvm::Twine Prefix, llvm::raw_ostream &os) const;
72 
73  void printErrorContent(const ErrorContent &content,
74  llvm::raw_ostream &os) const;
75 
76  std::vector<ContextFrame> contextStack;
77  std::vector<ErrorContent> errorValues;
78 };
79 
80 } // namespace mlir::query::matcher::internal
81 
82 #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_DIAGNOSTICS_H
ArgStream(std::vector< std::string > *out)
Definition: Diagnostics.h:33
ArgStream addError(SourceRange range, ErrorType error)
Definition: Diagnostics.cpp:20
void print(llvm::raw_ostream &os) const