cprover
does_remove_const.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Analyses
4 
5 Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
11 
12 #include "does_remove_const.h"
13 
15 #include <util/type.h>
16 #include <util/expr.h>
17 #include <util/std_code.h>
18 #include <util/base_type.h>
19 
25  const goto_programt &goto_program,
26  const namespacet &ns):
27  goto_program(goto_program),
28  ns(ns)
29 {}
30 
33 std::pair<bool, source_locationt> does_remove_constt::operator()() const
34 {
35  for(const goto_programt::instructiont &instruction :
37  {
38  if(!instruction.is_assign())
39  {
40  continue;
41  }
42 
43  const code_assignt &assign=to_code_assign(instruction.code);
44  const typet &rhs_type=assign.rhs().type();
45  const typet &lhs_type=assign.lhs().type();
46 
47  // Compare the types recursively for a point where the rhs is more
48  // const that the lhs
49  if(!does_type_preserve_const_correctness(&lhs_type, &rhs_type))
50  {
51  return {true, assign.find_source_location()};
52  }
53 
54  if(does_expr_lose_const(assign.rhs()))
55  {
56  return {true, assign.rhs().find_source_location()};
57  }
58  }
59 
60  return {false, source_locationt()};
61 }
62 
70 {
71  const typet &root_type=expr.type();
72 
73  // Look in each child that has the same base type as the root
74  for(const exprt &op : expr.operands())
75  {
76  const typet &op_type=op.type();
77  if(base_type_eq(op_type, root_type, ns))
78  {
79  // Is this child more const-qualified than the root
80  if(!does_type_preserve_const_correctness(&root_type, &op_type))
81  {
82  return true;
83  }
84  }
85 
86  // Recursively check the children of this child
87  if(does_expr_lose_const(op))
88  {
89  return true;
90  }
91  }
92  return false;
93 }
94 
123  const typet *target_type, const typet *source_type) const
124 {
125  while(target_type->id()==ID_pointer)
126  {
127  bool direct_subtypes_at_least_as_const=
129  target_type->subtype(), source_type->subtype());
130  // We have a pointer to something, but the thing it is pointing to can't be
131  // modified normally, but can through this pointer
132  if(!direct_subtypes_at_least_as_const)
133  return false;
134  // Check the subtypes if they are pointers
135  target_type=&target_type->subtype();
136  source_type=&source_type->subtype();
137  }
138  return true;
139 }
140 
163  const typet &type_more_const, const typet &type_compare) const
164 {
165  return !type_compare.get_bool(ID_C_constant) ||
166  type_more_const.get_bool(ID_C_constant);
167 }
does_remove_constt::ns
const namespacet & ns
Definition: does_remove_const.h:37
typet::subtype
const typet & subtype() const
Definition: type.h:47
typet
The type of an expression, extends irept.
Definition: type.h:29
code_assignt::rhs
exprt & rhs()
Definition: std_code.h:317
does_remove_constt::operator()
std::pair< bool, source_locationt > operator()() const
A naive analysis to look for casts that remove const-ness from pointers.
Definition: does_remove_const.cpp:33
does_remove_constt::does_expr_lose_const
bool does_expr_lose_const(const exprt &expr) const
Search the expression tree to look for any children that have the same base type, but a less strict c...
Definition: does_remove_const.cpp:69
exprt
Base class for all expressions.
Definition: expr.h:53
does_remove_constt::does_type_preserve_const_correctness
bool does_type_preserve_const_correctness(const typet *target_type, const typet *source_type) const
A recursive check that handles when assigning a source value to a target, is the assignment a loss of...
Definition: does_remove_const.cpp:122
type.h
Defines typet, type_with_subtypet and type_with_subtypest.
expr.h
does_remove_constt::is_type_at_least_as_const_as
bool is_type_at_least_as_const_as(const typet &type_more_const, const typet &type_compare) const
A simple check to check the type_more_const is at least as const as type compare.
Definition: does_remove_const.cpp:162
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
irept::get_bool
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:64
base_type.h
Base Type Computation.
goto_programt::instructiont::code
codet code
Do not read or modify directly – use get_X() instead.
Definition: goto_program.h:182
base_type_eq
bool base_type_eq(const typet &type1, const typet &type2, const namespacet &ns)
Check types for equality across all levels of hierarchy.
Definition: base_type.cpp:290
exprt::find_source_location
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
Definition: expr.cpp:231
does_remove_constt::does_remove_constt
does_remove_constt(const goto_programt &goto_program, const namespacet &ns)
A naive analysis to look for casts that remove const-ness from pointers.
Definition: does_remove_const.cpp:24
code_assignt::lhs
exprt & lhs()
Definition: std_code.h:312
irept::id
const irep_idt & id() const
Definition: irep.h:418
std_code.h
goto_program.h
Concrete Goto Program.
source_locationt
Definition: source_location.h:20
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:585
does_remove_const.h
Analyses.
to_code_assign
const code_assignt & to_code_assign(const codet &code)
Definition: std_code.h:383
goto_programt::instructiont::is_assign
bool is_assign() const
Definition: goto_program.h:460
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
exprt::operands
operandst & operands()
Definition: expr.h:95
does_remove_constt::goto_program
const goto_programt & goto_program
Definition: does_remove_const.h:36
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:179