Coverage Report

Created: 2026-06-30 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/solidity/liblangutil/UniqueErrorReporter.h
Line
Count
Source
1
/*
2
  This file is part of solidity.
3
4
  solidity is free software: you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation, either version 3 of the License, or
7
  (at your option) any later version.
8
9
  solidity is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
// SPDX-License-Identifier: GPL-3.0
18
19
#pragma once
20
21
#include <liblangutil/ErrorReporter.h>
22
#include <liblangutil/Exceptions.h>
23
24
namespace solidity::langutil
25
{
26
27
/*
28
 * Wrapper for ErrorReporter that removes duplicates.
29
 * Two errors are considered the same if their error ID and location are the same.
30
 */
31
class UniqueErrorReporter
32
{
33
public:
34
25.1k
  UniqueErrorReporter(): m_errorReporter(m_uniqueErrors) {}
35
36
  void append(UniqueErrorReporter const& _other)
37
0
  {
38
0
    m_errorReporter.append(_other.m_errorReporter.errors());
39
0
  }
40
41
  void warning(ErrorId _error, SourceLocation const& _location, std::string const& _description)
42
33.9k
  {
43
33.9k
    if (!seen(_error, _location, _description))
44
28.7k
    {
45
28.7k
      m_errorReporter.warning(_error, _location, _description);
46
28.7k
      markAsSeen(_error, _location, _description);
47
28.7k
    }
48
33.9k
  }
49
50
  void warning(
51
    ErrorId _error,
52
    SourceLocation const& _location,
53
    std::string const& _description,
54
    SecondarySourceLocation const& _secondaryLocation
55
  )
56
0
  {
57
0
    if (!seen(_error, _location, _description))
58
0
    {
59
0
      m_errorReporter.warning(_error, _location, _description, _secondaryLocation);
60
0
      markAsSeen(_error, _location, _description);
61
0
    }
62
0
  }
63
64
  void warning(ErrorId _error, std::string const& _description)
65
0
  {
66
0
    m_errorReporter.warning(_error, _description);
67
0
  }
68
69
  void info(ErrorId _error, SourceLocation const& _location, std::string const& _description)
70
0
  {
71
0
    if (!seen(_error, _location, _description))
72
0
    {
73
0
      m_errorReporter.info(_error, _location, _description);
74
0
      markAsSeen(_error, _location, _description);
75
0
    }
76
0
  }
77
78
  void info(ErrorId _error, std::string const& _description)
79
0
  {
80
0
    m_errorReporter.info(_error, _description);
81
0
  }
82
83
  bool seen(ErrorId _error, SourceLocation const& _location, std::string const& _description) const
84
33.9k
  {
85
33.9k
    if (m_seenErrors.count({_error, _location}))
86
5.17k
    {
87
5.17k
      solAssert(m_seenErrors.at({_error, _location}) == _description, "");
88
5.17k
      return true;
89
5.17k
    }
90
28.7k
    return false;
91
33.9k
  }
92
93
  void markAsSeen(ErrorId _error, SourceLocation const& _location, std::string const& _description)
94
28.7k
  {
95
28.7k
    if (_location != SourceLocation{})
96
18.5k
      m_seenErrors[{_error, _location}] = _description;
97
28.7k
  }
98
99
29.4k
  ErrorList const& errors() const { return m_errorReporter.errors(); }
100
101
13.4k
  void clear() { m_errorReporter.clear(); }
102
103
private:
104
  ErrorList m_uniqueErrors;
105
  ErrorReporter m_errorReporter;
106
  std::map<std::pair<ErrorId, SourceLocation>, std::string> m_seenErrors;
107
};
108
109
}