CBMC
Loading...
Searching...
No Matches
cprover_library.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#include "cprover_library.h"
10
11#include <util/config.h>
12#include <util/cprover_prefix.h>
14
15#include "ansi_c_language.h"
16
17#include <sstream>
18
19static std::string get_cprover_library_text(
20 const std::set<irep_idt> &functions,
21 const symbol_table_baset &symbol_table,
22 const bool force_load)
23{
24 std::ostringstream library_text;
25
26 library_text << "#line 1 \"<built-in-additions>\"\n"
27 "#define " CPROVER_PREFIX "malloc_failure_mode "
28 << std::to_string(config.ansi_c.malloc_failure_mode)
29 << "\n"
30 "#define " CPROVER_PREFIX "malloc_failure_mode_return_null "
31 << std::to_string(config.ansi_c.malloc_failure_mode_return_null)
32 << "\n"
33 "#define " CPROVER_PREFIX
34 "malloc_failure_mode_assert_then_assume "
35 << std::to_string(
36 config.ansi_c.malloc_failure_mode_assert_then_assume)
37 << "\n"
38 "#define " CPROVER_PREFIX "malloc_may_fail "
39 << std::to_string(config.ansi_c.malloc_may_fail) << "\n";
40
42 "#line 1 \"<builtin-library>\"\n"
43 "#undef inline\n";
44
45 if(config.ansi_c.string_abstraction)
46 library_text << "#define " CPROVER_PREFIX "STRING_ABSTRACTION\n";
47
48 if(config.ansi_c.dfcc_debug_lib)
49 library_text << "#define " CPROVER_PREFIX "DFCC_DEBUG_LIB\n";
50
51 if(config.ansi_c.simple_invalid_pointer_model)
52 library_text << "#define " CPROVER_PREFIX
53 "DFCC_SIMPLE_INVALID_POINTER_MODEL\n";
54
55 // cprover_library.inc may not have been generated when running Doxygen, thus
56 // make Doxygen skip this part
59#include "cprover_library.inc" // IWYU pragma: keep
60 ; // NOLINT(whitespace/semicolon)
62
64 functions, symbol_table, cprover_library, library_text.str(), force_load);
65}
66
68 const std::set<irep_idt> &functions,
69 const symbol_table_baset &symbol_table,
71 const std::string &prologue,
72 const bool force_load)
73{
74 // the default mode is ios_base::out which means subsequent write to the
75 // stream will overwrite the original content
76 std::ostringstream library_text(prologue, std::ios_base::ate);
77
78 std::size_t count=0;
79
80 for(const cprover_library_entryt *e = cprover_library; e->function != nullptr;
81 e++)
82 {
83 irep_idt id=e->function;
84
85 if(functions.find(id)!=functions.end())
86 {
87 symbol_table_baset::symbolst::const_iterator old =
88 symbol_table.symbols.find(id);
89
90 if(
91 force_load ||
92 (old != symbol_table.symbols.end() && old->second.value.is_nil()))
93 {
94 count++;
95 library_text << e->model << '\n';
96 }
97 }
98 }
99
100 if(count==0)
101 return std::string();
102 else
103 return library_text.str();
104}
105
107 const std::set<irep_idt> &functions,
108 const symbol_table_baset &symbol_table,
110 message_handlert &message_handler)
111{
113 return;
114
115 std::string library_text =
116 get_cprover_library_text(functions, symbol_table, false);
117
118 add_library(library_text, dest_symbol_table, message_handler);
119}
120
122 const std::string &src,
123 symbol_table_baset &symbol_table,
124 message_handlert &message_handler,
125 const std::set<irep_idt> &keep)
126{
127 if(src.empty())
128 return;
129
130 std::istringstream in(src);
131
133 ansi_c_language.parse(in, "", message_handler);
134
135 ansi_c_language.typecheck(
136 symbol_table, "<built-in-library>", message_handler, true, keep);
137}
138
140 const std::set<irep_idt> &functions,
141 symbol_table_baset &symbol_table,
142 message_handlert &message_handler)
143{
144 std::string library_text =
145 get_cprover_library_text(functions, symbol_table, true);
146 add_library(library_text, symbol_table, message_handler, functions);
147}
static std::string get_cprover_library_text(const std::set< irep_idt > &functions, const symbol_table_baset &symbol_table, const bool force_load)
void add_library(const std::string &src, symbol_table_baset &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.
void cprover_c_library_factory_force_load(const std::set< irep_idt > &functions, symbol_table_baset &symbol_table, message_handlert &message_handler)
Load the requested function symbols from the cprover library and add them to the symbol table regardl...
void cprover_c_library_factory(const std::set< irep_idt > &functions, const symbol_table_baset &symbol_table, symbol_table_baset &dest_symbol_table, message_handlert &message_handler)
configt config
Definition config.cpp:25
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
struct configt::ansi_ct ansi_c
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
The symbol table base class interface.
const symbolst & symbols
Read-only field, used to look up symbols given their names.
#define CPROVER_PREFIX
Author: Diffblue Ltd.