CBMC
add_failed_symbols.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Pointer Dereferencing
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "add_failed_symbols.h"
13 
14 #include <util/c_types.h>
15 #include <util/namespace.h>
16 #include <util/std_expr.h>
17 #include <util/symbol.h>
18 #include <util/symbol_table_base.h>
19 
20 #include <list>
21 
27 {
28  return id2string(id)+"$object";
29 }
30 
35 void add_failed_symbol(symbolt &symbol, symbol_table_baset &symbol_table)
36 {
37  if(symbol.type.id()==ID_pointer)
38  {
39  symbolt new_symbol;
40  new_symbol.is_lvalue=true;
41  new_symbol.module=symbol.module;
42  new_symbol.mode=symbol.mode;
43  new_symbol.base_name=failed_symbol_id(symbol.base_name);
44  new_symbol.name=failed_symbol_id(symbol.name);
45  new_symbol.type = to_pointer_type(symbol.type).base_type();
46  new_symbol.value.make_nil();
47  new_symbol.type.set(ID_C_is_failed_symbol, true);
48 
49  symbol.type.set(ID_C_failed_symbol, new_symbol.name);
50 
51  if(new_symbol.type.id()==ID_pointer)
52  add_failed_symbol(new_symbol, symbol_table); // recursive call
53 
54  symbol_table.insert(std::move(new_symbol));
55  }
56 }
57 
64  const symbolt &symbol, symbol_table_baset &symbol_table)
65 {
66  if(!symbol.is_lvalue)
67  return;
68 
69  if(!symbol.type.get(ID_C_failed_symbol).empty())
70  return;
71 
72  add_failed_symbol(symbol_table.get_writeable_ref(symbol.name), symbol_table);
73 }
74 
79 {
80  // the symbol table iterators are not stable, and
81  // we are adding new symbols, this
82  // is why we need a list of pointers
83  std::list<const symbolt *> symbol_list;
84  for(auto &named_symbol : symbol_table.symbols)
85  symbol_list.push_back(&(named_symbol.second));
86 
87  for(const symbolt *symbol : symbol_list)
88  add_failed_symbol_if_needed(*symbol, symbol_table);
89 }
90 
92 get_failed_symbol(const symbol_exprt &expr, const namespacet &ns)
93 {
94  const symbolt &symbol=ns.lookup(expr);
95  irep_idt failed_symbol_id=symbol.type.get(ID_C_failed_symbol);
96 
98  return {};
99 
100  const symbolt &failed_symbol=ns.lookup(failed_symbol_id);
101 
102  return symbol_exprt(failed_symbol_id, failed_symbol.type);
103 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
get_failed_symbol
optionalt< symbol_exprt > get_failed_symbol(const symbol_exprt &expr, const namespacet &ns)
Get the failed-dereference symbol for the given symbol.
Definition: add_failed_symbols.cpp:92
add_failed_symbol
void add_failed_symbol(symbolt &symbol, symbol_table_baset &symbol_table)
Create a failed-dereference symbol for the given base symbol if it is pointer-typed; if not,...
Definition: add_failed_symbols.cpp:35
irept::make_nil
void make_nil()
Definition: irep.h:454
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
namespace.h
irept::get
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:45
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
symbol_table_baset::get_writeable_ref
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
Definition: symbol_table_base.h:121
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
failed_symbol_id
irep_idt failed_symbol_id(const irep_idt &id)
Get the name of the special symbol used to denote an unknown referee pointed to by a given pointer-ty...
Definition: add_failed_symbols.cpp:26
irept::set
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:420
symbol_table_baset
The symbol table base class interface.
Definition: symbol_table_base.h:21
to_pointer_type
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: pointer_expr.h:79
symbol.h
Symbol table entry.
irept::id
const irep_idt & id() const
Definition: irep.h:396
dstringt::empty
bool empty() const
Definition: dstring.h:88
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
symbolt
Symbol table entry.
Definition: symbol.h:27
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
add_failed_symbol_if_needed
void add_failed_symbol_if_needed(const symbolt &symbol, symbol_table_baset &symbol_table)
Create a failed-dereference symbol for the given base symbol if it is pointer-typed,...
Definition: add_failed_symbols.cpp:63
pointer_typet::base_type
const typet & base_type() const
The type of the data what we point to.
Definition: pointer_expr.h:35
symbol_table_baset::insert
virtual std::pair< symbolt &, bool > insert(symbolt symbol)=0
Move or copy a new symbol to the symbol table.
symbolt::is_lvalue
bool is_lvalue
Definition: symbol.h:66
add_failed_symbols.h
add_failed_symbols
void add_failed_symbols(symbol_table_baset &symbol_table)
Create a failed-dereference symbol for all symbols in the given table that need one (i....
Definition: add_failed_symbols.cpp:78
symbol_table_base.h
Author: Diffblue Ltd.
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
std_expr.h
c_types.h
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40