cprover
Loading...
Searching...
No Matches
remove_const_function_pointers.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Goto Programs
4
5Author: Thomas Kiley, thomas.kiley@diffblue.com
6
7\*******************************************************************/
8
11
13
14#include <util/arith_tools.h>
15#include <util/format_expr.h>
16#include <util/namespace.h>
17#include <util/pointer_expr.h>
18#include <util/simplify_expr.h>
19#include <util/std_expr.h>
20#include <util/symbol_table.h>
21
22#define LOG(message, irep) \
23 do \
24 { \
25 log.debug().source_location = irep.source_location(); \
26 log.debug() << message << ": " << format(irep) << messaget::eom; \
27 } while(0)
28
35 message_handlert &message_handler,
36 const namespacet &ns,
37 const symbol_tablet &symbol_table)
38 : log(message_handler), ns(ns), symbol_table(symbol_table)
39{}
40
60
69 const exprt &expression) const
70{
71 if(expression.id()==ID_symbol)
72 {
73 if(is_const_expression(expression))
74 {
75 const symbolt &symbol =
77 if(symbol.type.id()!=ID_code)
78 {
79 const exprt &symbol_value=symbol.value;
81 }
82 else
83 {
84 return expression;
85 }
86 }
87 else
88 {
89 return expression;
90 }
91 }
92 else
93 {
96 for(const exprt &op : expression.operands())
97 {
100 }
101
103 }
104}
105
110 const symbol_exprt &symbol_expr) const
111{
112 const symbolt &symbol = symbol_table.lookup_ref(symbol_expr.get_identifier());
113 return symbol.value;
114}
115
125 const exprt &expr, functionst &out_functions)
126{
128 const exprt &simplified_expr=simplify_expr(expr, ns);
129 bool resolved=false;
131 if(simplified_expr.id()==ID_index)
132 {
133 const index_exprt &index_expr=to_index_expr(simplified_expr);
135 }
136 else if(simplified_expr.id()==ID_member)
137 {
138 const member_exprt &member_expr=to_member_expr(simplified_expr);
140 }
141 else if(simplified_expr.id()==ID_address_of)
142 {
146 }
147 else if(simplified_expr.id()==ID_dereference)
148 {
149 const dereference_exprt &deref=to_dereference_expr(simplified_expr);
151 }
152 else if(simplified_expr.id()==ID_typecast)
153 {
155 resolved=
157 }
158 else if(simplified_expr.id()==ID_symbol)
159 {
160 if(simplified_expr.type().id()==ID_code)
161 {
162 resolved_functions.insert(to_symbol_expr(simplified_expr));
163 resolved=true;
164 }
165 else
166 {
167 LOG("Non const symbol wasn't squashed", simplified_expr);
168 resolved=false;
169 }
170 }
171 else if(simplified_expr.id()==ID_constant)
172 {
173 if(simplified_expr.is_zero())
174 {
175 // We have the null pointer - no need to throw everything away
176 // but we don't add any functions either
177 resolved=true;
178 }
179 else
180 {
181 LOG("Non-zero constant value found", simplified_expr);
182 resolved=false;
183 }
184 }
185 else
186 {
187 LOG("Unrecognised expression", simplified_expr);
188 resolved=false;
189 }
190
191 if(resolved)
192 {
194 return true;
195 }
196 else
197 {
198 return false;
199 }
200}
201
210{
211 for(const exprt &value : exprs)
212 {
214 bool resolved_value=
216
218 {
219 out_functions.insert(
222 }
223 else
224 {
225 LOG("Could not resolve expression in array", value);
226 return false;
227 }
228 }
229 return true;
230}
231
245{
247 bool array_const;
248 bool resolved=
250
251 if(!resolved)
252 {
253 LOG("Could not resolve array", index_expr);
254 return false;
255 }
256
257 if(!array_const)
258 {
259 LOG("Array not const", index_expr);
260 return false;
261 }
262
264}
265
277{
279 bool struct_const;
280 bool resolved=
282
283 if(!resolved)
284 {
285 LOG("Could not resolve struct", member_expr);
286 return false;
287 }
288
289 if(!struct_const)
290 {
291 LOG("Struct was not const so can't resolve values on it", member_expr);
292 return false;
293 }
294
296}
297
317
329{
331 bool deref_const;
332 bool resolved=
334
335 if(!resolved)
336 {
337 LOG("Failed to squash dereference", deref_expr);
338 return false;
339 }
340
341 if(!deref_const)
342 {
343 LOG("Dereferenced value was not const so can't dereference", deref_expr);
344 return false;
345 }
346
348}
349
361{
362 // We simply ignore typecasts and assume they are valid
363 // I thought simplify_expr would deal with this, but for example
364 // a cast from a 32 bit width int to a 64bit width int it doesn't seem
365 // to allow
367 bool resolved=
369
370 if(resolved)
371 {
372 out_functions.insert(typecast_values.begin(), typecast_values.end());
373 return true;
374 }
375 else
376 {
377 LOG("Failed to squash typecast", typecast_expr);
378 return false;
379 }
380}
381
398{
399 exprt simplified_expr=simplify_expr(expr, ns);
400 bool resolved;
402 bool is_resolved_expression_const = false;
403 if(simplified_expr.id()==ID_index)
404 {
405 const index_exprt &index_expr=to_index_expr(simplified_expr);
406 resolved=
409 }
410 else if(simplified_expr.id()==ID_member)
411 {
412 const member_exprt &member_expr=to_member_expr(simplified_expr);
415 }
416 else if(simplified_expr.id()==ID_dereference)
417 {
418 const dereference_exprt &deref=to_dereference_expr(simplified_expr);
419 resolved=
422 }
423 else if(simplified_expr.id()==ID_typecast)
424 {
426 resolved=
429 }
430 else if(simplified_expr.id()==ID_symbol)
431 {
432 LOG("Non const symbol will not be squashed", simplified_expr);
433 resolved=false;
434 }
435 else
436 {
437 resolved_expressions.push_back(simplified_expr);
439 resolved=true;
440 }
441
442 if(resolved)
443 {
446 resolved_expressions.begin(),
449 return true;
450 }
451 else
452 {
453 return false;
454 }
455}
456
466 const exprt &expr, mp_integer &out_array_index)
467{
469 bool is_const=false;
471 if(resolved)
472 {
473 if(index_value_expressions.size()==1 &&
475 {
480 if(!errors)
481 {
483 }
484 return !errors;
485 }
486 else
487 {
488 return false;
489 }
490 }
491 else
492 {
493 return false;
494 }
495}
496
508 const index_exprt &index_expr,
510 bool &out_is_const)
511{
512 // Get the array(s) it belongs to
514 bool array_const=false;
515 bool resolved_array=
517 index_expr.array(),
520
522 {
523 bool all_possible_const=true;
525 {
528 is_const_type(potential_array_expr.type().subtype());
529
531 {
532 // Get the index if we can
533 mp_integer value;
534 if(try_resolve_index_value(index_expr.index(), value))
535 {
537 const exprt &func_expr =
539 bool value_const=false;
540 bool resolved_value=
542
544 {
545 out_expressions.insert(
546 out_expressions.end(),
547 array_out_functions.begin(),
548 array_out_functions.end());
549 }
550 else
551 {
552 LOG("Failed to resolve array value", func_expr);
553 return false;
554 }
555 }
556 else
557 {
558 // We don't know what index it is,
559 // but we know the value is from the array
560 for(const exprt &array_entry : potential_array_expr.operands())
561 {
563 bool is_entry_const;
564 bool resolved_value=
567
568 if(!resolved_value)
569 {
570 LOG("Failed to resolve array value", array_entry);
571 return false;
572 }
573
575 {
577 }
578 }
579 }
580 }
581 else
582 {
583 LOG(
584 "Squashing index of did not result in an array",
586 return false;
587 }
588 }
589
591 return true;
592 }
593 else
594 {
595 LOG("Failed to squash index of to array expression", index_expr);
596 return false;
597 }
598}
599
611 bool &out_is_const)
612{
614 bool is_struct_const;
615
616 // Get the struct it belongs to
617 bool resolved_struct=
621 {
623 {
625 {
627 const exprt &component_value=
630 bool component_const=false;
631 bool resolved=
634 if(resolved)
635 {
636 out_expressions.insert(
637 out_expressions.end(),
638 resolved_expressions.begin(),
640 }
641 else
642 {
643 LOG("Could not resolve component value", component_value);
644 return false;
645 }
646 }
647 else
648 {
649 LOG(
650 "Squashing member access did not resolve in a struct",
652 return false;
653 }
654 }
656 return true;
657 }
658 else
659 {
660 LOG("Failed to squash struct access", member_expr);
661 return false;
662 }
663}
664
678 bool &out_is_const)
679{
680 // We had a pointer, we need to check both the pointer
681 // type can't be changed, and what it what pointing to
682 // can't be changed
684 bool pointer_const;
685 bool resolved=
688 {
689 bool all_objects_const=true;
690 for(const exprt &pointer_val : pointer_values)
691 {
692 if(pointer_val.id()==ID_address_of)
693 {
695 bool object_const=false;
699
701 {
702 out_expressions.insert(
703 out_expressions.end(),
704 out_object_values.begin(),
705 out_object_values.end());
706
708 }
709 else
710 {
711 LOG("Failed to resolve value of a dereference", address_expr);
712 }
713 }
714 else
715 {
716 LOG(
717 "Squashing dereference did not result in an address", pointer_val);
718 return false;
719 }
720 }
722 return true;
723 }
724 else
725 {
726 if(!resolved)
727 {
728 LOG("Failed to resolve pointer of dereference", deref_expr);
729 }
730 else if(!pointer_const)
731 {
732 LOG("Pointer value not const so can't squash", deref_expr);
733 }
734 return false;
735 }
736}
737
748 bool &out_is_const)
749{
751 bool typecast_const;
752 bool resolved=
755
756 if(resolved)
757 {
758 out_expressions.insert(
759 out_expressions.end(),
760 typecast_values.begin(),
761 typecast_values.end());
763 return true;
764 }
765 else
766 {
767 LOG("Could not resolve typecast value", typecast_expr);
768 return false;
769 }
770}
771
776 const exprt &expression) const
777{
778 return is_const_type(expression.type());
779}
780
786{
787 if(type.id() == ID_array)
789 else
790 return type.get_bool(ID_C_constant);
791}
792
800{
802 size_t component_number=
803 struct_type.component_number(member_expr.get_component_name());
804
805 return struct_expr.operands()[component_number];
806}
bool to_integer(const constant_exprt &expr, mp_integer &int_value)
Convert a constant expression expr to an arbitrary-precision integer.
Operator to return the address of an object.
virtual void clear()
Reset the abstract state.
Definition ai.h:267
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:564
const typet & element_type() const
The type of the elements of the array.
Definition std_types.h:777
A constant literal expression.
Definition std_expr.h:2807
Operator to dereference a pointer.
Base class for all expressions.
Definition expr.h:54
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition expr.cpp:64
typet & type()
Return the type of the expression.
Definition expr.h:82
operandst & operands()
Definition expr.h:92
Array index operator.
Definition std_expr.h:1328
bool get_bool(const irep_idt &name) const
Definition irep.cpp:58
const irep_idt & id() const
Definition irep.h:396
Extract member of struct or union.
Definition std_expr.h:2667
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition namespace.cpp:49
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
remove_const_function_pointerst(message_handlert &message_handler, const namespacet &ns, const symbol_tablet &symbol_table)
To take a function call on a function pointer, and if possible resolve it to a small collection of po...
bool try_resolve_function_call(const exprt &expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
bool try_resolve_function_calls(const expressionst &exprs, functionst &out_functions)
To resolve a collection of expressions to the specific function calls they can be.
bool try_resolve_expression(const exprt &expr, expressionst &out_resolved_expression, bool &out_is_const)
To squash various expr types to simplify the expression.
exprt get_component_value(const struct_exprt &struct_expr, const member_exprt &member_expr)
To extract the value of the specific component within a struct.
bool try_resolve_typecast(const typecast_exprt &typecast_expr, expressionst &out_expressions, bool &out_is_const)
To squash a typecast access.
bool try_resolve_dereference_function_call(const dereference_exprt &deref_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
bool try_resolve_member(const member_exprt &member_expr, expressionst &out_expressions, bool &out_is_const)
To squash an member access by first finding the struct it is accessing Then return the squashed value...
bool is_const_type(const typet &type) const
To evaluate the const-ness of the type.
bool try_resolve_index_of_function_call(const index_exprt &index_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
bool try_resolve_index_value(const exprt &index_value_expr, mp_integer &out_array_index)
Given an index into an array, resolve, if possible, the index that is being accessed.
bool operator()(const exprt &base_expression, functionst &out_functions)
To take a function call on a function pointer, and if possible resolve it to a small collection of po...
bool is_const_expression(const exprt &expression) const
To evaluate the const-ness of the expression type.
std::unordered_set< symbol_exprt, irep_hash > functionst
bool try_resolve_address_of_function_call(const address_of_exprt &address_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
bool try_resolve_member_function_call(const member_exprt &member_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
exprt resolve_symbol(const symbol_exprt &symbol_expr) const
Look up a symbol in the symbol table and return its value.
bool try_resolve_index_of(const index_exprt &index_expr, expressionst &out_expressions, bool &out_is_const)
To squash an index access by first finding the array it is accessing Then if the index can be resolve...
bool try_resolve_dereference(const dereference_exprt &deref_expr, expressionst &out_expressions, bool &out_is_const)
To squash a dereference access by first finding the address_of the dereference is dereferencing.
bool try_resolve_typecast_function_call(const typecast_exprt &typecast_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
exprt replace_const_symbols(const exprt &expression) const
To collapse the symbols down to their values where possible This takes a very general approach,...
Struct constructor from list of elements.
Definition std_expr.h:1722
Structure type, corresponds to C style structs.
Definition std_types.h:231
Expression to hold a symbol (variable)
Definition std_expr.h:80
const irep_idt & get_identifier() const
Definition std_expr.h:109
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
The symbol table.
Symbol table entry.
Definition symbol.h:28
typet type
Type of symbol.
Definition symbol.h:31
exprt value
Initial value of symbol.
Definition symbol.h:34
Semantic type conversion.
Definition std_expr.h:1920
The type of an expression, extends irept.
Definition type.h:29
API to expression classes for Pointers.
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
#define LOG(message, irep)
exprt simplify_expr(exprt src, const namespacet &ns)
static optionalt< smt_termt > get_identifier(const exprt &expr, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_handle_identifiers, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_identifiers)
#define PRECONDITION(CONDITION)
Definition invariant.h:463
API to expression classes.
const struct_exprt & to_struct_expr(const exprt &expr)
Cast an exprt to a struct_exprt.
Definition std_expr.h:1745
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition std_expr.h:1391
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast an exprt to a typecast_exprt.
Definition std_expr.h:1954
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition std_expr.h:2751
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition std_expr.h:2840
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:189
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition std_types.h:308
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition std_types.h:832
Author: Diffblue Ltd.