CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cl_message_handler.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Print messages like CL.exe does
4
5Author: Michael Tautschnig
6
7\*******************************************************************/
8
10
11#include <util/unicode.h>
12
13#include <fstream>
14
16 unsigned level,
17 const std::string &message,
18 const source_locationt &location)
19{
20 if(verbosity < level || location == source_locationt())
21 {
22 console_message_handlert::print(level, message);
23 return;
24 }
25
26 std::ostringstream formatted_message;
27
29 formatted_message << "error: warning treated as error\n";
30
31 const irep_idt file = location.get_file();
32 const std::string &line = id2string(location.get_line());
33 formatted_message << file << '(' << line << "): ";
34
35 if(level == messaget::M_ERROR)
36 formatted_message << "error: ";
37 else if(level == messaget::M_WARNING)
38 formatted_message << "warning: ";
39
40 formatted_message << message;
41
42 const auto full_path = location.full_path();
43
44 if(full_path.has_value() && !line.empty())
45 {
46 std::ifstream in(widen_if_needed(full_path.value()));
47
48 if(in)
49 {
50 const auto line_number = std::stoull(line);
51 std::string source_line;
52 for(std::size_t l = 0; l < line_number; l++)
53 std::getline(in, source_line);
54
55 if(in)
56 {
57 formatted_message << '\n';
58 formatted_message << file << '(' << line << "): " << source_line;
59 }
60 }
61 }
62
64}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
void print(unsigned, const xmlt &) override
void print(unsigned, const xmlt &) override
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
unsigned verbosity
Definition message.h:71
@ M_ERROR
Definition message.h:169
@ M_WARNING
Definition message.h:169
const irep_idt & get_file() const
const irep_idt & get_line() const
std::optional< std::string > full_path() const
Get a path to the file, including working directory.
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
#define widen_if_needed(s)
Definition unicode.h:28