100 rate(std::size_t covered, std::size_t total,
bool per_cent =
false)
102 std::ostringstream oss;
110 fraction =
static_cast<float>(covered) /
static_cast<float>(total);
113 oss << fraction * 100.0 <<
'%';
117 oss << covered <<
" of " << total;
124 rate_detailed(std::size_t covered, std::size_t total,
bool per_cent =
false)
126 std::ostringstream oss;
127 oss <<
rate(covered, total, per_cent) <<
" (" << covered <<
'/' << total
145 end_function->is_end_function(),
146 "last instruction in a function body is end function");
147 file_name = end_function->source_location().get_file();
176 for(
const auto &cov_line : coverage_lines_map)
182 if(cov_line.second.conditions.empty())
190 std::size_t number = 0, total_taken = 0;
191 for(
const auto &c : cov_line.second.conditions)
197 unsigned taken = c.second.false_taken + c.second.true_taken;
198 total_taken += taken;
203 "condition-coverage",
rate_detailed(total_taken, number * 2,
true));
216 it->source_location().is_nil() ||
217 it->source_location().get_file() !=
file_name || it->is_dead() ||
218 it->is_end_function())
221 const bool is_branch = it->is_goto() && !it->condition().is_constant();
225 std::pair<coverage_lines_mapt::iterator, bool> entry =
236 if(!entry.first->second.conditions.insert({it, coverage_conditiont()})
241 symex_coveraget::coveraget::const_iterator c_entry = coverage.find(it);
242 if(c_entry != coverage.end())
245 c_entry->second.size() == 1 || is_branch || it->is_function_call(),
246 "instructions other than branch instructions or function calls have "
247 "exactly 1 successor",
248 "found at goto program instruction number " +
251 for(
const auto &cov : c_entry->second)
254 cov.second.num_executions > 0,
255 "coverage entries can only exist with at least one execution");
257 if(entry.first->second.hits == 0)
260 if(cov.second.num_executions > entry.first->second.hits)
261 entry.first->second.hits = cov.second.num_executions;
265 auto cond_entry = entry.first->second.conditions.find(it);
267 cond_entry != entry.first->second.conditions.end(),
268 "branch should have condition");
270 if(it->get_target() == cov.second.succ)
272 if(!cond_entry->second.false_taken)
274 cond_entry->second.false_taken =
true;
280 if(!cond_entry->second.true_taken)
282 cond_entry->second.true_taken =
true;
296 typedef std::map<irep_idt, coverage_recordt> file_recordst;
297 file_recordst file_records;
302 !gf_entry.second.body_available() ||
310 ns, gf_entry.first, gf_entry.second.body,
coverage);
312 std::pair<file_recordst::iterator, bool> entry = file_records.insert(
326 for(xmlt::elementst::const_iterator it =
328 it != func_cov.
xml.
elements.front().elements.end();
343 for(file_recordst::const_iterator it = file_records.begin();
344 it != file_records.end();
371 xmlt &xml_coverage)
const
376 std::string overall_line_rate_str =
378 std::string overall_branch_rate_str =
381 auto now = std::chrono::system_clock::now();
382 auto current_time = std::chrono::time_point_cast<std::chrono::seconds>(now);
383 std::time_t tt = std::chrono::system_clock::to_time_t(current_time);
389 xml_coverage.
set_attribute(
"line-rate", overall_line_rate_str);
390 xml_coverage.
set_attribute(
"branch-rate", overall_branch_rate_str);
406 xmlt &
package = packages.new_element(overall_cov.xml);
408 package.set_attribute(
"line-rate", overall_line_rate_str);
409 package.set_attribute(
"branch-rate", overall_branch_rate_str);
410 package.set_attribute(
"complexity",
"0.0");
415 std::ostream &os)
const
417 xmlt xml_coverage(
"coverage");
420 os <<
"<?xml version=\"1.0\"?>\n";
421 os <<
"<!DOCTYPE coverage SYSTEM \""
422 <<
"http://cobertura.sourceforge.net/xml/coverage-04.dtd\">\n";
430 const std::string &path)
const
438 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
bool output_report(const goto_functionst &goto_functions, std::ostream &os) const
void build_cobertura(const goto_functionst &goto_functions, xmlt &xml_coverage) const
std::map< goto_programt::const_targett, coverage_innert, goto_programt::target_less_than > coveraget
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 DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
#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, goto_programt::target_less_than > conditions
A total order over targett and const_targett.
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.