cprover
Loading...
Searching...
No Matches
gcc_message_handler.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
10
11#ifdef _MSC_VER
12# include <util/unicode.h>
13#endif
14
15#include <fstream>
16#include <iostream>
17
19 unsigned level,
20 const std::string &message,
21 const source_locationt &location)
22{
24
25 if(verbosity >= level)
26 {
27 // gcc appears to send everything to cerr
28 auto &out = std::cerr;
29
30 const irep_idt file = location.get_file();
31 const irep_idt line = location.get_line();
32 const irep_idt column = location.get_column();
33 const irep_idt function = location.get_function();
34
35 if(!function.empty())
36 {
37 if(!file.empty())
38 out << string(messaget::bold) << file << ':' << string(messaget::reset)
39 << ' ';
40 out << "In function " << string(messaget::bold) << '\'' << function
41 << '\'' << string(messaget::reset) << ":\n";
42 }
43
44 if(!line.empty())
45 {
46 out << string(messaget::bold);
47
48 if(!file.empty())
49 out << file << ':';
50
51 out << line << ':';
52
53 if(column.empty())
54 out << "1: ";
55 else
56 out << column << ": ";
57
59 out << string(messaget::red) << "error: ";
60 else if(level == messaget::M_WARNING)
61 out << string(messaget::bright_magenta) << "warning: ";
62
63 out << string(messaget::reset);
64 }
65
66 out << message << '\n';
67
68 const auto file_name = location.full_path();
69 if(file_name.has_value() && !line.empty())
70 {
71#ifdef _MSC_VER
72 std::ifstream in(widen(file_name.value()));
73#else
74 std::ifstream in(file_name.value());
75#endif
76 if(in)
77 {
78 const auto line_number = std::stoull(id2string(line));
79 std::string source_line;
80 for(std::size_t l = 0; l < line_number; l++)
81 std::getline(in, source_line);
82
83 if(in)
84 out << ' ' << source_line << '\n'; // gcc adds a space, clang doesn't
85 }
86 }
87
88 out << std::flush;
89 }
90}
91
92void gcc_message_handlert::print(unsigned level, const std::string &message)
93{
95
96 // gcc appears to send everything to cerr
97 if(verbosity >= level)
98 std::cerr << message << '\n' << std::flush;
99}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:564
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:37
bool empty() const
Definition dstring.h:88
std::string string(const messaget::commandt &c) const
feed a command into a string
void print(unsigned, const xmlt &) override
virtual void print(unsigned level, const std::string &message)=0
Definition message.cpp:60
unsigned verbosity
Definition message.h:72
static const commandt bright_magenta
render text with bright magenta foreground color
Definition message.h:376
static const commandt reset
return to default formatting, as defined by the terminal
Definition message.h:343
static const commandt bold
render text with bold font
Definition message.h:382
@ M_ERROR
Definition message.h:170
@ M_WARNING
Definition message.h:170
static const commandt red
render text with red foreground color
Definition message.h:346
optionalt< std::string > full_path() const
Get a path to the file, including working directory.
const irep_idt & get_column() const
const irep_idt & get_function() const
const irep_idt & get_file() const
const irep_idt & get_line() const
const std::string & id2string(const irep_idt &d)
Definition irep.h:47
Definition kdev_t.h:19
std::wstring widen(const char *s)
Definition unicode.cpp:49