Coverage Report

Created: 2022-08-24 06:55

/src/solidity/liblangutil/SourceReferenceFormatter.h
Line
Count
Source (jump to first uncovered line)
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
 * Formatting functions for errors referencing positions and locations in the source.
20
 */
21
22
#pragma once
23
24
#include <liblangutil/Exceptions.h>
25
#include <liblangutil/SourceReferenceExtractor.h>
26
27
#include <libsolutil/AnsiColorized.h>
28
29
#include <ostream>
30
#include <sstream>
31
#include <functional>
32
33
namespace solidity::langutil
34
{
35
36
class CharStream;
37
class CharStreamProvider;
38
struct SourceLocation;
39
40
class SourceReferenceFormatter
41
{
42
public:
43
  SourceReferenceFormatter(
44
    std::ostream& _stream,
45
    CharStreamProvider const& _charStreamProvider,
46
    bool _colored,
47
    bool _withErrorIds
48
  ):
49
    m_stream(_stream), m_charStreamProvider(_charStreamProvider), m_colored(_colored), m_withErrorIds(_withErrorIds)
50
0
  {}
51
52
  /// Prints source location if it is given.
53
  void printSourceLocation(SourceReference const& _ref);
54
  void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
55
  void printExceptionInformation(util::Exception const& _exception, std::string const& _severity);
56
  void printErrorInformation(langutil::ErrorList const& _errors);
57
  void printErrorInformation(Error const& _error);
58
59
  static std::string formatExceptionInformation(
60
    util::Exception const& _exception,
61
    std::string const& _name,
62
    CharStreamProvider const& _charStreamProvider,
63
    bool _colored = false,
64
    bool _withErrorIds = false
65
  )
66
0
  {
67
0
    std::ostringstream errorOutput;
68
0
    SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
69
0
    formatter.printExceptionInformation(_exception, _name);
70
0
    return errorOutput.str();
71
0
  }
72
73
  static std::string formatErrorInformation(
74
    Error const& _error,
75
    CharStreamProvider const& _charStreamProvider
76
  )
77
0
  {
78
0
    return formatExceptionInformation(
79
0
      _error,
80
0
      Error::formatErrorSeverity(Error::errorSeverity(_error.type())),
81
0
      _charStreamProvider
82
0
    );
83
0
  }
84
85
  static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream);
86
87
private:
88
  util::AnsiColorized normalColored() const;
89
  util::AnsiColorized frameColored() const;
90
  util::AnsiColorized errorColored(std::optional<langutil::Error::Severity> _severity) const;
91
  util::AnsiColorized messageColored() const;
92
  util::AnsiColorized secondaryColored() const;
93
  util::AnsiColorized highlightColored() const;
94
  util::AnsiColorized diagColored() const;
95
96
private:
97
  std::ostream& m_stream;
98
  CharStreamProvider const& m_charStreamProvider;
99
  bool m_colored;
100
  bool m_withErrorIds;
101
};
102
103
}