Skip to content

Commit 30cc479

Browse files
Petr BauchPetr Bauch
Petr Bauch
authored and
Petr Bauch
committed
Small rework to allow further invariant checking
This commit follows the style of PR #918 in terms of case analysis of goto instructions. The errors are now directly send to messaget instance and source location are added. No functionality was added, just a different form.
1 parent 0f2ee10 commit 30cc479

File tree

5 files changed

+72
-42
lines changed

5 files changed

+72
-42
lines changed

src/cbmc/cbmc_parse_options.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ int cbmc_parse_optionst::doit()
517517
return get_goto_program_ret;
518518

519519
INVARIANT(
520-
goto_model.check_id_consistency_symbol_table(),
521-
"identifier inconsistency between goto program and symbol table");
520+
!goto_model.check_internal_invariants(*this),
521+
"goto program internal invariants failed");
522522

523523
if(cmdline.isset("show-claims") || // will go away
524524
cmdline.isset("show-properties")) // use this one

src/goto-programs/goto_function.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,10 @@ class goto_functiont
111111
return *this;
112112
}
113113

114-
void check_id_consistency_symbol_table(
115-
const symbol_tablet &table,
116-
std::vector<std::string> &message_collector) const
114+
bool
115+
check_internal_invariants(const symbol_tablet &table, messaget &msg) const
117116
{
118-
body.check_id_consistency_symbol_table(table, message_collector);
117+
return body.check_internal_invariants(table, msg);
119118
}
120119
};
121120

src/goto-programs/goto_model.h

+10-19
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include <util/symbol_table.h>
1616
#include <util/journalling_symbol_table.h>
17+
#include <util/message.h>
1718

1819
#include "abstract_goto_model.h"
1920
#include "goto_functions.h"
2021

21-
#include <iostream>
22-
2322
// A model is a pair consisting of a symbol table
2423
// and the CFGs for the functions.
2524

@@ -98,27 +97,19 @@ class goto_modelt : public abstract_goto_modelt
9897
goto_functions.function_map.end();
9998
}
10099

101-
/// Iterates over the symbols in this goto-model and checks that each one is
102-
/// present in the symbol table.
103-
/// \return true if no inconsistency was found
104-
bool check_id_consistency_symbol_table() const
100+
/// Iterates over the functions inside the goto model and checks invariants
101+
/// in all of them. Prints out error message collected.
102+
/// \param msg message instance to collect errors
103+
/// \return true if any violation was found
104+
bool check_internal_invariants(messaget &msg) const
105105
{
106-
std::vector<std::string> message_collector;
106+
bool found_violation = false;
107107
forall_goto_functions(it, goto_functions)
108108
{
109-
it->second.check_id_consistency_symbol_table(
110-
symbol_table, message_collector);
111-
}
112-
if(!message_collector.empty())
113-
{
114-
std::cerr << "Error messages collected while checking the goto "
115-
"program/symbol table\n consistency:\n";
116-
for(const auto &m : message_collector)
117-
std::cerr << m << std::endl;
118-
return false;
109+
found_violation = found_violation ||
110+
it->second.check_internal_invariants(symbol_table, msg);
119111
}
120-
else
121-
return true;
112+
return found_violation;
122113
}
123114
};
124115

src/goto-programs/goto_program.cpp

+50-10
Original file line numberDiff line numberDiff line change
@@ -668,26 +668,63 @@ bool goto_programt::instructiont::equals(const instructiont &other) const
668668
// clang-format on
669669
}
670670

671-
void goto_programt::instructiont::check_id_consistency_symbol_table(
671+
bool goto_programt::instructiont::check_internal_invariants(
672672
const symbol_tablet &table,
673-
std::vector<std::string> &message_collector) const
673+
messaget &msg) const
674674
{
675+
bool found_violation = false;
676+
std::vector<std::string> id_collector;
675677
auto symbol_finder = [&](const exprt &e) {
676678
if(e.id() == ID_symbol)
677679
{
678680
auto symbol_expr = to_symbol_expr(e);
679681
if(!table.has_symbol(symbol_expr.get_identifier()))
680-
message_collector.push_back(
681-
id2string(symbol_expr.get_identifier()) + " not found");
682+
id_collector.push_back(id2string(symbol_expr.get_identifier()));
682683
}
683684
};
684685

685-
code.visit(symbol_finder);
686686
if(!table.has_symbol(function))
687+
id_collector.push_back(id2string(function));
688+
689+
switch(type)
690+
{
691+
case GOTO:
692+
case ASSUME:
693+
case ASSERT:
694+
guard.visit(symbol_finder);
695+
break;
696+
case ASSIGN:
697+
case DECL:
698+
case DEAD:
699+
case FUNCTION_CALL:
700+
code.visit(symbol_finder);
701+
break;
702+
case OTHER:
703+
case SKIP:
704+
case LOCATION:
705+
case END_FUNCTION:
706+
case START_THREAD:
707+
case END_THREAD:
708+
case ATOMIC_BEGIN:
709+
case ATOMIC_END:
710+
case RETURN:
711+
case THROW:
712+
case CATCH:
713+
case NO_INSTRUCTION_TYPE:
714+
case INCOMPLETE_GOTO:
715+
break;
716+
}
717+
718+
if(!id_collector.empty())
687719
{
688-
message_collector.push_back("function id not found");
720+
for(const auto &id : id_collector)
721+
{
722+
msg.error() << id << " not found (" << source_location << ")"
723+
<< messaget::eom;
724+
}
725+
found_violation = true;
689726
}
690-
guard.visit(symbol_finder);
727+
return found_violation;
691728
}
692729

693730
bool goto_programt::equals(const goto_programt &other) const
@@ -721,14 +758,17 @@ bool goto_programt::equals(const goto_programt &other) const
721758
return true;
722759
}
723760

724-
void goto_programt::check_id_consistency_symbol_table(
761+
bool goto_programt::check_internal_invariants(
725762
const symbol_tablet &table,
726-
std::vector<std::string> &message_collector) const
763+
messaget &msg) const
727764
{
765+
bool found_violation = false;
728766
forall_goto_program_instructions(it, (*this))
729767
{
730-
it->check_id_consistency_symbol_table(table, message_collector);
768+
found_violation =
769+
found_violation || it->check_internal_invariants(table, msg);
731770
}
771+
return found_violation;
732772
}
733773

734774
/// Outputs a string representation of a `goto_program_instruction_typet`

src/goto-programs/goto_program.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Author: Daniel Kroening, [email protected]
2424
#include <util/source_location.h>
2525
#include <util/std_expr.h>
2626
#include <util/std_code.h>
27+
#include <util/message.h>
2728

2829
/// The type of an instruction in a GOTO program.
2930
enum goto_program_instruction_typet
@@ -402,10 +403,10 @@ class goto_programt
402403
/// Iterate over code and guard and collect inconsistencies with symbol
403404
/// table.
404405
/// \param table the symbol table
405-
/// \param message_collector container to store the error messages
406-
void check_id_consistency_symbol_table(
407-
const symbol_tablet &table,
408-
std::vector<std::string> &message_collector) const;
406+
/// \param msg container to store the error messages
407+
/// \return true if any violation was found
408+
bool
409+
check_internal_invariants(const symbol_tablet &table, messaget &msg) const;
409410
};
410411

411412
// Never try to change this to vector-we mutate the list while iterating
@@ -686,9 +687,8 @@ class goto_programt
686687
/// and relative jumps have the same distance.
687688
bool equals(const goto_programt &other) const;
688689

689-
void check_id_consistency_symbol_table(
690-
const symbol_tablet &table,
691-
std::vector<std::string> &message_collector) const;
690+
bool
691+
check_internal_invariants(const symbol_tablet &table, messaget &msg) const;
692692
};
693693

694694
/// Get control-flow successors of a given instruction. The instruction is

0 commit comments

Comments
 (0)