Skip to content

Introduce expression/type formatting functor #2192

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/analyses/constant_propagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Author: Peter Schrammel

#ifdef DEBUG
#include <iostream>
#include <util/format_expr.h>
#include <util/formatter.h>
#endif

#include <util/ieee_float.h>
Expand Down Expand Up @@ -223,7 +223,7 @@ bool constant_propagator_domaint::two_way_propagate_rec(
const namespacet &ns)
{
#ifdef DEBUG
std::cout << "two_way_propagate_rec: " << format(expr) << '\n';
std::cout << "two_way_propagate_rec: " << debug_formatter(expr) << '\n';
#endif

bool change=false;
Expand Down
1 change: 1 addition & 0 deletions src/ansi-c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ SRC = anonymous_member.cpp \
ansi_c_convert_type.cpp \
ansi_c_declaration.cpp \
ansi_c_entry_point.cpp \
ansi_c_formatter.cpp \
ansi_c_internal_additions.cpp \
ansi_c_language.cpp \
ansi_c_lex.yy.cpp \
Expand Down
29 changes: 29 additions & 0 deletions src/ansi-c/ansi_c_formatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************\

Module:

Author: Daniel Kroening, [email protected]

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

#include "ansi_c_formatter.h"

#include "expr2c.h"

#include <ostream>

std::ostream &ansi_c_formattert::format(std::ostream &os, const exprt &expr)
{
return os << expr2c(expr, ns);
}

std::ostream &ansi_c_formattert::format(std::ostream &os, const typet &type)
{
return os << type2c(type, ns);
}

std::ostream &
ansi_c_formattert::format(std::ostream &os, const source_locationt &loc)
{
return os << loc;
}
28 changes: 28 additions & 0 deletions src/ansi-c/ansi_c_formatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************\

Module:

Author: Daniel Kroening, [email protected]

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

#ifndef CPROVER_ANSI_C_FORMATTER_H
#define CPROVER_ANSI_C_FORMATTER_H

#include <util/formatter.h>

class ansi_c_formattert : public formattert
{
public:
explicit ansi_c_formattert(const namespacet &_ns) : ns(_ns)
{
}

std::ostream &format(std::ostream &, const exprt &) override;
std::ostream &format(std::ostream &, const typet &) override;
std::ostream &format(std::ostream &, const source_locationt &) override;

const namespacet &ns;
};

#endif // CPROVER_ANSI_C_FORMATTER_H
5 changes: 4 additions & 1 deletion src/cbmc/cbmc_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Author: Daniel Kroening, [email protected]
#include <langapi/language.h>

#include <ansi-c/c_preprocess.h>
#include <ansi-c/ansi_c_formatter.h>

#include <goto-programs/adjust_float_expressions.h>
#include <goto-programs/initialize_goto_model.h>
Expand Down Expand Up @@ -595,7 +596,9 @@ int cbmc_parse_optionst::get_goto_program(

if(cmdline.isset("show-symbol-table"))
{
show_symbol_table(goto_model, ui_message_handler.get_ui());
namespacet ns(goto_model.symbol_table);
ansi_c_formattert formatter(ns);
show_symbol_table(goto_model, ui_message_handler.get_ui(), formatter);
return CPROVER_EXIT_SUCCESS;
}

Expand Down
1 change: 1 addition & 0 deletions src/cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SRC = cpp_constructor.cpp \
cpp_destructor.cpp \
cpp_enum_type.cpp \
cpp_exception_id.cpp \
cpp_formatter.cpp \
cpp_id.cpp \
cpp_instantiate_template.cpp \
cpp_internal_additions.cpp \
Expand Down
29 changes: 29 additions & 0 deletions src/cpp/cpp_formatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************\

Module:

Author: Daniel Kroening, [email protected]

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

#include "cpp_formatter.h"

#include "expr2cpp.h"

#include <ostream>

std::ostream &cpp_formattert::format(std::ostream &os, const exprt &expr)
{
return os << expr2cpp(expr, ns);
}

std::ostream &cpp_formattert::format(std::ostream &os, const typet &type)
{
return os << type2cpp(type, ns);
}

std::ostream &
cpp_formattert::format(std::ostream &os, const source_locationt &loc)
{
return os << loc;
}
28 changes: 28 additions & 0 deletions src/cpp/cpp_formatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************\

Module:

Author: Daniel Kroening, [email protected]

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

#ifndef CPROVER_CPP_FORMATTER_H
#define CPROVER_CPP_FORMATTER_H

#include <util/formatter.h>

class cpp_formattert : public formattert
{
public:
explicit cpp_formattert(const namespacet &_ns) : ns(_ns)
{
}

std::ostream &format(std::ostream &, const exprt &) override;
std::ostream &format(std::ostream &, const typet &) override;
std::ostream &format(std::ostream &, const source_locationt &) override;

const namespacet &ns;
};

#endif // CPROVER_CPP_FORMATTER_H
6 changes: 5 additions & 1 deletion src/goto-instrument/goto_instrument_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Author: Daniel Kroening, [email protected]
#include <util/json.h>
#include <util/exit_codes.h>

#include <ansi-c/ansi_c_formatter.h>

#include <goto-programs/class_hierarchy.h>
#include <goto-programs/goto_convert_functions.h>
#include <goto-programs/remove_calls_no_body.h>
Expand Down Expand Up @@ -463,7 +465,9 @@ int goto_instrument_parse_optionst::doit()

if(cmdline.isset("show-symbol-table"))
{
::show_symbol_table(goto_model, get_ui());
namespacet ns(goto_model.symbol_table);
ansi_c_formattert formatter(ns);
::show_symbol_table(goto_model, get_ui(), formatter);
return CPROVER_EXIT_SUCCESS;
}

Expand Down
10 changes: 5 additions & 5 deletions src/goto-programs/generate_function_bodies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Author: Diffblue Ltd.
#include "generate_function_bodies.h"

#include <util/arith_tools.h>
#include <util/format_expr.h>
#include <util/formatter.h>
#include <util/make_unique.h>
#include <util/string_utils.h>

Expand Down Expand Up @@ -102,7 +102,7 @@ class assert_false_generate_function_bodiest : public generate_function_bodiest
const namespacet ns(symbol_table);
std::ostringstream comment_stream;
comment_stream << id2string(ID_assertion) << " "
<< format(assert_instruction->guard);
<< debug_formatter(assert_instruction->guard);
assert_instruction->source_location.set_comment(comment_stream.str());
assert_instruction->source_location.set_property_class(ID_assertion);
auto end_instruction = add_instruction();
Expand Down Expand Up @@ -134,7 +134,7 @@ class assert_false_then_assume_false_generate_function_bodiest
const namespacet ns(symbol_table);
std::ostringstream comment_stream;
comment_stream << id2string(ID_assertion) << " "
<< format(assert_instruction->guard);
<< debug_formatter(assert_instruction->guard);
assert_instruction->source_location.set_comment(comment_stream.str());
assert_instruction->source_location.set_property_class(ID_assertion);
auto assume_instruction = add_instruction();
Expand Down Expand Up @@ -203,7 +203,7 @@ class havoc_generate_function_bodiest : public generate_function_bodiest,
bool constant_known_array_size = lhs_array_type.size().is_constant();
if(!constant_known_array_size)
{
warning() << "Cannot havoc non-const array " << format(lhs)
warning() << "Cannot havoc non-const array " << debug_formatter(lhs)
<< " in function " << function_name
<< ": Unknown array size" << eom;
}
Expand All @@ -219,7 +219,7 @@ class havoc_generate_function_bodiest : public generate_function_bodiest,
// Pretty much the same thing as a unknown size array
// Technically not allowed by the C standard, but we model
// unknown size arrays in structs this way
warning() << "Cannot havoc non-const array " << format(lhs)
warning() << "Cannot havoc non-const array " << debug_formatter(lhs)
<< " in function " << function_name << ": Array size 0"
<< eom;
}
Expand Down
8 changes: 4 additions & 4 deletions src/goto-programs/goto_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Author: Daniel Kroening
#include <ostream>

#include <util/arith_tools.h>
#include <util/format_expr.h>
#include <util/formatter.h>
#include <util/symbol.h>

#include <ansi-c/printf_formatter.h>
Expand Down Expand Up @@ -74,8 +74,8 @@ void goto_trace_stept::output(

if(is_assignment())
{
out << " " << format(full_lhs)
<< " = " << format(full_lhs_value)
out << " " << debug_formatter(full_lhs)
<< " = " << debug_formatter(full_lhs_value)
<< '\n';
}

Expand All @@ -95,7 +95,7 @@ void goto_trace_stept::output(
if(!comment.empty())
out << " " << comment << '\n';

out << " " << format(pc->guard) << '\n';
out << " " << debug_formatter(pc->guard) << '\n';
out << '\n';
}
}
Expand Down
Loading