CBMC
cprover_library.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "cprover_library.h"
10 
11 #include <sstream>
12 
13 #include <util/config.h>
14 #include <util/symbol_table.h>
15 
16 #include "ansi_c_language.h"
17 
18 static std::string get_cprover_library_text(
19  const std::set<irep_idt> &functions,
20  const symbol_tablet &symbol_table,
21  const bool force_load)
22 {
23  std::ostringstream library_text;
24 
25  library_text << "#line 1 \"<built-in-additions>\"\n"
26  "#define " CPROVER_PREFIX "malloc_failure_mode "
28  << "\n"
29  "#define " CPROVER_PREFIX "malloc_failure_mode_return_null "
31  << "\n"
32  "#define " CPROVER_PREFIX
33  "malloc_failure_mode_assert_then_assume "
34  << std::to_string(
36  << "\n"
37  "#define " CPROVER_PREFIX "malloc_may_fail "
39 
40  library_text <<
41  "#line 1 \"<builtin-library>\"\n"
42  "#undef inline\n";
43 
45  library_text << "#define " CPROVER_PREFIX "STRING_ABSTRACTION\n";
46 
47  // cprover_library.inc may not have been generated when running Doxygen, thus
48  // make Doxygen skip this part
50  const struct cprover_library_entryt cprover_library[] =
51 #include "cprover_library.inc"
52  ; // NOLINT(whitespace/semicolon)
54 
56  functions, symbol_table, cprover_library, library_text.str(), force_load);
57 }
58 
60  const std::set<irep_idt> &functions,
61  const symbol_tablet &symbol_table,
62  const struct cprover_library_entryt cprover_library[],
63  const std::string &prologue,
64  const bool force_load)
65 {
66  // the default mode is ios_base::out which means subsequent write to the
67  // stream will overwrite the original content
68  std::ostringstream library_text(prologue, std::ios_base::ate);
69 
70  std::size_t count=0;
71 
72  for(const cprover_library_entryt *e = cprover_library; e->function != nullptr;
73  e++)
74  {
75  irep_idt id=e->function;
76 
77  if(functions.find(id)!=functions.end())
78  {
79  symbol_tablet::symbolst::const_iterator old=
80  symbol_table.symbols.find(id);
81 
82  if(
83  force_load ||
84  (old != symbol_table.symbols.end() && old->second.value.is_nil()))
85  {
86  count++;
87  library_text << e->model << '\n';
88  }
89  }
90  }
91 
92  if(count==0)
93  return std::string();
94  else
95  return library_text.str();
96 }
97 
99  const std::set<irep_idt> &functions,
100  symbol_tablet &symbol_table,
101  message_handlert &message_handler)
102 {
104  return;
105 
106  std::string library_text =
107  get_cprover_library_text(functions, symbol_table, false);
108 
109  add_library(library_text, symbol_table, message_handler);
110 }
111 
113  const std::string &src,
114  symbol_tablet &symbol_table,
115  message_handlert &message_handler,
116  const std::set<irep_idt> &keep)
117 {
118  if(src.empty())
119  return;
120 
121  std::istringstream in(src);
122 
123  ansi_c_languaget ansi_c_language;
124  ansi_c_language.set_message_handler(message_handler);
125  ansi_c_language.parse(in, "");
126 
127  ansi_c_language.typecheck(symbol_table, "<built-in-library>", true, keep);
128 }
129 
131  const std::set<irep_idt> &functions,
132  symbol_tablet &symbol_table,
133  message_handlert &message_handler)
134 {
135  std::string library_text =
136  get_cprover_library_text(functions, symbol_table, true);
137  add_library(library_text, symbol_table, message_handler, functions);
138 }
get_cprover_library_text
static std::string get_cprover_library_text(const std::set< irep_idt > &functions, const symbol_tablet &symbol_table, const bool force_load)
Definition: cprover_library.cpp:18
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
cprover_library_entryt::function
const char * function
Definition: cprover_library.h:22
configt::ansi_ct::malloc_failure_mode_assert_then_assume
@ malloc_failure_mode_assert_then_assume
Definition: config.h:270
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:58
configt::ansi_c
struct configt::ansi_ct ansi_c
configt::ansi_ct::libt::LIB_NONE
@ LIB_NONE
configt::ansi_ct::malloc_failure_mode_return_null
@ malloc_failure_mode_return_null
Definition: config.h:269
cprover_library_entryt
Definition: cprover_library.h:20
configt::ansi_ct::string_abstraction
bool string_abstraction
Definition: config.h:263
messaget::set_message_handler
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:179
message_handlert
Definition: message.h:27
config
configt config
Definition: config.cpp:25
cprover_c_library_factory_force_load
void cprover_c_library_factory_force_load(const std::set< irep_idt > &functions, symbol_tablet &symbol_table, message_handlert &message_handler)
Load the requested function symbols from the cprover library and add them to the symbol table regardl...
Definition: cprover_library.cpp:130
ansi_c_language.h
cprover_c_library_factory
void cprover_c_library_factory(const std::set< irep_idt > &functions, symbol_tablet &symbol_table, message_handlert &message_handler)
Definition: cprover_library.cpp:98
ansi_c_languaget::parse
bool parse(std::istream &instream, const std::string &path) override
Definition: ansi_c_language.cpp:50
add_library
void add_library(const std::string &src, symbol_tablet &symbol_table, message_handlert &message_handler, const std::set< irep_idt > &keep)
Parses and typechecks the given src and adds its contents to the symbol table.
Definition: cprover_library.cpp:112
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
CPROVER_PREFIX
#define CPROVER_PREFIX
Definition: cprover_prefix.h:14
config.h
configt::ansi_ct::malloc_failure_mode
malloc_failure_modet malloc_failure_mode
Definition: config.h:273
ansi_c_languaget::typecheck
bool typecheck(symbol_tablet &symbol_table, const std::string &module, const bool keep_file_local) override
typecheck without removing specified entries from the symbol table
Definition: ansi_c_language.cpp:103
configt::ansi_ct::lib
libt lib
Definition: config.h:261
symbol_table.h
Author: Diffblue Ltd.
configt::ansi_ct::malloc_may_fail
bool malloc_may_fail
Definition: config.h:264
ansi_c_languaget
Definition: ansi_c_language.h:34
cprover_library.h