forked from SimonKagstrom/kcov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line2addr.cc
73 lines (57 loc) · 1.39 KB
/
line2addr.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <configuration.hh>
#include <file-parser.hh>
#include <utils.hh>
using namespace kcov;
const char *kcov_version = "";
class Listener : public IFileParser::ILineListener
{
public:
Listener(IFileParser &parser, const std::string &filePattern, int lineNr) :
m_filePattern(filePattern),
m_lineNr(lineNr)
{
parser.registerLineListener(*this);
}
virtual ~Listener()
{
}
void onLine(const std::string &file, unsigned int lineNr, uint64_t addr)
{
if (file.find(m_filePattern) != std::string::npos) {
if (m_lineNr < 0 || lineNr == (unsigned int)m_lineNr)
report(addr);
}
}
private:
void report(unsigned long addr)
{
printf("0x%lx\n", addr);
}
const std::string m_filePattern;
int m_lineNr;
};
int main(int argc, const char *argv[])
{
if (argc < 3) {
fprintf(stderr, "Usage: line2addr in-file file-pattern [line-nr]\n");
return 1;
}
std::string file(argv[1]);
std::string fileName(argv[2]);
int lineNr = -1;
if (argc >= 4) {
if (!string_is_integer(argv[3])) {
fprintf(stderr, "line-nr argument (%s) must be integer\n", argv[3]);
exit(1);
}
lineNr = string_to_integer(argv[3]);
}
IFileParser *parser = IParserManager::getInstance().matchParser(file);
if (!parser) {
fprintf(stderr, "Can't match parser for %s\n", file.c_str());
return 1;
}
Listener listener(*parser, fileName, lineNr);
parser->addFile(file);
return parser->parse() ? 0 : 1;
}