1 //===--- Diagnostics.h - Helper class for error diagnostics -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Diagnostics class to manage error messages.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_DIAGNOSTICS_H
15 #define LLVM_CLANG_ASTMATCHERS_DYNAMIC_DIAGNOSTICS_H
17 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
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"
27 namespace ast_matchers {
30 struct SourceLocation {
31 SourceLocation() : Line(), Column() {}
41 /// A VariantValue instance annotated with its parser context.
43 ParserValue() : Text(), Range(), Value() {}
49 /// Helper class to manage error messages.
52 /// Parser context types.
55 CT_MatcherConstruct = 1
58 /// All errors from the system.
62 ET_RegistryMatcherNotFound = 1,
63 ET_RegistryWrongArgCount = 2,
64 ET_RegistryWrongArgType = 3,
65 ET_RegistryNotBindable = 4,
66 ET_RegistryAmbiguousOverload = 5,
67 ET_RegistryValueNotFound = 6,
69 ET_ParserStringError = 100,
70 ET_ParserNoOpenParen = 101,
71 ET_ParserNoCloseParen = 102,
72 ET_ParserNoComma = 103,
73 ET_ParserNoCode = 104,
74 ET_ParserNotAMatcher = 105,
75 ET_ParserInvalidToken = 106,
76 ET_ParserMalformedBindExpr = 107,
77 ET_ParserTrailingCode = 108,
78 ET_ParserNumberError = 109,
79 ET_ParserOverloadedType = 110
82 /// Helper stream class.
85 ArgStream(std::vector<std::string> *Out) : Out(Out) {}
86 template <class T> ArgStream &operator<<(const T &Arg) {
87 return operator<<(Twine(Arg));
89 ArgStream &operator<<(const Twine &Arg);
92 std::vector<std::string> *Out;
95 /// Class defining a parser context.
97 /// Used by the parser to specify (possibly recursive) contexts where the
98 /// parsing/construction can fail. Any error triggered within a context will
99 /// keep information about the context chain.
100 /// This class should be used as a RAII instance in the stack.
103 /// About to call the constructor for a matcher.
104 enum ConstructMatcherEnum { ConstructMatcher };
105 Context(ConstructMatcherEnum, Diagnostics *Error, StringRef MatcherName,
106 SourceRange MatcherRange);
107 /// About to recurse into parsing one argument for a matcher.
108 enum MatcherArgEnum { MatcherArg };
109 Context(MatcherArgEnum, Diagnostics *Error, StringRef MatcherName,
110 SourceRange MatcherRange, unsigned ArgNumber);
114 Diagnostics *const Error;
117 /// Context for overloaded matcher construction.
119 /// This context will take care of merging all errors that happen within it
120 /// as "candidate" overloads for the same matcher.
121 struct OverloadContext {
123 OverloadContext(Diagnostics* Error);
126 /// Revert all errors that happened within this context.
130 Diagnostics *const Error;
134 /// Add an error to the diagnostics.
136 /// All the context information will be kept on the error message.
137 /// \return a helper class to allow the caller to pass the arguments for the
138 /// error message, using the << operator.
139 ArgStream addError(SourceRange Range, ErrorType Error);
141 /// Information stored for one frame of the context.
142 struct ContextFrame {
145 std::vector<std::string> Args;
148 /// Information stored for each error found.
149 struct ErrorContent {
150 std::vector<ContextFrame> ContextStack;
154 std::vector<std::string> Args;
156 std::vector<Message> Messages;
158 ArrayRef<ErrorContent> errors() const { return Errors; }
160 /// Returns a simple string representation of each error.
162 /// Each error only shows the error message without any context.
163 void printToStream(llvm::raw_ostream &OS) const;
164 std::string toString() const;
166 /// Returns the full string representation of each error.
168 /// Each error message contains the full context.
169 void printToStreamFull(llvm::raw_ostream &OS) const;
170 std::string toStringFull() const;
173 /// Helper function used by the constructors of ContextFrame.
174 ArgStream pushContextFrame(ContextType Type, SourceRange Range);
176 std::vector<ContextFrame> ContextStack;
177 std::vector<ErrorContent> Errors;
180 } // namespace dynamic
181 } // namespace ast_matchers
184 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_DIAGNOSTICS_H