84 std::map<goto_programt::const_targett, coverage_conditiont>
conditions;
96 rate(std::size_t covered, std::size_t total,
bool per_cent =
false)
98 std::ostringstream oss;
106 fraction =
static_cast<float>(covered) /
static_cast<float>(total);
109 oss << fraction * 100.0 <<
'%';
113 oss << covered <<
" of " << total;
120 rate_detailed(std::size_t covered, std::size_t total,
bool per_cent =
false)
122 std::ostringstream oss;
123 oss <<
rate(covered, total, per_cent) <<
" (" << covered <<
'/' << total
141 end_function->is_end_function(),
142 "last instruction in a function body is end function");
143 file_name = end_function->source_location.get_file();
172 for(
const auto &cov_line : coverage_lines_map)
178 if(cov_line.second.conditions.empty())
186 std::size_t number = 0, total_taken = 0;
187 for(
const auto &c : cov_line.second.conditions)
193 unsigned taken = c.second.false_taken + c.second.true_taken;
194 total_taken += taken;
199 "condition-coverage",
rate_detailed(total_taken, number * 2,
true));
212 it->source_location.is_nil() ||
213 it->source_location.get_file() !=
file_name || it->is_dead() ||
214 it->is_end_function())
217 const bool is_branch = it->is_goto() && !it->guard.is_constant();
221 std::pair<coverage_lines_mapt::iterator, bool> entry =
232 if(!entry.first->second.conditions.insert({it, coverage_conditiont()})
237 symex_coveraget::coveraget::const_iterator c_entry = coverage.find(it);
238 if(c_entry != coverage.end())
240 if(!(c_entry->second.size() == 1 || is_branch))
242 std::cerr << it->location_number <<
'\n';
243 for(
const auto &cov : c_entry->second)
244 std::cerr << cov.second.succ->location_number <<
'\n';
247 c_entry->second.size() == 1 || is_branch,
248 "instructions other than branch instructions have exactly 1 successor");
250 for(
const auto &cov : c_entry->second)
253 cov.second.num_executions > 0,
254 "coverage entries can only exist with at least one execution");
256 if(entry.first->second.hits == 0)
259 if(cov.second.num_executions > entry.first->second.hits)
260 entry.first->second.hits = cov.second.num_executions;
264 auto cond_entry = entry.first->second.conditions.find(it);
266 cond_entry != entry.first->second.conditions.end(),
267 "branch should have condition");
269 if(it->get_target() == cov.second.succ)
271 if(!cond_entry->second.false_taken)
273 cond_entry->second.false_taken =
true;
279 if(!cond_entry->second.true_taken)
281 cond_entry->second.true_taken =
true;
295 typedef std::map<irep_idt, coverage_recordt> file_recordst;
296 file_recordst file_records;
301 !gf_entry.second.body_available() ||
309 ns, gf_entry.first, gf_entry.second.body,
coverage);
311 std::pair<file_recordst::iterator, bool> entry = file_records.insert(
325 for(xmlt::elementst::const_iterator it =
327 it != func_cov.
xml.
elements.front().elements.end();
342 for(file_recordst::const_iterator it = file_records.begin();
343 it != file_records.end();
370 xmlt &xml_coverage)
const
375 std::string overall_line_rate_str =
377 std::string overall_branch_rate_str =
380 auto now = std::chrono::system_clock::now();
381 auto current_time = std::chrono::time_point_cast<std::chrono::seconds>(now);
382 std::time_t tt = std::chrono::system_clock::to_time_t(current_time);
388 xml_coverage.
set_attribute(
"line-rate", overall_line_rate_str);
389 xml_coverage.
set_attribute(
"branch-rate", overall_branch_rate_str);
405 xmlt &
package = packages.new_element(overall_cov.xml);
407 package.set_attribute(
"line-rate", overall_line_rate_str);
408 package.set_attribute(
"branch-rate", overall_branch_rate_str);
409 package.set_attribute(
"complexity",
"0.0");
414 std::ostream &os)
const
416 xmlt xml_coverage(
"coverage");
419 os <<
"<?xml version=\"1.0\"?>\n";
420 os <<
"<!DOCTYPE coverage SYSTEM \""
421 <<
"http://cobertura.sourceforge.net/xml/coverage-04.dtd\">\n";
429 const std::string &path)
const
437 std::ofstream out(path.c_str());
std::size_t lines_covered
std::size_t branches_total
std::size_t branches_covered
coverage_recordt(const std::string &node_id)
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
A collection of goto functions.
function_mapt function_map
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
void compute_coverage_lines(const goto_programt &goto_program, const symex_coveraget::coveraget &coverage, coverage_lines_mapt &dest)
std::map< unsigned, coverage_linet > coverage_lines_mapt
goto_program_coverage_recordt(const namespacet &ns, const irep_idt &function_id, const goto_programt &goto_program, const symex_coveraget::coveraget &coverage)
const irep_idt & get_file() const
A generic container class for the GOTO intermediate representation of one function.
instructionst instructions
The list of instructions in the goto program.
instructionst::const_iterator const_targett
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
void compute_overall_coverage(const goto_functionst &goto_functions, coverage_recordt &dest) const
bool generate_report(const goto_functionst &goto_functions, const std::string &path) const
std::map< goto_programt::const_targett, coverage_innert > coveraget
bool output_report(const goto_functionst &goto_functions, std::ostream &os) const
void build_cobertura(const goto_functionst &goto_functions, xmlt &xml_coverage) const
void set_attribute(const std::string &attribute, unsigned value)
xmlt & new_element(const std::string &key)
Goto Programs with Functions.
#define forall_goto_program_instructions(it, program)
const std::string & id2string(const irep_idt &d)
std::string from_type(const namespacet &ns, const irep_idt &identifier, const typet &type)
#define UNREACHABLE
This should be used to mark dead code.
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
#define PRECONDITION(CONDITION)
#define INITIALIZE_FUNCTION
unsigned safe_string2unsigned(const std::string &str, int base)
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
std::map< goto_programt::const_targett, coverage_conditiont > conditions
static std::string rate(std::size_t covered, std::size_t total, bool per_cent=false)
static std::string rate_detailed(std::size_t covered, std::size_t total, bool per_cent=false)
Record and print code coverage of symbolic execution.