Skip to content

Invariant with diagnostics #2906

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
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
31 changes: 31 additions & 0 deletions regression/invariants/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ class structured_error_testt: public invariant_failedt
}
};

struct DiagnosticA
{
};
struct DiagnosticB
{
};

template <>
struct diagnostics_helpert<DiagnosticA>
{
static std::string diagnostics_as_string(const DiagnosticA &)
{
return "Diagnostic A";
}
};

template <>
struct diagnostics_helpert<DiagnosticB>
{
static std::string diagnostics_as_string(const DiagnosticB &)
{
return "Diagnostic B";
}
};

/// Causes an invariant failure dependent on first argument value.
/// One ignored argument is accepted to conform with the test.pl script,
/// which would be the input source file for other cbmc driver programs.
Expand Down Expand Up @@ -118,6 +143,12 @@ int main(int argc, char** argv)
"diagnostic 2",
"diagnostic 3",
"diagnostic 4");
else if(arg == "invariant-with-custom-diagnostics")
INVARIANT_WITH_DIAGNOSTICS(
false,
"an invariant with some custom diagnostics",
DiagnosticA{},
DiagnosticB{});
else
return 1;
}
11 changes: 11 additions & 0 deletions regression/invariants/invariant-failure19/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
dummy_parameter.c
invariant-with-lots-of-diagnostics
^EXIT=(0|127|134|137)$
^SIGNAL=0$
--- begin invariant violation report ---
Invariant check failed
diagnostic 1
diagnostic 2
diagnostic 3
diagnostic 4
9 changes: 9 additions & 0 deletions regression/invariants/invariant-failure20/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
dummy_parameter.c
invariant-with-custom-diagnostics
^EXIT=(0|127|134|137)$
^SIGNAL=0$
--- begin invariant violation report ---
Invariant check failed
Diagnostic A
Diagnostic B
31 changes: 11 additions & 20 deletions src/util/invariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ class invariant_with_diagnostics_failedt : public invariant_failedt
#else
#define CBMC_NORETURN
#endif
// The macros MACRO<n> (e.g., INVARIANT2) are for internal use in this file
// only. The corresponding macros that take a variable number of arguments (see
// further below) should be used instead, which in turn call those with a fixed
// number of arguments. For example, if INVARIANT(...) is called with two
// arguments, it calls INVARIANT2().

#if defined(CPROVER_INVARIANT_CPROVER_ASSERT)
Copy link
Contributor

Choose a reason for hiding this comment

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

In the following we should also add the new macro INVARIANT_WITH_DIAGNOSTICS, etc. and remove the macros INVARIANT3, etc.

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, thanks!

// Used to allow CPROVER to check itself
Expand Down Expand Up @@ -321,17 +316,17 @@ inline void write_rest_diagnostics(std::ostream &)
template <typename T, typename... Ts>
void write_rest_diagnostics(std::ostream &out, T &&next, Ts &&... rest)
{
out << ", " << diagnostic_as_string(std::forward<T>(next));
out << "\n" << diagnostic_as_string(std::forward<T>(next));
write_rest_diagnostics(out, std::forward<Ts>(rest)...);
}

template <typename T, typename... Ts>
std::string assemble_diagnostics(T &&first, Ts &&... rest)
template <typename... Diagnostics>
std::string assemble_diagnostics(Diagnostics &&... args)
{
std::ostringstream out;
out << "[ " << diagnostic_as_string(std::forward<T>(first));
write_rest_diagnostics(out, std::forward<Ts>(rest)...);
out << " ]";
out << "\n<< EXTRA DIAGNOSTICS >>";
write_rest_diagnostics(out, std::forward<Diagnostics>(args)...);
out << "\n<< END EXTRA DIAGNOSTICS >>";
return out.str();
}
} // namespace detail
Expand Down Expand Up @@ -457,18 +452,14 @@ CBMC_NORETURN void report_invariant_failure(
#define CHECK_RETURN_STRUCTURED(CONDITION, TYPENAME, ...) \
EXPAND_MACRO(INVARIANT_STRUCTURED(CONDITION, TYPENAME, __VA_ARGS__))

// This should be used to mark dead code
/// This should be used to mark dead code
#define UNREACHABLE INVARIANT(false, "Unreachable")
#define UNREACHABLE_STRUCTURED(TYPENAME, ...) \
EXPAND_MACRO(INVARIANT_STRUCTURED(false, TYPENAME, __VA_ARGS__))

// This condition should be used to document that assumptions that are
// made on goto_functions, goto_programs, exprts, etc. being well formed.
// "The data structure is not corrupt or malformed"
#define DATA_INVARIANT2(CONDITION, REASON) INVARIANT2(CONDITION, REASON)
#define DATA_INVARIANT3(CONDITION, REASON, DIAGNOSTICS) \
INVARIANT3(CONDITION, REASON, DIAGNOSTICS)

/// This condition should be used to document that assumptions that are
/// made on goto_functions, goto_programs, exprts, etc. being well formed.
/// "The data structure is not corrupt or malformed"
#define DATA_INVARIANT(CONDITION, REASON) INVARIANT(CONDITION, REASON)
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON, ...) \
INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON, __VA_ARGS__)
Expand All @@ -482,6 +473,6 @@ CBMC_NORETURN void report_invariant_failure(
// to migrate documentation and "error handling" in older code.
#define TODO INVARIANT(false, "Todo")
#define UNIMPLEMENTED INVARIANT(false, "Unimplemented")
#define UNHANDLED_CASE INVARIANT2(false, "Unhandled case")
#define UNHANDLED_CASE INVARIANT(false, "Unhandled case")

#endif // CPROVER_UTIL_INVARIANT_H
2 changes: 1 addition & 1 deletion src/util/source_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ struct diagnostics_helpert<source_locationt>
static std::string
diagnostics_as_string(const source_locationt &source_location)
{
return source_location.as_string();
return "source location: " + source_location.as_string();
}
};

Expand Down