-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathgcc_message_handler.cpp
98 lines (75 loc) · 2.38 KB
/
gcc_message_handler.cpp
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
/*******************************************************************\
Module:
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#include "gcc_message_handler.h"
#include <util/unicode.h>
#include <fstream> // IWYU pragma: keep
#include <iostream>
void gcc_message_handlert::print(
unsigned level,
const std::string &message,
const source_locationt &location)
{
message_handlert::print(level, message);
if(verbosity >= level)
{
// gcc appears to send everything to cerr
auto &out = std::cerr;
const irep_idt file = location.get_file();
const irep_idt line = location.get_line();
const irep_idt column = location.get_column();
const irep_idt function = location.get_function();
if(!function.empty())
{
if(!file.empty())
out << string(messaget::bold) << file << ':' << string(messaget::reset)
<< ' ';
out << "In function " << string(messaget::bold) << '\'' << function
<< '\'' << string(messaget::reset) << ":\n";
}
if(!line.empty())
{
out << string(messaget::bold);
if(!file.empty())
out << file << ':';
out << line << ':';
if(column.empty())
out << "1: ";
else
out << column << ": ";
if(
level == messaget::M_ERROR ||
(level == messaget::M_WARNING && warnings_are_errors))
{
out << string(messaget::red) << "error: ";
}
else if(level == messaget::M_WARNING)
out << string(messaget::bright_magenta) << "warning: ";
out << string(messaget::reset);
}
out << message << '\n';
const auto file_name = location.full_path();
if(file_name.has_value() && !line.empty())
{
std::ifstream in(widen_if_needed(file_name.value()));
if(in)
{
const auto line_number = std::stoull(id2string(line));
std::string source_line;
for(std::size_t l = 0; l < line_number; l++)
std::getline(in, source_line);
if(in)
out << ' ' << source_line << '\n'; // gcc adds a space, clang doesn't
}
}
out << std::flush;
}
}
void gcc_message_handlert::print(unsigned level, const std::string &message)
{
message_handlert::print(level, message);
// gcc appears to send everything to cerr
if(verbosity >= level)
std::cerr << message << '\n' << std::flush;
}