Commit e795ef75 authored by Bednárek David RNDr. Ph.D.'s avatar Bednárek David RNDr. Ph.D.
Browse files

-c for coverage counters

parent 5b6c905e
......@@ -69,9 +69,49 @@ namespace cecko {
extern err_def_n INCOMPATIBLE;
}
struct coverage_counter {
public:
coverage_counter()
: num(0)
{}
void inc()
{
++num;
}
int get() const
{
return num;
}
private:
int num;
};
struct coverage_data {
public:
void inc(std::string n)
{
auto rv = map_.try_emplace(std::move(n));
rv.first->second.inc();
}
template< typename F>
void for_each(F&& f) const
{
for (auto&& a : map_)
{
f(a.first, a.second);
}
}
private:
using map_t = std::map<std::string, coverage_counter>;
map_t map_;
};
class context : public CKContext {
public:
context(CKTablesObs tables, std::ostream* outp) : CKContext(tables), line_(1), outp_(outp) {}
context(CKTablesObs tables, std::ostream* outp, coverage_data * cd) : CKContext(tables), line_(1), outp_(outp), cd_(cd) {}
std::ostream& out() { return *outp_; }
......@@ -82,10 +122,18 @@ namespace cecko {
loc_t line() const { return line_; }
loc_t incline() { return line_++; } // returns line value before increment
void cov(std::string n)
{
cd_->inc(std::move(n));
}
private:
loc_t line_;
std::ostream * outp_;
coverage_data * cd_;
};
using context_obs = context*;
......
#include "ckmain.hpp"
#include <iomanip>
#include <iostream>
namespace cecko {
template< typename AR>
......@@ -63,6 +66,23 @@ namespace cecko {
{
out() << "========== tables ==========" << std::endl;
the_tables.dump_tables(out());
if (!covname.empty())
{
std::ofstream covf(covname);
cd_.for_each([&covf](auto&& name, auto&& cc) {
covf << std::setw(5) << cc.get() << "\t" << name << std::endl;
});
}
return true;
}
bool main_state_parser::dump_coverage() const
{
out() << "========== coverage ==========" << std::endl;
cd_.for_each([this](auto&& name, auto&& cc) {
out() << "#" << std::setw(5) << cc.get() << " " << name << std::endl;
});
return true;
}
......@@ -76,6 +96,11 @@ namespace cecko {
return false;
oname = get_val();
return true;
case 'c':
if (!covname.empty())
return false;
covname = get_val();
return true;
case 'z':
if (!!outp_owner_)
return false;
......
......@@ -25,13 +25,14 @@ namespace cecko {
class main_state_parser : public main_state_lexer {
public:
std::string input_fname;
std::string covname;
main_state_parser() : outp_(&std::cout) {}
template< class the_parser>
bool parse()
{
context ctx(&the_tables, &out());
context ctx(&the_tables, &out(), &cd_);
FILE* iff = fopen(input_fname.c_str(), "r");
if (iff == nullptr)
......@@ -52,10 +53,14 @@ namespace cecko {
bool dump_tables() const;
bool dump_coverage() const;
std::ostream& out() const { return *outp_; }
protected:
std::ostream* outp_;
std::unique_ptr< std::ofstream> outp_owner_;
coverage_data cd_;
};
class main_state_code : public main_state_parser {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment