Skip to content

Commit

Permalink
Add invariant helper that pretty-prints an irep
Browse files Browse the repository at this point in the history
  • Loading branch information
smowton committed Aug 15, 2017
1 parent c659d13 commit 92f70d6
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
3 changes: 2 additions & 1 deletion regression/invariants/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ SRC = driver.cpp

INCLUDES = -I ../../src

OBJ += ../../src/util/util$(LIBEXT)
OBJ += ../../src/big-int/big-int$(LIBEXT) \
../../src/util/util$(LIBEXT)

include ../../src/config.inc
include ../../src/common
Expand Down
4 changes: 4 additions & 0 deletions regression/invariants/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Author: Chris Smowton, [email protected]
#include <string>
#include <sstream>
#include <util/invariant.h>
#include <util/invariant_utils.h>
#include <util/std_types.h>

/// An example of structured invariants-- this contains fields to
/// describe the error to a catcher, and also produces a human-readable
Expand Down Expand Up @@ -83,6 +85,8 @@ int main(int argc, char** argv)
DATA_INVARIANT_STRUCTURED(false, structured_error_testt, 1, "Structured error"); // NOLINT
else if(arg=="data-invariant-string")
DATA_INVARIANT(false, "Test invariant failure");
else if(arg=="irep")
INVARIANT_WITH_IREP(false, "error with irep", pointer_typet(void_typet()));
else
return 1;
}
14 changes: 14 additions & 0 deletions regression/invariants/invariant-failure13/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CORE
dummy_parameter.c
irep
^EXIT=(0|127|134|137)$
^SIGNAL=0$
--- begin invariant violation report ---
Invariant check failed
error with irep
pointer
0: empty
^(Backtrace)|(Backtraces not supported)$
--
^warning: ignoring
^VERIFICATION SUCCESSFUL$
1 change: 1 addition & 0 deletions src/util/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SRC = arith_tools.cpp \
irep_hash_container.cpp \
irep_ids.cpp \
irep_serialization.cpp \
invariant_utils.cpp \
json.cpp \
json_expr.cpp \
json_irep.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/util/invariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ std::string invariant_failedt::get_invariant_failed_message(
<< " function " << function
<< " line " << line << '\n'
<< "Reason: " << reason
<< "Backtrace:\n"
<< "\nBacktrace:\n"
<< backtrace << '\n';
return out.str();
}
27 changes: 27 additions & 0 deletions src/util/invariant_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************\
Module: Invariant helper utilities
Author: Chris Smowton, [email protected]
\*******************************************************************/

/// \file
/// Invariant helper utilities

#include "invariant_utils.h"

std::string pretty_print_invariant_with_irep(
const irept &problem_node,
const std::string &description)
{
if(problem_node.is_nil())
return description;
else
{
std::string ret=description;
ret+="\nProblem irep:\n";
ret+=problem_node.pretty(0,0);
return ret;
}
}
48 changes: 48 additions & 0 deletions src/util/invariant_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************\
Module: Invariant helper utilities
Author: Chris Smowton, [email protected]
\*******************************************************************/

#ifndef CPROVER_UTIL_INVARIANT_TYPES_H
#define CPROVER_UTIL_INVARIANT_TYPES_H

#include "invariant.h"

#include <util/irep.h>

#include <string>

/// Produces a plain string error description from an irep and some
/// explanatory text. If `problem_node` is nil, returns `description`.
/// \param problem_node: irep to pretty-print
/// \param description: descriptive text to prepend
/// \return error message
std::string pretty_print_invariant_with_irep(
const irept &problem_node,
const std::string &description);

/// Equivalent to
/// `INVARIANT_STRUCTURED(CONDITION, invariant_failedt,
/// pretty_print_invariant_with_irep(IREP, DESCRIPTION))`
#define INVARIANT_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
INVARIANT_STRUCTURED( \
CONDITION, \
invariant_failedt, \
pretty_print_invariant_with_irep((IREP), (DESCRIPTION)))

/// See `INVARIANT_WITH_IREP`
#define PRECONDITION_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))
#define POSTCONDITION_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))
#define CHECK_RETURN_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))
#define UNREACHABLE_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))
#define DATA_INVARIANT_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))

#endif

0 comments on commit 92f70d6

Please sign in to comment.