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 #ifdef _MSC_VER
12 # include <util/unicode.h>
13 #endif
14 
15 #include <fstream>
16 
18  unsigned level,
19  const std::string &message,
20  const source_locationt &location)
21 {
22  if(verbosity < level || location == source_locationt())
23  {
24  console_message_handlert::print(level, message);
25  return;
26  }
27 
28  std::ostringstream formatted_message;
29 
30  const irep_idt file = location.get_file();
31  const std::string &line = id2string(location.get_line());
32  formatted_message << file << '(' << line << "): ";
33 
34  if(level == messaget::M_ERROR)
35  formatted_message << "error: ";
36  else if(level == messaget::M_WARNING)
37  formatted_message << "warning: ";
38 
39  formatted_message << message;
40 
41  const auto full_path = location.full_path();
42 
43  if(full_path.has_value() && !line.empty())
44  {
45 #ifdef _MSC_VER
46  std::ifstream in(widen(full_path.value()));
47 #else
48  std::ifstream in(full_path.value());
49 #endif
50  if(in)
51  {
52  const auto line_number = std::stoull(line);
53  std::string source_line;
54  for(std::size_t l = 0; l < line_number; l++)
55  std::getline(in, source_line);
56 
57  if(in)
58  {
59  formatted_message << '\n';
60  formatted_message << file << '(' << line << "): " << source_line;
61  }
62  }
63  }
64 
65  console_message_handlert::print(level, formatted_message.str());
66 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
console_message_handlert::print
void print(unsigned, const xmlt &) override
Definition: cout_message.h:32
message_handlert::verbosity
unsigned verbosity
Definition: message.h:72
source_locationt::get_line
const irep_idt & get_line() const
Definition: source_location.h:45
cl_message_handler.h
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
cl_message_handlert::print
void print(unsigned, const xmlt &) override
Definition: cl_message_handler.h:17
messaget::M_ERROR
@ M_ERROR
Definition: message.h:170
source_locationt::full_path
optionalt< std::string > full_path() const
Get a path to the file, including working directory.
Definition: source_location.cpp:85
widen
std::wstring widen(const char *s)
Definition: unicode.cpp:48
source_locationt
Definition: source_location.h:18
messaget::M_WARNING
@ M_WARNING
Definition: message.h:170
unicode.h
source_locationt::get_file
const irep_idt & get_file() const
Definition: source_location.h:35