Skip to content

Replace owning raw pointers #835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/analyses/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ Author: Daniel Kroening, [email protected]
#ifndef CPROVER_ANALYSES_AI_H
#define CPROVER_ANALYSES_AI_H

#include <map>
#include <iosfwd>
#include <map>
#include <memory>

#include <util/json.h>
#include <util/xml.h>
#include <util/expr.h>
#include <util/make_unique.h>

#include <goto-programs/goto_model.h>

Expand Down Expand Up @@ -334,7 +336,7 @@ class ai_baset
const namespacet &ns)=0;
virtual statet &get_state(locationt l)=0;
virtual const statet &find_state(locationt l) const=0;
virtual statet* make_temporary_state(const statet &s)=0;
virtual std::unique_ptr<statet> make_temporary_state(const statet &s)=0;
};

// domainT is expected to be derived from ai_domain_baseT
Expand Down Expand Up @@ -400,9 +402,9 @@ class ait:public ai_baset
static_cast<const domainT &>(src), from, to);
}

statet *make_temporary_state(const statet &s) override
std::unique_ptr<statet> make_temporary_state(const statet &s) override
{
return new domainT(static_cast<const domainT &>(s));
return util_make_unique<domainT>(static_cast<const domainT &>(s));
}

void fixedpoint(
Expand Down
41 changes: 23 additions & 18 deletions src/analyses/goto_rw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ Date: April 2010

#include "goto_rw.h"

#include <limits>
#include <algorithm>
#include <limits>
#include <memory>

#include <util/std_code.h>
#include <util/std_expr.h>
Expand All @@ -20,6 +21,7 @@ Date: April 2010
#include <util/endianness_map.h>
#include <util/arith_tools.h>
#include <util/simplify_expr.h>
#include <util/make_unique.h>

#include <goto-programs/goto_functions.h>

Expand Down Expand Up @@ -49,12 +51,12 @@ rw_range_sett::~rw_range_sett()
for(rw_range_sett::objectst::iterator it=r_range_set.begin();
it!=r_range_set.end();
++it)
delete it->second;
it->second=nullptr;

for(rw_range_sett::objectst::iterator it=w_range_set.begin();
it!=w_range_set.end();
++it)
delete it->second;
it->second=nullptr;
}

void rw_range_sett::output(std::ostream &out) const
Expand Down Expand Up @@ -461,16 +463,18 @@ void rw_range_sett::add(
const range_spect &range_start,
const range_spect &range_end)
{
objectst::iterator entry=(mode==get_modet::LHS_W ? w_range_set : r_range_set).
insert(
std::pair<const irep_idt&, range_domain_baset*>(
identifier, nullptr)).first;
objectst::iterator entry=
(mode==get_modet::LHS_W?w_range_set:r_range_set)
.insert(
std::pair<const irep_idt &, std::unique_ptr<range_domain_baset>>(
identifier, nullptr))
.first;

if(entry->second==nullptr)
entry->second=new range_domaint();
entry->second=util_make_unique<range_domaint>();

static_cast<range_domaint*>(entry->second)->push_back(
std::make_pair(range_start, range_end));
static_cast<range_domaint&>(*entry->second).push_back(
{range_start, range_end});
}

void rw_range_sett::get_objects_rec(
Expand Down Expand Up @@ -662,17 +666,18 @@ void rw_guarded_range_set_value_sett::add(
const range_spect &range_start,
const range_spect &range_end)
{
objectst::iterator entry=(mode==get_modet::LHS_W ? w_range_set : r_range_set).
insert(
std::pair<const irep_idt&, range_domain_baset*>(
identifier, nullptr)).first;
objectst::iterator entry=
(mode==get_modet::LHS_W?w_range_set:r_range_set)
.insert(
std::pair<const irep_idt &, std::unique_ptr<range_domain_baset>>(
identifier, nullptr))
.first;

if(entry->second==nullptr)
entry->second=new guarded_range_domaint();
entry->second=util_make_unique<guarded_range_domaint>();

static_cast<guarded_range_domaint*>(entry->second)->insert(
std::make_pair(range_start,
std::make_pair(range_end, guard.as_expr())));
static_cast<guarded_range_domaint&>(*entry->second).insert(
{range_start, {range_end, guard.as_expr()}});
}

void goto_rw(goto_programt::const_targett target,
Expand Down
16 changes: 9 additions & 7 deletions src/analyses/goto_rw.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Date: April 2010
#include <map>
#include <ostream>
#include <limits>
#include <memory> // unique_ptr

#include <util/guard.h>

Expand Down Expand Up @@ -83,10 +84,10 @@ class rw_range_sett
{
public:
#ifdef USE_DSTRING
typedef std::map<irep_idt, range_domain_baset*> objectst;
typedef std::map<irep_idt, std::unique_ptr<range_domain_baset>> objectst;
#else
typedef std::unordered_map<irep_idt, range_domain_baset*, string_hash>
objectst;
typedef std::unordered_map<
irep_idt, std::unique_ptr<range_domain_baset>, string_hash> objectst;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself: no idea why I've used string_hash instead of irep_id_hash here. I'll git grep and cleanup, no need for you to make any changes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget about that one, btw, it's for the !USE_DSTRING case, and thus makes sense.

#endif

virtual ~rw_range_sett();
Expand All @@ -108,8 +109,8 @@ class rw_range_sett

const range_domaint &get_ranges(objectst::const_iterator it) const
{
PRECONDITION(dynamic_cast<range_domaint*>(it->second)!=nullptr);
return *static_cast<range_domaint*>(it->second);
PRECONDITION(dynamic_cast<range_domaint*>(it->second.get())!=nullptr);
return static_cast<const range_domaint &>(*it->second);
}

enum class get_modet { LHS_W, READ };
Expand Down Expand Up @@ -277,8 +278,9 @@ class rw_guarded_range_set_value_sett:public rw_range_set_value_sett

const guarded_range_domaint &get_ranges(objectst::const_iterator it) const
{
PRECONDITION(dynamic_cast<guarded_range_domaint*>(it->second)!=nullptr);
return *static_cast<guarded_range_domaint*>(it->second);
PRECONDITION(
dynamic_cast<guarded_range_domaint*>(it->second.get())!=nullptr);
return static_cast<const guarded_range_domaint &>(*it->second);
}

virtual void get_objects_rec(
Expand Down
6 changes: 3 additions & 3 deletions src/analyses/local_may_alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ Author: Daniel Kroening, [email protected]
#ifndef CPROVER_ANALYSES_LOCAL_MAY_ALIAS_H
#define CPROVER_ANALYSES_LOCAL_MAY_ALIAS_H

#include <stack>
#include <memory>
#include <stack>

#include <util/union_find.h>
#include <util/make_unique.h>

#include "locals.h"
#include "dirty.h"
Expand Down Expand Up @@ -117,8 +118,7 @@ class local_may_alias_factoryt
goto_functionst::function_mapt::const_iterator f_it2=
goto_functions->function_map.find(fkt);
assert(f_it2!=goto_functions->function_map.end());
return *(fkt_map[fkt]=std::unique_ptr<local_may_aliast>(
new local_may_aliast(f_it2->second)));
return *(fkt_map[fkt]=util_make_unique<local_may_aliast>(f_it2->second));
}

local_may_aliast &operator()(goto_programt::const_targett t)
Expand Down
30 changes: 16 additions & 14 deletions src/analyses/reaching_definitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@ Date: February 2013

#include "reaching_definitions.h"

#include <memory>

#include <util/pointer_offset_size.h>
#include <util/prefix.h>
#include <util/make_unique.h>

#include <pointer-analysis/value_set_analysis_fi.h>

#include "is_threaded.h"
#include "dirty.h"

reaching_definitions_analysist::reaching_definitions_analysist(
const namespacet &_ns):
concurrency_aware_ait<rd_range_domaint>(),
ns(_ns)
{
}

reaching_definitions_analysist::~reaching_definitions_analysist()=default;

void rd_range_domaint::populate_cache(const irep_idt &identifier) const
{
assert(bv_container);
Expand Down Expand Up @@ -717,26 +729,16 @@ const rd_range_domaint::ranges_at_loct &rd_range_domaint::get(
return entry->second;
}

reaching_definitions_analysist::~reaching_definitions_analysist()
{
if(is_dirty)
delete is_dirty;
if(is_threaded)
delete is_threaded;
if(value_sets)
delete value_sets;
}

void reaching_definitions_analysist::initialize(
const goto_functionst &goto_functions)
{
value_set_analysis_fit *value_sets_=new value_set_analysis_fit(ns);
auto value_sets_=util_make_unique<value_set_analysis_fit>(ns);
(*value_sets_)(goto_functions);
value_sets=value_sets_;
value_sets=std::move(value_sets_);

is_threaded=new is_threadedt(goto_functions);
is_threaded=util_make_unique<is_threadedt>(goto_functions);

is_dirty=new dirtyt(goto_functions);
is_dirty=util_make_unique<dirtyt>(goto_functions);

concurrency_aware_ait<rd_range_domaint>::initialize(goto_functions);
}
15 changes: 4 additions & 11 deletions src/analyses/reaching_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,7 @@ class reaching_definitions_analysist:
{
public:
// constructor
explicit reaching_definitions_analysist(const namespacet &_ns):
concurrency_aware_ait<rd_range_domaint>(),
ns(_ns),
value_sets(nullptr),
is_threaded(nullptr),
is_dirty(nullptr)
{
}
explicit reaching_definitions_analysist(const namespacet &_ns);

virtual ~reaching_definitions_analysist();

Expand Down Expand Up @@ -290,9 +283,9 @@ class reaching_definitions_analysist:

protected:
const namespacet &ns;
value_setst * value_sets;
is_threadedt * is_threaded;
dirtyt * is_dirty;
std::unique_ptr<value_setst> value_sets;
std::unique_ptr<is_threadedt> is_threaded;
std::unique_ptr<dirtyt> is_dirty;
};

#endif // CPROVER_ANALYSES_REACHING_DEFINITIONS_H
11 changes: 7 additions & 4 deletions src/analyses/static_analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ Author: Daniel Kroening, [email protected]
#error Deprecated, use ai.h instead
#endif

#include <map>
#include <iosfwd>
#include <map>
#include <memory>
#include <unordered_set>

#include <util/make_unique.h>

#include <goto-programs/goto_functions.h>

// don't use me -- I am just a base class
Expand Down Expand Up @@ -252,7 +255,7 @@ class static_analysis_baset
virtual void generate_state(locationt l)=0;
virtual statet &get_state(locationt l)=0;
virtual const statet &get_state(locationt l) const=0;
virtual statet* make_temporary_state(statet &s)=0;
virtual std::unique_ptr<statet> make_temporary_state(statet &s)=0;

typedef domain_baset::expr_sett expr_sett;

Expand Down Expand Up @@ -331,9 +334,9 @@ class static_analysist:public static_analysis_baset
return static_cast<T &>(a).merge(static_cast<const T &>(b), to);
}

virtual statet *make_temporary_state(statet &s)
virtual std::unique_ptr<statet> make_temporary_state(statet &s)
{
return new T(static_cast<T &>(s));
return util_make_unique<T>(static_cast<T &>(s));
}

virtual void generate_state(locationt l)
Expand Down
4 changes: 2 additions & 2 deletions src/ansi-c/ansi_c_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ void ansi_c_languaget::show_parse(std::ostream &out)
parse_tree.output(out);
}

languaget *new_ansi_c_language()
std::unique_ptr<languaget> new_ansi_c_language()
{
return new ansi_c_languaget;
return util_make_unique<ansi_c_languaget>();
}

bool ansi_c_languaget::from_expr(
Expand Down
11 changes: 8 additions & 3 deletions src/ansi-c/ansi_c_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ Author: Daniel Kroening, [email protected]
#ifndef CPROVER_ANSI_C_ANSI_C_LANGUAGE_H
#define CPROVER_ANSI_C_ANSI_C_LANGUAGE_H

/*! \defgroup gr_ansi_c ANSI-C front-end */

#include <memory>

#include <util/language.h>
#include <util/make_unique.h>

#include "ansi_c_parse_tree.h"

Expand Down Expand Up @@ -59,8 +64,8 @@ class ansi_c_languaget:public languaget
exprt &expr,
const namespacet &ns) override;

languaget *new_language() override
{ return new ansi_c_languaget; }
std::unique_ptr<languaget> new_language() override
{ return util_make_unique<ansi_c_languaget>(); }

std::string id() const override { return "C"; }
std::string description() const override { return "ANSI-C 99"; }
Expand All @@ -73,6 +78,6 @@ class ansi_c_languaget:public languaget
std::string parse_path;
};

languaget *new_ansi_c_language();
std::unique_ptr<languaget> new_ansi_c_language();

#endif // CPROVER_ANSI_C_ANSI_C_LANGUAGE_H
6 changes: 3 additions & 3 deletions src/cbmc/bmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ safety_checkert::resultt bmct::run(
std::unique_ptr<memory_model_baset> memory_model;

if(mm.empty() || mm=="sc")
memory_model=std::unique_ptr<memory_model_baset>(new memory_model_sct(ns));
memory_model=util_make_unique<memory_model_sct>(ns);
else if(mm=="tso")
memory_model=std::unique_ptr<memory_model_baset>(new memory_model_tsot(ns));
memory_model=util_make_unique<memory_model_tsot>(ns);
else if(mm=="pso")
memory_model=std::unique_ptr<memory_model_baset>(new memory_model_psot(ns));
memory_model=util_make_unique<memory_model_psot>(ns);
else
{
error() << "Invalid memory model " << mm
Expand Down
4 changes: 3 additions & 1 deletion src/cbmc/cbmc_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ extern unsigned long long irep_cmp_ne_cnt;
#ifdef _MSC_VER
int wmain(int argc, const wchar_t **argv_wide)
{
const char **argv=narrow_argv(argc, argv_wide);
auto vec=narrow_argv(argc, argv_wide);
auto narrow=to_c_str_array(std::begin(vec), std::end(vec));
auto argv=narrow.data();
#else
int main(int argc, const char **argv)
{
Expand Down
Loading