Skip to content

Commit 40831f5

Browse files
smowtonOwen
authored and
Owen
committed
Remove const-cast from value_sett::get_entry
1 parent 35088bf commit 40831f5

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/pointer-analysis/value_set.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ value_sett::entryt *value_sett::find_entry(const value_sett::idt &id)
5757
const value_sett::entryt *value_sett::find_entry(const value_sett::idt &id)
5858
const
5959
{
60-
return const_cast<value_sett *>(this)->find_entry(id);
60+
auto found = values.find(id);
61+
return found == values.end() ? nullptr : &found->second;
6162
}
6263

6364
value_sett::entryt &value_sett::get_entry(const entryt &e, const typet &type)
@@ -371,11 +372,15 @@ static std::string strip_first_field_from_suffix(
371372
return suffix.substr(field.length() + 1);
372373
}
373374

374-
value_sett::entryt *value_sett::get_entry_for_symbol(
375+
template<class maybe_const_value_sett>
376+
auto value_sett::get_entry_for_symbol(
377+
maybe_const_value_sett &value_set,
375378
const irep_idt identifier,
376379
const typet &type,
377380
const std::string &suffix,
378-
const namespacet &ns)
381+
const namespacet &ns) ->
382+
typename std::conditional<std::is_const<maybe_const_value_sett>::value,
383+
const value_sett::entryt *, value_sett::entryt *>::type
379384
{
380385
if(
381386
type.id() != ID_pointer && type.id() != ID_signedbv &&
@@ -393,7 +398,8 @@ value_sett::entryt *value_sett::get_entry_for_symbol(
393398
: type;
394399

395400
// look it up
396-
value_sett::entryt *entry = find_entry(id2string(identifier) + suffix);
401+
auto *entry =
402+
value_set.find_entry(id2string(identifier) + suffix);
397403

398404
// try first component name as suffix if not yet found
399405
if(
@@ -406,29 +412,40 @@ value_sett::entryt *value_sett::get_entry_for_symbol(
406412
const irep_idt &first_component_name =
407413
struct_union_type.components().front().get_name();
408414

409-
entry = find_entry(
415+
entry = value_set.find_entry(
410416
id2string(identifier) + "." + id2string(first_component_name) + suffix);
411417
}
412418

413419
if(!entry)
414420
{
415421
// not found? try without suffix
416-
entry = find_entry(identifier);
422+
entry = value_set.find_entry(identifier);
417423
}
418424

419425
return entry;
420426
}
421427

428+
// Explicitly instantiate the two possible versions of the method above:
429+
430+
value_sett::entryt *value_sett::get_entry_for_symbol(
431+
irep_idt identifier,
432+
const typet &type,
433+
const std::string &suffix,
434+
const namespacet &ns)
435+
{
436+
return get_entry_for_symbol(*this, identifier, type, suffix, ns);
437+
}
438+
422439
const value_sett::entryt *value_sett::get_entry_for_symbol(
423-
const irep_idt identifier,
440+
irep_idt identifier,
424441
const typet &type,
425442
const std::string &suffix,
426443
const namespacet &ns) const
427444
{
428-
return const_cast<value_sett *>(this)->get_entry_for_symbol(
429-
identifier, type, suffix, ns);
445+
return get_entry_for_symbol(*this, identifier, type, suffix, ns);
430446
}
431447

448+
432449
void value_sett::get_value_set_rec(
433450
const exprt &expr,
434451
object_mapt &dest,

src/pointer-analysis/value_set.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Daniel Kroening, [email protected]
1313
#define CPROVER_POINTER_ANALYSIS_VALUE_SET_H
1414

1515
#include <set>
16+
#include <type_traits>
1617
#include <unordered_set>
1718

1819
#include <util/mp_arith.h>
@@ -464,6 +465,19 @@ class value_sett
464465
exprt &expr,
465466
const namespacet &ns) const;
466467

468+
private:
469+
/// Helper method for \ref get_entry_for_symbol
470+
template<class maybe_const_value_sett>
471+
static auto get_entry_for_symbol(
472+
maybe_const_value_sett &value_set,
473+
irep_idt identifier,
474+
const typet &type,
475+
const std::string &suffix,
476+
const namespacet &ns) ->
477+
typename std::conditional<std::is_const<maybe_const_value_sett>::value,
478+
const value_sett::entryt *, value_sett::entryt *>::type;
479+
480+
public:
467481
/// Get the entry for the symbol and suffix
468482
/// \param identifier: The identifier for the symbol
469483
/// \param type: The type of the symbol

0 commit comments

Comments
 (0)