Coverage Report

Created: 2022-08-24 06:55

/src/solidity/test/libsolidity/util/SoltestErrors.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
  This file is part of solidity.
3
  solidity is free software: you can redistribute it and/or modify
4
  it under the terms of the GNU General Public License as published by
5
  the Free Software Foundation, either version 3 of the License, or
6
  (at your option) any later version.
7
  solidity is distributed in the hope that it will be useful,
8
  but WITHOUT ANY WARRANTY; without even the implied warranty of
9
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
  GNU General Public License for more details.
11
  You should have received a copy of the GNU General Public License
12
  along with solidity.  If not, see <http://www.gnu.org/licenses/>.
13
*/
14
15
#pragma once
16
17
#include <libsolutil/AnsiColorized.h>
18
#include <libsolutil/Assertions.h>
19
#include <libsolutil/CommonData.h>
20
#include <libsolutil/Exceptions.h>
21
22
#include <boost/preprocessor/cat.hpp>
23
#include <boost/preprocessor/facilities/empty.hpp>
24
#include <boost/preprocessor/facilities/overload.hpp>
25
26
namespace solidity::frontend::test
27
{
28
29
struct InternalSoltestError: virtual util::Exception {};
30
31
#if !BOOST_PP_VARIADICS_MSVC
32
22
#define soltestAssert(...) BOOST_PP_OVERLOAD(soltestAssert_,__VA_ARGS__)(__VA_ARGS__)
33
#else
34
#define soltestAssert(...) BOOST_PP_CAT(BOOST_PP_OVERLOAD(soltestAssert_,__VA_ARGS__)(__VA_ARGS__),BOOST_PP_EMPTY())
35
#endif
36
37
#define soltestAssert_1(CONDITION) \
38
  soltestAssert_2((CONDITION), "")
39
40
#define soltestAssert_2(CONDITION, DESCRIPTION) \
41
22
  assertThrowWithDefaultDescription( \
42
22
    (CONDITION), \
43
22
    ::solidity::frontend::test::InternalSoltestError, \
44
22
    (DESCRIPTION), \
45
22
    "Soltest assertion failed" \
46
22
  )
47
48
class TestParserError: virtual public util::Exception
49
{
50
public:
51
  explicit TestParserError(std::string const& _description)
52
0
  {
53
0
    *this << util::errinfo_comment(_description);
54
0
  }
55
};
56
57
/**
58
 * Representation of a notice, warning or error that can occur while
59
 * formatting and therefore updating an interactive function call test.
60
 */
61
struct FormatError
62
{
63
  enum Type
64
  {
65
    Notice,
66
    Warning,
67
    Error
68
  };
69
70
  explicit FormatError(Type _type, std::string _message):
71
    type(_type),
72
    message(std::move(_message))
73
0
  {}
74
75
  Type type;
76
  std::string message;
77
};
78
using FormatErrors = std::vector<FormatError>;
79
80
/**
81
 * Utility class that collects notices, warnings and errors and is able
82
 * to format them for ANSI colorized output during the interactive update
83
 * process in isoltest.
84
 * Its purpose is to help users of isoltest to automatically
85
 * update test files and always keep track of what is happening.
86
 */
87
class ErrorReporter
88
{
89
public:
90
0
  explicit ErrorReporter() {}
91
92
  /// Adds a new FormatError of type Notice with the given message.
93
  void notice(std::string _notice)
94
0
  {
95
0
    m_errors.push_back(FormatError{FormatError::Notice, std::move(_notice)});
96
0
  }
97
98
  /// Adds a new FormatError of type Warning with the given message.
99
  void warning(std::string _warning)
100
0
  {
101
0
    m_errors.push_back(FormatError{FormatError::Warning, std::move(_warning)});
102
0
  }
103
104
  /// Adds a new FormatError of type Error with the given message.
105
  void error(std::string _error)
106
0
  {
107
0
    m_errors.push_back(FormatError{FormatError::Error, std::move(_error)});
108
0
  }
109
110
  /// Prints all errors depending on their type using ANSI colorized output.
111
  /// It will be used to print notices, warnings and errors during the
112
  /// interactive update process.
113
  std::string format(std::string const& _linePrefix, bool _formatted)
114
0
  {
115
0
    std::stringstream os;
116
0
    for (auto const& error: m_errors)
117
0
    {
118
0
      switch (error.type)
119
0
      {
120
0
      case FormatError::Notice:
121
0
122
0
        break;
123
0
      case FormatError::Warning:
124
0
        util::AnsiColorized(
125
0
          os,
126
0
          _formatted,
127
0
          {util::formatting::YELLOW}
128
0
        ) << _linePrefix << "Warning: " << error.message << std::endl;
129
0
        break;
130
0
      case FormatError::Error:
131
0
        util::AnsiColorized(
132
0
          os,
133
0
          _formatted,
134
0
          {util::formatting::RED}
135
0
        ) << _linePrefix << "Error: " << error.message << std::endl;
136
0
        break;
137
0
      }
138
0
    }
139
0
    return os.str();
140
0
  }
141
142
private:
143
  FormatErrors m_errors;
144
};
145
146
}