summaryrefslogtreecommitdiff
path: root/include/clang/ASTMatchers/Dynamic/Diagnostics.h
blob: 417bc67967a10f92b6821d40a0661ca4f05c49c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//===--- Diagnostics.h - Helper class for error diagnostics -----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Diagnostics class to manage error messages.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_AST_MATCHERS_DYNAMIC_DIAGNOSTICS_H
#define LLVM_CLANG_AST_MATCHERS_DYNAMIC_DIAGNOSTICS_H

#include <string>
#include <vector>

#include "clang/ASTMatchers/Dynamic/VariantValue.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"

namespace clang {
namespace ast_matchers {
namespace dynamic {

struct SourceLocation {
  SourceLocation() : Line(), Column() {}
  unsigned Line;
  unsigned Column;
};

struct SourceRange {
  SourceLocation Start;
  SourceLocation End;
};

/// \brief A VariantValue instance annotated with its parser context.
struct ParserValue {
  ParserValue() : Text(), Range(), Value() {}
  StringRef Text;
  SourceRange Range;
  VariantValue Value;
};

/// \brief Helper class to manage error messages.
class Diagnostics {
 public:
  /// \brief All errors from the system.
  enum ErrorType {
    ET_None = 0,

    ET_RegistryNotFound = 1,
    ET_RegistryWrongArgCount = 2,
    ET_RegistryWrongArgType = 3,

    ET_ParserStringError = 100,
    ET_ParserMatcherArgFailure = 101,
    ET_ParserMatcherFailure = 102,
    ET_ParserNoOpenParen = 103,
    ET_ParserNoCloseParen = 104,
    ET_ParserNoComma = 105,
    ET_ParserNoCode = 106,
    ET_ParserNotAMatcher = 107,
    ET_ParserInvalidToken = 108
  };

  /// \brief Helper stream class.
  struct ArgStream {
    template <class T> ArgStream &operator<<(const T &Arg) {
      return operator<<(Twine(Arg));
    }
    ArgStream &operator<<(const Twine &Arg);
    std::vector<std::string> *Out;
  };

  /// \brief Push a frame to the beginning of the list
  ///
  /// Returns a helper class to allow the caller to pass the arguments for the
  /// error message, using the << operator.
  ArgStream pushErrorFrame(const SourceRange &Range, ErrorType Error);

  struct ErrorFrame {
    SourceRange Range;
    ErrorType Type;
    std::vector<std::string> Args;

    std::string ToString() const;
  };
  ArrayRef<ErrorFrame> frames() const { return Frames; }

  /// \brief Returns a string representation of the last frame.
  std::string ToString() const;
  /// \brief Returns a string representation of the whole frame stack.
  std::string ToStringFull() const;

 private:
   std::vector<ErrorFrame> Frames;
};

}  // namespace dynamic
}  // namespace ast_matchers
}  // namespace clang

#endif  // LLVM_CLANG_AST_MATCHERS_DYNAMIC_DIAGNOSTICS_H