Skip to content

Commit

Permalink
Added utility class to convert strings into expressions
Browse files Browse the repository at this point in the history
In turns the error return state into a CATCH exception so the test will
fail without cluttering tests with checks on the flag when it is just setup
code for the actual test.
  • Loading branch information
thk123 committed Jul 18, 2017
1 parent 0047733 commit 3185028
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Source files for test utilities
SRC = src/expr/require_expr.cpp \
src/ansi-c/c_to_expr.cpp \
# Empty last line

# Test source files
Expand Down
35 changes: 35 additions & 0 deletions unit/src/ansi-c/c_to_expr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************\
Module: Unit test utilities
Author: DiffBlue Limited. All rights reserved.
\*******************************************************************/

/// \file
/// Utility for converting strings in to exprt, throwing a CATCH exception
/// if this fails in any way.
///
#include "c_to_expr.h"

#include <catch.hpp>

c_to_exprt::c_to_exprt():
message_handler(
std::unique_ptr<message_handlert>(new ui_message_handlert()))
{
language.set_message_handler(*message_handler);
}

/// Take an input string that should be a valid C rhs expression
/// \param input_string: The string to convert
/// \param ns: The global namespace
/// \return: A constructed expr representing the string
exprt c_to_exprt::operator()(
const std::string &input_string, const namespacet &ns)
{
exprt expr;
bool result=language.to_expr(input_string, "", expr, ns);
REQUIRE(!result);
return expr;
}
35 changes: 35 additions & 0 deletions unit/src/ansi-c/c_to_expr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************\
Module: Unit test utilities
Author: DiffBlue Limited. All rights reserved.
\*******************************************************************/

/// \file
/// Utility for converting strings in to exprt, throwing a CATCH exception
/// if this fails in any way.

#ifndef CPROVER_SRC_ANSI_C_C_TO_EXPR_H
#define CPROVER_SRC_ANSI_C_C_TO_EXPR_H

#include <memory>

#include <util/expr.h>
#include <util/message.h>
#include <util/ui_message.h>
#include <util/namespace.h>
#include <ansi-c/ansi_c_language.h>

class c_to_exprt
{
public:
c_to_exprt();
exprt operator()(const std::string &input_string, const namespacet &ns);

private:
std::unique_ptr<message_handlert> message_handler;
ansi_c_languaget language;
};

#endif // CPROVER_SRC_ANSI_C_C_TO_EXPR_H

0 comments on commit 3185028

Please sign in to comment.