Skip to content

Cleanup error handling of cbmc/ folder #2703

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
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/cbmc/cbmc_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main(int argc, const char **argv)
#endif
cbmc_parse_optionst parse_options(argc, argv);

int res=parse_options.main();
int res = parse_options.main();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unnecessary change?


#ifdef IREP_HASH_STATS
std::cout << "IREP_HASH_CNT=" << irep_hash_cnt << '\n';
Copy link
Member

Choose a reason for hiding this comment

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

indentation

Expand Down
1 change: 1 addition & 0 deletions src/util/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SRC = arith_tools.cpp \
expr.cpp \
expr_initializer.cpp \
expr_util.cpp \
exception_utils.cpp \
file_util.cpp \
find_macros.cpp \
find_symbols.cpp \
Expand Down
20 changes: 20 additions & 0 deletions src/util/exception_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************\

Module: Exception helper utilities

Author: Fotis Koutoulakis, [email protected]

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

#include "exception_utils.h"

std::string invalid_user_input_exceptiont::what() const noexcept
Copy link
Member

Choose a reason for hiding this comment

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

Ideally, there should also be a version hat can serialise into json and XML in a more structure way (in a future PR).

{
std::string res;
res += "\nInvalid User Input\n";
res += "Option: " + option + "\n";
res += "Reason: " + reason;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wishlist: please add a test for this, i.e., intentionally use CBMC with incorrect options.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you need this as part of this PR, or can it go in as part of a follow up PR for example?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Up to you, as long as it happens at some point :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

We plan to make some further improvements to user error handling, so we'll definitely be adding suitable test cases as part of that work, so I'd be happy for this PR to go in, with tests following as part of the later improvements.

// Print an optional correct usage message assuming correct input parameters have been passed
res += correct_input + "\n";
return res;
}
32 changes: 32 additions & 0 deletions src/util/exception_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************\

Module: Exception helper utilities

Author: Fotis Koutoulakis, [email protected]

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

#ifndef CPROVER_UTIL_EXCEPTION_UTILS_H
#define CPROVER_UTIL_EXCEPTION_UTILS_H

#include <string>

class invalid_user_input_exceptiont
{
std::string reason;
std::string option;
std::string correct_input;

public:
invalid_user_input_exceptiont(
std::string reason,
std::string option,
std::string correct_input = "")
: reason(reason), option(option), correct_input(correct_input)
{
}

std::string what() const noexcept;
};

#endif // CPROVER_UTIL_EXCEPTION_UTILS_H
39 changes: 26 additions & 13 deletions src/util/parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Author: Daniel Kroening, [email protected]
#endif

#include "cmdline.h"
#include "exception_utils.h"
#include "exit_codes.h"
#include "signal_catcher.h"

parse_options_baset::parse_options_baset(
Expand Down Expand Up @@ -47,23 +49,34 @@ void parse_options_baset::unknown_option_msg()

int parse_options_baset::main()
{
if(parse_result)
// catch all exceptions here so that this code is not duplicated
// for each tool
try
{
usage_error();
unknown_option_msg();
return EX_USAGE;
if(parse_result)
{
usage_error();
unknown_option_msg();
return EX_USAGE;
}

if(cmdline.isset('?') || cmdline.isset('h') || cmdline.isset("help"))
{
help();
return EX_OK;
}

// install signal catcher
install_signal_catcher();

return doit();
}

if(cmdline.isset('?') || cmdline.isset('h') || cmdline.isset("help"))
catch(invalid_user_input_exceptiont &e)
{
help();
return EX_OK;
std::cerr << e.what() << "\n";
return CPROVER_EXIT_USAGE_ERROR;
}

// install signal catcher
install_signal_catcher();

return doit();
return CPROVER_EXIT_SUCCESS;
}

std::string
Expand Down