clang  14.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 /// \file
10 /// Diagnostics class to manage error messages.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_DIAGNOSTICS_H
15 #define LLVM_CLANG_ASTMATCHERS_DYNAMIC_DIAGNOSTICS_H
16 
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <string>
24 #include <vector>
25 
26 namespace clang {
27 namespace ast_matchers {
28 namespace dynamic {
29 
32  unsigned Line;
33  unsigned Column;
34 };
35 
36 struct SourceRange {
39 };
40 
41 /// A VariantValue instance annotated with its parser context.
42 struct ParserValue {
43  ParserValue() : Text(), Range(), Value() {}
44  StringRef Text;
47 };
48 
49 /// Helper class to manage error messages.
50 class Diagnostics {
51 public:
52  /// Parser context types.
53  enum ContextType {
56  };
57 
58  /// All errors from the system.
59  enum ErrorType {
60  ET_None = 0,
61 
71 
85  };
86 
87  /// Helper stream class.
88  class ArgStream {
89  public:
90  ArgStream(std::vector<std::string> *Out) : Out(Out) {}
91  template <class T> ArgStream &operator<<(const T &Arg) {
92  return operator<<(Twine(Arg));
93  }
94  ArgStream &operator<<(const Twine &Arg);
95 
96  private:
97  std::vector<std::string> *Out;
98  };
99 
100  /// Class defining a parser context.
101  ///
102  /// Used by the parser to specify (possibly recursive) contexts where the
103  /// parsing/construction can fail. Any error triggered within a context will
104  /// keep information about the context chain.
105  /// This class should be used as a RAII instance in the stack.
106  struct Context {
107  public:
108  /// About to call the constructor for a matcher.
110  Context(ConstructMatcherEnum, Diagnostics *Error, StringRef MatcherName,
111  SourceRange MatcherRange);
112  /// About to recurse into parsing one argument for a matcher.
114  Context(MatcherArgEnum, Diagnostics *Error, StringRef MatcherName,
115  SourceRange MatcherRange, unsigned ArgNumber);
116  ~Context();
117 
118  private:
119  Diagnostics *const Error;
120  };
121 
122  /// Context for overloaded matcher construction.
123  ///
124  /// This context will take care of merging all errors that happen within it
125  /// as "candidate" overloads for the same matcher.
127  public:
130 
131  /// Revert all errors that happened within this context.
132  void revertErrors();
133 
134  private:
135  Diagnostics *const Error;
136  unsigned BeginIndex;
137  };
138 
139  /// Add an error to the diagnostics.
140  ///
141  /// All the context information will be kept on the error message.
142  /// \return a helper class to allow the caller to pass the arguments for the
143  /// error message, using the << operator.
144  ArgStream addError(SourceRange Range, ErrorType Error);
145 
146  /// Information stored for one frame of the context.
147  struct ContextFrame {
150  std::vector<std::string> Args;
151  };
152 
153  /// Information stored for each error found.
154  struct ErrorContent {
155  std::vector<ContextFrame> ContextStack;
156  struct Message {
159  std::vector<std::string> Args;
160  };
161  std::vector<Message> Messages;
162  };
163  ArrayRef<ErrorContent> errors() const { return Errors; }
164 
165  /// Returns a simple string representation of each error.
166  ///
167  /// Each error only shows the error message without any context.
168  void printToStream(llvm::raw_ostream &OS) const;
169  std::string toString() const;
170 
171  /// Returns the full string representation of each error.
172  ///
173  /// Each error message contains the full context.
174  void printToStreamFull(llvm::raw_ostream &OS) const;
175  std::string toStringFull() const;
176 
177 private:
178  /// Helper function used by the constructors of ContextFrame.
179  ArgStream pushContextFrame(ContextType Type, SourceRange Range);
180 
181  std::vector<ContextFrame> ContextStack;
182  std::vector<ErrorContent> Errors;
183 };
184 
185 } // namespace dynamic
186 } // namespace ast_matchers
187 } // namespace clang
188 
189 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_DIAGNOSTICS_H
clang::ast_matchers::dynamic::ParserValue
A VariantValue instance annotated with its parser context.
Definition: Diagnostics.h:42
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryMatcherNotFound
@ ET_RegistryMatcherNotFound
Definition: Diagnostics.h:62
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:20
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::ast_matchers::dynamic::Diagnostics::ErrorContent::ContextStack
std::vector< ContextFrame > ContextStack
Definition: Diagnostics.h:155
clang::ast_matchers::dynamic::Diagnostics::CT_MatcherArg
@ CT_MatcherArg
Definition: Diagnostics.h:54
clang::ast_matchers::dynamic::Diagnostics::Context::~Context
~Context()
Definition: Diagnostics.cpp:39
clang::ast_matchers::dynamic::Diagnostics::OverloadContext::OverloadContext
OverloadContext(Diagnostics *Error)
Definition: Diagnostics.cpp:41
clang::ast_matchers::dynamic::Diagnostics::OverloadContext::revertErrors
void revertErrors()
Revert all errors that happened within this context.
Definition: Diagnostics.cpp:55
clang::ast_matchers::dynamic::SourceLocation::SourceLocation
SourceLocation()
Definition: Diagnostics.h:31
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNotAMatcher
@ ET_ParserNotAMatcher
Definition: Diagnostics.h:77
clang::ast_matchers::dynamic::Diagnostics::OverloadContext::~OverloadContext
~OverloadContext()
Definition: Diagnostics.cpp:44
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryMatcherNoWithSupport
@ ET_RegistryMatcherNoWithSupport
Definition: Diagnostics.h:70
clang::ast_matchers::dynamic::Diagnostics::Context::MatcherArg
@ MatcherArg
Definition: Diagnostics.h:113
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryValueNotFound
@ ET_RegistryValueNotFound
Definition: Diagnostics.h:67
clang::ast_matchers::dynamic::Diagnostics::Context
Class defining a parser context.
Definition: Diagnostics.h:106
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
clang::ast_matchers::dynamic::Diagnostics::ET_ParserMalformedChainedExpr
@ ET_ParserMalformedChainedExpr
Definition: Diagnostics.h:83
clang::ast_matchers::dynamic::Diagnostics::ErrorType
ErrorType
All errors from the system.
Definition: Diagnostics.h:59
clang::ast_matchers::dynamic::Diagnostics::toStringFull
std::string toStringFull() const
Definition: Diagnostics.cpp:222
clang::ast_matchers::dynamic::ParserValue::Range
SourceRange Range
Definition: Diagnostics.h:45
clang::ast_matchers::dynamic::Diagnostics::ArgStream
Helper stream class.
Definition: Diagnostics.h:88
clang::ast_matchers::dynamic::Diagnostics::toString
std::string toString() const
Definition: Diagnostics.cpp:203
clang::ast_matchers::dynamic::Diagnostics::ArgStream::operator<<
ArgStream & operator<<(const T &Arg)
Definition: Diagnostics.h:91
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNoComma
@ ET_ParserNoComma
Definition: Diagnostics.h:75
clang::ast_matchers::dynamic::ParserValue::ParserValue
ParserValue()
Definition: Diagnostics.h:43
clang::ast_matchers::dynamic::Diagnostics::ContextType
ContextType
Parser context types.
Definition: Diagnostics.h:53
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNoCode
@ ET_ParserNoCode
Definition: Diagnostics.h:76
VariantValue.h
clang::ast_matchers::dynamic::Diagnostics::Context::ConstructMatcherEnum
ConstructMatcherEnum
About to call the constructor for a matcher.
Definition: Diagnostics.h:109
clang::ast_matchers::dynamic::Diagnostics::ET_ParserStringError
@ ET_ParserStringError
Definition: Diagnostics.h:72
clang::ast_matchers::dynamic::Diagnostics::ET_ParserTrailingCode
@ ET_ParserTrailingCode
Definition: Diagnostics.h:80
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryNonNodeMatcher
@ ET_RegistryNonNodeMatcher
Definition: Diagnostics.h:69
clang::ast_matchers::dynamic::Diagnostics::ErrorContent::Messages
std::vector< Message > Messages
Definition: Diagnostics.h:161
clang::ast_matchers::dynamic::Diagnostics::ContextFrame::Range
SourceRange Range
Definition: Diagnostics.h:149
clang::ast_matchers::dynamic::SourceRange::End
SourceLocation End
Definition: Diagnostics.h:38
clang::ast_matchers::dynamic::ParserValue::Text
StringRef Text
Definition: Diagnostics.h:44
clang::ast_matchers::dynamic::Diagnostics::Context::ConstructMatcher
@ ConstructMatcher
Definition: Diagnostics.h:109
clang::ast_matchers::dynamic::Diagnostics::Context::Context
Context(ConstructMatcherEnum, Diagnostics *Error, StringRef MatcherName, SourceRange MatcherRange)
Definition: Diagnostics.cpp:23
clang::ast_matchers::dynamic::Diagnostics::ContextFrame::Type
ContextType Type
Definition: Diagnostics.h:148
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryWrongArgCount
@ ET_RegistryWrongArgCount
Definition: Diagnostics.h:63
clang::ast_matchers::dynamic::Diagnostics::Context::MatcherArgEnum
MatcherArgEnum
About to recurse into parsing one argument for a matcher.
Definition: Diagnostics.h:113
clang::ast_matchers::dynamic::Diagnostics::ContextFrame::Args
std::vector< std::string > Args
Definition: Diagnostics.h:150
clang::ast_matchers::dynamic::SourceLocation::Column
unsigned Column
Definition: Diagnostics.h:33
llvm::ArrayRef
Definition: LLVM.h:34
Value
Value
Definition: UninitializedValues.cpp:102
clang::ast_matchers::dynamic::ParserValue::Value
VariantValue Value
Definition: Diagnostics.h:46
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryUnknownEnumWithReplace
@ ET_RegistryUnknownEnumWithReplace
Definition: Diagnostics.h:68
LLVM.h
clang::ast_matchers::dynamic::SourceRange
Definition: Diagnostics.h:36
clang::ast_matchers::dynamic::Diagnostics::ErrorContent::Message::Type
ErrorType Type
Definition: Diagnostics.h:158
clang::ast_matchers::dynamic::Diagnostics::ET_ParserFailedToBuildMatcher
@ ET_ParserFailedToBuildMatcher
Definition: Diagnostics.h:84
clang::ast_matchers::dynamic::VariantValue
Variant value class.
Definition: VariantValue.h:254
clang::ast_matchers::dynamic::Diagnostics::ET_None
@ ET_None
Definition: Diagnostics.h:60
clang::ast_matchers::dynamic::SourceRange::Start
SourceLocation Start
Definition: Diagnostics.h:37
clang::ast_matchers::dynamic::Diagnostics::printToStreamFull
void printToStreamFull(llvm::raw_ostream &OS) const
Returns the full string representation of each error.
Definition: Diagnostics.cpp:210
clang::ast_matchers::dynamic::Diagnostics
Helper class to manage error messages.
Definition: Diagnostics.h:50
clang::ast_matchers::dynamic::Diagnostics::ET_ParserInvalidToken
@ ET_ParserInvalidToken
Definition: Diagnostics.h:78
clang
Definition: CalledOnceCheck.h:17
clang::ast_matchers::dynamic::Diagnostics::ContextFrame
Information stored for one frame of the context.
Definition: Diagnostics.h:147
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryWrongArgType
@ ET_RegistryWrongArgType
Definition: Diagnostics.h:64
clang::ast_matchers::dynamic::Diagnostics::errors
ArrayRef< ErrorContent > errors() const
Definition: Diagnostics.h:163
clang::ast_matchers::dynamic::Diagnostics::addError
ArgStream addError(SourceRange Range, ErrorType Error)
Add an error to the diagnostics.
Definition: Diagnostics.cpp:65
clang::ast_matchers::dynamic::SourceLocation
Definition: Diagnostics.h:30
clang::ast_matchers::dynamic::SourceLocation::Line
unsigned Line
Definition: Diagnostics.h:32
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNoCloseParen
@ ET_ParserNoCloseParen
Definition: Diagnostics.h:74
clang::ast_matchers::dynamic::Diagnostics::ErrorContent
Information stored for each error found.
Definition: Diagnostics.h:154
clang::ast_matchers::dynamic::Diagnostics::OverloadContext
Context for overloaded matcher construction.
Definition: Diagnostics.h:126
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryNotBindable
@ ET_RegistryNotBindable
Definition: Diagnostics.h:65
clang::ast_matchers::dynamic::Diagnostics::ErrorContent::Message::Args
std::vector< std::string > Args
Definition: Diagnostics.h:159
clang::ast_matchers::dynamic::Diagnostics::ET_ParserOverloadedType
@ ET_ParserOverloadedType
Definition: Diagnostics.h:82
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNumberError
@ ET_ParserNumberError
Definition: Diagnostics.h:81
clang::ast_matchers::dynamic::Diagnostics::ErrorContent::Message::Range
SourceRange Range
Definition: Diagnostics.h:157
clang::ast_matchers::dynamic::Diagnostics::ArgStream::ArgStream
ArgStream(std::vector< std::string > *Out)
Definition: Diagnostics.h:90
clang::ast_matchers::dynamic::Diagnostics::ET_ParserNoOpenParen
@ ET_ParserNoOpenParen
Definition: Diagnostics.h:73
clang::ast_matchers::dynamic::Diagnostics::ET_RegistryAmbiguousOverload
@ ET_RegistryAmbiguousOverload
Definition: Diagnostics.h:66
clang::ast_matchers::dynamic::Diagnostics::ErrorContent::Message
Definition: Diagnostics.h:156
clang::ast_matchers::dynamic::Diagnostics::ET_ParserMalformedBindExpr
@ ET_ParserMalformedBindExpr
Definition: Diagnostics.h:79
clang::ast_matchers::dynamic::Diagnostics::printToStream
void printToStream(llvm::raw_ostream &OS) const
Returns a simple string representation of each error.
Definition: Diagnostics.cpp:196
clang::ast_matchers::dynamic::Diagnostics::CT_MatcherConstruct
@ CT_MatcherConstruct
Definition: Diagnostics.h:55