Skip to content

Refactoring around the goto-cc entry point. #5442

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 8 commits into from
Aug 11, 2020

Conversation

thomasspriggs
Copy link
Contributor

As I was working on #5440, I found the entry point code less than straight forward to follow. I carried out this refactorings locally in order to make it easier for me to understand. I am raising this separate PR containing the refactoring work only as it was not required in order to implement the feature in the original PR, but should still improve maintainability.

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • N/A - No user visible behaviour changes. The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • N/A - None claimed My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@thomasspriggs thomasspriggs force-pushed the tas/goto-cc-refactoring branch from 95a56d9 to 49e9b69 Compare August 3, 2020 13:39
@@ -265,6 +265,21 @@ static const char *options_with_arg[]=
nullptr
};

static bool
prefix_in_list(const char *option, const char **list, std::string &prefix)

Choose a reason for hiding this comment

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

I think we could simplify a lot of this if we stored the args in the mode as std::vector<std::string> instead of char **. Then we could also just use regular std::find or something like that instead of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds like a good follow up refactoring.

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.

@@ -14,6 +14,8 @@ Author: Daniel Kroening
#include <cstring>
#include <iostream>

#include "optional.h"

Choose a reason for hiding this comment

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

should be <util/optional.h>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -265,19 +267,18 @@ static const char *options_with_arg[]=
nullptr
};

static bool
prefix_in_list(const char *option, const char **list, std::string &prefix)
static optionalt<std::string>

Choose a reason for hiding this comment

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

Like I said above, I'd prefer not touching this like this for now and instead changing how we store command line arguments, then we don't have to invent our own finds at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think that using a standard container would be a great follow-up refactoring. However I would argue that removing the need for an output parameter is already a step in the right direction, which makes the usages of this function more readable.

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.

@@ -117,3 +117,13 @@ void goto_cc_cmdlinet::add_infile_arg(const std::string &arg)
fclose(tmp);
}
}

bool goto_cc_cmdlinet::have_infile_arg() const

Choose a reason for hiding this comment

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

I think we should probably just make sure to put definitions in cpp files in general.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


Author: Diffblue Ltd

\*******************************************************************/

Choose a reason for hiding this comment

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

I think I mentioned this before but we want to move away from this style of module header... we might have to adjust our linter for that though.

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 have removed this commit from this PR.


#include <cstddef>

struct bv_encodingt

Choose a reason for hiding this comment

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

Moves it into a different namespace though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I'll leave it where it was then.

@@ -11,7 +11,7 @@ Author: Diffblue Ltd

#include <cstddef>

struct bv_encodingt
struct bv_encodingt final

Choose a reason for hiding this comment

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

I'm not sure that's necessary, I find final to be a bit noisy. To me final actually suggests the opposite of what you're saying here: This is part of a class hierarchy, it's just supposed to be at the bottom of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You know that isn't part of a hierarchy from reading this line of code, because no base classes are specified. Having it specified as final also means that you are always safe to pass / return / assign instances of this class by value, without risk of slicing occurring. I do take your point about noise however. I think I may drop this commit as I don't really want to include anything in this PR, where the pros and cons could be argued either way. Suffice to say that my current habit is to make new classes/structs final by default, in the same way as I would make variables and member functions const by default.

const std::string &argument,
const std::size_t pointer_width)
{
const unsigned int object_bits = unsafe_string2unsigned(argument);

Choose a reason for hiding this comment

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

Use string2optional here so you can handle a parse error with a proper error message. The unsafe_ functions are for when you know the parse will succeed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. A non-integer argument will cause an invariant violation, both before and after this PR. This is expected as the PR is intended to be refactoring only, not bug fixing. I agree that we should improve the error handling here.

Making this member function non-virtual makes it easier to follow the
control flow because it is clear it not overridden in any sub class.
This is should be easy enough to add back should a need to override this
arise.
Putting implementations in `.cpp` files is the best-practise because
it reduces compile times.
This avoids the need to declare and mutate iterators. Its states what it
is doing by means of the name of the algorithm - `std::any_of`, which
saves on reading a loop to work it out.
Making this member function non-virtual makes it easier to follow the
control flow because it is clear it not overridden in any sub class.
This is should be easy enough to add back should a need to override this
arise.
This makes the `goto_cc_cmdlinet` class easier to follow because it can
be read without seeing this special case for `armcc`.
This avoids using an output parameter, which makes it more straight
forward to read the data flow.
@thomasspriggs thomasspriggs force-pushed the tas/goto-cc-refactoring branch from 49e9b69 to c562025 Compare August 10, 2020 11:30
@thomasspriggs thomasspriggs changed the title Refactoring around the goto-cc entry point and the object-bits argument. Refactoring around the goto-cc entry point. Aug 10, 2020
Because this gives us compatability with modern C++ features such as
ranged for loops and the algorithms in the standard template library.
Because using a standard container rather than a null terminated array
improves memory safety.

Clang format is off for the lists of options in order to keep the
existing manual formatting. This existing formatting is consistent with
the other lists of options.
@thomasspriggs thomasspriggs force-pushed the tas/goto-cc-refactoring branch from c562025 to 53d609d Compare August 10, 2020 13:32
@thomasspriggs thomasspriggs marked this pull request as ready for review August 10, 2020 13:43
@thomasspriggs thomasspriggs force-pushed the tas/goto-cc-refactoring branch 2 times, most recently from 1f3710f to feacb93 Compare August 10, 2020 16:45
@@ -0,0 +1,32 @@
// Copyright 2020 Diffblue Limited. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the licence header is wrong - thats the internal Diffblue header - CBMC contributions require a different header.

Copy link
Contributor

@chrisr-diffblue chrisr-diffblue left a comment

Choose a reason for hiding this comment

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

All looks good apart from the copyright header - happy to approve once that is fixed up.

To demonstrate that the refactored version works as expected.
@thomasspriggs thomasspriggs force-pushed the tas/goto-cc-refactoring branch from feacb93 to f7787c8 Compare August 11, 2020 15:34
@thomasspriggs thomasspriggs merged commit 7d30335 into diffblue:develop Aug 11, 2020
@thomasspriggs thomasspriggs deleted the tas/goto-cc-refactoring branch August 11, 2020 22:19
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.

3 participants