CBMC
cl_message_handler.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Print messages like CL.exe does
4 
5 Author: Michael Tautschnig
6 
7 \*******************************************************************/
8 
9 #include "cl_message_handler.h"
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 
63  console_message_handlert::print(level, formatted_message.str());
64 }
void print(unsigned, const xmlt &) override
Definition: cout_message.h:32
void print(unsigned, const xmlt &) override
Definition: cout_message.h:32
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_line() const
const irep_idt & get_file() 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