Skip to content

Replace owning raw pointers with unique_ptr #1029

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

Closed
wants to merge 8 commits into from

Conversation

reuk
Copy link
Contributor

@reuk reuk commented Jun 17, 2017

The same as #835 but targeting test-gen-support.

@reuk reuk force-pushed the no-raw-ptrs-tgs branch 5 times, most recently from a159f00 to 42a2bf5 Compare June 18, 2017 09:10
@reuk reuk force-pushed the no-raw-ptrs-tgs branch from 42a2bf5 to 513b705 Compare June 18, 2017 14:00
@smowton
Copy link
Contributor

smowton commented Jun 19, 2017

  1. Suggest whoever comes to merge this should instead merge in the corresponding master commit and then apply residual changes in the merge, such that the commit graph resembles the true situation and merge decisions are more likely to be taken automatically in the future

  2. Suggest unrelated virtual -> override changes be done in a different PR / commit

  3. I don't understand the purpose of std::forward in freert, could you explain?

Copy link
Contributor

@thk123 thk123 left a comment

Choose a reason for hiding this comment

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

I'm neither well versed enough in C++ memory types or the code in question to approve but I've spotted some inconsistencies in the changes so I've pointed them out

@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
#include <util/json.h>
#include <util/xml.h>
#include <util/expr.h>
#include <util/make_unique.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we need #include <memory>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

util/make_unique.h itself includes memory. My preference would be to leave it as-is, and then if we change to C++14 we can find-and-replace util/make_unique -> memory and util_make_unique -> std::make_unique.

@@ -19,6 +19,7 @@ Date: April 2010
#include <util/endianness_map.h>
#include <util/arith_tools.h>
#include <util/simplify_expr.h>
#include <util/make_unique.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we need #include <memory>

@@ -108,8 +109,8 @@ class rw_range_sett

const range_domaint &get_ranges(objectst::const_iterator it) const
{
assert(dynamic_cast<range_domaint*>(it->second)!=0);
return *static_cast<range_domaint*>(it->second);
assert(dynamic_cast<range_domaint*>(it->second.get()));
Copy link
Contributor

Choose a reason for hiding this comment

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

While you're modifying the line, can this become INVARIANT or possibly one of the others?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not against this, but wonder whether it should really be part of this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

@reuk Yup you're right - it is already a big change

@@ -277,8 +278,8 @@ class rw_guarded_range_set_value_sett:public rw_range_set_value_sett

const guarded_range_domaint &get_ranges(objectst::const_iterator it) const
{
assert(dynamic_cast<guarded_range_domaint*>(it->second)!=0);
return *static_cast<guarded_range_domaint*>(it->second);
assert(dynamic_cast<guarded_range_domaint*>(it->second.get())!=nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

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

While you're modifying the line, can this become INVARIANT or possibly one of the others?

value_setst * value_sets;
is_threadedt * is_threaded;
dirtyt * is_dirty;
std::unique_ptr<value_setst> value_sets;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a #include <memory>


// We offer the option to disable the SAT preprocessor
if(options.get_bool_option("sat-preprocessor"))
auto prop=[this]() -> std::unique_ptr<propt>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this a lambda?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So that prop can be directly initialised to its correct state rather than two-stage constructed-then-initialised. I think this is cleaner than

std::unique_ptr<propt> prop;
// We offer the option to disable the SAT preprocessor
if(options.get_bool_option("sat-preprocessor"))
{
  no_beautification();
  prop=util_make_unique<satcheckt>();
}
else
  prop=util_make_unique<satcheck_no_simplifiert>();

...although there's not much in it, so if you'd prefer it like that then I'll change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't have a strong preference - do have a strong preference that you drop the auto if you do it this way, I assumed on first reading that prop was a lambda

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -247,9 +247,9 @@ bool clobber_parse_optionst::get_goto_program(
return true;
}

languaget *language=get_language_from_filename(filename);
auto language=get_language_from_filename(filename);
Copy link
Contributor

Choose a reason for hiding this comment

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

Type not on RHS so don't think this should be auto


if(language==NULL)
if(language==nullptr)
Copy link
Contributor

Choose a reason for hiding this comment

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

if(!language) to be consistent with earlier changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well spotted :)


if(ptr==NULL)
if(ptr==nullptr)
Copy link
Contributor

Choose a reason for hiding this comment

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

if(!ptr) to be consistent

@@ -28,9 +28,9 @@ static languaget* get_language(
symbol->mode=="")
return get_default_language();

languaget *ptr=get_language_from_mode(symbol->mode);
auto ptr=get_language_from_mode(symbol->mode);
Copy link
Contributor

Choose a reason for hiding this comment

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

Type not on RHS so don't think this should be auto

@reuk
Copy link
Contributor Author

reuk commented Jun 19, 2017

@smowton:

  1. Good idea, but that PR has been open two months and no-one is doing anything about it. This stuff is important, so the plan was to merge it into test-gen-support, which is normally quicker.
  2. Good point, removed from this PR.
  3. For wrappers like this which don't implement any semantic changes, I like to use forward so that the interface for the wrapper exactly matches the interface of the wrapped function.

@reuk reuk force-pushed the no-raw-ptrs-tgs branch from caee053 to 1a6249c Compare June 19, 2017 10:56
@smowton
Copy link
Contributor

smowton commented Jun 19, 2017

Let's try to aggrevate for the master PR tomorrow in the interest of less merge chaff downstream

@reuk
Copy link
Contributor Author

reuk commented Jun 19, 2017

Updated to include <memory> as requested.

@reuk
Copy link
Contributor Author

reuk commented Jun 20, 2017

Should I apply the changes from this PR to the original one?

src/util/freer.h Outdated
@@ -12,7 +12,7 @@ Author: Reuben Thomas, [email protected]
#include <cstdlib>
#include <utility>

struct freert final
struct freert
{
template <typename T>
void operator()(T &&t) const
Copy link
Contributor

Choose a reason for hiding this comment

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

You can still have final on member function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not a virtual method, therefore putting final on it causes a compiler error.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine with me, I was not really a fan of putting final in there in the first place.

@thk123
Copy link
Contributor

thk123 commented Jun 20, 2017

@reuk I suppose so (sorry for not reviewing the original one - that would have been more helpful)

@reuk reuk mentioned this pull request Jun 20, 2017
@@ -750,19 +750,17 @@ void cbmc_parse_optionst::preprocessing()
return;
}

languaget *ptr=get_language_from_filename(filename);
std::unique_ptr<languaget> ptr=get_language_from_filename(filename);
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer calling it language instead of ptr


// the following never gets deleted
const char **argv_narrow=new const char *[argc+1];
std::vector<const char *> argv_narrow(argc+1);
argv_narrow[argc]=0;

for(int i=0; i<argc; i++)
Copy link
Contributor

@janmroczkowski janmroczkowski Jun 21, 2017

Choose a reason for hiding this comment

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

While the above fixes array leak, it still leaks elements.
Maybe change from char* to std::string or unique pointer would help.

@reuk reuk force-pushed the no-raw-ptrs-tgs branch from 80f7b89 to 92e02b5 Compare June 21, 2017 13:06
@peterschrammel
Copy link
Member

peterschrammel commented Jun 25, 2017

Let's get #835 merged into master first.

@reuk reuk closed this Jul 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants